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
48 lines
2.4 KiB
JavaScript
48 lines
2.4 KiB
JavaScript
// Codex desktop offers codex_app's automation_update, whose parameters are
|
|
// a union at the root whose create and update branches are unions
|
|
// 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) =>
|
|
JSON.stringify({
|
|
model: "grok-4.7",
|
|
tools: [
|
|
{ type: "function", name: "exec_command", parameters: { type: "object", properties: { cmd: { type: "string" } } } },
|
|
{ type: "function", name: "mcp__codex_app__automation_update", strict: false, parameters },
|
|
],
|
|
})
|
|
|
|
test("a root union goes to Grok as an object, the nested branches' fields kept", () => {
|
|
const sent = JSON.parse(_internal.rewrite(body(JSON.parse(SCHEMA))))
|
|
const ps = sent.tools[1].parameters
|
|
expect(ps.type).toBe("object")
|
|
expect(ps.anyOf).toBeUndefined()
|
|
expect(ps.oneOf).toBeUndefined()
|
|
for (const k of ["mode", "id", "name", "prompt", "rrule", "status", "kind", "projectId", "model", "targetThreadId", "executionEnvironment"])
|
|
expect(Object.keys(ps.properties)).toContain(k)
|
|
expect(ps.required).toEqual(["mode"])
|
|
const mode = JSON.stringify(ps.properties.mode)
|
|
for (const v of ["view", "delete", "__schema11"]) expect(mode).toContain(v)
|
|
// the plain tool goes as it came
|
|
expect(sent.tools[0]).toEqual({ type: "function", name: "exec_command", parameters: { type: "object", properties: { cmd: { type: "string" } } } })
|
|
})
|
|
|
|
test("a body with no root union is passed on byte for byte", () => {
|
|
const b = body({ type: "object", properties: { a: { anyOf: [{ type: "string" }, { type: "null" }] } } })
|
|
expect(_internal.rewrite(b)).toBe(b)
|
|
})
|
|
|
|
test("a Chat function's parameters are folded too", () => {
|
|
const b = JSON.stringify({ tools: [{ type: "function", function: { name: "t", parameters: { anyOf: [{ type: "object", properties: { a: { type: "string" } } }, { type: "string" }] } } }] })
|
|
expect(JSON.parse(_internal.rewrite(b)).tools[0].function.parameters).toEqual({ type: "object", properties: { a: { type: "string" } } })
|
|
})
|