Files
learnharness/tests/settings-body.test.ts
T
amosblomqvist 0fcffdffaf Birth-session clustering + the prober graph-tools data seam (wave 7 chunk A)
Embeddings are dying (chunk B removes the machinery); the clusters they
used to carry become the session that created them — derived on read.

- New src/core/session-clusters.ts: birthSessionPartition groups merged
  nodes by their FIRST SURVIVING provenance point's chatId (owner-locked
  2026-09-23). Deletion is self-healing by construction: stripped-birth
  survivors re-attribute to their earliest remaining contributor, and
  empty-provenance nodes stay unclustered. Notes imports are birth-sessions
  too (owner ruling) — their label is the file path.
- graphSnapshot: clusters = the birth partition filtered >= MIN_CLUSTER_SIZE
  (the hull presentation rule), id = session id, label = session name (chat
  title / file path; a deleted record renders unlabeled). The persisted
  cluster-labelings join dies; no LLM naming pass needed anymore.
- settleGraphLayout derives the same partition from readNodes — drops its
  readEmbeddings dep. One cluster concept everywhere.
- Prober data seam (core/graph.ts, pure ports-driven like the snapshot):
  graphSessionIndex (every birth session: id, name, chat|file kind,
  createdAt/updatedAt, birth-proposition count, recency-ordered) and
  graphSessionPropositions (a session's birth propositions with the
  extractor's latest understanding level and THAT event's own timestamp —
  owner ruling: quiz outcomes are ledger evidence, they never move the
  evaluation date). Teacher's tool handlers consume these in-process.
- New agent.graphTools subsection { enabled?: boolean } (owner ruling:
  the toggle gates prober tool REGISTRATION — agent capability, agent
  section, its own subsection, NOT the tool-toggles map; absent = ON).
  Lenient sanitizer, strict settings-body parser, passthrough resolution,
  and graphToolsEnabled(effective) = graph.enabled && toggle for Teacher's
  registry wiring.

Gates: tsc clean, eslint clean, 923/923 tests, next build compiles.
Live-verified on the worktree dev data: 10 nodes → 2 clusters labeled
with their chat titles; session index + propositions carry levels and
their own timestamps.
2026-09-23 14:51:37 +03:00

248 lines
11 KiB
TypeScript

/**
* Tests for the section parsers (settings-storage wave, 2026-09-19):
* src/server/settings-body.ts — general/agent/appearance/graph strictness.
* Same discipline as parseSettingsBody (its tests live in
* default-settings.test.ts): every known field is strict (bad type/shape
* → 400-style error, never a silent clamp), unknown keys drop, absent
* fields mean absent overrides. Pure — no fs.
*/
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
parseAgentSettingsBody,
parseModelsSettingsBody,
parseAppearanceSettingsBody,
parseGeneralSettingsBody,
parseGraphSettingsBody,
SETTINGS_SECTIONS,
} from "../src/server/settings-body.ts";
describe("parseGeneralSettingsBody", () => {
it("parses {} and {autoUpdate} replace-whole", () => {
const empty = parseGeneralSettingsBody({});
assert.ok(empty.ok);
assert.deepEqual(empty.settings, {});
const on = parseGeneralSettingsBody({ autoUpdate: true });
assert.ok(on.ok);
assert.deepEqual(on.settings, { autoUpdate: true });
});
it("rejects non-objects and non-boolean autoUpdate", () => {
assert.equal(parseGeneralSettingsBody("x").ok, false);
assert.equal(parseGeneralSettingsBody({ autoUpdate: "yes" }).ok, false);
});
});
describe("parseAgentSettingsBody", () => {
it("models: valid roles with model pair and/or thinkingLevel; unknown role is a 400 with the list", () => {
const r = parseAgentSettingsBody({
models: {
researcher: { model: { provider: "openrouter", id: "qwen/qwen3.8-flash" } },
labeler: { thinkingLevel: "high" },
},
});
assert.ok(r.ok);
assert.deepEqual(r.settings.models, {
researcher: { model: { provider: "openrouter", id: "qwen/qwen3.8-flash" } },
labeler: { thinkingLevel: "high" },
});
const bad = parseAgentSettingsBody({ models: { teacher: {} } });
assert.equal(bad.ok, false);
if (!bad.ok) assert.match(bad.error, /unknown role "teacher" \(supported: researcher, demoBuilder, visualMaker, extractor, auditor, labeler\)/);
const malformed = parseAgentSettingsBody({ models: { researcher: { model: { provider: 1, id: "x" } } } });
assert.equal(malformed.ok, false);
});
it("tools and prompts: boolean maps; non-boolean values are 400s", () => {
const r = parseAgentSettingsBody({ tools: { research: false }, prompts: { "system-prompt.md": true } });
assert.ok(r.ok);
assert.deepEqual(r.settings.tools, { research: false });
assert.deepEqual(r.settings.prompts, { "system-prompt.md": true });
assert.equal(parseAgentSettingsBody({ tools: { quiz: "no" } }).ok, false);
assert.equal(parseAgentSettingsBody({ prompts: { "x.md": 1 } }).ok, false);
// switch validity (segment membership, file presence) is the ROUTE's
// cross-validation — the parser only sees shapes.
});
it("search: engine pin is tavily|brave; anything else is a 400 (search wave, 2026-09-20)", () => {
const ok = parseAgentSettingsBody({ search: { engine: "tavily" } });
assert.ok(ok.ok);
assert.deepEqual(ok.settings.search, { engine: "tavily" });
const clear = parseAgentSettingsBody({ search: {} });
assert.ok(clear.ok);
assert.equal(clear.settings.search, undefined, "absent engine = back to auto-detect");
assert.equal(parseAgentSettingsBody({ search: { engine: "google" } }).ok, false);
assert.equal(parseAgentSettingsBody({ search: "tavily" }).ok, false, "search must be an object");
assert.equal(parseAgentSettingsBody({ search: { engine: 1 } }).ok, false);
});
it("graphTools: strict object subsection, enabled boolean when present — wave 7, 2026-09-23", () => {
const off = parseAgentSettingsBody({ graphTools: { enabled: false } });
assert.ok(off.ok);
assert.deepEqual(off.settings.graphTools, { enabled: false });
const bare = parseAgentSettingsBody({ graphTools: {} });
assert.ok(bare.ok);
assert.deepEqual(bare.settings.graphTools, {}, "explicit subsection with no key = the default ON");
assert.equal(parseAgentSettingsBody({ graphTools: "off" }).ok, false, "graphTools must be an object");
assert.equal(parseAgentSettingsBody({ graphTools: { enabled: "yes" } }).ok, false, "enabled must be a boolean");
assert.equal(parseAgentSettingsBody({ graphTools: null }).ok, false);
});
it("an empty {} agent section parses to {} (replace-whole clear)", () => {
const r = parseAgentSettingsBody({});
assert.ok(r.ok);
assert.deepEqual(r.settings, {});
});
});
describe("parseAppearanceSettingsBody", () => {
it("parses every field; hex-or-null colors; clamps happen at sanitize, rejects here", () => {
const r = parseAppearanceSettingsBody({
mode: "light",
background: "#ffffff",
fontFamily: "Some Font",
proseFontSize: 20,
sessionNameSerif: false,
mdRendering: { headingWeight: 700, boldColor: "#ff8800", italicColor: null },
});
assert.ok(r.ok);
assert.deepEqual(r.settings, {
mode: "light",
background: "#ffffff",
fontFamily: "Some Font",
proseFontSize: 20,
sessionNameSerif: false,
mdRendering: { headingWeight: 700, boldColor: "#ff8800", italicColor: null },
});
});
it("rejects bad enums, ranges, and colors — with specific messages, never clamps", () => {
const cases: unknown[] = [
{ mode: "sepia" },
{ background: "blue" },
{ background: 42 },
{ fontFamily: "" },
{ fontFamily: 12 },
{ proseFontSize: 30 },
{ proseFontSize: 64.5 },
{ sessionNameSerif: 1 },
{ mdRendering: { headingWeight: 1000 } },
{ mdRendering: { boldColor: "red" } },
];
for (const body of cases) {
assert.equal(parseAppearanceSettingsBody(body).ok, false, JSON.stringify(body));
}
// boundary values that MUST pass: 12 and 24, 100 and 900, #fff
assert.ok(parseAppearanceSettingsBody({ proseFontSize: 12 }));
assert.ok(parseAppearanceSettingsBody({ proseFontSize: 24 }));
assert.ok(parseAppearanceSettingsBody({ mdRendering: { headingWeight: 100 } }));
assert.ok(parseAppearanceSettingsBody({ mdRendering: { headingWeight: 900 } }));
assert.ok(parseAppearanceSettingsBody({ background: "#abc" }));
// null = "the mode's default" — explicit, valid
assert.ok(parseAppearanceSettingsBody({ background: null, fontFamily: null }));
});
it("rejects non-object bodies and non-object mdRendering", () => {
assert.equal(parseAppearanceSettingsBody([1]).ok, false);
assert.equal(parseAppearanceSettingsBody({ mdRendering: "big" }).ok, false);
});
});
describe("parseGraphSettingsBody", () => {
it("parses the four-rung ladder replace-whole", () => {
const r = parseGraphSettingsBody({ enabled: false, embedding: false });
assert.ok(r.ok);
assert.deepEqual(r.settings, { enabled: false, embedding: false });
assert.equal(parseGraphSettingsBody({}).ok, true);
});
it("rejects non-boolean rungs and unknown bodies", () => {
assert.equal(parseGraphSettingsBody({ enabled: "off" }).ok, false);
assert.equal(parseGraphSettingsBody("nope").ok, false);
});
it("the ladder DEPENDENCY (graph → embedding → clustering → labeling) is a UI concern, not a parse concern", () => {
// Nothing stops a stored {enabled: false, labeling: true} — the
// values persist but resolve inert (grays out in the panel).
const r = parseGraphSettingsBody({ enabled: false, clustering: true, labeling: true });
assert.ok(r.ok);
});
it("embeddingModel (wave 6): null or a shipped-provider slug; anything else is a 400", () => {
const nulled = parseGraphSettingsBody({ embeddingModel: null });
assert.ok(nulled.ok);
assert.deepEqual(nulled.settings, { embeddingModel: null });
for (const slug of [
"openai:text-embedding-3-small",
"openrouter:openai/text-embedding-3-large",
"google:gemini-embedding-001",
// OpenRouter ids may carry a :free-style suffix — the provider is
// the part before the FIRST colon (live catalog fact, 2026-09-23).
"openrouter:liquid/lfm-2.5-embedding-350m:free",
]) {
const r = parseGraphSettingsBody({ embeddingModel: slug });
assert.ok(r.ok, slug);
assert.deepEqual(r.settings, { embeddingModel: slug });
}
// Bad shapes never become a live model.
assert.equal(parseGraphSettingsBody({ embeddingModel: "text-embedding-3-small" }).ok, false, "no provider prefix");
assert.equal(parseGraphSettingsBody({ embeddingModel: "bedrock:whatever" }).ok, false, "unshipped provider");
assert.equal(parseGraphSettingsBody({ embeddingModel: "openai:" }).ok, false, "empty id");
assert.equal(parseGraphSettingsBody({ embeddingModel: 42 }).ok, false, "non-string");
assert.equal(parseGraphSettingsBody({ embeddingModel: "openai:has spaces" }).ok, false, "whitespace id");
});
});
describe("SETTINGS_SECTIONS (the route's section map)", () => {
it("maps exactly the six record sections", () => {
assert.deepEqual(Object.keys(SETTINGS_SECTIONS), ["general", "chat", "agent", "appearance", "graph", "models"]);
});
});
describe("parseModelsSettingsBody (model slots wave, 2026-09-20)", () => {
it("parses the three slot tiers — model required, level rides (BYOK, owner 2026-09-22)", () => {
const r = parseModelsSettingsBody({
strong: { model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" } },
cheap: { model: { provider: "zai", id: "glm-5.3-flash" }, thinkingLevel: "high" },
vision: { model: { provider: "zai", id: "glm-5.3-flash" }, thinkingLevel: "low" },
});
assert.ok(r.ok);
assert.deepEqual(r.settings, {
strong: { model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" } },
cheap: { model: { provider: "zai", id: "glm-5.3-flash" }, thinkingLevel: "high" },
vision: { model: { provider: "zai", id: "glm-5.3-flash" }, thinkingLevel: "low" },
});
// a thinkingLevel WITHOUT a model can never run (no factory floor)
const levelOnly = parseModelsSettingsBody({ cheap: { thinkingLevel: "high" } });
assert.equal(levelOnly.ok, false);
if (!levelOnly.ok) {
assert.match(levelOnly.error, /slot "cheap" must set a model/);
}
});
it("{} is the legal clear-slots write", () => {
const r = parseModelsSettingsBody({});
assert.ok(r.ok);
assert.deepEqual(r.settings, {});
});
it("unknown slots are 400s with the supported list", () => {
const bad = parseModelsSettingsBody({ mega: { thinkingLevel: "high" } });
assert.equal(bad.ok, false);
if (!bad.ok) assert.match(bad.error, /unknown slot "mega" \(supported: strong, cheap, vision\)/);
});
it("a present-but-empty slot object is rejected (absent slot = unset)", () => {
const bad = parseModelsSettingsBody({ strong: {} });
assert.equal(bad.ok, false);
if (!bad.ok) assert.match(bad.error, /slot "strong" must set a model/);
});
it("malformed slot shapes are 400s", () => {
assert.equal(parseModelsSettingsBody({ cheap: { model: { provider: "x" } } }).ok, false);
assert.equal(parseModelsSettingsBody({ vision: { thinkingLevel: 7 } }).ok, false);
assert.equal(parseModelsSettingsBody("nope").ok, false);
});
});