grok: the login and refresh tests' fake CLI is /bin/sh, not a script just written, so a busy Mac doesn't time them out (#65)

What waited was not a timer in the tests. On macOS the first run of an
executable just written waits for the system's scan of it: 4-15s each
here on a busy machine (a fresh `#!/bin/sh` file timed at 3.8-15.4s on
its first run, 0.00s after; a hard link or symlink to a scanned file, or
the same file read by sh, 0.00s). login.test.mjs wrote a new fake per
test, refresh.test.mjs one per file, so each paid it inside a test.

The fake grok is now a symlink to /bin/sh, and the plugin's own
`grok models` / `grok login --device-auth` become `sh models` /
`sh login …`, which read a plain script in the folder the plugin runs
the CLI in (root above the account's home; the home folder for login,
which login.test.mjs now only uses when HOME is a temp home). No
timeout was raised; refresh's `sleep 0.2` (for the joined-run test)
stays.

Run:
- With 14 `yes` burners of mine (load ~45): origin/main's two files
  5 and 6 fail (bun's 5s), this commit 0 fail in 1.5-1.9s, 3 runs.
- Idle: 10 pass, ~1.2s, 3 runs. Linux (oven/bun:1.4.2, /bin/sh = dash):
  10 pass.
- Broken by hand: links not taken (1 red), the CLI's last line not the
  reason (2 red), renewals not joined (1 red).
This commit is contained in:
yetone
2026-10-12 18:22:47 +08:00
parent b033987516
commit 6f1605f01d
2 changed files with 34 additions and 13 deletions
+22 -7
View File
@@ -1,19 +1,34 @@
// A `grok login` that prints no link says why (𝕏 on Discord): the sign-in
// said only "grok login gave no link to open", where the CLI had said it
// couldn't reach x.ai.
import { afterAll, expect, test } from "bun:test"
import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
//
// The fake CLI is /bin/sh under grok's name, not a script written here: on
// macOS the first run of a file just written waits for the system's scan
// of it, which took 4-15s on a busy machine and timed these tests out
// (#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 { 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"
const dir = mkdtempSync(join(tmpdir(), "grok-login-"))
afterAll(() => rmSync(dir, { recursive: true, force: true }))
const script = join(homedir(), "login")
beforeAll(() => {
// the script goes in the home folder, so only a home made for the test
if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test")
})
afterAll(() => {
rmSync(dir, { recursive: true, force: true })
rmSync(script, { force: true })
})
async function signIn(script) {
async function signIn(body) {
const bin = mkdtempSync(join(dir, "bin-"))
writeFileSync(join(bin, "grok"), `#!/bin/sh\n${script}\n`)
chmodSync(join(bin, "grok"), 0o755)
symlinkSync("/bin/sh", join(bin, "grok"))
writeFileSync(script, `${body}\n`)
const env = { GROK_BIN_DIR: process.env.GROK_BIN_DIR, GROK_HOME: process.env.GROK_HOME }
process.env.GROK_BIN_DIR = bin
process.env.GROK_HOME = mkdtempSync(join(dir, "home-"))
+12 -6
View File
@@ -4,7 +4,7 @@
// `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 { afterAll, afterEach, beforeEach, expect, test } from "bun:test"
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"
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"
@@ -14,14 +14,21 @@ const env = { ...process.env }
const root = mkdtempSync(join(tmpdir(), "grok-refresh-"))
afterAll(() => rmSync(root, { recursive: true, force: true }))
// The fake CLI is /bin/sh under grok's name, not a script written here: on
// macOS the first run of a file just written waits for the system's scan
// of it, which took 4-15s on a busy machine and timed these tests out
// (#65). The plugin runs `grok models` in the folder above the account's
// home, so that is `sh models` reading the script `models` in root: a
// plain file, read and never scanned. `grok --version` is sh's own answer
// (bash's version, or dash refusing the flag); no test here checks the
// version a request says.
const bin = join(root, "bin")
const later = new Date(Date.now() + 24 * 3600_000).toISOString()
mkdirSync(bin)
symlinkSync("/bin/sh", join(bin, "grok"))
writeFileSync(
join(bin, "grok"),
`#!/bin/sh
[ "$1" = models ] || exit 0
n=$(( $(cat "$GROK_HOME/runs" 2>/dev/null || echo 0) + 1 ))
join(root, "models"),
`n=$(( $(cat "$GROK_HOME/runs" 2>/dev/null || echo 0) + 1 ))
echo $n > "$GROK_HOME/runs"
sleep 0.2
case "$FAKE_GROK" in
@@ -31,7 +38,6 @@ case "$FAKE_GROK" in
esac
`,
)
chmodSync(join(bin, "grok"), 0o755)
let home
beforeEach(() => {