droboticflow/internal/api/annotation_source_full_admission_test.go

144 lines
5.8 KiB
Go

package api
import (
"context"
"encoding/json"
"net/http"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
"droboticflow/internal/config"
"droboticflow/internal/domain"
)
func TestSourceValidationFullProofPrecedesLegacyClaimAndPreservesOwnedWork(t *testing.T) {
for _, owned := range []bool{false, true} {
t.Run(map[bool]string{true: "owned", false: "unclaimed"}[owned], func(t *testing.T) {
t.Setenv("ANNOTATION_PROCESSOR_MODE", "external")
t.Setenv("ANNOTATION_PROCESSOR_ROLE", "validation")
f, root := annotationProcessorFixture(t)
f.item.Subject.Payload["annotationWorkspace"] = map[string]any{
"sliceSegments": []domain.SliceSegment{{ID: "retained-slice", Name: "测试切片", Start: 0, End: 4}},
"segments": []domain.AnnotationSegment{{ID: "retained-action", Start: 0, End: 4, Description: "保留的人工标注", Status: "described"}},
"userId": f.annotator.ID, "savedAt": nowISO(),
}
f.item.Subject.Payload["annotationDraft"] = map[string]any{
"sliceSegments": []domain.SliceSegment{{ID: "retained-slice", Name: "测试切片", Start: 0, End: 4}},
"segments": []domain.AnnotationSegment{{ID: "newer-draft", Start: 0, End: 4, Description: "较新的本地保存内容", Status: "described"}},
"userId": f.annotator.ID, "savedAt": nowISO(),
}
if !owned {
f.item.Status, f.item.AssignedUserID = domain.HumanItemReady, ""
}
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
t.Fatal(err)
}
before, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
originals := map[string][]byte{}
// Remove the matching frame and hardware timestamp from both eyes.
// Counts still match, and a hardware-time gap is legal; only the
// complete media-clock check catches the retained presentation gap.
for _, side := range []string{"left", "right"} {
path := filepath.Join(root, side+".mp4")
pts := filepath.Join(root, side+"_pts.csv")
for _, name := range []string{path, pts} {
body, err := os.ReadFile(name)
if err != nil {
t.Fatal(err)
}
originals[name] = body
}
if out, err := exec.Command("ffmpeg", "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=s=64x48:r=8:d=4", "-vf", "select=not(eq(n\\,16))", "-fps_mode", "vfr", "-c:v", "libx264", "-pix_fmt", "yuv420p", path).CombinedOutput(); err != nil {
t.Fatalf("damaged fixture: %v %s", err, out)
}
if err := os.WriteFile(pts, []byte(strings.Replace(string(originals[pts]), "3000000\n", "", 1)), 0600); err != nil {
t.Fatal(err)
}
}
engine := &server{store: f.store, workspaceMountPath: root}
if _, err := engine.annotationFrameTimelineForTask(f.ctx, f.task, "left"); err != nil {
t.Fatalf("fixture did not reach the weaker legacy timeline path: %v", err)
}
now := time.Now().UTC()
old, err := f.store.ClaimAnnotationIndex(f.ctx, "legacy-validator", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
if err != nil || old == nil {
t.Fatal("missing validation fixture", err)
}
old.Status, old.RevalidateSource, old.SourceFingerprint, old.Attempt = "queued", false, "", 4
if ok, err := f.store.FinishAnnotationIndex(f.ctx, *old); !ok || err != nil {
t.Fatal(err)
}
// A fresh processor instance must finish the actual source check,
// including old queued records without the new validation flag.
runUntil := func(want string) {
t.Helper()
ctx, cancel := context.WithCancel(t.Context())
done := make(chan error, 1)
go func() {
done <- RunAnnotationProcessor(ctx, f.store, config.AppConfig{WorkspaceMountPath: root}, 1, false)
}()
defer func() { cancel(); <-done }()
deadline := time.Now().Add(8 * time.Second)
for time.Now().Before(deadline) {
job, err := f.store.GetAnnotationIndexJob(f.ctx, f.task.ID)
if err != nil {
t.Fatal(err)
}
if job.Status == want {
return
}
time.Sleep(10 * time.Millisecond)
}
t.Fatalf("validation did not reach %s", want)
}
runUntil("failed")
job, _ := f.store.GetAnnotationIndexJob(f.ctx, f.task.ID)
if job.Error == "" || job.SourceFingerprint != "" {
t.Fatal("invalid source published a success proof")
}
if got := claimRandom(t, f, "full-source-failed-01"); got.ID != "" {
t.Fatal("failed source reached a new annotator")
}
after, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
if !reflect.DeepEqual(before, after) {
t.Fatal("failed validation/claim changed saved annotations or ownership")
}
for path, original := range originals {
if err := os.WriteFile(path, original, 0600); err != nil {
t.Fatal(err)
}
}
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/source-validation-retries", f.admin, map[string]any{"sourceRepaired": true})
if r.Code != 202 {
t.Fatalf("repair: %d %s", r.Code, r.Body.String())
}
runUntil("completed")
job, _ = f.store.GetAnnotationIndexJob(f.ctx, f.task.ID)
if job.SourceFingerprint == "" || !job.RevalidateSource {
t.Fatal("complete validation did not publish identity-bound proof")
}
after, _ = f.store.GetHumanItem(f.ctx, f.item.ID)
if !reflect.DeepEqual(before, after) {
t.Fatal("repair overwrote annotation/workspace/draft/ownership")
}
r = f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"?view=status", f.admin, nil)
var status struct {
SourceValidation annotationSourceValidation `json:"sourceValidation"`
}
if r.Code != 200 || json.Unmarshal(r.Body.Bytes(), &status) != nil || status.SourceValidation.Status != "valid" {
t.Fatalf("invalid repaired status: %d %s", r.Code, r.Body.String())
}
if !owned {
if got := claimRandom(t, f, "full-source-passed-01"); got.ID != f.item.ID {
t.Fatal("fully verified task could not be claimed")
}
}
})
}
}