| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | import { tmpdir } from "node:os"; |
| 4 | import { join } from "node:path"; |
| 5 | import { test, type TestContext } from "node:test"; |
| 6 | import { loadBuildIdentity } from "./buildIdentity.js"; |
| 7 | import { buildHelloParams } from "./handshake.js"; |
| 8 | |
| 9 | function resources(t: TestContext, content?: unknown): string { |
| 10 | const dir = mkdtempSync(join(tmpdir(), "reasonix-identity-")); |
| 11 | t.after(() => rmSync(dir, { recursive: true, force: true })); |
| 12 | if (content !== undefined) writeFileSync(join(dir, "build.json"), JSON.stringify(content)); |
| 13 | return dir; |
| 14 | } |
| 15 | |
| 16 | for (const version of ["v1.38.5", "v1.38.6-rc.1", "v0.0.0-ci"]) { |
| 17 | test(`packaged hello preserves the complete ${version} identity without environment overrides`, (t) => { |
| 18 | const build = { version, channel: "canary", commit: "abc123def456" }; |
| 19 | const dir = resources(t, { schemaVersion: 1, ...build, electron: "44.2.0", platform: "windows/amd64" }); |
| 20 | const identity = loadBuildIdentity(true, dir, { REASONIX_CHANNEL: "wrong", REASONIX_COMMIT: "wrong" }); |
| 21 | const hello = buildHelloParams({ ...identity, protocolVersion: 3, contractDigest: "sha256:fixture", hostVersion: "44.2.0", chromeVersion: "152", platform: "win32", arch: "x64", home: dir, dev: false }); |
| 22 | assert.deepEqual(hello.build, build); |
| 23 | assert.equal(hello.instance.dev, false); |
| 24 | }); |
| 25 | } |
| 26 | |
| 27 | test("unpackaged development needs no manifest", () => { |
| 28 | assert.deepEqual(loadBuildIdentity(false, "unused", {}), { version: "dev", channel: "dev", commit: "dev" }); |
| 29 | assert.deepEqual(loadBuildIdentity(false, "unused", { REASONIX_CHANNEL: "canary", REASONIX_COMMIT: "local" }), { version: "dev", channel: "canary", commit: "local" }); |
| 30 | }); |
| 31 | |
| 32 | test("missing or invalid packaged metadata cannot silently fall back to dev", (t) => { |
| 33 | const good = { schemaVersion: 1, version: "v1.38.5", channel: "stable", commit: "abc123" }; |
| 34 | assert.throws(() => loadBuildIdentity(true, resources(t), {}), /ENOENT/); |
| 35 | for (const bad of [null, [], {}, { ...good, schemaVersion: 2 }, { ...good, version: "dev" }, { ...good, version: "1.38.5" }, { ...good, version: "v1.38.5 " }, { ...good, channel: "" }, { ...good, commit: " " }]) { |
| 36 | assert.throws(() => loadBuildIdentity(true, resources(t, bad), {}), /build/); |
| 37 | } |
| 38 | const dir = resources(t); |
| 39 | writeFileSync(join(dir, "build.json"), "{"); |
| 40 | assert.throws(() => loadBuildIdentity(true, dir, {}), SyntaxError); |
| 41 | }); |
| 42 |