hermes-agent/apps/desktop/electron/portal-session.test.ts
Gille a7059225d5
fix(desktop): keep Cloud portal sign-in alive through redirect aborts (#123091)
* fix(desktop): keep portal sign-in alive across redirect aborts

(cherry picked from commit 3de10973b8e234853ddd0a8ed15bdb66722f5728)

* refactor(desktop): share the ERR_ABORTED predicate between both cookie windows

The remote OAuth window (main.ts, #110308) and the portal window
(portal-session.ts) each carried the same inline `code === -3 ||
/ERR_ABORTED/` test. One resolver per policy: extract it to
oauth-navigation.ts so the two windows cannot drift on what counts as a
superseded navigation. Helper and its unit test come from #89377.

Co-authored-by: Matt Earls <[email protected]>

---------

Co-authored-by: Austin Pickett <[email protected]>
Co-authored-by: Matt Earls <[email protected]>
2026-09-26 20:43:56 +00:00

75 lines
2.3 KiB
TypeScript

import { EventEmitter } from 'node:events'
import type { BrowserWindow, Session } from 'electron'
import { expect, test, vi } from 'vitest'
import { createPortalSession } from './portal-session'
vi.mock('./window-renderer-lifecycle', () => ({
installWindowRendererLifecycle: () => {}
}))
class PortalWindow extends EventEmitter {
readonly webContents = new EventEmitter()
readonly loadURL: ReturnType<typeof vi.fn>
private destroyed = false
constructor(error: Error) {
super()
this.loadURL = vi.fn().mockRejectedValue(error)
}
isDestroyed() {
return this.destroyed
}
destroy() {
this.destroyed = true
this.emit('closed')
}
}
function portalWithRejectedInitialLoad(error: Error) {
const win = new PortalWindow(error)
const cookies: { name: string; value: string }[] = []
const session = { cookies: { get: async () => cookies } } as unknown as Session
const portal = createPortalSession({
isReady: () => true,
getOauthSession: () => session,
resolvePortalBaseUrl: () => 'https://portal.example.test',
warmOauthCookieStore: async () => {},
createWindow: () => win as unknown as BrowserWindow,
rememberLog: () => {}
})
return { portal, win, cookies }
}
test('a superseded portal navigation stays open until the new access cookie lands', async () => {
const error = Object.assign(new Error('ERR_ABORTED (-3) loading the portal'), { code: -3 })
const { portal, win, cookies } = portalWithRejectedInitialLoad(error)
const result = portal.openPortalLoginWindow().then(
() => 'landed',
(failure: Error) => failure.message
)
await vi.waitFor(() => expect(win.loadURL).toHaveBeenCalledOnce())
await new Promise<void>(resolve => setImmediate(resolve))
expect(win.isDestroyed()).toBe(false)
cookies.push({ name: 'nas-session', value: 'new-access' })
win.webContents.emit('did-redirect-navigation')
expect(await result).toBe('landed')
expect(win.isDestroyed()).toBe(true)
})
test('a real portal load failure still ends sign-in', async () => {
const error = Object.assign(new Error('ERR_FAILED (-2) loading the portal'), { code: -2 })
const { portal, win } = portalWithRejectedInitialLoad(error)
await expect(portal.openPortalLoginWindow()).rejects.toThrow(error.message)
expect(win.isDestroyed()).toBe(true)
})