|
| 1 | +package boatstack |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// Regression for the evidence-path split-brain: activate-plan writes the evidence |
| 11 | +// ledger under compiled/, and pr-context resolves it through the shared dual-layout |
| 12 | +// rule (feature-root canonical, compiled/ fallback). But the delivery-gate recorder |
| 13 | +// used to hand-join the feature-root path with no fallback, so once a real project |
| 14 | +// kept its ledger only at compiled/evidence.md the recorder could not find it and |
| 15 | +// the gate failed with "delivery gate requires current evidence". These tests pin |
| 16 | +// the recorder to the same resolver every other layer uses. |
| 17 | + |
| 18 | +// TestFeatureEvidencePathResolvesBothLayouts locks the shared resolver's ordering: |
| 19 | +// feature-root (legacy canonical) first, compiled/ as the current-layout fallback, |
| 20 | +// and the canonical path returned even when neither exists so the caller reports a |
| 21 | +// clear error location. |
| 22 | +func TestFeatureEvidencePathResolvesBothLayouts(t *testing.T) { |
| 23 | + dir := t.TempDir() |
| 24 | + root := filepath.Join(dir, "evidence.md") |
| 25 | + compiled := filepath.Join(dir, "compiled", "evidence.md") |
| 26 | + |
| 27 | + // With neither present the resolver returns its last candidate (the compiled |
| 28 | + // copy) so a missing-evidence error names the canonical write location. |
| 29 | + if got := featureEvidencePath(dir); got != compiled { |
| 30 | + t.Fatalf("with neither present, want the compiled error path %q, got %q", compiled, got) |
| 31 | + } |
| 32 | + |
| 33 | + if err := os.MkdirAll(filepath.Dir(compiled), 0o755); err != nil { |
| 34 | + t.Fatal(err) |
| 35 | + } |
| 36 | + if err := os.WriteFile(compiled, []byte("x"), 0o644); err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + if got := featureEvidencePath(dir); got != compiled { |
| 40 | + t.Fatalf("with only compiled present, want %q, got %q", compiled, got) |
| 41 | + } |
| 42 | + |
| 43 | + if err := os.WriteFile(root, []byte("x"), 0o644); err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + if got := featureEvidencePath(dir); got != root { |
| 47 | + t.Fatalf("with both present, legacy root must win to match pr-context, got %q", got) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// TestRecordGateResolvesCompiledEvidenceLedger is the proof of fix: a delivery whose |
| 52 | +// ledger lives ONLY at compiled/evidence.md (the taxweave state) must gate cleanly |
| 53 | +// with no explicit --evidence. This fails on the pre-fix recorder, which resolved |
| 54 | +// only the feature root. |
| 55 | +func TestRecordGateResolvesCompiledEvidenceLedger(t *testing.T) { |
| 56 | + repo, feature := activateTwoSliceDelivery(t) |
| 57 | + dir := filepath.Join(repo, ".product-loop", "features", feature) |
| 58 | + |
| 59 | + // Move the edited gate ledger to the compiled layout and drop the feature-root |
| 60 | + // copy, so only compiled/evidence.md carries the gate outcomes. |
| 61 | + ledger := "# Evidence ledger\n\n- Test gate (phase-one): `PASS`\n- Review gate (phase-one): `PASS`\n- Test gate (phase-two): `BLOCKED`\n- Review gate (phase-two): `BLOCKED`\n" |
| 62 | + if err := os.WriteFile(filepath.Join(dir, "compiled", "evidence.md"), []byte(ledger), 0o644); err != nil { |
| 63 | + t.Fatal(err) |
| 64 | + } |
| 65 | + if err := os.Remove(filepath.Join(dir, "evidence.md")); err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + runGit(t, repo, "add", "-A") |
| 69 | + runGit(t, repo, "commit", "-m", "keep the evidence ledger only in the compiled layout") |
| 70 | + |
| 71 | + receipt, err := RecordDeliveryGate(DeliveryGateOptions{Repo: repo, Feature: feature, SliceID: "phase-one", Gate: "test", Status: "PASS"}) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("recorder could not resolve compiled-only evidence ledger: %v", err) |
| 74 | + } |
| 75 | + if !strings.HasSuffix(filepath.ToSlash(receipt.EvidencePath), "compiled/evidence.md") { |
| 76 | + t.Fatalf("recorder bound the wrong evidence path: %q", receipt.EvidencePath) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// TestRecordGateStillResolvesFeatureRootEvidence guards the legacy layout: a ledger |
| 81 | +// at the feature root (no compiled copy) must still gate, so the added fallback is |
| 82 | +// strictly additive. |
| 83 | +func TestRecordGateStillResolvesFeatureRootEvidence(t *testing.T) { |
| 84 | + repo, feature := activateTwoSliceDelivery(t) |
| 85 | + dir := filepath.Join(repo, ".product-loop", "features", feature) |
| 86 | + |
| 87 | + // activateTwoSliceDelivery already writes the ledger at the feature root; remove |
| 88 | + // the compiled copy so only the legacy location remains. |
| 89 | + if err := os.Remove(filepath.Join(dir, "compiled", "evidence.md")); err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + runGit(t, repo, "add", "-A") |
| 93 | + runGit(t, repo, "commit", "-m", "keep the evidence ledger only at the feature root") |
| 94 | + |
| 95 | + receipt, err := RecordDeliveryGate(DeliveryGateOptions{Repo: repo, Feature: feature, SliceID: "phase-one", Gate: "test", Status: "PASS"}) |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("recorder could not resolve feature-root evidence ledger: %v", err) |
| 98 | + } |
| 99 | + if strings.Contains(filepath.ToSlash(receipt.EvidencePath), "compiled/") { |
| 100 | + t.Fatalf("recorder ignored the legacy feature-root ledger: %q", receipt.EvidencePath) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// TestRecorderAndPRContextResolveSameEvidence is the anti-drift invariant: the |
| 105 | +// recorder's default evidence resolution and pr-context both route through |
| 106 | +// featureEvidencePath, so for any given feature they can never resolve different |
| 107 | +// evidence files. This is the structural guarantee the layers cannot re-diverge — |
| 108 | +// the compiled-artifact analogue of the published-slice addressability invariant. |
| 109 | +func TestRecorderAndPRContextResolveSameEvidence(t *testing.T) { |
| 110 | + dir := t.TempDir() |
| 111 | + compiled := filepath.Join(dir, "compiled", "evidence.md") |
| 112 | + if err := os.MkdirAll(filepath.Dir(compiled), 0o755); err != nil { |
| 113 | + t.Fatal(err) |
| 114 | + } |
| 115 | + if err := os.WriteFile(compiled, []byte("x"), 0o644); err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + |
| 119 | + // The recorder's default (empty --evidence) and pr-context's managedPRSources |
| 120 | + // both call featureEvidencePath(dir); assert that single resolver returns the |
| 121 | + // file that actually exists rather than a hand-joined feature-root guess. |
| 122 | + if got := featureEvidencePath(dir); got != compiled { |
| 123 | + t.Fatalf("shared resolver must return the existing ledger both consumers read, got %q", got) |
| 124 | + } |
| 125 | +} |
0 commit comments