204 lines
12 KiB
Go
204 lines
12 KiB
Go
package api
|
|
|
|
import (
|
|
"droboticflow/internal/domain"
|
|
"encoding/json"
|
|
"net/http"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestSliceEditApplicationDiscardsAndRestoresWholeSliceAtomically(t *testing.T) {
|
|
for _, rework := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "first-pass", true: "rework"}[rework], func(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
reason := dispositionFixturePayload(t, f)
|
|
before := []domain.SliceSegment{{ID: "a", Name: "测试切片", Start: 0, End: 5}, {ID: "b", Name: "测试切片", Start: 5, End: 10}}
|
|
actions := []domain.AnnotationSegment{{ID: "pending", Start: 0, End: 2, Status: "pending"}, {ID: "described", Start: 2, End: 4, Status: "described", Description: "synthetic action"}, {ID: "invalid", Start: 4, End: 5, Status: "invalid", IsInvalid: true, Description: "无效片段"}, {ID: "other", Start: 5, End: 10, Status: "pending"}}
|
|
item := f.item
|
|
item.ReviewRound = 0
|
|
if rework {
|
|
item.ReviewRound = 1
|
|
}
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
|
|
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": before, "segments": actions}
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
proposed := append([]domain.SliceSegment(nil), before...)
|
|
proposed[0].DeliveryDisposition = "discard"
|
|
proposed[0].DiscardReason = "相机被遮挡"
|
|
proposed[0].DiscardedByUserID = "forged"
|
|
cmd := sliceDispositionInput{SliceID: "a", Disposition: "discard", ReasonLabelID: reason.ReasonLabelID, ReasonVersion: reason.ReasonVersion}
|
|
input := sliceEditApplicationInput{ApplicationID: "application-1", combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: item.Version, SliceSegments: proposed, Segments: actions}, Dispositions: []sliceDispositionInput{cmd}}
|
|
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/slice-edit-applications"
|
|
r := f.request(http.MethodPost, endpoint, f.annotator, input)
|
|
if r.Code != 200 {
|
|
t.Fatalf("discard %d %s", r.Code, r.Body.String())
|
|
}
|
|
var got struct {
|
|
Item domain.HumanItemRecord `json:"item"`
|
|
Workspace combinedAnnotationDraftInput `json:"workspace"`
|
|
}
|
|
json.Unmarshal(r.Body.Bytes(), &got)
|
|
if got.Item.Version != item.Version+1 || len(got.Workspace.Segments) != 1 || len(got.Workspace.SliceSegments[0].DiscardedAnnotations) != 3 || got.Workspace.SliceSegments[0].DiscardedByUserID != f.annotator.ID {
|
|
t.Fatalf("not atomic/archive-safe: %#v", got.Workspace)
|
|
}
|
|
// A fresh handler reads the persisted state; no process-local success illusion.
|
|
f.handler = NewHandler(f.store, nil)
|
|
saved, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
if saved.Version != got.Item.Version || !decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(saved)["sliceSegments"])[0].IsDiscarded() {
|
|
t.Fatal("discard not persisted")
|
|
}
|
|
if stale := f.request(http.MethodPost, endpoint, f.annotator, input); stale.Code != 409 {
|
|
t.Fatalf("stale accepted: %d", stale.Code)
|
|
}
|
|
restore := sliceEditApplicationInput{ApplicationID: "application-2", combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: got.Item.Version, SliceSegments: before, Segments: actions}, Dispositions: []sliceDispositionInput{{SliceID: "a", Disposition: "keep"}}}
|
|
r = f.request(http.MethodPost, endpoint, f.annotator, restore)
|
|
if r.Code != 200 {
|
|
t.Fatalf("restore %d %s", r.Code, r.Body.String())
|
|
}
|
|
json.Unmarshal(r.Body.Bytes(), &got)
|
|
if len(got.Workspace.Segments) != 4 || got.Workspace.SliceSegments[0].IsDiscarded() {
|
|
t.Fatal("restore lost original actions")
|
|
}
|
|
// Redo uses the persisted discarded snapshot, which has no active
|
|
// actions for a. Its archive must still contain all three originals.
|
|
redo := sliceEditApplicationInput{ApplicationID: "redo", combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: got.Item.Version, SliceSegments: proposed, Segments: actions[3:]}, Dispositions: []sliceDispositionInput{cmd}}
|
|
r = f.request("POST", endpoint, f.annotator, redo)
|
|
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) != 3 {
|
|
t.Fatalf("redo dropped archive: %+v", got.Workspace.SliceSegments[0])
|
|
}
|
|
restore.Version = got.Item.Version
|
|
r = f.request("POST", endpoint, f.annotator, restore)
|
|
if r.Code != 200 {
|
|
t.Fatalf("restore after redo %d %s", r.Code, r.Body.String())
|
|
}
|
|
json.Unmarshal(r.Body.Bytes(), &got)
|
|
if len(got.Workspace.Segments) != 4 {
|
|
t.Fatal("redo/restore lost actions")
|
|
}
|
|
// Failure of the second command must not leave the first one applied.
|
|
original, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
input.Version = got.Item.Version
|
|
input.ApplicationID = "application-fail"
|
|
input.Dispositions = append(input.Dispositions, sliceDispositionInput{SliceID: "b", Disposition: "discard", ReasonLabelID: "missing", ReasonVersion: 1})
|
|
r = f.request(http.MethodPost, endpoint, f.annotator, input)
|
|
if r.Code == 200 {
|
|
t.Fatal("invalid dictionary accepted")
|
|
}
|
|
unchanged, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
if !reflect.DeepEqual(original, unchanged) {
|
|
t.Fatal("partial batch survived a failed command")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSliceEditApplicationRejectsUncommandedPolicyAndProcessing(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
input := sliceEditApplicationInput{ApplicationID: "bad", combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: f.item.Version, SliceSegments: []domain.SliceSegment{{ID: "s", Start: 0, End: 10, DeliveryDisposition: "discard", DiscardReason: "forged"}}}}
|
|
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/slice-edit-applications"
|
|
r := f.request("POST", endpoint, f.annotator, input)
|
|
if r.Code == 200 {
|
|
t.Fatal("uncommanded discard accepted")
|
|
}
|
|
item := f.item
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "processing", "phase": "materializing"}
|
|
f.store.UpsertHumanItem(f.ctx, item)
|
|
r = f.request("POST", endpoint, f.annotator, input)
|
|
if r.Code != 409 {
|
|
t.Fatalf("processing %d", r.Code)
|
|
}
|
|
}
|
|
|
|
// Exercise the panel's split/discard followed by persisted undo and redo. The
|
|
// removed discarded child must be restored explicitly before its parent rejoins.
|
|
func TestSliceEditApplicationSplitDiscardUndoRedoAndSubmit(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
reason := dispositionFixturePayload(t, f)
|
|
seedCompletedMaterialization(t, f, 1)
|
|
original := []domain.SliceSegment{{ID: "parent", Name: "测试切片", Start: 0, End: 10}}
|
|
actions := []domain.AnnotationSegment{{ID: "left", Start: 0, End: 5, Status: "pending"}, {ID: "right", Start: 5, End: 10, Status: "pending"}}
|
|
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
|
|
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": original, "segments": actions}
|
|
f.store.UpsertHumanItem(f.ctx, *item)
|
|
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID
|
|
split := []domain.SliceSegment{{ID: "child-left", SourceIDs: []string{"parent"}, Start: 0, End: 5, DeliveryDisposition: "discard", DiscardReason: "相机被遮挡"}, {ID: "child-right", SourceIDs: []string{"parent"}, Name: "测试切片", Start: 5, End: 10}}
|
|
call := func(id string, slices []domain.SliceSegment, segments []domain.AnnotationSegment, commands []sliceDispositionInput) combinedAnnotationDraftInput {
|
|
t.Helper()
|
|
saved, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
r := f.request("POST", endpoint+"/slice-edit-applications", f.annotator, sliceEditApplicationInput{ApplicationID: id, combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: saved.Version, SliceSegments: slices, Segments: segments}, Dispositions: commands})
|
|
if r.Code != 200 {
|
|
t.Fatalf("%s %d %s", id, r.Code, r.Body.String())
|
|
}
|
|
var got struct {
|
|
Workspace combinedAnnotationDraftInput `json:"workspace"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return got.Workspace
|
|
}
|
|
discard := []sliceDispositionInput{{SliceID: "child-left", Disposition: "discard", ReasonLabelID: reason.ReasonLabelID, ReasonVersion: reason.ReasonVersion}}
|
|
got := call("split", split, actions, discard)
|
|
if len(got.Segments) != 1 || len(got.SliceSegments[0].DiscardedAnnotations) != 1 {
|
|
t.Fatalf("split lost evidence: %+v", got)
|
|
}
|
|
got = call("undo", original, actions, []sliceDispositionInput{{SliceID: "child-left", Disposition: "keep"}})
|
|
if len(got.SliceSegments) != 1 || len(got.Segments) != 2 || got.SliceSegments[0].IsDiscarded() {
|
|
t.Fatalf("undo lost actions: %+v", got)
|
|
}
|
|
got = call("redo", split, actions, discard)
|
|
if len(got.Segments) != 1 || len(got.SliceSegments[0].DiscardedAnnotations) != 1 {
|
|
t.Fatal("redo failed")
|
|
}
|
|
// Discard the remaining half; all-discarded submissions require no active actions.
|
|
all := append([]domain.SliceSegment(nil), got.SliceSegments...)
|
|
all[1].DeliveryDisposition = "discard"
|
|
got = call("discard-rest", all, got.Segments, []sliceDispositionInput{{SliceID: "child-right", Disposition: "discard", ReasonLabelID: reason.ReasonLabelID, ReasonVersion: reason.ReasonVersion}})
|
|
saved, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
got.Version = saved.Version
|
|
if r := f.request("PUT", endpoint+"/drafts", f.annotator, got); r.Code != 200 {
|
|
t.Fatalf("save progress %d %s", r.Code, r.Body.String())
|
|
}
|
|
saved, _ = f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
got.Version = saved.Version
|
|
r := f.request("POST", endpoint+"/combined-submissions", f.annotator, got)
|
|
if r.Code != 201 && r.Code != 202 {
|
|
t.Fatalf("submit discarded %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestSliceEditApplicationMergeAndUndoPreserveDiscardedSourceEvidence(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
reason := dispositionFixturePayload(t, f)
|
|
slices := []domain.SliceSegment{{ID: "a", Name: "测试切片", Start: 0, End: 5}, {ID: "b", Start: 5, End: 10, DeliveryDisposition: "discard", DiscardReason: "相机被遮挡", DiscardReasonLabelID: reason.ReasonLabelID, DiscardReasonVersion: reason.ReasonVersion, DiscardedAnnotations: []domain.AnnotationSegment{{ID: "archive", Start: 5, End: 10, Status: "invalid", IsInvalid: true}}}}
|
|
actions := []domain.AnnotationSegment{{ID: "active", Start: 0, End: 5, Status: "pending"}}
|
|
item := f.item
|
|
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": slices, "segments": actions}
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
|
|
f.store.UpsertHumanItem(f.ctx, item)
|
|
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/slice-edit-applications"
|
|
merged := []domain.SliceSegment{{ID: "a", Start: 0, End: 10, SourceIDs: []string{"a", "b"}}}
|
|
r := f.request("POST", endpoint, f.annotator, sliceEditApplicationInput{ApplicationID: "merge", combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: item.Version, SliceSegments: merged}, Dispositions: []sliceDispositionInput{{SliceID: "b", Disposition: "keep"}}})
|
|
if r.Code != 200 {
|
|
t.Fatalf("merge %d %s", r.Code, r.Body.String())
|
|
}
|
|
saved, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
r = f.request("POST", endpoint, f.annotator, sliceEditApplicationInput{ApplicationID: "undo-merge", combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: saved.Version, SliceSegments: slices, Segments: actions}, Dispositions: []sliceDispositionInput{{SliceID: "b", Disposition: "discard", ReasonLabelID: reason.ReasonLabelID, ReasonVersion: reason.ReasonVersion}}})
|
|
if r.Code != 200 {
|
|
t.Fatalf("undo merge %d %s", r.Code, r.Body.String())
|
|
}
|
|
saved, _ = f.store.GetHumanItem(f.ctx, item.ID)
|
|
got := decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(saved)["sliceSegments"])
|
|
if len(got) != 2 || len(got[1].DiscardedAnnotations) != 1 || got[1].DiscardedAnnotations[0].ID != "archive" {
|
|
t.Fatalf("merge undo lost original archive: %+v", got)
|
|
}
|
|
}
|