Files
pulse/tests/integration/workflows.int.js
T
jaredandClaude Opus 5.5 dc732a1ec5
CI / UI screenshots (before/after) (push) Skipped
CI / Test (jest) (push) Successful in 32s
CI / Security (npm audit) (push) Successful in 15s
CI / Integration (api) (push) Successful in 4m4s
CI / Scan (secrets, workflows, shell) (push) Successful in 3m26s
CI / Lint (eslint) (pull_request) Successful in 1m12s
CI / Test (jest) (pull_request) Successful in 1m11s
CI / Scan (secrets, workflows, shell) (pull_request) Successful in 7m1s
CI / Integration (api) (pull_request) Successful in 7m41s
CI / UI screenshots (before/after) (pull_request) Successful in 28s
CI / UI (playwright) (pull_request) Successful in 4m41s
CI / Lint (eslint) (push) Successful in 24s
CI / Security (npm audit) (pull_request) Successful in 23s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped
CI / UI (playwright) (push) Successful in 3m41s
CI / Deploy (push) Skipped
CI / Deploy worker (push) Skipped
CI / Notify (push) Skipped
Fix abort, missing goto, offline schedules and the cron 500 (#8, #20, #9)
The four known-bug tests from #59 now pass as normal tests.

- #8 abort: each workflow execution gets an AbortController. The abort
  route fires it; the engine checks it before every step and stops; wait
  steps, retry delays and waits for a worker's result end as soon as it
  fires. New test: aborting during a 20 s command stops the workflow
  within seconds and runs no further step.
- #20 goto to a missing step: the execution now fails (it reported
  completed). Workflows are also checked on save: a goto, route or prompt
  route to a step id that doesn't exist is a 400 ('end' is always valid).
  A workflow saved before the check still fails at runtime (tested by
  writing one directly).
- #9 schedules whose worker is offline: instead of skipping silently, the
  scheduler records a failed run ('Worker offline: the scheduled command
  was not sent'), visible in Executions.
- An invalid cron expression (or any bad schedule) is a 400 with the
  parser's message instead of a 500.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 23:54:32 -05:00

207 lines
9.6 KiB
JavaScript

'use strict';
// The workflow engine (executeWorkflowSteps in server.js), driven through the API
// with real workers. Known bugs, when there are any, are written as test.failing: they
// document today's behaviour and start failing 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');
});
test('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]);
});
test('a goto to a missing step is refused on save (#20)', async () => {
const r = await sso('POST', '/api/workflows', { name: `int ${unique()}`, definition: { steps: [
{ id: 'jump', type: 'route', conditions: [{ default: true, goto: 'no-such-step' }] },
] } });
expect(r.status).toBe(400);
expect(r.json.problems[0]).toMatchObject({ step: 'jump', goto: 'no-such-step' });
});
test('a saved workflow whose goto is missing fails at runtime instead of completing (#20)', async () => {
// Workflows saved before the check existed can still contain one: write it directly.
const mysql = require('mysql2/promise');
const db = await mysql.createConnection({
host: process.env.DB_HOST || '127.0.0.1', port: Number(process.env.DB_PORT || 3306),
user: process.env.DB_USER || 'pulse', password: process.env.DB_PASSWORD || 'ci-only', database: process.env.DB_NAME || 'pulse',
});
const wf = require('crypto').randomUUID();
try {
await db.query('INSERT INTO workflows (id, name, description, definition, created_by) VALUES (?, ?, ?, ?, ?)', [
wf, `int legacy ${unique()}`, '', JSON.stringify({ steps: [
{ id: 'jump', type: 'route', conditions: [{ default: true, goto: 'no-such-step' }] },
{ id: 'after', type: 'execute', command: 'echo should-not-run', targets: ['node-01'] },
] }), 'int.tester',
]);
} finally { await db.end(); }
const ex = await waitForExecution(await start(wf));
expect(ex.status).toBe('failed');
expect(logs(ex, 'goto_error')[0]).toMatchObject({ target: 'no-such-step' });
expect(outputs(ex)).toEqual([]);
});
test('a workflow whose condition does not parse is refused on save', async () => {
const create = await sso('POST', '/api/workflows', { name: `int ${unique()}`, definition: { steps: [
{ id: 'a', type: 'execute', command: 'true', targets: ['node-01'], condition: 'state.constructor' },
] } });
expect(create.status).toBe(400);
expect(create.json.problems[0]).toMatchObject({ step: 'a', condition: 'state.constructor' });
const id = await createWorkflow([{ id: 'ok', type: 'execute', command: 'true', targets: ['node-01'] }]);
const update = await sso('PUT', `/api/workflows/${id}`, { name: 'x', definition: { steps: [
{ id: 'r', type: 'route', conditions: [{ if: 'process', goto: 'end' }] },
] } });
expect(update.status).toBe(400);
});
test('abort during a long command stops the workflow at once, without waiting for the command (#8)', async () => {
const id = await start(await createWorkflow([
{ id: 'slow', type: 'execute', command: 'sleep 20', targets: ['node-02'] },
{ id: 'after', type: 'execute', command: 'echo after-abort', targets: ['node-02'] },
]));
await sleep(1000);
const t0 = Date.now();
expect((await sso('POST', `/api/executions/${id}/abort`)).status).toBe(200);
await sleep(2500);
const ex = (await sso('GET', `/api/executions/${id}`)).json;
expect(ex.status).toBe('failed');
expect(logs(ex, 'step_started').map(l => l.step)).toEqual([1]);
expect(Date.now() - t0).toBeLessThan(10_000);
});