test(cloud): deterministic stale re-pair test; typed shared-control reconnect test

This commit is contained in:
Neil
2026-11-09 17:43:54 -08:00
parent 7e19d48a0f
commit 54223fa8fa
2 changed files with 25 additions and 10 deletions
@@ -236,6 +236,7 @@ describe('resuming a recipe VM server', () => {
const firstGate = new Promise<void>((resolve) => (releaseFirst = resolve))
const auth = serverAuth(serverDataPath)
const repairedA = vi.fn()
const firstProbeSettled = Promise.withResolvers<void>()
// Resume A: its probe is held, so its re-pair would land late.
await applyResumedRuntimePairing({
userDataPath: desktopDataPath,
@@ -244,7 +245,11 @@ describe('resuming a recipe VM server', () => {
invalidateTransport: vi.fn(),
probeSavedPairing: async (connection) => {
await firstGate
return auth(connection)
try {
return await auth(connection)
} finally {
firstProbeSettled.resolve()
}
},
confirmInBackground: true,
onRepairedInBackground: repairedA
@@ -259,7 +264,11 @@ describe('resuming a recipe VM server', () => {
})
const afterB = resolveEnvironmentPairingOffer(desktopDataPath, environmentId)
releaseFirst()
await new Promise((resolve) => setTimeout(resolve, 20))
await firstProbeSettled.promise
// Let A's confirmation chain (catch → guarded repair → then) run to the end.
for (let tick = 0; tick < 10; tick++) {
await Promise.resolve()
}
expect(repairedA).not.toHaveBeenCalled()
expect(resolveEnvironmentPairingOffer(desktopDataPath, environmentId)).toEqual(afterB)
expect(outcomeB.environment.id).toBe(environmentId)
@@ -1,15 +1,18 @@
import { describe, expect, it, vi } from 'vitest'
import type { RemoteRuntimeSharedConnectionDiagnostics } from '../../../shared/remote-runtime/control/remote-runtime-shared-control-types'
import { reconnectSharedControlIfIdle } from './runtime-environment-shared-control-reconnect'
function connection(state: string | null) {
type State = RemoteRuntimeSharedConnectionDiagnostics['state']
function connection(state: State | null) {
return {
getDiagnostics: () => (state === null ? null : ({ state } as never)),
getDiagnostics: () => (state === null ? null : { state }),
reconnectNow: vi.fn()
}
}
describe('reconnectSharedControlIfIdle', () => {
it.each(['awaiting_ready', 'awaiting_authenticated', 'ready'])(
it.each<State>(['awaiting_ready', 'awaiting_authenticated', 'ready'])(
'leaves a socket that is %s alone',
(state) => {
const socket = connection(state)
@@ -18,11 +21,14 @@ describe('reconnectSharedControlIfIdle', () => {
}
)
it.each(['closed', 'reconnecting', null])('reconnects when the socket is %s', (state) => {
const socket = connection(state)
reconnectSharedControlIfIdle(socket)
expect(socket.reconnectNow).toHaveBeenCalledTimes(1)
})
it.each<State | null>(['closed', 'reconnecting', null])(
'reconnects when the socket is %s',
(state) => {
const socket = connection(state)
reconnectSharedControlIfIdle(socket)
expect(socket.reconnectNow).toHaveBeenCalledTimes(1)
}
)
it('does nothing without a connection', () => {
expect(() => reconnectSharedControlIfIdle(undefined)).not.toThrow()