Files
pulse/tests/integration/workflows.int.js
T
jaredandClaude Opus 5.5 a34867af92
CI / UI screenshots (before/after) (push) Skipped
CI / Lint (eslint) (push) Successful in 17s
CI / Test (jest) (push) Successful in 18s
CI / Security (npm audit) (push) Successful in 9s
CI / Scan (secrets, workflows, shell) (push) Successful in 2m2s
CI / Integration (api) (push) Successful in 2m31s
CI / Lint (eslint) (pull_request) Successful in 16s
CI / Test (jest) (pull_request) Successful in 15s
CI / Security (npm audit) (pull_request) Successful in 7s
CI / UI (playwright) (push) Successful in 1m39s
CI / Deploy (push) Skipped
CI / Deploy worker (push) Skipped
CI / Notify (push) Skipped
CI / Integration (api) (pull_request) Successful in 1m51s
CI / Scan (secrets, workflows, shell) (pull_request) Successful in 1m24s
CI / UI screenshots (before/after) (pull_request) Successful in 6s
CI / UI (playwright) (pull_request) Successful in 2m21s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped
Integration tests for the workflow engine and the scheduler
- workflows.int.js (11): all-worker execute, failure stops the workflow,
  parse to route, false conditions skip, prompts (invalid option refused,
  routes on the answer), params (required, substituted, unsafe refused),
  dry run, endless goto detection, abort stays failed.
- scheduler.int.js (5): a due schedule runs on its worker and advances
  next_run; disabled schedules don't run; admin-only and input checks.
- Known bugs as test.failing (each verified to fail on its assertion):
  abort doesn't stop the engine (#8), goto to a missing step completes
  (#20), an offline worker's schedule leaves no trace (#9), and an invalid
  cron expression returns 500 instead of 400.
- PULSE_SCHEDULER_TICK_MS (default 60 s, minimum 1 s) so the test stack
  ticks the scheduler every second.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 21:50:58 -05:00

158 lines
7.1 KiB
JavaScript

'use strict';
// The workflow engine (executeWorkflowSteps in server.js), driven through the API
// with real workers. Known bugs are written as test.failing: they document today's
// behaviour and start failing (so the marker must be removed) once the bug is fixed.
const { sso, waitFor, waitForExecution, sleep } = require('./helpers');
const unique = () => `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`;
async function createWorkflow(steps, extra = {}) {
const r = await sso('POST', '/api/workflows', { name: `int ${unique()}`, definition: { steps, ...extra } });
expect(r.status).toBe(200);
return r.json.id;
}
async function start(workflowId, body = {}) {
const r = await sso('POST', '/api/executions', { workflow_id: workflowId, ...body });
expect(r.status).toBe(200);
return r.json.id;
}
const logs = (ex, action) => (ex.logs || []).filter(l => l.action === action);
const outputs = ex => logs(ex, 'command_result').map(l => (l.stdout || '').trim());
test('an execute step on all workers runs everywhere and completes', async () => {
const marker = `all-${unique()}`;
const id = await start(await createWorkflow([
{ id: 'hello', name: 'Hello', type: 'execute', command: `echo ${marker}`, targets: ['all'] },
]));
const ex = await waitForExecution(id);
expect(ex.status).toBe('completed');
expect(outputs(ex).filter(o => o === marker)).toHaveLength(3);
});
test('a failing step fails the execution and stops the workflow', async () => {
const id = await start(await createWorkflow([
{ id: 'bad', type: 'execute', command: 'exit 3', targets: ['node-01'] },
{ id: 'never', type: 'execute', command: 'echo should-not-run', targets: ['node-01'] },
]));
const ex = await waitForExecution(id);
expect(ex.status).toBe('failed');
expect(outputs(ex)).not.toContain('should-not-run');
});
test('parse reads KEY=VALUE output and route jumps on it', async () => {
const id = await start(await createWorkflow([
{ id: 'probe', type: 'execute', command: 'echo STATUS=degraded', targets: ['node-01'] },
{ id: 'parse', type: 'parse' },
{ id: 'decide', type: 'route', conditions: [
{ if: "state.status === 'degraded'", goto: 'fix', label: 'needs fixing' },
{ default: true, goto: 'end' },
] },
{ id: 'skipped', type: 'execute', command: 'echo not-me', targets: ['node-01'] },
{ id: 'fix', type: 'execute', command: 'echo fixed', targets: ['node-01'] },
]));
const ex = await waitForExecution(id);
expect(ex.status).toBe('completed');
expect(logs(ex, 'parse_complete')[0].parsed).toEqual({ status: 'degraded' });
expect(logs(ex, 'route_taken')[0]).toMatchObject({ goto: 'fix', label: 'needs fixing' });
expect(outputs(ex)).toContain('fixed');
expect(outputs(ex)).not.toContain('not-me');
});
test('a false condition skips the step', async () => {
const id = await start(await createWorkflow([
{ id: 'prod-only', type: 'execute', command: 'echo prod', targets: ['node-01'], condition: "params.env === 'prod'" },
{ id: 'always', type: 'execute', command: 'echo always', targets: ['node-01'] },
], { params: [{ name: 'env' }] }), { params: { env: 'staging' } });
const ex = await waitForExecution(id);
expect(ex.status).toBe('completed');
expect(logs(ex, 'step_skipped')).toHaveLength(1);
expect(outputs(ex)).toEqual(['always']);
});
test('a prompt waits for an answer, rejects invalid options, and routes on the answer', async () => {
const id = await start(await createWorkflow([
{ id: 'ask', type: 'prompt', message: 'Restart?', options: ['Yes', 'No'], key: 'answer', routes: { No: 'end' } },
{ id: 'restart', type: 'execute', command: 'echo restarted', targets: ['node-02'] },
]));
await waitFor(async () => logs((await sso('GET', `/api/executions/${id}`)).json || {}, 'prompt').length > 0,
{ what: 'the prompt to be asked' });
expect((await sso('POST', `/api/executions/${id}/respond`, { response: 'Maybe' })).status).toBe(400);
expect((await sso('POST', `/api/executions/${id}/respond`, { response: 'Yes' })).status).toBe(200);
const ex = await waitForExecution(id);
expect(ex.status).toBe('completed');
expect(logs(ex, 'prompt_response')[0]).toMatchObject({ response: 'Yes', responded_by: 'int.tester' });
expect(outputs(ex)).toEqual(['restarted']);
});
test('parameters are substituted, and unsafe values are refused', async () => {
const wf = await createWorkflow([
{ id: 'greet', type: 'execute', command: 'echo host={{host}}', targets: ['node-03'] },
], { params: [{ name: 'host', required: true }] });
expect((await sso('POST', '/api/executions', { workflow_id: wf })).status).toBe(400); // required
const ok = await waitForExecution(await start(wf, { params: { host: 'db-01.lan' } }));
expect(ok.status).toBe('completed');
expect(outputs(ok)).toEqual(['host=db-01.lan']);
const bad = await waitForExecution(await start(wf, { params: { host: 'x; reboot' } }));
expect(bad.status).toBe('failed');
expect(outputs(bad)).toEqual([]);
});
test('a dry run logs the commands without running them', async () => {
const id = await start(await createWorkflow([
{ id: 'danger', type: 'execute', command: 'echo would-run', targets: ['all'] },
]), { dry_run: true });
const ex = await waitForExecution(id);
expect(ex.status).toBe('completed');
expect(logs(ex, 'dry_run_skipped')).toHaveLength(1);
expect(logs(ex, 'command_result')).toHaveLength(0);
});
test('an endless goto loop is detected and fails the execution', async () => {
const id = await start(await createWorkflow([
{ id: 'loop', type: 'route', conditions: [{ default: true, goto: 'loop' }] },
]));
const ex = await waitForExecution(id, { timeout: 60_000 });
expect(ex.status).toBe('failed');
expect(logs(ex, 'workflow_error')[0].error).toMatch(/Infinite loop/);
});
test('an aborted execution stays failed', async () => {
const id = await start(await createWorkflow([
{ id: 'pause', type: 'wait', duration: 2 },
{ id: 'after', type: 'execute', command: 'echo after-abort', targets: ['node-01'] },
]));
await sleep(500);
expect((await sso('POST', `/api/executions/${id}/abort`)).status).toBe(200);
await sleep(4000);
const ex = (await sso('GET', `/api/executions/${id}`)).json;
expect(ex.status).toBe('failed');
});
// Known bug #8: abort marks the execution failed but the engine keeps going.
test.failing('abort stops the workflow before its next step (#8)', async () => {
const id = await start(await createWorkflow([
{ id: 'pause', type: 'wait', duration: 2 },
{ id: 'after', type: 'execute', command: 'echo after-abort', targets: ['node-01'] },
]));
await sleep(500);
await sso('POST', `/api/executions/${id}/abort`);
await sleep(5000);
const ex = (await sso('GET', `/api/executions/${id}`)).json;
expect(logs(ex, 'step_started').map(l => l.step)).toEqual([1]);
});
// Known bug #20: a goto to a step that doesn't exist ends as 'completed'.
test.failing('a goto to a missing step fails the execution (#20)', async () => {
const id = await start(await createWorkflow([
{ id: 'jump', type: 'route', conditions: [{ default: true, goto: 'no-such-step' }] },
]));
const ex = await waitForExecution(id);
expect(ex.status).toBe('failed');
});