From aff27a4f9e7020562e1ffbb9068ae308937bef65 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 6 Sep 2026 09:38:12 -0700 Subject: [PATCH] test: cover failed re-pair selection and background window safety --- ...ed-client-runtime-environment.unit.test.ts | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/e2e/helpers/paired-client-runtime-environment.unit.test.ts diff --git a/tests/e2e/helpers/paired-client-runtime-environment.unit.test.ts b/tests/e2e/helpers/paired-client-runtime-environment.unit.test.ts new file mode 100644 index 00000000000..a7a3e0d41df --- /dev/null +++ b/tests/e2e/helpers/paired-client-runtime-environment.unit.test.ts @@ -0,0 +1,70 @@ +import { afterEach, expect, it, vi } from 'vitest' +import { rePairPairedElectronClient } from './paired-client-runtime-environment' +import type { PairedElectronClient } from './paired-electron-client' + +afterEach(() => { + vi.unstubAllGlobals() + vi.unstubAllEnvs() +}) + +function fixture(canSelectLocal: boolean) { + let selected: string | null = 'old-hub' + const remove = vi.fn(async () => { + if (selected !== null) throw new Error('Cannot remove the selected runtime') + }) + const state = { + setActiveRuntimeEnvironmentPreference: vi.fn(async (id: string | null) => { + if (id === null && !canSelectLocal) return false + selected = id + return true + }), + setRuntimeEnvironments: vi.fn(), + refreshRuntimeEnvironmentStatus: vi.fn(async () => true) + } + vi.stubGlobal('window', { + __store: { getState: () => state }, + api: { + runtimeEnvironments: { + remove, + addFromPairingCode: vi.fn(async () => ({ environment: { id: 'new-hub' } })), + list: vi.fn(async () => [{ id: 'new-hub' }]) + } + } + }) + const nativeEvaluate = vi.fn() + const reload = vi.fn(async () => undefined) + const client = { + environmentId: 'old-hub', + captureDirectSshAttempts: vi.fn(async () => undefined), + installDirectSshAttemptProbe: vi.fn(async () => undefined), + app: { evaluate: nativeEvaluate }, + page: { + evaluate: async (callback: (args: unknown) => unknown, args: unknown) => callback(args), + reload, + waitForFunction: vi.fn(async () => undefined) + } + } as unknown as PairedElectronClient + return { client, remove, reload, nativeEvaluate } +} + +it('keeps the old pairing when selecting local fails', async () => { + const { client, remove, reload } = fixture(false) + await expect(rePairPairedElectronClient(client, { pairingUrl: 'code' }, 'HUB')).rejects.toThrow( + 'could not select local' + ) + expect(remove).not.toHaveBeenCalled() + expect(reload).not.toHaveBeenCalled() + expect(client.environmentId).toBe('old-hub') +}) + +it('replaces the active pairing without touching native windows in background mode', async () => { + vi.stubEnv('ORCA_BACKGROUND_LAUNCH', '1') + vi.stubEnv('GITHUB_ACTIONS', 'true') + vi.stubEnv('DISPLAY', ':99') + const { client, remove, reload, nativeEvaluate } = fixture(true) + await rePairPairedElectronClient(client, { pairingUrl: 'code' }, 'HUB') + expect(remove).toHaveBeenCalledWith({ selector: 'old-hub' }) + expect(client.environmentId).toBe('new-hub') + expect(reload).toHaveBeenCalledOnce() + expect(nativeEvaluate).not.toHaveBeenCalled() +})