fix: hide the official ChatView tab without removing its ledger row

Splicing the live conversation.view row made the official disposer early-return, leaking conversation.chat.flow and the Chat store when ui-chat tore down while shadowed. Keep the row (and its official lifecycle) and clear only the View id the tab projection reads; restore it on teardown. A View subscription re-hides a ChatView that ui-chat re-registers.

Tests: real SlotCore cases for shadow/hide, official teardown + re-registration while hidden, repeat retire/restore, and subscriber notifications. Wiring test now guards against reintroducing splice().
This commit is contained in:
QQ 2026-11-27 18:43:24 +08:00
parent 3262d20b6f
commit f5da78063a
8 changed files with 301 additions and 179 deletions

View File

@ -4,7 +4,7 @@
- 恢复 1.0.4 删除的阅读视图源码(`src/client/**` 组件、样式、Markdown/KaTeX 渲染、官方槽位镜像、文本变换服务、逐字动效、时间轴、产物卡、MCP 交互卡片、设置节)与对应的纯逻辑测试。 - 恢复 1.0.4 删除的阅读视图源码(`src/client/**` 组件、样式、Markdown/KaTeX 渲染、官方槽位镜像、文本变换服务、逐字动效、时间轴、产物卡、MCP 交互卡片、设置节)与对应的纯逻辑测试。
- 阅读视图不再注册「阅读」页签:改为在 `conversation.view` 里以 `id: 'chat'`、`priority: -10` 接管内置「对话」页签的正文区域。页头、输入框、审批区、外层滚动画布仍走官方。 - 阅读视图不再注册「阅读」页签:改为在 `conversation.view` 里以 `id: 'chat'`、`priority: -10` 接管内置「对话」页签的正文区域。页头、输入框、审批区、外层滚动画布仍走官方。
- 因为列表槽的页签清单读的是原始注册账本(不是渲染胜者),新增 `chat-view-seat.ts`:注册后把官方 ChatView 的账本行退役,插件卸载时按序还原,避免出现两个「对话」页签;若宿主不再暴露可写的账本数组,则只退化为多一个页签,正文渲染不受影响。 - 因为列表槽的页签清单读的是原始注册账本(不是渲染胜者),新增 `chat-view-seat.ts`:注册后把官方 ChatView 条目的 View `id` 暂时清空,让页签投影只看到本插件那一项;插件卸载时恢复。官方条目本身始终留在账本里,`conversation.chat.flow` 子槽与 `chatStore` 仍由官方 disposer 释放;ui-chat 遮蔽期间重注册会被自动重新隐藏(有专项用例)。
- 未恢复宿主 `/better-display/reveal` 与 `/better-display/metrics` 路由;「在访达/资源管理器中定位」会走官方父目录打开的回退路径。 - 未恢复宿主 `/better-display/reveal` 与 `/better-display/metrics` 路由;「在访达/资源管理器中定位」会走官方父目录打开的回退路径。
- 验证:`pnpm build`、`pnpm typecheck`、`pnpm test`(33 个文件 / 237 个用例)通过。 - 验证:`pnpm build`、`pnpm typecheck`、`pnpm test`(33 个文件 / 237 个用例)通过。

View File

@ -6,7 +6,7 @@
阅读视图不再注册单独的 View 页签。它复用内置 View id `chat`,以 `priority: -10` 在 `conversation.view`(list 槽)里遮蔽官方 ChatView,因此「对话」页签的正文区域由 Reader 绘制,页头、输入框、审批区和 `[data-conversation-scroll]` 滚动画布仍是官方实现。 阅读视图不再注册单独的 View 页签。它复用内置 View id `chat`,以 `priority: -10` 在 `conversation.view`(list 槽)里遮蔽官方 ChatView,因此「对话」页签的正文区域由 Reader 绘制,页头、输入框、审批区和 `[data-conversation-scroll]` 滚动画布仍是官方实现。
`conversation.view` 的页签清单读的是原始注册账本,不是渲染胜者;只做遮蔽会出现两个「对话」页签。`src/client/chat-view-seat.ts` 在注册后把官方 ChatView 的账本行暂时退役,并在插件卸载/热重载时按序还原。若未来宿主不再暴露可写的 `entries()` 数组,退役会退化为 no-op:正文仍由 Reader 渲染,最多多出一个重复页签。 `conversation.view` 的页签清单读的是原始注册账本,不是渲染胜者;只做遮蔽会出现两个「对话」页签。`src/client/chat-view-seat.ts` 不删除官方行(删行会让官方 disposer 提前返回,`conversation.chat.flow` 子槽和 `chatStore` 句柄就漏了),只把官方条目的 View `id` 暂时清空——页签投影唯一读取的字段;插件卸载时还原。官方行、子槽、store 与 disposer 全程归官方所有。若 ui-chat 在遮蔽期间重注册 ChatView,订阅会把新行重新隐藏。若未来宿主不再允许修改 `entry.options`,隐藏会退化为 no-op:正文仍由 Reader 渲染,最多多出一个重复页签。
下文的“阅读页”均指接管后的「对话」正文。`entry.tsx`(打开会话时强制切到阅读页)已停用,源码仅作参考。 下文的“阅读页”均指接管后的「对话」正文。`entry.tsx`(打开会话时强制切到阅读页)已停用,源码仅作参考。

View File

@ -53012,36 +53012,67 @@ ${trimmed}
// src/client/chat-view-seat.ts // src/client/chat-view-seat.ts
const CHAT_VIEW_ID = "chat"; const CHAT_VIEW_ID = "chat";
const PROBE_ID = "dsh-better-chat:chat-view-ledger-probe"; const PROBE_ID = "dsh-better-chat:chat-view-ledger-probe";
function sortLedger(list) { /** One active retirement per View ledger (`ctx.slots` is a single shared object). */
list.sort((a, b) => (a.options.priority ?? 0) - (b.options.priority ?? 0) || (a.options.order ?? 0) - (b.options.order ?? 0)); const ACTIVE = /* @__PURE__ */ new WeakMap();
}
/** /**
* Remove the official ChatView entry from the raw View ledger so the header * Hide the official ChatView row from the View tab projection by clearing its
* lists one 对话 tab. @returns idempotent restore for plugin teardown. * View `id`; the row itself stays live and keeps its official disposer.
* @param ledger - Slot registry surfaced through `ctx.slots`.
* @returns idempotent restore for plugin teardown.
*/ */
function retireHostedChatView(ledger) { function retireHostedChatView(ledger) {
const list = ledger.entries("conversation.view"); if (ACTIVE.has(ledger)) return () => {};
const index = list.findIndex((entry) => entry.options.id === "chat" && (entry.options.priority ?? 0) !== -10); const hidden = /* @__PURE__ */ new Map();
if (index < 0) return () => {}; let live = true;
const [hosted] = list.splice(index, 1); let notifying = false;
if (hosted === void 0) return () => {};
const notify = () => { const notify = () => {
ledger.register({ if (notifying) return;
name: "conversation.view", notifying = true;
id: PROBE_ID, try {
priority: -10, ledger.register({
order: 0, name: "conversation.view",
label: () => "" id: PROBE_ID,
}, () => null)(); priority: -10,
order: 0,
label: () => ""
}, () => null)();
} finally {
notifying = false;
}
}; };
notify(); const hide = () => {
return () => { let changed = false;
const live = ledger.entries("conversation.view"); for (const entry of ledger.entries("conversation.view")) {
if (live.includes(hosted)) return; if (entry.options.id !== "chat") continue;
live.push(hosted); if ((entry.options.priority ?? 0) === -10) continue;
sortLedger(live); if (hidden.has(entry)) continue;
notify(); hidden.set(entry, { id: CHAT_VIEW_ID });
entry.options.id = void 0;
changed = true;
}
if (changed) notify();
}; };
const unsubscribe = ledger.subscribe("conversation.view", () => {
if (live) hide();
});
hide();
const restore = () => {
if (!live) return;
live = false;
unsubscribe();
ACTIVE.delete(ledger);
const present = new Set(ledger.entries("conversation.view"));
let changed = false;
for (const [entry, row] of hidden) {
if (!present.has(entry)) continue;
entry.options.id = row.id;
changed = true;
}
hidden.clear();
if (changed) notify();
};
ACTIVE.set(ledger, restore);
return restore;
} }
// src/client/settings-copy.ts // src/client/settings-copy.ts
const en = { const en = {

File diff suppressed because one or more lines are too long

View File

@ -1,21 +1,28 @@
/** /**
* Chat-view seat ownership for the reader-in-chat layout. * Chat-view tab ownership for the reader-in-chat layout.
* *
* The reader used to live in its own "阅读" View tab. It now occupies the * The reader used to live in its own "阅读" View tab. It now occupies the
* built-in "对话" View id instead, so the transcript area draws the reader * built-in "对话" View id instead, so the transcript area draws the reader
* while the composer, header, approvals, and scroll port stay official. * while the composer, header, approvals, and scroll port stay official.
* *
* `conversation.view` is a list Slot: a registration with the same `id` at a * `conversation.view` is a list Slot: a registration with the same `id` at a
* lower priority shadows the official ChatView for rendering. The tab row, * lower priority wins the cell for rendering. The tab row, however, is derived
* however, is derived from the raw entry ledger (`slots.entries`), so a mere * from the raw entry ledger (`slots.entries`), and it skips entries whose
* shadow would show two "对话" tabs. This module retires the official entry * `id` is undefined. Shadowing alone would therefore show two "对话" tabs.
* from that ledger and restores it on disposal, keeping the header honest.
* *
* Only public registry operations are used: `entries()` returns the live * The official ChatView row must stay on the ledger. Its disposer only runs
* ledger array, `register()`/its disposer bump the version and notify View * `releaseEntry` (store-handle count and declared child slots such as
* subscribers. If a future Harness stops exposing the live array, the splice * `conversation.chat.flow`) while the row is still in `entries`; removing the
* becomes a no-op and the reader still shadows the official view; only the * row would leak those if ui-chat tears down while we shadow it, and the next
* duplicate tab returns. * ui-chat registration would throw "slot is already declared". So instead of
* touching the lifecycle, this seat clears only the row's View `id` — the one
* field the tab projection reads — and puts it back on teardown. The row,
* its children, store, and disposer remain entirely official.
*
* A subscription re-hides a ChatView row that ui-chat re-registers while the
* plugin stays mounted. Every visibility change bumps the ledger version with
* a throwaway probe registration (never selected) so View subscribers such as
* the header recompute their tab list.
*/ */
import type { StoredEntry } from '@deepseek-ai/dsh-client-ui-slots'; import type { StoredEntry } from '@deepseek-ai/dsh-client-ui-slots';
@ -26,7 +33,7 @@ export const CHAT_VIEW_PRIORITY = -10;
const PROBE_ID = 'dsh-better-chat:chat-view-ledger-probe'; const PROBE_ID = 'dsh-better-chat:chat-view-ledger-probe';
/** The two registry operations this seat needs; documented public surface. */ /** The registry operations this seat needs; documented public surface. */
export interface ViewLedger { export interface ViewLedger {
entries(name: string): readonly StoredEntry[]; entries(name: string): readonly StoredEntry[];
register(options: { register(options: {
@ -36,49 +43,83 @@ export interface ViewLedger {
order?: number; order?: number;
label?: () => string; label?: () => string;
}, component: unknown): () => void; }, component: unknown): () => void;
subscribe(name: string, listener: () => void): () => void;
} }
interface ViewEntry extends StoredEntry { interface HiddenRow {
options: StoredEntry['options'] & { id: string }; /** The original View id, restored on teardown. */
readonly id: string;
} }
function sortLedger(list: StoredEntry[]): void { /** One active retirement per View ledger (`ctx.slots` is a single shared object). */
list.sort((a, b) => (a.options.priority ?? 0) - (b.options.priority ?? 0) const ACTIVE = new WeakMap<ViewLedger, () => void>();
|| (a.options.order ?? 0) - (b.options.order ?? 0));
}
/** /**
* Remove the official ChatView entry from the raw View ledger so the header * Hide the official ChatView row from the View tab projection by clearing its
* lists one 对话 tab. @returns idempotent restore for plugin teardown. * View `id`; the row itself stays live and keeps its official disposer.
* @param ledger - Slot registry surfaced through `ctx.slots`.
* @returns idempotent restore for plugin teardown.
*/ */
export function retireHostedChatView(ledger: ViewLedger): () => void { export function retireHostedChatView(ledger: ViewLedger): () => void {
const list = ledger.entries('conversation.view') as StoredEntry[]; if (ACTIVE.has(ledger)) return () => {};
const index = list.findIndex(entry => entry.options.id === CHAT_VIEW_ID const hidden = new Map<StoredEntry, HiddenRow>();
&& (entry.options.priority ?? 0) !== CHAT_VIEW_PRIORITY); let live = true;
if (index < 0) return () => {}; let notifying = false;
const [hosted] = list.splice(index, 1) as ViewEntry[];
if (hosted === undefined) return () => {};
// Splicing alone does not bump the slot version, so View subscribers would // Splicing ids does not bump the slot version, so View subscribers would
// keep the stale tab list. A throwaway registration both notifies and is // keep a stale tab list. A throwaway registration both notifies and is gone
// gone before paint: its id is never the selected View. // before paint: its id is never the selected View.
const notify = () => { const notify = (): void => {
const dispose = ledger.register({ if (notifying) return;
name: 'conversation.view', notifying = true;
id: PROBE_ID, try {
priority: CHAT_VIEW_PRIORITY, const dispose = ledger.register({
order: 0, name: 'conversation.view',
label: () => '', id: PROBE_ID,
}, () => null); priority: CHAT_VIEW_PRIORITY,
dispose(); order: 0,
label: () => '',
}, () => null);
dispose();
} finally {
notifying = false;
}
}; };
notify();
return () => { const hide = (): void => {
const live = ledger.entries('conversation.view') as StoredEntry[]; let changed = false;
if (live.includes(hosted)) return; for (const entry of ledger.entries('conversation.view')) {
live.push(hosted); if (entry.options.id !== CHAT_VIEW_ID) continue;
sortLedger(live); if ((entry.options.priority ?? 0) === CHAT_VIEW_PRIORITY) continue;
notify(); if (hidden.has(entry)) continue;
hidden.set(entry, { id: CHAT_VIEW_ID });
entry.options.id = undefined;
changed = true;
}
if (changed) notify();
}; };
// Registration-time callback; a later ui-chat registration also lands here.
const unsubscribe = ledger.subscribe('conversation.view', () => {
if (live) hide();
});
hide();
const restore = (): void => {
if (!live) return;
live = false;
unsubscribe();
ACTIVE.delete(ledger);
const present = new Set(ledger.entries('conversation.view'));
let changed = false;
for (const [entry, row] of hidden) {
if (!present.has(entry)) continue;
entry.options.id = row.id;
changed = true;
}
hidden.clear();
if (changed) notify();
};
ACTIVE.set(ledger, restore);
return restore;
} }

View File

@ -236,8 +236,8 @@ export function apply(ctx: Context, options: { chatPrefs?: ChatPrefs } = {}): vo
}; };
}, },
}, Reader); }, Reader);
// The official ChatView shadowed by the registration above stays on the // The official ChatView row stays live (its disposer owns the chat.flow
// raw View ledger; retire it so the header shows one 对话 tab. // child and chatStore); only its View tab id is hidden while we shadow it.
yield retireHostedChatView(ctx.slots as unknown as ViewLedger); yield retireHostedChatView(ctx.slots as unknown as ViewLedger);
yield installOfficialSlots(ctx); yield installOfficialSlots(ctx);
}); });

View File

@ -4,120 +4,165 @@ import type { StoredEntry } from '@deepseek-ai/dsh-client-ui-slots';
import { SlotCore } 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'; 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 { const CHAT_FLOW = 'conversation.chat.flow';
return { component: () => null, options: { id, priority, order } } as unknown as StoredEntry;
}
function sortEntries(list: StoredEntry[]): StoredEntry[] { /** The projection `viewTabs()` uses: raw ledger rows, skipping entries without an id. */
return [...list].sort((a, b) => (a.options.priority ?? 0) - (b.options.priority ?? 0) const tabIds = (core: SlotCore): string[] => core.entries('conversation.view')
|| (a.options.order ?? 0) - (b.options.order ?? 0)); .flatMap(entry => entry.options.id === undefined ? [] : [entry.options.id]);
}
/** Minimal stand-in for the View ledger: live array, version bumps, subscriptions. */ const chatIds = (core: SlotCore): string[] => core.entries('conversation.view')
function viewLedger(initial: StoredEntry[]) { .flatMap(entry => entry.options.id === CHAT_VIEW_ID ? [entry.options.id] : []);
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 ledgerOf = (core: SlotCore): ViewLedger => ({
const official = entry(CHAT_VIEW_ID, 0, 0); entries: key => core.entries(key) as readonly StoredEntry[],
const trajectory = entry('trajectory', 0, 1); register: (options, component) => core.register(options as never, component),
const readerSeat = entry(CHAT_VIEW_ID, CHAT_VIEW_PRIORITY, 0); subscribe: (key, listener) => core.subscribe(key, listener),
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', () => { function viewsSlot(core: SlotCore): void {
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: { core.register({ name: 'root', children: {
'conversation.view': { kind: 'list', scope: 'session' }, 'conversation.view': { kind: 'list', scope: 'session' },
} }, () => null); } }, () => 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 OfficialChatView = () => null;
const ReaderSeat = () => null; const ReaderSeat = () => null;
const disposeOfficial = core.register({ name: 'conversation.view', id: CHAT_VIEW_ID, order: 0 }, OfficialChatView); const disposeOfficial = registerChatView(core, OfficialChatView);
const disposeReader = core.register({ name: 'conversation.view', id: CHAT_VIEW_ID, priority: CHAT_VIEW_PRIORITY, order: 0 }, ReaderSeat); 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. const ledger = ledgerOf(core);
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); const restore = retireHostedChatView(ledger);
assert.equal(core.entries('conversation.view').length, 1);
assert.equal(core.entriesOfSlot('conversation.view').length, 1);
// 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(); restore();
assert.equal(core.entries('conversation.view').length, 2); const after = core.entries('conversation.view').indexOf(
assert.equal(core.entriesOfSlot('conversation.view').length, 1); core.entries('conversation.view').find(entry => entry.component === OfficialChatView)!,
assert.equal(core.entries('conversation.view').some(item => item.component === OfficialChatView), true); );
restore(); assert.equal(after, before, 'restore must keep the official row in place');
assert.equal(core.entries('conversation.view').length, 2); 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(); disposeReader();
assert.equal(core.entriesOfSlot('conversation.view')[0].component, OfficialChatView);
disposeOfficial(); disposeOfficial();
}); });

View File

@ -4,12 +4,12 @@ import { test } from 'vitest';
/** /**
* The reader-in-chat wiring is mostly declarative (slot id, priority, child * The reader-in-chat wiring is mostly declarative (slot id, priority, child
* seats, retired official entry), so the cheap regression guard is reading the * seats, hidden official row), so the cheap regression guard is reading the
* entry module itself. Behaviour of the ledger surgery lives in * entry modules. Executable ledger behaviour lives in `chat-view-seat.test.ts`;
* `chat-view-seat.test.ts`. * this file guards the two files' agreed contract.
*/ */
const url = new URL('../src/client/reader-plugin.tsx', import.meta.url); const source = await readFile(new URL('../src/client/reader-plugin.tsx', import.meta.url), 'utf8');
const source = await readFile(url, 'utf8'); const seat = await readFile(new URL('../src/client/chat-view-seat.ts', import.meta.url), 'utf8');
test('the reader occupies the built-in 对话 View at a shadowing priority', () => { test('the reader occupies the built-in 对话 View at a shadowing priority', () => {
assert.match(source, /name: 'conversation\.view'/u); assert.match(source, /name: 'conversation\.view'/u);
@ -20,8 +20,13 @@ test('the reader occupies the built-in 对话 View at a shadowing priority', ()
assert.doesNotMatch(source, /installReaderEntry/u, 'no forced View switch on open'); assert.doesNotMatch(source, /installReaderEntry/u, 'no forced View switch on open');
}); });
test('the freed View ledger row is restored on teardown', () => { test('the official ChatView row stays live; only its tab id is hidden', () => {
assert.match(source, /retireHostedChatView/u); assert.match(source, /retireHostedChatView/u);
assert.match(source, /yield retireHostedChatView\(/u); assert.match(source, /yield retireHostedChatView\(/u);
assert.match(source, /yield installOfficialSlots\(ctx\)/u); assert.match(source, /yield installOfficialSlots\(ctx\)/u);
// Removing a live ledger row would skip the official disposer's releaseEntry
// and leak conversation.chat.flow / the Chat store on ui-chat teardown.
assert.doesNotMatch(seat, /\.splice\(/u, 'never remove an official ledger row');
assert.match(seat, /entry\.options\.id = undefined/u);
assert.match(seat, /ACTIVE\.set\(ledger, restore\)/u);
}); });