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 |
| 2 | import * as NodeFS from "node:fs"; |
| 3 | import * as NodeOS from "node:os"; |
| 4 | import * as NodePath from "node:path"; |
| 5 | import * as NodeURL from "node:url"; |
| 6 | import { expect, it } from "vite-plus/test"; |
| 7 | import { cacheKey, seedPreparedCache } from "./server-compile-cache.ts"; |
| 8 | |
| 9 | it.each([ |
| 10 | { platform: "freebsd", hostArch: "x64", arch: 1, reason: "unsupported target freebsd" }, |
| 11 | { platform: "linux", hostArch: "x64", arch: 3, reason: "unexecutable target linux/arm64" }, |
| 12 | { platform: "linux", hostArch: "arm64", arch: 1, reason: "unexecutable target linux/x64" }, |
| 13 | { platform: "darwin", hostArch: "arm64", arch: 3, reason: "untrusted bundle", untrusted: true }, |
| 14 | ])( |
| 15 | "skips cache generation for $reason", |
| 16 | async ({ platform, hostArch, arch, reason, untrusted }) => { |
| 17 | const { spawnSync } = await import("node:child_process"); |
| 18 | const hook = NodeURL.fileURLToPath( |
| 19 | new URL("../prepare-server-compile-cache.cjs", import.meta.url), |
| 20 | ); |
| 21 | const result = spawnSync( |
| 22 | process.execPath, |
| 23 | [ |
| 24 | "-e", |
| 25 | `Object.defineProperty(process, "platform", { value: ${JSON.stringify(platform)} }); |
| 26 | Object.defineProperty(process, "arch", { value: ${JSON.stringify(hostArch)} }); |
| 27 | require(${JSON.stringify(hook)})({ electronPlatformName: ${JSON.stringify(platform)}, arch: ${arch} }).catch(error => { console.error(error); process.exitCode = 1; });`, |
| 28 | ], |
| 29 | { |
| 30 | encoding: "utf8", |
| 31 | env: { ...process.env, T3CODE_DESKTOP_UNTRUSTED_BUNDLE: untrusted ? "true" : "" }, |
| 32 | }, |
| 33 | ); |
| 34 | expect(result.status, result.stderr).toBe(0); |
| 35 | expect(result.stdout).toContain(`Skipping ${reason}`); |
| 36 | }, |
| 37 | ); |
| 38 | |
| 39 | // macOS runs on an ARM64 host to exercise the Rosetta build-host check. |
| 40 | it.each([ |
| 41 | { |
| 42 | platform: "darwin", |
| 43 | hostArch: "arm64", |
| 44 | arch: 1, |
| 45 | resources: "Fixture.app/Contents/Resources", |
| 46 | executable: "Fixture.app/Contents/MacOS/Fixture", |
| 47 | }, |
| 48 | { platform: "linux", hostArch: "x64", arch: 1, resources: "resources", executable: "fixture" }, |
| 49 | { platform: "linux", hostArch: "arm64", arch: 3, resources: "resources", executable: "fixture" }, |
| 50 | ])("prepares $platform/$hostArch cache", async ({ platform, hostArch, arch, ...layout }) => { |
| 51 | const { spawnSync } = await import("node:child_process"); |
| 52 | const root = NodeFS.realpathSync( |
| 53 | NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-hook-cache-")), |
| 54 | ); |
| 55 | try { |
| 56 | const resources = NodePath.join(root, layout.resources); |
| 57 | const archive = NodePath.join(resources, "app.asar"); |
| 58 | const dist = NodePath.join(archive, "apps/server/dist"); |
| 59 | const helper = NodePath.join(archive, "apps/desktop/dist-electron/serverCompileCache.cjs"); |
| 60 | NodeFS.mkdirSync(dist, { recursive: true }); |
| 61 | NodeFS.mkdirSync(NodePath.dirname(helper), { recursive: true }); |
| 62 | NodeFS.writeFileSync(NodePath.join(dist, "binCli-fixture.mjs"), "export const answer = 42;"); |
| 63 | NodeFS.writeFileSync(NodePath.join(dist, "server-fixture.mjs"), "export const answer = 43;"); |
| 64 | NodeFS.writeFileSync( |
| 65 | helper, |
| 66 | `module.exports = require(${JSON.stringify(NodeURL.fileURLToPath(new URL("./server-compile-cache.ts", import.meta.url)))});`, |
| 67 | ); |
| 68 | const hook = NodeURL.fileURLToPath( |
| 69 | new URL("../prepare-server-compile-cache.cjs", import.meta.url), |
| 70 | ); |
| 71 | // Node stands in for the packaged engine. |
| 72 | const result = spawnSync( |
| 73 | process.execPath, |
| 74 | [ |
| 75 | "-e", |
| 76 | ` |
| 77 | const childProcess = require("node:child_process"); |
| 78 | const spawn = childProcess.spawnSync; |
| 79 | childProcess.spawnSync = (executable, args, options) => { |
| 80 | require("node:assert/strict").equal(executable, ${JSON.stringify(NodePath.join(root, layout.executable))}); |
| 81 | return spawn(process.execPath, args, options); |
| 82 | }; |
| 83 | Object.defineProperty(process, "platform", { value: ${JSON.stringify(platform)} }); |
| 84 | Object.defineProperty(process, "arch", { value: ${JSON.stringify(hostArch)} }); |
| 85 | require(${JSON.stringify(hook)})({ |
| 86 | appOutDir: ${JSON.stringify(root)}, electronPlatformName: ${JSON.stringify(platform)}, arch: ${arch}, |
| 87 | packager: { appInfo: { productFilename: "Fixture" }, executableName: "fixture" } |
| 88 | }).catch(error => { console.error(error); process.exitCode = 1; }); |
| 89 | `, |
| 90 | ], |
| 91 | { encoding: "utf8" }, |
| 92 | ); |
| 93 | expect(result.status, result.stderr).toBe(0); |
| 94 | const manifest = JSON.parse( |
| 95 | NodeFS.readFileSync(NodePath.join(resources, "server-compile-cache/manifest.json"), "utf8"), |
| 96 | ); |
| 97 | expect( |
| 98 | manifest.entries.sort((a: { module: string }, b: { module: string }) => |
| 99 | a.module.localeCompare(b.module), |
| 100 | ), |
| 101 | ).toEqual([ |
| 102 | { |
| 103 | module: "app.asar/apps/server/dist/binCli-fixture.mjs", |
| 104 | file: expect.stringMatching(/^[a-f0-9]{8}$/), |
| 105 | }, |
| 106 | { |
| 107 | module: "app.asar/apps/server/dist/server-fixture.mjs", |
| 108 | file: expect.stringMatching(/^[a-f0-9]{8}$/), |
| 109 | }, |
| 110 | ]); |
| 111 | expect( |
| 112 | NodeFS.statSync(NodePath.join(resources, "server-compile-cache", manifest.entries[0].file)) |
| 113 | .size, |
| 114 | ).toBeGreaterThan(0); |
| 115 | } finally { |
| 116 | NodeFS.rmSync(root, { recursive: true, force: true }); |
| 117 | } |
| 118 | }); |
| 119 | |
| 120 | it("rejects ambiguous bundled server entries before importing them", async () => { |
| 121 | const { spawnSync } = await import("node:child_process"); |
| 122 | const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-cache-ambiguous-")); |
| 123 | try { |
| 124 | const dist = NodePath.join(root, "app.asar/apps/server/dist"); |
| 125 | NodeFS.mkdirSync(dist, { recursive: true }); |
| 126 | for (const file of ["binCli-fixture.mjs", "server-fixture.mjs", "server-unrelated.mjs"]) { |
| 127 | NodeFS.writeFileSync(NodePath.join(dist, file), "throw Error('Unexpected import');"); |
| 128 | } |
| 129 | const result = spawnSync( |
| 130 | process.execPath, |
| 131 | [ |
| 132 | "--input-type=module", |
| 133 | "-e", |
| 134 | `import {generatePreparedServerCache} from ${JSON.stringify(new URL("./server-compile-cache.ts", import.meta.url).href)}; await generatePreparedServerCache(${JSON.stringify(root)});`, |
| 135 | ], |
| 136 | { |
| 137 | encoding: "utf8", |
| 138 | env: { |
| 139 | ...process.env, |
| 140 | NODE_COMPILE_CACHE: undefined, |
| 141 | NODE_DISABLE_COMPILE_CACHE: undefined, |
| 142 | }, |
| 143 | }, |
| 144 | ); |
| 145 | expect(result.status).not.toBe(0); |
| 146 | expect(result.stderr).toContain("Ambiguous bundled server entry"); |
| 147 | expect(NodeFS.existsSync(NodePath.join(root, "server-compile-cache/manifest.json"))).toBe( |
| 148 | false, |
| 149 | ); |
| 150 | } finally { |
| 151 | NodeFS.rmSync(root, { recursive: true, force: true }); |
| 152 | } |
| 153 | }); |
| 154 | |
| 155 | it("reclaims only AppImage cache directories linked to gone mounts", async () => { |
| 156 | const { spawnSync } = await import("node:child_process"); |
| 157 | const root = NodeFS.realpathSync( |
| 158 | NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-cache-reclaim-")), |
| 159 | ); |
| 160 | try { |
| 161 | const resources = NodePath.join(root, ".mount_live/resources"); |
| 162 | const prepared = NodePath.join(resources, "server-compile-cache"); |
| 163 | const cacheRoot = NodePath.join(root, "cache"); |
| 164 | const server = NodePath.join(cacheRoot, "server"); |
| 165 | NodeFS.mkdirSync(prepared, { recursive: true }); |
| 166 | NodeFS.writeFileSync(NodePath.join(prepared, "manifest.json"), "{}"); |
| 167 | NodeFS.writeFileSync(NodePath.join(prepared, "12345678"), "live bytes"); |
| 168 | const gone = NodePath.join(root, ".mount_gone/resources/server-compile-cache/12345678"); |
| 169 | for (const [name, targets] of [ |
| 170 | ["appimage-stale", [gone]], |
| 171 | ["appimage-live", [NodePath.join(prepared, "12345678")]], |
| 172 | ["appimage-live-damaged", [NodePath.join(prepared, "87654321")]], |
| 173 | ["appimage-mixed", [gone, NodePath.join(prepared, "12345678")]], |
| 174 | ["appimage-unrelated", [NodePath.join(root, "unrelated-missing")]], |
| 175 | ["other-cache", [gone]], |
| 176 | ["appimage-initializing", []], |
| 177 | ] as const) { |
| 178 | const active = NodePath.join(server, name, "v24-engine-uid"); |
| 179 | NodeFS.mkdirSync(active, { recursive: true }); |
| 180 | targets.forEach((target, index) => |
| 181 | NodeFS.symlinkSync(target, NodePath.join(active, index === 0 ? "12345678" : "87654321")), |
| 182 | ); |
| 183 | NodeFS.writeFileSync(NodePath.join(active, "abcdef01"), "runtime replacement"); |
| 184 | } |
| 185 | NodeFS.symlinkSync( |
| 186 | NodePath.join(server, "other-cache"), |
| 187 | NodePath.join(server, "appimage-directory-link"), |
| 188 | ); |
| 189 | const result = spawnSync( |
| 190 | process.execPath, |
| 191 | [ |
| 192 | "--input-type=module", |
| 193 | "-e", |
| 194 | `process.env.APPIMAGE = "fixture"; import {enablePreparedServerCache} from ${JSON.stringify(new URL("./server-compile-cache.ts", import.meta.url).href)}; if (!enablePreparedServerCache(${JSON.stringify(NodePath.join(resources, "app.asar/apps/server/dist/binCli-fixture.mjs"))}, ${JSON.stringify(cacheRoot)})) throw Error("cache disabled");`, |
| 195 | ], |
| 196 | { |
| 197 | encoding: "utf8", |
| 198 | env: { |
| 199 | ...process.env, |
| 200 | NODE_COMPILE_CACHE: undefined, |
| 201 | NODE_DISABLE_COMPILE_CACHE: undefined, |
| 202 | }, |
| 203 | }, |
| 204 | ); |
| 205 | expect(result.status, result.stderr).toBe(0); |
| 206 | expect(NodeFS.readdirSync(server).sort()).toEqual([ |
| 207 | "appimage-directory-link", |
| 208 | "appimage-initializing", |
| 209 | "appimage-live", |
| 210 | "appimage-live-damaged", |
| 211 | "appimage-mixed", |
| 212 | "appimage-unrelated", |
| 213 | "other-cache", |
| 214 | ]); |
| 215 | expect(NodeFS.readFileSync(NodePath.join(prepared, "12345678"), "utf8")).toBe("live bytes"); |
| 216 | } finally { |
| 217 | NodeFS.rmSync(root, { recursive: true, force: true }); |
| 218 | } |
| 219 | }); |
| 220 | |
| 221 | it("seeds relocated cache entries without allowing writes into shipped resources", () => { |
| 222 | const root = NodeFS.realpathSync( |
| 223 | NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-prepared-test-")), |
| 224 | ); |
| 225 | try { |
| 226 | const prepared = NodePath.join(root, "resources", "server-compile-cache"); |
| 227 | const active = NodePath.join(root, "user", "different-uid-tag"); |
| 228 | NodeFS.mkdirSync(prepared, { recursive: true }); |
| 229 | NodeFS.mkdirSync(active, { recursive: true }); |
| 230 | NodeFS.writeFileSync(NodePath.join(prepared, "12345678"), "prepared bytes"); |
| 231 | const manifest = { |
| 232 | format: 1, |
| 233 | runtime: "matching", |
| 234 | entries: [{ module: "app.asar/apps/server/dist/chunk.mjs", file: "12345678" }], |
| 235 | }; |
| 236 | NodeFS.writeFileSync(NodePath.join(prepared, "manifest.json"), JSON.stringify(manifest)); |
| 237 | expect(seedPreparedCache(prepared, active, "wrong-engine")).toBe(false); |
| 238 | expect(seedPreparedCache(prepared, active, "matching")).toBe(true); |
| 239 | const key = cacheKey(active, NodePath.join(root, "resources", manifest.entries[0]!.module)); |
| 240 | expect(NodeFS.readFileSync(NodePath.join(active, key), "utf8")).toBe("prepared bytes"); |
| 241 | // Node persists by replacing the directory entry, never by overwriting the linked inode. |
| 242 | NodeFS.rmSync(NodePath.join(active, key)); |
| 243 | NodeFS.writeFileSync(NodePath.join(active, key), "runtime replacement"); |
| 244 | expect(NodeFS.readFileSync(NodePath.join(prepared, "12345678"), "utf8")).toBe("prepared bytes"); |
| 245 | expect(seedPreparedCache(prepared, active, "matching")).toBe(true); |
| 246 | expect(NodeFS.readFileSync(NodePath.join(active, key), "utf8")).toBe("runtime replacement"); |
| 247 | const missing = { module: "app.asar/apps/server/dist/missing.mjs", file: "87654321" }; |
| 248 | for (const entries of [ |
| 249 | [...manifest.entries, missing], |
| 250 | [missing, ...manifest.entries], |
| 251 | ]) { |
| 252 | NodeFS.writeFileSync( |
| 253 | NodePath.join(prepared, "manifest.json"), |
| 254 | JSON.stringify({ ...manifest, entries }), |
| 255 | ); |
| 256 | expect(seedPreparedCache(prepared, active, "matching")).toBe(false); |
| 257 | } |
| 258 | NodeFS.writeFileSync(NodePath.join(prepared, "manifest.json"), "corrupt"); |
| 259 | expect(seedPreparedCache(prepared, active, "matching")).toBe(false); |
| 260 | expect(seedPreparedCache(NodePath.join(root, "missing"), active, "matching")).toBe(false); |
| 261 | } finally { |
| 262 | NodeFS.rmSync(root, { recursive: true, force: true }); |
| 263 | } |
| 264 | }); |
| 265 | |
| 266 | it.each(["app", "server"])( |
| 267 | "generates %s.asar cache with the consumer engine and falls back after damage", |
| 268 | async (archive) => { |
| 269 | const { spawnSync } = await import("node:child_process"); |
| 270 | const helper = new URL("./server-compile-cache.ts", import.meta.url).href; |
| 271 | const root = NodeFS.realpathSync( |
| 272 | NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-cache-engine-")), |
| 273 | ); |
| 274 | const run = (code: string) => { |
| 275 | const env = { |
| 276 | ...process.env, |
| 277 | NODE_DEBUG_NATIVE: "COMPILE_CACHE", |
| 278 | NODE_COMPILE_CACHE: undefined, |
| 279 | NODE_DISABLE_COMPILE_CACHE: undefined, |
| 280 | }; |
| 281 | const result = spawnSync(process.execPath, ["--input-type=module", "-e", code], { |
| 282 | env, |
| 283 | encoding: "utf8", |
| 284 | }); |
| 285 | expect(result.status, result.stderr).toBe(0); |
| 286 | return result.stderr; |
| 287 | }; |
| 288 | try { |
| 289 | const producer = NodePath.join(root, "producer", "resources"); |
| 290 | const consumer = NodePath.join(root, "relocated", "resources"); |
| 291 | const relative = `${archive}.asar/apps/server/dist/binCli-fixture.mjs`; |
| 292 | NodeFS.mkdirSync(NodePath.dirname(NodePath.join(producer, relative)), { recursive: true }); |
| 293 | NodeFS.writeFileSync( |
| 294 | NodePath.join(producer, relative), |
| 295 | "export const answer = 42; export const loadServer = () => import('./server-fixture.mjs');", |
| 296 | ); |
| 297 | NodeFS.writeFileSync( |
| 298 | NodePath.join(producer, `${archive}.asar/apps/server/dist/server-fixture.mjs`), |
| 299 | "export const answer = 43;", |
| 300 | ); |
| 301 | run( |
| 302 | `import {generatePreparedServerCache} from ${JSON.stringify(helper)}; await generatePreparedServerCache(${JSON.stringify(producer)});`, |
| 303 | ); |
| 304 | NodeFS.cpSync(producer, consumer, { recursive: true }); |
| 305 | const consume = (home: string, appImage = false) => |
| 306 | run( |
| 307 | `if (${appImage}) process.env.APPIMAGE = "fixture"; import {enablePreparedServerCache} from ${JSON.stringify(helper)}; import {pathToFileURL} from 'node:url'; if(!enablePreparedServerCache(${JSON.stringify(NodePath.join(consumer, relative))},${JSON.stringify(NodePath.join(root, home))})) throw Error('cache disabled'); const m = await import(pathToFileURL(${JSON.stringify(NodePath.join(consumer, relative))}).href); if(m.answer!==42 || (await m.loadServer()).answer!==43) throw Error('source fallback failed');`, |
| 308 | ); |
| 309 | const prepared = NodePath.join(consumer, "server-compile-cache"); |
| 310 | NodeFS.chmodSync(prepared, 0o555); |
| 311 | try { |
| 312 | const first = consume("first-user"); |
| 313 | expect(first).toMatch(/binCli-fixture\.mjs was accepted/); |
| 314 | expect(first).toMatch(/server-fixture\.mjs was accepted/); |
| 315 | expect(consume("appimage-user", true)).toContain("was accepted"); |
| 316 | expect(NodeFS.readdirSync(NodePath.join(root, "appimage-user", "server"))).toEqual([]); |
| 317 | } finally { |
| 318 | NodeFS.chmodSync(prepared, 0o755); |
| 319 | } |
| 320 | for (const file of NodeFS.readdirSync(prepared).filter((file) => file !== "manifest.json")) { |
| 321 | NodeFS.writeFileSync(NodePath.join(prepared, file), "broken"); |
| 322 | } |
| 323 | expect(consume("second-user")).toContain("was not initialized"); |
| 324 | for (const file of NodeFS.readdirSync(prepared).filter((file) => file !== "manifest.json")) { |
| 325 | expect(NodeFS.readFileSync(NodePath.join(prepared, file), "utf8")).toBe("broken"); |
| 326 | } |
| 327 | NodeFS.rmSync(prepared, { recursive: true }); |
| 328 | expect(consume("third-user")).toContain("was not initialized"); |
| 329 | } finally { |
| 330 | NodeFS.rmSync(root, { recursive: true, force: true }); |
| 331 | } |
| 332 | }, |
| 333 | ); |