droboticflow/internal/api/slice_edit_restoration_test.go

364 lines
16 KiB
Go

package api
import (
"encoding/json"
"net/http"
"testing"
"time"
"droboticflow/internal/domain"
)
func sliceRestorationFixture(t *testing.T) combinedDraftFixture {
t.Helper()
f := newCombinedDraftFixture(t)
f.item.Subject.Payload["sliceWorkflow"] = map[string]any{"phase": "slice", "status": "editing"}
f.item.Subject.Payload["annotationDraft"] = map[string]any{"obsolete": true}
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
t.Fatal(err)
}
return f
}
func readSliceRestoration(t *testing.T, f combinedDraftFixture) (domain.HumanItemRecord, combinedAnnotationDraftInput) {
t.Helper()
r := f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID, f.admin, nil)
if r.Code != 200 {
t.Fatalf("GET %d: %s", r.Code, r.Body.String())
}
var got struct {
HumanItem domain.HumanItemRecord `json:"humanItem"`
}
if err := json.Unmarshal(r.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
raw, _ := json.Marshal(got.HumanItem.Subject.Payload["annotationWorkspace"])
var workspace combinedAnnotationDraftInput
if err := json.Unmarshal(raw, &workspace); err != nil {
t.Fatal(err)
}
return got.HumanItem, workspace
}
func TestSliceEditRestorationPersistsUndoRedoThroughHTTP(t *testing.T) {
f := sliceRestorationFixture(t)
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID
body := dispositionFixturePayload(t, f)
// Persist split, discard, then explicit restore before undoing the partition.
for _, disposition := range []string{"discard", "keep"} {
body.Disposition = disposition
r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body)
if r.Code != 200 {
t.Fatalf("%s: %d %s", disposition, r.Code, r.Body.String())
}
item, workspace := readSliceRestoration(t, f)
body.combinedAnnotationDraftInput = workspace
body.Version = item.Version
}
split := body.combinedAnnotationDraftInput
original := combinedAnnotationDraftInput{SliceSegments: []domain.SliceSegment{{ID: "whole", Name: "默认切片", Start: 0, End: 10}}, Segments: split.Segments, Statuses: map[string]domain.SliceAnnotationStatus{"whole": domain.SliceAnnotationPending}}
for _, snapshot := range []combinedAnnotationDraftInput{original, split, original} {
before, _ := readSliceRestoration(t, f)
snapshot.Version = before.Version
r := f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, snapshot)
if r.Code != 200 {
t.Fatalf("restore: %d %s", r.Code, r.Body.String())
}
var response struct {
Item domain.HumanItemRecord `json:"item"`
Workspace combinedAnnotationDraftInput `json:"workspace"`
}
if err := json.Unmarshal(r.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
item, workspace := readSliceRestoration(t, f)
if item.Version != before.Version+1 || response.Item.Version != item.Version || len(workspace.SliceSegments) != len(snapshot.SliceSegments) || len(response.Workspace.SliceSegments) != len(snapshot.SliceSegments) {
t.Fatalf("snapshot not persisted: %+v", workspace)
}
if len(workspace.Segments) != 1 || workspace.Segments[0].Description != "old text" {
t.Fatalf("lost restored evidence: %+v", workspace.Segments)
}
if item.Subject.Payload["annotationDraft"] != nil || item.CurrentStageID != before.CurrentStageID || item.ProcessingJob != nil || stringValue(mapValue(item.Subject.Payload["sliceWorkflow"])["phase"]) != "slice" {
t.Fatalf("unexpected transition: %+v", item)
}
}
}
// Ticket 07 (spec §3.5): in-place structure edits happen in the annotate
// pass too, so undo/redo of a persisted slice edit must replay there. The
// slice-phase-only refusal predates the annotate-phase unlock (ticket 02);
// materializing/processing states must still refuse.
func TestSliceEditRestorationReplaysAnnotatePhaseStructureEdits(t *testing.T) {
f := sliceRestorationFixture(t)
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID
body := dispositionFixturePayload(t, f)
// Discard then restore, so the workspace holds an all-kept split whose
// undo (merge back to one slice) and redo (re-split) are both legal.
for _, disposition := range []string{"discard", "keep"} {
body.Disposition = disposition
if r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body); r.Code != 200 {
t.Fatalf("%s: %d %s", disposition, r.Code, r.Body.String())
}
nextItem, workspace := readSliceRestoration(t, f)
body.combinedAnnotationDraftInput = workspace
body.Version = nextItem.Version
}
item, split := readSliceRestoration(t, f)
item = enterAnnotatePhase(t, f, item)
original := combinedAnnotationDraftInput{SliceSegments: []domain.SliceSegment{{ID: "whole", Name: "默认切片", Start: 0, End: 10}}, Segments: split.Segments, Statuses: map[string]domain.SliceAnnotationStatus{"whole": domain.SliceAnnotationPending}}
for _, snapshot := range []combinedAnnotationDraftInput{original, split} {
snapshot.Version = item.Version
r := f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, snapshot)
if r.Code != 200 {
t.Fatalf("annotate-phase restore: %d %s", r.Code, r.Body.String())
}
after, workspace := readSliceRestoration(t, f)
if after.Version != item.Version+1 || stringValue(mapValue(after.Subject.Payload["sliceWorkflow"])["phase"]) != "annotate" {
t.Fatalf("restore must not transition phase or skip CAS: version=%d phase=%s", after.Version, stringValue(mapValue(after.Subject.Payload["sliceWorkflow"])["phase"]))
}
if len(workspace.SliceSegments) != len(snapshot.SliceSegments) {
t.Fatalf("snapshot not persisted: %+v", workspace.SliceSegments)
}
item = after
}
}
func TestSliceEditRestorationRejectsUnsafeWritesThroughHTTP(t *testing.T) {
for _, scenario := range []string{"stale", "nonowner", "unclaimed", "reviewer", "admin", "stage", "materializing", "missing-phase", "processing", "running", "queued", "job-queued", "job-running", "submission", "coverage", "bounds", "duplicate", "event-bounds", "event-crosses-slice", "unknown-status", "invalid-status", "forged-disposition", "forged-reason", "hardware-contract"} {
t.Run(scenario, func(t *testing.T) {
f := sliceRestorationFixture(t)
item := f.item
body := combinedAnnotationDraftInput{Version: item.Version, SliceSegments: []domain.SliceSegment{{ID: "whole", Start: 0, End: 10}}, Segments: []domain.AnnotationSegment{}}
user := f.annotator
want := http.StatusConflict
workflow := mapValue(item.Subject.Payload["sliceWorkflow"])
switch scenario {
case "stale":
body.Version--
case "nonowner":
item.AssignedUserID = f.admin.ID
case "unclaimed":
item.Status = domain.HumanItemReady
case "reviewer":
user = f.reviewer
want = 403
case "admin":
user = f.admin
want = 403
case "stage":
item.CurrentStageID = "quality_review"
case "materializing":
workflow["phase"] = scenario
case "missing-phase":
delete(workflow, "phase")
case "processing", "running", "queued":
workflow["status"] = scenario
workflow["leaseExpiresAt"] = time.Now().Add(time.Hour).Format(time.RFC3339Nano)
case "job-queued":
item.ProcessingJob = &domain.AnnotationProcessingJob{ID: "job", Status: "queued"}
case "job-running":
item.ProcessingJob = &domain.AnnotationProcessingJob{ID: "job", Status: "running"}
case "submission":
item.PendingCombinedSubmissionID = fanyuV5CombinedSubmissionID(item.ID, item.Version-1)
item.PendingCombinedPayloadSHA = "snapshot"
item.ProcessingJob = &domain.AnnotationProcessingJob{ID: item.PendingCombinedSubmissionID, Status: "queued", Kind: "submit"}
default:
want = http.StatusBadRequest
switch scenario {
case "coverage":
body.SliceSegments[0].End = 9
case "bounds":
body.SliceSegments[0].Start = -1
case "duplicate":
body.SliceSegments = append(body.SliceSegments, body.SliceSegments[0])
case "event-bounds":
body.Segments = []domain.AnnotationSegment{{ID: "event", Start: 9, End: 11}}
case "event-crosses-slice":
body.SliceSegments = []domain.SliceSegment{{ID: "a", Start: 0, End: 5}, {ID: "b", Start: 5, End: 10}}
body.Segments = []domain.AnnotationSegment{{ID: "event", Start: 4, End: 6}}
case "unknown-status":
body.Statuses = map[string]domain.SliceAnnotationStatus{"missing": domain.SliceAnnotationPending}
case "invalid-status":
body.Statuses = map[string]domain.SliceAnnotationStatus{"whole": "made-up"}
case "forged-disposition":
body.SliceSegments[0].DeliveryDisposition = "discard"
body.SliceSegments[0].DiscardReason = "forged"
case "forged-reason":
body.SliceSegments[0].DiscardReason = "forged"
case "hardware-contract":
workflow["boundaryContract"] = "hardware-frame-v1"
want = http.StatusUnprocessableEntity
}
}
item.Subject.Payload["sliceWorkflow"] = workflow
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
t.Fatal(err)
}
before, _ := readSliceRestoration(t, f)
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-edit-restorations", user, body)
if r.Code != want {
t.Fatalf("want %d got %d: %s", want, r.Code, r.Body.String())
}
after, _ := readSliceRestoration(t, f)
a, _ := json.Marshal(before)
b, _ := json.Marshal(after)
if string(a) != string(b) {
t.Fatalf("rejected request mutated item\nbefore %s\nafter %s", a, b)
}
})
}
}
func TestSliceEditRestorationConcurrentSnapshotsHaveOneWinner(t *testing.T) {
f := sliceRestorationFixture(t)
body := combinedAnnotationDraftInput{Version: f.item.Version, SliceSegments: []domain.SliceSegment{{ID: "whole", Start: 0, End: 10}}}
start := make(chan struct{})
results := make(chan int, 2)
for i := 0; i < 2; i++ {
go func() {
<-start
results <- f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-edit-restorations", f.annotator, body).Code
}()
}
close(start)
a, b := <-results, <-results
if !((a == 200 && b == 409) || (a == 409 && b == 200)) {
t.Fatalf("concurrent statuses %d %d", a, b)
}
item, workspace := readSliceRestoration(t, f)
if item.Version != f.item.Version+1 || len(workspace.SliceSegments) != 1 {
t.Fatalf("bad winning state: %+v", item)
}
}
func TestSliceEditRestorationPreservesDiscardAuthority(t *testing.T) {
f := sliceRestorationFixture(t)
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID
body := dispositionFixturePayload(t, f)
r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body)
if r.Code != 200 {
t.Fatalf("discard: %d %s", r.Code, r.Body.String())
}
item, saved := readSliceRestoration(t, f)
for _, scenario := range []string{"restore-disposition", "change-reason", "drop-discard"} {
t.Run(scenario, func(t *testing.T) {
raw, _ := json.Marshal(saved)
var payload combinedAnnotationDraftInput
if err := json.Unmarshal(raw, &payload); err != nil {
t.Fatal(err)
}
payload.Version = item.Version
switch scenario {
case "restore-disposition":
payload.SliceSegments[1].DeliveryDisposition = "keep"
payload.SliceSegments[1].DiscardReason = ""
case "change-reason":
payload.SliceSegments[1].DiscardReason = "forged"
case "drop-discard":
payload.SliceSegments = []domain.SliceSegment{{ID: "whole", Start: 0, End: 10}}
}
rejected := f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, payload)
if rejected.Code != 400 {
t.Fatalf("forged %d %s", rejected.Code, rejected.Body.String())
}
latest, _ := readSliceRestoration(t, f)
if latest.Version != item.Version {
t.Fatal("forgery changed version")
}
})
}
saved.Version = item.Version
// An unchanged discarded snapshot round-trips without revalidating dictionary names.
saved.Operations = []domain.SliceOperation{{Type: "split", SegmentIDs: []string{"a", "b", "c"}}}
r = f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, saved)
if r.Code != 200 {
t.Fatalf("saved discard: %d %s", r.Code, r.Body.String())
}
_, workspace := readSliceRestoration(t, f)
if len(workspace.Operations) != 1 || workspace.Operations[0].Type != "split" || len(workspace.SliceSegments[1].DiscardedAnnotations) != 1 || workspace.SliceSegments[1].DiscardedAnnotations[0].Description != "old text" || workspace.SliceSegments[1].DiscardedByUserID != f.annotator.ID {
t.Fatalf("discard evidence lost: %+v", workspace)
}
}
func TestSliceEditRestorationHardwareFrameContract(t *testing.T) {
f, _ := annotationProcessorFixture(t)
item := f.item
item.Subject.Payload["sliceWorkflow"] = map[string]any{"phase": "slice", "status": "editing", "boundaryContract": "hardware-frame-v1"}
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
t.Fatal(err)
}
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID
payload := combinedAnnotationDraftInput{Version: item.Version, SliceSegments: []domain.SliceSegment{{ID: "whole", Start: 0, End: 4}}, Segments: []domain.AnnotationSegment{{ID: "event", Start: 0.125, End: 0.25}}}
// Both slice and event edges must resolve against the real stereo PTS grid.
for _, target := range []string{"slice", "event"} {
bad := payload
bad.SliceSegments = append([]domain.SliceSegment(nil), payload.SliceSegments...)
bad.Segments = append([]domain.AnnotationSegment(nil), payload.Segments...)
if target == "slice" {
bad.SliceSegments[0].End = 3.99
} else {
bad.Segments[0].Start = 0.12
}
r := f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, bad)
if r.Code != 400 {
t.Fatalf("off-grid %s: %d %s", target, r.Code, r.Body.String())
}
}
r := f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, payload)
if r.Code != 200 {
t.Fatalf("aligned: %d %s", r.Code, r.Body.String())
}
latest, workspace := readSliceRestoration(t, f)
if latest.Version != item.Version+1 || workspace.SliceSegments[0].StartTimestampUS != 1000000 || workspace.SliceSegments[0].EndTimestampUS != 5000000 || workspace.Segments[0].StartTimestampUS != 1125000 || workspace.Segments[0].EndTimestampUS != 1250000 {
t.Fatalf("frame timestamps: %+v", workspace)
}
}
func TestSliceEditRestorationCanonicalizesHistoricalAuditThroughHTTP(t *testing.T) {
f := sliceRestorationFixture(t)
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID
body := dispositionFixturePayload(t, f)
var historical combinedAnnotationDraftInput
for index, disposition := range []string{"discard", "keep", "discard"} {
body.Disposition = disposition
if index == 2 {
body.Segments[0].Description = "new authoritative evidence"
}
r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body)
if r.Code != 200 {
t.Fatalf("disposition: %d %s", r.Code, r.Body.String())
}
item, workspace := readSliceRestoration(t, f)
if index == 0 {
historical = workspace
}
body.combinedAnnotationDraftInput = workspace
body.Version = item.Version
}
current := body.SliceSegments[1]
if current.DiscardedAt == historical.SliceSegments[1].DiscardedAt {
t.Fatal("fixture must produce a new discard timestamp")
}
historical.Version = body.Version
// Historical timestamps and archive are stale; even arbitrary client audit
// identity/reason snapshots must be replaced by the current server evidence.
historical.SliceSegments[1].DiscardedByUserID = "forged"
historical.SliceSegments[1].DiscardReasonLabelID = "forged"
historical.SliceSegments[1].DiscardReasonVersion = 999
historical.SliceSegments[1].DiscardReasonName = "forged"
historical.SliceSegments[1].DiscardReasonNote = "forged"
r := f.request(http.MethodPost, path+"/slice-edit-restorations", f.annotator, historical)
if r.Code != 200 {
t.Fatalf("historical snapshot: %d %s", r.Code, r.Body.String())
}
item, workspace := readSliceRestoration(t, f)
actual := workspace.SliceSegments[1]
a, _ := json.Marshal(current)
b, _ := json.Marshal(actual)
if string(a) != string(b) || item.Version != body.Version+1 {
t.Fatalf("audit was not canonicalized: want %s got %s", a, b)
}
}