Skip to content

Commit 896ac3d

Browse files
committed
test(headless): isolate HOME so the ConfigError tests are hermetic
These tests force "no provider" by clearing env keys but read the real ~/.codebase/credentials.json, so they passed only when the dev had no valid login — and broke the moment a token refreshed. Point HOME at a temp dir per test so credential lookup always comes up empty.
1 parent a630808 commit 896ac3d

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/headless/run.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { mkdtempSync, rmSync } from "node:fs";
2+
import { tmpdir } from "node:os";
3+
import { join } from "node:path";
14
import type { Model } from "@earendil-works/pi-ai";
25
import { fauxAssistantMessage, registerFauxProvider } from "@earendil-works/pi-ai";
36
import { afterEach, beforeEach, describe, expect, it } from "vitest";
@@ -26,8 +29,16 @@ function makeCapture(): { capture: Capture; write: { stdout: (s: string) => void
2629
describe("runHeadless", () => {
2730
let faux: ReturnType<typeof registerFauxProvider>;
2831
let model: Model<string>;
32+
let tmpHome: string;
33+
let prevHome: string | undefined;
2934

3035
beforeEach(() => {
36+
// Isolate HOME so CredentialsStore can't pick up the dev's real
37+
// ~/.codebase login — otherwise the ConfigError tests are non-hermetic
38+
// and pass only when no valid credentials happen to exist on the box.
39+
prevHome = process.env.HOME;
40+
tmpHome = mkdtempSync(join(tmpdir(), "headless-home-"));
41+
process.env.HOME = tmpHome;
3142
faux = registerFauxProvider({
3243
models: [
3344
{
@@ -47,6 +58,9 @@ describe("runHeadless", () => {
4758

4859
afterEach(() => {
4960
faux.unregister();
61+
if (prevHome !== undefined) process.env.HOME = prevHome;
62+
else delete process.env.HOME;
63+
rmSync(tmpHome, { recursive: true, force: true });
5064
});
5165

5266
it("text mode emits the assistant reply on stdout", async () => {

0 commit comments

Comments
 (0)