Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

codexResetCredit.test.ts

86 lines3.5 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Deferred from "effect/Deferred";
3import * as Effect from "effect/Effect";
4import * as Fiber from "effect/Fiber";
5import * as Ref from "effect/Ref";
6​
7import { CodexResetCreditCoordinator, layerTest } from "./codexResetCredit.ts";
8​
9describe("CodexResetCreditCoordinator", () => {
10 it.effect("re-sends the same idempotency key after a failed attempt, then clears it", () =>
11 Effect.gen(function* () {
12 const { redeem } = yield* CodexResetCreditCoordinator;
13 const keys = yield* Ref.make<ReadonlyArray<string>>([]);
14 const attempts = yield* Ref.make(0);
15 const consume = (key: string) =>
16 Effect.gen(function* () {
17 yield* Ref.update(keys, (seen) => [...seen, key]);
18 const attempt = yield* Ref.updateAndGet(attempts, (n) => n + 1);
19 if (attempt === 1) return yield* Effect.fail("timed out" as const);
20 return "reset" as const;
21 });
22​
23 const first = yield* redeem("acct", consume).pipe(Effect.result);
24 assert.isTrue(first._tag === "Failure");
25 const second = yield* redeem("acct", consume);
26 assert.strictEqual(second, "reset");
27 // A fresh redemption after success must be a fresh attempt.
28 yield* redeem("acct", consume);
29​
30 const seen = yield* Ref.get(keys);
31 assert.strictEqual(seen.length, 3);
32 assert.strictEqual(seen[0], seen[1]);
33 assert.notStrictEqual(seen[1], seen[2]);
34 }).pipe(Effect.provide(layerTest)),
35 );
36​
37 it.effect("serialises concurrent redemptions on the same account, not per caller", () =>
38 Effect.gen(function* () {
39 const { redeem } = yield* CodexResetCreditCoordinator;
40 const release = yield* Deferred.make<void>();
41 const inFlight = yield* Ref.make(0);
42 const peak = yield* Ref.make(0);
43 const consume = () =>
44 Effect.gen(function* () {
45 const now = yield* Ref.updateAndGet(inFlight, (n) => n + 1);
46 yield* Ref.update(peak, (p) => Math.max(p, now));
47 yield* Deferred.await(release);
48 yield* Ref.update(inFlight, (n) => n - 1);
49 return "reset" as const;
50 });
51​
52 // Two instances of the same account redeem at once.
53 const a = yield* redeem("acct", consume).pipe(Effect.forkChild);
54 const b = yield* redeem("acct", consume).pipe(Effect.forkChild);
55 yield* Effect.yieldNow;
56 yield* Deferred.succeed(release, undefined);
57 yield* Fiber.join(a);
58 yield* Fiber.join(b);
59​
60 assert.strictEqual(yield* Ref.get(peak), 1);
61 }).pipe(Effect.provide(layerTest)),
62 );
63​
64 it.effect("keeps different accounts independent", () =>
65 Effect.gen(function* () {
66 const { redeem } = yield* CodexResetCreditCoordinator;
67 const release = yield* Deferred.make<void>();
68 const peak = yield* Ref.make(0);
69 const inFlight = yield* Ref.make(0);
70 const consume = () =>
71 Effect.gen(function* () {
72 const now = yield* Ref.updateAndGet(inFlight, (n) => n + 1);
73 yield* Ref.update(peak, (p) => Math.max(p, now));
74 yield* Deferred.await(release);
75 return "reset" as const;
76 });
77 const a = yield* redeem("acct-a", consume).pipe(Effect.forkChild);
78 const b = yield* redeem("acct-b", consume).pipe(Effect.forkChild);
79 yield* Effect.yieldNow;
80 yield* Deferred.succeed(release, undefined);
81 yield* Fiber.join(a);
82 yield* Fiber.join(b);
83 assert.strictEqual(yield* Ref.get(peak), 2);
84 }).pipe(Effect.provide(layerTest)),
85 );
86});