Commit Graph
3 Commits
Author SHA1 Message Date
yetone 5ab7df285b 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
2026-10-23 03:19:41 +08:00
yetone 6f1605f01d 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).
2026-10-12 18:22:47 +08:00
yetone 0af8500c08 grok 0.1.6: magpie renews the sign-in ahead of time through auth.refresh
auth.refresh with refreshLead 5 minutes (the in-fetch margin): the CLI
renews the sign-in in the account's home (grok models), once per home
however many ask, and the token is read back. A token the CLI renewed
already is given without running it; no credential left (grok logout) is
signIn "expired"; a token the CLI couldn't renew throws and is retried.

The check before each request stays for OpenCode, which doesn't call the
hook (magpie 0.1.684+ does). refresh.test.mjs fails without the change;
HOME=$(mktemp -d) bun test passes (396 across the repo).
2026-10-03 12:07:22 +08:00