feat(companion): add heartbeat failure-threshold controls

This commit is contained in:
William Valentin
2026-02-16 18:42:13 -08:00
parent cd8bfe26dd
commit ec2811eaef
4 changed files with 59 additions and 1 deletions
+30
View File
@@ -100,4 +100,34 @@ describe('CompanionHeartbeatLoop', () => {
expect(publishHeartbeat).toHaveBeenCalledTimes(1);
expect(loop.running).toBe(false);
});
it('stops when maxConsecutiveFailures threshold is reached', async () => {
const publishHeartbeat = vi
.fn<() => Promise<NodeStatusSetResult>>()
.mockRejectedValue(new Error('persistent-failure'));
const onError = vi.fn();
const onFailureLimitReached = vi.fn();
const loop = new CompanionHeartbeatLoop(
{ publishHeartbeat },
{
intervalMs: 300,
onError,
maxConsecutiveFailures: 2,
onFailureLimitReached,
},
);
loop.start();
await Promise.resolve();
expect(loop.running).toBe(true);
await vi.advanceTimersByTimeAsync(300);
expect(loop.running).toBe(false);
expect(onError).toHaveBeenCalledTimes(2);
expect(onFailureLimitReached).toHaveBeenCalledTimes(1);
expect(onFailureLimitReached).toHaveBeenCalledWith(expect.any(Error), 2);
await vi.advanceTimersByTimeAsync(1000);
expect(publishHeartbeat).toHaveBeenCalledTimes(2);
});
});