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'; function entry(id: string, priority = 0, order = 0): StoredEntry { return { component: () => null, options: { id, priority, order } } as unknown as StoredEntry; } function sortEntries(list: StoredEntry[]): StoredEntry[] { return [...list].sort((a, b) => (a.options.priority ?? 0) - (b.options.priority ?? 0) || (a.options.order ?? 0) - (b.options.order ?? 0)); } /** Minimal stand-in for the View ledger: live array, version bumps, subscriptions. */ function viewLedger(initial: StoredEntry[]) { let list = sortEntries(initial); const listeners = new Set<() => void>(); const api: ViewLedger = { entries: () => list, register(options, component) { const added = { component, options: { ...options } } as unknown as StoredEntry; list = sortEntries([...list, added]); for (const listener of listeners) listener(); return () => { list = list.filter(candidate => candidate !== added); for (const listener of listeners) listener(); }; }, }; return { api, subscribe(listener: () => void) { listeners.add(listener); return () => { listeners.delete(listener); }; }, ids: () => list.map(candidate => candidate.options.id), idsOfChat: () => list.filter(candidate => candidate.options.id === CHAT_VIEW_ID).length, }; } test('the reader seat retires one official 对话 entry while keeping other Views', () => { const official = entry(CHAT_VIEW_ID, 0, 0); const trajectory = entry('trajectory', 0, 1); const readerSeat = entry(CHAT_VIEW_ID, CHAT_VIEW_PRIORITY, 0); const ledger = viewLedger([readerSeat, official, trajectory]); let notified = 0; ledger.subscribe(() => { notified += 1; }); const restore = retireHostedChatView(ledger.api); // One 对话 survives (the reader seat); the official ChatView ledger row is // gone, so the header derives a single chat tab and keep its sibling Views. assert.equal(ledger.idsOfChat(), 1); assert.equal(ledger.ids().includes('trajectory'), true); assert.equal(notified >= 1, true, 'retirement must bump the View ledger version'); restore(); assert.equal(ledger.idsOfChat(), 2, 'plugin teardown restores the official ChatView'); assert.deepEqual(ledger.ids(), [CHAT_VIEW_ID, CHAT_VIEW_ID, 'trajectory']); assert.equal(notified >= 2, true, 'restore must notify View subscribers'); }); test('restore is idempotent and cannot duplicate the official entry', () => { const official = entry(CHAT_VIEW_ID, 0, 0); const ledger = viewLedger([entry(CHAT_VIEW_ID, CHAT_VIEW_PRIORITY, 0), official]); const restore = retireHostedChatView(ledger.api); restore(); restore(); assert.equal(ledger.idsOfChat(), 2); assert.equal(ledger.api.entries('conversation.view').filter(candidate => candidate === official).length, 1); }); test('a ledger without the official ChatView is left untouched', () => { const readerSeat = entry(CHAT_VIEW_ID, CHAT_VIEW_PRIORITY, 0); const ledger = viewLedger([readerSeat]); let notified = 0; ledger.subscribe(() => { notified += 1; }); const restore = retireHostedChatView(ledger.api); assert.deepEqual(ledger.ids(), [CHAT_VIEW_ID]); assert.equal(notified, 0); restore(); assert.deepEqual(ledger.ids(), [CHAT_VIEW_ID]); }); test('real SlotCore: a lower-priority 对话 registration shadows the official renderer', () => { const core = new SlotCore(); core.register({ name: 'root', children: { 'conversation.view': { kind: 'list', scope: 'session' }, } }, () => null); const OfficialChatView = () => null; const ReaderSeat = () => null; const disposeOfficial = core.register({ name: 'conversation.view', id: CHAT_VIEW_ID, order: 0 }, OfficialChatView); const disposeReader = core.register({ name: 'conversation.view', id: CHAT_VIEW_ID, priority: CHAT_VIEW_PRIORITY, order: 0 }, ReaderSeat); // Same id, different priority: legal registration; the lower one wins the cell. assert.deepEqual(core.entriesOfSlot('conversation.view').map(item => item.component), [ReaderSeat]); // Retiring the raw official row keeps the shadowed cell and drops the tab duplicate. const ledger: ViewLedger = { entries: key => core.entries(key) as readonly StoredEntry[], register: (options, component) => core.register(options as never, component), }; const restore = retireHostedChatView(ledger); assert.equal(core.entries('conversation.view').length, 1); assert.equal(core.entriesOfSlot('conversation.view').length, 1); restore(); assert.equal(core.entries('conversation.view').length, 2); assert.equal(core.entriesOfSlot('conversation.view').length, 1); assert.equal(core.entries('conversation.view').some(item => item.component === OfficialChatView), true); restore(); assert.equal(core.entries('conversation.view').length, 2); disposeReader(); assert.equal(core.entriesOfSlot('conversation.view')[0].component, OfficialChatView); disposeOfficial(); });