Files
learnharness/tests/settings-store.test.ts
amosblomqvist 3e9a85d60f per-session models: every session owns its model + thinking level from birth
The reported bug (2026-09-28): every chat ran glm-5.3-flash regardless of
any pick, while composer and settings UIs agreed with each other and lied
about the run. Root cause: a fossil default-settings record (settings.json
chat section, frozen pre-slots-wave) was stamped verbatim into every new
chat at birth by the new-session flow, and resolveConversationSpec prefers
the per-chat record over the strong slot forever.

Owner redesign — models are per session, always; the strong slot is the
new-session default:

- POST /api/chats stamps the newborn verbatim from the strong slot
  (model + thinking level); no client settings PUT on create anymore
- the composer control is dual-bound: in-session it reads/writes the
  session's own record (GET /api/chats/:id, PUT .../settings); on the
  draft surfaces it binds the strong slot (picking there = changing the
  default)
- thinking levels follow the identical mechanics (confirmed by owner)
- the zombie default-settings system dies whole: useDefaultSettings,
  /api/settings/defaults, src/store/default-settings.ts, the chat
  section of UserSettings (sanitizer strips fossil copies), the legacy
  default-settings.json fold, and DEFAULT_SETTINGS_FILE
- catalogDefaultsFor collapses to the strong slot alone
- thinking display for a legacy unstamped session resolves exactly like
  the engine (session ?? strong ?? model ladder)

Tests: new chat-birth-stamp suite pins the stamp, the unconfigured boot,
the unset level, and the copy-not-live-view regression; defaults pins
rewritten strong-only; legacy-migration suite deleted.
2026-09-28 23:30:11 +03:00

77 lines
2.7 KiB
TypeScript

/**
* 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("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);
});
});