229 lines
8.7 KiB
TypeScript
229 lines
8.7 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { test, vi } 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, hideDuplicateChatTabs, 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<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('entries that cannot be hidden remain owned by the host', () => {
|
|
const rows = Object.freeze([stubbornEntry('official'), viewEntry(CHAT_VIEW_ID, CHAT_VIEW_PRIORITY, 'reader')]);
|
|
for (const copyArrays of [false, true]) {
|
|
const restore = retireHostedChatView(fakeLedger(rows, copyArrays));
|
|
assert.equal(rows[0]?.options.id, CHAT_VIEW_ID);
|
|
assert.equal(rows.length, 2);
|
|
restore();
|
|
}
|
|
});
|
|
|
|
test('the header hides only repeated chat tabs and removes its marks on teardown', () => {
|
|
const tab = (textContent: string) => ({
|
|
textContent, hidden: false,
|
|
toggleAttribute(_name: string, hidden: boolean) { this.hidden = hidden; },
|
|
removeAttribute() { this.hidden = false; },
|
|
});
|
|
const first = tab('对话');
|
|
const duplicate = tab('Chat');
|
|
const gallery = tab('画廊');
|
|
const another = tab('对话');
|
|
const doc = {
|
|
documentElement: {},
|
|
querySelectorAll: (selector: string) => selector.startsWith('[data-conversation-tabs]')
|
|
? [{ querySelectorAll: () => [first, duplicate, gallery] }, { querySelectorAll: () => [another] }]
|
|
: [duplicate],
|
|
} as unknown as Document;
|
|
vi.stubGlobal('MutationObserver', class { observe() {} disconnect() {} });
|
|
try {
|
|
const restore = hideDuplicateChatTabs(doc);
|
|
assert.deepEqual([first.hidden, duplicate.hidden, gallery.hidden, another.hidden], [false, true, false, false]);
|
|
restore();
|
|
assert.equal(duplicate.hidden, false);
|
|
} finally {
|
|
vi.unstubAllGlobals();
|
|
}
|
|
});
|
|
|
|
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();
|
|
});
|