|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import type { PlatformAdapter, PtyInfo } from './platform/types'; |
| 3 | +import type { PersistedSession } from './session-types'; |
| 4 | + |
| 5 | +const terminalRegistryMocks = vi.hoisted(() => ({ |
| 6 | + reconnectTerminal: vi.fn(), |
| 7 | + restoreTerminal: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock('./terminal-registry', () => ({ |
| 11 | + reconnectTerminal: terminalRegistryMocks.reconnectTerminal, |
| 12 | + restoreTerminal: terminalRegistryMocks.restoreTerminal, |
| 13 | +})); |
| 14 | + |
| 15 | +import { reconnectFromInit } from './reconnect'; |
| 16 | + |
| 17 | +function createPlatform(ptys: PtyInfo[], savedState: PersistedSession | null): PlatformAdapter { |
| 18 | + const listHandlers = new Set<(detail: { ptys: PtyInfo[] }) => void>(); |
| 19 | + const replayHandlers = new Set<(detail: { id: string; data: string }) => void>(); |
| 20 | + |
| 21 | + return { |
| 22 | + init: async () => {}, |
| 23 | + shutdown: () => {}, |
| 24 | + getAvailableShells: vi.fn(async () => []), |
| 25 | + spawnPty: vi.fn(), |
| 26 | + writePty: vi.fn(), |
| 27 | + resizePty: vi.fn(), |
| 28 | + killPty: vi.fn(), |
| 29 | + getCwd: vi.fn(async () => null), |
| 30 | + getScrollback: vi.fn(async () => null), |
| 31 | + onPtyData: vi.fn(), |
| 32 | + offPtyData: vi.fn(), |
| 33 | + onPtyExit: vi.fn(), |
| 34 | + offPtyExit: vi.fn(), |
| 35 | + requestInit: vi.fn(() => { |
| 36 | + for (const handler of listHandlers) handler({ ptys }); |
| 37 | + for (const pty of ptys) { |
| 38 | + for (const handler of replayHandlers) handler({ id: pty.id, data: `${pty.id}-replay` }); |
| 39 | + } |
| 40 | + }), |
| 41 | + onPtyList: (handler) => { listHandlers.add(handler); }, |
| 42 | + offPtyList: (handler) => { listHandlers.delete(handler); }, |
| 43 | + onPtyReplay: (handler) => { replayHandlers.add(handler); }, |
| 44 | + offPtyReplay: (handler) => { replayHandlers.delete(handler); }, |
| 45 | + onRequestSessionFlush: vi.fn(), |
| 46 | + offRequestSessionFlush: vi.fn(), |
| 47 | + notifySessionFlushComplete: vi.fn(), |
| 48 | + alarmRemove: vi.fn(), |
| 49 | + alarmToggle: vi.fn(), |
| 50 | + alarmDisable: vi.fn(), |
| 51 | + alarmDismiss: vi.fn(), |
| 52 | + alarmDismissOrToggle: vi.fn(), |
| 53 | + alarmAttend: vi.fn(), |
| 54 | + alarmResize: vi.fn(), |
| 55 | + alarmClearAttention: vi.fn(), |
| 56 | + alarmToggleTodo: vi.fn(), |
| 57 | + alarmMarkTodo: vi.fn(), |
| 58 | + alarmClearTodo: vi.fn(), |
| 59 | + alarmDrainTodoBucket: vi.fn(), |
| 60 | + onAlarmState: vi.fn(), |
| 61 | + offAlarmState: vi.fn(), |
| 62 | + saveState: vi.fn(), |
| 63 | + getState: vi.fn(() => savedState), |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +describe('reconnectFromInit', () => { |
| 68 | + beforeEach(() => { |
| 69 | + vi.clearAllMocks(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('restores saved visible layout and detached doors for matching live PTYs', async () => { |
| 73 | + const layout = { |
| 74 | + panels: { |
| 75 | + 'pane-a': {}, |
| 76 | + 'pane-b': {}, |
| 77 | + }, |
| 78 | + }; |
| 79 | + const detached = [{ |
| 80 | + id: 'pane-c', |
| 81 | + title: 'Pane C', |
| 82 | + neighborId: 'pane-b', |
| 83 | + direction: 'right' as const, |
| 84 | + remainingPanelIds: ['pane-a', 'pane-b'], |
| 85 | + restoreLayout: layout, |
| 86 | + detachedLayoutSignature: 'sig', |
| 87 | + }]; |
| 88 | + const saved: PersistedSession = { |
| 89 | + version: 1, |
| 90 | + layout, |
| 91 | + detached, |
| 92 | + panes: [ |
| 93 | + { id: 'pane-a', title: 'Pane A', cwd: null, scrollback: null, resumeCommand: null }, |
| 94 | + { id: 'pane-b', title: 'Pane B', cwd: null, scrollback: null, resumeCommand: null }, |
| 95 | + { id: 'pane-c', title: 'Pane C', cwd: null, scrollback: null, resumeCommand: null }, |
| 96 | + ], |
| 97 | + }; |
| 98 | + |
| 99 | + const result = await reconnectFromInit(createPlatform([ |
| 100 | + { id: 'pane-a', alive: true }, |
| 101 | + { id: 'pane-b', alive: true }, |
| 102 | + { id: 'pane-c', alive: true }, |
| 103 | + ], saved)); |
| 104 | + |
| 105 | + expect(result).toEqual({ |
| 106 | + paneIds: ['pane-a', 'pane-b'], |
| 107 | + detached, |
| 108 | + layout, |
| 109 | + }); |
| 110 | + expect(terminalRegistryMocks.reconnectTerminal).toHaveBeenCalledWith('pane-c', 'pane-c-replay', { |
| 111 | + alive: true, |
| 112 | + exitCode: undefined, |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('does not reuse a saved layout when live PTYs do not match saved panes', async () => { |
| 117 | + const saved: PersistedSession = { |
| 118 | + version: 1, |
| 119 | + layout: { panels: { 'pane-a': {}, 'pane-b': {} } }, |
| 120 | + panes: [ |
| 121 | + { id: 'pane-a', title: 'Pane A', cwd: null, scrollback: null, resumeCommand: null }, |
| 122 | + { id: 'pane-b', title: 'Pane B', cwd: null, scrollback: null, resumeCommand: null }, |
| 123 | + ], |
| 124 | + }; |
| 125 | + |
| 126 | + const result = await reconnectFromInit(createPlatform([ |
| 127 | + { id: 'pane-a', alive: true }, |
| 128 | + { id: 'pane-b', alive: true }, |
| 129 | + { id: 'extra-pane', alive: true }, |
| 130 | + ], saved)); |
| 131 | + |
| 132 | + expect(result).toEqual({ |
| 133 | + paneIds: ['pane-a', 'pane-b', 'extra-pane'], |
| 134 | + detached: [], |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + it('ignores stale saved panes when the saved layout still matches live visible panes', async () => { |
| 139 | + const layout = { panels: { 'pane-a': {}, 'pane-b': {} } }; |
| 140 | + const saved: PersistedSession = { |
| 141 | + version: 1, |
| 142 | + layout, |
| 143 | + panes: [ |
| 144 | + { id: 'pane-a', title: 'Pane A', cwd: null, scrollback: null, resumeCommand: null }, |
| 145 | + { id: 'pane-b', title: 'Pane B', cwd: null, scrollback: null, resumeCommand: null }, |
| 146 | + { id: 'stale-pane', title: 'Stale Pane', cwd: null, scrollback: null, resumeCommand: null }, |
| 147 | + ], |
| 148 | + }; |
| 149 | + |
| 150 | + const result = await reconnectFromInit(createPlatform([ |
| 151 | + { id: 'pane-a', alive: true }, |
| 152 | + { id: 'pane-b', alive: true }, |
| 153 | + ], saved)); |
| 154 | + |
| 155 | + expect(result).toEqual({ |
| 156 | + paneIds: ['pane-a', 'pane-b'], |
| 157 | + detached: [], |
| 158 | + layout, |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments