droboticflow-new/internal/api/annotation_source_load_fixture_test.go

109 lines
3.0 KiB
Go

package api
import (
"bufio"
"bytes"
"compress/gzip"
"crypto/sha256"
"droboticflow/internal/domain"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
)
// Small images keep fixture generation cheap; full production-scale frame and
// sensor counts exercise metadata/cache/write costs. Decoder capacity is not
// inferred from this synthetic fixture.
func sourceLoadProductionShape(t *testing.T, f *combinedDraftFixture, root string) {
t.Helper()
for _, side := range []string{"left", "right"} {
command := exec.Command("ffmpeg", "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=s=64x48:r=30:d=900", "-c:v", "libx264", "-preset", "ultrafast", "-threads", "1", "-pix_fmt", "yuv420p", filepath.Join(root, side+".mp4"))
if b, e := command.CombinedOutput(); e != nil {
t.Fatalf("large synthetic video: %v %s", e, b)
}
file, e := os.Create(filepath.Join(root, side+"_pts.csv"))
if e != nil {
t.Fatal(e)
}
writer := bufio.NewWriter(file)
fmt.Fprintln(writer, "timestamp_us")
for n := 0; n < 27000; n++ {
fmt.Fprintln(writer, 1000000+int64(n)*1000000/30)
}
if e = writer.Flush(); e != nil {
t.Fatal(e)
}
if e = file.Close(); e != nil {
t.Fatal(e)
}
}
file, e := os.Create(filepath.Join(root, "imu.csv"))
if e != nil {
t.Fatal(e)
}
writer := bufio.NewWriterSize(file, 1<<20)
fmt.Fprintln(writer, "timestamp_us,x,y,z,type")
for n := 0; n < 900000; n++ {
stamp := 1000000 + n*1000
fmt.Fprintf(writer, "%d,1,2,3,accel\n%d,4,5,6,gyro\n", stamp, stamp)
}
if e = writer.Flush(); e != nil {
t.Fatal(e)
}
if e = file.Close(); e != nil {
t.Fatal(e)
}
f.task.ClipEnd = 900
f.task.SourceDurationSeconds = 900
f.task.CoverageEndSecond = 900
}
func sourceLoadStoredBody(t *testing.T, value any) ([]byte, string) {
t.Helper()
b, e := json.Marshal(value)
if e != nil {
t.Fatal(e)
}
var buffer bytes.Buffer
z := gzip.NewWriter(&buffer)
if _, e = z.Write(b); e != nil {
t.Fatal(e)
}
if e = z.Close(); e != nil {
t.Fatal(e)
}
sum := sha256.Sum256(buffer.Bytes())
return buffer.Bytes(), hex.EncodeToString(sum[:])
}
func sourceLoadCertificateForRun(t *testing.T, root string, base *annotationSourceCertificate, task domain.AnnotationTaskRecord, fingerprint string) *annotationSourceCertificate {
t.Helper()
b, e := json.Marshal(base)
if e != nil {
t.Fatal(e)
}
var cert annotationSourceCertificate
if e = json.Unmarshal(b, &cert); e != nil {
t.Fatal(e)
}
cert.RunID, cert.TaskID, cert.Fingerprint, cert.Timeline.Fingerprint = task.RunID, task.ID, fingerprint, fingerprint
// Distinct identities and cache entries model 100 parts. Hard links avoid
// copying 100 synthetic sensor files and never alias customer source files.
for kind, source := range cert.Sources {
path := filepath.Join(root, "system", "runs", task.RunID, "annotation-sources", fingerprint, filepath.Base(source.Path))
if e = os.MkdirAll(filepath.Dir(path), 0755); e != nil {
t.Fatal(e)
}
if e = os.Link(source.Path, path); e != nil {
t.Fatal(e)
}
source.Path = path
cert.Sources[kind] = source
}
return &cert
}