The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off - verifies native task caching in isolated subprocesses. |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | import * as NodeChildProcess from "node:child_process"; |
| 4 | import * as NodeFSP from "node:fs/promises"; |
| 5 | import * as NodeOS from "node:os"; |
| 6 | import * as NodePath from "node:path"; |
| 7 | import * as NodeURL from "node:url"; |
| 8 | |
| 9 | import { cachedTestTasks, matchesTestPath, readRunSummary } from "./cached-tests.ts"; |
| 10 | |
| 11 | describe("cachedTestTasks", () => { |
| 12 | it("selects every test for a checkout-root path", () => { |
| 13 | const root = NodePath.resolve(import.meta.dirname, "../.."); |
| 14 | for (const path of [".", root]) { |
| 15 | expect( |
| 16 | matchesTestPath("apps/server/src/auth/utils.test.ts", [ |
| 17 | NodePath.relative(root, NodePath.resolve(root, path)), |
| 18 | ]), |
| 19 | ).toBe(true); |
| 20 | } |
| 21 | expect(matchesTestPath("apps/server/src/auth/utils.test.ts", ["apps/server"])).toBe(true); |
| 22 | expect(matchesTestPath("apps/server-other/a.test.ts", ["apps/server"])).toBe(false); |
| 23 | }); |
| 24 | it("keeps a file's cache task stable when unrelated tests are added", () => { |
| 25 | const first = cachedTestTasks( |
| 26 | JSON.stringify({ run: "r", files: ["src/b.test.ts"], groupSize: 1 }), |
| 27 | ); |
| 28 | const second = cachedTestTasks( |
| 29 | JSON.stringify({ run: "r", files: ["src/a.test.ts", "src/b.test.ts"], groupSize: 1 }), |
| 30 | ); |
| 31 | const key = Object.keys(first).find((key) => key !== "test-cache")!; |
| 32 | expect(second[key]).toEqual(first[key]); |
| 33 | }); |
| 34 | |
| 35 | it("keeps shell metacharacters in filenames out of the command", () => { |
| 36 | const tasks = cachedTestTasks( |
| 37 | JSON.stringify({ run: "r", files: ["src/a'$(touch injected).test.ts"], groupSize: 1 }), |
| 38 | ); |
| 39 | const task = Object.entries(tasks).find(([key]) => key !== "test-cache")![1]; |
| 40 | expect(task.command).toBe("vp test run 'src/a'\"'\"'$(touch injected).test.ts'"); |
| 41 | }); |
| 42 | |
| 43 | it("rejects paths outside the checkout, empty groups, and unsafe run ids", () => { |
| 44 | for (const request of [ |
| 45 | { run: "r", files: ["../elsewhere.test.ts"], groupSize: 1 }, |
| 46 | { run: "r", files: ["/absolute.test.ts"], groupSize: 1 }, |
| 47 | { run: "r", files: ["src/a.test.ts"], groupSize: 0 }, |
| 48 | { files: ["src/a.test.ts"], groupSize: 1 }, |
| 49 | { run: "r; touch injected", files: ["src/a.test.ts"], groupSize: 1 }, |
| 50 | ]) { |
| 51 | expect(() => cachedTestTasks(JSON.stringify(request))).toThrow(); |
| 52 | } |
| 53 | }); |
| 54 | |
| 55 | it("accepts spec files discovered by Vitest", () => { |
| 56 | const tasks = cachedTestTasks( |
| 57 | JSON.stringify({ run: "r", files: ["src/example.spec.ts"], groupSize: 1 }), |
| 58 | ); |
| 59 | expect( |
| 60 | Object.values(tasks).some((task) => task.command === "vp test run 'src/example.spec.ts'"), |
| 61 | ).toBe(true); |
| 62 | }); |
| 63 | |
| 64 | it("does not add tasks without an explicit request", () => { |
| 65 | expect(cachedTestTasks(undefined)).toEqual({}); |
| 66 | }); |
| 67 | |
| 68 | it("reads only the task summary of the given run", async () => { |
| 69 | const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "cached-tests-")); |
| 70 | const write = async (version: string, run: string) => { |
| 71 | const file = NodePath.join(directory, version, "last-summary.json"); |
| 72 | await NodeFSP.mkdir(NodePath.dirname(file)); |
| 73 | const tasks = cachedTestTasks(JSON.stringify({ run, files: ["a.test.ts"], groupSize: 1 })); |
| 74 | await NodeFSP.writeFile( |
| 75 | file, |
| 76 | JSON.stringify({ |
| 77 | tasks: Object.entries(tasks).map(([task_name, task]) => ({ |
| 78 | task_name, |
| 79 | command: task.command, |
| 80 | result: {}, |
| 81 | })), |
| 82 | }), |
| 83 | ); |
| 84 | }; |
| 85 | try { |
| 86 | expect(readRunSummary(NodePath.join(directory, "missing"), "own")).toBeUndefined(); |
| 87 | await NodeFSP.writeFile(NodePath.join(directory, "file"), ""); |
| 88 | expect(readRunSummary(NodePath.join(directory, "file"), "own")).toBeUndefined(); |
| 89 | await write("v18", "other"); |
| 90 | expect(readRunSummary(directory, "own")).toBeUndefined(); |
| 91 | await write("v19", "own"); |
| 92 | await NodeFSP.mkdir(NodePath.join(directory, "corrupt")); |
| 93 | await NodeFSP.writeFile(NodePath.join(directory, "corrupt", "last-summary.json"), "{"); |
| 94 | expect(readRunSummary(directory, "own")?.tasks).toHaveLength(3); |
| 95 | } finally { |
| 96 | await NodeFSP.rm(directory, { recursive: true, force: true }); |
| 97 | } |
| 98 | }); |
| 99 | |
| 100 | it("reuses individual results, invalidates filesystem inputs, and never caches failures", async () => { |
| 101 | const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "cached-tests-")); |
| 102 | const root = NodePath.resolve(import.meta.dirname, "../.."); |
| 103 | let runId = 0; |
| 104 | const run = (files = ["a.test.ts", "b.test.ts"], uncached: Array<string> = []) => |
| 105 | new Promise<number | null>((resolve, reject) => { |
| 106 | runId += 1; |
| 107 | const child = NodeChildProcess.spawn( |
| 108 | process.execPath, |
| 109 | [NodePath.join(root, "node_modules/vite-plus/dist/bin.js"), "run", "test-cache"], |
| 110 | { |
| 111 | cwd: directory, |
| 112 | env: { |
| 113 | ...process.env, |
| 114 | NODE_OPTIONS: "", |
| 115 | VP_REMOTE_CACHE: "off", |
| 116 | T3_CACHED_TESTS: JSON.stringify({ |
| 117 | run: String(runId), |
| 118 | files, |
| 119 | groupSize: 1, |
| 120 | uncached, |
| 121 | }), |
| 122 | VITE_CACHE_PATH: NodePath.join(directory, "cache"), |
| 123 | PATH: `${NodePath.join(root, "node_modules/.bin")}${NodePath.delimiter}${process.env.PATH}`, |
| 124 | }, |
| 125 | stdio: "ignore", |
| 126 | }, |
| 127 | ); |
| 128 | child.once("error", reject); |
| 129 | child.once("close", resolve); |
| 130 | }); |
| 131 | const summary = async () => |
| 132 | readRunSummary(NodePath.join(directory, "cache"), String(runId))!.tasks.filter( |
| 133 | (task) => task.result.CacheHit, |
| 134 | ).length; |
| 135 | try { |
| 136 | await NodeFSP.mkdir(NodePath.join(directory, "node_modules")); |
| 137 | await NodeFSP.symlink( |
| 138 | NodePath.join(root, "node_modules/vite-plus"), |
| 139 | NodePath.join(directory, "node_modules/vite-plus"), |
| 140 | "dir", |
| 141 | ); |
| 142 | await NodeFSP.writeFile(NodePath.join(directory, "package.json"), '{"type":"module"}'); |
| 143 | await NodeFSP.writeFile( |
| 144 | NodePath.join(directory, "vite.config.ts"), |
| 145 | `import {cachedTestTasks} from ${JSON.stringify(NodeURL.pathToFileURL(NodePath.join(root, "scripts/lib/cached-tests.ts")).href)}; export default {run:{tasks:cachedTestTasks(process.env.T3_CACHED_TESTS, import.meta.dirname)},test:{include:["*.test.ts"]}};`, |
| 146 | ); |
| 147 | await NodeFSP.writeFile( |
| 148 | NodePath.join(directory, "a.test.ts"), |
| 149 | 'import {test,expect} from "vite-plus/test"; import {readFileSync} from "node:fs"; test("fixture",()=>expect(readFileSync("fixture.txt","utf8")).toMatch(/^hello/));', |
| 150 | ); |
| 151 | await NodeFSP.writeFile( |
| 152 | NodePath.join(directory, "b.test.ts"), |
| 153 | 'import {test,expect} from "vite-plus/test"; test("independent",()=>expect(1).toBe(1));', |
| 154 | ); |
| 155 | await NodeFSP.writeFile(NodePath.join(directory, "fixture.txt"), "hello"); |
| 156 | expect(await run()).toBe(0); |
| 157 | expect(await run()).toBe(0); |
| 158 | expect(await summary()).toBe(2); |
| 159 | expect(await run(["b.test.ts"])).toBe(0); |
| 160 | expect(await summary()).toBe(1); |
| 161 | expect(await run()).toBe(0); |
| 162 | expect(await summary()).toBe(2); |
| 163 | await NodeFSP.writeFile(NodePath.join(directory, "fixture.txt"), "hello changed"); |
| 164 | expect(await run()).toBe(0); |
| 165 | expect(await summary()).toBe(1); |
| 166 | await NodeFSP.writeFile(NodePath.join(directory, "fixture.txt"), "broken"); |
| 167 | expect(await run()).toBe(1); |
| 168 | expect(await run()).toBe(1); |
| 169 | expect(await summary()).toBe(1); |
| 170 | await NodeFSP.writeFile(NodePath.join(directory, "fixture.txt"), "hello again"); |
| 171 | await NodeFSP.writeFile( |
| 172 | NodePath.join(directory, "b.test.ts"), |
| 173 | 'import {test} from "vite-plus/test";import {writeFileSync} from "node:fs";test("report",()=>writeFileSync("report.json","{}"));', |
| 174 | ); |
| 175 | expect(await run(undefined, ["b.test.ts"])).toBe(0); |
| 176 | await NodeFSP.unlink(NodePath.join(directory, "report.json")); |
| 177 | expect(await run(undefined, ["b.test.ts"])).toBe(0); |
| 178 | expect(await NodeFSP.readFile(NodePath.join(directory, "report.json"), "utf8")).toBe("{}"); |
| 179 | } finally { |
| 180 | await NodeFSP.rm(directory, { recursive: true, force: true }); |
| 181 | } |
| 182 | }); |
| 183 | }); |