The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off - Vite loads this before dependency installation and service initialization. |
| 2 | import * as NodeCrypto from "node:crypto"; |
| 3 | import * as NodeFS from "node:fs"; |
| 4 | import * as NodePath from "node:path"; |
| 5 | |
| 6 | interface CachedTestTask { |
| 7 | command: string; |
| 8 | cwd?: string; |
| 9 | dependsOn?: Array<string>; |
| 10 | cache: |
| 11 | | false |
| 12 | | { |
| 13 | input: Array<string | { auto: true }>; |
| 14 | output: Array<string>; |
| 15 | }; |
| 16 | } |
| 17 | |
| 18 | interface TaskRunSummary { |
| 19 | tasks: Array<{ |
| 20 | task_name: string; |
| 21 | command: string; |
| 22 | result: { CacheHit?: { source: string; saved_duration_ms: number } }; |
| 23 | }>; |
| 24 | } |
| 25 | |
| 26 | // vp keeps one last-summary.json per cache, rewritten by every `vp run`, so a run is |
| 27 | // recognized by an uncached task that every test depends on and that carries its id. |
| 28 | const runTask = "test-cache:run"; |
| 29 | const runCommand = (run: string) => `node -e "" ${run}`; |
| 30 | |
| 31 | export function readRunSummary(cacheRoot: string, run: string): TaskRunSummary | undefined { |
| 32 | try { |
| 33 | for (const entry of NodeFS.readdirSync(cacheRoot)) { |
| 34 | try { |
| 35 | const summary: TaskRunSummary = JSON.parse( |
| 36 | NodeFS.readFileSync(NodePath.join(cacheRoot, entry, "last-summary.json"), "utf8"), |
| 37 | ); |
| 38 | if (summary.tasks.some((task) => task.command === runCommand(run))) return summary; |
| 39 | } catch { |
| 40 | continue; |
| 41 | } |
| 42 | } |
| 43 | } catch { |
| 44 | return undefined; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | export function matchesTestPath(file: string, filters: Array<string>) { |
| 49 | return ( |
| 50 | filters.length === 0 || |
| 51 | filters.some((filter) => filter === "" || file === filter || file.startsWith(`${filter}/`)) |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | export function cachedTestTasks( |
| 56 | raw: string | undefined, |
| 57 | root = NodePath.resolve(import.meta.dirname, "../.."), |
| 58 | ): Record<string, CachedTestTask> { |
| 59 | if (!raw) return {}; |
| 60 | const request: unknown = JSON.parse(raw); |
| 61 | if ( |
| 62 | typeof request !== "object" || |
| 63 | request === null || |
| 64 | !("files" in request) || |
| 65 | !Array.isArray(request.files) || |
| 66 | !("groupSize" in request) || |
| 67 | typeof request.groupSize !== "number" || |
| 68 | !Number.isSafeInteger(request.groupSize) || |
| 69 | request.groupSize < 1 || |
| 70 | !("run" in request) || |
| 71 | typeof request.run !== "string" || |
| 72 | !/^[\w-]+$/.test(request.run) |
| 73 | ) { |
| 74 | throw new Error("Invalid cached test request"); |
| 75 | } |
| 76 | const uncached = "uncached" in request ? request.uncached : []; |
| 77 | if (!Array.isArray(uncached) || !uncached.every((file) => typeof file === "string")) { |
| 78 | throw new Error("Invalid uncached test files"); |
| 79 | } |
| 80 | const files: Array<string> = []; |
| 81 | for (const file of request.files) { |
| 82 | if ( |
| 83 | typeof file !== "string" || |
| 84 | NodePath.isAbsolute(file) || |
| 85 | file.split(/[\\/]/).includes("..") || |
| 86 | !/\.(?:test|spec)\.[cm]?[jt]sx?$/.test(file) |
| 87 | ) { |
| 88 | throw new Error("Cached tests must be test files inside the checkout"); |
| 89 | } |
| 90 | files.push(file); |
| 91 | } |
| 92 | const tasks: Record<string, CachedTestTask> = {}; |
| 93 | const sorted = [...new Set(files)].sort(); |
| 94 | const packages = new Map<string, Array<string>>(); |
| 95 | for (const file of sorted) { |
| 96 | let directory = NodePath.dirname(NodePath.join(root, file)); |
| 97 | while (directory !== root && !NodeFS.existsSync(NodePath.join(directory, "package.json"))) { |
| 98 | directory = NodePath.dirname(directory); |
| 99 | } |
| 100 | const entries = packages.get(directory) ?? []; |
| 101 | entries.push(file); |
| 102 | packages.set(directory, entries); |
| 103 | } |
| 104 | for (const [directory, entries] of packages) { |
| 105 | const manifest: unknown = JSON.parse( |
| 106 | NodeFS.readFileSync(NodePath.join(directory, "package.json"), "utf8"), |
| 107 | ); |
| 108 | const command = |
| 109 | directory !== root && |
| 110 | typeof manifest === "object" && |
| 111 | manifest !== null && |
| 112 | "scripts" in manifest && |
| 113 | typeof manifest.scripts === "object" && |
| 114 | manifest.scripts !== null && |
| 115 | "test" in manifest.scripts && |
| 116 | typeof manifest.scripts.test === "string" |
| 117 | ? manifest.scripts.test |
| 118 | : "vp test run"; |
| 119 | if (!command.startsWith("vp test run")) { |
| 120 | throw new Error(`Unsupported test command in ${directory}`); |
| 121 | } |
| 122 | const cacheable = entries.filter((file) => !uncached.includes(file)); |
| 123 | const groups: Array<Array<string>> = []; |
| 124 | for (let index = 0; index < cacheable.length; index += request.groupSize) { |
| 125 | groups.push(cacheable.slice(index, index + request.groupSize)); |
| 126 | } |
| 127 | groups.push(...entries.filter((file) => uncached.includes(file)).map((file) => [file])); |
| 128 | for (const group of groups) { |
| 129 | const key = NodeCrypto.createHash("sha256").update(group.join("\0")).digest("hex"); |
| 130 | tasks[`test-cache:${key}`] = { |
| 131 | dependsOn: [runTask], |
| 132 | command: `${command} ${group.map((file) => `'${NodePath.relative(directory, NodePath.join(root, file)).replaceAll("'", "'\"'\"'")}'`).join(" ")}`, |
| 133 | ...(directory === root ? {} : { cwd: NodePath.relative(root, directory) }), |
| 134 | cache: uncached.includes(group[0]!) |
| 135 | ? false |
| 136 | : { |
| 137 | // Vitest reads and rewrites these caches while running otherwise unchanged tests. |
| 138 | input: [ |
| 139 | { auto: true }, |
| 140 | "!**/node_modules/.vite/**", |
| 141 | "!**/node_modules/.vitest-cache/**", |
| 142 | ], |
| 143 | output: [], |
| 144 | }, |
| 145 | }; |
| 146 | } |
| 147 | } |
| 148 | return { |
| 149 | ...tasks, |
| 150 | [runTask]: { command: runCommand(request.run), cache: false }, |
| 151 | "test-cache": { command: 'node -e ""', dependsOn: Object.keys(tasks), cache: false }, |
| 152 | }; |
| 153 | } |