droboticflow/internal/api/annotation_delegated_timeline_test.go

116 lines
4.6 KiB
Go

package api
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
"droboticflow/internal/config"
"droboticflow/internal/domain"
)
func TestDelegatedReadOnlyFrameTimelineIncludesIMUSource(t *testing.T) {
t.Setenv("ANNOTATION_PROCESSOR_MODE", "external")
t.Setenv("ANNOTATION_PROCESSOR_ROLE", "validation")
f, root := annotationProcessorFixture(t)
workspace := filepath.Dir(root)
prefix := filepath.Base(root)
endpoint := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodHead && r.Method != http.MethodGet {
t.Errorf("unexpected object-store write: %s", r.Method)
w.WriteHeader(403)
return
}
key := strings.TrimPrefix(r.URL.Path, "/bucket/")
if !annotationPrefixAllows(key, []string{prefix}) {
w.WriteHeader(403)
return
}
// BOS accepts its unquoted ETag in If-Match; net/http's file server
// requires a quoted HTTP entity tag. Check BOS semantics explicitly.
if r.Method == http.MethodGet {
if strings.Trim(r.Header.Get("If-Match"), "\"") != "fixture-version" {
w.WriteHeader(http.StatusPreconditionFailed)
return
}
r.Header.Del("If-Match")
}
w.Header().Set("ETag", `"fixture-version"`)
http.ServeFile(w, r, filepath.Join(workspace, key))
}))
defer endpoint.Close()
if err := f.store.UpsertStorageConnection(f.ctx, domain.StorageConnectionRecord{ID: "shared", OrganizationID: "storage-owner", Provider: domain.StorageProviderBOS, Endpoint: endpoint.URL, AccessKeyID: "test", SecretAccessKey: "test"}); err != nil {
t.Fatal(err)
}
raw, err := json.Marshal(map[string]annotationWorkspaceMapping{f.task.OrganizationID: {ConnectionID: "shared", ReadConnectionOrganizationID: "storage-owner", Bucket: "bucket", ReadPrefixes: []string{prefix}}})
if err != nil {
t.Fatal(err)
}
t.Setenv("ANNOTATION_WORKSPACE_MAPPINGS", string(raw))
f.handler = NewHandlerWithConfig(f.store, nil, config.AppConfig{WorkspaceMountPath: workspace})
pending := f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/frame-timeline", f.annotator, nil)
if pending.Code != http.StatusUnprocessableEntity || !strings.Contains(pending.Body.String(), "source_validation_pending") {
t.Fatalf("unvalidated delegated source was admitted: %d %s", pending.Code, pending.Body.String())
}
// Reading a delegated timeline remains supported. It cannot stand in for
// full proof when this mapping has no authorized pinned-source output.
reader := &server{store: f.store, workspaceMountPath: workspace}
for _, camera := range []string{"left", "right"} {
if _, err := reader.annotationFrameTimelineForTask(f.ctx, f.task, camera); err != nil {
t.Fatalf("%s delegated timeline read: %v", camera, err)
}
}
before, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
job, err := f.store.ClaimAnnotationIndex(f.ctx, "legacy-readonly", nowISO(), time.Now().Add(time.Minute).UTC().Format(time.RFC3339Nano))
if err != nil || job == nil {
t.Fatal("missing validation job", err)
}
job.Status, job.Attempt = "queued", 4
if ok, err := f.store.FinishAnnotationIndex(f.ctx, *job); err != nil || !ok {
t.Fatal("failed to prepare final validation attempt", err)
}
ctx, cancel := context.WithCancel(f.ctx)
done := make(chan error, 1)
go func() {
done <- RunAnnotationProcessor(ctx, f.store, config.AppConfig{WorkspaceMountPath: workspace}, 1, false)
}()
defer func() { cancel(); <-done }()
deadline := time.Now().Add(10 * time.Second)
for {
job, err := f.store.GetAnnotationIndexJob(f.ctx, f.task.ID)
if err != nil {
t.Fatal(err)
}
if job != nil && job.Status == "failed" {
if job.SourceFingerprint != "" || !strings.Contains(job.Error, "read-only") {
t.Fatalf("incomplete read-only proof published: %+v", job)
}
break
}
if time.Now().After(deadline) {
t.Fatalf("delegated read-only validation did not fail closed: %+v", job)
}
time.Sleep(10 * time.Millisecond)
}
for _, camera := range []string{"left", "right"} {
r := f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/frame-timeline?camera="+camera+"&encoding=compact", f.annotator, nil)
if r.Code != http.StatusUnprocessableEntity {
t.Fatalf("%s admitted an incomplete source: %d %s", camera, r.Code, r.Body.String())
}
}
after, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
if !reflect.DeepEqual(before, after) {
t.Fatal("read-only validation changed saved work or ownership")
}
s := &server{store: f.store, workspaceMountPath: workspace}
if _, _, _, err := s.annotationWorkspaceObject(f.ctx, f.task.OrganizationID, filepath.Join(root, "imu.csv")); err == nil {
t.Fatal("source write allowed after read")
}
}