Files
oh-my-pi/packages/coding-agent/test/settings-manager.test.ts
T
can1357 fcbfab765b refactor: consolidated module import paths
- Removed custom TypeScript path aliases and the associated transformation script.
- Replaced aliased imports with direct `@oh-my-pi` package imports for improved clarity.
- Simplified internal module references by removing explicit `/index` suffixes.
2026-01-23 21:01:13 +01:00

116 lines
3.8 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import { existsSync, mkdirSync, rmSync } from "node:fs";
import { join } from "node:path";
import { SettingsManager } from "@oh-my-pi/pi-coding-agent/config/settings-manager";
import { YAML } from "bun";
describe("SettingsManager", () => {
let testDir: string;
let agentDir: string;
let projectDir: string;
beforeEach(() => {
// Use random UUID to isolate parallel test runs (SQLite files can't be shared)
testDir = join(process.cwd(), "test-settings-tmp", crypto.randomUUID());
agentDir = join(testDir, "agent");
projectDir = join(testDir, "project");
if (existsSync(testDir)) {
rmSync(testDir, { recursive: true });
}
mkdirSync(agentDir, { recursive: true });
mkdirSync(join(projectDir, ".pi"), { recursive: true });
});
const getConfigPath = () => join(agentDir, "config.yml");
const writeSettings = async (settings: Record<string, unknown>) => {
await Bun.write(getConfigPath(), YAML.stringify(settings, null, 2));
};
const readSettings = async (): Promise<Record<string, unknown>> => {
const file = Bun.file(getConfigPath());
if (!(await file.exists())) return {};
const content = await file.text();
const parsed = YAML.parse(content);
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return {};
return parsed as Record<string, unknown>;
};
afterEach(() => {
if (existsSync(testDir)) {
rmSync(testDir, { recursive: true });
}
});
// Tests that SettingsManager merges with DB state on save rather than blindly overwriting.
// This ensures external edits (via AgentStorage directly) aren't lost when the app saves.
describe("preserves externally added settings", () => {
it("should preserve enabledModels when changing thinking level", async () => {
// Seed initial settings in config.yml
await writeSettings({
theme: "dark",
modelRoles: { default: "claude-sonnet" },
});
// Manager loads the initial state
const manager = await SettingsManager.create(projectDir, agentDir);
// Simulate external edit (e.g., user modifying DB directly or another process)
await writeSettings({
theme: "dark",
modelRoles: { default: "claude-sonnet" },
enabledModels: ["claude-opus-4-5", "gpt-5.2-codex"],
});
// Manager saves a change - should merge, not overwrite
await manager.setDefaultThinkingLevel("high");
const savedSettings = await readSettings();
expect(savedSettings.enabledModels).toEqual(["claude-opus-4-5", "gpt-5.2-codex"]);
expect(savedSettings.defaultThinkingLevel).toBe("high");
expect(savedSettings.theme).toBe("dark");
expect((savedSettings.modelRoles as { default?: string } | undefined)?.default).toBe("claude-sonnet");
});
it("should preserve custom settings when changing theme", async () => {
await writeSettings({
modelRoles: { default: "claude-sonnet" },
});
const manager = await SettingsManager.create(projectDir, agentDir);
await writeSettings({
modelRoles: { default: "claude-sonnet" },
shellPath: "/bin/zsh",
extensions: ["/path/to/extension.ts"],
});
await manager.setTheme("light");
const savedSettings = await readSettings();
expect(savedSettings.shellPath).toBe("/bin/zsh");
expect(savedSettings.extensions).toEqual(["/path/to/extension.ts"]);
expect(savedSettings.theme).toBe("light");
});
it("should let in-memory changes override file changes for same key", async () => {
await writeSettings({
theme: "dark",
});
const manager = await SettingsManager.create(projectDir, agentDir);
await writeSettings({
theme: "dark",
defaultThinkingLevel: "low",
});
await manager.setDefaultThinkingLevel("high");
const savedSettings = await readSettings();
expect(savedSettings.defaultThinkingLevel).toBe("high");
});
});
});