forked from longtao.wu/droboticflow
141 lines
7.7 KiB
Go
141 lines
7.7 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
func TestSliceEditBoundaryArchivesOnlyNewlyDiscardedActions(t *testing.T) {
|
|
for _, choice := range []string{"truncate", "move", "invalidate"} {
|
|
t.Run(choice, func(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
reason := dispositionFixturePayload(t, f)
|
|
archive := domain.AnnotationSegment{ID: "old", Start: 0, End: 5, Status: "described", Description: "旧描述"}
|
|
before := []domain.SliceSegment{{ID: "d", Start: 0, End: 5, DeliveryDisposition: "discard", DiscardReason: "相机被遮挡", DiscardReasonLabelID: reason.ReasonLabelID, DiscardReasonVersion: reason.ReasonVersion, DiscardedAnnotations: []domain.AnnotationSegment{archive}}, {ID: "k", Name: "测试切片", Start: 5, End: 10}}
|
|
original := []domain.AnnotationSegment{{ID: "a", Start: 5, End: 10, Status: "described", Description: "取放商品"}}
|
|
item := f.item
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
|
|
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": before, "segments": original}
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
after := append([]domain.SliceSegment(nil), before...)
|
|
after[0].End, after[1].Start = 6, 6
|
|
after[0].DiscardedAnnotations = []domain.AnnotationSegment{{ID: "forged", Start: 0, End: 6, Description: "伪造归档"}}
|
|
actions := []domain.AnnotationSegment{{ID: "left", Start: 5, End: 6, Status: "described", Description: "取放商品"}, {ID: "right", Start: 6, End: 10, Status: "described", Description: "取放商品"}}
|
|
if choice != "truncate" {
|
|
actions[1].Status, actions[1].IsInvalid, actions[1].Description = "invalid", true, "无效片段"
|
|
}
|
|
if choice == "invalidate" {
|
|
actions[0].Status, actions[0].IsInvalid, actions[0].Description = "invalid", true, "无效片段"
|
|
}
|
|
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/slice-edit-applications"
|
|
input := sliceEditApplicationInput{ApplicationID: "boundary", combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: item.Version, SliceSegments: after, Segments: actions}}
|
|
r := f.request(http.MethodPost, endpoint, f.annotator, input)
|
|
if r.Code != 200 {
|
|
t.Fatalf("%d %s", r.Code, r.Body.String())
|
|
}
|
|
var got struct {
|
|
Item domain.HumanItemRecord `json:"item"`
|
|
Workspace combinedAnnotationDraftInput `json:"workspace"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got.Workspace.Segments) != 1 || !reflect.DeepEqual(got.Workspace.Segments[0].Description, actions[1].Description) {
|
|
t.Fatalf("kept action lost: %+v", got.Workspace.Segments)
|
|
}
|
|
archived := got.Workspace.SliceSegments[0].DiscardedAnnotations
|
|
if len(archived) != 2 || !reflect.DeepEqual(archived[0], archive) || archived[1].Description != actions[0].Description {
|
|
t.Fatalf("archive lost or forged: %+v", archived)
|
|
}
|
|
// Read persisted evidence through a fresh handler, then undo and redo.
|
|
f.handler = NewHandler(f.store, nil)
|
|
input.ApplicationID, input.Version, input.SliceSegments, input.Segments = "undo", got.Item.Version, before, original
|
|
r = f.request(http.MethodPost, endpoint, f.annotator, input)
|
|
if r.Code != 200 {
|
|
t.Fatalf("undo: %d %s", r.Code, r.Body.String())
|
|
}
|
|
json.Unmarshal(r.Body.Bytes(), &got)
|
|
input.ApplicationID, input.Version, input.SliceSegments, input.Segments = "redo", got.Item.Version, after, actions
|
|
r = f.request(http.MethodPost, endpoint, f.annotator, input)
|
|
if r.Code != 200 {
|
|
t.Fatalf("redo: %d %s", r.Code, r.Body.String())
|
|
}
|
|
json.Unmarshal(r.Body.Bytes(), &got)
|
|
if len(got.Workspace.SliceSegments[0].DiscardedAnnotations) != 2 {
|
|
t.Fatal("redo duplicated archive")
|
|
}
|
|
if r := f.request(http.MethodPost, endpoint, f.annotator, input); r.Code != 409 {
|
|
t.Fatalf("stale application accepted: %d", r.Code)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBoundaryArchiveDoesNotAcceptActionsInPreviouslyDiscardedArea(t *testing.T) {
|
|
item := domain.HumanItemRecord{Subject: domain.HumanSubjectRef{Payload: map[string]any{}}}
|
|
before := []domain.SliceSegment{{ID: "d", Start: 0, End: 5, DeliveryDisposition: "discard"}}
|
|
after := []domain.SliceSegment{{ID: "d", Start: 0, End: 6, DeliveryDisposition: "discard"}}
|
|
bad := []domain.AnnotationSegment{{ID: "forged", Start: 1, End: 2, Description: "伪造"}}
|
|
if got := archiveSliceEditBoundaryActions(&item, before, after, bad); !reflect.DeepEqual(got, bad) || len(after[0].DiscardedAnnotations) != 0 {
|
|
t.Fatal("old discarded area bypassed validation")
|
|
}
|
|
}
|
|
|
|
func TestBoundaryArchiveHandlesLeftExpansionAndInheritedChildren(t *testing.T) {
|
|
for _, child := range []bool{false, true} {
|
|
item := domain.HumanItemRecord{Subject: domain.HumanSubjectRef{Payload: map[string]any{}}}
|
|
before := []domain.SliceSegment{{ID: "d", Start: 5, End: 10, DeliveryDisposition: "discard"}}
|
|
after := []domain.SliceSegment{{ID: "d", Start: 4, End: 10, DeliveryDisposition: "discard"}}
|
|
if child {
|
|
after[0].ID, after[0].SourceIDs = "child", []string{"d"}
|
|
}
|
|
incoming := domain.AnnotationSegment{ID: "incoming", Start: 4, End: 5, Status: "described", Description: "新纳入区域"}
|
|
kept := domain.AnnotationSegment{ID: "kept", Start: 0, End: 4, Status: "described", Description: "保留内容"}
|
|
got := archiveSliceEditBoundaryActions(&item, before, after, []domain.AnnotationSegment{kept, incoming})
|
|
if !reflect.DeepEqual(got, []domain.AnnotationSegment{kept}) || !reflect.DeepEqual(after[0].DiscardedAnnotations, []domain.AnnotationSegment{incoming}) {
|
|
t.Fatalf("left expansion/child=%v: %+v %+v", child, got, after)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBoundaryArchiveFailedApplicationPreservesSavedEvidence(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
reason := dispositionFixturePayload(t, f)
|
|
before := []domain.SliceSegment{{ID: "d", Start: 0, End: 5, DeliveryDisposition: "discard", DiscardReason: "相机被遮挡", DiscardReasonLabelID: reason.ReasonLabelID, DiscardReasonVersion: reason.ReasonVersion}, {ID: "k", Name: "测试切片", Start: 5, End: 10}}
|
|
item := f.item
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
|
|
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": before, "segments": []domain.AnnotationSegment{{ID: "a", Start: 5, End: 10, Status: "described", Description: "原描述"}}}
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
saved, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
after := append([]domain.SliceSegment(nil), before...)
|
|
after[0].End, after[1].Start = 6, 6
|
|
actions := []domain.AnnotationSegment{{ID: "left", Start: 5, End: 6, Status: "described", Description: "原描述"}, {ID: "right", Start: 6, End: 10, Status: "described", Description: "原描述"}}
|
|
for _, index := range []int{0, 1} {
|
|
for _, field := range []string{"status", "label"} {
|
|
invalid := append([]domain.AnnotationSegment(nil), actions...)
|
|
if field == "status" {
|
|
invalid[index].Status = "unknown"
|
|
} else {
|
|
invalid[index].LabelID = "not-published"
|
|
}
|
|
input := sliceEditApplicationInput{ApplicationID: "invalid-input", combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: item.Version, SliceSegments: after, Segments: invalid}}
|
|
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-edit-applications", f.annotator, input)
|
|
if r.Code != 400 {
|
|
t.Fatalf("invalid %s at %d accepted: %d %s", field, index, r.Code, r.Body.String())
|
|
}
|
|
current, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
if !reflect.DeepEqual(saved, current) {
|
|
t.Fatal("failed application changed persisted archive or recovery evidence")
|
|
}
|
|
}
|
|
}
|
|
}
|