droboticflow/internal/api/annotation_original_source_test.go

239 lines
9.9 KiB
Go

package api
import (
"encoding/json"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"droboticflow/internal/domain"
)
func consumerSourceRef() *domain.ArtifactReference {
root := "incoming/capture-A"
keys := []string{root + "/meta_capture-A.json", root + "/capture-A_calibration_camera.yaml", root + "/capture-A_calibration_imu.yaml", root + "/part0001/capture-A_imu_part0001.csv"}
for _, side := range []string{"left", "right"} {
keys = append(keys, root+"/part0001/capture-A_camera_"+side+"_part0001.mp4", root+"/part0001/capture-A_camera_"+side+"_part0001_pts.csv")
}
return &domain.ArtifactReference{Provider: domain.StorageProviderBOS, Kind: domain.ArtifactRefKindDirectory, ConnectionID: "PRIVATE_CONNECTION", Bucket: "synthetic-original", Prefix: root, CaptureUnit: "capture-A/part0001", ObjectKeys: keys, VersionID: "frozen-input-version"}
}
func TestAnnotationOriginalSourceUsesFrozenInputSelection(t *testing.T) {
ref := consumerSourceRef()
run := &domain.RunRecord{ID: "run", OrganizationID: "org", Inputs: map[string]domain.ArtifactPayload{"raw": {Ref: ref}}}
capture := &domain.CaptureRecord{RunID: "run", OrganizationID: "org", Bucket: ref.Bucket, Prefix: ref.Prefix + "/part0001", SourcePrefix: ref.Prefix, CaptureID: ref.CaptureUnit, SourceKeys: []string{"later-current-directory-must-not-be-used"}}
source, err := annotationOriginalSource(run, capture)
if err != nil {
t.Fatal(err)
}
if len(source["objectKeys"].([]string)) != 8 {
t.Fatal("frozen source selection lost")
}
if _, ok := source["connectionId"]; ok {
t.Fatal("private connection leaked")
}
ref.ObjectKeys = append(ref.ObjectKeys, ref.Prefix+"/part0002/foreign.mp4")
if _, err = annotationOriginalSource(run, capture); err == nil {
t.Fatal("foreign part accepted")
}
ref.ObjectKeys = ref.ObjectKeys[:len(ref.ObjectKeys)-1]
capture.OrganizationID = "other"
if _, err = annotationOriginalSource(run, capture); err == nil {
t.Fatal("foreign organization accepted")
}
}
// Retain real synthetic source data for the independent Python consumer test.
func TestDeliveryV4ConsumerReplicaFixture(t *testing.T) {
parent := os.Getenv("FANYU_DELIVERY_CONSUMER_FIXTURE_DIR")
if parent == "" {
t.Skip("opt-in retained synthetic fixture")
}
if !filepath.IsAbs(parent) {
t.Fatal("absolute local output required")
}
for _, sourceIndex := range []bool{false, true} {
mode := "physical"
if sourceIndex {
mode = "source-index"
}
t.Run(mode, func(t *testing.T) {
f, temp := annotationProcessorFixture(t)
dir := filepath.Join(parent, mode)
root := filepath.Join(dir, "workspace")
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatal(err)
}
if err := copyAnnotationTree(f.ctx, temp, root); err != nil {
t.Fatal(err)
}
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))
}
}
}
ref := consumerSourceRef()
run.Inputs = map[string]domain.ArtifactPayload{"raw": {Ref: ref}}
if err := f.store.UpsertRun(f.ctx, *run); err != nil {
t.Fatal(err)
}
capture := domain.CaptureRecord{ID: "consumer-capture", RunID: run.ID, OrganizationID: run.OrganizationID, Bucket: ref.Bucket, Prefix: ref.Prefix + "/part0001", SourcePrefix: ref.Prefix, CaptureID: ref.CaptureUnit}
if err := f.store.UpsertCaptureRecord(f.ctx, capture); err != nil {
t.Fatal(err)
}
base := &domain.DictionarySelection{ID: "base", Kind: "slice", Version: 2, Code: "operate", Name: "物品操作", Tags: []string{"商品", "手部"}}
reason := &domain.DictionarySelection{ID: "discard", Kind: "discard_reason", Version: 1, Code: "occluded", Name: "遮挡", Tags: []string{"不可用"}}
action := &domain.DictionarySelection{ID: "pick", Kind: "action", Version: 3, Code: "pick", Name: "拿取", Tags: []string{"抓取"}}
input := combinedAnnotationSubmissionInput{SliceSegments: []domain.SliceSegment{{ID: "first", Name: base.Name, Dictionary: base, Start: 0, End: 1}, {ID: "discard", Start: 1, End: 3, DeliveryDisposition: "discard", DiscardReason: "遮挡", DiscardReasonDictionary: reason}, {ID: "last", Name: base.Name, Dictionary: base, Start: 3, End: 4}}, Segments: []domain.AnnotationSegment{{ID: "blank", Start: 0, End: .5}, {ID: "pick", Start: .5, End: 1, Description: "拿起杯子", Dictionary: action}, {ID: "invalid", Start: 3, End: 3.5, IsInvalid: true}, {ID: "free-text", Start: 3.5, End: 4, Description: "人工补充描述"}}}
s := &server{store: f.store, workspaceMountPath: root, annotationSourceIndex: sourceIndex}
result, err := s.materializeCombinedSliceSubmission(f.ctx, f.task, f.item, input, 1)
if err != nil {
t.Fatal(err)
}
if !sourceIndex {
before, err := os.ReadFile(filepath.Join(result.Artifact.Path, "revision-manifest.json"))
if err != nil {
t.Fatal(err)
}
var first map[string]any
if err = json.Unmarshal(before, &first); err != nil {
t.Fatal(err)
}
// Metadata-only reuse must keep the original source, even if discovery changes.
capture.Bucket = "later-bucket"
if err = f.store.UpsertCaptureRecord(f.ctx, capture); err != nil {
t.Fatal(err)
}
run, _ = f.store.GetRun(f.ctx, f.task.RunID)
result, err = s.materializeCombinedSliceSubmission(f.ctx, f.task, f.item, input, 2)
if err != nil {
t.Fatal(err)
}
if result.Payload["metadataOnly"] != true {
t.Fatal("metadata-only recovery not exercised")
}
after, err := os.ReadFile(filepath.Join(result.Artifact.Path, "revision-manifest.json"))
if err != nil {
t.Fatal(err)
}
var second map[string]any
if err = json.Unmarshal(after, &second); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(first["originalSource"], second["originalSource"]) {
t.Fatal("metadata revision followed mutable source")
}
}
original := filepath.Join(dir, "original")
for _, key := range ref.ObjectKeys {
relative := key[len(ref.Prefix)+1:]
dst := filepath.Join(original, filepath.FromSlash(relative))
if err = os.MkdirAll(filepath.Dir(dst), 0700); err != nil {
t.Fatal(err)
}
var body []byte
switch {
case filepath.Ext(dst) == ".yaml":
body = []byte("synthetic: true\n")
case filepath.Ext(dst) == ".json":
body = []byte("{\"synthetic\":true,\"partId\":\"part0001\"}\n")
default:
name := "imu.csv"
for _, side := range []string{"left", "right"} {
if strings.Contains(relative, "camera_"+side) {
name = side + ".mp4"
if strings.HasSuffix(relative, "_pts.csv") {
name = side + "_pts.csv"
}
}
}
body, err = os.ReadFile(filepath.Join(root, name))
if err != nil {
t.Fatal(err)
}
}
if err = os.WriteFile(dst, body, 0600); err != nil {
t.Fatal(err)
}
}
body, _ := json.MarshalIndent(map[string]any{"artifact": result.Artifact, "original": original, "workspace": root}, "", " ")
if err = os.WriteFile(filepath.Join(dir, "context.json"), body, 0600); err != nil {
t.Fatal(err)
}
})
}
}
// Legacy capture inputs can select an entire multi-part directory. They are
// insufficient evidence for v4, but must not block existing annotation work.
func TestLegacyMaterializationWithoutPartScopedOriginalSelection(t *testing.T) {
for _, sourceIndex := range []bool{false, true} {
name := "physical"
if sourceIndex {
name = "source-index"
}
t.Run(name, func(t *testing.T) {
f, root := annotationProcessorFixture(t)
run, err := f.store.GetRun(f.ctx, f.task.RunID)
if err != nil {
t.Fatal(err)
}
ref := consumerSourceRef()
ref.ObjectKeys = append(ref.ObjectKeys, ref.Prefix+"/part0002/other.mp4")
run.Inputs = map[string]domain.ArtifactPayload{"legacy-capture": {Ref: ref}}
if err = f.store.UpsertRun(f.ctx, *run); err != nil {
t.Fatal(err)
}
capture := domain.CaptureRecord{ID: "legacy-capture", RunID: run.ID, OrganizationID: run.OrganizationID, Bucket: ref.Bucket, Prefix: ref.Prefix + "/part0001", SourcePrefix: ref.Prefix, CaptureID: ref.CaptureUnit}
if err = f.store.UpsertCaptureRecord(f.ctx, capture); err != nil {
t.Fatal(err)
}
input := combinedAnnotationSubmissionInput{SliceSegments: []domain.SliceSegment{{ID: "whole", Name: "历史名称", Start: 0, End: 4}}, Segments: []domain.AnnotationSegment{{ID: "old", Start: 0, End: 4, Description: "历史描述"}}}
s := &server{store: f.store, workspaceMountPath: root, annotationSourceIndex: sourceIndex}
for revision := 1; revision <= 2; revision++ {
result, err := s.materializeCombinedSliceSubmission(f.ctx, f.task, f.item, input, revision)
if err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(filepath.Join(result.Artifact.Path, "revision-manifest.json"))
if err != nil {
t.Fatal(err)
}
var manifest map[string]any
if err = json.Unmarshal(data, &manifest); err != nil {
t.Fatal(err)
}
if manifest["originalSource"] != nil {
t.Fatal("unreliable source must not be exported as v4 evidence")
}
if len(listValue(manifest["items"])) != 1 {
t.Fatal("legacy materialization lost its slice")
}
if !sourceIndex && revision == 2 && result.Payload["metadataOnly"] != true {
t.Fatal("metadata-only reuse was not exercised")
}
}
})
}
}
func TestOptionalOriginalSourceStillRejectsIdentityMismatch(t *testing.T) {
ref := consumerSourceRef()
run := &domain.RunRecord{ID: "run", OrganizationID: "org", Inputs: map[string]domain.ArtifactPayload{"raw": {Ref: ref}}}
capture := &domain.CaptureRecord{RunID: "other-run", OrganizationID: "org"}
if _, err := annotationRevisionOriginalSource(run, capture); err == nil {
t.Fatal("run mismatch accepted")
}
capture.RunID = run.ID
capture.OrganizationID = "other-org"
if _, err := annotationRevisionOriginalSource(run, capture); err == nil {
t.Fatal("organization mismatch accepted")
}
}