droboticflow/internal/api/discard_reset_pending_test.go

138 lines
6.5 KiB
Go

package api
import (
"encoding/json"
"testing"
"droboticflow/internal/domain"
)
func TestExplicitDiscardRestoreResetsWholeSliceToPending(t *testing.T) {
for _, rework := range []bool{false, true} {
t.Run(map[bool]string{false: "first-pass", true: "rework"}[rework], func(t *testing.T) {
f := continuousFixture(t)
reason := dispositionFixturePayload(t, f)
slices := []domain.SliceSegment{{ID: "a", Name: "测试切片", Start: 0, End: 5}, {ID: "b", Name: "测试切片", Start: 5, End: 10}}
actions := []domain.AnnotationSegment{{ID: "described", Start: 0, End: 2, Status: "described", Description: "saved original"}, {ID: "invalid", Start: 2, End: 5, Status: "invalid", IsInvalid: true}, {ID: "neighbor", Start: 5, End: 10, Status: "pending"}}
item := f.item
if rework {
item.ReviewRound = 1
}
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": slices, "segments": actions}
if e := f.store.UpsertHumanItem(f.ctx, item); e != nil {
t.Fatal(e)
}
proposed := append([]domain.SliceSegment(nil), slices...)
proposed[0].DeliveryDisposition = "discard"
input := sliceEditApplicationInput{ApplicationID: "discard", combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: item.Version, SliceSegments: proposed, Segments: actions}, Dispositions: []sliceDispositionInput{{SliceID: "a", Disposition: "discard", ReasonLabelID: reason.ReasonLabelID, ReasonVersion: reason.ReasonVersion}}}
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/slice-edit-applications"
r := f.request("POST", endpoint, f.annotator, input)
if r.Code != 200 {
t.Fatalf("discard %d %s", r.Code, r.Body.String())
}
var got struct {
Item domain.HumanItemRecord `json:"item"`
Workspace combinedAnnotationDraftInput `json:"workspace"`
}
if e := json.Unmarshal(r.Body.Bytes(), &got); e != nil {
t.Fatal(e)
}
restore := map[string]any{"applicationId": "restore-pending", "version": got.Item.Version, "sliceSegments": slices, "segments": actions[2:], "dispositions": []map[string]any{{"sliceId": "a", "disposition": "keep", "resetToPending": true}}}
r = f.request("POST", endpoint, f.annotator, restore)
if r.Code != 200 {
t.Fatalf("restore %d %s", r.Code, r.Body.String())
}
if e := json.Unmarshal(r.Body.Bytes(), &got); e != nil {
t.Fatal(e)
}
if len(got.Workspace.Segments) != 2 {
t.Fatalf("explicit restore must reset to one full pending action plus unchanged neighbor, got %+v", got.Workspace.Segments)
}
var own []domain.AnnotationSegment
for _, a := range got.Workspace.Segments {
if a.Start < 5 {
own = append(own, a)
}
}
if len(own) != 1 || own[0].Start != 0 || own[0].End != 5 || own[0].Status != "pending" || own[0].Description != "" || own[0].IsInvalid {
t.Fatalf("not a full pending interval: %+v", own)
}
f.handler = NewHandler(f.store, nil)
saved, _ := f.store.GetHumanItem(f.ctx, item.ID)
if len(decodeMaterializationValue[domain.AnnotationSegment](annotationWorkspacePayload(saved)["segments"])) != 2 {
t.Fatal("reset not durable")
}
if len(decodeMaterializationValue[sliceRestoreEvidence](saved.Subject.Payload[sliceRestoreEvidenceKey])) == 0 {
t.Fatal("audit recovery evidence lost")
}
// Reject a reset command on an already-kept slice without any CAS.
restore["version"] = got.Item.Version
restore["applicationId"] = "illegal-reset-kept"
bad := f.request("POST", endpoint, f.annotator, restore)
if bad.Code != 400 {
t.Fatalf("reset kept slice accepted: %d %s", bad.Code, bad.Body.String())
}
unchanged, _ := f.store.GetHumanItem(f.ctx, item.ID)
if unchanged.Version != got.Item.Version {
t.Fatal("invalid reset changed the record")
}
// Undo/redo of the restore must never resurrect old descriptions.
input.ApplicationID = "undo-restore"
input.Version = got.Item.Version
input.Segments = got.Workspace.Segments
r = f.request("POST", endpoint, f.annotator, input)
if r.Code != 200 {
t.Fatalf("undo restore %d %s", r.Code, r.Body.String())
}
json.Unmarshal(r.Body.Bytes(), &got)
if len(got.Workspace.SliceSegments[0].DiscardedAnnotations) != 2 {
t.Fatal("undo lost original archived evidence")
}
restore["version"] = got.Item.Version
restore["applicationId"] = "redo-restore-pending"
r = f.request("POST", endpoint, f.annotator, restore)
if r.Code != 200 {
t.Fatalf("redo reset %d %s", r.Code, r.Body.String())
}
json.Unmarshal(r.Body.Bytes(), &got)
if len(got.Workspace.Segments) != 2 {
t.Fatal("redo resurrected archived descriptions")
}
for _, a := range got.Workspace.Segments {
if a.Start < 5 && (a.Start != 0 || a.End != 5 || a.Status != "pending" || a.Description != "") {
t.Fatalf("redo not pending: %+v", a)
}
}
})
}
}
func TestDiscardRestoreEmptyArchiveNeverCreatesEmptyTrack(t *testing.T) {
for _, reset := range []bool{false, true} {
t.Run(map[bool]string{false: "legacy-client", true: "explicit-reset"}[reset], func(t *testing.T) {
f := continuousFixture(t)
dispositionFixturePayload(t, f)
item := f.item
discarded := domain.SliceSegment{ID: "a", Start: 0, End: 10, DeliveryDisposition: "discard", DiscardReason: "legacy"}
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": []domain.SliceSegment{discarded}, "segments": []domain.AnnotationSegment{}}
f.store.UpsertHumanItem(f.ctx, item)
kept := discarded
kept.DeliveryDisposition = "keep"
kept.Name = "测试切片"
in := sliceEditApplicationInput{ApplicationID: "empty-restore", combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: item.Version, SliceSegments: []domain.SliceSegment{kept}}, Dispositions: []sliceDispositionInput{{SliceID: "a", Disposition: "keep", ResetToPending: reset}}}
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-edit-applications", f.annotator, in)
if r.Code != 200 {
t.Fatalf("restore %d %s", r.Code, r.Body.String())
}
saved, _ := f.store.GetHumanItem(f.ctx, item.ID)
actions := decodeMaterializationValue[domain.AnnotationSegment](annotationWorkspacePayload(saved)["segments"])
if len(actions) != 1 || actions[0].Start != 0 || actions[0].End != 10 || actions[0].Status != "pending" {
t.Fatalf("empty restored track: %+v", actions)
}
})
}
}