|
| 1 | +/** |
| 2 | + * v5.9.5 — self-healing machine_id regression suite. |
| 3 | + * |
| 4 | + * v5.9.4 fixed the derivation (resolveHostname() falls back to os.hostname()) |
| 5 | + * but didn't migrate the stale `unknown-<rand>` cache rows already persisted |
| 6 | + * in `gnosys_meta`. v5.9.5's `getMachineId()` now heals those cached values |
| 7 | + * (and any `dream_machine_id` pointing at the same broken id) the next time |
| 8 | + * it's called with a real hostname available. |
| 9 | + * |
| 10 | + * Pin process.env.HOSTNAME so the resolveHostname() path is deterministic |
| 11 | + * across dev and CI; pin to "" + use os.hostname() fallback for the |
| 12 | + * "still-unknown" branch. |
| 13 | + */ |
| 14 | + |
| 15 | +import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 16 | +import * as fs from "fs"; |
| 17 | +import * as fsp from "fs/promises"; |
| 18 | +import * as os from "os"; |
| 19 | +import * as path from "path"; |
| 20 | +import { GnosysDB } from "../lib/db.js"; |
| 21 | +import { getMachineId } from "../lib/remote.js"; |
| 22 | + |
| 23 | +const STALE_ID = "unknown-mp9cyh4j"; |
| 24 | +const ORIGINAL_HOSTNAME_ENV = process.env.HOSTNAME; |
| 25 | +const ORIGINAL_COMPUTERNAME_ENV = process.env.COMPUTERNAME; |
| 26 | + |
| 27 | +interface Env { |
| 28 | + dir: string; |
| 29 | + db: GnosysDB; |
| 30 | +} |
| 31 | + |
| 32 | +async function makeDb(): Promise<Env> { |
| 33 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "gnosys-v595-")); |
| 34 | + const db = new GnosysDB(dir); |
| 35 | + return { dir, db }; |
| 36 | +} |
| 37 | + |
| 38 | +async function cleanup(env: Env): Promise<void> { |
| 39 | + env.db.close(); |
| 40 | + await fsp.rm(env.dir, { recursive: true, force: true }); |
| 41 | +} |
| 42 | + |
| 43 | +describe("v5.9.5 — self-healing machine_id", () => { |
| 44 | + beforeEach(() => { |
| 45 | + process.env.HOSTNAME = "EdsMacStudio"; |
| 46 | + delete process.env.COMPUTERNAME; |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + if (ORIGINAL_HOSTNAME_ENV === undefined) delete process.env.HOSTNAME; |
| 51 | + else process.env.HOSTNAME = ORIGINAL_HOSTNAME_ENV; |
| 52 | + if (ORIGINAL_COMPUTERNAME_ENV === undefined) delete process.env.COMPUTERNAME; |
| 53 | + else process.env.COMPUTERNAME = ORIGINAL_COMPUTERNAME_ENV; |
| 54 | + }); |
| 55 | + |
| 56 | + it("heals a stale `unknown-<rand>` cache when a real hostname is available", async () => { |
| 57 | + const env = await makeDb(); |
| 58 | + try { |
| 59 | + env.db.setMeta("machine_id", STALE_ID); |
| 60 | + const id = getMachineId(env.db); |
| 61 | + expect(id).not.toBe(STALE_ID); |
| 62 | + expect(id.startsWith("EdsMacStudio-")).toBe(true); |
| 63 | + expect(env.db.getMeta("machine_id")).toBe(id); |
| 64 | + } finally { |
| 65 | + await cleanup(env); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + it("heals a stale `dream_machine_id` pointing at the same broken cached id", async () => { |
| 70 | + const env = await makeDb(); |
| 71 | + try { |
| 72 | + env.db.setMeta("machine_id", STALE_ID); |
| 73 | + env.db.setDreamMachineId(STALE_ID); |
| 74 | + const id = getMachineId(env.db); |
| 75 | + expect(id.startsWith("EdsMacStudio-")).toBe(true); |
| 76 | + expect(env.db.getDreamMachineId()).toBe(id); |
| 77 | + } finally { |
| 78 | + await cleanup(env); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + it("leaves a real cached id untouched (no churn)", async () => { |
| 83 | + const env = await makeDb(); |
| 84 | + try { |
| 85 | + const realId = "EdsMacStudio-abc123"; |
| 86 | + env.db.setMeta("machine_id", realId); |
| 87 | + const id = getMachineId(env.db); |
| 88 | + expect(id).toBe(realId); |
| 89 | + expect(env.db.getMeta("machine_id")).toBe(realId); |
| 90 | + } finally { |
| 91 | + await cleanup(env); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + it("leaves an unrelated `dream_machine_id` alone when machine_id heals", async () => { |
| 96 | + const env = await makeDb(); |
| 97 | + try { |
| 98 | + env.db.setMeta("machine_id", STALE_ID); |
| 99 | + env.db.setDreamMachineId("OtherMachine-xyz789"); |
| 100 | + const id = getMachineId(env.db); |
| 101 | + expect(id.startsWith("EdsMacStudio-")).toBe(true); |
| 102 | + // dream_machine_id pointed at a DIFFERENT machine — don't touch it. |
| 103 | + expect(env.db.getDreamMachineId()).toBe("OtherMachine-xyz789"); |
| 104 | + } finally { |
| 105 | + await cleanup(env); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | +}); |
0 commit comments