244 lines
11 KiB
Go
244 lines
11 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSliceDiscardMaterialization60Seconds(t *testing.T) {
|
|
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
|
t.Skip("ffmpeg required")
|
|
}
|
|
root := os.Getenv("FANYU_DISCARD_ARTIFACT_DIR")
|
|
if root == "" {
|
|
root = t.TempDir()
|
|
}
|
|
if !filepath.IsAbs(root) || filepath.Clean(root) == "/" {
|
|
t.Fatal("explicit fixture output directory required")
|
|
}
|
|
if err := os.MkdirAll(filepath.Join(root, "fixtures"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fixtures := filepath.Join(root, "fixtures")
|
|
ctx := context.Background()
|
|
db := store.NewMemoryStore()
|
|
pts, imu := "frame_index,timestamp_us\n", "timestamp_us,x,y,z,type\n"
|
|
for i := 0; i < 240; i++ {
|
|
ts := 1000000 + i*250000
|
|
pts += fmt.Sprintf("%d,%d\n", i, ts)
|
|
imu += fmt.Sprintf("%d,0,0,9.8,accel\n%d,0,0,0,gyro\n", ts, ts)
|
|
}
|
|
artifacts := []domain.ExecutionArtifact{}
|
|
bindings := []domain.NodeInputBinding{}
|
|
for _, camera := range []string{"left", "right"} {
|
|
video := filepath.Join(fixtures, "capture_"+camera+".mp4")
|
|
command := exec.Command("ffmpeg", "-nostdin", "-hide_banner", "-loglevel", "error", "-y", "-f", "lavfi", "-i", "testsrc2=s=160x120:r=4:d=60", "-c:v", "libx264", "-pix_fmt", "yuv420p", video)
|
|
if output, err := command.CombinedOutput(); err != nil {
|
|
t.Fatalf("fixture video: %v %s", err, output)
|
|
}
|
|
ptsPath := filepath.Join(fixtures, "capture_"+camera+"_pts.csv")
|
|
if err := os.WriteFile(ptsPath, []byte(pts), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
v := domain.ExecutionArtifact{ID: camera + "-video", Name: filepath.Base(video), Path: video, Type: domain.PortTypeVideo, Data: map[string]any{"collectionId": "capture"}}
|
|
p := domain.ExecutionArtifact{ID: camera + "-pts", Name: filepath.Base(ptsPath), Path: ptsPath, Type: domain.PortTypeFile, Data: map[string]any{"collectionId": "capture"}}
|
|
artifacts = append(artifacts, v, p)
|
|
prefix := "Left"
|
|
if camera == "right" {
|
|
prefix = "Right"
|
|
}
|
|
bindings = append(bindings, domain.NodeInputBinding{TargetPortID: "full" + prefix + "Videos", Artifacts: []domain.ExecutionArtifact{v}}, domain.NodeInputBinding{TargetPortID: camera + "Timestamps", Artifacts: []domain.ExecutionArtifact{p}})
|
|
}
|
|
imuPath := filepath.Join(fixtures, "capture_imu.csv")
|
|
if err := os.WriteFile(imuPath, []byte(imu), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
imuArtifact := domain.ExecutionArtifact{ID: "imu", Name: "capture_imu.csv", Path: imuPath, Type: domain.PortTypeFile, Data: map[string]any{"collectionId": "capture"}}
|
|
artifacts = append(artifacts, imuArtifact)
|
|
bindings = append(bindings, domain.NodeInputBinding{TargetPortID: "imu", Artifacts: []domain.ExecutionArtifact{imuArtifact}})
|
|
run := domain.RunRecord{ID: "run-discard-60s", ProjectID: "project-discard-test", OrganizationID: domain.DefaultOrganizationID, WorkflowID: "workflow-discard-test", Status: domain.RunStatusWaitingForHuman, NodeExecutions: []domain.NodeExecutionRecord{{NodeID: "human", Status: domain.NodeExecutionStatusWaitingForHuman, InputArtifacts: artifacts, InputBindings: bindings}}}
|
|
if err := db.UpsertRun(ctx, run); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := &server{store: db, workspaceMountPath: root}
|
|
task := domain.AnnotationTaskRecord{ID: "task-discard-60s", OrganizationID: run.OrganizationID, ProjectID: run.ProjectID, WorkflowID: run.WorkflowID, RunID: run.ID, NodeID: "human", ArtifactID: "left-video", ClipStart: 0, ClipEnd: 60, SourceIsFullVideo: true, SourceDurationSeconds: 60, TemplateVersion: 6}
|
|
item := domain.HumanItemRecord{ID: "item-discard-60s", OrganizationID: run.OrganizationID, RunID: run.ID, GateNodeID: "human", Subject: domain.HumanSubjectRef{ItemKey: "capture", Payload: map[string]any{}}}
|
|
evidence := map[string]any{}
|
|
videoSHA := ""
|
|
for revision, mode := range []string{"keep", "mixed", "discard", "blank", "partial"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
slices := []domain.SliceSegment{}
|
|
segments := []domain.AnnotationSegment{}
|
|
for i := 0; i < 3; i++ {
|
|
start, end := float64(i*20), float64((i+1)*20)
|
|
sl := domain.SliceSegment{ID: fmt.Sprintf("segment-%d", i), Name: fmt.Sprintf("片段 %d", i+1), Start: start, End: end}
|
|
sl.Dictionary = &domain.DictionarySelection{ID: fmt.Sprintf("base-%d", i), Kind: "slice", Version: 2, Code: fmt.Sprintf("class-%d", i), Name: sl.Name, Tags: []string{"基础分类", "商品"}}
|
|
if mode == "discard" || (mode == "mixed" && i == 1) {
|
|
sl.DeliveryDisposition = "discard"
|
|
sl.DiscardReason = "审查测试:画面被遮挡"
|
|
sl.DiscardReasonLabelID = "reason-test"
|
|
sl.DiscardReasonVersion = 3
|
|
sl.Dictionary = nil
|
|
sl.DiscardReasonDictionary = &domain.DictionarySelection{ID: "reason-test", Kind: "discard_reason", Version: 3, Code: "occluded", Name: "审查测试", Tags: []string{"不可用"}}
|
|
sl.DiscardReasonName = "审查测试"
|
|
sl.DiscardReasonNote = "画面被遮挡"
|
|
sl.DiscardedAt = "2026-09-16T00:00:00Z"
|
|
sl.DiscardedByUserID = "test-annotator"
|
|
} else if mode == "partial" && i == 0 {
|
|
segments = append(segments, domain.AnnotationSegment{ID: "partial", Start: 5, End: 10, Description: "仅描述这五秒", Source: "human"})
|
|
} else if mode != "blank" && mode != "partial" {
|
|
segments = append(segments, domain.AnnotationSegment{ID: fmt.Sprintf("action-%d", i), Start: start, End: end, Description: "测试动作", Dictionary: &domain.DictionarySelection{ID: "action-test", Kind: "action", Version: 4, Code: "pick", Name: "拿取", Tags: []string{"抓取"}}, Source: "human"})
|
|
}
|
|
slices = append(slices, sl)
|
|
}
|
|
result, err := s.materializeCombinedSliceSubmission(ctx, task, item, combinedAnnotationSubmissionInput{SliceSegments: slices, Segments: segments}, revision+1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if revision > 0 && intValue(result.Artifact.Data["reusedSliceCount"]) != 3 {
|
|
t.Fatal("unchanged media not reused across annotation/disposition revisions")
|
|
}
|
|
sha, _, err := sha256File(filepath.Join(result.Artifact.Path, "slices", "slice-000001", "left.mp4"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if revision == 0 {
|
|
videoSHA = sha
|
|
} else if sha != videoSHA {
|
|
t.Fatal("reused video bytes changed")
|
|
}
|
|
content, err := os.ReadFile(filepath.Join(result.Artifact.Path, "revision-manifest.json"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var m map[string]any
|
|
if err = json.Unmarshal(content, &m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m["coverage"].(map[string]any)["durationSeconds"] != float64(60) || len(m["items"].([]any)) != 3 {
|
|
t.Fatal("review coverage not preserved")
|
|
}
|
|
for i, raw := range m["items"].([]any) {
|
|
row := raw.(map[string]any)
|
|
dir := filepath.Join(result.Artifact.Path, filepath.FromSlash(row["directory"].(string)))
|
|
for _, name := range []string{"left.mp4", "right.mp4", "left_pts.csv", "right_pts.csv", "imu.csv", "annotation.json", "synchronized-slice.json"} {
|
|
if st, e := os.Stat(filepath.Join(dir, name)); e != nil || st.Size() == 0 {
|
|
t.Fatalf("missing review evidence %d/%s", i, name)
|
|
}
|
|
}
|
|
for _, cam := range []string{"left", "right"} {
|
|
if n, e := ffprobeFrameCount(ctx, filepath.Join(dir, cam+".mp4")); e != nil || n != 80 {
|
|
t.Fatalf("frame evidence %s: %d %v", cam, n, e)
|
|
}
|
|
}
|
|
if slices[i].IsDiscarded() {
|
|
b, e := os.ReadFile(filepath.Join(dir, "synchronized-slice.json"))
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
var sync map[string]any
|
|
if e = json.Unmarshal(b, &sync); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
reason := mapValue(sync["discardEvidence"])
|
|
if reason["reasonLabelId"] != "reason-test" || intValue(reason["reasonVersion"]) != 3 || reason["reasonNote"] != "画面被遮挡" {
|
|
t.Fatalf("lost audit snapshot: %+v", reason)
|
|
}
|
|
}
|
|
if slices[i].IsDiscarded() || mode == "blank" || (mode == "partial" && i > 0) {
|
|
b, _ := os.ReadFile(filepath.Join(dir, "annotation.json"))
|
|
var a map[string]any
|
|
json.Unmarshal(b, &a)
|
|
if len(a["actions"].([]any)) != 0 {
|
|
t.Fatal("blank or discarded slice contains invented annotations")
|
|
}
|
|
}
|
|
}
|
|
result.Artifact.PortID = "acceptedSliceDirectory"
|
|
result.Artifact.PortName = "acceptedSliceDirectory"
|
|
evidence[mode] = map[string]any{"artifact": result.Artifact, "materialization": result.Payload}
|
|
})
|
|
}
|
|
t.Run("reuse-integrity-and-boundaries", func(t *testing.T) {
|
|
slices := []domain.SliceSegment{{ID: "a", Name: "新名称", Start: 0, End: 20}, {ID: "b", Name: "新名称", Start: 20, End: 40}, {ID: "c", Name: "新名称", Start: 40, End: 60}}
|
|
result, err := s.materializeCombinedSliceSubmission(ctx, task, item, combinedAnnotationSubmissionInput{SliceSegments: slices}, 6)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if intValue(result.Artifact.Data["reusedSliceCount"]) != 3 {
|
|
t.Fatal("rename recoded media")
|
|
}
|
|
bad := filepath.Join(result.Artifact.Path, "slices", "slice-000001", "left.mp4")
|
|
if err := os.WriteFile(bad, []byte("corrupt test cache"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err = s.materializeCombinedSliceSubmission(ctx, task, item, combinedAnnotationSubmissionInput{SliceSegments: slices}, 7)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if intValue(result.Artifact.Data["reusedSliceCount"]) != 2 {
|
|
t.Fatal("corrupt cache was trusted or unaffected slices recoded")
|
|
}
|
|
old := filepath.Join(root, "system", "runs", task.RunID, "human-materializations", item.ID, task.ID, "revision-000006")
|
|
if err := os.RemoveAll(old); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if count, e := ffprobeFrameCount(ctx, filepath.Join(result.Artifact.Path, "slices", "slice-000001", "left.mp4")); e != nil || count != 80 {
|
|
t.Fatal("old generation cleanup broke current media", e)
|
|
}
|
|
slices[0].End = 10
|
|
slices[1].Start = 10
|
|
result, err = s.materializeCombinedSliceSubmission(ctx, task, item, combinedAnnotationSubmissionInput{SliceSegments: slices}, 8)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if intValue(result.Artifact.Data["reusedSliceCount"]) != 1 {
|
|
t.Fatal("changed boundaries reused wrong video")
|
|
}
|
|
})
|
|
// Caching must not alter hardware boundaries; source replacement invalidates
|
|
// the grid even when artifact IDs remain unchanged.
|
|
first, err := s.annotationFrameTimelineForTask(ctx, task, "left")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
second, err := s.annotationFrameTimelineForTask(ctx, task, "left")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if first["sourceFingerprint"] != second["sourceFingerprint"] {
|
|
t.Fatal("stable source cache missed")
|
|
}
|
|
stamp := time.Now().Add(time.Minute)
|
|
if err := os.Chtimes(imuPath, stamp, stamp); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
third, err := s.annotationFrameTimelineForTask(ctx, task, "left")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if first["sourceFingerprint"] == third["sourceFingerprint"] {
|
|
t.Fatal("source change did not invalidate")
|
|
}
|
|
if !reflect.DeepEqual(first["boundaryTimestampUs"], third["boundaryTimestampUs"]) {
|
|
t.Fatal("cache changed boundaries")
|
|
}
|
|
if os.Getenv("FANYU_DISCARD_ARTIFACT_DIR") != "" {
|
|
body, _ := json.MarshalIndent(evidence, "", " ")
|
|
if err := os.WriteFile(filepath.Join(root, "verification-context.json"), body, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Log("real materialization fixtures:", strings.TrimSpace(root))
|
|
}
|
|
}
|