Expire undelivered health checks when their wait times out

This commit is contained in:
2026-10-06 12:00:25 -04:00
parent 85e167a606
commit 9bcdd8cc22
2 changed files with 51 additions and 1 deletions
+32
View File
@@ -15,6 +15,38 @@ async function enrolled() {
}
describe('native host enrollment and scoped authentication', () => {
it.each(['instance.inspect','rcon.status-batch'])('expires undelivered %s on timeout and delivers the next fresh check', async kind => {
const api = await enrolled();
const { db } = await import('../../src/lib/server/db/index.js');
api.heartbeat(api.host.id,{...inventory,capabilities:['operations.health-lane']});
const stale = api.queueHostOperation(api.host.id,kind,{});
await expect(api.waitHostOperationStatus(stale.id,0)).rejects.toThrow('expired before delivery');
expect(api.getHostOperationStatus(stale.id)).toMatchObject({state:'failed',error:'Expired before delivery.'});
expect(await api.waitHostOperationStatus(stale.id,0)).toMatchObject({state:'failed'});
expect(db().prepare("SELECT state,error FROM ec_host_operation_events WHERE operation_id=? AND state='failed'").all(stale.id))
.toEqual([{state:'failed',error:'Expired before delivery.'}]);
const fresh = api.queueHostOperation(api.host.id,kind,{});
expect(api.pendingHostOperations(api.host.id,'health').map(op=>op.id)).toEqual([fresh.id]);
});
it.each(['pending','accepted','running'])('preserves delivered health checks in %s state after the caller times out', async state => {
const api = await enrolled();
const operation = api.queueHostOperation(api.host.id,'instance.inspect',{});
api.pendingHostOperations(api.host.id,'health');
if (state !== 'pending') api.reportHostOperationStatus(api.host.id,operation.id,{state});
await expect(api.waitHostOperationStatus(operation.id,0)).rejects.toThrow('awaiting acknowledgement');
expect(api.getHostOperationStatus(operation.id)?.state).toBe(state);
api.reportHostOperationStatus(api.host.id,operation.id,{state:'completed',result:{healthy:true}});
expect(await api.waitHostOperationStatus(operation.id)).toMatchObject({state:'completed',result:{healthy:true}});
});
it.each(['instance.restart','files.stage','rcon.execute'])('preserves undelivered %s when its caller times out', async kind => {
const api = await enrolled();
const operation = api.queueHostOperation(api.host.id,kind,{command:'status'});
await expect(api.waitHostOperationStatus(operation.id,0)).rejects.toThrow('awaiting acknowledgement');
expect(api.getHostOperationStatus(operation.id)?.state).toBe('pending');
});
it('keeps the host online and delivers work when individual state observations are invalid', async () => {
const api = await enrolled();
const good = { instanceId: 'healthy-server', generation: 1, state: 'running', healthy: true, observedAt: new Date().toISOString() };