feat: draft settings persistence — shared model/thinking/context record for the new-session surfaces

Both new-session pages (the home graph view's topic dock and /chat/new)
held their model + thinking + knowledge-context choices in plain
useState(undefined) — every navigation reset them to config defaults.

Now both surfaces read/write ONE server-side record
(data/draft-settings.json via GET/PUT /api/settings/draft, the
render-settings pattern): whatever the learner last left the controls on
is what every later visit shows. useDraftSettings loads on mount with a
touch-guard (a slow GET never clobbers a fast choice), saves
optimistically. Session creation still carries the choice into the new
chat exactly as before; per-session settings are untouched.

The PUT validation is the exact per-chat contract — parseSettingsBody
extracted verbatim from app/api/chats/[id]/settings and shared by both
routes (unknown model/level → 400 with the supported list, never a
silent clamp). 400s on garbage, {} = the clear-overrides write.

Live-verified: Cmd+K+Enter model pick, Shift+Tab thinking cycle, context
slider — all held across home → session → home → /chat/new, and carried
into a created session's record.
This commit is contained in:
amosblomqvist
2026-09-18 09:25:40 +03:00
parent 597b3f43e1
commit a94a108e74
10 changed files with 359 additions and 59 deletions
+108
View File
@@ -0,0 +1,108 @@
/**
* Tests for the draft-settings wave (2026-09-18): the shared settings-body
* parser (the one seam behind BOTH settings routes — per-chat and draft —
* so their parse behavior is identical) and the draft-settings store
* (data/draft-settings.json read/write, first-boot null semantics).
*
* The store tests write the real repo data file (npm test's shared data/,
* disposable scratch by doctrine) and clean up after themselves.
*/
import assert from "node:assert/strict";
import fs from "node:fs";
import { afterEach, describe, it } from "node:test";
import { GRAPH_CONTEXT_TOKENS_MAX, type ChatSettings } from "../src/core";
import { parseSettingsBody } from "../src/server/settings-body.ts";
import { DRAFT_SETTINGS_FILE } from "../config.ts";
import { readDraftSettings, writeDraftSettings } from "../src/store/draft-settings.ts";
describe("parseSettingsBody (shared by both settings routes)", () => {
it("an empty object parses to {} — the clear-overrides write", () => {
const r = parseSettingsBody({});
assert.ok(r.ok);
assert.deepEqual(r.ok ? r.settings : null, {});
});
it("a full valid body parses field-for-field", () => {
const r = parseSettingsBody({
model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" },
thinkingLevel: "high",
graphContextTokens: 256,
});
assert.ok(r.ok);
assert.deepEqual(r.settings, {
model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" },
thinkingLevel: "high",
graphContextTokens: 256,
});
});
it("partial bodies keep only the present overrides (replace-whole)", () => {
const r = parseSettingsBody({ thinkingLevel: "low" });
assert.ok(r.ok);
assert.deepEqual(r.settings, { thinkingLevel: "low" });
});
it("rejects non-object bodies", () => {
for (const body of ["junk", 42, [{}]]) {
const r = parseSettingsBody(body);
assert.equal(r.ok, false, JSON.stringify(body));
if (!r.ok) assert.match(r.error, /Invalid JSON body/);
}
});
it("rejects malformed model and non-string thinkingLevel", () => {
assert.equal(parseSettingsBody({ model: "nope" }).ok, false);
assert.equal(parseSettingsBody({ model: { provider: 1, id: "x" } }).ok, false);
assert.equal(parseSettingsBody({ thinkingLevel: 7 }).ok, false);
});
it("rejects out-of-range or non-integer graphContextTokens — never clamps", () => {
for (const bad of [-1, GRAPH_CONTEXT_TOKENS_MAX + 1, 64.5, "256"]) {
const r = parseSettingsBody({ graphContextTokens: bad });
assert.equal(r.ok, false, String(bad));
}
const ok = parseSettingsBody({ graphContextTokens: GRAPH_CONTEXT_TOKENS_MAX });
assert.equal(ok.ok, true);
});
});
describe("the draft-settings store", () => {
afterEach(() => {
try {
fs.rmSync(DRAFT_SETTINGS_FILE);
} catch {
/* nothing to clean */
}
});
it("reads null on first boot (no file) — the surfaces use config defaults", () => {
try {
fs.rmSync(DRAFT_SETTINGS_FILE);
} catch {
/* absent already */
}
assert.equal(readDraftSettings(), null);
});
it("write then read roundtrips verbatim — validated shape, no sanitizer games", () => {
const settings: ChatSettings = {
model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" },
thinkingLevel: "high",
graphContextTokens: 1024,
};
writeDraftSettings(settings);
assert.deepEqual(readDraftSettings(), settings);
});
it("an empty {} record roundtrips (all overrides cleared)", () => {
writeDraftSettings({});
assert.deepEqual(readDraftSettings(), {});
});
it("a corrupt file reads as null — defaults, never a crash", () => {
fs.rmSync(DRAFT_SETTINGS_FILE, { force: true }); // a directory from a broken earlier run also blocks
fs.writeFileSync(DRAFT_SETTINGS_FILE, "{ not json", "utf-8");
assert.equal(readDraftSettings(), null);
});
});