Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
3 changes: 1 addition & 2 deletions src/cli/subagent-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions test/setup/global-home.ts
Original file line number Diff line number Diff line change
@@ -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 });
};
}
19 changes: 19 additions & 0 deletions test/setup/isolate-home.ts
Original file line number Diff line number Diff line change
@@ -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 });
});
19 changes: 13 additions & 6 deletions test/telegram-delivery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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<typeof fakeClock>): AsyncIterable<string> {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
12 changes: 8 additions & 4 deletions test/telegram-setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 () => {
Expand Down
13 changes: 10 additions & 3 deletions test/telegram-split.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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++) {
Expand All @@ -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;
Expand All @@ -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", () => {
Expand Down
9 changes: 9 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -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"],
},
});
Loading