Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

cliCompileCache.test.ts

69 lines3.2 KB

1// @effect-diagnostics nodeBuiltinImport:off -- Resolves the real CLI entrypoint for subprocess tests.
2import * as NodeURL from "node:url";
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import { expect, it } from "@effect/vitest";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Stream from "effect/Stream";
8import { ChildProcess, ChildProcessSpawner } from "effect/process";
9​
10it.live("enables CLI caching while respecting overrides and unavailable storage", () =>
11 Effect.gen(function* () {
12 const fs = yield* FileSystem.FileSystem;
13 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
14 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-cache-test-" });
15 const report = `import { getCompileCacheDir, registerHooks } from 'node:module'; registerHooks({ resolve(specifier, context, nextResolve) { if (specifier === './binCli.ts') console.error('CACHE=' + (getCompileCacheDir() ?? 'disabled')); return nextResolve(specifier, context); } });`;
16 const run = Effect.fn("cliCompileCache.test.run")(function* (env: Record<string, string>) {
17 const child = yield* spawner.spawn(
18 ChildProcess.make(
19 process.execPath,
20 [
21 "--experimental-strip-types",
22 "--import",
23 `data:text/javascript,${encodeURIComponent(report)}`,
24 NodeURL.fileURLToPath(new URL("./bin.ts", import.meta.url)),
25 "--help",
26 ],
27 {
28 env: {
29 XDG_CACHE_HOME: `${root}/cache`,
30 T3CODE_HOME: `${root}/home`,
31 NODE_COMPILE_CACHE: undefined,
32 NODE_DISABLE_COMPILE_CACHE: undefined,
33 APPIMAGE: "",
34 ...env,
35 },
36 },
37 ),
38 );
39 return yield* Effect.all(
40 {
41 stdout: child.stdout.pipe(Stream.decodeText(), Stream.mkString),
42 stderr: child.stderr.pipe(Stream.decodeText(), Stream.mkString),
43 exitCode: child.exitCode.pipe(Effect.map(Number)),
44 },
45 { concurrency: "unbounded" },
46 );
47 }, Effect.scoped);
48 const normal = yield* run({});
49 expect(normal.exitCode).toBe(0);
50 expect(normal.stderr.replaceAll("\\", "/")).toContain(
51 `CACHE=${root}/cache/t3code/compile-cache`.replaceAll("\\", "/"),
52 );
53 const appImage = yield* run({ APPIMAGE: `${root}/T3Code.AppImage` });
54 expect(appImage.exitCode).toBe(0);
55 expect(appImage.stderr).toContain("CACHE=disabled");
56 const existing = yield* run({ NODE_COMPILE_CACHE: `${root}/explicit` });
57 expect(existing.exitCode).toBe(0);
58 expect(existing.stderr.replaceAll("\\", "/")).toContain(
59 `${root}/explicit`.replaceAll("\\", "/"),
60 );
61 const disabled = yield* run({ NODE_DISABLE_COMPILE_CACHE: "1" });
62 expect(disabled.exitCode).toBe(0);
63 expect(disabled.stderr).toContain("CACHE=disabled");
64 yield* fs.writeFileString(`${root}/blocked`, "not a directory");
65 const unavailable = yield* run({ XDG_CACHE_HOME: `${root}/blocked` });
66 expect(unavailable.exitCode).toBe(0);
67 expect(unavailable.stderr).toContain("CACHE=disabled");
68 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
69);