package api import ( "net/http" "strings" "testing" "droboticflow/internal/domain" ) // Ticket 06 (spec rework-ux-adjust, D1 revised 2026-10-14): the reslice write // endpoint is first-pass only. A task without submission history resets // through the original write path and appends sliceResetHistory audit // evidence; anything with submission history (rework status, a human // annotation revision, or an advanced task revision fence) keeps the 409 // slice_reset_disabled contract from R3-2:a and is never mutated. func TestSliceResetSplitsFirstPassFromSubmissionHistory(t *testing.T) { scenarios := []struct { name string seed func(t *testing.T, f combinedDraftFixture) wantStatus int }{ {name: "first pass keep structure", wantStatus: http.StatusOK}, { name: "system suggestion is not submission history", seed: func(t *testing.T, f combinedDraftFixture) { if err := f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: "suggestion-only", OrganizationID: f.task.OrganizationID, TaskID: f.task.ID, Revision: 0, AuthorUserID: "system", Segments: []domain.AnnotationSegment{}}); err != nil { t.Fatal(err) } }, wantStatus: http.StatusOK, }, { name: "rework task status", seed: func(t *testing.T, f combinedDraftFixture) { task := f.task task.Status = domain.AnnotationTaskStatusRework if err := f.store.UpsertAnnotationTask(f.ctx, task); err != nil { t.Fatal(err) } }, wantStatus: http.StatusConflict, }, { name: "human annotation revision", seed: func(t *testing.T, f combinedDraftFixture) { if err := f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: "human-history", OrganizationID: f.task.OrganizationID, TaskID: f.task.ID, Revision: 1, AuthorUserID: f.annotator.ID, Segments: []domain.AnnotationSegment{}}); err != nil { t.Fatal(err) } }, wantStatus: http.StatusConflict, }, { name: "task revision fence", seed: func(t *testing.T, f combinedDraftFixture) { task := f.task task.CurrentRevision = 1 if err := f.store.UpsertAnnotationTask(f.ctx, task); err != nil { t.Fatal(err) } }, wantStatus: http.StatusConflict, }, } for _, scenario := range scenarios { t.Run(scenario.name, func(t *testing.T) { f := sliceRestorationFixture(t) body := dispositionFixturePayload(t, f) path := "/api/plugins/annotation-qa/tasks/" + f.task.ID if r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body); r.Code != http.StatusOK { t.Fatal(r.Body.String()) } item, _ := readSliceRestoration(t, f) item = enterAnnotatePhase(t, f, item) if scenario.seed != nil { scenario.seed(t, f) } r := f.request(http.MethodPost, path+"/slice-reset", f.annotator, map[string]any{"version": item.Version, "resetToUnifiedRange": false}) if r.Code != scenario.wantStatus { t.Fatalf("%s: HTTP %d %s", scenario.name, r.Code, r.Body.String()) } stored, workspace := readSliceRestoration(t, f) if scenario.wantStatus == http.StatusConflict { if !strings.Contains(r.Body.String(), "slice_reset_disabled") { t.Fatalf("%s: refusal must keep the deprecation code: %s", scenario.name, r.Body.String()) } if stored.Version != item.Version || stored.Subject.Payload["sliceResetHistory"] != nil || stringValue(mapValue(stored.Subject.Payload["sliceWorkflow"])["phase"]) != "annotate" { t.Fatalf("%s: refused reset mutated the item: %#v", scenario.name, stored.Subject.Payload) } return } if stored.Version != item.Version+1 { t.Fatalf("%s: reset must advance the CAS version: %d", scenario.name, stored.Version) } history, _ := normalizeStructuredValue(stored.Subject.Payload["sliceResetHistory"]).([]any) if len(history) != 1 { t.Fatalf("%s: sliceResetHistory audit evidence missing: %#v", scenario.name, stored.Subject.Payload["sliceResetHistory"]) } entry := mapValue(history[0]) if entry["actorUserId"] != f.annotator.ID || intValue(entry["itemVersion"]) != stored.Version || entry["resetToUnifiedRange"] != false { t.Fatalf("%s: reset history entry lacks identity: %#v", scenario.name, entry) } if len(workspace.SliceSegments) != 3 || len(workspace.Segments) != 0 || len(workspace.Operations) != 0 { t.Fatalf("%s: keep-structure reset changed the partition: %+v", scenario.name, workspace.SliceSegments) } for _, slice := range workspace.SliceSegments { if len(slice.DiscardedAnnotations) != 0 { t.Fatalf("%s: reset left an action archive live: %+v", scenario.name, slice) } if slice.AnnotationStatus != domain.SliceAnnotationPending { t.Fatalf("%s: reset statuses must return to pending: %+v", scenario.name, slice) } } workflow := mapValue(stored.Subject.Payload["sliceWorkflow"]) if stringValue(workflow["phase"]) != "slice" || stringValue(workflow["status"]) != "editing" || intValue(workflow["resetFromVersion"]) != item.Version { t.Fatalf("%s: reset must return the workflow to slice editing: %#v", scenario.name, workflow) } if _, exists := stored.Subject.Payload["annotationDraft"]; exists { t.Fatalf("%s: reset must drop the annotation draft: %#v", scenario.name, stored.Subject.Payload) } }) } }