droboticflow/internal/api/slice_edit_legacy_undo_test.go

158 lines
7.0 KiB
Go

package api
import (
"encoding/json"
"net/http"
"testing"
"droboticflow/internal/domain"
)
func TestLegacySliceEditUndoRedoAfterSuccessfulEdit(t *testing.T) {
for _, rework := range []bool{false, true} {
for _, key := range []string{"annotationWorkspace", "annotationDraft"} {
t.Run(map[bool]string{false: "first-pass", true: "rework"}[rework]+"/"+key, func(t *testing.T) {
f := continuousFixture(t)
item := f.item
item.CurrentRevision = 1
if rework {
item.ReviewRound = 1
task := f.task
task.Status = domain.AnnotationTaskStatusRework
if err := f.store.UpsertAnnotationTask(f.ctx, task); err != nil {
t.Fatal(err)
}
}
delete(item.Subject.Payload, "sliceWorkflow")
original := []domain.SliceSegment{{ID: "whole", Name: "测试切片", Start: 0, End: 10}}
split := []domain.SliceSegment{{ID: "left", Name: "测试切片", Start: 0, End: 5}, {ID: "right", Name: "测试切片", Start: 5, End: 10}}
actions := []domain.AnnotationSegment{{ID: "a", Start: 0, End: 5, Status: "pending"}, {ID: "b", Start: 5, End: 10, Status: "described", Description: "保留描述"}}
item.Subject.Payload[key] = map[string]any{"sliceSegments": original, "segments": actions}
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
t.Fatal(err)
}
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID
body := combinedAnnotationDraftInput{Version: item.Version, SliceSegments: split, Segments: actions}
r := f.request("POST", path+"/slice-edit-applications", f.annotator, sliceEditApplicationInput{ApplicationID: "legacy-split", combinedAnnotationDraftInput: body})
if r.Code != 200 {
t.Fatalf("edit %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)
}
for _, target := range [][]domain.SliceSegment{original, split, original} {
body.Version = response.Item.Version
body.SliceSegments = target
r = f.request("POST", path+"/slice-edit-restorations", f.annotator, body)
if r.Code != 200 {
t.Fatalf("undo/redo %d %s", r.Code, r.Body.String())
}
if err := json.Unmarshal(r.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
if len(response.Workspace.SliceSegments) != len(target) || len(response.Workspace.Segments) != 2 || response.Workspace.Segments[1].Description != "保留描述" {
t.Fatal("snapshot or actions lost")
}
if response.Item.Subject.Payload["sliceWorkflow"] != nil {
t.Fatal("legacy restore invented workflow")
}
// A lost response cannot be retried with the old version and overwrite the new state.
stale := f.request("POST", path+"/slice-edit-restorations", f.annotator, body)
if stale.Code != http.StatusConflict {
t.Fatalf("stale accepted %d", stale.Code)
}
saved, _ := f.store.GetHumanItem(f.ctx, item.ID)
if saved.Version != response.Item.Version {
t.Fatal("stale changed version")
}
f.handler = NewHandler(f.store, nil)
}
})
}
}
}
func TestLegacySliceUndoUsesStoredEvidenceAndKeepsSafetyGuards(t *testing.T) {
for _, scenario := range []string{"all-discarded-generated", "partial-generated", "client-only-evidence", "no-actions-no-generation", "full-video-no-generation", "explicit-missing-phase", "explicit-materializing", "workflow-failed", "job-queued", "job-running", "job-processing", "job-failed", "explicit-phase-job-failed", "submission", "review-stage", "nonowner"} {
t.Run(scenario, func(t *testing.T) {
f := continuousFixture(t)
item := f.item
item.CurrentRevision = 1
delete(item.Subject.Payload, "sliceWorkflow")
slices := []domain.SliceSegment{{ID: "whole", Name: "测试切片", Start: 0, End: 10}}
actions := []domain.AnnotationSegment{{ID: "a", Start: 0, End: 10, Status: "pending"}}
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": slices, "segments": actions}
want := http.StatusConflict
switch scenario {
case "all-discarded-generated":
slices[0].DeliveryDisposition = "discard"
slices[0].DiscardReason = "历史舍弃"
actions = nil
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": slices, "segments": actions}
item.Subject.Payload["materialization"] = map[string]any{"status": "completed", "revision": 1, "artifactId": "server-generated", "runId": f.task.RunID}
want = 200
case "partial-generated":
actions = actions[:1]
actions[0].End = 4
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": slices, "segments": actions}
item.Subject.Payload["materialization"] = map[string]any{"status": "completed", "revision": 1, "artifactId": "server-generated", "runId": f.task.RunID}
want = 200
case "client-only-evidence":
delete(item.Subject.Payload, "annotationWorkspace")
case "no-actions-no-generation":
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": slices}
case "full-video-no-generation":
task := f.task
task.SourceIsFullVideo = true
if err := f.store.UpsertAnnotationTask(f.ctx, task); err != nil {
t.Fatal(err)
}
case "explicit-missing-phase":
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed"}
case "explicit-materializing":
item.Subject.Payload["sliceWorkflow"] = map[string]any{"phase": "materializing"}
case "workflow-failed":
item.Subject.Payload["sliceWorkflow"] = map[string]any{"phase": "annotate", "status": "failed"}
case "job-queued", "job-running", "job-processing", "job-failed":
item.ProcessingJob = &domain.AnnotationProcessingJob{Status: scenario[4:]}
case "explicit-phase-job-failed":
item.Subject.Payload["sliceWorkflow"] = map[string]any{"phase": "annotate"}
item.ProcessingJob = &domain.AnnotationProcessingJob{Status: "failed"}
case "submission":
item.PendingCombinedSubmissionID = "pending"
case "review-stage":
item.CurrentStageID = domain.FanyuV58StageQualityReview
case "nonowner":
item.AssignedUserID = f.admin.ID
}
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
t.Fatal(err)
}
storedBefore, err := f.store.GetHumanItem(f.ctx, item.ID)
if err != nil {
t.Fatal(err)
}
before, _ := json.Marshal(storedBefore)
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-edit-restorations", f.annotator, combinedAnnotationDraftInput{Version: item.Version, SliceSegments: slices, Segments: actions})
if r.Code != want {
t.Fatalf("want %d got %d %s", want, r.Code, r.Body.String())
}
saved, _ := f.store.GetHumanItem(f.ctx, item.ID)
if want == 200 {
if saved.Version != item.Version+1 || saved.Subject.Payload["sliceWorkflow"] != nil {
t.Fatal("restore failed or invented workflow")
}
} else {
after, _ := json.Marshal(saved)
if string(before) != string(after) {
t.Fatal("rejected restore mutated server state")
}
}
})
}
}