|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { Effect, Predicate } from "effect"; |
| 3 | + |
| 4 | +import { makeInMemoryBlobStore, type BlobStore } from "./blob"; |
| 5 | +import { |
| 6 | + makePendingApprovalStore, |
| 7 | + PENDING_APPROVAL_TTL_MS, |
| 8 | + type PendingApproval, |
| 9 | +} from "./pending-approval"; |
| 10 | + |
| 11 | +// --------------------------------------------------------------------------- |
| 12 | +// Focused tests — pending-approval consume atomicity, deterministic layer. |
| 13 | +// |
| 14 | +// These tests pin the exactly-once consume invariant deterministically, |
| 15 | +// plus the expiry/corrupt semantics required to survive the consume |
| 16 | +// restructure. A companion fast-check property (concurrent consumes ⇒ |
| 17 | +// exactly one non-null) lives elsewhere. |
| 18 | +// |
| 19 | +// NOTE on test infrastructure: |
| 20 | +// - it.effect runs under @effect/vitest's TestContext scheduler, where |
| 21 | +// Effect.sleep never advances — any async boundary deadlocks the test. |
| 22 | +// Sync-only effects work; async ones must go through Effect.runPromise in |
| 23 | +// a plain vitest test. |
| 24 | +// - The concurrency proof needs the real scheduler AND an explicit |
| 25 | +// read-barrier: without one, the synchronous Map store completes each |
| 26 | +// consume before the next fiber starts, so the double-resume race can |
| 27 | +// never manifest and the test would be vacuous as a concurrency proof. |
| 28 | +// --------------------------------------------------------------------------- |
| 29 | + |
| 30 | +const approval = (overrides?: Partial<PendingApproval>): PendingApproval => ({ |
| 31 | + executionId: "exec_1", |
| 32 | + artifactId: "art_1", |
| 33 | + code: 'return await tools.github.user.main.issues.create({"title":"x"})', |
| 34 | + address: "github.user.main.issues.create", |
| 35 | + expiresAt: Date.now() + PENDING_APPROVAL_TTL_MS, |
| 36 | + ...overrides, |
| 37 | +}); |
| 38 | + |
| 39 | +describe("approval consume atomicity (compareAndDelete gate)", () => { |
| 40 | + it("exactly one of N concurrent consumes wins (single-winner invariant)", async () => { |
| 41 | + const N = 8; |
| 42 | + const backing = makeInMemoryBlobStore(); |
| 43 | + // Promise-latch barrier: every fiber increments the arrival count after |
| 44 | + // its read and parks on the shared promise, which resolves only when |
| 45 | + // all N have arrived — every read happens before any delete. Pure JS |
| 46 | + // promise semantics, immune to Effect scheduler/runtime differences. |
| 47 | + let arrived = 0; |
| 48 | + let releaseAll!: () => void; |
| 49 | + const allArrived = new Promise<void>((resolve) => { |
| 50 | + releaseAll = resolve; |
| 51 | + }); |
| 52 | + const barrier: BlobStore = { |
| 53 | + ...backing, |
| 54 | + get: (ns, key) => |
| 55 | + backing.get(ns, key).pipe( |
| 56 | + Effect.tap(() => |
| 57 | + Effect.sync(() => { |
| 58 | + arrived++; |
| 59 | + if (arrived === N) releaseAll(); |
| 60 | + }), |
| 61 | + ), |
| 62 | + // tap (not andThen — that would discard the payload): park until |
| 63 | + // every fiber has read, then pass the payload through untouched. |
| 64 | + Effect.tap(() => Effect.promise(() => allArrived)), |
| 65 | + ), |
| 66 | + }; |
| 67 | + |
| 68 | + const outcome = await Effect.runPromise( |
| 69 | + Effect.gen(function* () { |
| 70 | + const store = makePendingApprovalStore(barrier, "u:t:s"); |
| 71 | + yield* store.put(approval()); |
| 72 | + |
| 73 | + const results = yield* Effect.all( |
| 74 | + Array.from({ length: N }, () => store.consume("exec_1")), |
| 75 | + { concurrency: "unbounded" }, |
| 76 | + ); |
| 77 | + const winners = results.filter(Predicate.isNotNull); |
| 78 | + |
| 79 | + // Post-condition: the record is gone for everyone. |
| 80 | + const after = yield* store.consume("exec_1"); |
| 81 | + return { winners: winners.map((w) => w.address), after }; |
| 82 | + }), |
| 83 | + ); |
| 84 | + |
| 85 | + expect(outcome.winners.length).toBe(1); |
| 86 | + expect(outcome.winners[0]).toBe("github.user.main.issues.create"); |
| 87 | + expect(outcome.after).toBeNull(); |
| 88 | + }); |
| 89 | + |
| 90 | + it.effect("a replayed consume after a win reads absent", () => |
| 91 | + Effect.gen(function* () { |
| 92 | + const store = makePendingApprovalStore(makeInMemoryBlobStore(), "u:t:s"); |
| 93 | + yield* store.put(approval()); |
| 94 | + expect(yield* store.consume("exec_1")).not.toBeNull(); |
| 95 | + expect(yield* store.consume("exec_1")).toBeNull(); |
| 96 | + expect(yield* store.consume("exec_1")).toBeNull(); |
| 97 | + }), |
| 98 | + ); |
| 99 | + |
| 100 | + it.effect("expired records are consumed-and-dropped (never retried)", () => |
| 101 | + Effect.gen(function* () { |
| 102 | + const blobs = makeInMemoryBlobStore(); |
| 103 | + let now = 1_000_000; |
| 104 | + const store = makePendingApprovalStore(blobs, "u:t:s", () => now); |
| 105 | + yield* store.put(approval({ expiresAt: now + 10 })); |
| 106 | + |
| 107 | + now += 100; // expires |
| 108 | + expect(yield* store.consume("exec_1")).toBeNull(); |
| 109 | + |
| 110 | + // The record is gone — rolling the clock back does not resurrect it. |
| 111 | + now = 1_000_000; |
| 112 | + expect(yield* store.consume("exec_1")).toBeNull(); |
| 113 | + }), |
| 114 | + ); |
| 115 | + |
| 116 | + it.effect("corrupt records are consumed-and-dropped (never surfaced)", () => |
| 117 | + Effect.gen(function* () { |
| 118 | + const blobs = makeInMemoryBlobStore(); |
| 119 | + yield* blobs.put("u:t:s/@pending-approval", "exec_1", "not-json{{"); |
| 120 | + |
| 121 | + const store = makePendingApprovalStore(blobs, "u:t:s"); |
| 122 | + expect(yield* store.consume("exec_1")).toBeNull(); |
| 123 | + |
| 124 | + // Gone for good — a second consume finds nothing. |
| 125 | + expect(yield* store.consume("exec_1")).toBeNull(); |
| 126 | + }), |
| 127 | + ); |
| 128 | +}); |
0 commit comments