diff --git a/mobile/scripts/mock-server-rpc-handlers.ts b/mobile/scripts/mock-server-rpc-handlers.ts index d704c79e8..c5aebf054 100644 --- a/mobile/scripts/mock-server-rpc-handlers.ts +++ b/mobile/scripts/mock-server-rpc-handlers.ts @@ -12,6 +12,7 @@ import { handleMockFilePreviewRequest } from './mock-server-file-preview-data' import { handleMockGitRequest } from './mock-server-git-state' import { handleMockAccountRequest } from './mock-server-account-rpc' import { handleMockNativeChatRequest } from './mock-server-native-chat-scenario' +import { handleMockSessionTabsRequest } from './mock-server-session-tabs-fixture' import { createMockTerminals, FAKE_SCROLLBACK, @@ -124,7 +125,8 @@ export function handleRequest( handleMockGitRequest(request, respond, success) || handleMockFilePreviewRequest(request, respond, success, error) || handleMockAccountRequest(request, respond, success, error) || - handleMockNativeChatRequest(request, respond, success, error, ws) + handleMockNativeChatRequest(request, respond, success, error, ws) || + handleMockSessionTabsRequest(request, respond, success, terminalListWorktreeId) ) { return } diff --git a/mobile/scripts/mock-server-session-tabs-fixture.ts b/mobile/scripts/mock-server-session-tabs-fixture.ts new file mode 100644 index 000000000..fbc94c282 --- /dev/null +++ b/mobile/scripts/mock-server-session-tabs-fixture.ts @@ -0,0 +1,69 @@ +import { randomUUID } from 'node:crypto' +import type { RuntimeMobileSessionTabsResult } from '../../src/shared/runtime-types' +import type { RpcRequest, RpcResponse } from './mock-server-rpc-handlers' + +// Why: the client's snapshot-acceptance gate keys on the publisher epoch, so it +// must stay stable for the process and change on restart like a real publisher — +// hence a uuid, not a clock read two restarts could land on. +// The `mobile-local:` prefix is reserved for phone-local writes — never use it. +const PUBLICATION_EPOCH = `mock-server:${randomUUID()}` +const GROUP_ID = 'group-1' +const PARENT_TAB_ID = 'tab-1' +// The host only ever publishes terminal-layout UUIDs here; pane-key parsing +// rejects any other shape, so a placeholder would mask pane-attribution bugs. +const LEAF_ID = 'f47ac10b-58cc-4372-a567-0e02b2c3d479' +// The host publishes terminal surfaces as `${parentTabId}::${leafId}`. +const SURFACE_TAB_ID = `${PARENT_TAB_ID}::${LEAF_ID}` + +/** One ready terminal tab bound to the `term-1` fixture. Mirrors the full + * `session.tabs.list` contract so mock-server repros of tab, split-pane, and + * pane-attribution bugs aren't shape-incomplete. */ +function createMockSessionTabs(worktreeId: string): RuntimeMobileSessionTabsResult { + return { + worktree: worktreeId, + publicationEpoch: PUBLICATION_EPOCH, + snapshotVersion: 1, + activeGroupId: GROUP_ID, + activeTabId: SURFACE_TAB_ID, + activeTabType: 'terminal', + // Groups track top-level tabs, so they carry parentTabId, not surface ids. + tabGroups: [ + { + id: GROUP_ID, + activeTabId: PARENT_TAB_ID, + tabOrder: [PARENT_TAB_ID], + recentTabIds: [PARENT_TAB_ID] + } + ], + tabs: [ + { + type: 'terminal', + id: SURFACE_TAB_ID, + title: 'zsh', + parentTabId: PARENT_TAB_ID, + leafId: LEAF_ID, + status: 'ready', + terminal: 'term-1', + isActive: true + } + ] + } +} + +/** Default session-tabs backend: without it the session screen hangs on + * 'Loading tabs'. Returns false for methods it does not own. */ +export function handleMockSessionTabsRequest( + request: RpcRequest, + respond: (response: RpcResponse) => void, + success: (id: string, result: unknown, streaming?: boolean) => RpcResponse, + // Shared with `terminal.list` so both surfaces agree on which worktree an + // absent or `id:`-prefixed selector means. + resolveWorktreeId: (selector: unknown) => string | undefined +): boolean { + if (request.method !== 'session.tabs.list') { + return false + } + const worktreeId = resolveWorktreeId(request.params?.worktree) ?? 'mock' + respond(success(request.id, createMockSessionTabs(worktreeId))) + return true +} diff --git a/mobile/src/mock-server-session-tabs-fixture.test.ts b/mobile/src/mock-server-session-tabs-fixture.test.ts new file mode 100644 index 000000000..2ff9e82ba --- /dev/null +++ b/mobile/src/mock-server-session-tabs-fixture.test.ts @@ -0,0 +1,77 @@ +import { describe, expect, it } from 'vitest' +import type { WebSocket } from 'ws' +import { + handleRequest, + type RpcRequest, + type RpcResponse +} from '../scripts/mock-server-rpc-handlers' + +function callRpc(method: string, params?: Record): RpcResponse { + let response: RpcResponse | undefined + const request: RpcRequest = { id: 'request-1', method, ...(params ? { params } : {}) } + handleRequest( + request, + (nextResponse) => { + response = nextResponse + }, + {} as WebSocket + ) + expect(response).toBeDefined() + return response! +} + +function listSessionTabs(worktree: string): RpcResponse { + return callRpc('session.tabs.list', { worktree }) +} + +describe('mock server session tabs fixture', () => { + it('returns a contract-complete terminal surface for the requested worktree', () => { + const response = listSessionTabs('id:repo-1::worktree-1') + + expect(response.result).toEqual({ + worktree: 'repo-1::worktree-1', + publicationEpoch: expect.stringMatching(/^mock-server:/), + snapshotVersion: 1, + activeGroupId: 'group-1', + activeTabId: 'tab-1::f47ac10b-58cc-4372-a567-0e02b2c3d479', + activeTabType: 'terminal', + tabGroups: [ + { + id: 'group-1', + activeTabId: 'tab-1', + tabOrder: ['tab-1'], + recentTabIds: ['tab-1'] + } + ], + tabs: [ + { + type: 'terminal', + id: 'tab-1::f47ac10b-58cc-4372-a567-0e02b2c3d479', + title: 'zsh', + parentTabId: 'tab-1', + leafId: 'f47ac10b-58cc-4372-a567-0e02b2c3d479', + status: 'ready', + terminal: 'term-1', + isActive: true + } + ] + }) + }) + + it('passes a bare worktree selector through unprefixed', () => { + const response = listSessionTabs('repo-1::worktree-1') + + expect((response.result as { worktree: string }).worktree).toBe('repo-1::worktree-1') + }) + + it('falls back to the same worktree terminal.list uses when no selector is sent', () => { + const terminals = callRpc('terminal.list').result as { + terminals: { worktreeId: string }[] + } + const expected = terminals.terminals[0]?.worktreeId + expect(expected).toBeTruthy() + + const tabs = callRpc('session.tabs.list').result as { worktree: string } + expect(tabs.worktree).toBe(expected) + }) +})