droboticflow/internal/api/annotation_availability_test.go

258 lines
11 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package api
import (
"encoding/json"
"net/http"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
"droboticflow/internal/config"
"droboticflow/internal/domain"
)
func TestSourceAdvisoryPublishedReviewDoesNotRequireFullAudit(t *testing.T) {
t.Setenv("ANNOTATION_SOURCE_VALIDATION_MODE", "advisory")
t.Setenv("ANNOTATION_PROCESSOR_MODE", "external")
f, root := annotationProcessorFixture(t)
f.item.CurrentStageID = "quality_review"
f.item.Subject.Payload["annotationWorkspace"] = map[string]any{"description": "保留已有标注"}
if e := f.store.UpsertHumanItem(f.ctx, f.item); e != nil {
t.Fatal(e)
}
before, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
j := &domain.AnnotationIndexJob{ID: f.task.ID, OrganizationID: f.task.OrganizationID, ProjectID: f.task.ProjectID, RunID: f.task.RunID, Status: "failed", Error: "source video duration does not match its fixed frame cadence"}
probe := &sourceProofReadStore{MemoryStore: f.store, timeline: j}
f.handler = NewHandlerWithConfig(probe, nil, config.AppConfig{WorkspaceMountPath: root})
r := f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"?view=status", f.admin, nil)
var response struct {
SourceValidation annotationSourceValidation `json:"sourceValidation"`
}
if r.Code != 200 || json.Unmarshal(r.Body.Bytes(), &response) != nil || response.SourceValidation.blocked() || response.SourceValidation.Required {
t.Fatalf("review remains blocked: %d %s", r.Code, r.Body.String())
}
r = f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/frame-timeline", f.admin, nil)
if r.Code != 200 {
t.Fatalf("review timeline: %d %s", r.Code, r.Body.String())
}
after, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
if probe.taskWrites != 0 || !reflect.DeepEqual(before, after) {
t.Fatal("review read rewrote saved work")
}
}
func TestSourceAdvisoryPreparesIndexDespiteCadenceAuditFailure(t *testing.T) {
t.Setenv("ANNOTATION_SOURCE_VALIDATION_MODE", "advisory")
f, root := annotationProcessorFixture(t)
makeCadenceAuditFailure(t, root)
s := &server{store: f.store, workspaceMountPath: root}
run, _ := f.store.GetRun(f.ctx, f.task.RunID)
fp, e := s.annotationSourceFingerprint(f.ctx, f.task, *run)
if e != nil {
t.Fatal(e)
}
if e = s.requirePreparedAnnotationSource(f.ctx, f.task, fp, false); e != nil {
t.Fatalf("operational source blocked by audit: %v", e)
}
full, _ := f.store.GetAnnotationTimelineIndex(f.ctx, sourceCertificateID(f.task.OrganizationID, fp))
if full != nil {
t.Fatal("basic index masquerades as full source certificate")
}
index, _ := f.store.GetAnnotationTimelineIndex(f.ctx, "annotation-processing-v1:"+f.task.OrganizationID+":"+fp)
if index == nil {
t.Fatal("missing durable processing index")
}
if _, e = decodeSourceCertificate(index, fp); e == nil {
t.Fatal("strict decoder accepted an operational index")
}
duration, e := ffprobeDurationSeconds(f.ctx, filepath.Join(root, "left.mp4"))
if e != nil {
t.Fatal(e)
}
if e = validateSourceMediaClock(f.ctx, filepath.Join(root, "left.mp4"), 31, duration); e == nil {
t.Fatal("strict audit regression was not exercised")
}
if _, e = s.annotationSourceIndexForMode(f.ctx, f.task, *run, fp, false); e == nil {
t.Fatal("explicit full audit reused an operational index")
}
t.Setenv("PATH", t.TempDir())
restarted := &server{store: f.store, workspaceMountPath: root}
if e = restarted.requirePreparedAnnotationSource(f.ctx, f.task, fp, false); e != nil {
t.Fatalf("restart failed to reuse operational index without media tools: %v", e)
}
}
func makeCadenceAuditFailure(t *testing.T, root string) {
t.Helper()
for _, side := range []string{"left", "right"} {
video := filepath.Join(root, side+".mp4")
if out, e := 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", video).CombinedOutput(); e != nil {
t.Fatalf("fixture: %v %s", e, out)
}
pts := filepath.Join(root, side+"_pts.csv")
body, e := os.ReadFile(pts)
if e != nil {
t.Fatal(e)
}
if e = os.WriteFile(pts, []byte(strings.Replace(string(body), "3000000\n", "", 1)), 0600); e != nil {
t.Fatal(e)
}
}
}
func TestSourceAdvisoryCompleteWorkAndReviewFlow(t *testing.T) {
t.Setenv("ANNOTATION_SOURCE_VALIDATION_MODE", "advisory")
t.Setenv("ANNOTATION_SOURCE_INDEX", "true")
t.Setenv("ANNOTATION_PROCESSOR_MODE", "external")
f, root := annotationProcessorFixture(t)
makeCadenceAuditFailure(t, root)
one, zero := 1.0, 0.0
f.item.AcceptanceSampleProbability = &one
f.item.AcceptanceSampleValue = &zero
if e := f.store.UpsertHumanItem(f.ctx, f.item); e != nil {
t.Fatal(e)
}
batch, _ := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
batch.RequirePrePackageAcceptance = true
if e := f.store.UpsertHumanBatch(f.ctx, *batch); e != nil {
t.Fatal(e)
}
engine := &server{store: f.store, workspaceMountPath: root}
timeline, e := engine.annotationFrameTimelineForTask(f.ctx, f.task, "left")
if e != nil {
t.Fatal(e)
}
points := timeline["boundariesSeconds"].([]float64)
selected := []domain.SliceSegment{{ID: "a", Name: "测试切片", Start: points[0], End: points[len(points)/2]}, {ID: "b", Name: "测试切片", Start: points[len(points)/2], End: points[len(points)-1]}}
testDurableAnnotationOutbox(t, f, root, selected)
h, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
r := f.request(http.MethodPost, "/api/human-items/"+h.ID+"/claim", f.annotator, map[string]any{"version": h.Version})
if r.Code != 403 {
t.Fatalf("annotator can self-review: %d %s", r.Code, r.Body.String())
}
promoted := f.annotator
promoted.Role = domain.UserRoleQualityReviewer
if e := f.store.UpsertUser(f.ctx, promoted); e != nil {
t.Fatal(e)
}
r = f.request(http.MethodPost, "/api/human-items/"+h.ID+"/claim", promoted, map[string]any{"version": h.Version})
if r.Code != 403 {
t.Fatalf("promoted author can self-review: %d %s", r.Code, r.Body.String())
}
if e := f.store.UpsertUser(f.ctx, f.annotator); e != nil {
t.Fatal(e)
}
r = f.request(http.MethodPost, "/api/human-items/"+h.ID+"/claim", f.reviewer, map[string]any{"version": h.Version - 1})
if r.Code != 409 {
t.Fatalf("stale version admitted: %d %s", r.Code, r.Body.String())
}
r = f.request(http.MethodPost, "/api/human-items/"+h.ID+"/claim", f.reviewer, map[string]any{"version": h.Version})
if r.Code != 200 {
t.Fatalf("quality claim: %d %s", r.Code, r.Body.String())
}
for _, side := range []string{"left", "right"} {
if e := os.Remove(filepath.Join(root, side+".mp4")); e != nil {
t.Fatal(e)
}
}
f.handler = NewHandlerWithConfig(f.store, nil, config.AppConfig{WorkspaceMountPath: root})
r = f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/frame-timeline", f.reviewer, nil)
if r.Code != 200 {
t.Fatalf("published review depends on removed originals: %d %s", r.Code, r.Body.String())
}
r = f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/reviews", f.reviewer, map[string]any{"stage": "quality", "decision": "approved"})
if r.Code != 201 {
t.Fatalf("quality review: %d %s", r.Code, r.Body.String())
}
var aggregate *domain.HumanItemRecord
deadline := time.Now().Add(3 * time.Second)
for time.Now().Before(deadline) {
aggregate, e = f.store.GetHumanItem(f.ctx, fanyuV5PrePackageAcceptanceItemID(f.item.BatchID))
if e != nil || aggregate != nil {
break
}
time.Sleep(10 * time.Millisecond)
}
if e != nil || aggregate == nil {
recovery := &server{store: f.store, workspaceMountPath: root, annotationSourceIndex: true}
_, e = recovery.ensureFanyuV5PrePackageAcceptance(f.ctx, batch)
aggregate, _ = f.store.GetHumanItem(f.ctx, fanyuV5PrePackageAcceptanceItemID(f.item.BatchID))
if e != nil || aggregate == nil {
t.Fatalf("missing final acceptance: %v", e)
}
}
acceptance := domain.UserRecord{ID: "availability-acceptance", OrganizationID: f.task.OrganizationID, Role: domain.UserRoleAcceptanceReviewer, Status: domain.UserStatusActive, SupplierID: f.annotator.SupplierID, AcceptanceAccessMode: "managed"}
if e = f.store.UpsertUser(f.ctx, acceptance); e != nil {
t.Fatal(e)
}
if _, e = f.store.SetSupplierAcceptanceAuthorization(f.ctx, f.task.OrganizationID, f.annotator.SupplierID, true, 0, f.admin.ID); e != nil {
t.Fatal(e)
}
if e = f.store.UpsertProjectAnnotationMembership(f.ctx, domain.ProjectAnnotationMembershipRecord{ID: "availability-acceptance-membership", OrganizationID: f.task.OrganizationID, ProjectID: f.task.ProjectID, UserID: acceptance.ID, AssignmentRole: domain.AnnotationRoleAcceptanceReviewer}); e != nil {
t.Fatal(e)
}
r = f.request(http.MethodPost, "/api/human-items/"+aggregate.ID+"/claim", f.reviewer, map[string]any{"version": aggregate.Version})
if r.Code != 403 {
t.Fatalf("quality role reached acceptance: %d %s", r.Code, r.Body.String())
}
r = f.request(http.MethodPost, "/api/human-items/"+aggregate.ID+"/claim", acceptance, map[string]any{"version": aggregate.Version})
if r.Code != 200 {
t.Fatalf("acceptance claim: %d %s", r.Code, r.Body.String())
}
aggregate, _ = f.store.GetHumanItem(f.ctx, aggregate.ID)
r = f.request(http.MethodPost, "/api/human-items/"+aggregate.ID+"/decisions", acceptance, map[string]any{"version": aggregate.Version, "decision": "approved", "poseDelivery": map[string]any{"headpose": "exclude", "handpose": "exclude", "reasonCodes": []string{"not_required"}}})
if r.Code != 200 {
t.Fatalf("final acceptance: %d %s", r.Code, r.Body.String())
}
aggregate, _ = f.store.GetHumanItem(f.ctx, aggregate.ID)
if aggregate.Status != domain.HumanItemAccepted {
t.Fatalf("acceptance not published: %s", aggregate.Status)
}
}
func TestSourceAdvisoryIsolatedReworkDoesNotTrapRandomClaim(t *testing.T) {
t.Setenv("ANNOTATION_SOURCE_VALIDATION_MODE", "advisory")
f := randomClaimFixture(t, 1, false)
f.item.ReviewRound = 1
f.item.Status = domain.HumanItemRework
f.item.ProcessingJob = &domain.AnnotationProcessingJob{Status: "failed", Error: "基础帧映射不可用:video frame count mismatch"}
f.item.Subject.Payload["annotationDraft"] = map[string]any{"description": "保留隔离任务"}
if e := f.store.UpsertHumanItem(f.ctx, f.item); e != nil {
t.Fatal(e)
}
before, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
claimed := claimRandom(t, f, "advisory-isolated-rework-claim-01")
if claimed.ID != "random-item-0" {
t.Fatalf("isolated rework trapped claim: %s", claimed.ID)
}
after, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
if !reflect.DeepEqual(before, after) {
t.Fatal("isolated task annotations/ownership changed")
}
}
func TestSourceAdvisoryOwnedFailureCanRetryTimeline(t *testing.T) {
t.Setenv("ANNOTATION_SOURCE_VALIDATION_MODE", "advisory")
f, root := annotationProcessorFixture(t)
f.item.ProcessingJob = &domain.AnnotationProcessingJob{Status: "failed", Error: "基础帧映射不可用:previous media unavailable"}
f.item.Subject.Payload["annotationDraft"] = map[string]any{"description": "保留失败标注"}
if e := f.store.UpsertHumanItem(f.ctx, f.item); e != nil {
t.Fatal(e)
}
before, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
f.handler = NewHandlerWithConfig(f.store, nil, config.AppConfig{WorkspaceMountPath: root})
r := f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/frame-timeline", f.annotator, nil)
if r.Code != 200 {
t.Fatalf("repaired mapping permanently blocked by prior job: %d %s", r.Code, r.Body.String())
}
after, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
if !reflect.DeepEqual(before, after) {
t.Fatal("re-read rewrote annotations/ownership")
}
}