forked from longtao.wu/droboticflow
177 lines
7.2 KiB
Go
177 lines
7.2 KiB
Go
package deliveryindex
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func classificationFixture(allDiscard bool) Source {
|
|
src := fixture("part0001", 7)
|
|
var p map[string]any
|
|
json.Unmarshal(src.Part, &p)
|
|
acceptance := p["acceptance"]
|
|
delete(p, "acceptance")
|
|
p["schemaVersion"] = ClassificationPartSchema
|
|
if allDiscard {
|
|
p["slices"] = []any{}
|
|
p["discardedSlices"] = []any{map[string]any{"sliceId": "all", "taskSplit": map[string]any{"startSecond": 0, "endSecond": 3, "semantics": "half-open"}, "classification": map[string]any{"deliveryDisposition": "discard", "discardEvidence": map[string]any{"reason": "遮挡"}}, "segments": []any{}}}
|
|
p["deliveryClassification"] = map[string]any{"keptSliceCount": 0, "discardedSliceCount": 1, "keptDurationSeconds": 0, "discardedDurationSeconds": 3}
|
|
}
|
|
enrichConsumerFixture(p)
|
|
src.Part = encode(p)
|
|
src.History = encode(map[string]any{"schemaVersion": "droboticflow.delivery.review-history/v2", "captureId": "capture-A", "partId": "part0001", "revision": 7, "partSha256": Hash(src.Part), "acceptance": acceptance, "qualityReviews": []any{map[string]string{"comment": "PRIVATE_REWORK"}}, "trace": []any{}})
|
|
return src
|
|
}
|
|
func TestClassificationBatchNeverIncludesPrivateTrace(t *testing.T) {
|
|
for _, all := range []bool{false, true} {
|
|
src := classificationFixture(all)
|
|
out := build(t, src)
|
|
if len(out.Files) != 3 {
|
|
t.Fatalf("customer files %v", out.Files)
|
|
}
|
|
for name, body := range out.Files {
|
|
if strings.Contains(name, "history") || bytes.Contains(body, []byte("PRIVATE_REWORK")) || bytes.Contains(body, []byte(`"acceptance"`)) {
|
|
t.Fatalf("private trace in %s", name)
|
|
}
|
|
}
|
|
var h map[string]any
|
|
json.Unmarshal(src.History, &h)
|
|
h["qualityReviews"] = []any{map[string]string{"comment": "changed"}}
|
|
src.History = encode(h)
|
|
again := build(t, src)
|
|
if out.ID != again.ID || !bytes.Equal(out.Index, again.Index) {
|
|
t.Fatal("audit-only edit changed customer batch")
|
|
}
|
|
src.History = bytes.ReplaceAll(src.History, []byte(Hash(src.Part)), []byte(strings.Repeat("0", 64)))
|
|
if _, err := Build("project-A", "项目A", "delivery/项目A__project-A", []Source{src}); err == nil {
|
|
t.Fatal("wrong acceptance digest accepted")
|
|
}
|
|
}
|
|
}
|
|
func TestClassificationBatchRejectsMixedVersions(t *testing.T) {
|
|
if _, err := Build("project-A", "项目A", "delivery/项目A__project-A", []Source{classificationFixture(false), fixture("part0002", 1)}); err == nil {
|
|
t.Fatal("mixed versions accepted")
|
|
}
|
|
}
|
|
|
|
func TestClassificationBatchRejectsIncompleteConsumerContract(t *testing.T) {
|
|
for _, failure := range []string{"contract", "tags", "tag-source", "part-time", "pts", "original-file", "original-source"} {
|
|
t.Run(failure, func(t *testing.T) {
|
|
src := classificationFixture(false)
|
|
var doc, audit map[string]any
|
|
json.Unmarshal(src.Part, &doc)
|
|
slice := doc["slices"].([]any)[0].(map[string]any)
|
|
action := slice["segments"].([]any)[0].(map[string]any)
|
|
switch failure {
|
|
case "contract":
|
|
delete(doc, "consumerContract")
|
|
case "tags":
|
|
delete(action, "tags")
|
|
case "tag-source":
|
|
action["tagSource"] = "action"
|
|
case "part-time":
|
|
action["partStartSecond"] = -1
|
|
case "pts":
|
|
delete(action, "startTimestampUs")
|
|
case "original-source":
|
|
delete(doc, "originalSource")
|
|
case "original-file":
|
|
delete(doc["originalSource"].(map[string]any)["files"].(map[string]any), "leftPts")
|
|
}
|
|
src.Part = encode(doc)
|
|
json.Unmarshal(src.History, &audit)
|
|
// Keep acceptance hash valid: rejection must be the consumer guard.
|
|
audit["partSha256"] = Hash(src.Part)
|
|
src.History = encode(audit)
|
|
if _, err := Build("project-A", "项目A", "delivery/项目A__project-A", []Source{src}); err == nil || !strings.Contains(err.Error(), "consumer contract") {
|
|
t.Fatalf("incomplete %s accepted: %v", failure, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestClassificationBatchAcceptsExplicitSamplingExemptionWithoutInventingHumanDate(t *testing.T) {
|
|
src := classificationFixture(false)
|
|
var audit map[string]any
|
|
json.Unmarshal(src.History, &audit)
|
|
final := audit["acceptance"].(map[string]any)["finalAcceptance"].(map[string]any)
|
|
delete(final, "acceptedAt")
|
|
final["sampled"] = false
|
|
final["samplingDecision"] = "not_selected"
|
|
src.History = encode(audit)
|
|
out := build(t, src)
|
|
if bytes.Contains(out.Index, []byte("acceptedAt")) {
|
|
t.Fatal("invented human acceptance date")
|
|
}
|
|
final["samplingDecision"] = "selected"
|
|
src.History = encode(audit)
|
|
if _, err := Build("project-A", "项目A", "delivery/项目A__project-A", []Source{src}); err == nil {
|
|
t.Fatal("unverified human acceptance accepted")
|
|
}
|
|
}
|
|
|
|
func enrichConsumerFixture(p map[string]any) {
|
|
part := p["partId"].(string)
|
|
p["consumerContract"] = "droboticflow.classification-consumer/v1"
|
|
p["timeBases"] = map[string]string{"taskSplitSeconds": "original_left_video", "segmentSeconds": "slice_left_video", "segmentPartSeconds": "original_left_video", "timestampUs": "original_hardware_pts", "interval": "half-open", "frameRanges": "zero-based-half-open"}
|
|
p["pathBases"] = map[string]string{"sources": "original_capture", "sourceMetadata": "original_capture", "calibration": "original_capture"}
|
|
files := map[string]any{}
|
|
sources := map[string]any{}
|
|
for _, role := range []string{"leftVideo", "rightVideo", "leftPts", "rightPts", "imu", "cameraCalibration", "imuCalibration", "metadata"} {
|
|
name := part + "/" + role
|
|
if role == "cameraCalibration" || role == "imuCalibration" {
|
|
name = role + ".yaml"
|
|
}
|
|
if role == "metadata" {
|
|
name = "meta_capture-A.json"
|
|
}
|
|
files[role] = map[string]any{"path": name}
|
|
if role == "leftVideo" || role == "rightVideo" || role == "leftPts" || role == "rightPts" || role == "imu" {
|
|
sources[role] = files[role]
|
|
}
|
|
}
|
|
p["sources"] = sources
|
|
p["sourceMetadata"] = files["metadata"]
|
|
p["originalSource"] = map[string]any{"provider": "BOS", "bucket": "test-bucket", "prefix": "incoming/capture-A", "partPrefix": "incoming/capture-A/" + part, "files": files}
|
|
for _, kind := range []string{"slices", "discardedSlices"} {
|
|
for _, raw := range p[kind].([]any) {
|
|
item := raw.(map[string]any)
|
|
split := item["taskSplit"].(map[string]any)
|
|
number := func(v any) float64 {
|
|
switch n := v.(type) {
|
|
case float64:
|
|
return n
|
|
case int:
|
|
return float64(n)
|
|
default:
|
|
panic("invalid fixture time")
|
|
}
|
|
}
|
|
start, end := number(split["startSecond"]), number(split["endSecond"])
|
|
split["startTimestampUs"] = int64(1e6 + start*1e6)
|
|
split["endTimestampUs"] = int64(1e6 + end*1e6)
|
|
if kind == "discardedSlices" {
|
|
continue
|
|
}
|
|
class := item["classification"].(map[string]any)
|
|
class["dictionary"] = map[string]any{"id": "base", "kind": "slice", "version": 1, "name": "操作", "tags": []string{"操作"}}
|
|
class["tags"] = []string{"操作"}
|
|
actions := item["segments"].([]any)
|
|
for i, raw := range actions {
|
|
a := raw.(map[string]any)
|
|
low, high := float64(i)*(end-start)/float64(len(actions)), float64(i+1)*(end-start)/float64(len(actions))
|
|
a["startSecond"] = low
|
|
a["endSecond"] = high
|
|
a["partStartSecond"] = start + low
|
|
a["partEndSecond"] = start + high
|
|
a["startTimestampUs"] = int64(1e6 + (start+low)*1e6)
|
|
a["endTimestampUs"] = int64(1e6 + (start+high)*1e6)
|
|
a["tags"] = []string{"操作"}
|
|
a["tagSource"] = "slice"
|
|
}
|
|
}
|
|
}
|
|
}
|