@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, 06:00 UTC

main
ts 168 lines 5.81 KB
Raw
1 import { describe, expect, it } from "@alchemy.run/test";
2 import * as Context from "effect/Context";
3 import * as Effect from "effect/Effect";
4 import * as Exit from "effect/Exit";
5 import * as Fiber from "effect/Fiber";
6 import * as Layer from "effect/Layer";
7 import * as Result from "effect/Result";
8 import * as Scope from "effect/Scope";
9 import * as TestClock from "effect/testing/TestClock";
10 import { makeExecutionMemo } from "@/Runtime/ExecutionMemo";
11
12 interface Pool {
13 readonly id: number;
14 }
15
16 describe("makeExecutionMemo", { tags: ["unit", "local"] }, () => {
17 it.effect("builds at most once per execution scope and releases at scope close", () =>
18 Effect.gen(function* () {
19 let builds = 0;
20 const released: number[] = [];
21 const accessor = yield* makeExecutionMemo(
22 Effect.acquireRelease(
23 Effect.sync((): Pool => ({ id: ++builds })),
24 (pool) =>
25 Effect.sync(() => {
26 released.push(pool.id);
27 }),
28 ),
29 );
30
31 // Two accesses inside the same execution scope share one build.
32 const scopeA = yield* Scope.make();
33 const a1 = yield* accessor.pipe(Scope.provide(scopeA));
34 const a2 = yield* accessor.pipe(Scope.provide(scopeA));
35 expect(builds).toBe(1);
36 expect(a2).toBe(a1);
37 expect(released).toEqual([]);
38
39 // A different execution scope builds its own instance.
40 const scopeB = yield* Scope.make();
41 const b1 = yield* accessor.pipe(Scope.provide(scopeB));
42 expect(builds).toBe(2);
43 expect(b1).not.toBe(a1);
44
45 // Release finalizers fire when — and only when — the owning
46 // execution scope closes.
47 yield* Scope.close(scopeA, Exit.void);
48 expect(released).toEqual([1]);
49 yield* Scope.close(scopeB, Exit.void);
50 expect(released).toEqual([1, 2]);
51
52 // A fresh access on a fresh scope rebuilds (nothing is cached
53 // across executions).
54 const scopeC = yield* Scope.make();
55 yield* accessor.pipe(Scope.provide(scopeC));
56 expect(builds).toBe(3);
57 yield* Scope.close(scopeC, Exit.void);
58 expect(released).toEqual([1, 2, 3]);
59 }),
60 );
61
62 it.effect("concurrent first accesses join a single build", () =>
63 Effect.gen(function* () {
64 let builds = 0;
65 const accessor = yield* makeExecutionMemo(
66 Effect.acquireRelease(
67 // Asynchronous acquire so the second fiber arrives while the
68 // first build is still in flight.
69 Effect.sleep("10 millis").pipe(Effect.map((): Pool => ({ id: ++builds }))),
70 () => Effect.void,
71 ),
72 );
73
74 const scope = yield* Scope.make();
75 const fiber = yield* Effect.forkChild(
76 Effect.all([accessor, accessor], { concurrency: 2 }).pipe(Scope.provide(scope)),
77 );
78 // `it.effect` runs under a TestClock — advance it so the in-flight
79 // build's sleep elapses while both fibers are already waiting on it.
80 yield* TestClock.adjust("10 millis");
81 const [x, y] = yield* Fiber.join(fiber);
82 expect(builds).toBe(1);
83 expect(y).toBe(x);
84 yield* Scope.close(scope, Exit.void);
85 }),
86 );
87
88 it.effect("distinct call sites get distinct memo cells", () =>
89 Effect.gen(function* () {
90 let builds = 0;
91 const build = Effect.sync(() => ++builds);
92 const accessorA = yield* makeExecutionMemo(build);
93 const accessorB = yield* makeExecutionMemo(build);
94
95 const scope = yield* Scope.make();
96 const a = yield* accessorA.pipe(Scope.provide(scope));
97 const b = yield* accessorB.pipe(Scope.provide(scope));
98 expect(a).toBe(1);
99 expect(b).toBe(2);
100 // ...but each cell still memoizes within the scope.
101 expect(yield* accessorA.pipe(Scope.provide(scope))).toBe(1);
102 expect(yield* accessorB.pipe(Scope.provide(scope))).toBe(2);
103 yield* Scope.close(scope, Exit.void);
104 }),
105 );
106
107 it.effect("Layer.build inside the memo ties layer finalizers to the execution scope", () =>
108 Effect.gen(function* () {
109 class Db extends Context.Service<Db, { readonly id: number }>()("ExecutionMemoTest/Db") {}
110
111 let builds = 0;
112 let releases = 0;
113 const dbLayer = Layer.effect(
114 Db,
115 Effect.acquireRelease(
116 Effect.sync(() => ({ id: ++builds })),
117 () =>
118 Effect.sync(() => {
119 releases++;
120 }),
121 ),
122 );
123
124 // The Drizzle.Postgres shape: build a Layer against the ambient
125 // execution scope so its release fires when the event settles.
126 const accessor = yield* makeExecutionMemo(
127 Effect.gen(function* () {
128 const ctx = yield* Layer.build(dbLayer);
129 return Context.get(ctx, Db);
130 }),
131 );
132
133 const scope = yield* Scope.make();
134 const d1 = yield* accessor.pipe(Scope.provide(scope));
135 const d2 = yield* accessor.pipe(Scope.provide(scope));
136 expect(builds).toBe(1);
137 expect(d2).toBe(d1);
138 expect(releases).toBe(0);
139
140 yield* Scope.close(scope, Exit.void);
141 expect(releases).toBe(1);
142 }),
143 );
144
145 it.effect("build errors are surfaced and not poisoned across scopes", () =>
146 Effect.gen(function* () {
147 let attempts = 0;
148 const accessor = yield* makeExecutionMemo(
149 Effect.suspend(() => {
150 attempts++;
151 return attempts === 1 ? Effect.fail("boom" as const) : Effect.succeed(attempts);
152 }),
153 );
154
155 const scopeA = yield* Scope.make();
156 const first = yield* accessor.pipe(Scope.provide(scopeA), Effect.result);
157 expect(Result.isFailure(first)).toBe(true);
158 yield* Scope.close(scopeA, Exit.void);
159
160 // A new execution scope re-runs the build rather than replaying the
161 // stale failure forever.
162 const scopeB = yield* Scope.make();
163 const second = yield* accessor.pipe(Scope.provide(scopeB));
164 expect(second).toBe(2);
165 yield* Scope.close(scopeB, Exit.void);
166 }),
167 );
168 });