import assert from 'node:assert/strict'; import { test } from 'vitest'; import type { StoredEntry } from '@deepseek-ai/dsh-client-ui-slots'; import { SlotCore } from '@deepseek-ai/dsh-client-ui-slots'; import { CHAT_VIEW_ID, CHAT_VIEW_PRIORITY, retireHostedChatView, type ViewLedger } from '../src/client/chat-view-seat.js'; const CHAT_FLOW = 'conversation.chat.flow'; /** The projection `viewTabs()` uses: raw ledger rows, skipping entries without an id. */ const tabIds = (core: SlotCore): string[] => core.entries('conversation.view') .flatMap(entry => entry.options.id === undefined ? [] : [entry.options.id]); const chatIds = (core: SlotCore): string[] => core.entries('conversation.view') .flatMap(entry => entry.options.id === CHAT_VIEW_ID ? [entry.options.id] : []); const ledgerOf = (core: SlotCore): ViewLedger => ({ entries: key => core.entries(key) as readonly StoredEntry[], register: (options, component) => core.register(options as never, component), subscribe: (key, listener) => core.subscribe(key, listener), }); function viewsSlot(core: SlotCore): void { core.register({ name: 'root', children: { 'conversation.view': { kind: 'list', scope: 'session' }, } }, () => null); } /** Simulates the official ChatView entry: a child declaration and a store seat. */ function registerChatView(core: SlotCore, component: () => null) { return core.register({ name: 'conversation.view', id: CHAT_VIEW_ID, order: 0, children: { [CHAT_FLOW]: { kind: 'single', scope: 'session' } }, store: {} as never, }, component); } function viewEntry(id: string, priority: number | undefined, component: unknown): StoredEntry { return { component, options: { id, ...(priority === undefined ? {} : { priority }) } } as StoredEntry; } /** A row whose `options.id` setter is a no-op, as on hosts that ignore the primary hide. */ function stubbornEntry(component: unknown): StoredEntry { const options = {} as StoredEntry['options']; Object.defineProperty(options, 'id', { get: () => CHAT_VIEW_ID, set: () => {}, enumerable: true, configurable: true }); return { component, options } as StoredEntry; } function fakeLedger(rows: readonly StoredEntry[], copyArrays = false): ViewLedger { const listeners = new Set<() => void>(); return { entries: () => copyArrays ? [...rows] : rows, register: () => () => {}, subscribe: (_name, listener) => { listeners.add(listener); return () => { listeners.delete(listener); }; }, }; } test('shadowing keeps the official row live and removes only its tab', () => { const core = new SlotCore(); viewsSlot(core); const OfficialChatView = () => null; const ReaderSeat = () => null; const disposeOfficial = registerChatView(core, OfficialChatView); const disposeReader = core.register({ name: 'conversation.view', id: CHAT_VIEW_ID, priority: CHAT_VIEW_PRIORITY, order: 0, }, ReaderSeat); const ledger = ledgerOf(core); const restore = retireHostedChatView(ledger); // One 对话 tab; the reader wins the render seat. assert.deepEqual(tabIds(core), [CHAT_VIEW_ID]); assert.equal(core.entries('conversation.view').filter(entry => entry.component === OfficialChatView).length, 1); assert.equal(core.entriesOfSlot('conversation.view').filter(entry => entry.options.id === CHAT_VIEW_ID)[0].component, ReaderSeat); // The official row still owns its child declaration. assert.equal(core.spec(CHAT_FLOW)?.kind, 'single'); const before = core.entries('conversation.view').indexOf( core.entries('conversation.view').find(entry => entry.component === OfficialChatView)!, ); restore(); const after = core.entries('conversation.view').indexOf( core.entries('conversation.view').find(entry => entry.component === OfficialChatView)!, ); assert.equal(after, before, 'restore must keep the official row in place'); assert.equal(core.entries('conversation.view').find(entry => entry.component === OfficialChatView)?.options.id, CHAT_VIEW_ID); disposeReader(); disposeOfficial(); }); /** Slot subscriptions flush on a microtask; settle a couple of turns. */ const settle = async (): Promise => { await Promise.resolve(); await Promise.resolve(); await new Promise(resolve => setTimeout(resolve, 0)); }; test('official teardown while hidden still releases child slots and allows re-registration', async () => { const core = new SlotCore(); viewsSlot(core); const OfficialChatView = () => null; const disposeOfficial = registerChatView(core, OfficialChatView); const disposeReader = core.register({ name: 'conversation.view', id: CHAT_VIEW_ID, priority: CHAT_VIEW_PRIORITY, order: 0, }, () => null); const ledger = ledgerOf(core); const restore = retireHostedChatView(ledger); // ui-chat unloads while the reader shadows it. The official disposer must // still release its declared child (the old splice made it early-return). disposeOfficial(); assert.equal(core.spec(CHAT_FLOW), undefined); // ui-chat registers again (HMR): no "already declared" and the seat re-hides it. const Again = () => null; assert.doesNotThrow(() => { registerChatView(core, Again); }); assert.equal(core.spec(CHAT_FLOW)?.kind, 'single'); await settle(); assert.deepEqual(tabIds(core), [CHAT_VIEW_ID], 'a re-registered ChatView row must be hidden again'); assert.equal(core.entries('conversation.view').filter(entry => entry.component === Again).length, 1); restore(); assert.equal(core.entries('conversation.view').find(entry => entry.component === Again)?.options.id, CHAT_VIEW_ID); disposeReader(); }); test('repeat retirement and restore are idempotent', () => { const core = new SlotCore(); viewsSlot(core); const OfficialChatView = () => null; const disposeOfficial = registerChatView(core, OfficialChatView); const disposeReader = core.register({ name: 'conversation.view', id: CHAT_VIEW_ID, priority: CHAT_VIEW_PRIORITY, order: 0, }, () => null); const ledger = ledgerOf(core); const first = retireHostedChatView(ledger); const second = retireHostedChatView(ledger); first(); assert.equal(core.entries('conversation.view').find(entry => entry.component === OfficialChatView)?.options.id, CHAT_VIEW_ID); first(); second(); assert.equal(core.entries('conversation.view').filter(entry => entry.component === OfficialChatView).length, 1); assert.deepEqual(tabIds(core), [CHAT_VIEW_ID, CHAT_VIEW_ID]); disposeReader(); disposeOfficial(); }); test('fallback splices an official row whose View id cannot be cleared', () => { const official = stubbornEntry('official'); const reader = viewEntry(CHAT_VIEW_ID, CHAT_VIEW_PRIORITY, 'reader'); const rows = [official, reader]; const ledger = fakeLedger(rows); const restore = retireHostedChatView(ledger); assert.deepEqual(rows, [reader], 'the official row must leave the live ledger'); assert.equal(official.options.id, CHAT_VIEW_ID, 'the original id is remembered for restore'); restore(); assert.deepEqual(rows, [official, reader], 'restore must put the row back at its original index'); }); test('fallback tolerates an immutable entries array', () => { const official = stubbornEntry('official'); const reader = viewEntry(CHAT_VIEW_ID, CHAT_VIEW_PRIORITY, 'reader'); const rows = Object.freeze([official, reader]) as unknown as StoredEntry[]; const ledger = fakeLedger(rows); let restore = () => {}; assert.doesNotThrow(() => { restore = retireHostedChatView(ledger); }); assert.equal(rows.length, 2, 'an immutable ledger cannot be spliced; nothing may crash'); assert.doesNotThrow(() => { restore(); }); }); test('fallback does not mistake a copied entries array for the live ledger', () => { const official = stubbornEntry('official'); const reader = viewEntry(CHAT_VIEW_ID, CHAT_VIEW_PRIORITY, 'reader'); const rows = [official, reader]; const ledger = fakeLedger(rows, true); const restore = retireHostedChatView(ledger); assert.equal(rows.length, 2, 'splicing a copy must not be reported as a removal'); restore(); assert.deepEqual(rows, [official, reader]); }); test('fallback retirement is idempotent across repeated retire and restore', () => { const official = stubbornEntry('official'); const reader = viewEntry(CHAT_VIEW_ID, CHAT_VIEW_PRIORITY, 'reader'); const rows = [official, reader]; const ledger = fakeLedger(rows); const first = retireHostedChatView(ledger); const second = retireHostedChatView(ledger); assert.deepEqual(rows, [reader]); first(); assert.deepEqual(rows, [official, reader]); first(); second(); assert.deepEqual(rows, [official, reader], 'repeat calls must not duplicate the row'); }); test('visibility changes notify View subscribers', async () => { const core = new SlotCore(); viewsSlot(core); const disposeOfficial = registerChatView(core, () => null); const disposeReader = core.register({ name: 'conversation.view', id: CHAT_VIEW_ID, priority: CHAT_VIEW_PRIORITY, order: 0, }, () => null); const seen: string[][] = []; const unsubscribe = core.subscribe('conversation.view', () => { seen.push(tabIds(core)); }); const ledger = ledgerOf(core); const restore = retireHostedChatView(ledger); await settle(); assert.equal(seen.at(-1)?.filter(id => id === CHAT_VIEW_ID).length, 1, 'subscribers must see one 对话 tab'); restore(); await settle(); assert.equal(seen.at(-1)?.filter(id => id === CHAT_VIEW_ID).length, 2, 'restore must show the official tab again'); unsubscribe(); disposeReader(); disposeOfficial(); });