From 5ab7df285b47bd6b1338861b2bffa39cd5a0230a Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 23 Oct 2026 03:19:28 +0800 Subject: [PATCH] grok tests: no test finds an installed Grok Build (#325) Some grok tests looked Grok Build up as the plugin does and ran what they found (`grok --version`, `grok models`). On a machine with Grok Build installed that ran the real CLI, which can reach xAI. packages/grok/nocli.mjs is imported first by every grok test file, and each calls noRealGrok() before reading its environment. It sets every place executable() looks: - PATH: /usr/bin and /bin only (the fakes' scripts run cat, rm, sleep), each left out if it holds a grok; empty on Windows. - GROK_BIN_DIR: a folder that doesn't exist; GROK_HOME: an empty folder. - HOME and USERPROFILE: an empty folder, for ~/.grok/bin and ~/.local/bin. Bun's os.homedir() keeps the HOME the process started with (checked: setting process.env.HOME doesn't change it), so the helper mock.module()s node:os with a homedir that reads HOME (USERPROFILE) at call time, as Node's does, before index.mjs loads. Other packages in the same run see Node's behaviour: the started HOME unless a test changed it. A test that wants a CLI still puts its fake in GROK_BIN_DIR. Marker proof: a fake grok (under a .grok folder, so it counts as Grok Build) that appends its argv to a marker file, placed on the outer PATH, in ~/.local/bin and ~/.grok/bin under the test HOME. Run as TMPDIR=$H HOME=$H/home PATH=$H/outer:$PATH bun test: - old tests (92d5557), packages/grok: marker written, 5 runs (1 `grok --version`, 4 `grok models`, from ~/.grok/bin under the test HOME), 78 pass 4 fail - new tests, packages/grok: no marker, 82 pass 0 fail - new tests with the mock.module line commented out: marker written again (same 5 runs), 78 pass 4 fail - new tests, whole repo: no marker, 2166 pass 0 fail across 189 files - bun scripts/check.mjs: ok (no version bump: test-only change) Not tried: Windows; a run with the real HOME (the marker run covers the same folders under a test HOME); the real grok was never run. Closes #325 --- packages/grok/agent-message.test.mjs | 3 ++ packages/grok/import.test.mjs | 3 ++ packages/grok/login.test.mjs | 3 ++ packages/grok/models.test.mjs | 3 ++ packages/grok/nocli.mjs | 42 ++++++++++++++++++++++++++++ packages/grok/refresh.test.mjs | 3 ++ packages/grok/renew.test.mjs | 3 ++ packages/grok/schema.test.mjs | 3 ++ packages/grok/signin.test.mjs | 3 ++ packages/grok/tool-choice.test.mjs | 3 ++ packages/grok/usage.test.mjs | 3 ++ packages/grok/x-search.test.mjs | 3 ++ 12 files changed, 75 insertions(+) create mode 100644 packages/grok/nocli.mjs diff --git a/packages/grok/agent-message.test.mjs b/packages/grok/agent-message.test.mjs index 8c35eec..a3ab18a 100644 --- a/packages/grok/agent-message.test.mjs +++ b/packages/grok/agent-message.test.mjs @@ -1,6 +1,9 @@ +import { noRealGrok } from "./nocli.mjs" import { expect, test } from "bun:test" import { _internal } from "./index.mjs" +noRealGrok() + test("a plain Codex task reaches Grok as a user message without requiring tools", () => { const task = { type: "agent_message", author: "/root", recipient: "/root/research", diff --git a/packages/grok/import.test.mjs b/packages/grok/import.test.mjs index 78368d3..012cdec 100644 --- a/packages/grok/import.test.mjs +++ b/packages/grok/import.test.mjs @@ -1,12 +1,15 @@ // A CPA xAI account file is translated into the sign-in Grok Build keeps in // a home of the plugin's own. The file is only read: a CPA refresh token // rotates, so the account is migrated, not shared. +import { noRealGrok } from "./nocli.mjs" import { afterAll, afterEach, expect, test } from "bun:test" import { chmodSync, existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { basename, delimiter, dirname, join } from "node:path" import { GrokAuthPlugin } from "./index.mjs" +noRealGrok() + const CLIENT_ID = "b1a00492-073a-47ea-816f-4c329264a828" const SCOPE = `https://auth.x.ai::${CLIENT_ID}` diff --git a/packages/grok/login.test.mjs b/packages/grok/login.test.mjs index d33a66a..410c3d3 100644 --- a/packages/grok/login.test.mjs +++ b/packages/grok/login.test.mjs @@ -8,12 +8,15 @@ // (#65). `grok login --device-auth` is then `sh login --device-auth`, and // sh reads the script `login` in the folder the plugin runs it in, the // home folder: a plain file, read and never scanned. +import { noRealGrok } from "./nocli.mjs" import { afterAll, beforeAll, expect, test } from "bun:test" import { mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs" import { homedir, tmpdir } from "node:os" import { join } from "node:path" import { GrokAuthPlugin } from "./index.mjs" +noRealGrok() + const dir = mkdtempSync(join(tmpdir(), "grok-login-")) const script = join(homedir(), "login") beforeAll(() => { diff --git a/packages/grok/models.test.mjs b/packages/grok/models.test.mjs index 8f90fe2..45b8c29 100644 --- a/packages/grok/models.test.mjs +++ b/packages/grok/models.test.mjs @@ -1,9 +1,12 @@ // grok-4.7 offers the built-in's reasoning levels before Grok lists any: // magpie keeps the configured model when the models hook fails, and a // model with no variants lost the levels the built-in had (low…xhigh). +import { noRealGrok } from "./nocli.mjs" import { expect, test } from "bun:test" import { GrokAuthPlugin, _internal } from "./index.mjs" +noRealGrok() + test("the configured grok-4.7 carries the built-in's levels", async () => { const hooks = await GrokAuthPlugin({ client: { auth: { set: async () => {} } } }) const cfg = {} diff --git a/packages/grok/nocli.mjs b/packages/grok/nocli.mjs new file mode 100644 index 0000000..0e17392 --- /dev/null +++ b/packages/grok/nocli.mjs @@ -0,0 +1,42 @@ +// Imported first by every test here, and called before the file reads its +// environment: no test finds an installed Grok Build (plugins#325). The +// plugin looks for grok in GROK_BIN_DIR, GROK_HOME/bin, ~/.grok/bin, each +// folder on PATH and ~/.local/bin, and runs what it finds (`grok --version`, +// `grok models`), which can reach xAI. So PATH holds only /usr/bin and /bin +// (the fakes' scripts run cat, rm and sleep), and neither if it has a grok; +// on Windows it is empty. GROK_BIN_DIR is a folder that doesn't exist, and +// HOME (USERPROFILE on Windows) and GROK_HOME are empty folders of the +// test's own. A test that wants a CLI puts its fake +// in GROK_BIN_DIR, as before. +// +// 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 { existsSync, mkdirSync, mkdtempSync, rmSync } from "node:fs" +import * as os from "node:os" +import { delimiter, join } from "node:path" + +const started = os.homedir() +const homedir = () => (process.platform === "win32" ? process.env.USERPROFILE : process.env.HOME) || started +mock.module("node:os", () => ({ ...os, default: { ...os, homedir }, homedir })) + +const system = process.platform === "win32" ? [] : ["/usr/bin", "/bin"].filter((d) => !existsSync(join(d, "grok"))) +const root = mkdtempSync(join(os.tmpdir(), "grok-nocli-")) +process.on("exit", () => rmSync(root, { recursive: true, force: true })) + +export function noRealGrok() { + const home = join(root, "home") + mkdirSync(home, { recursive: true }) + mkdirSync(join(root, "grok-home"), { recursive: true }) + process.env.PATH = system.join(delimiter) + process.env.GROK_BIN_DIR = join(root, "no-bin") + process.env.GROK_HOME = join(root, "grok-home") + process.env.HOME = home + process.env.USERPROFILE = home +} + +noRealGrok() diff --git a/packages/grok/refresh.test.mjs b/packages/grok/refresh.test.mjs index b8c47a6..e8094ed 100644 --- a/packages/grok/refresh.test.mjs +++ b/packages/grok/refresh.test.mjs @@ -3,12 +3,15 @@ // CLI there twice at once. The CLI here is a fake in GROK_BIN_DIR: on // `grok models` it counts the run and, by FAKE_GROK, writes a new token // (renew), drops the sign-in (logout) or fails (anything else). +import { noRealGrok } from "./nocli.mjs" import { afterAll, afterEach, beforeEach, expect, test } from "bun:test" import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { join } from "node:path" import { GrokAuthPlugin } from "./index.mjs" +noRealGrok() + const real = globalThis.fetch const env = { ...process.env } const root = mkdtempSync(join(tmpdir(), "grok-refresh-")) diff --git a/packages/grok/renew.test.mjs b/packages/grok/renew.test.mjs index a55d046..ad19bd5 100644 --- a/packages/grok/renew.test.mjs +++ b/packages/grok/renew.test.mjs @@ -15,12 +15,15 @@ // token as it was (the CLI saying "Renewed" with pre_key == post_key), fail // exits 1 as a failed silent refresh. PATH is emptied so no real grok is // ever found, and fetch is a stand-in: nothing reaches the network. +import { noRealGrok } from "./nocli.mjs" import { afterAll, afterEach, beforeEach, expect, test } from "bun:test" import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { join } from "node:path" import { GrokAuthPlugin } from "./index.mjs" +noRealGrok() + const real = globalThis.fetch const env = { ...process.env } const root = mkdtempSync(join(tmpdir(), "grok-renew-")) diff --git a/packages/grok/schema.test.mjs b/packages/grok/schema.test.mjs index 9c6096a..7b35ccb 100644 --- a/packages/grok/schema.test.mjs +++ b/packages/grok/schema.test.mjs @@ -3,10 +3,13 @@ // themselves (testdata: what ChatGPT.app 26.930's zod builds). Grok turned // the request away: "tool parameter root must be an object type (root // schema is an anyOf/oneOf union with a non-object branch)" (magpie#1271). +import { noRealGrok } from "./nocli.mjs" import { expect, test } from "bun:test" import { readFileSync } from "node:fs" import { _internal } from "./index.mjs" +noRealGrok() + const SCHEMA = readFileSync(new URL("./testdata/codex_automation_update_schema.json", import.meta.url), "utf8") const body = (parameters) => diff --git a/packages/grok/signin.test.mjs b/packages/grok/signin.test.mjs index e813f45..27e2f85 100644 --- a/packages/grok/signin.test.mjs +++ b/packages/grok/signin.test.mjs @@ -2,12 +2,15 @@ // (internal/provider/grok.go, grokSigned): Grok's 401 goes on as it came, // and says X-Magpie-Sign-In: kept, as the built-in never marked a Grok // account lapsed. +import { noRealGrok } from "./nocli.mjs" import { afterAll, afterEach, expect, test } from "bun:test" import { mkdtempSync, rmSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { join } from "node:path" import { GrokAuthPlugin } from "./index.mjs" +noRealGrok() + const real = globalThis.fetch afterEach(() => (globalThis.fetch = real)) diff --git a/packages/grok/tool-choice.test.mjs b/packages/grok/tool-choice.test.mjs index d0090f9..980b4c6 100644 --- a/packages/grok/tool-choice.test.mjs +++ b/packages/grok/tool-choice.test.mjs @@ -1,6 +1,9 @@ +import { noRealGrok } from "./nocli.mjs" import { expect, test } from "bun:test" import { _internal } from "./index.mjs" +noRealGrok() + // magpie hands a request that turned tools off with none declared // "tool_choice": "none" (yetone/magpie#3110); Grok refuses a tool_choice // with no tools beside it, so it goes before the request does. diff --git a/packages/grok/usage.test.mjs b/packages/grok/usage.test.mjs index e5d33af..52ed326 100644 --- a/packages/grok/usage.test.mjs +++ b/packages/grok/usage.test.mjs @@ -1,12 +1,15 @@ // auth.usage tells what magpie's built-in Grok account shows // (internal/provider/grok_usage.go), against the CLI backend's replies as // its tests give them (grok_usage_test.go). +import { noRealGrok } from "./nocli.mjs" import { afterAll, afterEach, expect, test } from "bun:test" import { mkdtempSync, rmSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { join } from "node:path" import { GrokAuthPlugin, _internal } from "./index.mjs" +noRealGrok() + const real = globalThis.fetch afterEach(() => (globalThis.fetch = real)) diff --git a/packages/grok/x-search.test.mjs b/packages/grok/x-search.test.mjs index 29b7724..82c785c 100644 --- a/packages/grok/x-search.test.mjs +++ b/packages/grok/x-search.test.mjs @@ -1,9 +1,12 @@ +import { noRealGrok } from "./nocli.mjs" import { afterAll, afterEach, expect, test } from "bun:test" import { mkdtempSync, rmSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { join } from "node:path" import { GrokAuthPlugin, _internal } from "./index.mjs" +noRealGrok() + const realFetch = globalThis.fetch afterEach(() => { globalThis.fetch = realFetch }) const home = mkdtempSync(join(tmpdir(), "grok-x-search-"))