diff --git a/packages/chat/chat.test.mjs b/packages/chat/chat.test.mjs index bf3ae42..3a8b8a7 100644 --- a/packages/chat/chat.test.mjs +++ b/packages/chat/chat.test.mjs @@ -15,6 +15,8 @@ const tmp = mkdtempSync(join(tmpdir(), "page-chat-")) const cfg = join(tmp, "magpie") mkdirSync(cfg, { recursive: true }) writeFileSync(join(cfg, "caller-keys.json"), JSON.stringify([{ name: "chat", secret: "mk-test-key" }])) +// bun test runs every file in one process: put these back when the file ends +const outside = Object.fromEntries(["XDG_CONFIG_HOME", "MAGPIE_API_KEY", "MAGPIE_GATEWAY_KEY", "MAGPIE_ADDR"].map((k) => [k, process.env[k]])) process.env.XDG_CONFIG_HOME = tmp delete process.env.MAGPIE_API_KEY delete process.env.MAGPIE_GATEWAY_KEY @@ -76,6 +78,7 @@ afterAll(async () => { await svc?.close() gateway.stop(true) rmSync(tmp, { recursive: true, force: true }) + for (const [k, v] of Object.entries(outside)) v === undefined ? delete process.env[k] : (process.env[k] = v) }) // node:http, so the test sets Host and Origin exactly as a hostile page would diff --git a/packages/cursor/calls.test.mjs b/packages/cursor/calls.test.mjs index f7ad9a2..eb19a19 100644 --- a/packages/cursor/calls.test.mjs +++ b/packages/cursor/calls.test.mjs @@ -8,12 +8,16 @@ import { tmpdir } from "node:os" import { join } from "node:path" const data = mkdtempSync(join(tmpdir(), "cursor-calls-")) +const outside = process.env.XDG_DATA_HOME // put back when the file ends: bun test runs every file in one process process.env.XDG_DATA_HOME = data const { CursorAuthPlugin, _internal } = await import("./index.mjs") const real = globalThis.fetch afterEach(() => (globalThis.fetch = real)) -afterAll(() => rmSync(data, { recursive: true, force: true })) +afterAll(() => { + rmSync(data, { recursive: true, force: true }) + outside === undefined ? delete process.env.XDG_DATA_HOME : (process.env.XDG_DATA_HOME = outside) +}) const jwt = (exp) => ["e30", Buffer.from(JSON.stringify({ exp, sub: "calls-" + Math.random() })).toString("base64url"), "sig"].join(".") const auth = () => ({ type: "oauth", access: jwt(Math.floor(Date.now() / 1000) + 3600), refresh: "", expires: 0 }) diff --git a/packages/grok/nocli.mjs b/packages/grok/nocli.mjs index 0e17392..6750b0e 100644 --- a/packages/grok/nocli.mjs +++ b/packages/grok/nocli.mjs @@ -9,13 +9,20 @@ // test's own. A test that wants a CLI puts its fake // in GROK_BIN_DIR, as before. // +// bun test runs every file of the run in one process, so the file puts the +// environment back when it ends: every variable as it was before the file's +// first noRealGrok, and none it added. Left as it was, the next package's +// files ran with PATH=/usr/bin:/bin, and on a runner whose node is elsewhere +// (actions/setup-node's /opt/hostedtoolcache) gumloop's spawn("node") found +// none. +// // Bun's os.homedir() keeps the HOME the process started with, so a HOME set // here would leave ~/.grok/bin and ~/.local/bin where they were. homedir is // made to read HOME (USERPROFILE) when called, as Node's does; this module // is imported before index.mjs, so the plugin gets that one. Other packages' // tests in the same run see Node's behaviour, the started HOME unless a test // changed it. -import { mock } from "bun:test" +import { afterAll, mock } from "bun:test" import { existsSync, mkdirSync, mkdtempSync, rmSync } from "node:fs" import * as os from "node:os" import { delimiter, join } from "node:path" @@ -28,7 +35,19 @@ const system = process.platform === "win32" ? [] : ["/usr/bin", "/bin"].filter(( const root = mkdtempSync(join(os.tmpdir(), "grok-nocli-")) process.on("exit", () => rmSync(root, { recursive: true, force: true })) +let outside // the environment before this file's first noRealGrok +function putBack() { + if (!outside) return + for (const key of Object.keys(process.env)) if (!(key in outside)) delete process.env[key] + Object.assign(process.env, outside) + outside = undefined +} + export function noRealGrok() { + if (!outside) { + outside = { ...process.env } + afterAll(putBack) + } const home = join(root, "home") mkdirSync(home, { recursive: true }) mkdirSync(join(root, "grok-home"), { recursive: true }) diff --git a/packages/gumloop/regression.test.mjs b/packages/gumloop/regression.test.mjs index 0994c2f..470e326 100644 --- a/packages/gumloop/regression.test.mjs +++ b/packages/gumloop/regression.test.mjs @@ -8,6 +8,11 @@ import { createInterface } from "node:readline" import { GumloopPlugin, _internal } from "./index.mjs" import { LOCK_STALE_MS, LOCK_WAIT_MS, Store } from "./gumloop.mjs" +// Node's absolute path, found once on the PATH this file started with, so a +// later change to process.env.PATH can't hide it from the Node children. +// Not found, the Node tests fail as before ("node" not on PATH): no skip. +const node = Bun.which("node") ?? "node" + const auth = { type: "api", key: "mock-regression-key", metadata: { userId: "regression-user" } } const json = (body, status = 200) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } }) const dirs = [] @@ -218,9 +223,9 @@ describe("exclusive ledger updates", () => { expect(storeB.remaining("u")).toBe(99) }) - for (const runtime of ["node", process.execPath]) { + for (const runtime of [node, process.execPath]) { for (const phase of ["held", "partial", "choosing"]) { - test(`${runtime === "node" ? "Node" : "Bun"} crash at ${phase} recovers without manual cleanup`, async () => { + test(`${runtime === node ? "Node" : "Bun"} crash at ${phase} recovers without manual cleanup`, async () => { const directory = tempDir() const file = join(directory, "gumloop-ledger.json") new Store(file).anchor(auth.metadata.userId, 5000, "initial", 1) @@ -272,7 +277,7 @@ describe("exclusive ledger updates", () => { expect(readFileSync(file, "utf8")).toBe("keep") }) - for (const [name, runtime] of [["Node", "node"], ["Bun", process.execPath]]) { + for (const [name, runtime] of [["Node", node], ["Bun", process.execPath]]) { test(`${name}: two independent overlapping processes preserve all 200 chat charges`, async () => { const directory = tempDir() const file = join(directory, "gumloop-ledger.json")