gunk-dev/the-valley

verdict_test.go

source636d69c11f37main's published tip history
10.0 KiB
1package main
2
3// The commit rule, case by case. The verdict is a function of data, so
4// every rule of dcr-439b771 is exercised here without a repository, a key,
5// or a clock. What a repository does to produce these inputs is the refs
6// transport's, and it is exercised end to end by the integrator-e2e check.
7
8import (
9 "strings"
10 "testing"
11 "time"
12)
13
14const (
15 attestedTree = "aaaa"
16 movedTree = "bbbb"
17 closureA = "1111"
18 closureB = "2222"
19)
20
21var observed = time.Date(2026, 8, 6, 12, 0, 0, 0, time.UTC)
22
23func pureCheck(name string) Required {
24 return Required{Name: name, Classes: []string{"code"}, Runner: "nix", Attribute: name}
25}
26
27func effectfulCheck(name string, window time.Duration) Required {
28 return Required{Name: name, Classes: []string{"deploy"}, Runner: "command", Validity: window}
29}
30
31func pureEvidence(name, inputs string) Evidence {
32 return Evidence{Check: name, Kind: Pure, Admissible: Admissible, Result: "passed", Inputs: inputs, TextDigest: "d" + name}
33}
34
35func effectfulEvidence(name string, at time.Time) Evidence {
36 return Evidence{Check: name, Kind: Effectful, Admissible: Admissible, Result: "passed", ObservedAt: at, TextDigest: "d" + name}
37}
38
39func change(evidence ...Evidence) Change {
40 ch := Change{ID: "c1", Target: "refs/heads/main", Attested: attestedTree, Evidence: map[string]Evidence{}}
41 for _, e := range evidence {
42 ch.Evidence[e.Check] = e
43 }
44 return ch
45}
46
47func TestUnmovedTreeTransfersEveryPureCheck(t *testing.T) {
48 // Nothing intervened, so the landed tree is the attested tree and
49 // there is nothing for a closure comparison to compare.
50 v := Decide(
51 change(pureEvidence("build", closureA), pureEvidence("lint", closureB)),
52 Snapshot{
53 Apply: Applied{Clean: true, Tree: attestedTree},
54 Required: []Required{pureCheck("build"), pureCheck("lint")},
55 })
56 if v.Outcome != Land {
57 t.Fatalf("outcome = %s (%s)", v.Outcome, v.Reason)
58 }
59 for _, cv := range v.Checks {
60 if cv.Rule != "unmoved" {
61 t.Errorf("%s transferred by %q, want unmoved", cv.Name, cv.Rule)
62 }
63 }
64}
65
66func TestClosureEqualityTransfersAndInequalityInvalidates(t *testing.T) {
67 // The base moved. build reads what the intervening landing changed;
68 // lint does not. Exactly one check is named, and the other is
69 // reported as having transferred.
70 v := Decide(
71 change(pureEvidence("build", closureA), pureEvidence("lint", closureB)),
72 Snapshot{
73 Apply: Applied{Clean: true, Tree: movedTree},
74 Required: []Required{pureCheck("build"), pureCheck("lint")},
75 Closures: map[string]string{"build": "3333", "lint": closureB},
76 })
77 if v.Outcome != Stale || v.StaleReason != "evidence" {
78 t.Fatalf("outcome = %s/%s (%s)", v.Outcome, v.StaleReason, v.Reason)
79 }
80 if len(v.Invalidated) != 1 || v.Invalidated[0] != "build" {
81 t.Fatalf("invalidated = %v, want exactly [build]", v.Invalidated)
82 }
83 for _, cv := range v.Checks {
84 if cv.Name == "lint" && (!cv.Transferred || cv.Rule != "closure") {
85 t.Errorf("lint = %+v, want transferred by closure equality", cv)
86 }
87 if cv.Name == "build" && !strings.Contains(cv.Reason, "input closure changed") {
88 t.Errorf("build reason = %q", cv.Reason)
89 }
90 }
91}
92
93func TestConflictDemandsEveryCheckAgain(t *testing.T) {
94 // A conflict is new authorship, so retry is not partial.
95 v := Decide(
96 change(pureEvidence("build", closureA), pureEvidence("lint", closureB)),
97 Snapshot{
98 Apply: Applied{Clean: false, Conflict: "CONFLICT (content): src/main.txt"},
99 Required: []Required{pureCheck("build"), pureCheck("lint")},
100 })
101 if v.Outcome != Stale || v.StaleReason != "conflict" {
102 t.Fatalf("outcome = %s/%s", v.Outcome, v.StaleReason)
103 }
104 if got := strings.Join(v.Invalidated, ","); got != "build,lint" {
105 t.Fatalf("invalidated = %q, want every required check", got)
106 }
107}
108
109func TestUnknownSignerDoesNotTransfer(t *testing.T) {
110 // Not a forgery: a well-formed statement by a signer the integrator
111 // does not hold. Deciding what to do about one belongs to the trust
112 // backstop, so here it simply fails to transfer.
113 ev := pureEvidence("build", closureA)
114 ev.Admissible = UnknownSigner
115 v := Decide(change(ev), Snapshot{
116 Apply: Applied{Clean: true, Tree: attestedTree},
117 Required: []Required{pureCheck("build")},
118 })
119 if v.Outcome != Stale {
120 t.Fatalf("outcome = %s, want stale", v.Outcome)
121 }
122 if !strings.Contains(v.Checks[0].Reason, "no signer the integrator holds") {
123 t.Fatalf("reason = %q", v.Checks[0].Reason)
124 }
125}
126
127func TestTamperedEvidenceIsRejectedNotStale(t *testing.T) {
128 ev := pureEvidence("build", closureA)
129 ev.Admissible = Tampered
130 v := Decide(change(ev), Snapshot{
131 Apply: Applied{Clean: true, Tree: attestedTree},
132 Required: []Required{pureCheck("build")},
133 })
134 if v.Outcome != Reject {
135 t.Fatalf("outcome = %s, want reject", v.Outcome)
136 }
137 if len(v.Invalidated) != 0 {
138 t.Fatalf("a rejection named %v as stale; re-attesting would not fix a forgery", v.Invalidated)
139 }
140}
141
142func TestMissingAndFailedEvidence(t *testing.T) {
143 failed := pureEvidence("lint", closureB)
144 failed.Result = "failed"
145 v := Decide(change(failed), Snapshot{
146 Apply: Applied{Clean: true, Tree: attestedTree},
147 Required: []Required{pureCheck("build"), pureCheck("lint")},
148 })
149 if v.Outcome != Stale {
150 t.Fatalf("outcome = %s", v.Outcome)
151 }
152 if got := strings.Join(v.Invalidated, ","); got != "build,lint" {
153 t.Fatalf("invalidated = %q", got)
154 }
155 if !strings.Contains(v.Checks[0].Reason, "no attestation") {
156 t.Errorf("build reason = %q", v.Checks[0].Reason)
157 }
158 if !strings.Contains(v.Checks[1].Reason, "records the check as failed") {
159 t.Errorf("lint reason = %q", v.Checks[1].Reason)
160 }
161}
162
163func TestEffectfulTransfersWhileScopeUntouchedAndObservationFresh(t *testing.T) {
164 v := Decide(
165 change(effectfulEvidence("reachable", observed)),
166 Snapshot{
167 Apply: Applied{Clean: true, Tree: movedTree},
168 Required: []Required{effectfulCheck("reachable", time.Hour)},
169 Touched: map[string]bool{"prose": true},
170 Now: observed.Add(30 * time.Minute),
171 })
172 if v.Outcome != Land {
173 t.Fatalf("outcome = %s (%s)", v.Outcome, v.Reason)
174 }
175 if v.Checks[0].Rule != "untouched" {
176 t.Fatalf("rule = %q, want untouched", v.Checks[0].Rule)
177 }
178}
179
180func TestEffectfulFailsWhenItsClassWasTouched(t *testing.T) {
181 v := Decide(
182 change(effectfulEvidence("reachable", observed)),
183 Snapshot{
184 Apply: Applied{Clean: true, Tree: movedTree},
185 Required: []Required{effectfulCheck("reachable", time.Hour)},
186 Touched: map[string]bool{"deploy": true},
187 Now: observed.Add(time.Minute),
188 })
189 if v.Outcome != Stale || !strings.Contains(v.Checks[0].Reason, "touched the deploy class") {
190 t.Fatalf("verdict = %s, reason = %q", v.Outcome, v.Checks[0].Reason)
191 }
192}
193
194func TestEffectfulExpiresWithItsWindow(t *testing.T) {
195 v := Decide(
196 change(effectfulEvidence("reachable", observed)),
197 Snapshot{
198 Apply: Applied{Clean: true, Tree: attestedTree},
199 Required: []Required{effectfulCheck("reachable", time.Hour)},
200 Now: observed.Add(90 * time.Minute),
201 })
202 if v.Outcome != Stale || !strings.Contains(v.Checks[0].Reason, "the declared window is 1h0m0s") {
203 t.Fatalf("verdict = %s, reason = %q", v.Outcome, v.Checks[0].Reason)
204 }
205}
206
207func TestEffectfulWithNoDeclaredWindowIsReDemanded(t *testing.T) {
208 // Δ is declared policy. Absent a declaration there is no window, and
209 // the integrator may not invent one.
210 v := Decide(
211 change(effectfulEvidence("reachable", observed)),
212 Snapshot{
213 Apply: Applied{Clean: true, Tree: attestedTree},
214 Required: []Required{effectfulCheck("reachable", 0)},
215 Now: observed.Add(time.Second),
216 })
217 if v.Outcome != Stale || !strings.Contains(v.Checks[0].Reason, "declares no validity window") {
218 t.Fatalf("verdict = %s, reason = %q", v.Outcome, v.Checks[0].Reason)
219 }
220}
221
222func TestAnUnmovedTreeDoesNotExemptAnEffectfulCheckFromItsWindow(t *testing.T) {
223 // A pure claim carries no expiry because time is not among its
224 // inputs. An effectful one does, whatever happened to the tree.
225 v := Decide(
226 change(pureEvidence("build", closureA), effectfulEvidence("reachable", observed)),
227 Snapshot{
228 Apply: Applied{Clean: true, Tree: attestedTree},
229 Required: []Required{pureCheck("build"), effectfulCheck("reachable", time.Hour)},
230 Now: observed.Add(2 * time.Hour),
231 })
232 if v.Outcome != Stale {
233 t.Fatalf("outcome = %s", v.Outcome)
234 }
235 if got := strings.Join(v.Invalidated, ","); got != "reachable" {
236 t.Fatalf("invalidated = %q, want only the effectful check", got)
237 }
238}
239
240func TestClosureThatCouldNotBeRecomputedIsNotATransfer(t *testing.T) {
241 v := Decide(
242 change(pureEvidence("build", closureA)),
243 Snapshot{
244 Apply: Applied{Clean: true, Tree: movedTree},
245 Required: []Required{pureCheck("build")},
246 ClosureErrors: map[string]string{"build": "no such check attribute"},
247 })
248 if v.Outcome != Stale || !strings.Contains(v.Checks[0].Reason, "could not be recomputed") {
249 t.Fatalf("verdict = %s, reason = %q", v.Outcome, v.Checks[0].Reason)
250 }
251}
252
253func TestDeriverOutputIsRead(t *testing.T) {
254 // The one place the integrator is coupled to the deriver's output.
255 out := `abc1234..def5678: 3 changed path(s)
256policy: instance + policy
257classes matched: code, prose
258
259required checks:
260 code-build mandatory code
261 prose-format default prose, knowledge
262
263unclassified: 1 path(s) matched no class
264 odd.txt
265`
266 d, err := parseDerived(out)
267 if err != nil {
268 t.Fatal(err)
269 }
270 if !d.classes["code"] || !d.classes["prose"] || len(d.classes) != 2 {
271 t.Fatalf("classes = %v", d.classes)
272 }
273 if got := strings.Join(d.requires["prose-format"], "+"); got != "prose+knowledge" {
274 t.Fatalf("prose-format required by %q", got)
275 }
276 if got := strings.Join(d.requires["code-build"], "+"); got != "code" {
277 t.Fatalf("code-build required by %q", got)
278 }
279 if len(d.requires) != 2 {
280 t.Fatalf("required = %v", d.requires)
281 }
282}
283
284func TestValidityWindowsAreReadAsThePolicyWritesThem(t *testing.T) {
285 for _, c := range []struct {
286 in string
287 want time.Duration
288 }{
289 {"", 0},
290 {"30s", 30 * time.Second},
291 {"15m", 15 * time.Minute},
292 {"6h", 6 * time.Hour},
293 {"2d", 48 * time.Hour},
294 } {
295 got, err := parseWindow(c.in)
296 if err != nil || got != c.want {
297 t.Errorf("parseWindow(%q) = %v, %v; want %v", c.in, got, err, c.want)
298 }
299 }
300}