import { describe, expect, it, vi } from 'vitest' import type { JobStore } from '@devrunbook/application' import { runWorkerLoop } from './worker-loop' describe('worker loop', () => { it('stops cleanly after an idle poll is aborted', async () => { const controller = new AbortController() const store = { claim: vi.fn(async () => { controller.abort() return null }), } as unknown as JobStore await runWorkerLoop({ store, handlers: {}, workerId: 'worker-test', nextLeaseId: () => 'lease-test', leaseDurationMs: 30_000, pollIntervalMs: 2_000, signal: controller.signal, }) expect(store.claim).toHaveBeenCalledOnce() }) it('runs periodic planning before polling and contains planner failures', async () => { const controller = new AbortController() const order: string[] = [] const store = { claim: vi.fn(async () => { order.push('poll') controller.abort() return null }), } as unknown as JobStore await runWorkerLoop({ store, handlers: {}, workerId: 'worker-test', nextLeaseId: () => 'lease-test', leaseDurationMs: 30_000, pollIntervalMs: 2_000, signal: controller.signal, schedule: async () => { order.push('schedule') throw new Error('temporary planner failure') }, onScheduleError: vi.fn(), }) expect(order).toEqual(['schedule', 'poll']) }) })