const assert = require("node:assert/strict"); const { randomUUID } = require("node:crypto"); const { setTimeout: delay } = require("node:timers/promises"); async function main() { assert.ok(process.env.ARCHIE_SMOKE_URL, "Set ARCHIE_SMOKE_URL to a disposable Archie stack"); const origin = new URL(process.env.ARCHIE_SMOKE_URL); const token = process.env.ARCHIE_SMOKE_TOKEN; assert.equal(origin.protocol, "http:"); assert.equal(origin.hostname, "127.0.0.1", "Use a disposable Archie stack on IPv4 loopback"); assert.ok(token, "Set ARCHIE_SMOKE_TOKEN to the disposable stack's admin token"); async function request(path, { method = "GET", body, authenticated = true, status = 200 } = {}) { const response = await fetch(new URL(path, origin), { method, redirect: "error", signal: AbortSignal.timeout(10_000), headers: { ...(authenticated && { Authorization: `Bearer ${token}` }), ...(body !== undefined && { "Content-Type": "application/json" }), }, body: body === undefined ? undefined : JSON.stringify(body), }).catch((err) => { throw new Error(`${method} ${path}: could not reach Archie at ${origin.origin}: ${err.cause?.message || err.message}`, { cause: err }); }); assert.equal(response.status, status, `${method} ${path}: expected HTTP ${status}, got ${response.status}`); return response.json(); } const ready = await request("/readyz", { authenticated: false }); assert.equal(ready.ready, true, "Archie is not ready"); await request("/api/workflows", { authenticated: false, status: 401 }); const name = `smoke-${randomUUID()}`; const saved = await request("/api/workflows", { method: "POST", status: 201, body: { name, body: `repository: none\nsteps:\n - id: notify\n type: run.notify\n settings: {title: ${name}}\n` }, }); const trigger = { name, workflow_id: saved.id, every: "1m" }; const created = await request("/api/triggers", { method: "POST", body: trigger, status: 201 }); try { const deadline = Date.now() + 90_000; let last = "no scheduled run"; while (Date.now() < deadline) { const recent = await request("/api/runs"); const run = recent.find((row) => row.workflow_id === saved.id); if (run) { last = `run ${run.id}: ${run.state}`; assert.ok(!["failed", "cancelled"].includes(run.state), `${last}: ${run.error || "no error returned"}`); if (run.state === "succeeded") { const trail = await request(`/api/runs/${run.id}`); assert.equal(trail.run.workflow_version, saved.version, "Run used the wrong workflow version"); assert.equal(trail.origin?.trigger_id, created.id, "Run trail is missing the schedule trigger"); assert.ok(trail.origin?.event_id, "Run trail is missing the originating event"); assert.equal(trail.origin.declared_event_type, "schedule.tick"); const messages = await request("/api/composer/messages"); assert.equal(messages.filter((m) => m.run_id === run.id && m.title === name).length, 1, "Expected one composer notification linked to the run"); console.log(`Schedule, run trail and composer notification verified for ${run.id}`); return; } } await delay(1_000); } throw new Error(`Timed out waiting for ${name}: ${last}`); } finally { await request(`/api/triggers/${created.id}`, { method: "PUT", body: { ...trigger, disabled: true } }); } } main().catch((err) => { console.error(err.message); process.exitCode = 1; });