droboticflow/internal/api/annotation_timeline_index_test.go

83 lines
3.2 KiB
Go

package api
import (
"context"
"droboticflow/internal/config"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"testing"
"time"
)
func TestPersistentTimelineSurvivesProcessCacheAndRebuildsCorruption(t *testing.T) {
db := store.NewMemoryStore()
task := domain.AnnotationTaskRecord{ID: "task", OrganizationID: "org", ProjectID: "project", RunID: "run"}
count := 0
v := map[string]any{"camera": "left", "frameCount": 2, "leftFrameCount": 2, "rightFrameCount": 2, "originTimestampUs": int64(100), "coverageEndTimestampUs": int64(300), "boundariesSeconds": []float64{0, 0.1, 0.2}, "rightBoundariesSeconds": []float64{0, 0.1, 0.2}, "boundaryTimestampUs": []int64{100, 200, 300}, "leftFrameIndices": []int{0, 1, 2}, "rightFrameIndices": []int{0, 1, 2}, "rangeStartSecond": 0.0, "rangeEndSecond": 0.2, "boundaryContract": "hardware-frame-v1", "ptsToMediaScale": 1.0, "sourceFingerprint": "source"}
build := func(context.Context) (map[string]any, error) { count++; return cloneTimeline(v), nil }
s := &server{store: db}
if _, e := s.persistentTimeline(context.Background(), task, "source", build); e != nil {
t.Fatal(e)
}
s = &server{store: db}
got, e := s.persistentTimeline(context.Background(), task, "source", build)
if e != nil || count != 1 {
t.Fatal("persistent cache missed", count, e)
}
if got["boundaryTimestampUs"].([]int64)[1] != 200 {
t.Fatal("index changed integer timestamps")
}
id := "annotation-timeline-v1:org:source"
bad, _ := db.GetAnnotationTimelineIndex(context.Background(), id)
bad.Body[0] ^= 0xff
db.PutAnnotationTimelineIndex(context.Background(), *bad)
if _, e = s.persistentTimeline(context.Background(), task, "source", build); e != nil || count != 2 {
t.Fatal("corrupt cache not rebuilt", e)
}
other := task
other.OrganizationID = "other"
if _, e = s.persistentTimeline(context.Background(), other, "source", build); e != nil || count != 3 {
t.Fatal("organization cache isolation", e)
}
}
func TestTaskCreationPrecomputesPersistentTimeline(t *testing.T) {
f, root := annotationProcessorFixture(t)
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, 1)
go func() {
done <- RunAnnotationProcessor(ctx, f.store, config.AppConfig{WorkspaceMountPath: root}, 1, false)
}()
s := &server{store: f.store, workspaceMountPath: root}
run, _ := f.store.GetRun(ctx, f.task.RunID)
key, e := s.annotationSourceFingerprint(ctx, f.task, *run)
if e != nil {
cancel()
t.Fatal(e)
}
deadline := time.Now().Add(5 * time.Second)
found := false
for time.Now().Before(deadline) {
index, e := f.store.GetAnnotationTimelineIndex(ctx, "annotation-timeline-v1:"+f.task.OrganizationID+":"+key)
if e == nil && index != nil {
found = true
break
}
time.Sleep(20 * time.Millisecond)
}
cancel()
<-done
if !found {
t.Fatal("new task not proactively indexed")
}
restarted := &server{store: f.store, workspaceMountPath: root}
left, e := restarted.annotationFrameTimelineForTask(context.Background(), f.task, "left")
if e != nil {
t.Fatal(e)
}
right, e := restarted.annotationFrameTimelineForTask(context.Background(), f.task, "right")
if e != nil || left["frameCount"] != right["frameCount"] {
t.Fatal("stereo index", e)
}
}