Skip to content

The agent that grows with you

test(desktop): guard the renderer's terminal gateway ownership

Tekniumb564465a8e48
diff --git a/apps/desktop/electron/terminal-gateway-owner.test.ts b/apps/desktop/electron/terminal-gateway-owner.test.ts
index cbd98825f05..a2a72b3f273 100644
--- a/apps/desktop/electron/terminal-gateway-owner.test.ts
+++ b/apps/desktop/electron/terminal-gateway-owner.test.ts
@@ -179,3 +179,22 @@ test('app quit kills shells without telling the tabs they exited', async () => {
assert.equal(local.fake.pty.kill.mock.calls.length >= 1, true)
assert.equal(local.sender.send.mock.calls.filter(([channel]) => String(channel).endsWith(':exit')).length, 0)
})
+
+test("an SSH tab finds the pool's tunnel published under its bootstrap key (#97345)", () => {
+ // The pool's single writer keys the tunnel by its bootstrap scope and stamps the registry id.
+ const pool = new Map([['ssh:nas.lan:work', { registryConnectionId: 'nas', ssh: { host: 'nas.lan' } }]])
+
+ assert.deepEqual(terminalGatewayTarget({ connectionId: 'nas', profile: 'work' }, registry, pool), {
+ scope: 'ssh:nas.lan:work',
+ ssh: { host: 'nas.lan' }
+ })
+ assert.equal(terminalGatewayTarget({ connectionId: 'nas', profile: 'work' }, registry, new Map()), 'pending')
+})
+
+test('a tab that recorded no gateway adopts This device when its shell ran here', async () => {
+ const { start } = register(null)
+ const ownerless = await spawnFor(start, {})
+
+ assert.notEqual(ownerless.argv?.[0], '/usr/bin/ssh')
+ assert.equal(ownerless.session.connectionId, 'local')
+})
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts b/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
index e654241d2fa..1af35728b52 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
@@ -40,6 +40,23 @@ describe('terminal store persistence', () => {
])
})
+ it('a restored tab keeps the gateway it recorded; a later adoption never moves it', async () => {
+ const first = await loadTerminalStore()
+ const nasTab = first.createTerminal('/repo', { connectionId: 'nas', profile: 'work' })
+ const freshTab = first.createTerminal('/repo')
+
+ // Relaunch: the SSH tab must not come back gateway-less (it would follow the Sessions pick).
+ vi.resetModules()
+ const { $terminals, adoptTerminalGateway } = await loadTerminalStore()
+
+ expect($terminals.get().find(term => term.id === nasTab)).toMatchObject({ connectionId: 'nas', profile: 'work' })
+
+ adoptTerminalGateway(nasTab, { connectionId: 'local' })
+ adoptTerminalGateway(freshTab, { connectionId: 'local' })
+ expect($terminals.get().find(term => term.id === nasTab)).toMatchObject({ connectionId: 'nas', profile: 'work' })
+ expect($terminals.get().find(term => term.id === freshTab)?.connectionId).toBe('local')
+ })
+
it('persists user tabs and history synchronously, skipping agent mirrors', async () => {
const { createTerminal, ensureAgentTerminal, renameTerminal, selectTerminal, updateTerminalReviveBuffer } =
await loadTerminalStore()
diff --git a/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.test.tsx b/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.test.tsx
index a8efea119c5..f4ccaee6daf 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.test.tsx
+++ b/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.test.tsx
@@ -1,9 +1,12 @@
import { act, cleanup, render, screen, waitFor } from '@testing-library/react'
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+import type { DesktopConnectionsRegistry } from '@/global'
import { installBrowserDesktopBridge } from '@/lib/browser-desktop-bridge'
import { WEBAPP_LAUNCH_REQUIRED } from '@/lib/browser-launch-session'
import { $desktopBoot } from '@/store/boot'
+import { $connectionsRegistry } from '@/store/connections'
+import { $connection } from '@/store/session'
import { clearWebappSession, seedWebappSession } from '@/test/webapp-session'
import { $terminals, closeAllTerminals, closeOtherTerminals, createTerminal, updateTerminalReviveBuffer } from './terminals'
@@ -513,3 +516,61 @@ it('native Desktop: an SSH tab keeps Enter armed while its gateway is offline',
expect(start).toHaveBeenCalledTimes(3)
expect(start.mock.calls[2][0]).toMatchObject({ connectionId: 'nas', profile: 'default' })
})
+
+function nativeTerminalApi(start: ReturnType<typeof vi.fn>) {
+ const attach = vi.fn(async () => true)
+
+ delete window.document.documentElement.dataset.hermesDesktopHost
+ ;(window as unknown as { hermesDesktop: unknown }).hermesDesktop = {
+ terminal: {
+ attach,
+ cwd: vi.fn(async () => null),
+ dispose: vi.fn(async () => true),
+ onData: vi.fn(() => () => {}),
+ onExit: vi.fn(() => () => {}),
+ resize: vi.fn(async () => true),
+ start,
+ write: vi.fn(async () => true)
+ }
+ }
+
+ return attach
+}
+
+// The PR's contract: a tab's recorded gateway wins over whatever Sessions has picked since.
+it('native Desktop: a recorded SSH tab starts on its gateway while Sessions shows This device', async () => {
+ $connectionsRegistry.set({
+ connections: [
+ { id: 'local', kind: 'local', label: 'This device' },
+ { id: 'nas', kind: 'ssh', label: 'NAS' }
+ ],
+ primary: 'local',
+ version: 2
+ } as unknown as DesktopConnectionsRegistry)
+ $connection.set({ connectionId: 'local' } as never)
+
+ try {
+ const start = vi.fn(async (_payload: unknown) => ({ connectionId: 'nas', id: 'pty-1', shell: 'ssh' }))
+ nativeTerminalApi(start)
+ const id = createTerminal('/work', { connectionId: 'nas', profile: 'work' })
+ render(<Harness id={id} />)
+ await waitFor(() => expect(screen.getByText('open')).toBeTruthy())
+
+ expect(start.mock.calls[0][0]).toMatchObject({ connectionId: 'nas', profile: 'work' })
+ } finally {
+ $connection.set(null)
+ $connectionsRegistry.set(null)
+ }
+})
+
+it('native Desktop: a removed gateway shows the exited state and never attaches', async () => {
+ const start = vi.fn(async () => ({ connectionId: 'nas', cwd: null, id: '', shell: '', unavailable: 'missing' }))
+ const attach = nativeTerminalApi(start)
+ const id = createTerminal('/work', { connectionId: 'nas', profile: 'work' })
+ render(<Harness id={id} />)
+
+ await waitFor(() => expect(emulator.output).toContain('removed'))
+ expect(screen.getByText('closed')).toBeTruthy()
+ expect(attach).not.toHaveBeenCalled()
+ expect($terminals.get().some(tab => tab.id === id)).toBe(true)
+})