droboticflow/internal/api/slice_revision_undo_http_test.go
d-robotics 84d482f354 🔓 02: unlock in-place slice structure editing in annotate stage
- remove the two annotate-phase stage locks in canonicalSliceDispositionState:
  boundary moves of discarded slices and explicit merges are now edited in
  place, first pass and rework share one server path (spec §3.4, R2-Q7:a)
- keep the bottom line untouched: every action must fall fully inside exactly
  one slice and carry a whitelisted status (annotation_operations.go /
  annotation_contiguous.go), pinned by new table-driven cases incl. half
  actions and the three triage products
- retire the reslice write endpoint: POST /slice-reset now answers 409
  slice_reset_disabled pointing at in-place editing and never writes;
  sliceResetHistory evidence and reads stay intact (R3-2:a)
- reset-family tests flipped to the disabled contract; historical post-reset
  invariants still covered via a legacy-state fixture helper
2026-10-13 14:38:46 +05:30

229 lines
10 KiB
Go

package api
import (
"encoding/json"
"net/http"
"testing"
"droboticflow/internal/domain"
)
func TestSliceDispositionUndoTargetsOriginalNamedHistory(t *testing.T) {
f := sliceRestorationFixture(t)
body := dispositionFixturePayload(t, f)
body.SliceSegments[1].Name = "original-name"
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/slice-dispositions"
discardVersion := 0
// Discard, restore, undo restore, then undo the original discard. Each
// replay creates audit history, but must retain the original undo target.
for i, disposition := range []string{"discard", "keep", "discard", "keep", "discard", "keep"} {
body.Disposition = disposition
raw, _ := json.Marshal(body)
var payload map[string]any
if err := json.Unmarshal(raw, &payload); err != nil {
t.Fatal(err)
}
if i == 3 || i == 5 {
payload["restorePreviousName"] = true
payload["restoreNameFromVersion"] = discardVersion
}
r := f.request(http.MethodPost, path, f.annotator, payload)
if r.Code != 200 {
t.Fatalf("step %d: HTTP %d %s", i, r.Code, r.Body.String())
}
item, workspace := readSliceRestoration(t, f)
body.combinedAnnotationDraftInput = workspace
body.Version = item.Version
if i == 0 {
discardVersion = item.Version
}
wantName := ""
if i == 3 || i == 5 {
wantName = "original-name"
}
if workspace.SliceSegments[1].Name != wantName {
t.Fatalf("step %d: name = %q, want %q", i, workspace.SliceSegments[1].Name, wantName)
}
}
}
func localDiscardSplitAndCancel(before combinedAnnotationDraftInput) (combinedAnnotationDraftInput, combinedAnnotationDraftInput) {
left, right := before.SliceSegments[1], before.SliceSegments[1]
left.End, left.SourceIDs = 5, []string{"b"}
right.ID, right.Start, right.SourceIDs = "b-right", 5, []string{"b"}
split := combinedAnnotationDraftInput{SliceSegments: []domain.SliceSegment{before.SliceSegments[0], left, right, before.SliceSegments[2]}, Segments: []domain.AnnotationSegment{}}
split.Operations = []domain.SliceOperation{{Type: "split", SegmentIDs: []string{"b"}, At: 5, Before: before.SliceSegments, After: split.SliceSegments}}
merged := combinedAnnotationDraftInput{SliceSegments: []domain.SliceSegment{before.SliceSegments[0], {ID: "b-right", Start: 3, End: 7, DeliveryDisposition: "keep", SourceIDs: []string{"b", "b-right"}}, before.SliceSegments[2]}, Segments: []domain.AnnotationSegment{}}
merged.Operations = append(append([]domain.SliceOperation{}, split.Operations...), domain.SliceOperation{Type: "delete", SegmentIDs: []string{"b"}, Before: split.SliceSegments, After: merged.SliceSegments})
return split, merged
}
func TestSliceLocalDiscardSplitThenCancelPersistsAndUndoes(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())
}
_, original := readSliceRestoration(t, f)
split, merged := localDiscardSplitAndCancel(original)
for step, snapshot := range []combinedAnnotationDraftInput{merged, split, original, split, merged} {
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("step %d: HTTP %d %s", step, r.Code, r.Body.String())
}
_, saved := readSliceRestoration(t, f)
if step == 0 || step == 4 {
if saved.SliceSegments[1].IsDiscarded() || len(saved.SliceSegments[1].DiscardedAnnotations) != 0 {
t.Fatal("cancel retained discarded judgment or actions")
}
} else if !saved.SliceSegments[1].IsDiscarded() || saved.SliceSegments[1].DiscardedByUserID != f.annotator.ID || len(saved.SliceSegments[1].DiscardedAnnotations) != 1 {
t.Fatal("undo lost trusted discarded judgment or actions")
}
}
}
func TestSliceDispositionNameUndoRejectsUnrelatedHistory(t *testing.T) {
for _, scenario := range []string{"foreign-slice", "before-reset", "restore-entry", "unknown-version"} {
t.Run(scenario, func(t *testing.T) {
f := sliceRestorationFixture(t)
body := dispositionFixturePayload(t, f)
body.SliceSegments[1].Name = "original-name"
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID
save := func() {
t.Helper()
r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body)
if r.Code != 200 {
t.Fatal(r.Body.String())
}
item, workspace := readSliceRestoration(t, f)
body.combinedAnnotationDraftInput, body.Version = workspace, item.Version
}
save()
discardVersion := body.Version
switch scenario {
case "foreign-slice":
body.SliceID = "a"
save()
case "before-reset":
item := applyLegacySliceResetState(t, f)
_, workspace := readSliceRestoration(t, f)
body.combinedAnnotationDraftInput, body.Version = workspace, item.Version
case "restore-entry":
body.Disposition = "keep"
save()
discardVersion = body.Version
body.Disposition = "discard"
save()
case "unknown-version":
discardVersion += 100
}
body.Disposition, body.RestoreNameFromVersion = "keep", discardVersion
r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body)
if r.Code != 400 {
t.Fatalf("unsafe restoration accepted: HTTP %d %s", r.Code, r.Body.String())
}
item, _ := readSliceRestoration(t, f)
if item.Version != body.Version {
t.Fatal("rejected name restoration changed the workspace")
}
})
}
}
func TestSliceLocalDiscardCancelRejectsUntrustedOperationChains(t *testing.T) {
for _, scenario := range []string{"missing-split", "wrong-before", "changed-reason", "foreign-parent", "wrong-cut", "post-reset"} {
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, original := readSliceRestoration(t, f)
_, merged := localDiscardSplitAndCancel(original)
switch scenario {
case "missing-split":
merged.Operations = merged.Operations[1:]
case "wrong-before":
merged.Operations[0].Before[1].DiscardReason = "forged"
case "changed-reason":
merged.Operations[0].After[1].DiscardReason = "forged"
case "foreign-parent":
merged.Operations[0].After[2].SourceIDs = []string{"foreign"}
case "wrong-cut":
merged.Operations[0].At = 6
case "post-reset":
// A pre-reset merge chain must not revive removed action evidence.
item = applyLegacySliceResetState(t, f)
// Boundary/disposition identity survives this reset, so the chain
// may be replayed, but its old archive must remain untrusted.
}
merged.Version = item.Version
r = f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, merged)
if scenario != "post-reset" {
if r.Code != 400 {
t.Fatalf("unsafe chain accepted: HTTP %d %s", r.Code, r.Body.String())
}
return
}
if r.Code != 200 {
t.Fatal(r.Body.String())
}
item, _ = readSliceRestoration(t, f)
split, _ := localDiscardSplitAndCancel(original)
split.Version = item.Version
r = f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, split)
if r.Code != 200 {
t.Fatal(r.Body.String())
}
_, saved := readSliceRestoration(t, f)
if len(saved.SliceSegments[1].DiscardedAnnotations) != 0 || len(saved.SliceSegments[2].DiscardedAnnotations) != 0 {
t.Fatal("pre-reset chain revived archived actions")
}
})
}
}
func TestSliceNestedLocalDiscardSplitsAndCancelsUndoOneAtATime(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())
}
_, original := readSliceRestoration(t, f)
first, _ := localDiscardSplitAndCancel(original)
left, right := first.SliceSegments[2], first.SliceSegments[2]
left.End, left.SourceIDs = 6, []string{"b-right", "b"}
right.ID, right.Start, right.SourceIDs = "b-last", 6, []string{"b-right", "b"}
second := combinedAnnotationDraftInput{SliceSegments: []domain.SliceSegment{first.SliceSegments[0], first.SliceSegments[1], left, right, first.SliceSegments[3]}}
second.Operations = append(append([]domain.SliceOperation{}, first.Operations...), domain.SliceOperation{Type: "split", SegmentIDs: []string{"b-right"}, At: 6, Before: first.SliceSegments, After: second.SliceSegments})
partial := combinedAnnotationDraftInput{SliceSegments: []domain.SliceSegment{first.SliceSegments[0], first.SliceSegments[1], {ID: "b-last", Start: 5, End: 7, SourceIDs: []string{"b-right", "b-last", "b"}}, first.SliceSegments[3]}}
partial.Operations = append(append([]domain.SliceOperation{}, second.Operations...), domain.SliceOperation{Type: "merge", SegmentIDs: []string{"b-right", "b-last"}, Before: second.SliceSegments, After: partial.SliceSegments})
merged := combinedAnnotationDraftInput{SliceSegments: []domain.SliceSegment{original.SliceSegments[0], {ID: "b-last", Start: 3, End: 7, SourceIDs: []string{"b", "b-last", "b-right"}}, original.SliceSegments[2]}}
merged.Operations = append(append([]domain.SliceOperation{}, partial.Operations...), domain.SliceOperation{Type: "delete", SegmentIDs: []string{"b"}, Before: partial.SliceSegments, After: merged.SliceSegments})
// Intermediate client snapshots cannot supply audit identity or archive text.
second.SliceSegments[2].DiscardedByUserID = "forged-user"
second.SliceSegments[2].DiscardedAnnotations = []domain.AnnotationSegment{{ID: "forged", Start: 5, End: 6, Description: "forged-text"}}
for step, snapshot := range []combinedAnnotationDraftInput{merged, partial, second, first, original} {
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("step %d: HTTP %d %s", step, r.Code, r.Body.String())
}
_, saved := readSliceRestoration(t, f)
for _, slice := range saved.SliceSegments {
if slice.IsDiscarded() && (slice.DiscardedByUserID != f.annotator.ID || len(slice.DiscardedAnnotations) != 1 || slice.DiscardedAnnotations[0].Description != "old text") {
t.Fatalf("step %d: untrusted or missing archive: %+v", step, slice)
}
}
}
}