|
| 1 | +import {test, expect} from '@playwright/test'; |
| 2 | +import {isHungLeaseHolder} from '../../../../../../../ai/daemons/orchestrator/services/leaseWatchdog.mjs'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Coverage for the pure hung-heavy-lease watchdog decision (sub of the orchestrator heavy-maintenance |
| 6 | + * epic). Catches the alive + within-TTL but sustained-idle holder the pid/TTL stale-check misses. The |
| 7 | + * sustained-run semantics + the fail-SAFE-on-bad-data contract are pinned here. |
| 8 | + */ |
| 9 | +test.describe('ai/daemons/orchestrator/services/leaseWatchdog — isHungLeaseHolder', () => { |
| 10 | + test('a sustained trailing-idle run → hung (true)', () => { |
| 11 | + expect(isHungLeaseHolder({cpuPercentSamples: [42, 0, 0.2, 0.1]})).toBe(true); |
| 12 | + }); |
| 13 | + |
| 14 | + test('an active sample anywhere in the trailing window → not hung (false)', () => { |
| 15 | + expect(isHungLeaseHolder({cpuPercentSamples: [0, 0, 35]})).toBe(false); // newest active |
| 16 | + expect(isHungLeaseHolder({cpuPercentSamples: [0, 35, 0]})).toBe(false); // active mid-window |
| 17 | + }); |
| 18 | + |
| 19 | + test('too few samples → false (cannot conclude a sustained hang)', () => { |
| 20 | + expect(isHungLeaseHolder({cpuPercentSamples: [0, 0]})).toBe(false); // < minConsecutiveIdle (3) |
| 21 | + }); |
| 22 | + |
| 23 | + test('boundary: a sample exactly at the threshold counts as idle', () => { |
| 24 | + expect(isHungLeaseHolder({cpuPercentSamples: [1, 1, 1], idleThresholdPct: 1})).toBe(true); |
| 25 | + expect(isHungLeaseHolder({cpuPercentSamples: [1.1, 1, 1], idleThresholdPct: 1})).toBe(false); // 1.1 > 1 |
| 26 | + }); |
| 27 | + |
| 28 | + test('respects a custom minConsecutiveIdle window', () => { |
| 29 | + expect(isHungLeaseHolder({cpuPercentSamples: [0, 0], minConsecutiveIdle: 2})).toBe(true); |
| 30 | + expect(isHungLeaseHolder({cpuPercentSamples: [5, 0], minConsecutiveIdle: 2})).toBe(false); |
| 31 | + }); |
| 32 | + |
| 33 | + test('fail-SAFE on malformed / insufficient input (never force-release on bad data)', () => { |
| 34 | + expect(isHungLeaseHolder()).toBe(false); // no args |
| 35 | + expect(isHungLeaseHolder({cpuPercentSamples: 'nope'})).toBe(false); // non-array |
| 36 | + expect(isHungLeaseHolder({cpuPercentSamples: []})).toBe(false); // empty |
| 37 | + expect(isHungLeaseHolder({cpuPercentSamples: [0, 0, NaN]})).toBe(false); // non-finite → not idle |
| 38 | + expect(isHungLeaseHolder({cpuPercentSamples: [0, 0, 0], minConsecutiveIdle: 0})).toBe(false); // bad window |
| 39 | + }); |
| 40 | +}); |
0 commit comments