forked from longtao.wu/droboticflow
474 lines
16 KiB
Go
474 lines
16 KiB
Go
package api
|
|
|
|
import (
|
|
"droboticflow/internal/domain"
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestPR5ReviewArchiveBoundaryUndo(t *testing.T) {
|
|
f := randomClaimFixture(t, 0)
|
|
body := dispositionFixturePayload(t, f)
|
|
save := func(step string) {
|
|
t.Helper()
|
|
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body)
|
|
if r.Code != 200 {
|
|
t.Fatalf("%s: %d %s", step, r.Code, 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)
|
|
}
|
|
body.combinedAnnotationDraftInput = got.Workspace
|
|
body.Version = got.Item.Version
|
|
t.Logf("%s: active=%+v archive=%+v", step, body.Segments, body.SliceSegments[1].DiscardedAnnotations)
|
|
}
|
|
save("discard [3,7]")
|
|
body.SliceSegments[1].End = 5
|
|
body.SliceSegments[2].Start = 5
|
|
body.Disposition = "keep"
|
|
save("shorten and restore [3,5]")
|
|
body.Disposition = "discard"
|
|
save("undo restore")
|
|
body.SliceSegments[1].End = 7
|
|
body.SliceSegments[2].Start = 7
|
|
body.Disposition = "keep"
|
|
save("undo boundary and restore [3,7]")
|
|
if len(body.Segments) != 1 || body.Segments[0].Start != 3 || body.Segments[0].End != 7 {
|
|
t.Fatalf("lost original [3,7] archive: %+v", body.Segments)
|
|
}
|
|
}
|
|
|
|
// Every step reloads the stored item through HTTP, not just the POST response.
|
|
func archiveRoundTrip(t *testing.T, f combinedDraftFixture, body *sliceDispositionInput) {
|
|
t.Helper()
|
|
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID
|
|
r := f.request("POST", path+"/slice-dispositions", f.annotator, *body)
|
|
if r.Code != 200 {
|
|
t.Fatalf("%s: %d %s", body.Disposition, r.Code, r.Body.String())
|
|
}
|
|
archiveReadback(t, f, body)
|
|
}
|
|
|
|
func archiveReadback(t *testing.T, f combinedDraftFixture, body *sliceDispositionInput) {
|
|
t.Helper()
|
|
r := f.request("GET", "/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, err := json.Marshal(annotationWorkspacePayload(&got.HumanItem))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body.combinedAnnotationDraftInput = combinedAnnotationDraftInput{}
|
|
if err := json.Unmarshal(raw, &body.combinedAnnotationDraftInput); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body.Version = got.HumanItem.Version
|
|
}
|
|
|
|
func TestArchiveRecoveryRepeatedCyclesAndCurrentDescriptions(t *testing.T) {
|
|
for _, change := range []string{"unchanged", "edit", "delete", "replace-id", "invalid"} {
|
|
t.Run(change, func(t *testing.T) {
|
|
f := randomClaimFixture(t, 0)
|
|
f.handler = NewHandler(bsonDispositionStore{Store: f.store}, nil)
|
|
body := dispositionFixturePayload(t, f)
|
|
archiveRoundTrip(t, f, &body)
|
|
body.SliceSegments[1].End = 5
|
|
body.SliceSegments[2].Start = 5
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
switch change {
|
|
case "edit":
|
|
body.Segments[0].Description = "new text"
|
|
case "delete":
|
|
body.Segments = nil
|
|
case "replace-id":
|
|
body.Segments[0].ID = "replacement"
|
|
case "invalid":
|
|
body.Segments[0].IsInvalid = true
|
|
}
|
|
for cycle := 0; cycle < 3; cycle++ {
|
|
// Both the public archive and client snapshots are untrusted.
|
|
body.SliceSegments[1].DiscardedAnnotations = []domain.AnnotationSegment{{ID: "forged", Start: 3, End: 7, Description: "forged"}}
|
|
body.Disposition = "discard"
|
|
archiveRoundTrip(t, f, &body)
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
}
|
|
body.Disposition = "discard"
|
|
archiveRoundTrip(t, f, &body)
|
|
body.SliceSegments[1].End = 7
|
|
body.SliceSegments[2].Start = 7
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
if change == "unchanged" {
|
|
if len(body.Segments) != 1 || body.Segments[0].Start != 3 || body.Segments[0].End != 7 || body.Segments[0].Description != "old text" {
|
|
t.Fatalf("lost original: %+v", body.Segments)
|
|
}
|
|
} else {
|
|
// Only never-exposed [5,7] retains the old description.
|
|
expected := 2
|
|
if change == "delete" {
|
|
expected = 1
|
|
}
|
|
if len(body.Segments) != expected {
|
|
t.Fatalf("incorrect recovery: %+v", body.Segments)
|
|
}
|
|
for _, a := range body.Segments {
|
|
if a.Description == "forged" {
|
|
t.Fatal("client evidence accepted")
|
|
}
|
|
if a.Start == 3 {
|
|
if a.End != 5 || (change == "edit" && a.Description != "new text") || (change == "invalid" && !a.IsInvalid) || (change == "replace-id" && a.ID != "replacement") || change == "delete" {
|
|
t.Fatalf("stale text resurrected: %+v", a)
|
|
}
|
|
} else if a.Start != 5 || a.End != 7 || a.Description != "old text" {
|
|
t.Fatalf("unexposed archive lost: %+v", a)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestArchiveRecoveryAcrossWorkspaceCanonicalization(t *testing.T) {
|
|
f := randomClaimFixture(t, 0)
|
|
f.item.Subject.Payload["sliceWorkflow"] = map[string]any{"phase": "slice", "status": "editing"}
|
|
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := dispositionFixturePayload(t, f)
|
|
archiveRoundTrip(t, f, &body)
|
|
persist := func(end float64) {
|
|
t.Helper()
|
|
body.SliceSegments[1].End = end
|
|
body.SliceSegments[2].Start = end
|
|
body.SliceSegments[1].DiscardedAnnotations = nil
|
|
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-edit-restorations", f.annotator, body.combinedAnnotationDraftInput)
|
|
if r.Code != 200 {
|
|
t.Fatalf("restore snapshot: %d %s", r.Code, r.Body.String())
|
|
}
|
|
archiveReadback(t, f, &body)
|
|
}
|
|
persist(5)
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
// A kept workspace write must preserve the private recovery evidence too.
|
|
persist(5)
|
|
body.Disposition = "discard"
|
|
archiveRoundTrip(t, f, &body)
|
|
persist(7)
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
if len(body.Segments) != 1 || body.Segments[0].End != 7 {
|
|
t.Fatalf("snapshot lost archive: %+v", body.Segments)
|
|
}
|
|
}
|
|
|
|
func TestArchiveRecoverySeparatesSlicesWithReusedAnnotationID(t *testing.T) {
|
|
f := randomClaimFixture(t, 0)
|
|
body := dispositionFixturePayload(t, f)
|
|
archiveRoundTrip(t, f, &body)
|
|
body.SliceID = "c"
|
|
body.Segments = []domain.AnnotationSegment{{ID: "old-description", Start: 7, End: 10, Description: "other text", Source: "manual"}}
|
|
archiveRoundTrip(t, f, &body)
|
|
for cycle := 0; cycle < 3; cycle++ {
|
|
for _, id := range []string{"b", "c"} {
|
|
body.SliceID = id
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
}
|
|
if len(body.Segments) != 2 || body.Segments[0].Description != "old text" || body.Segments[1].Description != "other text" || body.Segments[0].ID == body.Segments[1].ID {
|
|
t.Fatalf("cross-contamination: %+v", body.Segments)
|
|
}
|
|
for _, id := range []string{"b", "c"} {
|
|
body.SliceID = id
|
|
body.Disposition = "discard"
|
|
archiveRoundTrip(t, f, &body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestArchiveInheritanceRetainsOriginalAndRejectsDuplicateSliceIDs(t *testing.T) {
|
|
parent := domain.SliceSegment{ID: "b", Start: 3, End: 5, DeliveryDisposition: "discard", DiscardReason: "reason", DiscardedAnnotations: []domain.AnnotationSegment{{ID: "old", Start: 3, End: 7, Description: "old"}}}
|
|
child := parent
|
|
child.ID = "child"
|
|
child.Start = 4
|
|
child.End = 8
|
|
child.SourceIDs = []string{"b"}
|
|
left := parent
|
|
left.End = 4
|
|
got, issues := canonicalDispositions([]domain.SliceSegment{parent}, []domain.SliceSegment{left, child})
|
|
if len(issues) > 0 || len(got[1].DiscardedAnnotations) != 1 || got[1].DiscardedAnnotations[0].End != 7 {
|
|
t.Fatalf("split and drag lost original evidence: %+v %v", got, issues)
|
|
}
|
|
if _, issues := canonicalDispositions([]domain.SliceSegment{parent}, []domain.SliceSegment{parent, parent}); len(issues) == 0 {
|
|
t.Fatal("duplicate slice IDs accepted")
|
|
}
|
|
}
|
|
|
|
func TestArchiveRejoinUsesEditedOrDeletedChildEvidence(t *testing.T) {
|
|
for _, deleted := range []bool{false, true} {
|
|
t.Run(fmt.Sprint(deleted), func(t *testing.T) {
|
|
f := randomClaimFixture(t, 0)
|
|
body := dispositionFixturePayload(t, f)
|
|
archiveRoundTrip(t, f, &body)
|
|
parent := body.SliceSegments[1]
|
|
left, right := parent, parent
|
|
left.End = 5
|
|
right.Start = 5
|
|
right.ID = "child"
|
|
right.SourceIDs = []string{"b"}
|
|
body.SliceSegments = []domain.SliceSegment{body.SliceSegments[0], left, right, body.SliceSegments[2]}
|
|
body.SliceID = "child"
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
if deleted {
|
|
body.Segments = nil
|
|
} else {
|
|
body.Segments[0].Description = "child edit"
|
|
}
|
|
// Older client snapshots may omit or forge lineage after restore.
|
|
body.SliceSegments[2].SourceIDs = []string{"unrelated"}
|
|
body.Disposition = "discard"
|
|
archiveRoundTrip(t, f, &body)
|
|
body.SliceSegments = []domain.SliceSegment{body.SliceSegments[0], parent, body.SliceSegments[3]}
|
|
body.SliceID = "b"
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
expected := 2
|
|
if deleted {
|
|
expected = 1
|
|
}
|
|
if len(body.Segments) != expected {
|
|
t.Fatalf("rejoin lost child decision: %+v", body.Segments)
|
|
}
|
|
for _, a := range body.Segments {
|
|
if a.End > 5 && (deleted || a.Description != "child edit") {
|
|
t.Fatalf("stale parent evidence resurrected: %+v", body.Segments)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestArchiveRecoveryExpiresAtRevisionBoundary(t *testing.T) {
|
|
f := randomClaimFixture(t, 0)
|
|
body := dispositionFixturePayload(t, f)
|
|
archiveRoundTrip(t, f, &body)
|
|
body.SliceSegments[1].End = 5
|
|
body.SliceSegments[2].Start = 5
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
item, err := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item.CurrentRevision++
|
|
if err := f.store.UpsertHumanItem(f.ctx, *item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body.Disposition = "discard"
|
|
archiveRoundTrip(t, f, &body)
|
|
body.SliceSegments[1].End = 7
|
|
body.SliceSegments[2].Start = 7
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
if len(body.Segments) != 1 || body.Segments[0].End != 5 {
|
|
t.Fatalf("replayed a previous revision: %+v", body.Segments)
|
|
}
|
|
}
|
|
|
|
func TestArchiveMultipleCutsDragAndUndoAllCuts(t *testing.T) {
|
|
for _, change := range []string{"unchanged", "edit", "delete"} {
|
|
t.Run(change, func(t *testing.T) {
|
|
f := randomClaimFixture(t, 0)
|
|
body := dispositionFixturePayload(t, f)
|
|
archiveRoundTrip(t, f, &body)
|
|
original := body.SliceSegments[1]
|
|
body.SliceSegments[1].End = 5
|
|
body.SliceSegments[2].Start = 5
|
|
archiveRoundTrip(t, f, &body)
|
|
// Split the saved [3,5] parent and drag the new child beyond its old
|
|
// boundary in one request. The archive still has original [3,7].
|
|
left, child := body.SliceSegments[1], body.SliceSegments[1]
|
|
left.End = 4
|
|
child.ID = "child"
|
|
child.Start = 4
|
|
child.End = 6
|
|
child.SourceIDs = []string{"b"}
|
|
tail := body.SliceSegments[2]
|
|
tail.Start = 6
|
|
body.SliceSegments = []domain.SliceSegment{body.SliceSegments[0], left, child, tail}
|
|
body.SliceID = "child"
|
|
archiveRoundTrip(t, f, &body)
|
|
child = body.SliceSegments[2]
|
|
child.End = 5
|
|
grandchild := child
|
|
grandchild.ID = "grandchild"
|
|
grandchild.Start = 5
|
|
grandchild.End = 7
|
|
grandchild.SourceIDs = []string{"child", "b"}
|
|
tail.Start = 7
|
|
body.SliceSegments = []domain.SliceSegment{body.SliceSegments[0], body.SliceSegments[1], child, grandchild, tail}
|
|
body.SliceID = "grandchild"
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
if len(body.Segments) != 1 || body.Segments[0].Start != 5 || body.Segments[0].End != 7 {
|
|
t.Fatalf("grandchild lost full original: %+v", body.Segments)
|
|
}
|
|
if change == "edit" {
|
|
body.Segments[0].Description = "edited grandchild"
|
|
}
|
|
if change == "delete" {
|
|
body.Segments = nil
|
|
}
|
|
body.Disposition = "discard"
|
|
archiveRoundTrip(t, f, &body)
|
|
// Undo the inner cut, then all cuts, then the initial shortening.
|
|
child = body.SliceSegments[2]
|
|
child.End = 7
|
|
body.SliceSegments = []domain.SliceSegment{body.SliceSegments[0], body.SliceSegments[1], child, body.SliceSegments[4]}
|
|
body.SliceID = "child"
|
|
archiveRoundTrip(t, f, &body)
|
|
body.SliceSegments = []domain.SliceSegment{body.SliceSegments[0], original, body.SliceSegments[3]}
|
|
body.SliceID = "b"
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
total := 0.0
|
|
for _, a := range body.Segments {
|
|
total += a.End - a.Start
|
|
if a.Start < 3 || a.End > 7 {
|
|
t.Fatalf("archive crossed original bounds: %+v", a)
|
|
}
|
|
if a.End > 5 && (change == "delete" || (change == "edit" && a.Description != "edited grandchild")) {
|
|
t.Fatalf("stale ancestor evidence: %+v", body.Segments)
|
|
}
|
|
}
|
|
expected := 4.0
|
|
if change == "delete" {
|
|
expected = 2
|
|
}
|
|
if total != expected {
|
|
t.Fatalf("cuts lost evidence: %+v", body.Segments)
|
|
}
|
|
if change == "unchanged" && (len(body.Segments) != 1 || body.Segments[0].ID != "old-description") {
|
|
t.Fatalf("original fragmented/lost: %+v", body.Segments)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestArchiveRejoinPreservesEditedDormantChildThroughHTTP(t *testing.T) {
|
|
for _, change := range []string{"edit", "delete", "parent-edit", "overlapping-parent-edit"} {
|
|
t.Run(change, func(t *testing.T) {
|
|
f := randomClaimFixture(t, 0)
|
|
f.handler = NewHandler(bsonDispositionStore{Store: f.store}, nil)
|
|
f.item.Subject.Payload["sliceWorkflow"] = map[string]any{"phase": "slice", "status": "editing"}
|
|
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := dispositionFixturePayload(t, f)
|
|
archiveRoundTrip(t, f, &body)
|
|
parent := body.SliceSegments[1]
|
|
left, child := parent, parent
|
|
left.End = 5
|
|
child.ID = "child"
|
|
child.Start = 5
|
|
child.SourceIDs = []string{"b"}
|
|
body.SliceSegments = []domain.SliceSegment{body.SliceSegments[0], left, child, body.SliceSegments[2]}
|
|
body.SliceID = "child"
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
if change == "delete" {
|
|
body.Segments = nil
|
|
} else {
|
|
body.Segments[0].Description = "new text"
|
|
}
|
|
body.Disposition = "discard"
|
|
archiveRoundTrip(t, f, &body)
|
|
restore := func() {
|
|
t.Helper()
|
|
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-edit-restorations", f.annotator, body.combinedAnnotationDraftInput)
|
|
if r.Code != 200 {
|
|
t.Fatalf("restore snapshot: %d %s", r.Code, r.Body.String())
|
|
}
|
|
archiveReadback(t, f, &body)
|
|
}
|
|
if change == "parent-edit" || change == "overlapping-parent-edit" {
|
|
if change == "overlapping-parent-edit" {
|
|
body.SliceSegments[1].End = 6
|
|
body.SliceSegments[2].Start = 6
|
|
restore()
|
|
}
|
|
body.SliceID = "b"
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
for i := range body.Segments {
|
|
body.Segments[i].Description = "parent newer"
|
|
}
|
|
body.Disposition = "discard"
|
|
archiveRoundTrip(t, f, &body)
|
|
body.SliceSegments[1].End = 5
|
|
body.SliceSegments[2].Start = 5
|
|
restore()
|
|
}
|
|
body.SliceSegments[2].End = 6
|
|
body.SliceSegments[3].Start = 6
|
|
restore()
|
|
// Original client archive is deliberately stale; the server must retain
|
|
// both child-owned dormant writes and independent newer parent writes.
|
|
body.SliceSegments = []domain.SliceSegment{body.SliceSegments[0], parent, body.SliceSegments[3]}
|
|
body.SliceSegments[2].Start = 7
|
|
body.Statuses = nil
|
|
restore()
|
|
body.SliceID = "b"
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
for cycle := 0; cycle < 2; cycle++ {
|
|
total := 0.0
|
|
for _, a := range body.Segments {
|
|
total += a.End - a.Start
|
|
parentEnd := 5.0
|
|
if change == "overlapping-parent-edit" {
|
|
parentEnd = 6
|
|
}
|
|
if a.Start < parentEnd {
|
|
expected := "old text"
|
|
if change == "parent-edit" || change == "overlapping-parent-edit" {
|
|
expected = "parent newer"
|
|
}
|
|
if a.End > parentEnd || a.Description != expected {
|
|
t.Fatalf("independent parent edit overwritten: %+v", body.Segments)
|
|
}
|
|
} else if change == "delete" || a.Description != "new text" {
|
|
t.Fatalf("updated dormant child archive lost: %+v", body.Segments)
|
|
}
|
|
}
|
|
expected := 4.0
|
|
if change == "delete" {
|
|
expected = 2
|
|
}
|
|
if total != expected {
|
|
t.Fatalf("archive coverage lost: %+v", body.Segments)
|
|
}
|
|
body.Disposition = "discard"
|
|
archiveRoundTrip(t, f, &body)
|
|
body.Disposition = "keep"
|
|
archiveRoundTrip(t, f, &body)
|
|
}
|
|
})
|
|
}
|
|
}
|