| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // TestFixture is the visible end-to-end run: go test ./internal/team/ |
| 11 | // -run TestFixture -v prints the transcript of every case. |
| 12 | func TestFixture(t *testing.T) { |
| 13 | t.Run("software_fix_succeeds", func(t *testing.T) { |
| 14 | e := newEnv(t) |
| 15 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 16 | w := e.workflow(scope) |
| 17 | repro, base, err := w.Reproduce(e.ctx, []byte(FixtureBaseSource)) |
| 18 | must(t, err) |
| 19 | if base.Outcome != CheckFail || base.TestsRun != 3 || base.TestsFailed != 2 { |
| 20 | t.Fatalf("base check %+v", base) |
| 21 | } |
| 22 | cand, err := w.Implement(e.ctx, "implement", "ws-implement-1") |
| 23 | must(t, err) |
| 24 | _, err = w.ReviewCandidate(e.ctx, "ws-review-1", cand) |
| 25 | must(t, err) |
| 26 | cr, err := w.Verify(e.ctx, repro, cand) |
| 27 | must(t, err) |
| 28 | if cr.Outcome != CheckPass || cr.TestsRun != 3 { |
| 29 | t.Fatalf("candidate check %+v", cr) |
| 30 | } |
| 31 | _, _, err = w.Publish(e.ctx, cand) |
| 32 | must(t, err) |
| 33 | rep, err := w.Conclude(e.ctx) |
| 34 | must(t, err) |
| 35 | if !rep.Satisfied || rep.Candidate != cand.Hash || rep.Reproduction != repro.Hash || rep.PublishReceipt == "" { |
| 36 | t.Fatalf("report %+v", rep) |
| 37 | } |
| 38 | if j := e.job(scope); j.State != JobSucceeded { |
| 39 | t.Fatalf("job %s", j.State) |
| 40 | } |
| 41 | led, err := e.store.Ledger(e.ctx, scope) |
| 42 | must(t, err) |
| 43 | if led.Reserved != (Usage{}) || led.Spent.Calls != 4 || led.Spent.CostMicros == 0 { |
| 44 | t.Fatalf("ledger %+v", led) |
| 45 | } |
| 46 | if e.tracker.Events() != 1 { |
| 47 | t.Fatalf("tracker events %d", e.tracker.Events()) |
| 48 | } |
| 49 | t.Logf("ledger: spent=%+v open=%+v; authority introspections=%d", led.Spent, led.Reserved, e.auth.Calls()) |
| 50 | }) |
| 51 | |
| 52 | t.Run("weakened_reproduction", func(t *testing.T) { |
| 53 | e := newEnv(t) |
| 54 | e.impl.Respond("implement", "func Sum(a, b int) int { return 5 }") // passes only sum_2_3 |
| 55 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 56 | w := e.workflow(scope) |
| 57 | repro, _, err := w.Reproduce(e.ctx, []byte(FixtureBaseSource)) |
| 58 | must(t, err) |
| 59 | // The implementer cannot replace the reproduction: wrong capability. |
| 60 | h := e.claim(scope, "implement", "runner-a", "ws-implement-1") |
| 61 | _, err = e.store.Register(e.ctx, scope, h, ArtifactSpec{Role: RoleReproduction, MediaType: "application/json", Payload: mustJSON([]FixtureCase{{Name: "sum_2_3", A: 2, B: 3, Want: 5}}), |
| 62 | Provenance: Provenance{Producer: "runner-a", Provider: "fake-a", Context: "ctx-implement"}}) |
| 63 | wantCode(t, err, CodeScope) |
| 64 | t.Logf("implementer registering a reproduction: %v", err) |
| 65 | cand, err := e.store.Register(e.ctx, scope, h, ArtifactSpec{Role: RoleCandidate, MediaType: "text/x-fixture", Payload: []byte("func Sum(a, b int) int { return 5 }"), |
| 66 | Provenance: Provenance{Producer: "runner-a", Provider: "fake-a", Context: "ctx-implement"}}) |
| 67 | must(t, err) |
| 68 | // Once a candidate exists the reproduction is frozen even for reproduce. |
| 69 | must(t, w.finish(e.ctx, h, AttemptResult{Outcome: OutcomeSuccess})) |
| 70 | _, err = w.ReviewCandidate(e.ctx, "ws-review-1", cand) |
| 71 | must(t, err) |
| 72 | hv := e.claim(scope, "verify", "runner-c", "ws-verify-1") |
| 73 | weak := mustJSON([]FixtureCase{{Name: "sum_2_3", A: 2, B: 3, Want: 5}}) |
| 74 | weakResult := RunCheck(weak, cand.Payload) |
| 75 | t.Logf("weakened reproduction on candidate: %s run=%d failed=%d", weakResult.Outcome, weakResult.TestsRun, weakResult.TestsFailed) |
| 76 | _, err = e.store.Register(e.ctx, scope, hv, ArtifactSpec{Role: RoleCandidateResult, MediaType: "application/json", Payload: mustJSON(weakResult), |
| 77 | Provenance: Provenance{Producer: "runner-c", Provider: "fixture-check", Context: "ctx-verify"}}) |
| 78 | wantCode(t, err, CodeEvidence) |
| 79 | t.Logf("registering the weakened result: %v", err) |
| 80 | frozen, err := e.store.Artifact(e.ctx, scope, repro.ID) |
| 81 | must(t, err) |
| 82 | real := RunCheck(frozen.Payload, cand.Payload) |
| 83 | t.Logf("frozen reproduction on candidate: %s run=%d failed=%d %q", real.Outcome, real.TestsRun, real.TestsFailed, real.FailureMessage) |
| 84 | _, err = e.store.Register(e.ctx, scope, hv, ArtifactSpec{Role: RoleCandidateResult, MediaType: "application/json", Payload: mustJSON(real), |
| 85 | Provenance: Provenance{Producer: "runner-c", Provider: "fixture-check", Context: "ctx-verify"}}) |
| 86 | must(t, err) |
| 87 | must(t, w.finish(e.ctx, hv, AttemptResult{Outcome: OutcomeFailure, Detail: "candidate fails frozen reproduction"})) |
| 88 | rep, err := w.Conclude(e.ctx) |
| 89 | wantCode(t, err, CodeEvidence) |
| 90 | if rep.Satisfied || e.job(scope).State != JobFailed { |
| 91 | t.Fatalf("report %+v", rep) |
| 92 | } |
| 93 | }) |
| 94 | |
| 95 | t.Run("stale_review_after_candidate_edit", func(t *testing.T) { |
| 96 | e := newEnv(t) |
| 97 | _, scope := e.createJob(softwareSpec(SoftwareSteps(true))) |
| 98 | w := e.workflow(scope) |
| 99 | repro, _, err := w.Reproduce(e.ctx, []byte(FixtureBaseSource)) |
| 100 | must(t, err) |
| 101 | c1, err := w.Implement(e.ctx, "implement", "ws-implement-1") |
| 102 | must(t, err) |
| 103 | _, err = w.ReviewCandidate(e.ctx, "ws-review-1", c1) |
| 104 | must(t, err) |
| 105 | e.impl.Respond("implement", "func Sum(a, b int) int { return b + a }") |
| 106 | c2, err := w.Implement(e.ctx, "rework", "ws-rework-1") |
| 107 | must(t, err) |
| 108 | if c2.Hash == c1.Hash { |
| 109 | t.Fatal("rework produced the reviewed candidate") |
| 110 | } |
| 111 | cr, err := w.Verify(e.ctx, repro, c2) |
| 112 | must(t, err) |
| 113 | if cr.Outcome != CheckPass { |
| 114 | t.Fatalf("rework check %+v", cr) |
| 115 | } |
| 116 | _, _, err = w.Publish(e.ctx, c2) |
| 117 | must(t, err) |
| 118 | rep, err := w.Conclude(e.ctx) |
| 119 | wantCode(t, err, CodeEvidence) |
| 120 | if rep.Satisfied || rep.Candidate != c2.Hash { |
| 121 | t.Fatalf("report %+v", rep) |
| 122 | } |
| 123 | }) |
| 124 | |
| 125 | t.Run("reviewer_rejects_or_is_not_independent", func(t *testing.T) { |
| 126 | e := newEnv(t) |
| 127 | e.reviewer.Respond("review", "reject: the change widens Sum beyond the symptom") |
| 128 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 129 | w := e.workflow(scope) |
| 130 | _, _, err := w.Reproduce(e.ctx, []byte(FixtureBaseSource)) |
| 131 | must(t, err) |
| 132 | cand, err := w.Implement(e.ctx, "implement", "ws-implement-1") |
| 133 | must(t, err) |
| 134 | // A review from the implementer's own provider and context is refused. |
| 135 | h := e.claim(scope, "review", "runner-a", "ws-self-review") |
| 136 | _, err = e.store.Register(e.ctx, scope, h, ArtifactSpec{Role: RoleReview, MediaType: "application/json", Payload: mustJSON(Review{SubjectHash: cand.Hash, Verdict: VerdictApprove}), |
| 137 | Provenance: Provenance{Producer: "runner-a", Provider: "fake-a", Context: "ctx-implement"}}) |
| 138 | wantCode(t, err, CodeEvidence) |
| 139 | t.Logf("self-review refused: %v", err) |
| 140 | must(t, e.store.Release(e.ctx, scope, h, WaitRunnerUnavailable, "self review refused")) |
| 141 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor")) |
| 142 | _, err = w.ReviewCandidate(e.ctx, "ws-review-1", cand) |
| 143 | must(t, err) |
| 144 | rep, err := w.Conclude(e.ctx) |
| 145 | wantCode(t, err, CodeEvidence) |
| 146 | if rep.Satisfied || rep.Reason != "review verdict reject" { |
| 147 | t.Fatalf("report %+v", rep) |
| 148 | } |
| 149 | }) |
| 150 | |
| 151 | t.Run("reviewer_unavailable_then_back", func(t *testing.T) { |
| 152 | e := newEnv(t) |
| 153 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 154 | w := e.workflow(scope) |
| 155 | repro, _, err := w.Reproduce(e.ctx, []byte(FixtureBaseSource)) |
| 156 | must(t, err) |
| 157 | cand, err := w.Implement(e.ctx, "implement", "ws-implement-1") |
| 158 | must(t, err) |
| 159 | e.reviewer.SetUnavailable(true) |
| 160 | _, err = w.ReviewCandidate(e.ctx, "ws-review-1", cand) |
| 161 | if err == nil { |
| 162 | t.Fatal("review succeeded without a provider") |
| 163 | } |
| 164 | if j := e.job(scope); j.State != JobWaiting || j.Wait != WaitProviderLimit { |
| 165 | t.Fatalf("job %s/%s", j.State, j.Wait) |
| 166 | } |
| 167 | t.Logf("job waiting: %s", e.job(scope).Wait) |
| 168 | led, _ := e.store.Ledger(e.ctx, scope) |
| 169 | if led.Reserved != (Usage{}) { |
| 170 | t.Fatalf("refused call left a reservation open: %+v", led) |
| 171 | } |
| 172 | e.reviewer.SetUnavailable(false) |
| 173 | _, err = w.ReviewCandidate(e.ctx, "ws-review-2", cand) |
| 174 | must(t, err) |
| 175 | _, err = w.Verify(e.ctx, repro, cand) |
| 176 | must(t, err) |
| 177 | _, _, err = w.Publish(e.ctx, cand) |
| 178 | must(t, err) |
| 179 | rep, err := w.Conclude(e.ctx) |
| 180 | must(t, err) |
| 181 | if !rep.Satisfied { |
| 182 | t.Fatalf("report %+v", rep) |
| 183 | } |
| 184 | }) |
| 185 | |
| 186 | t.Run("lost_publication_reply", func(t *testing.T) { |
| 187 | e := newEnv(t) |
| 188 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 189 | w := e.workflow(scope) |
| 190 | repro, _, err := w.Reproduce(e.ctx, []byte(FixtureBaseSource)) |
| 191 | must(t, err) |
| 192 | cand, err := w.Implement(e.ctx, "implement", "ws-implement-1") |
| 193 | must(t, err) |
| 194 | _, err = w.ReviewCandidate(e.ctx, "ws-review-1", cand) |
| 195 | must(t, err) |
| 196 | _, err = w.Verify(e.ctx, repro, cand) |
| 197 | must(t, err) |
| 198 | e.forge.SetLoseReply(true) |
| 199 | h, id, err := w.Publish(e.ctx, cand) |
| 200 | wantCode(t, err, CodeUncertain) |
| 201 | if j := e.job(scope); j.State != JobWaiting || j.Wait != WaitUncertainOutcome { |
| 202 | t.Fatalf("job %s/%s", j.State, j.Wait) |
| 203 | } |
| 204 | // Same key again: no blind replay, no second reservation. |
| 205 | job := e.job(scope) |
| 206 | payload := mustJSON(publication{Repository: job.Target.Repository, Ref: job.Target.Ref, Head: cand.Hash}) |
| 207 | _, err = e.store.Intend(e.ctx, scope, h, IntentSpec{Key: "publish/" + short(cand.Hash), Operation: OpForgePublish, Payload: payload}) |
| 208 | wantCode(t, err, CodeUncertain) |
| 209 | _, err = e.store.Admit(e.ctx, scope, id) |
| 210 | wantCode(t, err, CodeState) |
| 211 | wantCode(t, e.store.Complete(e.ctx, scope, h, AttemptResult{Outcome: OutcomeSuccess}), CodeState) |
| 212 | t.Logf("publish uncertain: replay refused, completion refused, job %s/%s", e.job(scope).State, e.job(scope).Wait) |
| 213 | e.forge.SetLoseReply(false) |
| 214 | a, err := e.store.Reconcile(e.ctx, scope, id) |
| 215 | must(t, err) |
| 216 | if a.State != ActionSucceeded || a.Receipt == nil { |
| 217 | t.Fatalf("after reconcile %+v", a) |
| 218 | } |
| 219 | t.Logf("reconciled by forge receipt %s; job %s", a.Receipt.ID, e.job(scope).State) |
| 220 | if j := e.job(scope); j.State != JobRunning { |
| 221 | t.Fatalf("job %s", j.State) |
| 222 | } |
| 223 | must(t, w.FinishPublish(e.ctx, h, cand, *a.Receipt)) |
| 224 | rep, err := w.Conclude(e.ctx) |
| 225 | must(t, err) |
| 226 | if !rep.Satisfied || rep.PublishReceipt != a.Receipt.ID { |
| 227 | t.Fatalf("report %+v", rep) |
| 228 | } |
| 229 | if _, ok := e.forge.Published("publish/" + short(cand.Hash)); !ok { |
| 230 | t.Fatal("forge lost the publication") |
| 231 | } |
| 232 | }) |
| 233 | |
| 234 | t.Run("cancel_then_clean_restart", func(t *testing.T) { |
| 235 | e := newEnv(t) |
| 236 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 237 | w := e.workflow(scope) |
| 238 | _, _, err := w.Reproduce(e.ctx, []byte(FixtureBaseSource)) |
| 239 | must(t, err) |
| 240 | h := e.claim(scope, "implement", "runner-a", "ws-implement-1") |
| 241 | _, _, err = w.call(e.ctx, h, "implement/call-1", "implement", "fix it") |
| 242 | must(t, err) |
| 243 | rep, err := e.store.Cancel(e.ctx, scope, "paul", "changed my mind") |
| 244 | must(t, err) |
| 245 | t.Logf("cancel: stops pending %v, in flight %v, revoked %v, open %+v", rep.StopsPending, rep.InFlight, rep.Revoked, rep.OpenReserved) |
| 246 | if len(rep.StopsPending) != 1 || rep.StopsPending[0] != h.Attempt { |
| 247 | t.Fatalf("report %+v", rep) |
| 248 | } |
| 249 | _, err = e.store.Register(e.ctx, scope, h, ArtifactSpec{Role: RoleCandidate, MediaType: "text/x-fixture", Payload: []byte(FixtureFixedSource), |
| 250 | Provenance: Provenance{Producer: "runner-a", Provider: "fake-a", Context: "ctx"}}) |
| 251 | wantCode(t, err, CodeFence) |
| 252 | t.Logf("stale worker after cancel: %v", err) |
| 253 | _, err = e.store.Claim(e.ctx, scope, "review", "runner-b", "ws-review-1") |
| 254 | wantCode(t, err, CodeState) |
| 255 | atts, _ := e.store.Attempts(e.ctx, scope) |
| 256 | if atts[1].State != AttemptStopping || atts[1].StopRequested.IsZero() { |
| 257 | t.Fatalf("attempt %+v", atts[1]) |
| 258 | } |
| 259 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor")) |
| 260 | atts, _ = e.store.Attempts(e.ctx, scope) |
| 261 | if atts[1].State != AttemptStopped || atts[1].StopAcked.IsZero() { |
| 262 | t.Fatalf("attempt %+v", atts[1]) |
| 263 | } |
| 264 | must(t, e.store.Close()) |
| 265 | e.open() |
| 266 | st, err := e.store.Status(e.ctx) |
| 267 | must(t, err) |
| 268 | if st.Quarantine || st.AdmissionPaused || st.Incarnation != 2 { |
| 269 | t.Fatalf("status %+v", st) |
| 270 | } |
| 271 | if j := e.job(scope); j.State != JobCancelled { |
| 272 | t.Fatalf("job %s after restart", j.State) |
| 273 | } |
| 274 | t.Logf("clean restart: incarnation %d, job %s", st.Incarnation, e.job(scope).State) |
| 275 | }) |
| 276 | |
| 277 | t.Run("crash_after_publication_before_receipt_recorded", func(t *testing.T) { |
| 278 | e := newEnv(t) |
| 279 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 280 | w := e.workflow(scope) |
| 281 | repro, _, err := w.Reproduce(e.ctx, []byte(FixtureBaseSource)) |
| 282 | must(t, err) |
| 283 | cand, err := w.Implement(e.ctx, "implement", "ws-implement-1") |
| 284 | must(t, err) |
| 285 | _, err = w.ReviewCandidate(e.ctx, "ws-review-1", cand) |
| 286 | must(t, err) |
| 287 | _, err = w.Verify(e.ctx, repro, cand) |
| 288 | must(t, err) |
| 289 | h := e.claim(scope, "publish", "runner-a", "ws-publish-1") |
| 290 | job := e.job(scope) |
| 291 | payload := mustJSON(publication{Repository: job.Target.Repository, Ref: job.Target.Ref, Head: cand.Hash}) |
| 292 | id, err := e.store.Intend(e.ctx, scope, h, IntentSpec{Key: "publish/" + short(cand.Hash), Operation: OpForgePublish, Payload: payload}) |
| 293 | must(t, err) |
| 294 | d, err := e.store.Admit(e.ctx, scope, id) |
| 295 | must(t, err) |
| 296 | _, err = e.forge.Execute(e.ctx, d) |
| 297 | must(t, err) |
| 298 | // The forge has the receipt; the store dies before recording it. |
| 299 | e.crash() |
| 300 | e.openNoResume() |
| 301 | w = e.workflow(scope) |
| 302 | st, _ := e.store.Status(e.ctx) |
| 303 | if !st.Quarantine || !st.AdmissionPaused { |
| 304 | t.Fatalf("status after unclean restart %+v", st) |
| 305 | } |
| 306 | t.Logf("unclean restart: quarantine %q", st.QuarantineWhy) |
| 307 | _, err = e.store.Claim(e.ctx, scope, "publish", "runner-x", "ws-x") |
| 308 | wantCode(t, err, CodeAdmissionPaused) |
| 309 | rr, err := e.store.Resume(e.ctx) |
| 310 | must(t, err) |
| 311 | t.Logf("resume: revalidated %v reconciled %v unresolved %v resumed %v", rr.Revalidated, rr.Reconciled, rr.Unresolved, rr.Resumed) |
| 312 | if !rr.Resumed || len(rr.Reconciled) != 1 { |
| 313 | t.Fatalf("resume %+v", rr) |
| 314 | } |
| 315 | a, err := e.store.GetAction(e.ctx, scope, id) |
| 316 | must(t, err) |
| 317 | if a.State != ActionSucceeded || a.Receipt == nil { |
| 318 | t.Fatalf("action after receipt recovery: %+v", a) |
| 319 | } |
| 320 | must(t, w.FinishPublish(e.ctx, h, cand, *a.Receipt)) |
| 321 | rep, err := w.Conclude(e.ctx) |
| 322 | must(t, err) |
| 323 | if !rep.Satisfied { |
| 324 | t.Fatalf("report %+v", rep) |
| 325 | } |
| 326 | led, _ := e.store.Ledger(e.ctx, scope) |
| 327 | if led.Reserved != (Usage{}) { |
| 328 | t.Fatalf("open reservations after recovery %+v", led) |
| 329 | } |
| 330 | }) |
| 331 | |
| 332 | t.Run("stale_snapshot_with_newer_revocation", func(t *testing.T) { |
| 333 | e := newEnv(t) |
| 334 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 335 | w := e.workflow(scope) |
| 336 | _, _, err := w.Reproduce(e.ctx, []byte(FixtureBaseSource)) |
| 337 | must(t, err) |
| 338 | e.claim(scope, "implement", "runner-a", "ws-implement-1") |
| 339 | snap := e.snapshot() |
| 340 | e.auth.Revoke(grant) |
| 341 | must(t, e.store.Close()) |
| 342 | e.store = nil |
| 343 | s, err := Open(e.ctx, snap, e.options()) |
| 344 | must(t, err) |
| 345 | defer s.Close() |
| 346 | st, _ := s.Status(e.ctx) |
| 347 | if !st.Quarantine { |
| 348 | t.Fatalf("restored snapshot not quarantined: %+v", st) |
| 349 | } |
| 350 | t.Logf("restored snapshot: quarantine %q", st.QuarantineWhy) |
| 351 | j, err := s.GetJob(e.ctx, scope) |
| 352 | must(t, err) |
| 353 | if j.State != JobRunning { |
| 354 | t.Fatalf("snapshot job %s", j.State) |
| 355 | } |
| 356 | rr, err := s.Resume(e.ctx) |
| 357 | must(t, err) |
| 358 | j, _ = s.GetJob(e.ctx, scope) |
| 359 | t.Logf("resume on snapshot: cancelled %v; job %s (%s)", rr.Cancelled, j.State, j.Outcome) |
| 360 | if len(rr.Cancelled) != 1 || j.State != JobCancelled { |
| 361 | t.Fatalf("resume %+v job %s", rr, j.State) |
| 362 | } |
| 363 | _, err = s.Claim(e.ctx, scope, "implement", "runner-a", "ws-implement-2") |
| 364 | wantCode(t, err, CodeAdmissionPaused) |
| 365 | }) |
| 366 | |
| 367 | t.Run("authority_unavailable_fails_closed", func(t *testing.T) { |
| 368 | e := newEnv(t) |
| 369 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 370 | w := e.workflow(scope) |
| 371 | _, _, err := w.Reproduce(e.ctx, []byte(FixtureBaseSource)) |
| 372 | must(t, err) |
| 373 | h := e.claim(scope, "implement", "runner-a", "ws-implement-1") |
| 374 | e.auth.SetUnavailable(true) |
| 375 | _, _, err = w.call(e.ctx, h, "implement/call-1", "implement", "fix it") |
| 376 | wantCode(t, err, CodeAuthorityUnavailable) |
| 377 | _, err = e.store.Renew(e.ctx, scope, h) |
| 378 | wantCode(t, err, CodeAuthorityUnavailable) |
| 379 | if j := e.job(scope); j.State != JobRunning { |
| 380 | t.Fatalf("unavailable authority changed the job to %s", j.State) |
| 381 | } |
| 382 | a, _ := e.store.Actions(e.ctx, scope) |
| 383 | if a[0].State != ActionIntent { |
| 384 | t.Fatalf("action %s", a[0].State) |
| 385 | } |
| 386 | t.Logf("authority offline: intent kept at %s, job still %s, nothing revoked", a[0].State, e.job(scope).State) |
| 387 | must(t, e.store.Close()) |
| 388 | e.store = nil |
| 389 | s, err := Open(e.ctx, e.path, e.options()) |
| 390 | must(t, err) |
| 391 | e.store = s |
| 392 | _, err = s.Resume(e.ctx) |
| 393 | wantCode(t, err, CodeAuthorityUnavailable) |
| 394 | st, _ := s.Status(e.ctx) |
| 395 | if !st.AdmissionPaused { |
| 396 | t.Fatal("resume with authority offline opened admission") |
| 397 | } |
| 398 | e.auth.SetUnavailable(false) |
| 399 | rr, err := s.Resume(e.ctx) |
| 400 | must(t, err) |
| 401 | if !rr.Resumed { |
| 402 | t.Fatalf("resume %+v", rr) |
| 403 | } |
| 404 | }) |
| 405 | |
| 406 | t.Run("oficina_reconciliation", func(t *testing.T) { |
| 407 | e := newEnv(t) |
| 408 | spec := JobSpec{Account: acctA, Requester: "oficina", GrantRef: grant, |
| 409 | Target: TargetBinding{Kind: TargetBusiness, Subject: "ledger-period/2026-10"}, |
| 410 | Evidence: EvidenceRequirement{Predicate: PredicateAttestedReconciliation, RequiredSources: 2}, |
| 411 | Policy: testPolicy(), Steps: BusinessSteps()} |
| 412 | _, scope := e.createJob(spec) |
| 413 | w := e.workflow(scope) |
| 414 | bank := mustJSON(map[string]any{"source": "bank", "lines": []map[string]any{{"ref": "inv-1", "amount": 1200}, {"ref": "inv-2", "amount": 800}, {"ref": "fee-9", "amount": 15}}}) |
| 415 | ledger := mustJSON(map[string]any{"source": "ledger", "lines": []map[string]any{{"ref": "inv-1", "amount": 1200}, {"ref": "inv-2", "amount": 850}}}) |
| 416 | h := e.claim(scope, "collect", "runner-a", "ws-collect") |
| 417 | prov := Provenance{Producer: "runner-a", Provider: "fake-a", Context: "ctx-collect"} |
| 418 | src1, err := e.store.Register(e.ctx, scope, h, ArtifactSpec{Role: RoleSourceRecord, MediaType: "application/json", Payload: bank, Provenance: prov}) |
| 419 | must(t, err) |
| 420 | src2, err := e.store.Register(e.ctx, scope, h, ArtifactSpec{Role: RoleSourceRecord, MediaType: "application/json", Payload: ledger, Provenance: prov}) |
| 421 | must(t, err) |
| 422 | t.Logf("collected sources %s %s", short(src1.Hash), short(src2.Hash)) |
| 423 | must(t, w.finish(e.ctx, h, AttemptResult{Outcome: OutcomeSuccess, Detail: "2 sources"})) |
| 424 | h = e.claim(scope, "reconcile", "runner-a", "ws-reconcile") |
| 425 | _, _, err = w.call(e.ctx, h, "reconcile/call-1", "reconcile", "compare bank and ledger") |
| 426 | must(t, err) |
| 427 | // A reconciliation that hides discrepancies is refused. |
| 428 | _, err = e.store.Register(e.ctx, scope, h, ArtifactSpec{Role: RoleReconciliation, MediaType: "application/json", |
| 429 | Payload: []byte(`{"source_hashes":["` + src1.Hash + `","` + src2.Hash + `"],"matched":1}`), Provenance: Provenance{Producer: "runner-a", Provider: "fake-a", Context: "ctx-reconcile"}}) |
| 430 | wantCode(t, err, CodeEvidence) |
| 431 | t.Logf("reconciliation without discrepancies: %v", err) |
| 432 | rec := Reconciliation{SourceHashes: []string{src1.Hash, src2.Hash}, Matched: 1, Discrepancies: []Discrepancy{ |
| 433 | {Key: "inv-2", Detail: "bank 800, ledger 850"}, {Key: "fee-9", Detail: "bank 15, ledger absent"}}} |
| 434 | recArt, err := e.store.Register(e.ctx, scope, h, ArtifactSpec{Role: RoleReconciliation, MediaType: "application/json", Payload: mustJSON(rec), |
| 435 | Provenance: Provenance{Producer: "runner-a", Provider: "fake-a", Context: "ctx-reconcile"}}) |
| 436 | must(t, err) |
| 437 | t.Logf("reconciliation %s: matched %d, discrepancies %d", short(recArt.Hash), rec.Matched, len(rec.Discrepancies)) |
| 438 | must(t, w.finish(e.ctx, h, AttemptResult{Outcome: OutcomeSuccess})) |
| 439 | _, err = w.ReviewCandidate(e.ctx, "ws-review-1", recArt) |
| 440 | must(t, err) |
| 441 | rep, err := w.Conclude(e.ctx) |
| 442 | must(t, err) |
| 443 | if !rep.Satisfied || rep.Reconciliation != recArt.Hash || len(rep.Sources) != 2 { |
| 444 | t.Fatalf("report %+v", rep) |
| 445 | } |
| 446 | t.Logf("conclude: SATISFIED reconciliation=%s sources=%d review=%s(%s)", short(rep.Reconciliation), len(rep.Sources), short(rep.Review), rep.Reviewer.Provider) |
| 447 | }) |
| 448 | } |
| 449 | |
| 450 | // crash closes the database without the clean-shutdown marker, as a |
| 451 | // killed process would. |
| 452 | func (e *env) crash() { |
| 453 | e.t.Helper() |
| 454 | must(e.t, e.store.db.Close()) |
| 455 | e.store = nil |
| 456 | } |
| 457 | |
| 458 | // openNoResume opens the store and leaves it paused. |
| 459 | func (e *env) openNoResume() { |
| 460 | e.t.Helper() |
| 461 | s, err := Open(e.ctx, e.path, e.options()) |
| 462 | must(e.t, err) |
| 463 | e.store = s |
| 464 | } |
| 465 | |
| 466 | // snapshot checkpoints the WAL and copies the database file alone (no |
| 467 | // marker), the way a backup taken while running would look. |
| 468 | func (e *env) snapshot() string { |
| 469 | e.t.Helper() |
| 470 | _, err := e.store.db.ExecContext(context.Background(), `PRAGMA wal_checkpoint(TRUNCATE)`) |
| 471 | must(e.t, err) |
| 472 | raw, err := os.ReadFile(e.path) |
| 473 | must(e.t, err) |
| 474 | dst := filepath.Join(e.dir, "restored", "team.db") |
| 475 | must(e.t, os.MkdirAll(filepath.Dir(dst), 0o755)) |
| 476 | must(e.t, os.WriteFile(dst, raw, 0o644)) |
| 477 | return dst |
| 478 | } |
| 479 | |