From bc1c6be74712bf407e997845de933e59c08e0997 Mon Sep 17 00:00:00 2001 From: gyixuan Date: Mon, 28 Sep 2026 14:38:12 +0800 Subject: [PATCH] test(native): pin activate-focus and close-meta; document the native lifecycle - five cases over the real createNativeSurface: activate rides ISidebarRight.focus for a known record (and stays a no-op for unknown ids), close hands the record meta back and rides the on-screen face for the mounted session (per-session face otherwise) - one service-level case: closeTab hands the native close meta through to descriptor.onClose - guide: activateTab's native path (focus via ISidebarRight.focus, dsh >= 0.1.5) and the close-meta carry, including the standing gap that a user-initiated x-close never reaches onClose --- docs/external-plugin-guide.md | 8 +-- tests/native-surface.spec.ts | 95 +++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 4 deletions(-) diff --git a/docs/external-plugin-guide.md b/docs/external-plugin-guide.md index d1dc6ed8..c0ef1d7e 100644 --- a/docs/external-plugin-guide.md +++ b/docs/external-plugin-guide.md @@ -14,13 +14,13 @@ - `registerTab` / `registerFileViewer` 签名不变; - `openTab` / `openFile` 默认落到原生右侧栏;新增可选 `OpenTabSeed.target`(`'right'` 默认 / `'bottom'` 落插件的底部工作台); -- `updateTab` / `closeTab` / `activateTab` 认识原生 tab id(插件为每个原生 tab 维护一条合成 `SidebarTab` 记录,`tab.meta` / `tab.path` 的写入照旧生效)。 +- `updateTab` / `closeTab` / `activateTab` 认识原生 tab id(插件为每个原生 tab 维护一条合成 `SidebarTab` 记录,`tab.meta` / `tab.path` 的写入照旧生效);`activateTab` 对原生 tab 经宿主 `ISidebarRight.focus`(dsh ≥ 0.1.5)聚焦 tab 及其所在 pane——多实例外部插件 tab(无 `(kind, 地址)` 身份可重开)因此也能被程序化唤起到前台。 行为差异(写在这里以免踩坑): | 事项 | 说明 | |---|---| -| 生命周期回调 | 原生面只有「一次打开」,不区分新建/聚焦,因此只触发 `onOpen`(`onActivate` 仅在插件自己的底部工作台里触发) | +| 生命周期回调 | 原生面的「打开」不区分新建/聚焦,因此 `onOpen` 每次打开都触发(`onActivate` 仅在插件自己的底部工作台里触发)。程序化 `closeTab` 与 `activateTab` 在原生 tab 上正常工作:关闭走宿主 per-session 面,回调拿到完整 `tab`(**含 `meta`**);激活经 `ISidebarRight.focus` 聚焦 tab 与其 pane。**例外**:用户直接点原生 tab 上的 × 关闭不会触发 `onClose`(宿主不广播该事件,插件在 tab 体卸载时丢弃记录) | | 去重 | 原生按 `(kind, 地址)` 去重:有 `createTab` 的类型每次新开一个 tab(sidechat / diff),其余聚焦已有 tab;`dedupeKey` 的自定义语义不参与原生面 | | 布局持久化 | 原生栏的布局**只在内存**(刷新后回到折叠默认),插件自己的底部工作台仍然持久化 | | 跨会话打开 | 目标会话的右侧栏 store 未挂载时,打开会排队到该会话上屏后重放 | @@ -248,9 +248,9 @@ interface TabDescriptor { badge?: (ctx: Context, scope: SessionScope, state: SidebarState) => string | number | null | undefined /** * 生命周期回调(v0.12.0+),只由 SERVICE 路径触发: - * - onOpen:openTab 真正**新建** tab 后(dedupe/id 安全网聚焦不算打开); + * - onOpen:openTab 真正**新建** tab 后(dedupe/id 安全网聚焦不算打开;原生面每次打开都触发); * - onActivate:tab 被聚焦时(dedupe 聚焦、id 安全网聚焦、tab 栏点击激活); - * - onClose:closeTab 关闭 tab 后。 + * - onClose:closeTab 关闭 tab 后(原生 tab 同样携带完整 tab,含 meta;用户点宿主 × 的关闭除外)。 * 内置专属流程(diff 拆分放置、sidechat 线程重开)直接改 state,不触发 * 回调——但它们只作用于内置类型(diff / sidechat),外部插件的 tab 永远走 * service 路径。回调抛错只 console.error,绝不打断打开/关闭流程。 diff --git a/tests/native-surface.spec.ts b/tests/native-surface.spec.ts index 88ca574c..786ce300 100644 --- a/tests/native-surface.spec.ts +++ b/tests/native-surface.spec.ts @@ -9,6 +9,7 @@ import { createElement } from 'react' import { createRoot, type Root } from 'react-dom/client' import { act } from 'react-dom/test-utils' import { createNativeTabRecords, NativeTabBody, NativeTabTitle } from '../src/client/native/tab-adapter.tsx' +import { createNativeSurface } from '../src/client/native/surface.ts' import { registerNativeSurface } from '../src/client/native/index.ts' import { createBetterSidebarService, type SidebarSurface } from '../src/client/service.ts' import { createSidebarStore, type SidebarTab } from '../src/client/state.ts' @@ -193,6 +194,30 @@ describe('service routing into the native surface', () => { expect(() => service.closeTab('other', scope)).not.toThrow() }) + it('hands the native close meta to descriptor.onClose', () => { + // Regression #644: the native close path projected the record down to + // type/title, starving meta-driven lifecycle consumers (recently-closed + // records, reopen flows). + const surface: SidebarSurface = { + openTab: () => {}, + openResource: () => {}, + fileAddress: () => 'addr', + close: () => ({ type: 'my-plugin:term', title: 'T', meta: { threadId: 't-9' } }), + update: () => false, + activate: () => false, + has: () => true, + } + const store = createSidebarStore() + store.setSession('s1') + const service = createBetterSidebarService(store) + service.setSurface(surface) + const seen: SidebarTab[] = [] + service.registerTab({ id: 'my-plugin:term', title: 'Terminal', component: () => null, onClose: tab => { seen.push(tab) } }) + service.closeTab('native-1', scope) + expect(seen).toHaveLength(1) + expect(seen[0]).toMatchObject({ id: 'native-1', type: 'my-plugin:term', title: 'T', meta: { threadId: 't-9' } }) + }) + it('refuses a disabled type before touching the surface', () => { const { service, calls } = mount() service.setSurface(undefined) @@ -212,6 +237,76 @@ describe('service routing into the native surface', () => { }) }) +describe('createNativeSurface activate/close (the real adapter)', () => { + const mountSurface = (): { + surface: ReturnType + records: ReturnType + focus: ReturnType + close: ReturnType + } => { + const focus = vi.fn() + const close = vi.fn() + const controller = { + openTab: () => {}, + openResource: () => {}, + close, + // `ISidebarRight.focus` (dsh >= 0.1.5): the face activate() rides. + focus, + mounted: { getSnapshot: () => 's1', subscribe: () => () => {} }, + openTabIn: () => {}, + openResourceIn: () => {}, + closeIn: () => {}, + } + const records = createNativeTabRecords() + const ctx = { + get: (name: string) => (name === 'sidebarRight' ? controller : undefined), + sessions: { list: { subscribe: () => () => {}, getSnapshot: () => ({}) } }, + } + const surface = createNativeSurface(ctx as never, records) + return { surface, records, focus, close } + } + + it('activate focuses an existing record through the controller focus face', () => { + // Regression #644: activate() used to return records.has(tabId) without + // focusing anything — external plugins with multi-instance native tabs + // (dsh-sidenote's side chats) could never bring a tab to the front. + const { surface, records, focus } = mountSurface() + records.ensure({ id: 'tab-9', kind: 'sidechat', title: 'Side Chat', params: { meta: { threadId: 't-1' } }, scope }) + expect(surface.activate('tab-9')).toBe(true) + expect(focus).toHaveBeenCalledTimes(1) + expect(focus).toHaveBeenCalledWith('tab-9') + }) + + it('activate leaves unknown ids alone and reports them', () => { + const { surface, focus } = mountSurface() + expect(surface.activate('missing')).toBe(false) + expect(focus).not.toHaveBeenCalled() + }) + + it('close returns the record meta and rides the on-screen face for the mounted session', () => { + const { surface, records, close } = mountSurface() + records.ensure({ id: 'tab-10', kind: 'sidechat', title: 'hello thread', params: { meta: { threadId: 't-7' } }, scope }) + const closed = surface.close('s1', 'tab-10') + expect(closed).toEqual({ type: 'sidechat', title: 'hello thread', meta: { threadId: 't-7' } }) + // The mounted seat is s1's, so the host close rode the on-screen face. + expect(close).toHaveBeenCalledWith('tab-10') + expect(records.has('tab-10')).toBe(false) + }) + + it('close of a non-mounted session rides the per-session face', () => { + const { surface, records } = mountSurface() + records.ensure({ id: 'tab-11', kind: 'sidechat', title: 'bg', params: undefined, scope }) + const closed = surface.close('s2', 'tab-11') + expect(closed).toEqual({ type: 'sidechat', title: 'bg' }) + }) + + it('close of an unknown id is undefined (a strict no-op)', () => { + const { surface, close } = mountSurface() + expect(surface.close('s1', 'missing')).toBeUndefined() + expect(close).not.toHaveBeenCalled() + }) +}) + describe('registerNativeSurface lifecycle (service-driven registration)', () => { it('registers the native tab types when the tab-type registry ARRIVES after the slot declaration', () => { // Regression: the native seat declares `sidebar.right.pane.tab` before it