Files
pulse/tests/integration/workflows.int.js
T
jaredandClaude Opus 5.5 040ad028c0
CI / UI screenshots (before/after) (push) Skipped
CI / Lint (eslint) (push) Successful in 25s
CI / Test (jest) (push) Successful in 25s
CI / Security (npm audit) (push) Successful in 21s
CI / Scan (secrets, workflows, shell) (push) Successful in 2m0s
CI / Integration (api) (push) Successful in 2m23s
CI / Lint (eslint) (pull_request) Successful in 24s
CI / Test (jest) (pull_request) Successful in 32s
CI / Security (npm audit) (pull_request) Successful in 39s
CI / UI (playwright) (push) Successful in 2m6s
CI / Deploy (push) Skipped
CI / Deploy worker (push) Skipped
CI / Notify (push) Skipped
CI / Integration (api) (pull_request) Successful in 2m13s
CI / Scan (secrets, workflows, shell) (pull_request) Successful in 2m46s
CI / UI screenshots (before/after) (pull_request) Successful in 27s
CI / UI (playwright) (pull_request) Successful in 3m5s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped
Safer workflow conditions; Pulse is admin-only
- Workflow conditions (step.condition and route `if`) are now parsed and
  interpreted by lib/condition.js instead of being run with Node's vm
  module. The language covers what workflows use: state/params lookups,
  parseInt/parseFloat/Number/String/Boolean, a few string methods, the
  usual operators with JavaScript semantics. Conditions can only read the
  workflow's own data. Every condition in the production workflows gives
  the same result as before (differential test, 150 random states each).
- Creating and updating a workflow rejects conditions that don't parse
  (400 with the step and the error).
- Creating a workflow requires admin, like editing already did.
- Only the admin group may use Pulse (pages, API and live WebSocket);
  employees get 403. Gandalf and workers use API keys and are unaffected.
- The live-update UI test finds its execution's row by id instead of
  counting rows (the list is capped at 50).

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

172 lines
7.9 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');
});
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);
});