forked from longtao.wu/droboticflow
336 lines
13 KiB
Go
336 lines
13 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"encoding/json"
|
|
"fmt"
|
|
"math"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSourceIndexRevisionReusesValidatedSourceWithoutMediaGeneration(t *testing.T) {
|
|
f, root := annotationProcessorFixture(t)
|
|
s := &server{store: f.store, workspaceMountPath: root, annotationSourceIndex: true}
|
|
run, _ := f.store.GetRun(f.ctx, f.task.RunID)
|
|
fingerprint, err := s.annotationSourceFingerprint(f.ctx, f.task, *run)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cert, err := s.annotationSourceCertificate(f.ctx, f.task, *run, fingerprint)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(cert.LeftPTS) != 32 || len(cert.RightPTS) != 32 {
|
|
t.Fatal("source frame index lost")
|
|
}
|
|
// A fresh process must reuse the persisted certificate. Neither revision may
|
|
// invoke ffmpeg/ffprobe or read and split the raw IMU again.
|
|
s = &server{store: f.store, workspaceMountPath: root, annotationSourceIndex: true}
|
|
t.Setenv("PATH", t.TempDir())
|
|
payload := combinedAnnotationSubmissionInput{sliceOnly: true, SliceSegments: []domain.SliceSegment{{ID: "a", Name: "测试切片", Start: 0, End: 2}, {ID: "b", Name: "测试切片", Start: 2, End: 4}}}
|
|
result, err := s.materializeCombinedSliceSubmission(f.ctx, f.task, f.item, payload, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !isSourceIndexArtifact(&result.Artifact) {
|
|
t.Fatal("physical revision generated")
|
|
}
|
|
err = filepath.WalkDir(result.Artifact.Path, func(path string, d os.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !d.IsDir() && !strings.HasSuffix(path, ".json") {
|
|
t.Errorf("unexpected materialized media/CSV: %s", path)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run, _ = f.store.GetRun(f.ctx, f.task.RunID)
|
|
doc := s.runArtifactManifestDocumentWithContext(f.ctx, run, &result.Artifact)
|
|
if !doc.Recognized || doc.Message != "" || len(doc.Members) != 10 {
|
|
t.Fatalf("virtual evidence unavailable: %+v", doc)
|
|
}
|
|
for _, m := range doc.Members {
|
|
if !m.ContentAvailable {
|
|
t.Fatalf("unavailable member: %s", m.Name)
|
|
}
|
|
}
|
|
// Recut uses the same fixed source, not a previous generation that cleanup
|
|
// can remove. Source file versions remain unchanged.
|
|
payload.SliceSegments = []domain.SliceSegment{{ID: "c", Name: "测试切片", Start: 0, End: 4}}
|
|
second, err := s.materializeCombinedSliceSubmission(context.Background(), f.task, f.item, payload, 2)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.RemoveAll(result.Artifact.Path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run, _ = f.store.GetRun(f.ctx, f.task.RunID)
|
|
doc = s.runArtifactManifestDocumentWithContext(f.ctx, run, &second.Artifact)
|
|
if !doc.Recognized || doc.Message != "" || len(doc.Members) != 5 {
|
|
t.Fatal("new revision depends on removed history", doc.Message)
|
|
}
|
|
}
|
|
|
|
func TestSourceIndexFrozenEvidenceSurvivesOriginalReplacementAndRejectsPinMutation(t *testing.T) {
|
|
f, root := annotationProcessorFixture(t)
|
|
s := &server{store: f.store, workspaceMountPath: root, annotationSourceIndex: true}
|
|
p := combinedAnnotationSubmissionInput{sliceOnly: true, SliceSegments: []domain.SliceSegment{{ID: "a", Name: "测试切片", Start: 0, End: 4}}}
|
|
result, e := s.materializeCombinedSliceSubmission(f.ctx, f.task, f.item, p, 1)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
run, _ := f.store.GetRun(f.ctx, f.task.RunID)
|
|
if e = os.WriteFile(filepath.Join(root, "left.mp4"), []byte("replacement"), 0600); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
doc := s.runArtifactManifestDocumentWithContext(f.ctx, run, &result.Artifact)
|
|
if doc.Message != "" || len(doc.Members) != 5 {
|
|
t.Fatal("frozen evidence followed replaced original", doc.Message)
|
|
}
|
|
if _, e = s.materializeCombinedSliceSubmission(f.ctx, f.task, f.item, p, 2); e == nil {
|
|
t.Fatal("changed original reused old certificate")
|
|
}
|
|
manifest, e := s.readSourceRevisionManifest(run, &result.Artifact)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
source := mapValue(mapValue(manifest["sources"])["leftVideo"])
|
|
if e = os.WriteFile(stringValue(source["path"]), []byte("corrupted pin"), 0600); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
doc = s.runArtifactManifestDocumentWithContext(f.ctx, run, &result.Artifact)
|
|
if doc.Message == "" {
|
|
t.Fatal("corrupted fixed source remained playable")
|
|
}
|
|
if _, e = s.materializeCombinedSliceSubmission(f.ctx, f.task, f.item, p, 1); e == nil {
|
|
t.Fatal("idempotent retry accepted corrupted fixed evidence")
|
|
}
|
|
}
|
|
|
|
func TestSourceIndexRejectsVideoPTSCountMismatchAndDuplicateSensorPair(t *testing.T) {
|
|
f, root := annotationProcessorFixture(t)
|
|
if e := validateSourceMediaClock(f.ctx, filepath.Join(root, "left.mp4"), 31, 4); e == nil {
|
|
t.Fatal("mismatched PTS/video admitted")
|
|
}
|
|
path := filepath.Join(root, "bad-imu.csv")
|
|
os.WriteFile(path, []byte("timestamp_us,x,y,z,type\n1,0,0,0,accel\n1,0,0,0,accel\n1,0,0,0,gyro\n2,0,0,0,accel\n2,0,0,0,gyro\n"), 0600)
|
|
if _, e := scanHardwareIMU(f.ctx, path, nil); e == nil {
|
|
t.Fatal("duplicate sensor sample admitted")
|
|
}
|
|
}
|
|
|
|
// Retained local synthetic artifact used by Python export and browser replica
|
|
// tests. Never points to BOS, a production database, or customer source files.
|
|
func TestSourceIndexReplicaExportFixture(t *testing.T) {
|
|
parent := os.Getenv("FANYU_SOURCE_INDEX_FIXTURE_DIR")
|
|
if parent == "" {
|
|
t.Skip("opt-in retained local synthetic fixture")
|
|
}
|
|
if !filepath.IsAbs(parent) {
|
|
t.Fatal("absolute local output required")
|
|
}
|
|
if e := os.MkdirAll(parent, 0700); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
out, e := os.MkdirTemp(parent, "source-index-replica-")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
f, oldRoot := annotationProcessorFixture(t)
|
|
root := filepath.Join(out, "workspace")
|
|
if e = copyAnnotationTree(f.ctx, oldRoot, root); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
run, _ := f.store.GetRun(f.ctx, f.task.RunID)
|
|
for i := range run.NodeExecutions {
|
|
for j := range run.NodeExecutions[i].InputBindings {
|
|
for k := range run.NodeExecutions[i].InputBindings[j].Artifacts {
|
|
a := &run.NodeExecutions[i].InputBindings[j].Artifacts[k]
|
|
a.Path = filepath.Join(root, filepath.Base(a.Path))
|
|
}
|
|
}
|
|
}
|
|
if e = f.store.UpsertRun(f.ctx, *run); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
s := &server{store: f.store, workspaceMountPath: root, annotationSourceIndex: true}
|
|
slices := []domain.SliceSegment{{ID: "first", Name: "测试切片", Start: 0, End: 1}, {ID: "discard", Start: 1, End: 3, DeliveryDisposition: "discard", DiscardReason: "synthetic-discard"}, {ID: "last", Name: "测试切片", Start: 3, End: 4}}
|
|
result, e := s.materializeCombinedSliceSubmission(f.ctx, f.task, f.item, combinedAnnotationSubmissionInput{sliceOnly: true, SliceSegments: slices}, 1)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
context := map[string]any{"artifact": result.Artifact, "task": f.task, "item": f.item, "workspace": root}
|
|
b, _ := json.MarshalIndent(context, "", " ")
|
|
if e = os.WriteFile(filepath.Join(out, "context.json"), b, 0600); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = os.WriteFile(filepath.Join(parent, "latest-context.txt"), []byte(filepath.Join(out, "context.json")), 0600); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
t.Log("retained synthetic fixture:", filepath.Join(out, "context.json"))
|
|
}
|
|
|
|
func TestSourceClockToleranceFollowsVideoTimeBase(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
timeBase string
|
|
want float64
|
|
}{{"1/15360", 0.0001}, {"1/90000", 0.0001}, {"1/1000", 0.0005 + 1e-9}} {
|
|
got, err := sourceClockTolerance(tc.timeBase)
|
|
if err != nil || math.Abs(got-tc.want) > 1e-12 {
|
|
t.Fatalf("tolerance(%s) = %v, %v; want %v", tc.timeBase, got, err, tc.want)
|
|
}
|
|
}
|
|
for _, bad := range []string{"", "0/1", "1/0", "x/1000", "1000"} {
|
|
if _, err := sourceClockTolerance(bad); err == nil {
|
|
t.Fatalf("tolerance(%q) accepted an invalid time base", bad)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A constant-rate 30fps camera muxed with a millisecond track timescale stores
|
|
// frame times 0, 0.033, 0.067, ... and must not be rejected as variable-rate;
|
|
// a genuinely missing frame still is.
|
|
func TestSourceClockAcceptsMillisecondTimescaleConstantRate(t *testing.T) {
|
|
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
|
t.Skip("ffmpeg unavailable")
|
|
}
|
|
ctx := t.Context()
|
|
dir := t.TempDir()
|
|
encode := func(name string, filter string) string {
|
|
path := filepath.Join(dir, name)
|
|
args := []string{"-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=s=64x48:r=30:d=4"}
|
|
if filter != "" {
|
|
args = append(args, "-vf", filter, "-fps_mode", "passthrough")
|
|
}
|
|
args = append(args, "-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-video_track_timescale", "1000", path)
|
|
if out, err := exec.CommandContext(ctx, "ffmpeg", args...).CombinedOutput(); err != nil {
|
|
t.Fatalf("encode %s: %v %s", name, err, out)
|
|
}
|
|
return path
|
|
}
|
|
constant := encode("constant.mp4", "")
|
|
duration, err := ffprobeDurationSeconds(ctx, constant)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := validateSourceMediaClock(ctx, constant, 120, duration); err != nil {
|
|
t.Fatalf("constant-rate ms-timescale video rejected: %v", err)
|
|
}
|
|
gap := encode("gap.mp4", "select='not(eq(n\\,60))'")
|
|
if duration, err = ffprobeDurationSeconds(ctx, gap); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := validateSourceMediaClock(ctx, gap, 119, duration); err == nil {
|
|
t.Fatal("video with a dropped frame admitted as fixed-rate")
|
|
}
|
|
}
|
|
|
|
func TestSourceClockQuantizedConstantRateLengths(t *testing.T) {
|
|
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
|
t.Skip("ffmpeg unavailable")
|
|
}
|
|
for _, tc := range []struct {
|
|
name, rate, timescale string
|
|
frames int
|
|
}{
|
|
{"119", "30", "1000", 119},
|
|
{"121", "30", "1000", 121},
|
|
{"122", "30", "1000", 122},
|
|
{"123", "30", "1000", 123},
|
|
{"fractional", "30000/1001", "1000", 121},
|
|
{"half-tick-24fps", "24", "900", 121},
|
|
{"half-tick-24fps-1001", "24", "1001", 121},
|
|
{"fractional-900", "30000/1001", "900", 121},
|
|
{"native-time-base", "10", "10", 5},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "constant.mp4")
|
|
args := []string{"-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=s=64x48:r=" + tc.rate,
|
|
"-frames:v", fmt.Sprint(tc.frames), "-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-video_track_timescale", tc.timescale, path}
|
|
if out, err := exec.CommandContext(t.Context(), "ffmpeg", args...).CombinedOutput(); err != nil {
|
|
t.Fatalf("encode: %v %s", err, out)
|
|
}
|
|
duration, err := ffprobeDurationSeconds(t.Context(), path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := validateSourceMediaClock(t.Context(), path, tc.frames, duration); err != nil {
|
|
t.Fatalf("constant-rate video rejected: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSourceClockRejectsDroppedFrameAtNativeTimeBase(t *testing.T) {
|
|
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
|
t.Skip("ffmpeg unavailable")
|
|
}
|
|
path := filepath.Join(t.TempDir(), "gap.mp4")
|
|
args := []string{"-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=s=64x48:r=10:d=0.5",
|
|
"-vf", "select='not(eq(n\\,2))'", "-fps_mode", "passthrough", "-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-video_track_timescale", "10", path}
|
|
if out, err := exec.CommandContext(t.Context(), "ffmpeg", args...).CombinedOutput(); err != nil {
|
|
t.Fatalf("encode: %v %s", err, out)
|
|
}
|
|
duration, err := ffprobeDurationSeconds(t.Context(), path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := validateSourceMediaClock(t.Context(), path, 4, duration); err == nil {
|
|
t.Fatal("video with frame times 0,0.1,0.3,0.4 admitted as fixed-rate")
|
|
}
|
|
}
|
|
|
|
// Frame threading must preserve presentation order even with B-frame reordering.
|
|
func TestSourceClockAcceptsBFramesWithParallelDecoder(t *testing.T) {
|
|
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
|
t.Skip("ffmpeg unavailable")
|
|
}
|
|
path := filepath.Join(t.TempDir(), "bframes.mp4")
|
|
args := []string{"-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=s=128x96:r=30:d=4", "-c:v", "libx264", "-bf", "3", "-g", "60", path}
|
|
if out, err := exec.CommandContext(t.Context(), "ffmpeg", args...).CombinedOutput(); err != nil {
|
|
t.Fatalf("encode: %v %s", err, out)
|
|
}
|
|
duration, err := ffprobeDurationSeconds(t.Context(), path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := validateSourceMediaClock(t.Context(), path, 120, duration); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// Oversized decoder output must release its worker slot instead of waiting for
|
|
// the job timeout with a blocked stdout writer.
|
|
func TestSourceClockOversizedOutputReleasesProcess(t *testing.T) {
|
|
dir := t.TempDir()
|
|
script := `#!/bin/sh
|
|
case "$*" in
|
|
*stream=time_base*) printf '%s' '{"streams":[{"time_base":"1/30","r_frame_rate":"30/1"}]}'; exit 0 ;;
|
|
esac
|
|
while :; do printf '%01000d' 0; done
|
|
`
|
|
if err := os.WriteFile(filepath.Join(dir, "ffprobe"), []byte(script), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("PATH", dir)
|
|
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
|
|
defer cancel()
|
|
started := time.Now()
|
|
err := validateSourceMediaClock(ctx, filepath.Join(dir, "fake.mp4"), 30, 1)
|
|
if err == nil || !strings.Contains(err.Error(), "token too long") {
|
|
t.Fatalf("wanted bounded scanner failure, got %v", err)
|
|
}
|
|
if ctx.Err() != nil || time.Since(started) > 3*time.Second {
|
|
t.Fatal("malformed decoder retained worker until timeout")
|
|
}
|
|
}
|