DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
test(web): cover the settings store and memory-graph helpers (#1710)
evan188199-techca2241aa7b23
diff --git a/web/tests/lib/memory-graph.test.ts b/web/tests/lib/memory-graph.test.tsnew file mode 100644index 000000000..03eb1f7d8--- /dev/null+++ b/web/tests/lib/memory-graph.test.ts@@ -0,0 +1,431 @@+import assert from 'node:assert/strict'+import test from 'node:test'++import {+ DEFAULT_LAYOUT,+ L3_SLOTS,+ SURFACES,+ buildGraph,+ parseDoc,+ splitRef,+} from '../../lib/memory-graph'+import type {+ L1Entity,+ L3Slot,+ ParsedDoc,+ ParsedEntry,+ RawMemorySnapshot,+ Surface,+} from '../../lib/memory-graph'++// Entry ULIDs use the Crockford base32 subset [0-9A-HJKMNP-TV-Z]; tags are+// padded with "0" so every generated id satisfies the 26-char ENTRY_ID shape.+const entryId = (tag: string): string => `m_${tag}${'0'.repeat(26 - tag.length)}`++const entity = (id: string, label = id): L1Entity => ({+ id,+ label,+ ts: '2026-10-03T00:00:00Z',+ content: `content of ${label}`,+})++const emptySurfaceEntries = (): Record<Surface, ParsedEntry[]> => {+ const out = {} as Record<Surface, ParsedEntry[]>+ for (const s of SURFACES) out[s] = []+ return out+}++const emptyDocs = (): Record<Surface, ParsedDoc> => {+ const out = {} as Record<Surface, ParsedDoc>+ for (const s of SURFACES) out[s] = { title: '', entries: [] }+ return out+}++const emptySlotDocs = (): Record<L3Slot, ParsedDoc> => {+ const out = {} as Record<L3Slot, ParsedDoc>+ for (const slot of L3_SLOTS) out[slot] = { title: '', entries: [] }+ return out+}++const emptyL1 = (): Record<Surface, L1Entity[]> => {+ const out = {} as Record<Surface, L1Entity[]>+ for (const s of SURFACES) out[s] = []+ return out+}++const emptySnapshot = (): RawMemorySnapshot => ({+ l1: emptyL1(),+ l2: emptyDocs(),+ l3: emptySlotDocs(),+})++const parseLines = (...lines: string[]): ParsedDoc => parseDoc(lines.join('\n'))++// ── parseDoc ──────────────────────────────────────────────────────────++test('parseDoc: empty content yields an empty document', () => {+ assert.deepEqual(parseDoc(''), { title: '', entries: [] })+})++test('parseDoc: whitespace-only content yields no entries and no title', () => {+ const doc = parseDoc('\n\n \n\t\n\r\n')+ assert.equal(doc.title, '')+ assert.deepEqual(doc.entries, [])+})++test('parseDoc: plain prose without bullets or footnotes is ignored', () => {+ const doc = parseDoc('Some intro text\n1. a numbered item\n> a quote\n---')+ assert.equal(doc.title, '')+ assert.deepEqual(doc.entries, [])+})++test('parseDoc: first H1 wins as title and later H1s are ignored', () => {+ const doc = parseLines('# First title', '# Second title', 'trailing prose')+ assert.equal(doc.title, 'First title')+ assert.deepEqual(doc.entries, [])+})++test('parseDoc: section headings annotate subsequent new-layout entries', () => {+ const id = entryId('A1')+ const doc = parseLines(+ '# Memory',+ '## Chat',+ `- asked about transformers [^1] <!--${id}-->`,+ '[^1]: chat:c_1'+ )+ assert.equal(doc.title, 'Memory')+ assert.equal(doc.entries.length, 1)+ assert.deepEqual(doc.entries[0], {+ id,+ section: 'Chat',+ text: 'asked about transformers',+ refs: ['chat:c_1'],+ })+})++test('parseDoc: multiple trailing markers resolve via the footnote table without duplicates', () => {+ const id = entryId('B2')+ const doc = parseLines(+ `- planned review [^1], [^2], [^1] <!--${id}-->`,+ '[^1]: chat:u_1',+ '[^2]: notebook:n_1'+ )+ assert.equal(doc.entries.length, 1)+ assert.deepEqual(doc.entries[0].refs, ['chat:u_1', 'notebook:n_1'])+})++test('parseDoc: footnote may appear after the bullet it resolves', () => {+ const id = entryId('C3')+ const doc = parseLines(`- deferred note [^1] <!--${id}-->`, '', '[^1]: kb:kb_9')+ assert.deepEqual(doc.entries[0].refs, ['kb:kb_9'])+})++test('parseDoc: legacy bullet with comma-separated footnote refs', () => {+ const id = entryId('D4')+ const doc = parseLines('## Quiz', `- legacy entry [^${id}]`, `[^${id}]: quiz:u_1, notebook:n_2`)+ assert.equal(doc.title, '')+ assert.deepEqual(doc.entries, [+ { id, section: 'Quiz', text: 'legacy entry', refs: ['quiz:u_1', 'notebook:n_2'] },+ ])+})++test('parseDoc: empty footnote payload resolves to no refs (marker dropped)', () => {+ const id = entryId('E5')+ // A degenerate footnote "[^1]:" stores [""]; the resolver filters empty+ // strings, so the entry ends up with no refs instead of a stub — the+ // same outcome as a legacy bullet whose footnote table is missing.+ const doc = parseLines(`- orphan marker [^1] <!--${id}-->`, '[^1]:')+ assert.deepEqual(doc.entries[0].refs, [])+})++test('parseDoc: unknown non-ULID label without a footnote is preserved as a stub', () => {+ const id = entryId('F6')+ const doc = parseLines(`- ghost marker [^ghost] <!--${id}-->`)+ assert.deepEqual(doc.entries[0].refs, ['ghost'])+})++test('parseDoc: legacy bullet with no footnote drops the unresolved entry id', () => {+ const id = entryId('G7')+ const doc = parseLines(`- unbacked legacy [^${id}]`)+ assert.equal(doc.entries.length, 1)+ assert.deepEqual(doc.entries[0].refs, [])+})++test('parseDoc: malformed bullet-like lines are skipped without throwing', () => {+ const doc = parseLines(+ '- a plain bullet with no marker',+ '- text [^short] with inline junk',+ '#### an h4 heading',+ '- text [^1] <!--truncated-marker-->',+ '[^1]: chat:c_1'+ )+ assert.deepEqual(doc.entries, [])+})++test('parseDoc: CRLF line endings are handled', () => {+ const id = entryId('H8')+ const doc = parseDoc(+ [`# Titled`, `- crlf entry [^1] <!--${id}-->`, `[^1]: book:b_1`].join('\r\n')+ )+ assert.equal(doc.title, 'Titled')+ assert.equal(doc.entries.length, 1)+ assert.equal(doc.entries[0].text, 'crlf entry')+ assert.deepEqual(doc.entries[0].refs, ['book:b_1'])+})++// ── splitRef ──────────────────────────────────────────────────────────++test('splitRef: splits a well-formed ref into surface and entity id', () => {+ assert.deepEqual(splitRef('chat:c_1'), { surface: 'chat', entityId: 'c_1' })+})++test('splitRef: keeps additional colons inside the entity id', () => {+ assert.deepEqual(splitRef('quiz:unified_x:q_1'), {+ surface: 'quiz',+ entityId: 'unified_x:q_1',+ })+})++test('splitRef: a bare surface with no colon yields an empty entity id', () => {+ assert.deepEqual(splitRef('chat'), { surface: 'chat', entityId: '' })+})++test('splitRef: empty ref yields empty surface and entity id', () => {+ assert.deepEqual(splitRef(''), { surface: '', entityId: '' })+})++test('splitRef: leading colon yields an empty surface', () => {+ assert.deepEqual(splitRef(':orphan'), { surface: '', entityId: 'orphan' })+})++test('splitRef: trailing colon yields an empty entity id', () => {+ assert.deepEqual(splitRef('chat:'), { surface: 'chat', entityId: '' })+})++// ── buildGraph ────────────────────────────────────────────────────────++test('buildGraph: empty snapshot produces anchors and clusters but no edges', () => {+ const graph = buildGraph(emptySnapshot())++ assert.equal(graph.edges.length, 0)+ assert.equal(graph.adjacency.size, 0)+ assert.equal(+ graph.clusters.length,+ L3_SLOTS.length + SURFACES.length * 2,+ 'one L3 slot cluster plus an L2 and L1 cluster per surface'+ )+ // Synthetic hidden anchors exist for every surface even with no data.+ const anchors = graph.nodes.filter(n => n.id.endsWith(':__anchor__'))+ assert.equal(anchors.length, SURFACES.length)+ for (const anchor of anchors) {+ assert.equal(anchor.layer, 'L2')+ assert.equal(anchor.r, 0, 'anchors are hidden from hit-testing')+ assert.equal(+ graph.nodeCluster.get(anchor.id),+ `L2:${anchor.id.split(':')[1]}`,+ "anchors belong to their surface's L2 cluster"+ )+ }+ for (const cluster of graph.clusters) {+ assert.equal(cluster.count, 0)+ assert.ok(cluster.endAngle > cluster.startAngle, 'even empty slices stay labelable')+ }+})++test('buildGraph: cluster slices tile the full circle for every ring', () => {+ const graph = buildGraph(emptySnapshot())+ for (const layer of ['L1', 'L2', 'L3'] as const) {+ const gap = layer === 'L3' ? DEFAULT_LAYOUT.l3ClusterGap : DEFAULT_LAYOUT.clusterGap+ const total = graph.clusters+ .filter(c => c.layer === layer)+ .reduce((acc, c) => acc + (c.endAngle - c.startAngle + gap), 0)+ assert.ok(Math.abs(total - 2 * Math.PI) < 1e-9, `${layer} spans should cover 2π, got ${total}`)+ }+ assert.equal(new Set(graph.clusters.map(c => c.id)).size, graph.clusters.length)+})++test('buildGraph: L2 entry citing an L1 entity on another surface yields a strong cross-surface edge', () => {+ const snap = emptySnapshot()+ snap.l1.chat = [entity('c_1', 'Chat message')]+ const nbEntry = entryId('NB1')+ snap.l2.notebook = parseLines(+ '## Session',+ `- discussed the chat thread [^1] <!--${nbEntry}-->`,+ '[^1]: chat:c_1'+ )++ const graph = buildGraph(snap)+ const expected = {+ source: `L2:notebook:${nbEntry}`,+ target: 'L1:chat:c_1',+ kind: 'strong' as const,+ }+ assert.deepEqual(graph.edges, [expected])++ // Adjacency is symmetric and both endpoints are real nodes mapped to+ // their own clusters.+ for (const edge of graph.edges) {+ assert.ok(graph.adjacency.get(edge.source)?.includes(edge.target))+ assert.ok(graph.adjacency.get(edge.target)?.includes(edge.source))+ assert.ok(graph.nodes.some(n => n.id === edge.source))+ assert.ok(graph.nodes.some(n => n.id === edge.target))+ }+ assert.equal(graph.nodeCluster.get(expected.source), 'L2:notebook')+ assert.equal(graph.nodeCluster.get(expected.target), 'L1:chat')+ assert.equal(graph.clusters.find(c => c.id === 'L1:chat')?.count, 1)+})++test('buildGraph: L3 entry citing a bare surface yields a soft edge to that surface anchor', () => {+ const snap = emptySnapshot()+ const pEntry = entryId('P01')+ snap.l3.profile = parseLines(`- the learner lives in chat [^1] <!--${pEntry}-->`, '[^1]: chat')++ const graph = buildGraph(snap)+ assert.deepEqual(graph.edges, [+ { source: `L3:profile:${pEntry}`, target: 'L2:chat:__anchor__', kind: 'soft' },+ ])+ const anchor = graph.nodes.find(n => n.id === 'L2:chat:__anchor__')+ assert.ok(anchor, 'anchor node exists')+})++test('buildGraph: L3 entry citing a specific L2 entry id yields a strong edge', () => {+ const snap = emptySnapshot()+ const chatEntry = entryId('CM01')+ snap.l2.chat = parseLines(`- a chat consolidation [^1] <!--${chatEntry}-->`, '[^1]: chat:c_1')+ const rEntry = entryId('R001')+ snap.l3.recent = parseLines(+ `- revisit that consolidation [^1] <!--${rEntry}-->`,+ `[^1]: chat:${chatEntry}`+ )++ const graph = buildGraph(snap)+ assert.deepEqual(graph.edges, [+ {+ source: `L3:recent:${rEntry}`,+ target: `L2:chat:${chatEntry}`,+ kind: 'strong',+ },+ ])+})++test('buildGraph: duplicate surface citations from one L3 entry produce a single soft edge', () => {+ const snap = emptySnapshot()+ const entry: ParsedEntry = {+ id: entryId('DD01'),+ section: '',+ text: 'duplicated citation',+ refs: ['chat', 'chat'],+ }+ snap.l3.scope = { title: '', entries: [entry] }++ const graph = buildGraph(snap)+ assert.equal(graph.edges.length, 1)+ assert.equal(graph.edges[0].kind, 'soft')+ assert.equal(graph.edges[0].target, 'L2:chat:__anchor__')+})++test('buildGraph: invalid refs produce no edges and never throw', () => {+ const snap = emptySnapshot()+ snap.l1.chat = [entity('c_1')]+ const badEntry: ParsedEntry = {+ id: entryId('BAD1'),+ section: '',+ text: 'bad citations',+ // surface-only ref, unknown surface, and unknown L1 entity id+ refs: ['chat', 'bogus:x', 'notebook:n_ghost'],+ }+ snap.l2.notebook = { title: '', entries: [badEntry] }+ const badL3: ParsedEntry = {+ id: entryId('BAD2'),+ section: '',+ text: 'bad citations',+ refs: ['bogus', 'chat:m_MISSING'],+ }+ snap.l3.recent = { title: '', entries: [badL3] }++ const graph = buildGraph(snap)+ assert.deepEqual(graph.edges, [])+ assert.equal(graph.adjacency.size, 0)+})++test('buildGraph: duplicate L1 entity ids are deduped per surface', () => {+ const snap = emptySnapshot()+ snap.l1.quiz = [entity('u_1'), entity('u_1'), entity('u_2')]++ const graph = buildGraph(snap)+ const quizL1 = graph.nodes.filter(n => n.id.startsWith('L1:quiz:'))+ assert.equal(quizL1.length, 2)+ assert.equal(+ quizL1.filter(n => n.id === 'L1:quiz:u_1').length,+ 1,+ 'repeated snapshot rows for the same question id collapse into one node'+ )+ assert.equal(graph.clusters.find(c => c.id === 'L1:quiz')?.count, 2)+})++test('buildGraph: duplicate L2 entry ids are deduped per surface', () => {+ const snap = emptySnapshot()+ const id = entryId('DUP1')+ snap.l2.book = {+ title: '',+ entries: [+ { id, section: 'Ch.1', text: 'first', refs: [] },+ { id, section: 'Ch.2', text: 'second', refs: [] },+ ],+ }++ const graph = buildGraph(snap)+ const bookL2 = graph.nodes.filter(n => n.id === `L2:book:${id}`)+ assert.equal(bookL2.length, 1)+ assert.equal(graph.clusters.find(c => c.id === 'L2:book')?.count, 1)+})++test('buildGraph: custom layout options move the geometry center', () => {+ const opts = { ...DEFAULT_LAYOUT, width: 600, height: 400 }+ const graph = buildGraph(emptySnapshot(), opts)++ const anchor = graph.nodes.find(n => n.id === 'L2:chat:__anchor__')+ assert.ok(anchor)+ const midRingRadius = (opts.l2InnerRadius + opts.l2OuterRadius) / 2+ const distance = Math.hypot(anchor.x - opts.width / 2, anchor.y - opts.height / 2)+ assert.ok(+ Math.abs(distance - midRingRadius) < 1e-6,+ `anchor should sit on the L2 mid ring at radius ${midRingRadius}, got ${distance}`+ )+})++test('buildGraph: every node is placed inside the canvas and bookkeeping maps stay consistent', () => {+ const snap = emptySnapshot()+ snap.l1.chat = [entity('c_1'), entity('c_2')]+ snap.l1.notebook = [entity('n_1')]+ const nbEntry = entryId('NB01')+ snap.l2.notebook = parseLines(`- cites chat [^1] <!--${nbEntry}-->`, '[^1]: chat:c_1')+ const pEntry = entryId('P001')+ snap.l3.profile = parseLines(`- cites notebook [^1] <!--${pEntry}-->`, '[^1]: notebook')++ const graph = buildGraph(snap)+ const maxRadius = DEFAULT_LAYOUT.l1OuterRadius + 20++ assert.equal(new Set(graph.nodes.map(n => n.id)).size, graph.nodes.length, 'node ids are unique')+ for (const node of graph.nodes) {+ assert.ok(+ Number.isFinite(node.x) && Number.isFinite(node.y),+ `${node.id} has finite coordinates`+ )+ const distance = Math.hypot(+ node.x - DEFAULT_LAYOUT.width / 2,+ node.y - DEFAULT_LAYOUT.height / 2+ )+ assert.ok(distance <= maxRadius, `${node.id} sits inside the outer ring`)+ assert.ok(graph.nodeCluster.has(node.id), `${node.id} is mapped to a cluster`)+ const clusterIds = new Set(graph.clusters.map(c => c.id))+ assert.ok(clusterIds.has(node.cluster), `${node.id} references a real cluster`)+ }+ for (const edge of graph.edges) {+ assert.ok(graph.adjacency.get(edge.source)?.includes(edge.target))+ assert.ok(graph.adjacency.get(edge.target)?.includes(edge.source))+ }+ // L1 nodes carry a navigation href scoped to their surface.+ const chatNode = graph.nodes.find(n => n.id === 'L1:chat:c_1')+ assert.ok(chatNode?.href.includes('surface=chat'))+})diff --git a/web/tests/settings/settings-store.test.ts b/web/tests/settings/settings-store.test.tsnew file mode 100644index 000000000..0e5000cee--- /dev/null+++ b/web/tests/settings/settings-store.test.ts@@ -0,0 +1,655 @@+import test from 'node:test'+import assert from 'node:assert/strict'++let mockLocalStorage: Record<string, string> = {}+let mockSessionStorage: Record<string, string> = {}+let dispatchedEvents: Array<{ type: string; detail: unknown }> = []++function mockWindow() {+ global.window = {+ location: { search: '', origin: 'http://localhost' },+ localStorage: {+ getItem: (key: string) => mockLocalStorage[key] ?? null,+ setItem: (key: string, value: string) => {+ mockLocalStorage[key] = value+ },+ removeItem: (key: string) => {+ delete mockLocalStorage[key]+ },+ },+ sessionStorage: {+ getItem: (key: string) => mockSessionStorage[key] ?? null,+ setItem: (key: string, value: string) => {+ mockSessionStorage[key] = value+ },+ removeItem: (key: string) => {+ delete mockSessionStorage[key]+ },+ },+ dispatchEvent: (event: { type: string; detail?: unknown }) => {+ dispatchedEvents.push({ type: event.type, detail: event.detail })+ return true+ },+ addEventListener: () => {},+ removeEventListener: () => {},+ } as unknown as typeof globalThis.window+}++mockWindow()++import * as settingsContext from '../../features/settings/store/SettingsStore'+import {+ CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY,+ CODE_BLOCK_SETTINGS_EVENT,+ CODE_BLOCK_THEME_STORAGE_KEY,+ CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY,+ DEFAULT_CODE_BLOCK_THEME,+ isResponseLanguage,+ readStoredCodeBlockShowLineNumbers,+ readStoredCodeBlockTheme,+ readStoredCodeBlockWrapLongLines,+} from '../../context/app-shell-storage'+import type {+ Catalog,+ CatalogProfile,+ DiagnosticsResult,+ ServiceName,+ UiSettings,+} from '../../features/settings/store/SettingsStore'++// ─── Fixtures ──────────────────────────────────────────────────────────────++const VALID_UI: UiSettings = {+ theme: 'dark',+ language: 'en',+ response_language: 'en',+ code_block_theme: 'dracula',+ code_block_show_line_numbers: true,+ code_block_wrap_long_lines: true,+}++function emptyService() {+ return { active_profile_id: null, active_model_id: null, profiles: [] }+}++/** Minimal catalog: llm active with two models, search with one provider. */+function makeCatalog(): Catalog {+ return {+ version: 1,+ connections: [],+ services: {+ llm: {+ active_profile_id: 'p1',+ active_model_id: 'm1',+ profiles: [+ {+ id: 'p1',+ name: 'OpenAI',+ binding: 'openai',+ base_url: 'https://api.openai.com/v1',+ api_key: 'sk-test',+ api_version: '',+ extra_headers: {},+ wire_api: 'auto',+ models: [+ { id: 'm1', name: 'M1', model: 'gpt-test' },+ { id: 'm2', name: 'M2', model: 'gpt-other' },+ ],+ },+ ],+ },+ search: {+ active_profile_id: 's1',+ profiles: [+ {+ id: 's1',+ name: 'Brave',+ provider: 'brave',+ base_url: '',+ api_key: 'bsk-test',+ api_version: '',+ models: [],+ },+ ],+ },+ task: emptyService(),+ embedding: emptyService(),+ tts: emptyService(),+ stt: emptyService(),+ imagegen: emptyService(),+ videogen: emptyService(),+ },+ }+}++function diagnostics(overrides: Partial<DiagnosticsResult>): DiagnosticsResult {+ return {+ state: 'success',+ message: 'ok',+ profileId: 'p1',+ modelId: 'm1',+ ...overrides,+ }+}++/** Fetch recorder for persistUiSettingsPatch. */+function recordingFetcher() {+ const calls: Array<{+ input: RequestInfo | URL+ init?: RequestInit+ }> = []+ const fetcher = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {+ calls.push({ input, init })+ return { ok: true } as Response+ }+ return { calls, fetcher }+}++function resetStorage() {+ mockLocalStorage = {}+ mockSessionStorage = {}+ dispatchedEvents = []+ mockWindow()+}++// ─── 验收 1a:patch 后本地存储与 store 同步 ─────────────────────────────────++test('sync writes backend-loaded code-block values into app-shell storage and returns the normalized values', () => {+ resetStorage()++ const normalized = settingsContext.syncLoadedCodeBlockSettingsToAppShell({+ code_block_theme: 'dracula',+ code_block_show_line_numbers: true,+ code_block_wrap_long_lines: true,+ })++ assert.deepEqual(normalized, {+ code_block_theme: 'dracula',+ code_block_show_line_numbers: true,+ code_block_wrap_long_lines: true,+ })+ // The app-shell storage mirrors exactly what the backend sent.+ assert.equal(readStoredCodeBlockTheme(), 'dracula')+ assert.equal(readStoredCodeBlockShowLineNumbers(), true)+ assert.equal(readStoredCodeBlockWrapLongLines(), true)+ assert.equal(+ mockLocalStorage[CODE_BLOCK_THEME_STORAGE_KEY],+ 'dracula',+ 'raw theme key should hold the theme id'+ )+ assert.equal(+ mockLocalStorage[CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY],+ 'true',+ 'raw switch keys are stored as strings'+ )+ assert.equal(mockLocalStorage[CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY], 'true')+})++test('sync announces every code-block value through the app-shell settings event', () => {+ resetStorage()++ settingsContext.syncLoadedCodeBlockSettingsToAppShell({+ code_block_theme: 'github',+ code_block_show_line_numbers: false,+ code_block_wrap_long_lines: true,+ })++ const events = dispatchedEvents.filter(event => event.type === CODE_BLOCK_SETTINGS_EVENT)+ assert.equal(events.length, 3, 'one event per written code-block setting')+ assert.deepEqual(+ events.map(event => event.detail),+ [+ { codeBlockTheme: 'github' },+ { codeBlockShowLineNumbers: false },+ { codeBlockWrapLongLines: true },+ ]+ )+})++test('sync makes backend values win over stale local ones', () => {+ resetStorage()+ mockLocalStorage = {+ [CODE_BLOCK_THEME_STORAGE_KEY]: 'oneDark',+ [CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY]: 'false',+ [CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY]: 'false',+ }++ const normalized = settingsContext.syncLoadedCodeBlockSettingsToAppShell({+ code_block_theme: 'dracula',+ code_block_show_line_numbers: true,+ code_block_wrap_long_lines: true,+ })++ assert.deepEqual(normalized, {+ code_block_theme: 'dracula',+ code_block_show_line_numbers: true,+ code_block_wrap_long_lines: true,+ })+ assert.equal(readStoredCodeBlockTheme(), 'dracula')+ assert.equal(readStoredCodeBlockShowLineNumbers(), true)+ assert.equal(readStoredCodeBlockWrapLongLines(), true)+})++test('persistUiSettingsPatch PUTs the patch to the workspace-scoped ui endpoint', async () => {+ const { calls, fetcher } = recordingFetcher()++ await settingsContext.persistUiSettingsPatch({ code_block_theme: 'dracula' }, fetcher)++ assert.equal(calls.length, 1)+ const { input, init } = calls[0]+ assert.match(String(input), /\/api\/settings\/ui\?dt_workspace=$/)+ assert.equal(init?.method, 'PUT')+ assert.equal((init?.headers as Record<string, string>)['Content-Type'], 'application/json')+ assert.deepEqual(JSON.parse(String(init?.body)), {+ code_block_theme: 'dracula',+ })+})++test('persistUiSettingsPatch sends a full ui patch verbatim — no field rewriting', async () => {+ const { calls, fetcher } = recordingFetcher()+ const patch: Partial<UiSettings> = {+ theme: 'glass',+ language: 'zh',+ response_language: 'zh',+ code_block_theme: 'oneDark',+ code_block_show_line_numbers: false,+ code_block_wrap_long_lines: true,+ }++ await settingsContext.persistUiSettingsPatch(patch, fetcher)++ assert.deepEqual(JSON.parse(String(calls[0].init?.body)), patch)+})++test('persistUiSettingsPatch surfaces fetch failures instead of swallowing them', async () => {+ await assert.rejects(+ settingsContext.persistUiSettingsPatch({ theme: 'dark' }, async () => {+ throw new Error('network down')+ }),+ /network down/+ )+})++// ─── 验收 1b:非法值归一化不抛错 ────────────────────────────────────────────++test('sync on an empty patch falls back to defaults without throwing', () => {+ resetStorage()++ const normalized = settingsContext.syncLoadedCodeBlockSettingsToAppShell({})++ assert.deepEqual(normalized, {+ code_block_theme: DEFAULT_CODE_BLOCK_THEME,+ code_block_show_line_numbers: false,+ code_block_wrap_long_lines: false,+ })+ assert.equal(readStoredCodeBlockTheme(), DEFAULT_CODE_BLOCK_THEME)+})++test('sync normalizes null, empty and whitespace-only themes to the default', () => {+ for (const theme of [null, undefined, '', ' '] as Array<string | null | undefined>) {+ resetStorage()+ const normalized = settingsContext.syncLoadedCodeBlockSettingsToAppShell({+ code_block_theme: theme as UiSettings['code_block_theme'],+ })+ assert.equal(+ normalized.code_block_theme,+ DEFAULT_CODE_BLOCK_THEME,+ `theme ${JSON.stringify(theme)} should normalize to the default`+ )+ assert.equal(readStoredCodeBlockTheme(), DEFAULT_CODE_BLOCK_THEME)+ }+})++test('sync trims surrounding whitespace from a valid theme id', () => {+ resetStorage()++ const normalized = settingsContext.syncLoadedCodeBlockSettingsToAppShell({+ code_block_theme: ' dracula ',+ })++ assert.equal(normalized.code_block_theme, 'dracula')+ assert.equal(readStoredCodeBlockTheme(), 'dracula')+})++test("sync accepts case-insensitive string 'true' for both switches", () => {+ resetStorage()++ const normalized = settingsContext.syncLoadedCodeBlockSettingsToAppShell({+ code_block_show_line_numbers: 'True',+ code_block_wrap_long_lines: 'TRUE',+ } as unknown as Partial<UiSettings>)++ assert.equal(normalized.code_block_show_line_numbers, true)+ assert.equal(normalized.code_block_wrap_long_lines, true)+ assert.equal(readStoredCodeBlockShowLineNumbers(), true)+ assert.equal(readStoredCodeBlockWrapLongLines(), true)+})++test('sync keeps an explicit boolean false — it must never be coerced to true', () => {+ resetStorage()++ const normalized = settingsContext.syncLoadedCodeBlockSettingsToAppShell({+ code_block_show_line_numbers: false,+ code_block_wrap_long_lines: false,+ })++ assert.equal(normalized.code_block_show_line_numbers, false)+ assert.equal(normalized.code_block_wrap_long_lines, false)+ assert.equal(readStoredCodeBlockShowLineNumbers(), false)+ assert.equal(readStoredCodeBlockWrapLongLines(), false)+})++test('sync maps missing and non-truthy garbage switch values to false without throwing', () => {+ const garbage: Array<unknown> = [+ undefined,+ null,+ 0,+ 1,+ 'false',+ 'False',+ 'yes',+ 'on',+ '1',+ {},+ [],+ ]+ for (const value of garbage) {+ resetStorage()+ const normalized = settingsContext.syncLoadedCodeBlockSettingsToAppShell({+ code_block_show_line_numbers: value as UiSettings['code_block_show_line_numbers'],+ code_block_wrap_long_lines: value as UiSettings['code_block_wrap_long_lines'],+ })+ assert.equal(+ normalized.code_block_show_line_numbers,+ false,+ `line-numbers value ${JSON.stringify(value)} should normalize to false`+ )+ assert.equal(+ normalized.code_block_wrap_long_lines,+ false,+ `wrap-lines value ${JSON.stringify(value)} should normalize to false`+ )+ assert.equal(readStoredCodeBlockShowLineNumbers(), false)+ assert.equal(readStoredCodeBlockWrapLongLines(), false)+ }+})++test("sync stringifies any value whose String() form is 'true' to boolean true", () => {+ // Documented contract: the check is `=== true || String(v).toLowerCase() === "true"`,+ // so anything coercing to the string "true" (case-insensitive) counts as on.+ resetStorage()++ const normalized = settingsContext.syncLoadedCodeBlockSettingsToAppShell({+ code_block_show_line_numbers: ['true'],+ code_block_wrap_long_lines: { toString: () => 'TRUE' },+ } as unknown as Partial<UiSettings>)++ assert.equal(normalized.code_block_show_line_numbers, true)+ assert.equal(normalized.code_block_wrap_long_lines, true)+ assert.equal(readStoredCodeBlockShowLineNumbers(), true)+ assert.equal(readStoredCodeBlockWrapLongLines(), true)+})++test('sync with a fully garbage patch still persists an internally consistent state', () => {+ resetStorage()++ const normalized = settingsContext.syncLoadedCodeBlockSettingsToAppShell({+ code_block_theme: null,+ code_block_show_line_numbers: {+ unexpected: 'field',+ } as unknown as UiSettings['code_block_show_line_numbers'],+ code_block_wrap_long_lines: 42 as unknown as UiSettings['code_block_wrap_long_lines'],+ } as unknown as Partial<UiSettings>)++ // Whatever was normalized is exactly what was persisted — storage can+ // never disagree with the store after a sync, even for garbage input.+ assert.equal(normalized.code_block_theme, DEFAULT_CODE_BLOCK_THEME)+ assert.equal(normalized.code_block_show_line_numbers, false)+ assert.equal(normalized.code_block_wrap_long_lines, false)+ assert.equal(+ mockLocalStorage[CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY],+ String(normalized.code_block_show_line_numbers)+ )+ assert.equal(+ mockLocalStorage[CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY],+ String(normalized.code_block_wrap_long_lines)+ )+ assert.equal(readStoredCodeBlockShowLineNumbers(), normalized.code_block_show_line_numbers)+ assert.equal(readStoredCodeBlockWrapLongLines(), normalized.code_block_wrap_long_lines)+})++test('sync without a window object is a no-op, not a crash (SSR guard)', () => {+ const originalWindow = global.window+ // @ts-expect-error -- simulating server-side rendering+ global.window = undefined+ try {+ assert.doesNotThrow(() =>+ settingsContext.syncLoadedCodeBlockSettingsToAppShell({+ code_block_theme: 'dracula',+ code_block_show_line_numbers: true,+ })+ )+ } finally {+ global.window = originalWindow+ }+})++// ─── 验收 2:常量与纯函数契约(修复卡可直接领取的夹具) ─────────────────────++test('TOUR_STEPS walk the settings navigator in order with complete steps', () => {+ const steps = settingsContext.TOUR_STEPS++ assert.ok(steps.length > 0, 'the tour must have steps')+ for (const step of steps) {+ assert.ok(step.target.length > 0, 'every step names a data-tour target')+ assert.ok(step.route.startsWith('/settings/'), 'tour steps live under /settings')+ assert.ok(step.titleKey.length > 0, 'every step has a title i18n key')+ assert.ok(step.descKey.length > 0, 'every step has a description i18n key')+ }+ const routes = steps.map(step => step.route)+ assert.equal(new Set(routes).size, routes.length, 'no duplicated tour routes')+ // The tour is ordered: status first, then navigator pages.+ assert.deepEqual(routes, [+ '/settings/status',+ '/settings/appearance',+ '/settings/network',+ '/settings/llm',+ '/settings/knowledge',+ '/settings/starters',+ '/settings/memory',+ ])+})++test('RESPONSE_LANGUAGE_OPTIONS covers the supported response languages exactly once each', () => {+ const options = settingsContext.RESPONSE_LANGUAGE_OPTIONS++ assert.equal(options.length, 15)+ const values = options.map(option => option.value)+ assert.equal(new Set(values).size, values.length, 'no duplicated codes')+ for (const value of values) {+ assert.ok(isResponseLanguage(value), `${value} must be a valid ResponseLanguage`)+ assert.ok(option_label(options, value).length > 0, 'every option has a label')+ }+ assert.equal(values[0], 'en', 'English is the first option')+ for (const expected of ['zh', 'zh-tw', 'ja', 'ar', 'ms']) {+ assert.ok(values.includes(expected as (typeof values)[number]))+ }+})++function option_label(+ options: ReadonlyArray<{ value: string; label: string }>,+ value: string+): string {+ return options.find(option => option.value === value)!.label+}++test('defaultCatalog has one bucket per service and is fresh on every call', () => {+ const catalog = settingsContext.defaultCatalog()++ assert.deepEqual(Object.keys(catalog.services).sort(), [+ 'embedding',+ 'imagegen',+ 'llm',+ 'search',+ 'stt',+ 'task',+ 'tts',+ 'videogen',+ ])+ for (const [service, bucket] of Object.entries(catalog.services)) {+ assert.deepEqual(bucket.profiles, [], `${service} starts without profiles`)+ assert.equal(bucket.active_profile_id, null)+ if (service !== 'search') {+ assert.equal(bucket.active_model_id, null)+ }+ }+ // Mutating one instance must not leak into the next.+ catalog.services.llm.profiles.push({ id: 'x' } as CatalogProfile)+ assert.equal(settingsContext.defaultCatalog().services.llm.profiles.length, 0)+})++test('cloneCatalog returns a deep copy — edits to the clone never reach the source', () => {+ const source = makeCatalog()+ const clone = settingsContext.cloneCatalog(source)++ assert.notEqual(clone, source)+ assert.deepEqual(clone, source)+ clone.services.llm.active_model_id = 'm2'+ clone.services.llm.profiles[0].models.pop()+ clone.services.llm.profiles[0].extra_headers = { injected: 'yes' }+ assert.equal(source.services.llm.active_model_id, 'm1')+ assert.equal(source.services.llm.profiles[0].models.length, 2)+ assert.deepEqual(source.services.llm.profiles[0].extra_headers, {})+})++test('voiceService and generationService classify audio and media services', () => {+ assert.equal(settingsContext.voiceService('tts'), true)+ assert.equal(settingsContext.voiceService('stt'), true)+ assert.equal(settingsContext.voiceService('llm'), false)+ assert.equal(settingsContext.generationService('imagegen'), true)+ assert.equal(settingsContext.generationService('videogen'), true)+ assert.equal(settingsContext.generationService('llm'), false)+ assert.equal(settingsContext.generationService('tts'), false)+})++test('getActiveProfile resolves the active profile, falls back to the first, else null', () => {+ const catalog = makeCatalog()++ assert.equal(settingsContext.getActiveProfile(catalog, 'llm')?.id, 'p1')+ // No active id: fall back to the first profile.+ catalog.services.llm.active_profile_id = null+ assert.equal(settingsContext.getActiveProfile(catalog, 'llm')?.id, 'p1')+ // Unknown active id still falls back rather than returning nothing.+ catalog.services.llm.active_profile_id = 'missing'+ assert.equal(settingsContext.getActiveProfile(catalog, 'llm')?.id, 'p1')+ // Empty bucket.+ assert.equal(settingsContext.getActiveProfile(catalog, 'embedding'), null)+})++test('getActiveModel resolves the active model with the same fallback chain; search has no models', () => {+ const catalog = makeCatalog()++ assert.equal(settingsContext.getActiveModel(catalog, 'llm')?.id, 'm1')+ assert.equal(settingsContext.getActiveModel(catalog, 'search'), null)++ // active_model_id not set: first model wins.+ catalog.services.llm.active_model_id = null+ assert.equal(settingsContext.getActiveModel(catalog, 'llm')?.id, 'm1')+ // active_model_id stale: first model wins over an unknown id.+ catalog.services.llm.active_model_id = 'gone'+ assert.equal(settingsContext.getActiveModel(catalog, 'llm')?.id, 'm1')+ // No profiles at all.+ assert.equal(settingsContext.getActiveModel(catalog, 'tts'), null)+})++test('serviceConfigured requires a usable model entry, and a provider for search', () => {+ const catalog = makeCatalog()++ assert.equal(settingsContext.serviceConfigured(catalog, 'llm'), true)+ assert.equal(settingsContext.serviceConfigured(catalog, 'search'), true)+ assert.equal(settingsContext.serviceConfigured(catalog, 'tts'), false)++ // A model entry without a model id is not configured.+ catalog.services.llm.profiles[0].models[0].model = ''+ assert.equal(settingsContext.serviceConfigured(catalog, 'llm'), false)+ // A search profile without a provider is not configured.+ catalog.services.search.profiles[0].provider = undefined+ assert.equal(settingsContext.serviceConfigured(catalog, 'search'), false)+})++test('currentDiagnosticsResult only reports results for the currently selected profile/model', () => {+ const catalog = makeCatalog()++ assert.equal(+ settingsContext.currentDiagnosticsResult(catalog, 'llm', {}),+ null,+ 'no diagnostics at all'+ )+ const matching = diagnostics({})+ assert.equal(+ settingsContext.currentDiagnosticsResult(catalog, 'llm', { llm: matching }),+ matching+ )+ // Stale: result was for a profile/model that is no longer selected.+ const stale = diagnostics({ profileId: 'p-other' })+ assert.equal(settingsContext.currentDiagnosticsResult(catalog, 'llm', { llm: stale }), null)+ // Search diagnostics match on a null model id.+ const searchResult = diagnostics({+ profileId: 's1',+ modelId: null,+ })+ assert.equal(+ settingsContext.currentDiagnosticsResult(catalog, 'search', {+ search: searchResult,+ }),+ searchResult+ )+})++test('serviceReadiness derives its four states from configuration and diagnostics', () => {+ const catalog = makeCatalog()+ const none: Partial<Record<ServiceName, DiagnosticsResult>> = {}++ assert.equal(settingsContext.serviceReadiness(catalog, 'tts', none), 'not_configured')+ assert.equal(settingsContext.serviceReadiness(catalog, 'llm', none), 'untested')+ assert.equal(+ settingsContext.serviceReadiness(catalog, 'llm', {+ llm: diagnostics({ state: 'success' }),+ }),+ 'passed'+ )+ assert.equal(+ settingsContext.serviceReadiness(catalog, 'llm', {+ llm: diagnostics({ state: 'failed' }),+ }),+ 'failed'+ )+ // A stale failure must not fail the service.+ assert.equal(+ settingsContext.serviceReadiness(catalog, 'llm', {+ llm: diagnostics({ state: 'failed', profileId: 'old' }),+ }),+ 'untested'+ )+})++test('servicePendingApply compares one service bucket at a time', () => {+ const catalog = makeCatalog()+ const draft = settingsContext.cloneCatalog(catalog)++ assert.equal(settingsContext.servicePendingApply(catalog, draft, 'llm'), false)+ draft.services.llm.active_model_id = 'm2'+ assert.equal(settingsContext.servicePendingApply(catalog, draft, 'llm'), true)+ // Other buckets stay clean.+ assert.equal(settingsContext.servicePendingApply(catalog, draft, 'search'), false)+ assert.equal(settingsContext.servicePendingApply(catalog, draft, 'tts'), false)+})++test('RESPONSE_LANGUAGE_OPTIONS values are usable as UiSettings.response_language fixtures', () => {+ // Fixture completeness: any option value round-trips through a UiSettings+ // object that persistUiSettingsPatch would send verbatim.+ const option = settingsContext.RESPONSE_LANGUAGE_OPTIONS.at(-1)!+ const ui: UiSettings = { ...VALID_UI, response_language: option.value }+ assert.equal(ui.response_language, option.value)+ assert.equal(VALID_UI.language, 'en')+})