test(workflow): add scheduler and health-check script tests
This commit is contained in:
parent
0d61ac70fc
commit
9744a7ac23
|
|
@ -0,0 +1,40 @@
|
|||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { mkdtemp, writeFile } from 'fs/promises';
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import { checkWorkflowHealth } from '../check-workflow-health.ts';
|
||||
|
||||
describe('check-workflow-health', () => {
|
||||
let consoleSpy: any;
|
||||
|
||||
beforeEach(() => {
|
||||
consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
consoleSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('maps status values to expected health outcomes', async () => {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), 'workflow-health-test-'));
|
||||
|
||||
const cases: Array<{ status: string; expectedCode: number; expectedOk: boolean }> = [
|
||||
{ status: 'idle', expectedCode: 0, expectedOk: true },
|
||||
{ status: 'running', expectedCode: 0, expectedOk: true },
|
||||
{ status: 'completed', expectedCode: 0, expectedOk: true },
|
||||
{ status: 'failed', expectedCode: 1, expectedOk: false },
|
||||
{ status: 'blocked', expectedCode: 1, expectedOk: false },
|
||||
];
|
||||
|
||||
for (const c of cases) {
|
||||
const statusPath = path.join(dir, `${c.status}.json`);
|
||||
await writeFile(statusPath, JSON.stringify({ overallStatus: c.status }), 'utf8');
|
||||
|
||||
const code = await checkWorkflowHealth(statusPath);
|
||||
expect(code).toBe(c.expectedCode);
|
||||
|
||||
const lastOutput = consoleSpy.mock.calls.at(-1)?.[0] as string;
|
||||
expect(JSON.parse(lastOutput)).toMatchObject({ ok: c.expectedOk, status: c.status });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
const { runWorkflowMock, generateMorningReportMock } = vi.hoisted(() => ({
|
||||
runWorkflowMock: vi.fn(),
|
||||
generateMorningReportMock: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('../run-workflow.ts', () => ({
|
||||
runWorkflow: runWorkflowMock,
|
||||
}));
|
||||
|
||||
vi.mock('../morning-report.ts', () => ({
|
||||
generateMorningReport: generateMorningReportMock,
|
||||
}));
|
||||
|
||||
import { runScheduledWorkflow } from '../schedule-workflow.ts';
|
||||
|
||||
describe('schedule-workflow', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('runs workflow in resume mode and then generates morning report', async () => {
|
||||
runWorkflowMock.mockResolvedValue(undefined);
|
||||
generateMorningReportMock.mockResolvedValue(undefined);
|
||||
|
||||
await runScheduledWorkflow();
|
||||
|
||||
expect(runWorkflowMock).toHaveBeenCalledWith({ mode: 'resume' });
|
||||
expect(generateMorningReportMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('propagates workflow failures and does not run report generation', async () => {
|
||||
const boom = new Error('workflow exploded');
|
||||
runWorkflowMock.mockRejectedValue(boom);
|
||||
|
||||
await expect(runScheduledWorkflow()).rejects.toThrow('workflow exploded');
|
||||
expect(generateMorningReportMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue