Skip to content

Commit f32aee2

Browse files
committed
test(settings): isolate settings writes from local settings.json
1 parent 4a06441 commit f32aee2

3 files changed

Lines changed: 59 additions & 7 deletions

File tree

tests/helpers/test-environment.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
1+
import fs from "node:fs";
2+
import os from "node:os";
3+
import path from "node:path";
4+
5+
function getDefaultTestHome(): string {
6+
const workerId = process.env.VITEST_WORKER_ID || "0";
7+
const preferredPath = path.join(process.cwd(), ".tmp", "test-home", `${process.pid}-${workerId}`);
8+
9+
try {
10+
fs.mkdirSync(preferredPath, { recursive: true });
11+
return preferredPath;
12+
} catch {
13+
const fallbackPath = path.join(
14+
os.tmpdir(),
15+
"opencode-telegram-bot",
16+
"test-home",
17+
`${process.pid}-${workerId}`,
18+
);
19+
fs.mkdirSync(fallbackPath, { recursive: true });
20+
return fallbackPath;
21+
}
22+
}
23+
124
const TEST_ENV_DEFAULTS: Record<string, string> = {
225
TELEGRAM_BOT_TOKEN: "test-telegram-token",
326
TELEGRAM_ALLOWED_USER_ID: "123456789",
427
OPENCODE_API_URL: "http://localhost:4096",
528
OPENCODE_MODEL_PROVIDER: "test-provider",
629
OPENCODE_MODEL_ID: "test-model",
730
LOG_LEVEL: "error",
31+
OPENCODE_TELEGRAM_HOME: getDefaultTestHome(),
832
};
933

1034
export function ensureTestEnvironment(): void {

tests/summary/aggregator.test.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
22
import type { Event } from "@opencode-ai/sdk/v2";
33
import { summaryAggregator } from "../../src/summary/aggregator.js";
4-
import { __resetSettingsForTests, setCurrentProject } from "../../src/settings/manager.js";
4+
5+
const mocked = vi.hoisted(() => ({
6+
getCurrentProjectMock: vi.fn(),
7+
}));
8+
9+
vi.mock("../../src/settings/manager.js", async () => {
10+
const actual = await vi.importActual<typeof import("../../src/settings/manager.js")>(
11+
"../../src/settings/manager.js",
12+
);
13+
14+
return {
15+
...actual,
16+
getCurrentProject: mocked.getCurrentProjectMock,
17+
};
18+
});
519

620
describe("summary/aggregator", () => {
721
beforeEach(() => {
8-
__resetSettingsForTests();
9-
setCurrentProject({ id: "p1", worktree: "D:/repo", name: "repo" });
22+
mocked.getCurrentProjectMock.mockReset();
23+
mocked.getCurrentProjectMock.mockReturnValue({ id: "p1", worktree: "D:/repo", name: "repo" });
1024
summaryAggregator.clear();
1125
summaryAggregator.setOnCleared(() => {});
1226
summaryAggregator.setOnTool(() => {});

tests/summary/formatter.test.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
import { beforeEach, describe, expect, it } from "vitest";
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
22
import { formatSummary, formatToolInfo, prepareCodeFile } from "../../src/summary/formatter.js";
3-
import { __resetSettingsForTests, setCurrentProject } from "../../src/settings/manager.js";
3+
4+
const mocked = vi.hoisted(() => ({
5+
getCurrentProjectMock: vi.fn(),
6+
}));
7+
8+
vi.mock("../../src/settings/manager.js", async () => {
9+
const actual = await vi.importActual<typeof import("../../src/settings/manager.js")>(
10+
"../../src/settings/manager.js",
11+
);
12+
13+
return {
14+
...actual,
15+
getCurrentProject: mocked.getCurrentProjectMock,
16+
};
17+
});
418

519
describe("summary/formatter", () => {
620
beforeEach(() => {
7-
__resetSettingsForTests();
8-
setCurrentProject({ id: "p1", worktree: "D:/repo", name: "repo" });
21+
mocked.getCurrentProjectMock.mockReset();
22+
mocked.getCurrentProjectMock.mockReturnValue({ id: "p1", worktree: "D:/repo", name: "repo" });
923
});
1024

1125
it("formats summary text and splits long output", () => {

0 commit comments

Comments
 (0)