183 lines
5.9 KiB
Go
183 lines
5.9 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAnnotationBOSVersionIgnoresSyntheticMtimeButDetectsSameSizeReplacement(t *testing.T) {
|
|
etag := "version-one"
|
|
endpoint := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodHead {
|
|
t.Error("non HEAD request")
|
|
}
|
|
w.Header().Set("ETag", etag)
|
|
w.Header().Set("Content-Length", "4")
|
|
w.Header().Set("Last-Modified", "Thu, 17 Sep 2026 00:00:00 GMT")
|
|
w.WriteHeader(200)
|
|
}))
|
|
defer endpoint.Close()
|
|
db := store.NewMemoryStore()
|
|
ctx := context.Background()
|
|
db.UpsertStorageConnection(ctx, domain.StorageConnectionRecord{ID: "cache-bos", OrganizationID: "cache-org", Provider: domain.StorageProviderBOS, Endpoint: endpoint.URL, AccessKeyID: "test", SecretAccessKey: "test"})
|
|
t.Setenv("ANNOTATION_WORKSPACE_CONNECTION_ID", "cache-bos")
|
|
t.Setenv("ANNOTATION_WORKSPACE_BUCKET", "bucket")
|
|
root := t.TempDir()
|
|
path := filepath.Join(root, "input.csv")
|
|
os.WriteFile(path, []byte("data"), 0600)
|
|
s := &server{store: db, workspaceMountPath: root}
|
|
one, e := s.annotationFileVersion(ctx, "cache-org", path)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
os.Chtimes(path, time.Now().Add(time.Hour), time.Now().Add(time.Hour))
|
|
two, e := s.annotationFileVersion(ctx, "cache-org", path)
|
|
if e != nil || one != two {
|
|
t.Fatal("unstable mount mtime affected BOS version", e)
|
|
}
|
|
etag = "version-two"
|
|
three, e := s.annotationFileVersion(ctx, "cache-org", path)
|
|
if e != nil || one == three {
|
|
t.Fatal("same size source overwrite not detected", e)
|
|
}
|
|
if _, e = s.annotationFileVersion(ctx, "foreign", path); e == nil {
|
|
t.Fatal("storage scope bypass")
|
|
}
|
|
}
|
|
|
|
func TestAnnotationTimelineCacheCoalescesAndOwnsValues(t *testing.T) {
|
|
var cache annotationTimelineCache
|
|
var calls atomic.Int32
|
|
gate := make(chan struct{})
|
|
var wg sync.WaitGroup
|
|
build := func(ctx context.Context) (map[string]any, error) {
|
|
calls.Add(1)
|
|
<-gate
|
|
return map[string]any{"boundariesSeconds": []float64{0, 1}}, nil
|
|
}
|
|
for i := 0; i < 8; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
value, err := cache.get(context.Background(), "source", build)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
value["boundariesSeconds"].([]float64)[0] = 7
|
|
}()
|
|
}
|
|
close(gate)
|
|
wg.Wait()
|
|
value, _ := cache.get(context.Background(), "source", build)
|
|
if calls.Load() != 1 || value["boundariesSeconds"].([]float64)[0] != 0 {
|
|
t.Fatal("duplicate build or shared mutable timeline")
|
|
}
|
|
cache.mu.Lock()
|
|
e := cache.entries["source"]
|
|
e.expires = time.Now().Add(-time.Second)
|
|
cache.entries["source"] = e
|
|
cache.mu.Unlock()
|
|
cache.get(context.Background(), "source", build)
|
|
if calls.Load() != 2 {
|
|
t.Fatal("TTL did not invalidate")
|
|
}
|
|
cache.get(context.Background(), "changed-source", build)
|
|
if calls.Load() != 3 {
|
|
t.Fatal("source identity did not invalidate")
|
|
}
|
|
}
|
|
|
|
func TestAnnotationTimelineCacheCancellationAndFailureNotCached(t *testing.T) {
|
|
var cache annotationTimelineCache
|
|
var calls int
|
|
build := func(ctx context.Context) (map[string]any, error) { calls++; return nil, context.Canceled }
|
|
for i := 0; i < 2; i++ {
|
|
if _, err := cache.get(context.Background(), "x", build); !errors.Is(err, context.Canceled) {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if calls != 2 || len(cache.entries) != 0 {
|
|
t.Fatal("cached failed timeline")
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
cache.flights["waiting"] = &annotationTimelineFlight{done: make(chan struct{})}
|
|
if _, err := cache.get(ctx, "waiting", build); !errors.Is(err, context.Canceled) {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestStreamingIMUSummaryAndValidation(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "imu.csv")
|
|
valid := "timestamp_us,x,y,z,type\n100,1,2,3,accel\n100,4,5,6,gyro\n200,1,2,3,accel\n200,4,5,6,gyro\n"
|
|
for _, tc := range []struct {
|
|
name, body string
|
|
valid bool
|
|
}{
|
|
{"valid", valid, true}, {"tsv", string(bytes.ReplaceAll([]byte(valid), []byte(","), []byte("\t"))), true},
|
|
{"missing-final-pair", valid[:len(valid)-len("200,4,5,6,gyro\n")], false},
|
|
{"missing-first-pair", string(bytes.ReplaceAll([]byte(valid), []byte("100,4,5,6,gyro\n"), nil)), false},
|
|
{"out-of-order", valid + "150,1,2,3,gyro\n", false}, {"empty", "timestamp_us,x,y,z,type\n", false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
os.WriteFile(p, []byte(tc.body), 0600)
|
|
summary, e := scanHardwareIMU(context.Background(), p, nil)
|
|
if (e == nil) != tc.valid {
|
|
t.Fatal(e)
|
|
}
|
|
if tc.valid {
|
|
rows, e := readHardwareIMU(p)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
end, _ := summary.coverageEnd()
|
|
oldEnd, _ := imuCoverageEnd(rows)
|
|
if summary.first != 100 || end != 300 || end != oldEnd || summary.count != len(rows) {
|
|
t.Fatal(summary)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
os.WriteFile(p, []byte(valid), 0600)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
if _, err := scanHardwareIMU(ctx, p, nil); !errors.Is(err, context.Canceled) {
|
|
t.Fatal("cancel ignored", err)
|
|
}
|
|
}
|
|
|
|
func TestAnnotationStatusPollIsSmallAuthorizedAndReadOnly(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
before, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
r := f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"?view=status", f.annotator, nil)
|
|
if r.Code != 200 {
|
|
t.Fatal(r.Code, r.Body.String())
|
|
}
|
|
if r.Body.Len() > 1024 || bytes.Contains(r.Body.Bytes(), []byte("annotationWorkspace")) || bytes.Contains(r.Body.Bytes(), []byte("sliceRevisions")) {
|
|
t.Fatal("poll returned heavy data")
|
|
}
|
|
after, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if !reflect.DeepEqual(before, after) {
|
|
t.Fatal("poll mutated task")
|
|
}
|
|
other := f.annotator
|
|
other.ID = "foreign"
|
|
other.OrganizationID = "foreign"
|
|
if r = f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"?view=status", other, nil); r.Code != 403 && r.Code != 404 {
|
|
t.Fatal("foreign task status leaked", r.Code)
|
|
}
|
|
}
|