package api import ( "errors" "net/http" "reflect" "testing" "droboticflow/internal/domain" ) func TestFailedSliceEditingPreservesFrozenWorkspaceAndRestoresDraft(t *testing.T) { for _, withJob := range []bool{false, true} { t.Run(map[bool]string{false: "legacy", true: "durable-job"}[withJob], func(t *testing.T) { f := continuousFixture(t) item := f.item item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "failed", "phase": "materializing", "error": "retained failure"} item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": []domain.SliceSegment{{ID: "original", Name: "测试切片", Start: 0, End: 10}}, "segments": []domain.AnnotationSegment{}, "savedAt": "2026-10-20T00:00:00Z", "userId": f.annotator.ID} if withJob { item.ProcessingJob = &domain.AnnotationProcessingJob{Kind: "slice", Status: "failed"} } before := []domain.SliceSegment{{ID: "a", Name: "测试切片", Start: 0, End: 5}, {ID: "b", Name: "测试切片", Start: 5, End: 10}} actions := []domain.AnnotationSegment{{ID: "a1", Start: 0, End: 5, Status: "described", Description: "retained classification", Dictionary: &domain.DictionarySelection{ID: "historical-action", Kind: "action", Version: 2, Name: "retained classification", Tags: []string{"original-tag"}}}, {ID: "b1", Start: 5, End: 10, Status: "pending"}} item.Subject.Payload["annotationDraft"] = map[string]any{"sliceSegments": before, "segments": actions, "savedAt": "2026-10-21T00:00:00Z", "savedVersion": item.Version, "userId": f.annotator.ID} if err := f.store.UpsertHumanItem(f.ctx, item); err != nil { t.Fatal(err) } frozen, _ := f.store.GetHumanItem(f.ctx, item.ID) original := mapValue(frozen.Subject.Payload["annotationWorkspace"]) path := "/api/plugins/annotation-qa/tasks/" + f.task.ID for step, end := range []float64{6, 5, 6} { current, _ := f.store.GetHumanItem(f.ctx, item.ID) slices := append([]domain.SliceSegment(nil), before...) slices[0].End, slices[1].Start = end, end nextActions := append([]domain.AnnotationSegment(nil), actions...) nextActions[0].End, nextActions[1].Start = end, end body := combinedAnnotationDraftInput{Version: current.Version, SliceSegments: slices, Segments: nextActions} endpoint := path + "/slice-edit-restorations" var payload any = body if step == 0 { endpoint = path + "/slice-edit-applications" payload = sliceEditApplicationInput{ApplicationID: "failed-edit", combinedAnnotationDraftInput: body} } r := f.request(http.MethodPost, endpoint, f.annotator, payload) if r.Code != http.StatusOK { t.Fatalf("step %d: %d %s", step, r.Code, r.Body.String()) } saved, _ := f.store.GetHumanItem(f.ctx, item.ID) if !reflect.DeepEqual(original, mapValue(saved.Subject.Payload["annotationWorkspace"])) || !reflect.DeepEqual(frozen.ProcessingJob, saved.ProcessingJob) || !reflect.DeepEqual(frozen.Subject.Payload["sliceWorkflow"], saved.Subject.Payload["sliceWorkflow"]) { t.Fatalf("editing changed frozen generation evidence: workspace=%v job=%v workflow=%v\nbefore=%#v\nafter=%#v", reflect.DeepEqual(original, mapValue(saved.Subject.Payload["annotationWorkspace"])), reflect.DeepEqual(frozen.ProcessingJob, saved.ProcessingJob), reflect.DeepEqual(frozen.Subject.Payload["sliceWorkflow"], saved.Subject.Payload["sliceWorkflow"]), original, mapValue(saved.Subject.Payload["annotationWorkspace"])) } selected, draft, err := retainedSliceRecoveryInput(saved, saved.Version) if err != nil || draft.SliceSegments[0].End != end || draft.Segments[0].Description != actions[0].Description || !reflect.DeepEqual(draft.Segments[0].Dictionary, actions[0].Dictionary) { t.Fatalf("current saved draft lost: %+v %v", selected, err) } if _, _, err := retainedSliceRetryInput(saved, saved.Version); !errors.Is(err, errSliceSnapshotConflict) { t.Fatalf("ordinary retry must not silently use changed boundaries: %v", err) } if r := f.request(http.MethodPost, endpoint, f.annotator, payload); r.Code != http.StatusConflict { t.Fatalf("stale edit accepted: %d", r.Code) } // Autosave after an atomic edit must validate against that current draft. body.Version = saved.Version r = f.request(http.MethodPut, path+"/drafts", f.annotator, body) if r.Code != http.StatusOK { t.Fatalf("autosave step %d: %d %s", step, r.Code, r.Body.String()) } afterSave, _ := f.store.GetHumanItem(f.ctx, item.ID) if !reflect.DeepEqual(original, mapValue(afterSave.Subject.Payload["annotationWorkspace"])) { t.Fatal("autosave changed frozen workspace") } f.handler = NewHandler(f.store, nil) } }) } } func TestFailedSliceEditingKeepsSafetyGuards(t *testing.T) { for _, scenario := range []string{"queued", "running", "processing", "completed", "other-job", "wrong-owner", "future-draft", "missing-time", "missing-frozen-time", "submission", "nonowner"} { t.Run(scenario, func(t *testing.T) { for _, endpoint := range []string{"slice-edit-applications", "slice-edit-restorations"} { f := continuousFixture(t) item := f.item item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "failed", "phase": "materializing"} slices := []domain.SliceSegment{{ID: "s", Name: "测试切片", Start: 0, End: 10}} workspace := map[string]any{"sliceSegments": slices, "savedAt": "2026-10-20T00:00:00Z", "userId": f.annotator.ID} item.Subject.Payload["annotationWorkspace"] = workspace draft := map[string]any{"sliceSegments": slices, "savedAt": "2026-10-21T00:00:00Z", "savedVersion": item.Version, "userId": f.annotator.ID} item.Subject.Payload["annotationDraft"] = draft switch scenario { case "queued", "running", "processing", "completed": item.ProcessingJob = &domain.AnnotationProcessingJob{Kind: "slice", Status: scenario} case "other-job": item.ProcessingJob = &domain.AnnotationProcessingJob{Kind: "submission", Status: "failed"} case "wrong-owner": draft["userId"] = f.reviewer.ID case "future-draft": draft["savedVersion"] = item.Version + 1 case "missing-time": delete(draft, "savedAt") case "missing-frozen-time": delete(workspace, "savedAt") case "submission": item.PendingCombinedSubmissionID = "pending" case "nonowner": item.AssignedUserID = f.admin.ID } if err := f.store.UpsertHumanItem(f.ctx, item); err != nil { t.Fatal(err) } before, _ := f.store.GetHumanItem(f.ctx, item.ID) r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/"+endpoint, f.annotator, sliceEditApplicationInput{ApplicationID: "guard", combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: item.Version, SliceSegments: slices}}) if r.Code != http.StatusConflict { t.Fatalf("%s: unsafe edit %d %s", endpoint, r.Code, r.Body.String()) } after, _ := f.store.GetHumanItem(f.ctx, item.ID) if !reflect.DeepEqual(before, after) { t.Fatal("rejected edit changed record") } } }) } }