@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 129 lines 5.34 KB
Raw
1 import { assert, describe, it } from "@effect/vitest";
2 import * as Deferred from "effect/Deferred";
3 import * as Effect from "effect/Effect";
4 import * as Fiber from "effect/Fiber";
5 import * as Ref from "effect/Ref";
6 import * as TestClock from "effect/testing/TestClock";
7
8 import * as ResetCreditCoordinator from "./resetCreditCoordinator.ts";
9
10 describe("ResetCreditCoordinator", () => {
11 it.effect("re-sends the same idempotency key after a failed attempt, then clears it", () =>
12 Effect.gen(function* () {
13 const { redeem } = yield* ResetCreditCoordinator.ResetCreditCoordinator;
14 const keys = yield* Ref.make<ReadonlyArray<string>>([]);
15 const attempts = yield* Ref.make(0);
16 const consume = (key: string) =>
17 Effect.gen(function* () {
18 yield* Ref.update(keys, (seen) => [...seen, key]);
19 const attempt = yield* Ref.updateAndGet(attempts, (n) => n + 1);
20 if (attempt === 1) return yield* Effect.fail("timed out" as const);
21 return "reset" as const;
22 });
23
24 const first = yield* redeem("acct", consume).pipe(Effect.result);
25 assert.isTrue(first._tag === "Failure");
26 const second = yield* redeem("acct", consume);
27 assert.strictEqual(second, "reset");
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(ResetCreditCoordinator.layerTest)),
35 );
36
37 it.effect("starts a fresh attempt after a settled failure", () =>
38 Effect.gen(function* () {
39 const { redeem } = yield* ResetCreditCoordinator.ResetCreditCoordinator;
40 const keys = yield* Ref.make<ReadonlyArray<string>>([]);
41 const consume = (key: string) =>
42 Ref.update(keys, (seen) => [...seen, key]).pipe(
43 Effect.andThen(Effect.fail("cooldown" as const)),
44 );
45 const isSettled = (error: "cooldown") => error === "cooldown";
46
47 yield* redeem("acct", consume, isSettled).pipe(Effect.result);
48 yield* redeem("acct", consume, isSettled).pipe(Effect.result);
49
50 const seen = yield* Ref.get(keys);
51 assert.strictEqual(seen.length, 2);
52 assert.notStrictEqual(seen[0], seen[1]);
53 }).pipe(Effect.provide(ResetCreditCoordinator.layerTest)),
54 );
55
56 it.effect("serialises concurrent redemptions on the same account, not per caller", () =>
57 Effect.gen(function* () {
58 const { redeem } = yield* ResetCreditCoordinator.ResetCreditCoordinator;
59 const release = yield* Deferred.make<void>();
60 const inFlight = yield* Ref.make(0);
61 const peak = yield* Ref.make(0);
62 const consume = () =>
63 Effect.gen(function* () {
64 const now = yield* Ref.updateAndGet(inFlight, (n) => n + 1);
65 yield* Ref.update(peak, (p) => Math.max(p, now));
66 yield* Deferred.await(release);
67 yield* Ref.update(inFlight, (n) => n - 1);
68 return "reset" as const;
69 });
70
71 const a = yield* redeem("acct", consume).pipe(Effect.forkChild);
72 const b = yield* redeem("acct", consume).pipe(Effect.forkChild);
73 yield* Effect.yieldNow;
74 yield* Deferred.succeed(release, undefined);
75 yield* Fiber.join(a);
76 yield* Fiber.join(b);
77
78 assert.strictEqual(yield* Ref.get(peak), 1);
79 }).pipe(Effect.provide(ResetCreditCoordinator.layerTest)),
80 );
81
82 it.effect("keeps different accounts independent", () =>
83 Effect.gen(function* () {
84 const { redeem } = yield* ResetCreditCoordinator.ResetCreditCoordinator;
85 const release = yield* Deferred.make<void>();
86 const peak = yield* Ref.make(0);
87 const inFlight = yield* Ref.make(0);
88 const consume = () =>
89 Effect.gen(function* () {
90 const now = yield* Ref.updateAndGet(inFlight, (n) => n + 1);
91 yield* Ref.update(peak, (p) => Math.max(p, now));
92 yield* Deferred.await(release);
93 return "reset" as const;
94 });
95 const a = yield* redeem("acct-a", consume).pipe(Effect.forkChild);
96 const b = yield* redeem("acct-b", consume).pipe(Effect.forkChild);
97 yield* Effect.yieldNow;
98 yield* Deferred.succeed(release, undefined);
99 yield* Fiber.join(a);
100 yield* Fiber.join(b);
101 assert.strictEqual(yield* Ref.get(peak), 2);
102 }).pipe(Effect.provide(ResetCreditCoordinator.layerTest)),
103 );
104
105 it.effect("re-probes after a reset until the limits are fresh, then gives up", () =>
106 Effect.gen(function* () {
107 const stale = { usageLimits: { checkedAt: "t0", windows: [] } };
108 const run = (freshFrom: number) =>
109 Effect.gen(function* () {
110 const probes = yield* Ref.make(0);
111 const refresh = Ref.updateAndGet(probes, (n) => n + 1).pipe(
112 Effect.map((n) =>
113 n >= freshFrom ? { usageLimits: { checkedAt: `t${n}`, windows: [] } } : stale,
114 ),
115 );
116 const fiber = yield* ResetCreditCoordinator.refreshAfterRedemption({
117 outcome: "reset",
118 getSnapshot: Effect.succeed(stale),
119 refresh,
120 }).pipe(Effect.forkChild);
121 yield* TestClock.adjust("1 minute");
122 return { confirmed: yield* Fiber.join(fiber), probes: yield* Ref.get(probes) };
123 });
124
125 assert.deepStrictEqual(yield* run(3), { confirmed: true, probes: 3 });
126 assert.deepStrictEqual(yield* run(Infinity), { confirmed: false, probes: 4 });
127 }),
128 );
129 });