dsh-better-chat/tests/chat-view-seat.test.ts

168 lines
6.3 KiB
TypeScript

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);
}
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<void> => {
await Promise.resolve();
await Promise.resolve();
await new Promise<void>(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('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();
});