From bfcba8fd45a383d9faf0fcd39b373d1089028700 Mon Sep 17 00:00:00 2001 From: HomeLab Agent Date: Sat, 14 Nov 2026 12:31:22 -0500 Subject: [PATCH 1/3] test(core): poll instead of sleeping in three scheduler timing tests Three scheduler tests waited a fixed wait(300)/wait(100)/wait(50) and then asserted the scheduler had already checked, triggered, run and persisted state.yaml. On a loaded CI runner the guess is sometimes short: all three failed on the #492 version PR (both attempts) and on e852a99, and scheduler.test.ts failures recur in older CI runs too, while the behaviour under test was fine. Replace those sleeps with waitUntil(), which polls the same condition up to a 5s deadline. The deadline uses performance.now(): two of these tests freeze Date.now to pin the cron clock, so a Date.now deadline would never expire. Assertions are unchanged; the tests only wait as long as they need to. Co-Authored-By: Claude Opus 5.5 --- .../src/scheduler/__tests__/scheduler.test.ts | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/packages/core/src/scheduler/__tests__/scheduler.test.ts b/packages/core/src/scheduler/__tests__/scheduler.test.ts index 0f46687b..ef1010b7 100644 --- a/packages/core/src/scheduler/__tests__/scheduler.test.ts +++ b/packages/core/src/scheduler/__tests__/scheduler.test.ts @@ -62,6 +62,32 @@ function wait(ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)); } +/** + * Poll until `predicate` holds, failing only after a generous deadline. + * + * Prefer this to a fixed `wait(n)` wherever a test is waiting for the scheduler + * to get somewhere — check, trigger, run, write `state.yaml`. A fixed sleep + * encodes a guess about how fast a CI runner is, and on a loaded one the guess + * is wrong: these tests failed on CI with `wait(300)` / `wait(50)` while the + * behaviour under test was fine. Polling waits exactly as long as needed. + * + * The deadline is measured with `performance.now()`, NOT `Date.now()`, because + * several tests here freeze `Date.now` to pin the cron clock — a `Date.now` + * deadline would never expire. + */ +async function waitUntil( + predicate: () => boolean | Promise, + { timeoutMs = 5000, intervalMs = 10 }: { timeoutMs?: number; intervalMs?: number } = {}, +): Promise { + const deadline = performance.now() + timeoutMs; + while (!(await predicate())) { + if (performance.now() > deadline) { + throw new Error(`waitUntil: condition not met within ${timeoutMs}ms`); + } + await wait(intervalMs); + } +} + describe("Scheduler", () => { let tempDir: string; let mockLogger: ReturnType; @@ -498,11 +524,15 @@ describe("Scheduler", () => { const startPromise = scheduler.start(agents); - // Wait for the scheduler to check, trigger, and complete - await wait(300); + // Wait for the scheduler to check, trigger, complete, and persist the + // completed run — i.e. until state.yaml carries a next_run_at. + const stateFile = join(tempDir, "state.yaml"); + await waitUntil(async () => { + const s = (await readFleetState(stateFile)).agents["test-agent"]?.schedules?.everyMinute; + return !!s?.next_run_at && s.status === "idle"; + }); // Check state was updated with next_run_at for cron - const stateFile = join(tempDir, "state.yaml"); const fleetState = await readFleetState(stateFile); const scheduleState = fleetState.agents["test-agent"]?.schedules?.everyMinute; @@ -632,7 +662,7 @@ describe("Scheduler", () => { ]; const startPromise = scheduler.start(agents); - await wait(300); + await waitUntil(() => triggers.length > 0); // The cron should trigger because the next occurrence after 10:00:00 // is 10:01:00, which is exactly now. @@ -1545,13 +1575,13 @@ describe("Scheduler", () => { }); const startPromise = scheduler.start([agent]); - await wait(100); + await waitUntil(() => scheduler.getTotalRunningJobCount() > 0); - // Should have 1 running job + // Should have 1 running job (the job is held open by jobPromise) expect(scheduler.getTotalRunningJobCount()).toBe(1); resolveJob!(); - await wait(50); + await waitUntil(() => scheduler.getTotalRunningJobCount() === 0); // Should be back to 0 expect(scheduler.getTotalRunningJobCount()).toBe(0); From 6d42b6c27c235149ffe363f777192205d51265b7 Mon Sep 17 00:00:00 2001 From: HomeLab Agent Date: Sat, 14 Nov 2026 12:41:28 -0500 Subject: [PATCH 2/3] test(core): poll in every scheduler test that waits for something to happen The first pass fixed the three tests seen failing on #492, and CI promptly failed a fourth: "triggers cron schedules when time is within trigger window", whose wait(500) had already been raised once "for slow CI runners". The flake is the pattern, not those three tests: any fixed sleep followed by an assertion that the scheduler got somewhere can lose on a busy runner. Every positive-condition sleep in scheduler.test.ts now polls (21 more). Sleeps that prove an ABSENCE (no second trigger, nothing after stop, max concurrency never exceeded) stay fixed, now after a waitUntil for the event they measure from. waitUntil's deadline drops to 4s, under vitest's 5s test timeout, so a real failure reports the helper's message. Mutation-checked: disabling the cron trigger window fails both window tests in 4s with "waitUntil: condition not met"; they neither hang nor pass. Co-Authored-By: Claude Opus 5.5 --- .../src/scheduler/__tests__/scheduler.test.ts | 91 ++++++++++++------- 1 file changed, 58 insertions(+), 33 deletions(-) diff --git a/packages/core/src/scheduler/__tests__/scheduler.test.ts b/packages/core/src/scheduler/__tests__/scheduler.test.ts index ef1010b7..16642592 100644 --- a/packages/core/src/scheduler/__tests__/scheduler.test.ts +++ b/packages/core/src/scheduler/__tests__/scheduler.test.ts @@ -73,11 +73,16 @@ function wait(ms: number): Promise { * * The deadline is measured with `performance.now()`, NOT `Date.now()`, because * several tests here freeze `Date.now` to pin the cron clock — a `Date.now` - * deadline would never expire. + * deadline would never expire. It defaults below vitest's 5s test timeout so a + * genuine failure reports this helper's message, not a bare "Test timed out". + * + * Only for waiting on something to HAPPEN. Assertions that something did NOT + * happen ("triggers only once", "no trigger after stop") still need a fixed + * wait — you cannot poll for an absence. */ async function waitUntil( predicate: () => boolean | Promise, - { timeoutMs = 5000, intervalMs = 10 }: { timeoutMs?: number; intervalMs?: number } = {}, + { timeoutMs = 4000, intervalMs = 10 }: { timeoutMs?: number; intervalMs?: number } = {}, ): Promise { const deadline = performance.now() + timeoutMs; while (!(await predicate())) { @@ -241,8 +246,8 @@ describe("Scheduler", () => { const startPromise = scheduler.start([]); - // Wait for a few checks - await wait(180); + // Wait for at least one check + await waitUntil(() => scheduler.getState().checkCount > 0); const state = scheduler.getState(); expect(state.checkCount).toBeGreaterThan(0); @@ -369,7 +374,7 @@ describe("Scheduler", () => { const agents = [createTestAgent("no-schedules-agent")]; const startPromise = scheduler.start(agents); - await wait(100); + await waitUntil(() => scheduler.getState().checkCount > 0); // Should complete check without errors const state = scheduler.getState(); @@ -436,9 +441,9 @@ describe("Scheduler", () => { const startPromise = scheduler.start(agents); - // Wait for the scheduler to check (using real setTimeout) - // 500ms gives ample headroom for slow CI runners doing async file I/O - await wait(500); + // Wait for the scheduler to check and trigger. A fixed wait(500) here + // still flaked on CI, so poll rather than guess at runner speed. + await waitUntil(() => triggers.length > 0); // Should have triggered since we're 5s past a minute boundary (within 30s trigger window) expect(triggers.length).toBeGreaterThan(0); @@ -466,7 +471,7 @@ describe("Scheduler", () => { ]; const startPromise = scheduler.start(agents); - await wait(100); + await waitUntil(() => mockLogger.warnings.some((m) => m.includes("missing cron expression"))); expect(mockLogger.warnings.some((m) => m.includes("missing cron expression"))).toBe(true); expect(scheduler.getState().triggerCount).toBe(0); @@ -489,7 +494,7 @@ describe("Scheduler", () => { ]; const startPromise = scheduler.start(agents); - await wait(100); + await waitUntil(() => mockLogger.warnings.some((m) => m.includes("invalid cron expression"))); expect(mockLogger.warnings.some((m) => m.includes("invalid cron expression"))).toBe(true); expect(scheduler.getState().triggerCount).toBe(0); @@ -597,6 +602,9 @@ describe("Scheduler", () => { ]; const startPromise = scheduler.start(agents); + // Wait for the catch-up trigger, then give the scheduler several more + // check cycles in which a (wrong) second trigger could fire. + await waitUntil(() => triggers.length > 0); await wait(200); // The schedule SHOULD trigger once to catch up for the missed window. @@ -762,7 +770,7 @@ describe("Scheduler", () => { ]; const startPromise = scheduler.start(agents); - await wait(100); + await waitUntil(() => mockLogger.debugs.some((m) => m.includes("disabled"))); expect(mockLogger.debugs.some((m) => m.includes("disabled"))).toBe(true); expect(scheduler.getState().triggerCount).toBe(0); @@ -785,7 +793,7 @@ describe("Scheduler", () => { ]; const startPromise = scheduler.start(agents); - await wait(100); + await waitUntil(() => mockLogger.warnings.some((m) => m.includes("missing interval value"))); expect(mockLogger.warnings.some((m) => m.includes("missing interval value"))).toBe(true); expect(scheduler.getState().triggerCount).toBe(0); @@ -813,7 +821,7 @@ describe("Scheduler", () => { ]; const startPromise = scheduler.start(agents); - await wait(150); + await waitUntil(() => triggers.length > 0); // First run should trigger immediately (no last_run_at) expect(triggers.length).toBeGreaterThan(0); @@ -842,10 +850,15 @@ describe("Scheduler", () => { ]; const startPromise = scheduler.start(agents); - await wait(150); - // Check state was updated + // Wait until the completed run is persisted const stateFile = join(tempDir, "state.yaml"); + await waitUntil(async () => { + const s = (await readFleetState(stateFile)).agents["test-agent"]?.schedules?.hourly; + return !!s?.last_run_at && s.status === "idle"; + }); + + // Check state was updated const fleetState = await readFleetState(stateFile); const scheduleState = fleetState.agents["test-agent"]?.schedules?.hourly; @@ -874,10 +887,16 @@ describe("Scheduler", () => { ]; const startPromise = scheduler.start(agents); - await wait(150); - // Check error was recorded + // Wait until the failure is persisted const stateFile = join(tempDir, "state.yaml"); + await waitUntil( + async () => + (await readFleetState(stateFile)).agents["test-agent"]?.schedules?.hourly?.last_error === + "Trigger failed!", + ); + + // Check error was recorded const fleetState = await readFleetState(stateFile); const scheduleState = fleetState.agents["test-agent"]?.schedules?.hourly; @@ -916,8 +935,10 @@ describe("Scheduler", () => { const startPromise = scheduler.start(agents); - // Wait long enough for multiple checks but only one trigger should complete - await wait(150); + // Wait for the first trigger, then for several 30ms check cycles while the + // 100ms job is still running — none of them may trigger it again. + await waitUntil(() => triggerCount > 0); + await wait(100); // Should only have triggered once due to running check expect(triggerCount).toBe(1); @@ -944,7 +965,7 @@ describe("Scheduler", () => { ]; const startPromise = scheduler.start(agents); - await wait(150); + await waitUntil(() => scheduler.getState().checkCount > 0); // Should have logged errors but kept running expect(scheduler.isRunning()).toBe(true); @@ -998,7 +1019,8 @@ describe("Scheduler", () => { const startPromise = scheduler.start(agents); - // Wait for both to have started running + // Wait for at least one trigger, then (briefly) for both to have started + await waitUntil(() => maxConcurrent >= 1); await Promise.race([bothStarted, wait(300)]); // Check max concurrent - may be 1 or 2 depending on timing @@ -1130,7 +1152,7 @@ describe("Scheduler", () => { const startPromise = scheduler.start([agent]); // Wait for first trigger to happen and start running - await wait(100); + await waitUntil(() => triggerCount > 0); // With 1 schedule, it should trigger once initially // Then subsequent checks should show "already_running" until it completes @@ -1138,7 +1160,7 @@ describe("Scheduler", () => { expect(scheduler.getRunningJobCount("test-agent")).toBe(1); // Wait for the job to complete - await wait(200); + await waitUntil(() => scheduler.getRunningJobCount("test-agent") === 0); // After completion, running count should be 0 expect(scheduler.getRunningJobCount("test-agent")).toBe(0); @@ -1178,7 +1200,7 @@ describe("Scheduler", () => { const startPromise = scheduler.start([agent]); - await wait(100); + await waitUntil(() => runningCountDuringExecution !== -1); // Count should have been 1 during execution expect(runningCountDuringExecution).toBe(1); @@ -1239,7 +1261,7 @@ describe("Scheduler", () => { const startPromise = scheduler.start([agent]); - await wait(150); + await waitUntil(() => maxCount >= 1); // Should have tracked multiple concurrent jobs expect(maxCount).toBeGreaterThanOrEqual(1); @@ -1304,7 +1326,7 @@ describe("Scheduler", () => { }); const startPromise = scheduler.start([agent]); - await wait(100); + await waitUntil(() => jobStarted); // Job should be running expect(jobStarted).toBe(true); @@ -1351,7 +1373,7 @@ describe("Scheduler", () => { }); const startPromise = scheduler.start([agent]); - await wait(100); + await waitUntil(() => jobStarted); // Job should be running expect(jobStarted).toBe(true); @@ -1388,7 +1410,7 @@ describe("Scheduler", () => { }); const startPromise = scheduler.start([agent]); - await wait(100); + await waitUntil(() => scheduler.getTotalRunningJobCount() === 1); // Stop with a short timeout let shutdownError: SchedulerShutdownError | null = null; @@ -1429,7 +1451,7 @@ describe("Scheduler", () => { }); const startPromise = scheduler.start([agent]); - await wait(50); + await waitUntil(() => triggerCount > 0); // First trigger should start const countAtStop = triggerCount; @@ -1477,7 +1499,7 @@ describe("Scheduler", () => { } as ResolvedAgent; const startPromise = scheduler.start([agent]); - await wait(100); + await waitUntil(() => runningCount >= 1); // Multiple jobs should be running expect(runningCount).toBeGreaterThanOrEqual(1); @@ -1534,10 +1556,13 @@ describe("Scheduler", () => { }); const startPromise = scheduler.start([agent]); - await wait(150); // Trigger should have completed and updated state const stateFile = join(tempDir, "state.yaml"); + await waitUntil(async () => { + const s = (await readFleetState(stateFile)).agents["test-agent"]?.schedules?.hourly; + return !!s?.last_run_at && s.status === "idle"; + }); let fleetState = await readFleetState(stateFile); const scheduleState = fleetState.agents["test-agent"]?.schedules?.hourly; @@ -1602,7 +1627,7 @@ describe("Scheduler", () => { }); const startPromise = scheduler.start([]); - await wait(70); + await waitUntil(() => onTick.mock.calls.length >= 2); await scheduler.stop(); await startPromise; @@ -1623,7 +1648,7 @@ describe("Scheduler", () => { }); const startPromise = scheduler.start([]); - await wait(70); + await waitUntil(() => calls >= 2); await scheduler.stop(); await startPromise; From 914dc3966e1d3d71c272d93622a36564547910ff Mon Sep 17 00:00:00 2001 From: HomeLab Agent Date: Sat, 14 Nov 2026 12:44:36 -0500 Subject: [PATCH 3/3] test(core): give the real-Docker container tests a 30s budget docker-security.test.ts "Container Configuration" shells out to the real Docker daemon (docker create / inspect / rm). On a cold or busy CI runner a single `docker create` can exceed vitest's 5s default: "sets memory limits" and "sets user to non-root" both timed out that way on #492, with the other 3772 tests green. Size the per-test timeout for real Docker on that block only; the pure-config tests elsewhere in the file keep the default. Co-Authored-By: Claude Opus 5.5 --- .../src/runner/runtime/__tests__/docker-security.test.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/core/src/runner/runtime/__tests__/docker-security.test.ts b/packages/core/src/runner/runtime/__tests__/docker-security.test.ts index 1ab0a36d..adb15fda 100644 --- a/packages/core/src/runner/runtime/__tests__/docker-security.test.ts +++ b/packages/core/src/runner/runtime/__tests__/docker-security.test.ts @@ -262,7 +262,11 @@ describeDocker("Docker Security Hardening", () => { }); }); - describe("Container Configuration", () => { + // These tests shell out to the real Docker daemon (`docker create` / `inspect` + // / `rm`). On a cold or busy CI runner one `docker create` alone can take + // longer than vitest's 5s default, and two tests here timed out that way on + // #492 with nothing wrong. Give each one a budget sized for real Docker. + describe("Container Configuration", { timeout: 30_000 }, () => { it("applies no-new-privileges security option", async () => { // Create a container to inspect (don't start it) const { stdout } = await execAsync(