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 Layer from "effect/Layer";
9import * as TestClock from "effect/testing/TestClock";
10import * as KeyValueStore from "effect/unstable/persistence/KeyValueStore";
11import * as Persistence from "effect/unstable/persistence/Persistence";
12import * as PullRequestReadCache from "./PullRequestReadCache.ts";
13
14const cacheLayer = (directory: string) =>
15 PullRequestReadCache.make.pipe(
16 Effect.provide(
17 Persistence.layerKvs.pipe(Layer.provideMerge(KeyValueStore.layerFileSystem(directory))),
18 ),
19 );
20
21it.layer(NodeServices.layer)("PR filesystem cache", (it) => {
22 it.effect("reuses files after restart and respects the original expiry", () =>
23 Effect.gen(function* () {
24 const fs = yield* FileSystem.FileSystem;
25 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pr-cache-" });
26 let reads = 0;
27 const lookup = Effect.sync(() => String(++reads));
28 const first = yield* cacheLayer(directory);
29 const key = "long/repository/key".repeat(100);
30 assert.strictEqual(yield* first.get(key, lookup), "1");
31 yield* TestClock.adjust("59 seconds");
32 const restarted = yield* cacheLayer(directory);
33 assert.strictEqual(yield* restarted.get(key, lookup), "1");
34 yield* TestClock.adjust("1 second");
35 assert.strictEqual(yield* restarted.get(key, lookup), "2");
36 assert.strictEqual(reads, 2);
37 assert.strictEqual((yield* fs.readDirectory(directory)).length, 1);
38 }),
39 );
40
41 it.effect("clears in-flight reads before a new service can reuse them", () =>
42 Effect.gen(function* () {
43 const fs = yield* FileSystem.FileSystem;
44 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pr-cache-" });
45 const started = yield* Deferred.make<void>();
46 const release = yield* Deferred.make<void>();
47 const cache = yield* cacheLayer(directory);
48 const read = yield* cache
49 .get(
50 "summary",
51 Deferred.succeed(started, undefined).pipe(
52 Effect.andThen(Deferred.await(release)),
53 Effect.as("old"),
54 ),
55 )
56 .pipe(Effect.forkChild);
57 yield* Deferred.await(started);
58 const invalidate = yield* cache.invalidate.pipe(Effect.forkChild({ startImmediately: true }));
59 yield* Deferred.succeed(release, undefined);
60 yield* Fiber.join(read);
61 yield* Fiber.join(invalidate);
62 const restarted = yield* cacheLayer(directory);
63 assert.strictEqual(yield* restarted.get("summary", Effect.succeed("new")), "new");
64 }),
65 );
66
67 it.effect("does not persist failed GitHub reads", () =>
68 Effect.gen(function* () {
69 const fs = yield* FileSystem.FileSystem;
70 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pr-cache-" });
71 const cache = yield* cacheLayer(directory);
72 const error = new PullRequestOperationError({ operation: "summary", detail: "unavailable" });
73 yield* cache.get("summary", Effect.fail(error)).pipe(Effect.flip);
74 const restarted = yield* cacheLayer(directory);
75 assert.strictEqual(yield* restarted.get("summary", Effect.succeed("recovered")), "recovered");
76 }),
77 );
78});