Files
learnharness/tests/settings-store.test.ts
T

83 lines
3.2 KiB
TypeScript
Raw Normal View History

/**
* Tests for the settings STORE (settings-storage wave, 2026-09-19):
* src/store/settings.ts — the record's fs adapter. Writes the real repo
* data/ files (npm test's shared data/, disposable scratch by doctrine)
* and cleans up after itself; a pre-existing settings.json is snapshotted
* and restored so the worktree's own dev state survives the run.
*
* (The legacy default-settings.json fold and the chat-section facade died
* with per-session models, owner 2026-09-28 — sessions own their model
* from birth, stamped from the strong slot server-side.)
*/
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { after, afterEach, describe, it } from "node:test";
import { SETTINGS_FILE } from "../config.ts";
import { readSettings, writeSettings } from "../src/store/settings.ts";
// Snapshot any pre-existing record; restore in after() so the worktree's
// own dev state survives a test run.
let hadSettings = false;
let savedSettings = "";
try {
savedSettings = fs.readFileSync(SETTINGS_FILE, "utf-8");
hadSettings = true;
} catch {
/* first boot — nothing to snapshot */
}
after(() => {
if (hadSettings) fs.writeFileSync(SETTINGS_FILE, savedSettings, "utf-8");
else fs.rmSync(SETTINGS_FILE, { force: true });
});
afterEach(() => {
fs.rmSync(SETTINGS_FILE, { force: true });
});
describe("the settings store", () => {
it("reads null on first boot (no record)", () => {
fs.rmSync(SETTINGS_FILE, { force: true });
assert.equal(readSettings(), null);
});
it("write→read round-trips through the sanitizer", () => {
writeSettings({ version: 1, graph: { enabled: false } });
const out = readSettings();
assert.ok(out);
assert.deepEqual(out.graph, { enabled: false });
});
it("persists multilingual STT independently of the interface language and update preference", () => {
const general = { language: "ru" as const, sttLanguage: "multi" as const, sttModel: "microsoft/mai-transcribe-2" as const, sttPreferredLanguages: ["en" as const, "ru" as const], autoUpdate: false };
writeSettings({ version: 1, general });
assert.deepEqual(JSON.parse(fs.readFileSync(SETTINGS_FILE, "utf-8")).general, general);
assert.deepEqual(readSettings()?.general, general);
});
it("a chat section on disk is IGNORED — the field died with per-session models (owner, 2026-09-28)", () => {
fs.mkdirSync(path.dirname(SETTINGS_FILE), { recursive: true });
// A fossil `chat` section (the old default-settings record) must not
// resurrect through the sanitizer — the key is simply gone.
fs.writeFileSync(
SETTINGS_FILE,
JSON.stringify({
version: 1,
chat: { model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" }, thinkingLevel: "low" },
graph: { enabled: true },
}),
"utf-8",
);
const out = readSettings();
assert.ok(out);
assert.equal("chat" in out, false, "the chat section never surfaces");
assert.deepEqual(out.graph, { enabled: true }); // siblings survive
});
it("corrupt settings.json → null (first-boot semantics), never throws", () => {
fs.mkdirSync(path.dirname(SETTINGS_FILE), { recursive: true });
fs.writeFileSync(SETTINGS_FILE, "{not json", "utf-8");
assert.equal(readSettings(), null);
});
});