From 2310ccdb7d8dfcf6d14a80d7bcf1b9f89d48457d Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Tue, 1 Sep 2026 16:08:59 +0000 Subject: [PATCH] test(gbrain): invalid version-probe timeout env falls back to the default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GSTACK_GBRAIN_VERSION_PROBE_TIMEOUT_MS set to 'abc', '-1', or '0' must use the default budget — exercised behaviorally through probeGbrainBin with a fresh PATH per case (the memo keys on PATH). Co-Authored-By: Claude Fable 5 --- test/gbrain-local-status.test.ts | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/gbrain-local-status.test.ts b/test/gbrain-local-status.test.ts index f37ac53ef..d1c534516 100644 --- a/test/gbrain-local-status.test.ts +++ b/test/gbrain-local-status.test.ts @@ -40,6 +40,7 @@ import { localEngineStatus, cacheFilePath, probeTimeoutMs, + probeGbrainBin, CACHE_TTL_MS, DEFAULT_PROBE_TIMEOUT_MS, type LocalEngineStatus, @@ -402,6 +403,44 @@ describe("probeTimeoutMs — env override parsing", () => { }); }); +describe("versionProbeTimeoutMs — invalid env overrides fall back to the default budget (behavioral via probeGbrainBin)", () => { + // versionProbeTimeoutMs is module-private, so pin its fallback BEHAVIOR: + // a fast healthy fake gbrain must probe identically whether the override + // env var is unset, non-numeric, or non-positive. If an invalid value ever + // reached execFileSync as its `timeout` (NaN / -1), the guarded call would + // throw into the catch and report { bin: null } — a fake "no-cli". + // + // probeGbrainBin memoizes per PATH key, so each case gets its OWN makeEnv + // (fresh mkdtemp bindir → unique PATH → fresh cache entry), and env is + // passed explicitly — no process.env mutation, no cross-case cache hits. + function probeWith(override?: string) { + const env = makeEnv({ withGbrain: true, gbrainBehavior: "ok", withConfig: true }); + try { + const probeEnv: NodeJS.ProcessEnv = { PATH: `${env.bindir}:/usr/bin:/bin` }; + if (override !== undefined) probeEnv.GSTACK_GBRAIN_VERSION_PROBE_TIMEOUT_MS = override; + return probeGbrainBin(probeEnv); + } finally { + env.cleanup(); + } + } + + it("unset override — the default-budget baseline resolves the bin", () => { + expect(probeWith()).toEqual({ bin: "gbrain", timedOut: false }); + }); + + it("non-numeric override ('abc') behaves as the default-budget case (no throw, sane shape)", () => { + expect(probeWith("abc")).toEqual({ bin: "gbrain", timedOut: false }); + }); + + it("negative override ('-1') behaves as the default-budget case (no throw, sane shape)", () => { + expect(probeWith("-1")).toEqual({ bin: "gbrain", timedOut: false }); + }); + + it("zero override ('0') behaves as the default-budget case (0 would mean NO timeout)", () => { + expect(probeWith("0")).toEqual({ bin: "gbrain", timedOut: false }); + }); +}); + describe("lib/gbrain-local-status — cache behavior", () => { let env: FakeEnv | null = null; let restoreEnv: (() => void) | null = null;