Files
pipelines/web/test/runEvents.test.mjs
T

406 lines
16 KiB
JavaScript

// The live-updates fold, exercised as a pure delta sequence so the client's
// correctness is pinned without a device or a socket: a created run, a step
// started, a step finished, a run finished, a delta for a run the list does not
// hold, an out-of-order/duplicate delta, and a reconnect resync.
//
// A direct port of android/.../ui/RunEventsTest.kt, so the browser fold and the
// app fold are proven against the same shape.
import { test } from "node:test";
import assert from "node:assert/strict";
import {
TERMINAL_RUN_STATUSES,
executingFor,
foldRunEvent,
foldRunEventDetail,
isTerminalRunStatus,
isWaitingRunStatus,
newRunSummaryFromFrame,
runDetailNeedsFullSync,
runLifecycleRank,
stepLifecycleRank,
} from "../src/runEvents.js";
function summary(id, { status = "running", executing = true, currentStage = "", completedSteps = 0, finishedAt = "" } = {}) {
return {
id,
pipeline_id: 4,
name: "pipeline",
status,
executing,
current_stage: currentStage,
completed_steps: completedSteps,
created_at: "2026-10-17T09:00:00Z",
finished_at: finishedAt,
};
}
function detail({ id = 1, status = "queued", executing = true, currentStage = "", completedSteps = 0, finishedAt = "", steps = [] } = {}) {
return {
id,
pipeline_id: 4,
name: "pipeline",
status,
executing,
current_stage: currentStage,
completed_steps: completedSteps,
created_at: "2026-10-17T09:00:00Z",
finished_at: finishedAt,
steps,
};
}
function step({ sequence = 1, stage = "build", status = "running", outcome = "", target = "" } = {}) {
return { sequence, stage, status, outcome, target };
}
function frame({
runId = 1,
status = "",
currentStage = "",
outcome = "",
completedSteps = 0,
finishedAt = "",
step: stepDelta = null,
pipelineId = 0,
name = "",
title = "",
version = 0,
createdAt = "",
} = {}) {
return {
run_id: runId,
workspace: "/work",
pipeline_id: pipelineId,
name,
title,
version,
status,
outcome,
current_stage: currentStage,
completed_steps: completedSteps,
created_at: createdAt,
finished_at: finishedAt,
step: stepDelta,
};
}
// --- lifecycle vocabulary ----------------------------------------------------
test("run summaries and detail preserve notification mode from live frames", () => {
const incoming = { ...frame({ pipelineId: 4, name: "test", status: "queued" }), notify_mode: "off" };
assert.equal(newRunSummaryFromFrame(incoming).notify_mode, "off");
const runs = [summary(1)];
const folded = foldRunEvent(runs, { ...incoming, status: "running" });
assert.equal(folded.runs[0].notify_mode, "off");
const result = foldRunEventDetail(detail({status:"running"}), { ...incoming, status: "failed" });
assert.equal(result.notify_mode, "off");
});
test("the lifecycle ladders mirror RunEvents.kt", () => {
assert.deepEqual(TERMINAL_RUN_STATUSES, ["succeeded", "failed", "canceled", "interrupted"]);
assert.equal(runLifecycleRank("queued"), 0);
assert.equal(runLifecycleRank("running"), 1);
assert.equal(runLifecycleRank("waiting"), 1);
assert.equal(runLifecycleRank("succeeded"), 2);
assert.equal(runLifecycleRank("nonsense"), -1);
assert.equal(stepLifecycleRank("running"), 1);
assert.equal(stepLifecycleRank("completed"), 2);
assert.equal(stepLifecycleRank(""), -1);
assert.equal(isWaitingRunStatus(" waiting "), true);
assert.equal(isTerminalRunStatus("SUCCEEDED"), true);
assert.equal(isTerminalRunStatus("waiting"), false);
assert.equal(executingFor("queued", false), true);
assert.equal(executingFor("waiting", true), false);
assert.equal(executingFor("", true), true);
assert.equal(executingFor("", false), false);
});
// --- runs list ---------------------------------------------------------------
test("a delta updates a matching row in place without reordering", () => {
const runs = [summary(3, { status: "succeeded", executing: false }), summary(1)];
const fold = foldRunEvent(runs, frame({ runId: 1, status: "running", currentStage: "build", completedSteps: 1 }));
assert.equal(fold.needsResync, false);
assert.deepEqual(fold.runs.map((run) => run.id), [3, 1]);
const updated = fold.runs.find((run) => run.id === 1);
assert.equal(updated.status, "running");
assert.equal(updated.current_stage, "build");
assert.equal(updated.completed_steps, 1);
assert.equal(updated.executing, true);
assert.equal(fold.runs[0], runs[0], "the other row is shared, not copied");
});
test("a new run appears from its frame without a fetch", () => {
const runs = [summary(1, { status: "succeeded", executing: false })];
const fold = foldRunEvent(
runs,
frame({
runId: 9,
status: "queued",
pipelineId: 4,
name: "pipeline",
title: "a fresh run",
version: 3,
createdAt: "2026-10-17T09:30:00Z",
}),
);
assert.equal(fold.needsResync, false);
assert.deepEqual(fold.runs.map((run) => run.id), [9, 1]);
const added = fold.runs[0];
assert.equal(added.pipeline_id, 4);
assert.equal(added.name, "pipeline");
assert.equal(added.title, "a fresh run");
assert.equal(added.version, 3);
assert.equal(added.status, "queued");
assert.equal(added.executing, true);
assert.equal(added.created_at, "2026-10-17T09:30:00Z");
});
test("a frame for an unknown run without identity still asks for a resync", () => {
const runs = [summary(1)];
const fold = foldRunEvent(runs, frame({ runId: 9, status: "queued" }));
assert.equal(fold.needsResync, true);
assert.equal(fold.runs, runs);
});
test("an inserted run then advances in place", () => {
const created = foldRunEvent(
[],
frame({ runId: 9, status: "queued", pipelineId: 4, name: "pipeline", version: 1, createdAt: "2026-10-17T09:30:00Z" }),
).runs;
const running = foldRunEvent(created, frame({ runId: 9, status: "running", currentStage: "build", completedSteps: 1 }));
assert.equal(running.needsResync, false);
assert.equal(running.runs.length, 1);
assert.equal(running.runs[0].status, "running");
assert.equal(running.runs[0].current_stage, "build");
const done = foldRunEvent(
running.runs,
frame({ runId: 9, status: "succeeded", outcome: "success", completedSteps: 1, finishedAt: "2026-10-17T09:31:00Z" }),
);
assert.equal(done.runs.length, 1);
assert.equal(done.runs[0].status, "succeeded");
assert.equal(done.runs[0].executing, false);
});
test("a delta for an unknown run also asks for a resync", () => {
const runs = [summary(1, { status: "succeeded", executing: false })];
const fold = foldRunEvent(runs, frame({ runId: 42, status: "running", currentStage: "x" }));
assert.equal(fold.needsResync, true);
assert.deepEqual(fold.runs.map((run) => run.id), [1]);
});
test("a duplicate or out-of-order list delta never regresses a run", () => {
const settled = [summary(1, { status: "succeeded", executing: false, finishedAt: "2026-10-17T09:05:00Z" })];
const fold = foldRunEvent(settled, frame({ runId: 1, status: "running", currentStage: "build" }));
assert.equal(fold.needsResync, false);
assert.equal(fold.runs[0].status, "succeeded");
assert.equal(fold.runs[0].executing, false);
assert.equal(fold.runs, settled, "an unchanged fold keeps the original list");
});
test("a terminal run delta settles the row and stops executing", () => {
const fold = foldRunEvent(
[summary(1, { status: "running" })],
frame({ runId: 1, status: "failed", outcome: "error", finishedAt: "2026-10-17T09:06:00Z" }),
);
const run = fold.runs[0];
assert.equal(run.status, "failed");
assert.equal(run.outcome, "error");
assert.equal(run.executing, false);
assert.equal(run.finished_at, "2026-10-17T09:06:00Z");
});
test("a stale running frame after terminal leaves the list unchanged whole", () => {
const terminal = [summary(1, { status: "succeeded", executing: false, finishedAt: "2026-10-17T09:06:00Z" })];
const fold = foldRunEvent(terminal, frame({ runId: 1, status: "running", currentStage: "build", completedSteps: 3 }));
assert.equal(fold.runs, terminal, "the whole frame is dropped, not just its status");
assert.equal(fold.runs[0].current_stage, "");
assert.equal(fold.runs[0].completed_steps, 0);
});
test("a second, different terminal word is dropped", () => {
const failed = [summary(1, { status: "failed", executing: false })];
const fold = foldRunEvent(failed, frame({ runId: 1, status: "canceled" }));
assert.equal(fold.runs, failed);
assert.equal(fold.runs[0].status, "failed");
});
// --- run detail --------------------------------------------------------------
test("a step started delta adds the visit and runs the run", () => {
const folded = foldRunEventDetail(
detail(),
frame({ runId: 1, status: "running", currentStage: "build", step: { sequence: 1, stage: "build", status: "running" } }),
);
assert.equal(folded.status, "running");
assert.equal(folded.executing, true);
assert.equal(folded.current_stage, "build");
assert.equal(folded.steps.length, 1);
assert.equal(folded.steps[0].sequence, 1);
assert.equal(folded.steps[0].status, "running");
});
test("a step finished delta settles the visit and counts it", () => {
const started = detail({ status: "running", currentStage: "build", steps: [step()] });
const folded = foldRunEventDetail(
started,
frame({
runId: 1,
status: "running",
currentStage: "build",
completedSteps: 1,
step: { sequence: 1, stage: "build", status: "completed", outcome: "success", target: "#success" },
}),
);
assert.equal(folded.steps.length, 1);
assert.equal(folded.steps[0].status, "completed");
assert.equal(folded.steps[0].outcome, "success");
assert.equal(folded.steps[0].target, "#success");
assert.equal(folded.completed_steps, 1);
assert.equal(folded.executing, true, "the run is still going until the terminal frame");
});
test("a run finished delta settles the run with its terminal fields", () => {
const folded = foldRunEventDetail(
detail({ status: "running", executing: true, currentStage: "build" }),
frame({ runId: 1, status: "succeeded", outcome: "success", finishedAt: "2026-10-17T09:07:00Z", completedSteps: 2 }),
);
assert.equal(folded.status, "succeeded");
assert.equal(folded.outcome, "success");
assert.equal(folded.executing, false);
assert.equal(folded.finished_at, "2026-10-17T09:07:00Z");
assert.equal(folded.completed_steps, 2);
});
test("a duplicate step delta is idempotent", () => {
const started = foldRunEventDetail(
detail(),
frame({ runId: 1, status: "running", step: { sequence: 1, stage: "build", status: "running" } }),
);
const again = foldRunEventDetail(
started,
frame({ runId: 1, status: "running", step: { sequence: 1, stage: "build", status: "running" } }),
);
assert.equal(again.steps.length, 1);
assert.equal(again, started, "a duplicate changes nothing");
});
test("an out-of-order step delta never reopens a settled visit", () => {
const settled = detail({
status: "running",
steps: [step({ status: "completed", outcome: "success", target: "#success" })],
});
const folded = foldRunEventDetail(
settled,
frame({ runId: 1, status: "running", step: { sequence: 1, stage: "build", status: "running" } }),
);
assert.equal(folded.steps[0].status, "completed");
assert.equal(folded.steps[0].outcome, "success");
assert.equal(folded, settled);
});
test("a settled step keeps its word against another terminal delta", () => {
const settled = detail({
status: "running",
steps: [step({ status: "completed", outcome: "success", target: "#success" })],
});
const folded = foldRunEventDetail(
settled,
frame({ runId: 1, status: "running", step: { sequence: 1, stage: "build", status: "failed", outcome: "error" } }),
);
assert.equal(folded.steps[0].status, "completed");
assert.equal(folded.steps[0].outcome, "success");
assert.equal(folded, settled);
});
test("a later visit is inserted in sequence order", () => {
const started = detail({
status: "running",
steps: [step({ sequence: 1, status: "completed", outcome: "success", target: "#success" })],
});
const folded = foldRunEventDetail(
started,
frame({ runId: 1, status: "running", step: { sequence: 2, stage: "review", status: "running" } }),
);
assert.deepEqual(folded.steps.map((s) => s.sequence), [1, 2]);
const lower = foldRunEventDetail(
folded,
frame({ runId: 1, status: "running", step: { sequence: 0, stage: "plan", status: "running" } }),
);
assert.deepEqual(lower.steps.map((s) => s.sequence), [0, 1, 2]);
});
test("a frame for another run leaves the detail untouched", () => {
const mine = detail({ id: 1, status: "running" });
const folded = foldRunEventDetail(mine, frame({ runId: 2, status: "succeeded" }));
assert.equal(folded, mine);
});
test("resource_wait_set sets and clears the wait explicitly", () => {
const waiting = foldRunEventDetail(
detail({ status: "running" }),
{ ...frame({ runId: 1, status: "waiting" }), resource_wait_set: true, resource_wait: { resource: "gpu", stage: "build", sequence: 1, queued_since: "2026-10-17T09:00:00Z" } },
);
assert.deepEqual(waiting.resource_wait, { resource: "gpu", stage: "build", sequence: 1, queued_since: "2026-10-17T09:00:00Z" });
const cleared = foldRunEventDetail(waiting, { ...frame({ runId: 1 }), resource_wait_set: true });
assert.equal(cleared.resource_wait, null);
// A terminal status always clears it.
const terminal = foldRunEventDetail(waiting, frame({ runId: 1, status: "succeeded" }));
assert.equal(terminal.resource_wait, null);
});
// --- resync ------------------------------------------------------------------
test("a reconnect resync takes the server snapshot then ignores a late older delta", () => {
const stale = detail({ status: "running", currentStage: "build" });
const snapshot = foldRunEventDetail(
stale,
frame({
runId: 1,
status: "succeeded",
outcome: "success",
completedSteps: 1,
finishedAt: "2026-10-17T09:07:00Z",
step: { sequence: 1, stage: "build", status: "completed", outcome: "success", target: "#success" },
}),
);
const after = foldRunEventDetail(snapshot, frame({ runId: 1, status: "running", currentStage: "build" }));
assert.equal(after.status, "succeeded");
assert.equal(after.outcome, "success");
assert.equal(after.executing, false);
assert.equal(after.completed_steps, 1);
assert.equal(after.steps.length, 1);
assert.equal(after.steps[0].status, "completed");
});
// --- sync heuristic ----------------------------------------------------------
test("settled visits, terminal runs and pending decisions need a full detail sync", () => {
assert.equal(runDetailNeedsFullSync(frame({ runId: 1, step: { sequence: 1, status: "completed" } })), true);
assert.equal(runDetailNeedsFullSync(frame({ runId: 1, step: { sequence: 1, status: "failed" } })), true);
assert.equal(runDetailNeedsFullSync(frame({ runId: 1, status: "succeeded" })), true);
assert.equal(runDetailNeedsFullSync(frame({ runId: 1, status: "canceled" })), true);
assert.equal(runDetailNeedsFullSync(frame({ runId: 1, status: "waiting" })), true);
assert.equal(runDetailNeedsFullSync(frame({ runId: 1, step: { sequence: 1, status: "waiting" } })), true);
assert.equal(runDetailNeedsFullSync(frame({ runId: 1, status: "running", step: { sequence: 1, status: "running" } })), false);
assert.equal(runDetailNeedsFullSync(frame({ runId: 1, status: "queued" })), false);
assert.equal(runDetailNeedsFullSync(frame({ runId: 1, status: "running" })), false);
});
test("a waiting human stage is not executing", () => {
assert.equal(isTerminalRunStatus("waiting"), false);
const fold = foldRunEvent([summary(1, { status: "running" })], frame({ runId: 1, status: "waiting", currentStage: "gate" }));
assert.equal(fold.runs[0].status, "waiting");
assert.equal(fold.runs[0].executing, false, "a parked human stage shows no spinner");
});
test("newRunSummaryFromFrame requires a pipeline identity and a name", () => {
assert.equal(newRunSummaryFromFrame(frame({ runId: 9, status: "queued" })), null);
assert.equal(newRunSummaryFromFrame(frame({ runId: 9, status: "queued", pipelineId: 4, name: " " })), null);
const built = newRunSummaryFromFrame(frame({ runId: 9, status: "queued", pipelineId: 4, name: "p", version: 2 }));
assert.equal(built.id, 9);
assert.equal(built.pipeline_id, 4);
assert.equal(built.title, null);
});