128 lines
5.9 KiB
Go

package deliveryindex
import (
"bytes"
"encoding/json"
"strings"
"testing"
)
func fixture(partID string, revision int) Source {
p := map[string]any{"schemaVersion": PartSchema, "capture": map[string]any{"captureId": "capture-A", "prefix": "incoming/capture-A"}, "partId": partID, "revision": revision, "sourceMetadata": map[string]string{"path": "meta_capture-A.json"}, "acceptance": map[string]any{"finalAcceptance": map[string]any{"confirmed": true, "acceptedAt": "2026-10-20T01:00:00Z"}}, "slices": []any{map[string]any{"sliceId": "s1", "taskSplit": map[string]any{"startSecond": 0, "endSecond": 2, "semantics": "half-open"}, "classification": map[string]any{"deliveryDisposition": "keep", "descriptionStatus": "invalid_and_blank"}, "segments": []any{map[string]any{"description": "", "contentKind": "gap_placeholder", "isInvalid": false}, map[string]any{"description": "无效", "contentKind": "invalid_marked", "isInvalid": true}}}}, "discardedSlices": []any{map[string]any{"sliceId": "s2", "taskSplit": map[string]any{"startSecond": 2, "endSecond": 3, "semantics": "half-open"}, "classification": map[string]any{"deliveryDisposition": "discard", "discardEvidence": map[string]string{"reason": "不交付"}}, "segments": []any{}}}}
p["deliveryClassification"] = map[string]any{"keptSliceCount": 1, "discardedSliceCount": 1, "keptDurationSeconds": 2, "discardedDurationSeconds": 1}
h := map[string]any{"schemaVersion": "droboticflow.delivery.review-history/v1", "captureId": "capture-A", "partId": partID, "revision": revision, "qualityReviews": []any{map[string]any{"revision": revision, "decision": "approved"}}}
return Source{Part: encode(p), History: encode(h), ExpectedParts: []string{"part0001", "part0002"}, MetadataSHA256: strings.Repeat("a", 64), MetadataBytes: 10}
}
func build(t *testing.T, sources ...Source) Snapshot {
t.Helper()
s, e := Build("project-A", "项目A", "delivery/项目A__project-A", sources)
if e != nil {
t.Fatal(e)
}
return s
}
func TestBatchMultipartIntegrityAndImmutableScopes(t *testing.T) {
a, b := fixture("part0001", 3), fixture("part0002", 8)
first := build(t, a)
full := build(t, a, b)
reversed := build(t, b, a)
if first.ID == full.ID || full.ID != reversed.ID {
t.Fatal("snapshot identity")
}
var index struct {
Summary Summary
Entries []Entry
Bytes int64 `json:"totalBytes"`
}
json.Unmarshal(full.Index, &index)
if index.Summary.PartCount != 2 || index.Summary.ExpectedPartCount != 2 || index.Summary.Completion != "full_run" || index.Summary.KeptCount != 2 || index.Summary.DiscardedCount != 2 || index.Summary.KeptDuration != 4 || index.Summary.DiscardedDuration != 2 {
t.Fatalf("summary %+v", index.Summary)
}
var size int64
for _, e := range index.Entries {
body := full.Files[e.Path]
if int64(len(body)) != e.SizeBytes || Hash(body) != e.SHA256 || e.Key != full.Root+"/"+e.Path {
t.Fatal("entry integrity", e)
}
size += e.SizeBytes
}
if size != index.Bytes {
t.Fatal("total bytes")
}
var manifest struct {
Entries []Entry
Summary Summary
Metadata struct {
Path string
Base string `json:"pathBase"`
} `json:"sourceMetadata"`
}
json.Unmarshal(full.Files["capture-A/annotation_manifest.json"], &manifest)
for _, e := range manifest.Entries {
if strings.Contains(e.Path, "/") || !bytes.Equal(full.Files["capture-A/"+e.Path], reversed.Files["capture-A/"+e.Path]) {
t.Fatal("capture-relative entry", e)
}
}
if manifest.Metadata.Path != "meta_capture-A.json" || manifest.Metadata.Base != "original_capture" {
t.Fatal("metadata reference")
}
again := build(t, a)
if !bytes.Equal(first.Index, again.Index) {
t.Fatal("old batch changed")
}
if !bytes.Contains(first.Index, []byte(`"partial_run"`)) || !bytes.Contains(first.Index, []byte(`"part0002"`)) {
t.Fatal("partial scope")
}
}
func TestBatchRejectsConflictingEvidence(t *testing.T) {
for _, tc := range []string{"duplicate", "revision-history", "missing-inventory", "description", "coverage", "acceptance", "embedded-history", "part-summary"} {
t.Run(tc, func(t *testing.T) {
a := fixture("part0001", 1)
sources := []Source{a}
var d map[string]any
json.Unmarshal(a.Part, &d)
switch tc {
case "duplicate":
sources = append(sources, fixture("part0001", 2))
case "revision-history":
sources[0].History = fixture("part0001", 2).History
case "missing-inventory":
sources[0].ExpectedParts = []string{"part0002"}
case "description":
d["slices"].([]any)[0].(map[string]any)["classification"].(map[string]any)["descriptionStatus"] = "described"
case "coverage":
d["discardedSlices"].([]any)[0].(map[string]any)["taskSplit"].(map[string]any)["startSecond"] = 2.5
case "acceptance":
d["acceptance"].(map[string]any)["finalAcceptance"].(map[string]any)["confirmed"] = false
case "part-summary":
d["deliveryClassification"].(map[string]any)["keptSliceCount"] = 99
case "embedded-history":
d["acceptance"].(map[string]any)["qualityReviews"] = []any{}
}
if tc == "description" || tc == "coverage" || tc == "acceptance" || tc == "embedded-history" || tc == "part-summary" {
sources[0].Part = encode(d)
}
if _, e := Build("project-A", "项目A", "delivery/项目A__project-A", sources); e == nil {
t.Fatal("accepted", tc)
}
})
}
}
func TestBatchAcceptanceUsesInstantsRatherThanTimestampText(t *testing.T) {
a, b := fixture("part0001", 1), fixture("part0002", 2)
var doc map[string]any
json.Unmarshal(a.Part, &doc)
doc["acceptance"].(map[string]any)["finalAcceptance"].(map[string]any)["acceptedAt"] = "2026-10-20T10:00:00+08:00"
a.Part = encode(doc)
json.Unmarshal(b.Part, &doc)
doc["acceptance"].(map[string]any)["finalAcceptance"].(map[string]any)["acceptedAt"] = "2026-10-20T03:00:00Z"
b.Part = encode(doc)
s := build(t, a, b)
var index struct{ Summary Summary }
json.Unmarshal(s.Index, &index)
if index.Summary.Acceptance["acceptedAt"] != "2026-10-20T03:00:00Z" {
t.Fatal("timezone aggregation", index.Summary.Acceptance)
}
}