| 1 | // @effect-diagnostics nodeBuiltinImport:off - The fixture verifies synchronous bootstrap selection on disk. |
| 2 | import * as NodeFS from "node:fs"; |
| 3 | import * as NodeOS from "node:os"; |
| 4 | import * as NodePath from "node:path"; |
| 5 | import { afterEach, beforeEach, expect, it } from "vite-plus/test"; |
| 6 | import { readActiveForkRuntime } from "./forkRuntime.ts"; |
| 7 | |
| 8 | let home: string; |
| 9 | let bundled: string; |
| 10 | let releases: string; |
| 11 | const shell = "a".repeat(64); |
| 12 | const pointer = { |
| 13 | directory: "release-abc123", |
| 14 | version: "0.0.2000000000", |
| 15 | shell, |
| 16 | runtime: "b".repeat(64), |
| 17 | }; |
| 18 | |
| 19 | beforeEach(() => { |
| 20 | home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "knife-bootstrap-")); |
| 21 | bundled = NodePath.join(home, "bundle"); |
| 22 | releases = NodePath.join(home, "fork-releases"); |
| 23 | NodeFS.mkdirSync(bundled); |
| 24 | NodeFS.mkdirSync(releases); |
| 25 | NodeFS.writeFileSync( |
| 26 | NodePath.join(bundled, "knife-runtime.json"), |
| 27 | JSON.stringify({ shell, version: "0.0.1999999999" }), |
| 28 | ); |
| 29 | for (const file of [ |
| 30 | "apps/desktop/dist-electron/main.cjs", |
| 31 | "apps/desktop/dist-electron/compileCache.cjs", |
| 32 | "apps/desktop/dist-electron/preload.cjs", |
| 33 | "apps/server/dist/bin.mjs", |
| 34 | "apps/server/dist/client/index.html", |
| 35 | ]) { |
| 36 | const target = NodePath.join(releases, pointer.directory, file); |
| 37 | NodeFS.mkdirSync(NodePath.dirname(target), { recursive: true }); |
| 38 | NodeFS.writeFileSync(target, "fixture"); |
| 39 | } |
| 40 | NodeFS.writeFileSync(NodePath.join(releases, "current.json"), JSON.stringify(pointer)); |
| 41 | }); |
| 42 | afterEach(() => NodeFS.rmSync(home, { recursive: true, force: true })); |
| 43 | |
| 44 | it("selects the fully staged runtime matching the signed shell", () => { |
| 45 | expect(readActiveForkRuntime(home, bundled)).toEqual({ |
| 46 | ...pointer, |
| 47 | directory: NodePath.join(releases, pointer.directory), |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | it.each([ |
| 52 | { ...pointer, shell: "c".repeat(64) }, |
| 53 | { ...pointer, directory: "../outside" }, |
| 54 | { ...pointer, runtime: "malformed" }, |
| 55 | ])("ignores an incompatible or malformed active pointer", (invalid) => { |
| 56 | NodeFS.writeFileSync(NodePath.join(releases, "current.json"), JSON.stringify(invalid)); |
| 57 | expect(readActiveForkRuntime(home, bundled)).toBeUndefined(); |
| 58 | }); |
| 59 | |
| 60 | it.each([ |
| 61 | "apps/server/dist/client/index.html", |
| 62 | "apps/desktop/dist-electron/compileCache.cjs", |
| 63 | "apps/desktop/dist-electron/preload.cjs", |
| 64 | ])("keeps the bundled app when the staged runtime is missing %s", (file) => { |
| 65 | NodeFS.unlinkSync(NodePath.join(releases, pointer.directory, file)); |
| 66 | expect(readActiveForkRuntime(home, bundled)).toBeUndefined(); |
| 67 | }); |
| 68 | |
| 69 | it("prefers a newer bundled release even when an older pointer uses the same shell", () => { |
| 70 | NodeFS.writeFileSync( |
| 71 | NodePath.join(bundled, "knife-runtime.json"), |
| 72 | JSON.stringify({ shell, version: "0.0.2000000001" }), |
| 73 | ); |
| 74 | expect(readActiveForkRuntime(home, bundled)).toBeUndefined(); |
| 75 | }); |
| 76 | |
| 77 | it("requires the separate Windows server tree before selecting its runtime", () => { |
| 78 | expect(readActiveForkRuntime(home, bundled, "win32")).toBeUndefined(); |
| 79 | const directory = NodePath.join(releases, pointer.directory); |
| 80 | NodeFS.mkdirSync(NodePath.join(directory, "server/apps"), { recursive: true }); |
| 81 | NodeFS.renameSync( |
| 82 | NodePath.join(directory, "apps/server"), |
| 83 | NodePath.join(directory, "server/apps/server"), |
| 84 | ); |
| 85 | expect(readActiveForkRuntime(home, bundled, "win32")?.version).toBe(pointer.version); |
| 86 | }); |