diff --git a/packages/grok/login.test.mjs b/packages/grok/login.test.mjs index 1d238bd..95696e7 100644 --- a/packages/grok/login.test.mjs +++ b/packages/grok/login.test.mjs @@ -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-")) diff --git a/packages/grok/refresh.test.mjs b/packages/grok/refresh.test.mjs index 8429083..b8c47a6 100644 --- a/packages/grok/refresh.test.mjs +++ b/packages/grok/refresh.test.mjs @@ -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(() => {