From 7babb1f2a0ee9fc96f54e3b85b86c09f3f85e3e7 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Mon, 16 Nov 2026 02:46:36 -0700 Subject: [PATCH] Pin the install-ID reset and a getBuiltinModule that fails The README's "Resetting it" says deleting the file's ID line makes the next start write a new one, but only a missing file and a file of invalid lines were tested. The new test empties the file three ways and checks that a new UUID is written and then reused, and that a value put in by hand is used. installIdFromFile was tested only where getBuiltinModule is missing. The second new test covers one that throws and one that returns nothing: a fresh ID each call, and no file written. No behavior changed. Co-Authored-By: Claude Opus 5.5 --- tests/trace-client.test.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/trace-client.test.ts b/tests/trace-client.test.ts index f6d2898..3439207 100644 --- a/tests/trace-client.test.ts +++ b/tests/trace-client.test.ts @@ -1209,6 +1209,44 @@ describe("installation ID", () => { assert.equal(existsSync(file), false); }); + it("keeps the ID in memory where getBuiltinModule throws or has no node:fs", () => { + const file = join(dir, "install-id"); + const original = process.getBuiltinModule; + const standIns = [ + (): never => { + throw new Error("not allowed here"); + }, + (): undefined => undefined, + ]; + try { + for (const standIn of standIns) { + (process as { getBuiltinModule?: unknown }).getBuiltinModule = standIn; + const first = TraceClient.installIdFromFile(file); + assert.match(first, UUID); + assert.notEqual(TraceClient.installIdFromFile(file), first, "nothing persisted, so a new one each call"); + } + } finally { + process.getBuiltinModule = original; + } + assert.equal(existsSync(file), false, "without node:fs nothing is written"); + }); + + it("writes a new ID, kept from then on, to a file emptied or stripped of its ID line", () => { + // What the README's "Resetting it" promises: delete the ID line and the next start writes a new one. + const file = join(dir, "install-id"); + const first = TraceClient.installIdFromFile(file); + for (const reset of ["", "\n", "# reset by hand\n"]) { + writeFileSync(file, reset); + const made = TraceClient.installIdFromFile(file); + assert.match(made, UUID, `file=${JSON.stringify(reset)}`); + assert.notEqual(made, first, "a reset gives a new ID"); + assert.equal(readFileSync(file, "utf8"), made + "\n"); + assert.equal(TraceClient.installIdFromFile(file), made, "and the next start reuses it"); + } + writeFileSync(file, "my-own-id\n"); + assert.equal(TraceClient.installIdFromFile(file), "my-own-id", "a value put in by hand is used as is"); + }); + it("makes a UUID-shaped ID without throwing where crypto.randomUUID is missing or throws", () => { const descriptor = Object.getOwnPropertyDescriptor(globalThis, "crypto"); assert.ok(descriptor, "Node defines globalThis.crypto");