hermes-agent/apps/desktop/electron/quit-teardown.test.ts
flyer103 e5050a040a fix(desktop): don't announce a reconnect when the app is quitting
Quitting the Desktop app wrote "[boot] Restarting desktop connection" to
desktop.log and pushed the same message to the renderer over
hermes:boot-progress. Nothing was restarting: the quit coordinator called
teardownPrimaryBackendAndWait() with the default soft=false, and soft=false
is what makes resetHermesConnectionState() call
resetBootProgressForReconnect().

Name the two teardown intents in quit-teardown.ts and use them at every
deliberate primary teardown: a teardown that re-homes ('reconnect', update
hand-off and bundle swap) keeps the announcement; one that brings nothing
back ('quit', the quit coordinator and the uninstall teardown) stays silent.
Fixes #123437.
2026-09-27 08:53:09 -05:00

136 lines
4.1 KiB
TypeScript

import assert from 'node:assert/strict'
import { test, vi } from 'vitest'
import { backendQuitNeedsWait, backendTeardownOptions, createQuitTeardownCoordinator } from './quit-teardown'
function deferred() {
let resolve!: () => void
const promise = new Promise<void>(done => {
resolve = done
})
return { promise, resolve }
}
test('remote-only cleanup does not cancel the original quit', () => {
const cleanup = vi.fn()
const requestFinalQuit = vi.fn()
const coordinator = createQuitTeardownCoordinator(requestFinalQuit)
const shouldPrevent = coordinator.begin([{ run: cleanup, waitForCompletion: false }])
assert.equal(shouldPrevent, false)
assert.equal(cleanup.mock.calls.length, 1)
assert.equal(requestFinalQuit.mock.calls.length, 0)
})
test('a settled remote descriptor is not backend work that requires a deferred quit', () => {
assert.equal(
backendQuitNeedsWait({
connectionPending: false,
poolPending: false,
processAttached: false,
shutdownPending: false
}),
false
)
for (const key of ['connectionPending', 'poolPending', 'processAttached', 'shutdownPending'] as const) {
assert.equal(
backendQuitNeedsWait({
connectionPending: false,
poolPending: false,
processAttached: false,
shutdownPending: false,
[key]: true
}),
true,
`${key} must defer quit`
)
}
})
test('one final quit waits for every required teardown branch', async () => {
const backend = deferred()
const ssh = deferred()
const requestFinalQuit = vi.fn()
const backendRun = vi.fn(() => backend.promise)
const sshRun = vi.fn(() => ssh.promise)
const coordinator = createQuitTeardownCoordinator(requestFinalQuit)
const tasks = [
{ run: backendRun, waitForCompletion: true },
{ run: sshRun, waitForCompletion: true }
]
assert.equal(coordinator.begin(tasks), true)
assert.equal(coordinator.begin(tasks), true, 'a re-entrant quit stays deferred while teardown is pending')
assert.equal(backendRun.mock.calls.length, 1)
assert.equal(sshRun.mock.calls.length, 1)
backend.resolve()
await Promise.resolve()
assert.equal(requestFinalQuit.mock.calls.length, 0)
ssh.resolve()
await Promise.all([backend.promise, ssh.promise])
await Promise.resolve()
assert.equal(requestFinalQuit.mock.calls.length, 1)
assert.equal(coordinator.begin(tasks), false, 'the coordinator must not cancel its own final quit')
assert.equal(backendRun.mock.calls.length, 1)
assert.equal(sshRun.mock.calls.length, 1)
})
test('teardown failure still releases the final quit after all branches settle', async () => {
const requestFinalQuit = vi.fn()
const coordinator = createQuitTeardownCoordinator(requestFinalQuit)
assert.equal(
coordinator.begin([
{
run: () => {
throw new Error('cleanup failed')
},
waitForCompletion: true
}
]),
true
)
await Promise.resolve()
await Promise.resolve()
assert.equal(requestFinalQuit.mock.calls.length, 1)
})
test('only a teardown that brings a backend back may announce it', () => {
// `soft` is what keeps a teardown out of resetBootProgressForReconnect(): the
// "[boot] Restarting desktop connection" line in desktop.log and the
// hermes:boot-progress push to the renderer. A shutdown must stay silent,
// a teardown that re-homes may announce itself.
assert.equal(backendTeardownOptions('quit').soft, true, 'a quit must not announce a reconnect')
assert.equal(backendTeardownOptions('reconnect').soft, false, 'a re-home keeps the announcement')
})
test('a synchronous reentrant quit cannot start a second teardown', async () => {
const finalQuit = vi.fn()
const coordinator = createQuitTeardownCoordinator(finalQuit)
const duplicate = vi.fn()
assert.equal(
coordinator.begin([
{
waitForCompletion: true,
run: () => {
assert.equal(coordinator.begin([{ run: duplicate, waitForCompletion: false }]), true)
}
}
]),
true
)
await Promise.resolve()
await Promise.resolve()
assert.equal(duplicate.mock.calls.length, 0)
assert.equal(finalQuit.mock.calls.length, 1)
})