diff --git a/.gitignore b/.gitignore index 9828d84..7cdc27a 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,6 @@ gateway-sessions/ # Claude Code local state (cron lock, transcripts, etc.) .claude/ + +# Tool state written under a relative HOME (never commit) +.local/ diff --git a/src/cli/subagent-command.ts b/src/cli/subagent-command.ts index 6652efc..e6882bb 100644 --- a/src/cli/subagent-command.ts +++ b/src/cli/subagent-command.ts @@ -8,12 +8,11 @@ import { readFileSync } from "node:fs"; import { join } from "node:path"; -import { homedir } from "node:os"; +import { ROUNDHOUSE_DIR } from "../config"; import { SubAgentOrchestratorImpl } from "../subagents/orchestrator"; import { validateRunId } from "../subagents/run-store"; import type { SpawnSpec, SubAgentRole, RoutingInfo } from "../subagents/types"; -const ROUNDHOUSE_DIR = join(homedir(), ".roundhouse"); function loadGatewayConfig(): { notifyChatIds: (string | number)[] } { try { diff --git a/test/setup/global-home.ts b/test/setup/global-home.ts new file mode 100644 index 0000000..c0a705d --- /dev/null +++ b/test/setup/global-home.ts @@ -0,0 +1,27 @@ +/** + * Vitest global setup: run the whole suite with HOME pointing at a throwaway + * directory, so nothing (tests, the processes they spawn, tools they shell out + * to) can read or write the real ~/.roundhouse, ~/.pi or ~/.mcporter. + * Runs in the main process before any worker starts; workers inherit the env. + */ +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +export default function setup(): () => void { + const realHome = process.env.HOME; + const realUserProfile = process.env.USERPROFILE; + const root = mkdtempSync(join(tmpdir(), "roundhouse-test-home-")); + process.env.HOME = root; + process.env.USERPROFILE = root; + process.env.ROUNDHOUSE_TEST_HOME_ROOT = root; + + return () => { + if (realHome === undefined) delete process.env.HOME; + else process.env.HOME = realHome; + if (realUserProfile === undefined) delete process.env.USERPROFILE; + else process.env.USERPROFILE = realUserProfile; + delete process.env.ROUNDHOUSE_TEST_HOME_ROOT; + rmSync(root, { recursive: true, force: true }); + }; +} diff --git a/test/setup/isolate-home.ts b/test/setup/isolate-home.ts new file mode 100644 index 0000000..768d53b --- /dev/null +++ b/test/setup/isolate-home.ts @@ -0,0 +1,19 @@ +/** + * Vitest setup file: every test file gets its own empty HOME, set before the + * test file (and so any src module) is imported. Paths such as ROUNDHOUSE_DIR + * are computed from homedir() at import time, so state written by one file can + * never be seen by another file or by the next run. + */ +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterAll } from "vitest"; + +const parent = process.env.ROUNDHOUSE_TEST_HOME_ROOT ?? tmpdir(); +const home = mkdtempSync(join(parent, "file-")); +process.env.HOME = home; +process.env.USERPROFILE = home; + +afterAll(() => { + rmSync(home, { recursive: true, force: true }); +}); diff --git a/test/telegram-delivery.test.ts b/test/telegram-delivery.test.ts index a841068..eb0fcef 100644 --- a/test/telegram-delivery.test.ts +++ b/test/telegram-delivery.test.ts @@ -29,6 +29,13 @@ import { CODE_MARK, firstDifference, flat, genDocument, rng, type Rng } from "./ const LIMIT = TELEGRAM_LIMIT; +/** + * The tests that loop over generated documents take a few seconds on a fast + * machine and several times that on a loaded CI runner. Vitest's default of + * 5 s made them fail there for speed alone, so they get their own timeout. + */ +const GENERATED_DOCS_TIMEOUT_MS = 60_000; + afterEach(() => { vi.restoreAllMocks(); }); @@ -657,7 +664,7 @@ describe("a table that is being streamed when a message fills up", () => { } // The table did straddle two messages in a good number of these runs. expect(boundaries).toBeGreaterThan(5); - }); + }, GENERATED_DOCS_TIMEOUT_MS); }); describe("a code block whose first line is longer than a message", () => { @@ -698,7 +705,7 @@ describe("delivery of generated documents", () => { const whole = htmlToVisible(markdownToTelegramHtml(md)).replace(/\n/g, ""); expect(firstDifference(flat(t.visible()), whole), `seed ${seed}`).toBeNull(); } - }); + }, GENERATED_DOCS_TIMEOUT_MS); /** The document as a model would stream it: mostly small pieces, now and then a big one */ async function* streamed(md: string, r: Rng, clock: ReturnType): AsyncIterable { @@ -731,7 +738,7 @@ describe("delivery of generated documents", () => { const whole = htmlToVisible(markdownToTelegramHtml(md)).replace(/\n/g, ""); expect(firstDifference(flat(t.visible()), whole), `seed ${seed}`).toBeNull(); } - }); + }, GENERATED_DOCS_TIMEOUT_MS); it("delivers every visible character when streaming 80 documents through rate limits", async () => { let limited = 0; @@ -760,7 +767,7 @@ describe("delivery of generated documents", () => { expect(firstDifference(flat(t.visible()), whole), `seed ${seed}`).toBeNull(); } expect(limited).toBeGreaterThan(40); - }); + }, GENERATED_DOCS_TIMEOUT_MS); it("delivers every table row of 120 documents with tables, and Telegram refuses nothing", async () => { /** The rows of the boxed tables in what a reader sees, without their padding */ @@ -793,7 +800,7 @@ describe("delivery of generated documents", () => { } } expect(rowsChecked).toBeGreaterThan(300); - }); + }, GENERATED_DOCS_TIMEOUT_MS); it("delivers every visible character when some bubbles can never be edited", async () => { let stuck = 0; @@ -826,5 +833,5 @@ describe("delivery of generated documents", () => { expect(firstDifference(strip(t.visible().join("")), whole), `seed ${seed}`).toBeNull(); } expect(stuck).toBeGreaterThan(40); - }); + }, GENERATED_DOCS_TIMEOUT_MS); }); diff --git a/test/telegram-setup.test.ts b/test/telegram-setup.test.ts index 4afa9fe..a351271 100644 --- a/test/telegram-setup.test.ts +++ b/test/telegram-setup.test.ts @@ -3,8 +3,10 @@ import { resolve } from "node:path"; import { mkdtemp, rm, readFile } from "node:fs/promises"; import { tmpdir } from "node:os"; -// Mock ROUNDHOUSE_DIR before importing pairing module -const origDir = process.env.ROUNDHOUSE_DIR; +// ROUNDHOUSE_DIR is derived from homedir() at import time, so the pairing +// tests point HOME at a fresh directory and re-import the modules. +const origHome = process.env.HOME; +const origUserProfile = process.env.USERPROFILE; describe("pairing", () => { let tempDir: string; @@ -287,14 +289,16 @@ describe("TelegramAdapter.handlePairing", () => { beforeEach(async () => { vi.resetModules(); // Clear module cache so ROUNDHOUSE_DIR is re-evaluated tempDir = await mkdtemp(resolve(tmpdir(), "roundhouse-adapter-test-")); - process.env.ROUNDHOUSE_DIR = tempDir; + process.env.HOME = tempDir; + process.env.USERPROFILE = tempDir; const { TelegramAdapter } = await import("../src/transports/telegram/telegram-adapter"); adapter = new TelegramAdapter(); }); afterEach(async () => { await rm(tempDir, { recursive: true, force: true }); - process.env.ROUNDHOUSE_DIR = origDir; + process.env.HOME = origHome; + process.env.USERPROFILE = origUserProfile; }); it("isPairingPending returns false when no pairing file exists", async () => { diff --git a/test/telegram-split.test.ts b/test/telegram-split.test.ts index 27a9a9a..2650015 100644 --- a/test/telegram-split.test.ts +++ b/test/telegram-split.test.ts @@ -23,6 +23,13 @@ import { CODE_MARK, PROSE_MARK, genDocument } from "./telegram-doc-generator"; const LIMIT = TELEGRAM_MESSAGE_LIMIT; +/** + * The tests that loop over generated documents take a few seconds on a fast + * machine and several times that on a loaded CI runner. Vitest's default of + * 5 s made them fail there for speed alone, so they get their own timeout. + */ +const GENERATED_DOCS_TIMEOUT_MS = 60_000; + /** The source each chunk covers, in order */ function sources(md: string, chunks: MarkdownChunk[]): string[] { return chunks.map((c) => md.slice(c.start, c.end)); @@ -324,7 +331,7 @@ describe("splitMarkdownForTelegram — generated documents", () => { const chunks = splitMarkdownForTelegram(md); expect(splitProblem(md, chunks, LIMIT), `seed ${seed}`).toBeNull(); } - }); + }, GENERATED_DOCS_TIMEOUT_MS); it("holds its properties for 150 documents at smaller limits", () => { for (let seed = 1001; seed <= 1150; seed++) { @@ -333,7 +340,7 @@ describe("splitMarkdownForTelegram — generated documents", () => { const chunks = splitMarkdownForTelegram(md, limit); expect(splitProblem(md, chunks, limit), `seed ${seed}, limit ${limit}`).toBeNull(); } - }); + }, GENERATED_DOCS_TIMEOUT_MS); it("covers what it claims to: long documents, split code blocks, split tables, hard cuts", () => { let split = 0, reopenedFence = 0, repeatedTable = 0, hardCut = 0, longest = 0; @@ -351,7 +358,7 @@ describe("splitMarkdownForTelegram — generated documents", () => { expect(repeatedTable).toBeGreaterThan(20); expect(hardCut).toBeGreaterThan(100); expect(longest).toBeGreaterThan(30_000); - }); + }, GENERATED_DOCS_TIMEOUT_MS); }); describe("splitMarkdownForTelegram — found in review", () => { diff --git a/vitest.config.ts b/vitest.config.ts new file mode 100644 index 0000000..d61dd1c --- /dev/null +++ b/vitest.config.ts @@ -0,0 +1,9 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + test: { + // Never let a test touch the real home directory (~/.roundhouse etc.). + globalSetup: ["./test/setup/global-home.ts"], + setupFiles: ["./test/setup/isolate-home.ts"], + }, +});