Worker unit tests, coverage floors, and an SBOM per main build
CI / Lint (eslint) (push) Successful in 17s
CI / Test (jest) (push) Successful in 18s
CI / Security (npm audit) (push) Failing after 12s
CI / Scan (secrets, workflows, shell) (push) Successful in 18s
CI / Integration (api) (push) Successful in 2m58s
CI / Lint (eslint) (pull_request) Successful in 24s
CI / Test (jest) (pull_request) Successful in 15s
CI / UI (playwright) (push) Successful in 3m11s
CI / Deploy (push) Skipped
CI / Notify (push) Skipped
CI / Security (npm audit) (pull_request) Failing after 12s
CI / Scan (secrets, workflows, shell) (pull_request) Successful in 15s
CI / Integration (api) (pull_request) Successful in 1m18s
CI / UI (playwright) (pull_request) Successful in 1m31s
CI / Deploy (pull_request) Skipped
CI / Notify (pull_request) Skipped
CI / Lint (eslint) (push) Successful in 17s
CI / Test (jest) (push) Successful in 18s
CI / Security (npm audit) (push) Failing after 12s
CI / Scan (secrets, workflows, shell) (push) Successful in 18s
CI / Integration (api) (push) Successful in 2m58s
CI / Lint (eslint) (pull_request) Successful in 24s
CI / Test (jest) (pull_request) Successful in 15s
CI / UI (playwright) (push) Successful in 3m11s
CI / Deploy (push) Skipped
CI / Notify (push) Skipped
CI / Security (npm audit) (pull_request) Failing after 12s
CI / Scan (secrets, workflows, shell) (pull_request) Successful in 15s
CI / Integration (api) (pull_request) Successful in 1m18s
CI / UI (playwright) (pull_request) Successful in 1m31s
CI / Deploy (pull_request) Skipped
CI / Notify (pull_request) Skipped
- worker/worker.js exports PulseWorker and starts only when run directly (no behaviour change for the service; CT 153 needs no redeploy). - tests/worker.test.js: success, exit code and stderr, timeout kill, capacity rejection, slot release after failure, ping/pong, no sends while disconnected. - jest coverage floors (ratchet at today's levels): lib/ 90% lines and statements, 80% branches and functions; worker/ 40%. - The audit job writes a CycloneDX SBOM of production dependencies and keeps it 90 days for each main build. Part of #45 phases 3 and 4. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
15f8ec4c9b
commit
38a6d1158d
6 files changed
+142
-18
No files matched your search
@@ -62,6 +62,15 @@ jobs:
|
||||
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
|
||||
- name: Audit production and dev dependencies
|
||||
run: npm audit --audit-level=high
|
||||
- name: Software bill of materials (production dependencies)
|
||||
run: npm sbom --sbom-format cyclonedx --omit dev > sbom.cdx.json
|
||||
- name: Keep the SBOM for each main build
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||
uses: actions/upload-artifact@c6a3b2bd78b3985e4b2f15397fec357f0fd808de # v3.2.2-node20 (v4 is unsupported on Gitea)
|
||||
with:
|
||||
name: sbom-cyclonedx
|
||||
path: sbom.cdx.json
|
||||
retention-days: 90
|
||||
|
||||
integration:
|
||||
name: Integration (api)
|
||||
|
||||
@@ -38,3 +38,4 @@ __pycache__/
|
||||
.ui-stack/
|
||||
test-results/
|
||||
playwright-report/
|
||||
sbom.cdx.json
|
||||
@@ -587,8 +587,8 @@ Everything runs from `.gitea/workflows/ci.yml`:
|
||||
| Job | What it does | When |
|
||||
|---|---|---|
|
||||
| `Lint (eslint)` | `npm run lint`, plus a check that fails on em or en dashes in Pulse-owned files | Every push and PR |
|
||||
| `Test (jest)` | `npm test`: unit tests for `lib/` and the deploy-script sandbox tests | Every push and PR |
|
||||
| `Security (npm audit)` | `npm audit --audit-level=high` | Every push and PR, and weekly (Monday 06:00) |
|
||||
| `Test (jest)` | `npm test`: unit tests for `lib/` and the worker, and the deploy-script sandbox tests. Fails if coverage drops below the floors in `package.json` (`lib/` 90% lines, `worker/` 40%) | Every push and PR |
|
||||
| `Security (npm audit)` | `npm audit --audit-level=high`, plus a CycloneDX SBOM of production dependencies (kept 90 days as the `sbom-cyclonedx` artifact of each `main` build) | Every push and PR, and weekly (Monday 06:00) |
|
||||
| `Scan (secrets, workflows, shell)` | gitleaks over the whole git history, actionlint on the workflow files (with shellcheck on their scripts), and shellcheck on every `*.sh` | Every push and PR |
|
||||
| `Integration (api)` | Starts the real server and three workers on Node 20 against MariaDB, then runs `tests/integration/` over HTTP and WebSocket: the Gandalf API contract, HTTP and WebSocket auth, and execution reliability (worker crash, capacity, abort) | Every push and PR |
|
||||
| `UI (playwright)` | Starts the real server and three workers against a MariaDB service container, then runs the Playwright tests in `tests/ui/` (desktop and phone) | Every push and PR |
|
||||
|
||||
@@ -31,5 +31,26 @@
|
||||
"@playwright/test": "1.64.1",
|
||||
"eslint": "^8.57.1",
|
||||
"jest": "^30.5.2"
|
||||
},
|
||||
"jest": {
|
||||
"testPathIgnorePatterns": [
|
||||
"/node_modules/",
|
||||
"/tests/ui/",
|
||||
"/tests/integration/"
|
||||
],
|
||||
"coverageThreshold": {
|
||||
"./lib/": {
|
||||
"statements": 90,
|
||||
"branches": 80,
|
||||
"functions": 80,
|
||||
"lines": 90
|
||||
},
|
||||
"./worker/": {
|
||||
"statements": 40,
|
||||
"branches": 70,
|
||||
"functions": 25,
|
||||
"lines": 40
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
'use strict';
|
||||
|
||||
// Unit tests for the worker's command execution (worker/worker.js), with no
|
||||
// server: results are captured from sendResult instead of a WebSocket.
|
||||
const { PulseWorker } = require('../worker/worker');
|
||||
|
||||
function makeWorker(opts = {}) {
|
||||
const w = new PulseWorker();
|
||||
w.maxConcurrentTasks = opts.max || 5;
|
||||
w.results = [];
|
||||
w.sendResult = r => w.results.push(r);
|
||||
return w;
|
||||
}
|
||||
const run = (w, command, extra = {}) =>
|
||||
w.executeCommand({ command, execution_id: 'ex-1', command_id: 'cmd-1', ...extra });
|
||||
|
||||
beforeAll(() => {
|
||||
jest.spyOn(console, 'log').mockImplementation(() => {});
|
||||
jest.spyOn(console, 'error').mockImplementation(() => {});
|
||||
});
|
||||
afterAll(() => jest.restoreAllMocks());
|
||||
|
||||
test('a successful command reports stdout, stderr and duration', async () => {
|
||||
const w = makeWorker();
|
||||
await run(w, 'echo out; echo err >&2');
|
||||
expect(w.results).toHaveLength(1);
|
||||
const r = w.results[0];
|
||||
expect(r).toMatchObject({
|
||||
type: 'command_result', execution_id: 'ex-1', command_id: 'cmd-1',
|
||||
worker_id: w.workerId, success: true, stdout: 'out\n', stderr: 'err\n',
|
||||
});
|
||||
expect(typeof r.duration).toBe('number');
|
||||
expect(w.activeTasks).toBe(0);
|
||||
});
|
||||
|
||||
test('a failing command reports its exit code and output (#14)', async () => {
|
||||
const w = makeWorker();
|
||||
await run(w, 'echo partial; echo broke >&2; exit 7');
|
||||
const r = w.results[0];
|
||||
expect(r).toMatchObject({ success: false, exit_code: 7, stdout: 'partial\n', stderr: 'broke\n' });
|
||||
expect(r.timed_out).toBeUndefined();
|
||||
expect(r.error).toMatch(/Command failed/);
|
||||
expect(w.activeTasks).toBe(0);
|
||||
});
|
||||
|
||||
test('a command that runs past its timeout is killed and reported as timed out', async () => {
|
||||
const w = makeWorker();
|
||||
const started = Date.now();
|
||||
await run(w, 'sleep 5', { timeout: 300 });
|
||||
expect(Date.now() - started).toBeLessThan(3000);
|
||||
expect(w.results[0]).toMatchObject({ success: false, timed_out: true, signal: 'SIGTERM' });
|
||||
expect(w.activeTasks).toBe(0);
|
||||
});
|
||||
|
||||
test('at capacity, extra commands get an immediate failure instead of silence (#7)', async () => {
|
||||
const w = makeWorker({ max: 1 });
|
||||
const first = run(w, 'sleep 0.5; echo first', { execution_id: 'ex-a', command_id: 'a' });
|
||||
await run(w, 'echo second', { execution_id: 'ex-b', command_id: 'b' });
|
||||
|
||||
const rejected = w.results.find(r => r.command_id === 'b');
|
||||
expect(rejected).toMatchObject({ success: false, execution_id: 'ex-b' });
|
||||
expect(rejected.error).toMatch(/capacity/);
|
||||
|
||||
await first;
|
||||
expect(w.results.find(r => r.command_id === 'a')).toMatchObject({ success: true, stdout: 'first\n' });
|
||||
expect(w.activeTasks).toBe(0);
|
||||
});
|
||||
|
||||
test('a slot frees up again after a command fails', async () => {
|
||||
const w = makeWorker({ max: 1 });
|
||||
await run(w, 'exit 1');
|
||||
await run(w, 'echo ok', { command_id: 'next' });
|
||||
expect(w.results.find(r => r.command_id === 'next')).toMatchObject({ success: true });
|
||||
});
|
||||
|
||||
test('ping is answered with pong over the socket', async () => {
|
||||
const w = makeWorker();
|
||||
const sent = [];
|
||||
w.ws = { readyState: 1, send: m => sent.push(JSON.parse(m)) }; // 1 = WebSocket.OPEN
|
||||
await w.handleMessage({ type: 'ping' });
|
||||
expect(sent).toEqual([{ type: 'pong', worker_id: w.workerId }]);
|
||||
});
|
||||
|
||||
test('results are not sent while disconnected', () => {
|
||||
const w = new PulseWorker();
|
||||
const send = jest.fn();
|
||||
w.ws = { readyState: 3, send }; // 3 = CLOSED
|
||||
w.sendResult({ type: 'command_result' });
|
||||
expect(send).not.toHaveBeenCalled();
|
||||
});
|
||||
+19
-16
@@ -256,22 +256,25 @@ class PulseWorker {
|
||||
}
|
||||
}
|
||||
|
||||
// Start worker
|
||||
const worker = new PulseWorker();
|
||||
module.exports = { PulseWorker };
|
||||
|
||||
// Handle graceful shutdown
|
||||
process.on('SIGTERM', async () => {
|
||||
await worker.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
// Start only when run directly (node worker.js), so tests can load the class.
|
||||
if (require.main === module) {
|
||||
const worker = new PulseWorker();
|
||||
|
||||
process.on('SIGINT', async () => {
|
||||
await worker.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
// Handle graceful shutdown
|
||||
process.on('SIGTERM', async () => {
|
||||
await worker.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
// Start the worker
|
||||
worker.start().catch((error) => {
|
||||
console.error('[PULSE Worker] Fatal error:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
process.on('SIGINT', async () => {
|
||||
await worker.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
worker.start().catch((error) => {
|
||||
console.error('[PULSE Worker] Fatal error:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user