849a184561
Day-grouped run list with sticky day headers (Today / Yesterday / d MMM yyyy / Unknown date), a usage roll-up block, the app's row hierarchy and a bounded filter/sort menu — mirroring RunHistoryScreen.kt, ListFilter.kt and ListSort.kt. New pure helpers live in src/runs.js with focused unit tests; app.js keeps the wiring and sets server strings with textContent. R1 done, R2 partial: no per-row "needs you" mark exists in the Android app; its wording is deferred to R15 (separate attention surface).
394 lines
15 KiB
JavaScript
394 lines
15 KiB
JavaScript
// Pure unit tests for the runs-list helpers: filtering (status/pipeline/time,
|
|
// AND-combined), every sort comparator with its id tie-break, day grouping,
|
|
// the wording helpers and the list's slot model. No server, no DOM.
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import {
|
|
RUN_STATES,
|
|
RUN_TIME_RANGES,
|
|
RUN_SORTS,
|
|
runStateOf,
|
|
runIdentity,
|
|
versionLabel,
|
|
visitCount,
|
|
progressText,
|
|
metaText,
|
|
formatDuration,
|
|
secondsSince,
|
|
durationSeconds,
|
|
localDayKey,
|
|
dayLabel,
|
|
defaultRunFilters,
|
|
runFiltersActiveCount,
|
|
runMatchesTimeRange,
|
|
filterRuns,
|
|
pipelineFilterOptions,
|
|
runComparator,
|
|
sortRuns,
|
|
groupRunsByDay,
|
|
runListEntries,
|
|
NO_RUNS_MESSAGE,
|
|
NO_MATCH_MESSAGE,
|
|
} from "../src/runs.js";
|
|
|
|
/** A finished run on the Today of the tests, with every field the list reads. */
|
|
function run(overrides = {}) {
|
|
return {
|
|
id: 1,
|
|
pipeline_id: 1,
|
|
name: "Pipe",
|
|
title: null,
|
|
version: 1,
|
|
status: "succeeded",
|
|
executing: false,
|
|
current_stage: "",
|
|
completed_steps: 0,
|
|
created_at: "2026-10-19T10:00:00Z",
|
|
finished_at: "2026-10-19T10:01:00Z",
|
|
diagnostic: "",
|
|
resource_wait: null,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
const TODAY = "2026-10-19";
|
|
const ZONE = "UTC";
|
|
const TIME_OPTS = { zone: ZONE, locale: "en-GB" };
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Vocabulary and wording
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("runStateOf maps the server vocabulary and the outcome synonyms", () => {
|
|
assert.equal(runStateOf("running"), "running");
|
|
assert.equal(runStateOf("succeeded"), "succeeded");
|
|
assert.equal(runStateOf("success"), "succeeded");
|
|
assert.equal(runStateOf("completed"), "succeeded");
|
|
assert.equal(runStateOf("done"), "succeeded");
|
|
assert.equal(runStateOf("error"), "failed");
|
|
assert.equal(runStateOf("cancelled"), "canceled");
|
|
assert.equal(runStateOf(" INTERRUPTED "), "interrupted");
|
|
assert.equal(runStateOf(""), "unknown");
|
|
assert.equal(runStateOf("nonsense"), "unknown");
|
|
});
|
|
|
|
test("RUN_STATES is exactly the server run vocabulary in display order", () => {
|
|
assert.deepEqual(RUN_STATES, [
|
|
"queued",
|
|
"running",
|
|
"waiting",
|
|
"succeeded",
|
|
"failed",
|
|
"canceled",
|
|
"interrupted",
|
|
]);
|
|
});
|
|
|
|
test("runIdentity falls back to the pipeline name and only splits when titled", () => {
|
|
assert.deepEqual(runIdentity(" Fix the thing ", "Plan and land"), {
|
|
label: "Fix the thing",
|
|
pipeline: "Plan and land",
|
|
});
|
|
assert.deepEqual(runIdentity(null, "Plan and land"), { label: "Plan and land", pipeline: null });
|
|
assert.deepEqual(runIdentity(" ", "Plan and land"), { label: "Plan and land", pipeline: null });
|
|
});
|
|
|
|
test("versionLabel and visitCount use the app's singular/plural spelling", () => {
|
|
assert.equal(versionLabel(5), "v5");
|
|
assert.equal(visitCount(1, "completed"), "1 completed visit");
|
|
assert.equal(visitCount(0, "completed"), "0 completed visits");
|
|
assert.equal(visitCount(3, "completed"), "3 completed visits");
|
|
});
|
|
|
|
test("progressText carries resource wait, completed visits and the stage", () => {
|
|
assert.equal(
|
|
progressText(run({ completed_steps: 2, current_stage: "implementer" })),
|
|
"2 completed visits · stage implementer",
|
|
);
|
|
assert.equal(
|
|
progressText(
|
|
run({ completed_steps: 1, current_stage: "reviewer", resource_wait: { resource: "pixel-9" } }),
|
|
),
|
|
"Waiting for resource pixel-9 · 1 completed visit · stage reviewer",
|
|
);
|
|
assert.equal(progressText(run({ completed_steps: 0 })), "0 completed visits");
|
|
});
|
|
|
|
test("metaText shows a live elapsed while executing and id/start/duration at rest", () => {
|
|
const executing = run({
|
|
id: 7,
|
|
executing: true,
|
|
created_at: "2026-10-19T09:30:00Z",
|
|
finished_at: "",
|
|
});
|
|
assert.equal(metaText(executing, Date.parse("2026-10-19T09:31:05Z"), TIME_OPTS), "run 7 · 1m 5s");
|
|
// A future start (clock skew) reads as 0s, never a negative.
|
|
assert.equal(metaText(executing, Date.parse("2026-10-19T09:00:00Z"), TIME_OPTS), "run 7 · 0s");
|
|
|
|
const done = run({
|
|
id: 7,
|
|
created_at: "2026-10-19T09:30:00Z",
|
|
finished_at: "2026-10-19T09:32:05Z",
|
|
});
|
|
assert.equal(metaText(done, 0, TIME_OPTS), "run 7 · 19 Oct 09:30 · 2m 5s");
|
|
});
|
|
|
|
test("formatDuration, secondsSince and durationSeconds mirror Format.kt", () => {
|
|
assert.equal(formatDuration(0), "0s");
|
|
assert.equal(formatDuration(59), "59s");
|
|
assert.equal(formatDuration(60), "1m 0s");
|
|
assert.equal(formatDuration(125), "2m 5s");
|
|
assert.equal(formatDuration(3725), "1h 2m");
|
|
assert.equal(formatDuration(-5), "0s");
|
|
|
|
assert.equal(secondsSince("2026-10-19T09:30:00Z", Date.parse("2026-10-19T09:31:05Z")), 65);
|
|
assert.equal(secondsSince("bad", 0), null);
|
|
assert.equal(durationSeconds("2026-10-19T09:30:00Z", "2026-10-19T09:32:05Z"), 125);
|
|
assert.equal(durationSeconds("2026-10-19T09:32:05Z", "2026-10-19T09:30:00Z"), null);
|
|
assert.equal(durationSeconds("", ""), null);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Filters
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("default filters change nothing and count as zero active facets", () => {
|
|
const runs = [run({ id: 1 }), run({ id: 2, status: "failed" })];
|
|
const filters = defaultRunFilters();
|
|
assert.equal(runFiltersActiveCount(filters), 0);
|
|
assert.equal(filterRuns(runs, filters, TODAY, ZONE), runs);
|
|
});
|
|
|
|
test("status filter narrows to the selected states; an empty set is any status", () => {
|
|
const runs = [
|
|
run({ id: 1, status: "succeeded" }),
|
|
run({ id: 2, status: "failed" }),
|
|
run({ id: 3, status: "canceled" }),
|
|
];
|
|
const filters = { ...defaultRunFilters(), statuses: new Set(["failed"]) };
|
|
assert.deepEqual(
|
|
filterRuns(runs, filters, TODAY, ZONE).map((r) => r.id),
|
|
[2],
|
|
);
|
|
// "success" resolves to the succeeded state, so selecting it matches.
|
|
const succeeded = { ...defaultRunFilters(), statuses: new Set(["succeeded"]) };
|
|
assert.deepEqual(
|
|
filterRuns([run({ id: 9, status: "success" })], succeeded, TODAY, ZONE).map((r) => r.id),
|
|
[9],
|
|
);
|
|
assert.equal(runFiltersActiveCount(filters), 1);
|
|
});
|
|
|
|
test("pipeline filter is a single-select; null means any pipeline", () => {
|
|
const runs = [run({ id: 1, pipeline_id: 3 }), run({ id: 2, pipeline_id: 4 })];
|
|
const filters = { ...defaultRunFilters(), pipelineId: 4 };
|
|
assert.deepEqual(
|
|
filterRuns(runs, filters, TODAY, ZONE).map((r) => r.id),
|
|
[2],
|
|
);
|
|
assert.equal(runFiltersActiveCount(filters), 1);
|
|
});
|
|
|
|
test("time range is inclusive calendar days ending today and rejects future/blank", () => {
|
|
const at = (created_at) => run({ created_at });
|
|
assert.equal(runMatchesTimeRange(at("2026-10-19T00:00:00Z"), "today", TODAY, ZONE), true);
|
|
assert.equal(runMatchesTimeRange(at("2026-10-18T23:59:59Z"), "today", TODAY, ZONE), false);
|
|
// last 7 days covers today and the six days before it (>= 13 Oct).
|
|
assert.equal(runMatchesTimeRange(at("2026-10-13T00:00:00Z"), "last_7_days", TODAY, ZONE), true);
|
|
assert.equal(runMatchesTimeRange(at("2026-10-12T23:00:00Z"), "last_7_days", TODAY, ZONE), false);
|
|
// last 30 days covers >= 20 Sep.
|
|
assert.equal(runMatchesTimeRange(at("2026-09-20T00:00:00Z"), "last_30_days", TODAY, ZONE), true);
|
|
assert.equal(runMatchesTimeRange(at("2026-09-19T00:00:00Z"), "last_30_days", TODAY, ZONE), false);
|
|
// A future-dated run and a blank/unparseable timestamp never match a window.
|
|
assert.equal(runMatchesTimeRange(at("2026-10-20T00:00:00Z"), "today", TODAY, ZONE), false);
|
|
assert.equal(runMatchesTimeRange(at("not-a-date"), "today", TODAY, ZONE), false);
|
|
assert.equal(runMatchesTimeRange(at(""), "last_30_days", TODAY, ZONE), false);
|
|
// Any time matches everything, including blank.
|
|
assert.equal(runMatchesTimeRange(at(""), "any_time", TODAY, ZONE), true);
|
|
});
|
|
|
|
test("the three facets AND-combine and the count is one per facet", () => {
|
|
const runs = [
|
|
run({ id: 1, pipeline_id: 3, status: "failed", created_at: "2026-10-19T10:00:00Z" }),
|
|
run({ id: 2, pipeline_id: 3, status: "succeeded", created_at: "2026-10-19T10:00:00Z" }),
|
|
run({ id: 3, pipeline_id: 4, status: "failed", created_at: "2026-10-19T10:00:00Z" }),
|
|
run({ id: 4, pipeline_id: 3, status: "failed", created_at: "2026-08-01T10:00:00Z" }),
|
|
];
|
|
const filters = {
|
|
statuses: new Set(["failed"]),
|
|
pipelineId: 3,
|
|
timeRange: "last_7_days",
|
|
};
|
|
assert.equal(runFiltersActiveCount(filters), 3);
|
|
assert.deepEqual(
|
|
filterRuns(runs, filters, TODAY, ZONE).map((r) => r.id),
|
|
[1],
|
|
);
|
|
});
|
|
|
|
test("pipelineFilterOptions names each pipeline from its newest non-blank run and sorts by name then id", () => {
|
|
const options = pipelineFilterOptions([
|
|
run({ pipeline_id: 5, name: "beta" }),
|
|
run({ pipeline_id: 5, name: "" }),
|
|
run({ pipeline_id: 2, name: "Alpha" }),
|
|
run({ pipeline_id: 9, name: "" }),
|
|
]);
|
|
assert.deepEqual(options, [
|
|
{ id: 2, name: "Alpha" },
|
|
{ id: 5, name: "beta" },
|
|
{ id: 9, name: "Pipeline 9" },
|
|
]);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Sort: every comparator plus an id tie-break
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("newest first orders by start descending, then id descending", () => {
|
|
const a = run({ id: 1, created_at: "2026-10-19T10:00:00Z" });
|
|
const b = run({ id: 2, created_at: "2026-10-19T10:00:00Z" });
|
|
const c = run({ id: 3, created_at: "2026-10-19T11:00:00Z" });
|
|
assert.deepEqual(
|
|
sortRuns([a, b, c], "newest_first").map((r) => r.id),
|
|
[3, 2, 1],
|
|
);
|
|
});
|
|
|
|
test("oldest first orders by start ascending, then id ascending", () => {
|
|
const a = run({ id: 1, created_at: "2026-10-19T10:00:00Z" });
|
|
const b = run({ id: 2, created_at: "2026-10-19T10:00:00Z" });
|
|
const c = run({ id: 3, created_at: "2026-10-19T11:00:00Z" });
|
|
assert.deepEqual(
|
|
sortRuns([a, b, c], "oldest_first").map((r) => r.id),
|
|
[1, 2, 3],
|
|
);
|
|
});
|
|
|
|
test("longest duration puts a missing duration last, then duration desc, created desc, id desc", () => {
|
|
const a = run({ id: 1, created_at: "2026-10-19T10:00:00Z", finished_at: "2026-10-19T10:05:00Z" }); // 300
|
|
const b = run({ id: 2, executing: true, created_at: "2026-10-19T10:00:00Z", finished_at: "" }); // null
|
|
const c = run({ id: 3, created_at: "2026-10-19T09:00:00Z", finished_at: "2026-10-19T09:10:00Z" }); // 600
|
|
const d = run({ id: 4, created_at: "2026-10-19T08:00:00Z", finished_at: "2026-10-19T08:05:00Z" }); // 300
|
|
assert.deepEqual(
|
|
sortRuns([a, b, c, d], "longest_duration").map((r) => r.id),
|
|
[3, 1, 4, 2],
|
|
);
|
|
});
|
|
|
|
test("shortest duration puts a missing duration last, then duration asc, created desc, id desc", () => {
|
|
const a = run({ id: 1, created_at: "2026-10-19T10:00:00Z", finished_at: "2026-10-19T10:05:00Z" }); // 300
|
|
const b = run({ id: 2, executing: true, created_at: "2026-10-19T10:00:00Z", finished_at: "" }); // null
|
|
const c = run({ id: 3, created_at: "2026-10-19T09:00:00Z", finished_at: "2026-10-19T09:10:00Z" }); // 600
|
|
const d = run({ id: 4, created_at: "2026-10-19T08:00:00Z", finished_at: "2026-10-19T08:05:00Z" }); // 300
|
|
assert.deepEqual(
|
|
sortRuns([a, b, c, d], "shortest_duration").map((r) => r.id),
|
|
[1, 4, 3, 2],
|
|
);
|
|
});
|
|
|
|
test("pipeline name sorts case-insensitively, then created desc, id desc", () => {
|
|
const x = run({ id: 1, name: "beta", created_at: "2026-10-19T10:00:00Z" });
|
|
const y = run({ id: 2, name: "Alpha", created_at: "2026-10-19T10:00:00Z" });
|
|
const z = run({ id: 3, name: "alpha", created_at: "2026-10-19T11:00:00Z" });
|
|
assert.deepEqual(
|
|
sortRuns([x, y, z], "pipeline_name").map((r) => r.id),
|
|
[3, 2, 1],
|
|
);
|
|
});
|
|
|
|
test("status sorts case-insensitively, then created desc, id desc", () => {
|
|
const p = run({ id: 1, status: "succeeded", created_at: "2026-10-19T10:00:00Z" });
|
|
const q = run({ id: 2, status: "failed", created_at: "2026-10-19T10:00:00Z" });
|
|
const r = run({ id: 3, status: "failed", created_at: "2026-10-19T11:00:00Z" });
|
|
assert.deepEqual(
|
|
sortRuns([p, q, r], "status").map((r) => r.id),
|
|
[3, 2, 1],
|
|
);
|
|
});
|
|
|
|
test("an unknown sort falls back to newest first", () => {
|
|
const a = run({ id: 1, created_at: "2026-10-19T10:00:00Z" });
|
|
const b = run({ id: 2, created_at: "2026-10-19T11:00:00Z" });
|
|
assert.deepEqual(
|
|
sortRuns([a, b], "not_a_sort").map((r) => r.id),
|
|
[2, 1],
|
|
);
|
|
});
|
|
|
|
test("RUN_SORTS and RUN_TIME_RANGES carry the app's exact labels", () => {
|
|
assert.deepEqual(
|
|
RUN_SORTS.map((s) => s.label),
|
|
["Newest first", "Oldest first", "Longest duration", "Shortest duration", "Pipeline name", "Status"],
|
|
);
|
|
assert.deepEqual(
|
|
RUN_TIME_RANGES.map((r) => r.label),
|
|
["Any time", "Today", "Last 7 days", "Last 30 days"],
|
|
);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Day grouping
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("dayLabel renders Today / Yesterday / d MMM yyyy / Unknown date", () => {
|
|
assert.equal(dayLabel("2026-10-19T01:00:00Z", TODAY, ZONE), "Today");
|
|
assert.equal(dayLabel("2026-10-18T23:00:00Z", TODAY, ZONE), "Yesterday");
|
|
assert.equal(dayLabel("2026-10-16T23:00:00Z", TODAY, ZONE), "16 Oct 2026");
|
|
assert.equal(dayLabel("", TODAY, ZONE), "Unknown date");
|
|
assert.equal(dayLabel("not-a-date", TODAY, ZONE), "Unknown date");
|
|
assert.equal(localDayKey("2026-10-19T01:00:00Z", ZONE), "2026-10-19");
|
|
assert.equal(localDayKey("", ZONE), null);
|
|
});
|
|
|
|
test("groupRunsByDay follows the sort order and preserves it within a day", () => {
|
|
const r1 = run({ id: 1, created_at: "2026-10-19T10:00:00Z" });
|
|
const r2 = run({ id: 2, created_at: "2026-10-19T09:00:00Z" });
|
|
const r3 = run({ id: 3, created_at: "2026-10-18T12:00:00Z" });
|
|
const r4 = run({ id: 4, created_at: "" });
|
|
const groups = groupRunsByDay([r2, r1, r4, r3], TODAY, ZONE, runComparator("newest_first"));
|
|
assert.deepEqual(
|
|
groups.map((g) => g.label),
|
|
["Today", "Yesterday", "Unknown date"],
|
|
);
|
|
assert.deepEqual(
|
|
groups[0].runs.map((r) => r.id),
|
|
[1, 2],
|
|
);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// The list's slots
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("runListEntries orders usage, the empty state, groups and the inline error", () => {
|
|
const usage = { total: 3 };
|
|
const recorded = runListEntries(usage, true, true, [], null);
|
|
assert.deepEqual(
|
|
recorded.map((e) => e.kind),
|
|
["usage", "empty"],
|
|
);
|
|
assert.equal(recorded[1].message, NO_RUNS_MESSAGE);
|
|
assert.equal(recorded[1].actionLabel, null);
|
|
|
|
const matched = runListEntries(null, false, true, [], "boom");
|
|
assert.deepEqual(
|
|
matched.map((e) => e.kind),
|
|
["empty", "error"],
|
|
);
|
|
assert.equal(matched[0].message, NO_MATCH_MESSAGE);
|
|
assert.equal(matched[0].actionLabel, "Clear filters");
|
|
|
|
const groups = [{ label: "Today", runs: [run({ id: 1 }), run({ id: 2 })] }];
|
|
const body = runListEntries(null, false, false, groups, null);
|
|
assert.deepEqual(
|
|
body.map((e) => e.kind),
|
|
["day", "run", "run"],
|
|
);
|
|
assert.equal(body[0].label, "Today");
|
|
assert.deepEqual(
|
|
body.filter((e) => e.kind === "run").map((e) => e.run.id),
|
|
[1, 2],
|
|
);
|
|
});
|