Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { PullRequestOperationError } from "@t3tools/contracts";
4import * as Deferred from "effect/Deferred";
5import * as Effect from "effect/Effect";
6import * as Fiber from "effect/Fiber";
7import * as FileSystem from "effect/FileSystem";
8import * as KeyValueStore from "effect/persistence/KeyValueStore";
9import * as PullRequestReadCache from "./PullRequestReadCache.ts";
10
11const cacheLayer = (directory: string) =>
12 PullRequestReadCache.make.pipe(Effect.provide(KeyValueStore.layerFileSystem(directory)));
13
14it.layer(NodeServices.layer)("PR filesystem cache", (it) => {
15 it.effect("clears in-flight reads before a new service can reuse them", () =>
16 Effect.gen(function* () {
17 const fs = yield* FileSystem.FileSystem;
18 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pr-cache-" });
19 const started = yield* Deferred.make<void>();
20 const release = yield* Deferred.make<void>();
21 const cache = yield* cacheLayer(directory);
22 const read = yield* cache
23 .get(
24 "summary",
25 Deferred.succeed(started, undefined).pipe(
26 Effect.andThen(Deferred.await(release)),
27 Effect.as("old"),
28 ),
29 ["pr"],
30 )
31 .pipe(Effect.forkChild);
32 yield* Deferred.await(started);
33 const invalidate = yield* cache
34 .invalidate("pr")
35 .pipe(Effect.forkChild({ startImmediately: true }));
36 yield* Deferred.succeed(release, undefined);
37 yield* Fiber.join(read);
38 yield* Fiber.join(invalidate);
39 const restarted = yield* cacheLayer(directory);
40 assert.strictEqual(yield* restarted.get("summary", Effect.succeed("new"), ["pr"]), "new");
41 }),
42 );
43
44 it.effect("invalidates only the changed scope across restarts and coalesces its next reads", () =>
45 Effect.gen(function* () {
46 const fs = yield* FileSystem.FileSystem;
47 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pr-cache-" });
48 let reads = 0;
49 const lookup = Effect.sync(() => String(++reads));
50 const cache = yield* cacheLayer(directory);
51 yield* cache.get("first", lookup, ["project", "pr-1"]);
52 yield* cache.get("second", lookup, ["project", "pr-2"]);
53 yield* cache.get("third", lookup, ["other-project", "pr-3"]);
54 yield* cache.invalidate("pr-1");
55 const restarted = yield* cacheLayer(directory);
56 const answers = yield* Effect.all(
57 Array.from({ length: 10 }, () => restarted.get("first", lookup, ["project", "pr-1"])),
58 { concurrency: 10 },
59 );
60 assert.deepStrictEqual(answers, Array(10).fill("4"));
61 assert.strictEqual(yield* restarted.get("second", lookup, ["project", "pr-2"]), "2");
62 yield* restarted.invalidate("project");
63 const again = yield* cacheLayer(directory);
64 assert.strictEqual(yield* again.get("second", lookup, ["project", "pr-2"]), "5");
65 assert.strictEqual(yield* again.get("third", lookup, ["other-project", "pr-3"]), "3");
66 assert.strictEqual(reads, 5);
67 const files = (yield* fs.readDirectory(directory)).length;
68 for (let index = 0; index < 3; index++) {
69 yield* again.invalidate("pr-1");
70 yield* again.get("first", lookup, ["project", "pr-1"]);
71 }
72 assert.strictEqual((yield* fs.readDirectory(directory)).length, files);
73 }),
74 );
75
76 it.effect("shares a pending refresh without blocking an unrelated cached PR", () =>
77 Effect.gen(function* () {
78 const cache = yield* PullRequestReadCache.make;
79 yield* cache.get("first", Effect.succeed("old"), ["pr-1"]);
80 yield* cache.get("second", Effect.succeed("warm"), ["pr-2"]);
81 yield* cache.invalidate("pr-1");
82 const started = yield* Deferred.make<void>();
83 const release = yield* Deferred.make<void>();
84 let reads = 0;
85 const refresh = cache.get(
86 "first",
87 Effect.gen(function* () {
88 reads++;
89 yield* Deferred.succeed(started, undefined);
90 yield* Deferred.await(release);
91 return "fresh";
92 }),
93 ["pr-1"],
94 );
95 const pending = yield* Effect.all(
96 Array.from({ length: 10 }, () => refresh),
97 {
98 concurrency: 10,
99 },
100 ).pipe(Effect.forkChild);
101 yield* Deferred.await(started);
102 assert.strictEqual(yield* cache.get("second", Effect.die("cache miss"), ["pr-2"]), "warm");
103 yield* Deferred.succeed(release, undefined);
104 assert.deepStrictEqual(yield* Fiber.join(pending), Array(10).fill("fresh"));
105 assert.strictEqual(reads, 1);
106 }).pipe(Effect.provide(KeyValueStore.layerMemory)),
107 );
108
109 it.effect("does not persist failed GitHub reads", () =>
110 Effect.gen(function* () {
111 const fs = yield* FileSystem.FileSystem;
112 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pr-cache-" });
113 const cache = yield* cacheLayer(directory);
114 const error = new PullRequestOperationError({ operation: "summary", detail: "unavailable" });
115 yield* cache.get("summary", Effect.fail(error)).pipe(Effect.flip);
116 const restarted = yield* cacheLayer(directory);
117 assert.strictEqual(yield* restarted.get("summary", Effect.succeed("recovered")), "recovered");
118 }),
119 );
120
121 it.effect("resumes caching after a failed scope read", () =>
122 Effect.gen(function* () {
123 const backing = yield* KeyValueStore.KeyValueStore;
124 let fail = true;
125 let reads = 0;
126 const cache = yield* PullRequestReadCache.make.pipe(
127 Effect.provideService(KeyValueStore.KeyValueStore, {
128 ...backing,
129 get: (key) =>
130 Effect.suspend(() => {
131 if (!fail) return backing.get(key);
132 fail = false;
133 return Effect.fail(
134 new KeyValueStore.KeyValueStoreError({ method: "get", message: "unavailable" }),
135 );
136 }),
137 }),
138 );
139 const read = cache.get(
140 "summary",
141 Effect.sync(() => String(++reads)),
142 ["pr"],
143 );
144 assert.strictEqual(yield* read, "1");
145 assert.strictEqual(yield* read, "2");
146 assert.strictEqual(yield* read, "2");
147 }).pipe(Effect.provide(KeyValueStore.layerMemory)),
148 );
149
150 it.effect("cancels abandoned reads without blocking invalidation", () =>
151 Effect.gen(function* () {
152 const cache = yield* PullRequestReadCache.make;
153 const started = yield* Deferred.make<void>();
154 const read = yield* cache
155 .get("summary", Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)), [
156 "pr",
157 ])
158 .pipe(Effect.forkChild);
159 yield* Deferred.await(started);
160 yield* Fiber.interrupt(read);
161 yield* cache.invalidate("pr");
162 assert.strictEqual(yield* cache.get("summary", Effect.succeed("fresh"), ["pr"]), "fresh");
163 }).pipe(Effect.provide(KeyValueStore.layerMemory)),
164 );
165
166 it.effect(
167 "finishes the in-memory revision update when invalidation is canceled after writing",
168 () =>
169 Effect.gen(function* () {
170 const backing = yield* KeyValueStore.KeyValueStore;
171 const written = yield* Deferred.make<void>();
172 const release = yield* Deferred.make<void>();
173 const cache = yield* PullRequestReadCache.make.pipe(
174 Effect.provideService(KeyValueStore.KeyValueStore, {
175 ...backing,
176 set: (key, value) =>
177 backing
178 .set(key, value)
179 .pipe(
180 Effect.andThen(
181 key === "revisions"
182 ? Deferred.succeed(written, undefined).pipe(
183 Effect.andThen(Deferred.await(release)),
184 )
185 : Effect.void,
186 ),
187 ),
188 }),
189 );
190 yield* cache.get("summary", Effect.succeed("old"), ["pr"]);
191 const invalidation = yield* cache.invalidate("pr").pipe(Effect.forkChild);
192 yield* Deferred.await(written);
193 const interrupt = yield* Fiber.interrupt(invalidation).pipe(
194 Effect.forkChild({ startImmediately: true }),
195 );
196 yield* Deferred.succeed(release, undefined);
197 yield* Fiber.join(interrupt);
198 assert.strictEqual(yield* cache.get("summary", Effect.succeed("fresh"), ["pr"]), "fresh");
199 }).pipe(Effect.provide(KeyValueStore.layerMemory)),
200 );
201});