1// @effect-diagnostics nodeBuiltinImport:off - the test kills a real Node process to prove the cache is on disk.
2import * as NodeChildProcess from "node:child_process";
3import * as NodeFSP from "node:fs/promises";
4import * as NodeOS from "node:os";
5import * as NodePath from "node:path";
6import { assert, it } from "@effect/vitest";
7
8it.each([false, true])(
9 "persists an enabled cache before forced exit (disabled: %s)",
10 async (disabled) => {
11 const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-compile-cache-"));
12 try {
13 const cacheDirectory = NodePath.join(directory, "cache");
14 const child = NodeChildProcess.spawnSync(
15 process.execPath,
16 [
17 "--input-type=module",
18 "--eval",
19 `import * as Effect from ${JSON.stringify(import.meta.resolve("effect/Effect"))};
20const { flushCompileCache } = await import(${JSON.stringify(new URL("./compileCache.ts", import.meta.url).href)});
21await Effect.runPromise(flushCompileCache);
22process.kill(process.pid, "SIGKILL");`,
23 ],
24 {
25 encoding: "utf8",
26 env: {
27 ...process.env,
28 NODE_COMPILE_CACHE: cacheDirectory,
29 NODE_DISABLE_COMPILE_CACHE: disabled ? "1" : undefined,
30 },
31 },
32 );
33 assert.equal(child.error, undefined);
34 assert.equal(child.stderr, "");
35 assert.notEqual(child.status, 0);
36 const entries = await NodeFSP.readdir(cacheDirectory, { recursive: true }).catch(
37 (error: NodeJS.ErrnoException) => {
38 if (error.code === "ENOENT") return [];
39 throw error;
40 },
41 );
42 const files = await Promise.all(
43 entries.map((entry) => NodeFSP.stat(NodePath.join(cacheDirectory, entry))),
44 );
45 assert.equal(
46 files.some((entry) => entry.isFile()),
47 !disabled,
48 );
49 } finally {
50 await NodeFSP.rm(directory, { recursive: true, force: true });
51 }
52 },
53);