hermes-agent/apps/desktop/electron/connection-apply.test.ts
teknium1 a4f93ce250 test: purge low-value tests, lane js01 (202 removed)
Change-detectors, tautologies, source-reading tests, redundant duplicates,
mock-echo tests and dead/unrunnable tests. Per-test rationale in the lane
ledger (category + reason for every removal).
2026-09-23 03:15:26 -07:00

162 lines
4.5 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import {
applyConnectionChange,
commitConnectionFailure,
resolveTerminalConnection,
sshQuitShouldBlock,
teardownSshState
} from './connection-apply'
function deferred() {
let resolve!: () => void
const promise = new Promise<void>(done => {
resolve = done
})
return { promise, resolve }
}
describe('applyConnectionChange', () => {
it('serializes connection changes behind bootstrap rollback before teardown and apply', async () => {
const gate = deferred()
const events: string[] = []
const run = applyConnectionChange({
cancelAndWait: async () => {
events.push('cancel')
await gate.promise
events.push('drained')
},
isPrimary: true,
scope: '',
sendApplied: () => events.push('applied'),
stopPool: vi.fn(),
teardownPrimary: async () => {
events.push('primary')
},
teardownSsh: async () => {
events.push('ssh')
}
})
await Promise.resolve()
expect(events).toEqual(['cancel'])
gate.resolve()
await run
expect(events).toEqual(['cancel', 'drained', 'ssh', 'primary', 'applied'])
})
it('tears down only a non-primary scope without applying the primary connection', async () => {
const events: string[] = []
await applyConnectionChange({
cancelAndWait: async scope => {
events.push(`cancel:${scope}`)
},
isPrimary: false,
scope: 'worker',
sendApplied: () => events.push('applied'),
stopPool: scope => events.push(`pool:${scope}`),
teardownPrimary: async () => {
events.push('primary')
},
teardownSsh: async scope => {
events.push(`ssh:${scope}`)
}
})
expect(events).toEqual(['cancel:worker', 'ssh:worker', 'pool:worker'])
})
})
describe('resolveTerminalConnection', () => {
it('joins an in-flight backend before resolving the SSH terminal target', async () => {
const target = { ssh: {}, scope: '' }
const getTarget = vi.fn().mockReturnValueOnce('pending').mockReturnValueOnce(target)
const ensureBackend = vi.fn(async () => undefined)
await expect(resolveTerminalConnection(getTarget, ensureBackend)).resolves.toBe(target)
expect(ensureBackend).toHaveBeenCalledOnce()
})
it('does not start a local terminal while configured SSH remains unavailable', async () => {
await expect(
resolveTerminalConnection(
() => 'pending',
async () => undefined
)
).rejects.toThrow('not ready')
})
})
it.each([
[false, 1, 0, false, true],
[false, 0, 1, false, true],
[false, 0, 0, true, true],
[true, 1, 1, true, false],
[false, 0, 0, false, false]
] as const)(
'quit guard: done=%s connections=%s bootstrap=%s stopping=%s → %s',
(
teardownDone: boolean,
connectionCount: number,
bootstrapPending: number,
stopping: boolean,
expected: boolean
): void => {
expect(
sshQuitShouldBlock({
teardownDone,
connectionCount,
bootstrapPending,
inFlight: stopping ? Promise.resolve() : null
})
).toBe(expected)
}
)
describe('teardownSshState', () => {
it('terminates the owned remote backend before closing its tunnel and SSH transport', async () => {
const events: string[] = []
const ssh = {
cancelForward: async () => events.push('forward'),
close: async () => events.push('ssh')
}
await teardownSshState(
{ ssh, ownershipId: 'owner', localPort: 1234, remotePort: 5678 },
{ cleanupRemote: async () => events.push('remote') }
)
expect(events).toEqual(['remote', 'forward', 'ssh'])
})
it('still closes the SSH transport when remote cleanup fails', async () => {
const close = vi.fn(async () => undefined)
await teardownSshState(
{ ssh: { cancelForward: vi.fn(async () => undefined), close }, ownershipId: 'owner' },
{
cleanupRemote: async () => {
throw new Error('remote unavailable')
}
}
)
expect(close).toHaveBeenCalledOnce()
})
})
describe('commitConnectionFailure', () => {
it('prevents a stale bootstrap from publishing failure state', () => {
const stale = Promise.resolve('stale')
const current = Promise.resolve('current')
const commit = vi.fn()
expect(commitConnectionFailure(current, stale, commit)).toBe(false)
expect(commit).not.toHaveBeenCalled()
expect(commitConnectionFailure(current, current, commit)).toBe(true)
expect(commit).toHaveBeenCalledOnce()
})
})