package api import ( "encoding/json" "net/http" "strings" "testing" "droboticflow/internal/domain" ) func TestSliceDispositionNamesAreExclusiveAndUndoRestoresSavedName(t *testing.T) { f := sliceRestorationFixture(t) body := dispositionFixturePayload(t, f) body.SliceSegments[1].Name = "已命名切片" path := "/api/plugins/annotation-qa/tasks/" + f.task.ID save := func(payload any) combinedAnnotationDraftInput { t.Helper() r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, payload) if r.Code != 200 { t.Fatalf("disposition: %d %s", r.Code, r.Body.String()) } item, workspace := readSliceRestoration(t, f) body.combinedAnnotationDraftInput = workspace body.Version = item.Version return workspace } saved := save(body) if saved.SliceSegments[1].Name != "" { t.Fatal("discard retained a normal slice name") } body.Disposition = "keep" saved = save(body) if saved.SliceSegments[1].Name != "" { t.Fatal("ordinary restore retained a normal slice name") } body.SliceSegments[1].Name = "重命名切片" body.Disposition = "discard" save(body) body.Disposition = "keep" raw, _ := json.Marshal(body) var replay map[string]any json.Unmarshal(raw, &replay) replay["restorePreviousName"] = true saved = save(replay) if saved.SliceSegments[1].Name != "重命名切片" { t.Fatalf("undo lost trusted name: %+v", saved.SliceSegments[1]) } if len(saved.Segments) != 1 || saved.Segments[0].Description != "old text" { t.Fatal("undo lost trusted action evidence") } } func TestSliceResetKeepsPartitionWithoutRevivingArchivedActions(t *testing.T) { f := sliceRestorationFixture(t) body := dispositionFixturePayload(t, f) path := "/api/plugins/annotation-qa/tasks/" + f.task.ID r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body) if r.Code != 200 { t.Fatal(r.Body.String()) } item, prior := readSliceRestoration(t, f) item.Subject.Payload["sliceWorkflow"] = map[string]any{"phase": "annotate", "status": "completed", "revision": 2} if err := f.store.UpsertHumanItem(f.ctx, item); err != nil { t.Fatal(err) } // Ticket 06:首标任务经真实重切写路径产出复位状态;档案分离不变量直接钉在 // 端点产物上(退役期曾用 legacy 状态模拟)。 r = f.request(http.MethodPost, path+"/slice-reset", f.annotator, map[string]any{"version": item.Version, "resetToUnifiedRange": false}) if r.Code != http.StatusOK { t.Fatalf("first-pass reset: %d %s", r.Code, r.Body.String()) } item, workspace := readSliceRestoration(t, f) if len(workspace.SliceSegments) != 3 || !workspace.SliceSegments[1].IsDiscarded() || len(workspace.Segments) != 0 { t.Fatal("reset did not retain only outer partition") } if len(workspace.SliceSegments[1].DiscardedAnnotations) != 0 { t.Fatal("reset leaves action archive live") } if workspace.SliceSegments[1].DiscardReason != prior.SliceSegments[1].DiscardReason { t.Fatal("reset lost discard reason") } body.combinedAnnotationDraftInput = workspace body.Version = item.Version body.Disposition = "keep" r = f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body) if r.Code != 200 { t.Fatalf("restore after reset: %d %s", r.Code, r.Body.String()) } item, workspace = readSliceRestoration(t, f) if len(workspace.Segments) != 1 { t.Fatal("restore after reset must create one pending interval") } pending := workspace.Segments[0] if pending.Start != 3 || pending.End != 7 || pending.Status != "pending" || pending.Description != "" || pending.IsInvalid { t.Fatalf("reset revived archived judgment instead of a pending interval: %+v", pending) } history, _ := item.Subject.Payload["sliceDispositionHistory"].([]any) if len(history) < 2 { t.Fatal("reset deleted audit history") } // 送检历史后同端点 409,且不再追加 reset 历史。 reworkTask := f.task reworkTask.Status = domain.AnnotationTaskStatusRework if err := f.store.UpsertAnnotationTask(f.ctx, reworkTask); err != nil { t.Fatal(err) } resetHistoryBefore, _ := normalizeStructuredValue(item.Subject.Payload["sliceResetHistory"]).([]any) r = f.request(http.MethodPost, path+"/slice-reset", f.annotator, map[string]any{"version": item.Version, "resetToUnifiedRange": false}) if r.Code != http.StatusConflict || !strings.Contains(r.Body.String(), "slice_reset_disabled") { t.Fatalf("reset must refuse with guidance: %d %s", r.Code, r.Body.String()) } item, _ = readSliceRestoration(t, f) resetHistoryAfter, _ := normalizeStructuredValue(item.Subject.Payload["sliceResetHistory"]).([]any) if len(resetHistoryAfter) != len(resetHistoryBefore) { t.Fatalf("refused reset appended history: %#v", resetHistoryAfter) } } func TestSliceCancelMergeClearsJudgmentsAndPersistedUndoRestoresEvidence(t *testing.T) { f := sliceRestorationFixture(t) body := dispositionFixturePayload(t, f) path := "/api/plugins/annotation-qa/tasks/" + f.task.ID r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body) if r.Code != 200 { t.Fatal(r.Body.String()) } item, before := readSliceRestoration(t, f) merged := combinedAnnotationDraftInput{Version: item.Version, SliceSegments: []domain.SliceSegment{before.SliceSegments[0], {ID: "c", Start: 3, End: 10, DeliveryDisposition: "keep", SourceIDs: []string{"b", "c"}}}, Segments: []domain.AnnotationSegment{}} for _, snapshot := range []combinedAnnotationDraftInput{merged, before, merged, before} { item, _ = readSliceRestoration(t, f) snapshot.Version = item.Version r = f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, snapshot) if r.Code != 200 { t.Fatalf("save merge/undo: %d %s", r.Code, r.Body.String()) } _, saved := readSliceRestoration(t, f) if len(snapshot.SliceSegments) == 2 { if saved.SliceSegments[1].IsDiscarded() || saved.SliceSegments[1].Name != "" || saved.SliceSegments[1].DiscardReason != "" || len(saved.SliceSegments[1].DiscardedAnnotations) != 0 { t.Fatal("merge kept prior judgment") } } else if !saved.SliceSegments[1].IsDiscarded() || saved.SliceSegments[1].DiscardedByUserID != f.annotator.ID || len(saved.SliceSegments[1].DiscardedAnnotations) != 1 { t.Fatal("persisted undo lost trusted discarded evidence") } } } func TestSliceResetAllSplitsFirstPassFromSubmissionHistory(t *testing.T) { // Ticket 06:首标「全部重置」塌缩成全时长一片并留痕;送检历史后同端点 409 零改写。 f, _ := annotationProcessorFixture(t) body := dispositionFixturePayload(t, f) body.SliceSegments = []domain.SliceSegment{{ID: "a", Name: "测试切片", Start: 0, End: 2}, {ID: "b", Start: 2, End: 4}} body.Segments = []domain.AnnotationSegment{{ID: "old", Start: 2, End: 4, Description: "cleared action"}} path := "/api/plugins/annotation-qa/tasks/" + f.task.ID r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body) if r.Code != 200 { t.Fatal(r.Body.String()) } item, _ := readSliceRestoration(t, f) item.Subject.Payload["sliceWorkflow"] = map[string]any{"phase": "annotate", "status": "completed", "revision": 2} if err := f.store.UpsertHumanItem(f.ctx, item); err != nil { t.Fatal(err) } r = f.request(http.MethodPost, path+"/slice-reset", f.annotator, map[string]any{"version": item.Version, "resetToUnifiedRange": true}) if r.Code != http.StatusOK { t.Fatalf("first-pass reset all: %d %s", r.Code, r.Body.String()) } saved, workspace := readSliceRestoration(t, f) if len(workspace.SliceSegments) != 1 || workspace.SliceSegments[0].Start != 0 || workspace.SliceSegments[0].End != 4 || workspace.SliceSegments[0].Name != "" || workspace.SliceSegments[0].IsDiscarded() || len(workspace.Segments) != 0 { t.Fatalf("reset did not restore full range: %+v", workspace.SliceSegments) } workflow := mapValue(saved.Subject.Payload["sliceWorkflow"]) if intValue(workflow["resetFromVersion"]) != item.Version || workflow["resetToUnifiedRange"] != true { t.Fatal("reset outcome lacks reconciliation identity") } history, _ := saved.Subject.Payload["sliceResetHistory"].([]any) if len(history) != 1 { t.Fatal("reset dropped historical workspace evidence") } // A stale retry is a plain CAS conflict, not a silent success. r = f.request(http.MethodPost, path+"/slice-reset", f.annotator, map[string]any{"version": item.Version, "resetToUnifiedRange": true}) if r.Code != http.StatusConflict || !strings.Contains(r.Body.String(), "version_conflict") { t.Fatalf("stale reset accepted silently: %d %s", r.Code, r.Body.String()) } // Submission history flips the same endpoint to the in-place guidance 409. reworkTask := f.task reworkTask.Status = domain.AnnotationTaskStatusRework if err := f.store.UpsertAnnotationTask(f.ctx, reworkTask); err != nil { t.Fatal(err) } r = f.request(http.MethodPost, path+"/slice-reset", f.annotator, map[string]any{"version": saved.Version, "resetToUnifiedRange": true}) if r.Code != http.StatusConflict || !strings.Contains(r.Body.String(), "slice_reset_disabled") { t.Fatalf("reset all must refuse with guidance: %d %s", r.Code, r.Body.String()) } after, workspaceAfter := readSliceRestoration(t, f) if after.Version != saved.Version || len(workspaceAfter.SliceSegments) != 1 || len(workspaceAfter.Segments) != 0 { t.Fatalf("refused reset changed the live workspace: %+v", workspaceAfter.SliceSegments) } if resetHistory, _ := after.Subject.Payload["sliceResetHistory"].([]any); len(resetHistory) != 1 { t.Fatalf("refused reset appended reset history: %#v", resetHistory) } } func TestSliceCancelMergeRejectsInventedLineageAndForeignUndoEvidence(t *testing.T) { for _, scenario := range []string{"missing-source", "wrong-range", "invalid-disposition", "changed-reason", "foreign-restoration", "post-reset-restoration"} { t.Run(scenario, func(t *testing.T) { f := sliceRestorationFixture(t) body := dispositionFixturePayload(t, f) path := "/api/plugins/annotation-qa/tasks/" + f.task.ID r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body) if r.Code != 200 { t.Fatal(r.Body.String()) } item, before := readSliceRestoration(t, f) merged := combinedAnnotationDraftInput{Version: item.Version, SliceSegments: []domain.SliceSegment{before.SliceSegments[0], {ID: "c", Start: 3, End: 10, SourceIDs: []string{"b", "c"}}}, Segments: []domain.AnnotationSegment{}} switch scenario { case "missing-source": merged.SliceSegments[1].SourceIDs = []string{"foreign", "c"} case "wrong-range": merged.SliceSegments[1].Start = 4 merged.SliceSegments[0].End = 4 case "invalid-disposition": merged.SliceSegments[1].DeliveryDisposition = "invented" case "changed-reason", "post-reset-restoration": r = f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, merged) if r.Code != 200 { t.Fatal(r.Body.String()) } if scenario == "post-reset-restoration" { item = applyLegacySliceResetState(t, f) } else { item, _ = readSliceRestoration(t, f) before.SliceSegments[1].DiscardReason = "forged reason" } merged = before merged.Version = item.Version case "foreign-restoration": // No saved merge exists for this task. Source IDs alone cannot create a // new discarded slice or impersonate an old discarded sibling. merged = before merged.SliceSegments[1].ID = "foreign" merged.Version = item.Version } r = f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, merged) if r.Code != 400 { t.Fatalf("unsafe %s: %d %s", scenario, r.Code, r.Body.String()) } }) } } func TestSliceCancelSeveralLocalMergesCanUndoOneAtATimeAfterSave(t *testing.T) { f := sliceRestorationFixture(t) body := dispositionFixturePayload(t, f) path := "/api/plugins/annotation-qa/tasks/" + f.task.ID r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body) if r.Code != 200 { t.Fatal(r.Body.String()) } item, original := readSliceRestoration(t, f) merged := combinedAnnotationDraftInput{Version: item.Version, SliceSegments: []domain.SliceSegment{{ID: "a", Start: 0, End: 10, SourceIDs: []string{"a", "c", "b"}}}} r = f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, merged) if r.Code != 200 { t.Fatal(r.Body.String()) } item, _ = readSliceRestoration(t, f) intermediate := combinedAnnotationDraftInput{Version: item.Version, SliceSegments: []domain.SliceSegment{original.SliceSegments[0], {ID: "c", Start: 3, End: 10, SourceIDs: []string{"b", "c"}}}} r = f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, intermediate) if r.Code != 200 { t.Fatalf("undo one local merge: %d %s", r.Code, r.Body.String()) } item, _ = readSliceRestoration(t, f) original.Version = item.Version r = f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, original) if r.Code != 200 { t.Fatalf("undo second local merge: %d %s", r.Code, r.Body.String()) } _, saved := readSliceRestoration(t, f) if !saved.SliceSegments[1].IsDiscarded() || len(saved.SliceSegments[1].DiscardedAnnotations) != 1 { t.Fatal("second undo lost archived evidence") } } func TestSliceCancelCanSaveANewDictionaryNameBeforeSubmission(t *testing.T) { f := sliceRestorationFixture(t) seedSliceDictionary(t, f, "重新命名") body := dispositionFixturePayload(t, f) path := "/api/plugins/annotation-qa/tasks/" + f.task.ID r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body) if r.Code != 200 { t.Fatal(r.Body.String()) } item, before := readSliceRestoration(t, f) merged := combinedAnnotationDraftInput{Version: item.Version, SliceSegments: []domain.SliceSegment{before.SliceSegments[0], {ID: "c", Name: "重新命名", Start: 3, End: 10, SourceIDs: []string{"b", "c"}}}, Segments: []domain.AnnotationSegment{}} r = f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, merged) if r.Code != 200 { t.Fatalf("save redefined merge: %d %s", r.Code, r.Body.String()) } _, saved := readSliceRestoration(t, f) if len(saved.SliceSegments) != 2 || saved.SliceSegments[1].Name != "重新命名" || saved.SliceSegments[1].IsDiscarded() || saved.SliceSegments[1].DiscardReason != "" { t.Fatalf("new merge name lost or stale decision retained: %+v", saved.SliceSegments) } } func TestNewTaskWithoutWorkflowPhaseCanPersistExplicitSliceMerge(t *testing.T) { f := newCombinedDraftFixture(t) body := dispositionFixturePayload(t, f) path := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/slice-dispositions" r := f.request(http.MethodPost, path, f.annotator, body) if r.Code != 200 { t.Fatal(r.Body.String()) } item, workspace := readSliceRestoration(t, f) body.combinedAnnotationDraftInput = workspace body.Version = item.Version body.SliceSegments = []domain.SliceSegment{workspace.SliceSegments[0], {ID: "c", Start: 3, End: 10, SourceIDs: []string{"b", "c"}}} body.SliceID = "a" body.Disposition = "keep" r = f.request(http.MethodPost, path, f.annotator, body) if r.Code != 200 { t.Fatalf("new task merge: %d %s", r.Code, r.Body.String()) } item, _ = readSliceRestoration(t, f) workspace.Version = item.Version r = f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-edit-restorations", f.annotator, workspace) if r.Code != 200 { t.Fatalf("new task persisted undo: %d %s", r.Code, r.Body.String()) } }