173 lines
6.0 KiB
Go
173 lines
6.0 KiB
Go
package sourceidentity
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
func fixture(t *testing.T) (*domain.RunRecord, *domain.CaptureRecord, map[string]domain.ObjectStorageObjectSummary) {
|
|
t.Helper()
|
|
root := "incoming/capture-A"
|
|
part := "part0001"
|
|
keys := []string{root + "/meta_capture-A.json", root + "/capture-A_calibration_camera.yaml", root + "/capture-A_calibration_imu.yaml", root + "/" + part + "/capture-A_imu_" + part + ".csv"}
|
|
for _, side := range []string{"left", "right"} {
|
|
keys = append(keys, root+"/"+part+"/capture-A_camera_"+side+"_"+part+".mp4", root+"/"+part+"/capture-A_camera_"+side+"_"+part+"_pts.csv")
|
|
}
|
|
keys = append(keys, root+"/"+part+"/capture-A_audio_"+part+".wav", root+"/"+part+"/capture-A_audio_"+part+"_pts.csv")
|
|
objects := []Object{}
|
|
heads := map[string]domain.ObjectStorageObjectSummary{}
|
|
for _, key := range keys {
|
|
objects = append(objects, Object{key, `"original-etag"`, 100})
|
|
heads[key] = domain.ObjectStorageObjectSummary{Bucket: "b", Key: key, Kind: "file", ETag: `"original-etag"`, SizeBytes: 100}
|
|
}
|
|
ref := &domain.ArtifactReference{Provider: domain.StorageProviderBOS, Kind: domain.ArtifactRefKindDirectory, ConnectionID: "c", Bucket: "b", Prefix: root, CaptureUnit: "capture-A/" + part, ObjectKeys: keys, VersionID: Fingerprint(objects)}
|
|
run := &domain.RunRecord{ID: "original", OrganizationID: "org", ProjectID: "p", WorkflowID: "w", Inputs: map[string]domain.ArtifactPayload{"source": {Ref: ref}}}
|
|
// Current capture state is deliberately different from immutable run evidence.
|
|
capture := &domain.CaptureRecord{OrganizationID: "org", ProjectID: "p", WorkflowID: "w", ConnectionID: "c", Bucket: "b", SourcePrefix: root, Prefix: root + "/" + part, CaptureID: ref.CaptureUnit, Fingerprint: "later-discovery", SourceKeys: []string{"later-key"}}
|
|
return run, capture, heads
|
|
}
|
|
|
|
func TestOriginalIdentityRecheck(t *testing.T) {
|
|
for _, scenario := range []string{"correct", "same-size-etag-tamper", "size-change", "missing-key", "empty-etag", "head-error", "optional-audio-tamper"} {
|
|
t.Run(scenario, func(t *testing.T) {
|
|
run, capture, heads := fixture(t)
|
|
selection, err := FromRun(run, capture)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key := selection.Ref.ObjectKeys[0]
|
|
if scenario == "optional-audio-tamper" {
|
|
for _, candidate := range selection.Ref.ObjectKeys {
|
|
if strings.HasSuffix(candidate, ".wav") {
|
|
key = candidate
|
|
}
|
|
}
|
|
}
|
|
info := heads[key]
|
|
switch scenario {
|
|
case "same-size-etag-tamper", "optional-audio-tamper":
|
|
info.ETag = "changed"
|
|
heads[key] = info
|
|
case "size-change":
|
|
info.SizeBytes++
|
|
heads[key] = info
|
|
case "missing-key":
|
|
delete(heads, key)
|
|
case "empty-etag":
|
|
info.ETag = ""
|
|
heads[key] = info
|
|
}
|
|
calls := 0
|
|
err = selection.Recheck(context.Background(), func(ctx context.Context, bucket, key string) (domain.ObjectStorageObjectSummary, error) {
|
|
calls++
|
|
if _, ok := ctx.Deadline(); !ok {
|
|
t.Fatal("unbounded HEAD")
|
|
}
|
|
if scenario == "head-error" {
|
|
return domain.ObjectStorageObjectSummary{}, fmt.Errorf("head unavailable")
|
|
}
|
|
return heads[key], nil
|
|
})
|
|
if (err == nil) != (scenario == "correct") {
|
|
t.Fatalf("err=%v", err)
|
|
}
|
|
if calls > 10 || scenario == "correct" && calls != 10 {
|
|
t.Fatalf("HEAD count=%d", calls)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOriginalSelectionScopeAndEvidence(t *testing.T) {
|
|
for _, scenario := range []string{"tenant", "project", "workflow", "connection", "bucket", "prefix", "capture-unit", "extra-selection", "foreign-part", "duplicate", "missing-version", "fake-version", "missing-inputs", "ambiguous-inputs"} {
|
|
t.Run(scenario, func(t *testing.T) {
|
|
run, capture, _ := fixture(t)
|
|
ref := run.Inputs["source"].Ref
|
|
switch scenario {
|
|
case "tenant":
|
|
run.OrganizationID = "other"
|
|
case "project":
|
|
run.ProjectID = "other"
|
|
case "workflow":
|
|
run.WorkflowID = "other"
|
|
case "connection":
|
|
ref.ConnectionID = "other"
|
|
case "bucket":
|
|
ref.Bucket = "other"
|
|
case "prefix":
|
|
ref.Prefix = "other"
|
|
case "capture-unit":
|
|
ref.CaptureUnit = "other"
|
|
case "extra-selection":
|
|
ref.ObjectKeys[0] = ref.Prefix + "/extra.yaml"
|
|
case "foreign-part":
|
|
ref.ObjectKeys[0] = ref.Prefix + "/part0002/foreign.mp4"
|
|
case "duplicate":
|
|
ref.ObjectKeys[0] = ref.ObjectKeys[1]
|
|
case "missing-version":
|
|
ref.VersionID = ""
|
|
case "fake-version":
|
|
ref.VersionID = "selectionFP"
|
|
case "missing-inputs":
|
|
run.Inputs = nil
|
|
case "ambiguous-inputs":
|
|
other := *ref
|
|
other.VersionID = strings.Repeat("a", 64)
|
|
run.Inputs["other"] = domain.ArtifactPayload{Ref: &other}
|
|
}
|
|
if _, err := FromRun(run, capture); err == nil {
|
|
t.Fatal("invalid selection accepted")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOriginalDeliveryFilesMatchFrozenSelection(t *testing.T) {
|
|
run, capture, _ := fixture(t)
|
|
selection, err := FromRun(run, capture)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = selection.MatchFiles("BOS", "b", capture.SourcePrefix, capture.Prefix, selection.Roles); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, scenario := range []string{"extra", "changed", "missing", "wrong-bucket", "wrong-part"} {
|
|
t.Run(scenario, func(t *testing.T) {
|
|
files := map[string]string{}
|
|
for k, v := range selection.Roles {
|
|
files[k] = v
|
|
}
|
|
bucket, part := "b", capture.Prefix
|
|
switch scenario {
|
|
case "extra":
|
|
files["audio"] = "extra.wav"
|
|
case "changed":
|
|
files["leftVideo"] = "part0001/other.mp4"
|
|
case "missing":
|
|
delete(files, "imu")
|
|
case "wrong-bucket":
|
|
bucket = "other"
|
|
case "wrong-part":
|
|
part += "2"
|
|
}
|
|
if selection.MatchFiles("BOS", bucket, capture.SourcePrefix, part, files) == nil {
|
|
t.Fatal("mismatched delivery accepted")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCaptureFingerprintHistoricalBytes(t *testing.T) {
|
|
// Independently pinned old key|trim(etag)|size serialization, sorted as strings.
|
|
sum := sha256.Sum256([]byte("a|abc|12\nz|def|34"))
|
|
if got := Fingerprint([]Object{{"z", `"def"`, 34}, {"a", `"abc"`, 12}}); got != hex.EncodeToString(sum[:]) {
|
|
t.Fatalf("old identity changed: %s", got)
|
|
}
|
|
}
|