droboticflow/internal/api/discard_editing_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

167 lines
6.3 KiB
Go

package api
import (
"droboticflow/internal/domain"
"encoding/json"
"strings"
"testing"
)
func TestDiscardedSliceCanSplitAdjustPersistAndRestoreClippedEvidence(t *testing.T) {
f := randomClaimFixture(t, 0)
body := dispositionFixturePayload(t, f)
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/slice-dispositions"
var got struct {
Item domain.HumanItemRecord `json:"item"`
Workspace combinedAnnotationDraftInput `json:"workspace"`
}
save := func() {
t.Helper()
r := f.request("POST", path, f.annotator, body)
if r.Code != 200 {
t.Fatalf("save: %d %s", r.Code, r.Body.String())
}
got.Workspace = combinedAnnotationDraftInput{}
if e := json.Unmarshal(r.Body.Bytes(), &got); e != nil {
t.Fatal(e)
}
body.combinedAnnotationDraftInput = got.Workspace
body.Version = got.Item.Version
}
save()
parent := body.SliceSegments[1]
left, right := parent, parent
left.End = 5
right.ID = "b-right"
right.Start = 5
right.SourceIDs = []string{parent.ID}
right.DiscardedByUserID = "forged"
body.SliceSegments = []domain.SliceSegment{body.SliceSegments[0], left, right, body.SliceSegments[2]}
body.SliceID = "b-right"
save()
if len(body.SliceSegments) != 4 || !body.SliceSegments[1].IsDiscarded() || !body.SliceSegments[2].IsDiscarded() || body.SliceSegments[2].DiscardedByUserID != f.annotator.ID {
t.Fatal("split did not retain canonical discard decisions")
}
// Adjust a discarded shared boundary and persist again.
body.SliceSegments[1].End = 4
body.SliceSegments[2].Start = 4
save()
// Rejoin siblings (undo the cut) and persist using their original identity.
body.SliceSegments = []domain.SliceSegment{body.SliceSegments[0], parent, body.SliceSegments[3]}
body.SliceID = parent.ID
save()
// Redo the cut and restore each child: archived text must not cross boundaries.
body.SliceSegments = []domain.SliceSegment{body.SliceSegments[0], left, right, body.SliceSegments[2]}
body.SliceID = right.ID
save()
body.SliceID = parent.ID
body.Disposition = "keep"
save()
if len(body.Segments) != 1 || body.Segments[0].Start != 3 || body.Segments[0].End != 5 || body.Segments[0].Description != "old text" {
t.Fatalf("left restore: %+v", body.Segments)
}
body.SliceID = right.ID
save()
if len(body.Segments) != 2 || body.Segments[1].Start != 5 || body.Segments[1].End != 7 || body.Segments[0].ID == body.Segments[1].ID {
t.Fatalf("right restore: %+v", body.Segments)
}
}
func TestDiscardInheritanceRejectsUnknownSourceAndKeepsAuditAuthoritative(t *testing.T) {
parent := domain.SliceSegment{ID: "a", Start: 0, End: 10, DeliveryDisposition: "discard", DiscardReason: "遮挡", DiscardedByUserID: "author", DiscardedAt: "saved"}
child := parent
child.ID = "child"
child.Start = 5
child.SourceIDs = []string{"other-task"}
left := parent
left.End = 5
if _, issues := canonicalDispositions([]domain.SliceSegment{parent}, []domain.SliceSegment{left, child}); len(issues) == 0 {
t.Fatal("unknown lineage accepted")
}
child.SourceIDs = []string{parent.ID}
child.DiscardedByUserID = "forged"
child.DiscardReason = "invented"
if _, issues := canonicalDispositions([]domain.SliceSegment{parent}, []domain.SliceSegment{left, child}); len(issues) == 0 {
t.Fatal("forged reason accepted")
}
child.DiscardReason = parent.DiscardReason
next, issues := canonicalDispositions([]domain.SliceSegment{parent}, []domain.SliceSegment{left, child})
if len(issues) > 0 || next[1].DiscardedByUserID != "author" {
t.Fatalf("inherited audit: %+v %v", next, issues)
}
}
func TestDiscardedSliceMultipleCutsKeepIntermediateAncestryForUndo(t *testing.T) {
root := domain.SliceSegment{ID: "root", Start: 0, End: 9, DeliveryDisposition: "discard", DiscardReason: "遮挡", DiscardedByUserID: "author"}
a, b, c := root, root, root
a.End = 3
b.ID = "child"
b.Start = 3
b.End = 6
b.SourceIDs = []string{"root"}
c.ID = "grandchild"
c.Start = 6
c.SourceIDs = []string{"child", "root"}
saved, issues := canonicalDispositions([]domain.SliceSegment{root}, []domain.SliceSegment{a, b, c})
if len(issues) > 0 {
t.Fatal(issues)
}
b = saved[1]
b.End = 9
if _, issues = canonicalDispositions(saved, []domain.SliceSegment{saved[0], b}); len(issues) > 0 {
t.Fatalf("undo intermediate cut rejected: %v", issues)
}
}
func TestDiscardedSliceBoundaryCanAdjustAfterMaterialization(t *testing.T) {
f := randomClaimFixture(t, 0)
body := dispositionFixturePayload(t, f)
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/slice-dispositions"
r := f.request("POST", path, f.annotator, body)
if r.Code != 200 {
t.Fatal(r.Body.String())
}
var got struct {
Item domain.HumanItemRecord `json:"item"`
Workspace combinedAnnotationDraftInput `json:"workspace"`
}
if err := json.Unmarshal(r.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
got.Item.Subject.Payload["sliceWorkflow"] = map[string]any{"phase": "annotate", "status": "completed"}
if err := f.store.UpsertHumanItem(f.ctx, got.Item); err != nil {
t.Fatal(err)
}
body.combinedAnnotationDraftInput = got.Workspace
body.Version = got.Item.Version
body.SliceSegments[1].End = 6
body.SliceSegments[2].Start = 6
r = f.request("POST", path, f.annotator, body)
if r.Code != 200 || strings.Contains(r.Body.String(), "请先返回重新切片") {
t.Fatalf("materialized boundaries must stay editable in place: %d %s", r.Code, r.Body.String())
}
got = struct {
Item domain.HumanItemRecord `json:"item"`
Workspace combinedAnnotationDraftInput `json:"workspace"`
}{}
if err := json.Unmarshal(r.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
if got.Workspace.SliceSegments[1].End != 6 || got.Workspace.SliceSegments[2].Start != 6 {
t.Fatalf("boundary move not persisted: %+v", got.Workspace.SliceSegments)
}
}
func TestDiscardInheritanceDoesNotBypassRestoredImmediateParent(t *testing.T) {
root := domain.SliceSegment{ID: "root", Start: 0, End: 5, DeliveryDisposition: "discard", DiscardReason: "遮挡"}
restored := domain.SliceSegment{ID: "restored", Start: 5, End: 10, SourceIDs: []string{"root"}, DeliveryDisposition: "keep"}
child := root
child.ID = "child"
child.Start = 5
child.End = 10
child.SourceIDs = []string{"restored", "root"}
if _, issues := canonicalDispositions([]domain.SliceSegment{root, restored}, []domain.SliceSegment{root, child}); len(issues) == 0 {
t.Fatal("restored parent was silently discarded via an older ancestor")
}
}