tests: grok's nocli puts PATH back when its file ends, so gumloop's Node children find node on CI

publish.yml has been red on main since 7ebab07c (run 43011771755, and
43012668584, 43012789287 after it): 4 of gumloop's regression tests
failed with `Executable not found in $PATH: "node"`, so dist and npm
were skipped and gumloop 0.1.0 and agy-cli 0.1.19 reached neither.

Cause: bun test runs every file of the root run in one process.
packages/grok/nocli.mjs sets PATH=/usr/bin:/bin (and HOME, USERPROFILE,
GROK_HOME, GROK_BIN_DIR) for grok's files and never put them back, so
every file after grok's ran with that PATH. On ubuntu CI node is
actions/setup-node's, under /opt/hostedtoolcache, so gumloop's
spawn("node") found none whenever bun ran grok's files before
gumloop's (CI's order). Locally bun ran gumloop first, so it passed.

Change:
- grok/nocli.mjs: the first noRealGrok of a file snapshots process.env
  and registers an afterAll that puts it back, removing what was added.
- gumloop/regression.test.mjs: node's absolute path is found once with
  Bun.which at load and spawned by path; not found, the Node tests fail
  as before (no skip).
- Siblings the same way (a file-level env change never put back), found
  by a full-run env audit: chat/chat.test.mjs (XDG_CONFIG_HOME pointing
  at its deleted temp dir, MAGPIE_API_KEY/GATEWAY_KEY/ADDR deleted) and
  cursor/calls.test.mjs (XDG_DATA_HOME) now restore them in afterAll.
  window-warmup's fake-gateway.ts/scheduler.test.ts XDG pins are left,
  filed as #996 (fake-gateway is also the --serve demo's).
  Other PATH writers (devin, gcp-ai, grok's own tests, sap-aicore,
  agy-cli, layerfs) already restore theirs; the audit saw PATH change
  only at grok's files.

Ran (magpie-desktop WSL, node only in ~/sdk/node/bin, bun 1.4.2):
- Red first, origin/main: `bun test --randomize --seed=3` (and 5) on
  grok/loop-check + gumloop/regression: the 4 CI failures, same error;
  also with --preload ./packages/grok/nocli.mjs.
- Same seeds with this change: 95 pass 0 fail (new gumloop test plus
  origin/main's copy of it).
- Broken by hand: afterAll(putBack) commented out -> both seeds red
  again, 4 fail.
- Root `HOME=$(mktemp -d) bun test` (with an env-audit preload): 8298
  pass, 19 skip, 0 fail, 8317 tests, 584 files; PATH back to the start
  value at the first file after grok's.
- Touched packages (chat, cursor, grok, gumloop, qoder) after the
  chat/cursor change: 1117 pass 0 fail, each XDG value back to start.

Not tried: ubuntu CI itself before pushing (publish.yml runs it next);
Windows (nocli sets PATH empty there, restored the same way).
This commit is contained in:
yetone
2026-11-11 19:53:46 +08:00
parent d45eb69f23
commit 740e1af252
4 changed files with 36 additions and 5 deletions
+3
View File
@@ -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
+5 -1
View File
@@ -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 })
+20 -1
View File
@@ -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 })
+8 -3
View File
@@ -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")