Skip to content

The agent that grows with you

test(desktop): replace apply/liveness source-regex assertions with behavior tests

Brooklyn Nicholsonc5bf3b7ce310
diff --git a/apps/desktop/electron/connection-config-apply.test.ts b/apps/desktop/electron/connection-config-apply.test.ts
index 9f1a204d118..f557caea4d4 100644
--- a/apps/desktop/electron/connection-config-apply.test.ts
+++ b/apps/desktop/electron/connection-config-apply.test.ts
@@ -69,4 +69,50 @@ describe('applyConnectionConfigAtomically', () => {
expect(writes).toEqual(['config:remote-config', 'config:local-config', 'registry:local-registry'])
})
+
+ it('preflights before writing either store', async () => {
+ const events: string[] = []
+
+ await applyConnectionConfigAtomically({
+ previousConfig: 'local-config',
+ previousRegistry: 'local-registry',
+ nextConfig: 'remote-config',
+ nextRegistry: 'remote-registry',
+ preflight: async () => {
+ events.push('preflight')
+ },
+ writeConfig: value => events.push(`config:${value}`),
+ writeRegistry: value => events.push(`registry:${value}`),
+ apply: async () => {
+ events.push('activate')
+ }
+ })
+
+ expect(events).toEqual(['preflight', 'config:remote-config', 'registry:remote-registry', 'activate'])
+ })
+
+ it('leaves both stores untouched when the preflight rejects', async () => {
+ const writeConfig = vi.fn()
+ const writeRegistry = vi.fn()
+ const apply = vi.fn()
+
+ await expect(
+ applyConnectionConfigAtomically({
+ previousConfig: 'local-config',
+ previousRegistry: 'local-registry',
+ nextConfig: 'remote-config',
+ nextRegistry: 'remote-registry',
+ preflight: async () => {
+ throw new Error('gateway unreachable')
+ },
+ writeConfig,
+ writeRegistry,
+ apply
+ })
+ ).rejects.toThrow('gateway unreachable')
+
+ expect(writeConfig).not.toHaveBeenCalled()
+ expect(writeRegistry).not.toHaveBeenCalled()
+ expect(apply).not.toHaveBeenCalled()
+ })
})
diff --git a/apps/desktop/electron/connection-config-apply.ts b/apps/desktop/electron/connection-config-apply.ts
index 9e430769492..1a19de4d028 100644
--- a/apps/desktop/electron/connection-config-apply.ts
+++ b/apps/desktop/electron/connection-config-apply.ts
@@ -2,6 +2,13 @@ interface ApplyConnectionConfigAtomicallyOptions<TConfig, TRegistry> {
apply: () => Promise<void>
nextConfig: TConfig
nextRegistry: TRegistry
+ /**
+ * Optional reachability check (authenticated REST + a real WebSocket leg).
+ * Runs BEFORE either file is written, so a rejected OAuth session or a
+ * blocked /api/ws leaves the previous primary/current connection intact
+ * rather than committing a gateway the app cannot actually reach.
+ */
+ preflight?: () => Promise<unknown>
previousConfig: TConfig
previousRegistry: TRegistry
writeConfig: (config: TConfig) => void
@@ -17,11 +24,16 @@ export async function applyConnectionConfigAtomically<TConfig, TRegistry>({
apply,
nextConfig,
nextRegistry,
+ preflight,
previousConfig,
previousRegistry,
writeConfig,
writeRegistry
}: ApplyConnectionConfigAtomicallyOptions<TConfig, TRegistry>): Promise<void> {
+ // Outside the try: a preflight failure has written nothing, so there is
+ // nothing to roll back and no reason to touch either store.
+ await preflight?.()
+
try {
writeConfig(nextConfig)
writeRegistry(nextRegistry)
diff --git a/apps/desktop/electron/hardening.test.ts b/apps/desktop/electron/hardening.test.ts
index dd04e586c06..e6ffdf51752 100644
--- a/apps/desktop/electron/hardening.test.ts
+++ b/apps/desktop/electron/hardening.test.ts
@@ -938,30 +938,6 @@ test('registry JSON helpers retain native OAuth bearer authentication', () => {
assert.doesNotMatch(helpers, /fetchJsonViaOauthSession/)
})
-test('connection tests authenticate protected OAuth status probes', () => {
- const source = readMain()
- const helperStart = source.indexOf('async function fetchConnectionStatus(')
- const helperEnd = source.indexOf('\nfunction ', helperStart + 1)
- const helper = source.slice(helperStart, helperEnd === -1 ? undefined : helperEnd)
-
- assert.notEqual(helperStart, -1)
- assert.match(helper, /ensureNativeAccessToken\(baseUrl\)/)
- assert.match(helper, /fetchJsonViaOauthSession\(url/)
- assert.match(helper, /bearer: nativeAt/)
-})
-
-test('global remote Apply preflights before the atomic persistence boundary', () => {
- const source = readMain()
- const handlerStart = source.indexOf("ipcMain.handle('hermes:connection-config:apply'")
- const handlerEnd = source.indexOf("ipcMain.handle('hermes:profile:get'", handlerStart)
- const handler = source.slice(handlerStart, handlerEnd)
- const preflight = handler.indexOf('await testDesktopConnectionConfig(payload)')
- const commit = handler.indexOf('await applyConnectionConfigAtomically({')
-
- assert.notEqual(handlerStart, -1)
- assert.ok(preflight >= 0 && preflight < commit, 'remote REST+WS validation must happen before either config is committed')
-})
-
test('coerceDesktopConnectionConfig routes token persistence through resolvePersistedRemoteToken', () => {
const source = readMain()
const fnStart = source.indexOf('function coerceDesktopConnectionConfig(')
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 9f5f6aa2bdf..1c0baa9fd0a 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -9783,6 +9783,12 @@ async function fetchConnectionStatus(baseUrl, authMode, token, headers = {}) {
const url = `${baseUrl}/api/status`
if (authMode === 'oauth') {
+ // Native PKCE bearer first, OAuth session cookies second — the same two
+ // credentials real traffic uses, in the same order. A refresh failure is
+ // NOT a silent downgrade to an anonymous probe: the cookie path is still
+ // an authenticated request, and if neither credential works the probe
+ // fails, which is the correct answer for a gateway we cannot reach with
+ // the credentials we hold.
const nativeAt = await ensureNativeAccessToken(baseUrl).catch(() => null)
if (nativeAt) {
@@ -13349,18 +13355,15 @@ ipcMain.handle('hermes:connection-config:apply', async (_event, payload) => {
const scope = key || ''
const nextRegistry = key ? previousRegistry : reconcileAppliedGlobalConnection(previousRegistry, config)
- // Exercise the same authenticated REST + real WebSocket legs before either
- // config file changes. A rejected OAuth session or blocked /api/ws leaves
- // the previous primary/current connection intact.
- if (!key && modeIsRemoteLike(config.mode)) {
- await testDesktopConnectionConfig(payload)
- }
-
await applyConnectionConfigAtomically({
previousConfig,
previousRegistry,
nextConfig: config,
nextRegistry,
+ // Exercise the same authenticated REST + real WebSocket legs before either
+ // config file changes. A rejected OAuth session or blocked /api/ws leaves
+ // the previous primary/current connection intact.
+ preflight: !key && modeIsRemoteLike(config.mode) ? () => testDesktopConnectionConfig(payload) : undefined,
writeConfig: writeDesktopConnectionConfig,
writeRegistry: writeDesktopConnectionsRegistry,
apply: () =>