hermes-agent/apps/desktop/electron/backend-connection-state.test.ts
Brooklyn Nicholson ee8275a8b2 test(desktop): port backend-connection-state test to vitest
Rebased onto current main, where the electron test harness migrated from
node:test to vitest (test:desktop:platforms = `vitest run --project electron`,
auto-discovering electron/*.test.ts). Swap the node:test import for vitest and
drop the .ts-extension import hack; the obsolete package.json node --test list
edit is dropped in the cherry-pick resolution since vitest auto-discovers.

Co-authored-by: Gille <4317663+helix4u@users.noreply.github.com>
2026-07-16 14:55:58 -04:00

70 lines
2.3 KiB
TypeScript

import assert from 'node:assert/strict'
import { test } from 'vitest'
import { createBackendConnectionState } from './backend-connection-state'
type FakeProcess = { id: string }
test('a stale backend exit cannot clear a newer connection attempt', () => {
const state = createBackendConnectionState<FakeProcess, string>()
const oldAttempt = state.startAttempt()
const oldPromise = Promise.resolve('old')
state.setPromise(oldAttempt, oldPromise)
const oldOwner = state.attachProcess(oldAttempt, { id: 'old' })
assert.ok(oldOwner)
state.invalidate()
const newAttempt = state.startAttempt()
const newPromise = Promise.resolve('new')
const newProcess = { id: 'new' }
state.setPromise(newAttempt, newPromise)
assert.ok(state.attachProcess(newAttempt, newProcess))
assert.equal(state.clearForCurrentProcess(oldOwner), false)
assert.equal(state.getProcess(), newProcess)
assert.equal(state.getPromise(), newPromise)
})
test('the current backend exit clears its process and connection promise', () => {
const state = createBackendConnectionState<FakeProcess, string>()
const attempt = state.startAttempt()
state.setPromise(attempt, Promise.resolve('current'))
const owner = state.attachProcess(attempt, { id: 'current' })
assert.ok(owner)
assert.equal(state.clearForCurrentProcess(owner), true)
assert.equal(state.clearPromiseForAttempt(attempt), true)
assert.equal(state.getProcess(), null)
assert.equal(state.getPromise(), null)
})
test('a stale rejected attempt cannot clear a newer connection promise', () => {
const state = createBackendConnectionState<FakeProcess, string>()
const oldAttempt = state.startAttempt()
state.setPromise(oldAttempt, Promise.resolve('old'))
state.invalidate()
const newAttempt = state.startAttempt()
const newPromise = Promise.resolve('new')
state.setPromise(newAttempt, newPromise)
assert.equal(state.clearPromiseForAttempt(oldAttempt), false)
assert.equal(state.getPromise(), newPromise)
})
test('an invalidated attempt cannot attach a late-spawned process', () => {
const state = createBackendConnectionState<FakeProcess, string>()
const staleAttempt = state.startAttempt()
state.invalidate()
assert.equal(state.attachProcess(staleAttempt, { id: 'late' }), null)
assert.equal(state.getProcess(), null)
})