diff --git a/src/client/native/surface.ts b/src/client/native/surface.ts index 6dc08617..4c1cd134 100644 --- a/src/client/native/surface.ts +++ b/src/client/native/surface.ts @@ -10,10 +10,11 @@ * * - the surface exists only while a session's panel is mounted, and the * service's public face (`ISidebarRight`) writes only into THAT session. - * The controller also carries `openTabIn` / `openResourceIn` / - * `closeIn`, which act on any session whose store the runtime has minted; - * both are probed at call time, and an open for a session that has no - * store yet is QUEUED and replayed when that session comes on screen; + * The controller also carries `openTabIn` / `openResourceIn` / `closeIn`, + * but the two open faces are silent no-ops for a session whose store the + * runtime never minted or adopted — they report nothing back — so an open + * aimed at a session that is not on screen is NOT handed to them: it stays + * QUEUED and is replayed once that session comes on screen; * - layout state is memory-only, so a queued open is not durable either. */ import type { Context } from '../../context-types.ts' @@ -31,7 +32,12 @@ interface NativeController { openTab(kind: string, options?: { params?: unknown; revealIfOpened?: boolean }): void openResource(address: string, options?: { params?: unknown; revealIfOpened?: boolean }): void close(tabId: string): void - /** Not part of `ISidebarRight`: the concrete controller's per-session writes. */ + /** + * Not part of `ISidebarRight`: the concrete controller's per-session writes. + * The two open faces are kept as the seam a boolean-reporting host API would + * plug into, but `place()` deliberately does not use them: they cannot tell + * "written" from "no store adopted yet" (see the module header). + */ openTabIn?(sessionId: string, kind: string, options?: { params?: unknown; revealIfOpened?: boolean }): void openResourceIn?(sessionId: string, address: string, options?: { params?: unknown; revealIfOpened?: boolean }): void closeIn?(sessionId: string, tabId: string): void @@ -76,10 +82,12 @@ export function createNativeSurface(ctx: Context, records: NativeTabRecords): Na api.openTab(entry.tabKind, options) return true } - if (api.openTabIn !== undefined) { - api.openTabIn(entry.sessionId, entry.tabKind, options) - return true - } + // Do not hand a non-current session's open to openTabIn(): for a session + // whose rightbar store the runtime never adopted (it was not opened since + // this page load) that call is a silent no-op that reports nothing back, so + // trusting it drops the open instead of queueing it as this module + // documents. Leave the entry queued — flushPending() replays it once that + // session comes on screen. return false } const options = { @@ -90,10 +98,8 @@ export function createNativeSurface(ctx: Context, records: NativeTabRecords): Na api.openResource(entry.address, options) return true } - if (api.openResourceIn !== undefined) { - api.openResourceIn(entry.sessionId, entry.address, options) - return true - } + // Same as the tab branch: a session with no adopted rightbar store makes + // openResourceIn() a silent no-op, so queue instead. return false } diff --git a/tests/native-surface-cross-session.spec.ts b/tests/native-surface-cross-session.spec.ts new file mode 100644 index 00000000..94f7fb55 --- /dev/null +++ b/tests/native-surface-cross-session.spec.ts @@ -0,0 +1,129 @@ +/** + * Cross-session opens at the native right-sidebar surface + * (`src/client/native/surface.ts`). + * + * The runtime only adopts a session's rightbar store once that session has been + * mounted; for a session whose store was never minted, `openTabIn()` / + * `openResourceIn()` are silent no-ops — they write nothing and report nothing + * back. Handing an off-screen session's open to them therefore LOSES it: + * `place()` returns true, `enqueue()` never queues the entry, and the replay + * subscribed to the session list never sees it. + * + * The queueing contract this file locks down is already the documented one: + * `docs/external-plugin-guide.md` (§0 「跨会话打开」: 目标会话的右侧栏 store + * 未挂载时,打开会排队到该会话上屏后重放) and AGENTS.md §3.10. + */ +import { describe, expect, it, vi } from 'vitest' +import { createNativeSurface } from '../src/client/native/surface.ts' +import { createNativeTabRecords } from '../src/client/native/tab-adapter.tsx' + +describe('createNativeSurface cross-session opens', () => { + /** Mount a surface over a recording controller + a switchable session list. */ + const mount = (initial = 's1') => { + const list = { current: initial } + let notify: (() => void) | undefined + const controller = { + openTab: vi.fn(), + openTabIn: vi.fn(), + openResource: vi.fn(), + openResourceIn: vi.fn(), + close: vi.fn(), + } + const ctx = { + get: () => controller, + sessions: { + list: { + getSnapshot: () => ({ current: list.current }), + subscribe: (fn: () => void) => { + notify = fn + return () => { notify = undefined } + }, + }, + }, + } + return { + surface: createNativeSurface(ctx as never, createNativeTabRecords()), + controller, + /** Switch the on-screen session, then let the surface replay its queue. */ + switchTo: (sessionId: string) => { + list.current = sessionId + notify?.() + }, + } + } + + it('queues a tab open for an off-screen session instead of handing it to openTabIn()', () => { + const { surface, controller, switchTo } = mount('s1') + + surface.openTab({ sessionId: 's2', kind: 'ego-browser:watch', params: {}, revealIfOpened: true }) + + expect(controller.openTabIn).not.toHaveBeenCalled() + expect(controller.openTab).not.toHaveBeenCalled() + + switchTo('s2') + expect(controller.openTab).toHaveBeenCalledTimes(1) + expect(controller.openTab).toHaveBeenCalledWith('ego-browser:watch', { params: {}, revealIfOpened: true }) + surface.dispose() + }) + + it('replays a queued tab open exactly once', () => { + const { surface, controller, switchTo } = mount('s1') + + surface.openTab({ sessionId: 's2', kind: 'files', params: {}, revealIfOpened: true }) + switchTo('s2') + switchTo('s2') + + expect(controller.openTab).toHaveBeenCalledTimes(1) + surface.dispose() + }) + + it('keeps a queued tab open across an unrelated session switch', () => { + const { surface, controller, switchTo } = mount('s1') + + surface.openTab({ sessionId: 's2', kind: 'files', params: {}, revealIfOpened: false }) + switchTo('s3') + expect(controller.openTab).not.toHaveBeenCalled() + + switchTo('s2') + expect(controller.openTab).toHaveBeenCalledTimes(1) + surface.dispose() + }) + + it('queues a resource open for an off-screen session instead of handing it to openResourceIn()', () => { + const { surface, controller, switchTo } = mount('s1') + + surface.openResource({ + sessionId: 's2', + address: 'dsh-resource://file/session/s2/a.ts', + line: 3, + revealIfOpened: true, + }) + + expect(controller.openResourceIn).not.toHaveBeenCalled() + expect(controller.openResource).not.toHaveBeenCalled() + + switchTo('s2') + expect(controller.openResource).toHaveBeenCalledTimes(1) + expect(controller.openResource).toHaveBeenCalledWith('dsh-resource://file/session/s2/a.ts', { + params: { line: 3 }, + revealIfOpened: true, + }) + surface.dispose() + }) + + it('still writes the on-screen session opens immediately', () => { + const { surface, controller } = mount('s1') + + surface.openTab({ sessionId: 's1', kind: 'browser', params: { url: 'https://example.com' }, revealIfOpened: true }) + surface.openResource({ sessionId: 's1', address: 'dsh-resource://file/session/s1/a.ts', revealIfOpened: true }) + + expect(controller.openTab).toHaveBeenCalledWith('browser', { + params: { url: 'https://example.com' }, + revealIfOpened: true, + }) + expect(controller.openResource).toHaveBeenCalledWith('dsh-resource://file/session/s1/a.ts', { + revealIfOpened: true, + }) + surface.dispose() + }) +})