// Unit tests for the pure pipeline helpers (src/pipelines.js): timestamp and // duration formatting, the last-run join, the sort comparators, the filter and // the empty-state strings. No DOM and no server: `node --test test/` proves the // module is pure. Wording is asserted against the Android app's own strings. import { test } from "node:test"; import assert from "node:assert/strict"; import { PIPELINE_DETAIL_TABS, PIPELINE_SORTS, RUN_PRESENCES, RUN_STATUS_FILTERS, durationSeconds, filterPipelines, formatDuration, formatLocalTimestamp, formatRanAgo, graphNote, isMoreRecent, latestRunByPipeline, parseInstant, pipelineComparator, pipelineFilterCount, pipelineListEmptyState, pipelineMatchesLastRunStatus, pipelineMatchesName, pipelineMatchesPresence, pipelineSubtitle, pipelineUpdatedLine, runBadge, runBadgeMetaLine, runDurationText, runStateOf, sortPipelines, stageCountText, summaryFields, versionRow, } from "../src/pipelines.js"; const LOCAL_STAMP = /^\d{1,2} [A-Z][a-z]{2} \d{2}:\d{2}$/; // --------------------------------------------------------------------------- // Timestamps, durations and stage counts // --------------------------------------------------------------------------- test("stageCountText spells 1 singular and everything else plural", () => { assert.equal(stageCountText(0), "0 stages"); assert.equal(stageCountText(1), "1 stage"); assert.equal(stageCountText(2), "2 stages"); assert.equal(stageCountText(5), "5 stages"); }); test("parseInstant accepts RFC3339 and rejects blank/unparseable input", () => { assert.equal(parseInstant("2026-10-17T21:57:31Z"), Date.parse("2026-10-17T21:57:31Z")); assert.equal(parseInstant(""), null); assert.equal(parseInstant(" "), null); assert.equal(parseInstant("not a date"), null); assert.equal(parseInstant(undefined), null); }); test("formatLocalTimestamp renders 'd MMM HH:mm' and null on bad input", () => { assert.equal(formatLocalTimestamp("2026-10-17T21:57:31Z", { timeZone: "UTC" }), "17 Oct 21:57"); assert.equal(formatLocalTimestamp("2026-01-05T00:00:00Z", { timeZone: "UTC" }), "5 Jan 00:00"); assert.equal(formatLocalTimestamp("2026-10-17T21:57:31Z", { timeZone: "Europe/London" }), "17 Oct 22:57"); assert.equal(formatLocalTimestamp("", { timeZone: "UTC" }), null); assert.equal(formatLocalTimestamp("nope", { timeZone: "UTC" }), null); }); test("formatDuration matches the app's s / m s / h m rhythm", () => { assert.equal(formatDuration(0), "0s"); assert.equal(formatDuration(45), "45s"); assert.equal(formatDuration(59), "59s"); assert.equal(formatDuration(60), "1m 0s"); assert.equal(formatDuration(125), "2m 5s"); assert.equal(formatDuration(3599), "59m 59s"); assert.equal(formatDuration(3600), "1h 0m"); assert.equal(formatDuration(3725), "1h 2m"); assert.equal(formatDuration(-5), "0s"); }); test("durationSeconds is null when the end precedes the start or a timestamp is bad", () => { assert.equal(durationSeconds("2026-10-17T21:00:00Z", "2026-10-17T21:02:05Z"), 125); assert.equal(durationSeconds("2026-10-17T21:02:05Z", "2026-10-17T21:00:00Z"), null); assert.equal(durationSeconds("", "2026-10-17T21:00:00Z"), null); assert.equal(durationSeconds("2026-10-17T21:00:00Z", "nope"), null); }); test("runDurationText is live while executing and stored once finished", () => { const start = "2026-10-17T21:00:00Z"; const now = Date.parse(start) + 100_000; assert.equal(runDurationText({ created_at: start, finished_at: "", executing: true }, now), "1m 40s"); assert.equal( runDurationText({ created_at: start, finished_at: "2026-10-17T21:02:05Z", executing: false }, now), "2m 5s", ); assert.equal(runDurationText({ created_at: "", finished_at: "", executing: true }, now), null); assert.equal(runDurationText({ created_at: start, finished_at: "", executing: false }, now), null); }); test("a running run with a future start reads as 0s, never negative", () => { const start = "2026-10-17T21:00:00Z"; const now = Date.parse(start) - 5_000; assert.equal(runDurationText({ created_at: start, finished_at: "", executing: true }, now), "0s"); }); test("formatRanAgo buckets by minute/hour/day and nulls a bad start", () => { const start = Date.parse("2026-10-17T21:00:00Z"); const ago = (seconds) => formatRanAgo("2026-10-17T21:00:00Z", start + seconds * 1000); assert.equal(ago(0), "just now"); assert.equal(ago(59), "just now"); assert.equal(ago(60), "1m ago"); assert.equal(ago(90), "1m ago"); assert.equal(ago(3600), "1h ago"); assert.equal(ago(7200), "2h ago"); assert.equal(ago(86400), "1d ago"); assert.equal(ago(3 * 86400), "3d ago"); assert.equal(formatRanAgo("2026-10-17T21:00:00Z", start - 10_000), "just now"); assert.equal(formatRanAgo("", start), null); assert.equal(formatRanAgo("nope", start), null); }); // --------------------------------------------------------------------------- // Run states and the last-run join // --------------------------------------------------------------------------- test("runStateOf accepts the server vocabulary and falls back to unknown", () => { assert.equal(runStateOf("succeeded"), "succeeded"); assert.equal(runStateOf("RUNNING"), "running"); assert.equal(runStateOf("success"), "succeeded"); assert.equal(runStateOf("error"), "failed"); assert.equal(runStateOf("cancelled"), "canceled"); assert.equal(runStateOf("interrupted"), "interrupted"); assert.equal(runStateOf(""), "unknown"); assert.equal(runStateOf(undefined), "unknown"); }); test("latestRunByPipeline picks the newest run per pipeline independent of input order", () => { const runs = [ { id: 1, pipeline_id: 7, created_at: "2026-10-01T00:00:00Z" }, { id: 3, pipeline_id: 7, created_at: "2026-10-03T00:00:00Z" }, { id: 2, pipeline_id: 7, created_at: "2026-10-02T00:00:00Z" }, { id: 5, pipeline_id: 9, created_at: "2026-10-01T00:00:00Z" }, ]; const forward = latestRunByPipeline(runs); const backward = latestRunByPipeline([...runs].reverse()); assert.equal(forward.get(7).id, 3); assert.equal(backward.get(7).id, 3); assert.equal(forward.get(9).id, 5); assert.equal(forward.size, 2); }); test("isMoreRecent breaks an exact timestamp tie (and a bad timestamp) with the higher id", () => { const sameTime = { created_at: "2026-10-01T00:00:00Z" }; assert.equal(isMoreRecent({ id: 7, ...sameTime }, { id: 5, ...sameTime }), true); assert.equal(isMoreRecent({ id: 5, ...sameTime }, { id: 7, ...sameTime }), false); assert.equal(isMoreRecent({ id: 8, created_at: "bad" }, { id: 6, created_at: "also bad" }), true); assert.equal(isMoreRecent({ id: 6, created_at: "bad" }, { id: 8, created_at: "bad" }), false); }); test("runBadgeMetaLine omits missing fragments but always prints the run id", () => { assert.equal(runBadgeMetaLine({ ranAgo: "2h ago", duration: "1m 5s", runId: 42 }), "2h ago · 1m 5s · run 42"); assert.equal(runBadgeMetaLine({ ranAgo: null, duration: "1m 5s", runId: 42 }), "1m 5s · run 42"); assert.equal(runBadgeMetaLine({ ranAgo: "2h ago", duration: null, runId: 42 }), "2h ago · run 42"); assert.equal(runBadgeMetaLine({ ranAgo: null, duration: null, runId: 42 }), "run 42"); assert.equal(runBadgeMetaLine({ ranAgo: "", duration: "", runId: 7 }), "run 7"); }); test("runBadge bundles the state, age, duration and run id", () => { const start = "2026-10-17T21:00:00Z"; const badge = runBadge( { id: 12, status: "failed", created_at: start, finished_at: "2026-10-17T21:02:05Z", executing: false }, Date.parse(start) + 3600_000, ); assert.deepEqual(badge, { runId: 12, state: "failed", ranAgo: "1h ago", duration: "2m 5s" }); }); // --------------------------------------------------------------------------- // Sorting // --------------------------------------------------------------------------- const P = (id, name, stages) => ({ id, name, stages, version: 1 }); function ids(list) { return list.map((p) => p.id); } test("the sort list matches the app's labels with Server order default", () => { assert.deepEqual( PIPELINE_SORTS.map((option) => option.label), ["Server order", "Name A\u2013Z", "Name Z\u2013A", "Most recent run", "Fewest stages", "Most stages"], ); assert.equal(PIPELINE_SORTS[0].key, "server_order"); }); test("server order preserves the server's own ordering (and never mutates the input)", () => { const pipelines = [P(3, "beta", 2), P(1, "Alpha", 3), P(2, "alpha", 1), P(4, "Gamma", 1)]; assert.deepEqual(ids(sortPipelines(pipelines, "server_order")), [3, 1, 2, 4]); assert.deepEqual(ids(pipelines), [3, 1, 2, 4], "input list is untouched"); }); test("name comparators ignore case and tie-break on the exact name then the id", () => { const pipelines = [P(3, "beta", 2), P(1, "Alpha", 3), P(2, "alpha", 1), P(4, "Gamma", 1)]; assert.deepEqual(ids(sortPipelines(pipelines, "name_asc")), [1, 2, 3, 4]); assert.deepEqual(ids(sortPipelines(pipelines, "name_desc")), [4, 3, 1, 2]); }); test("stage-count comparators tie-break on the name then the id", () => { const pipelines = [P(3, "beta", 2), P(1, "Alpha", 3), P(2, "alpha", 1), P(4, "Gamma", 1)]; assert.deepEqual(ids(sortPipelines(pipelines, "fewest_stages")), [2, 4, 3, 1]); assert.deepEqual(ids(sortPipelines(pipelines, "most_stages")), [1, 3, 2, 4]); }); test("most-recent-run puts no-run pipelines last and orders by run recency then run id", () => { const pipelines = [P(1, "One", 1), P(2, "Two", 1), P(3, "Three", 1), P(4, "Four", 1)]; const latestRuns = new Map([ [1, { id: 11, pipeline_id: 1, created_at: "2026-10-03T00:00:00Z" }], [3, { id: 10, pipeline_id: 3, created_at: "2026-10-01T00:00:00Z" }], [4, { id: 9, pipeline_id: 4, created_at: "not a date" }], ]); assert.deepEqual(ids(sortPipelines(pipelines, "recent_run", latestRuns)), [1, 3, 4, 2]); }); test("most-recent-run breaks an exact timestamp tie with the higher run id", () => { const pipelines = [P(1, "One", 1), P(2, "Two", 1)]; const at = "2026-10-01T00:00:00Z"; const latestRuns = new Map([ [1, { id: 4, pipeline_id: 1, created_at: at }], [2, { id: 9, pipeline_id: 2, created_at: at }], ]); assert.deepEqual(ids(sortPipelines(pipelines, "recent_run", latestRuns)), [2, 1]); }); test("every comparator ends in a deterministic id tie-break", () => { // Identical names and stage counts: only the id can decide, whatever the input order. const pipelines = [P(5, "same", 2), P(2, "same", 2), P(9, "same", 2)]; assert.deepEqual(ids(sortPipelines(pipelines, "name_asc")), [2, 5, 9]); assert.deepEqual(ids(sortPipelines([...pipelines].reverse(), "name_asc")), [2, 5, 9]); assert.deepEqual(ids(sortPipelines(pipelines, "fewest_stages")), [2, 5, 9]); assert.equal(typeof pipelineComparator("server_order"), "function"); }); // --------------------------------------------------------------------------- // Filtering and empty states // --------------------------------------------------------------------------- test("the status filter vocabulary is exactly the server's seven run states", () => { assert.deepEqual(RUN_STATUS_FILTERS, [ "queued", "running", "waiting", "succeeded", "failed", "canceled", "interrupted", ]); assert.deepEqual( RUN_PRESENCES.map((p) => p.label), ["Any", "Has runs", "No runs"], ); }); const FILTER_PIPELINES = [P(1, "Android", 3), P(2, "Beta", 2), P(3, "Gamma", 1)]; const FILTER_RUNS = new Map([ [1, { id: 1, pipeline_id: 1, status: "succeeded" }], [2, { id: 2, pipeline_id: 2, status: "failed" }], ]); const FILTERS = (overrides = {}) => ({ presence: "any", lastRunStatuses: [], name: "", ...overrides }); test("the name facet narrows case-insensitively and needs no run history", () => { assert.ok(pipelineMatchesName("", "anything")); assert.ok(pipelineMatchesName("and", "Android")); assert.ok(!pipelineMatchesName("zzz", "Android")); assert.deepEqual( filterPipelines(FILTER_PIPELINES, FILTERS({ name: "and" }), FILTER_RUNS, false).map((p) => p.id), [1], ); }); test("run facets AND together and a no-run pipeline never matches a selected status", () => { assert.ok(pipelineMatchesPresence("any", false)); assert.ok(pipelineMatchesPresence("has_runs", true)); assert.ok(!pipelineMatchesPresence("has_runs", false)); assert.ok(pipelineMatchesPresence("no_runs", false)); assert.ok(pipelineMatchesLastRunStatus(null, [])); assert.ok(!pipelineMatchesLastRunStatus(null, ["failed"])); assert.deepEqual( filterPipelines(FILTER_PIPELINES, FILTERS({ presence: "has_runs" }), FILTER_RUNS, true).map((p) => p.id), [1, 2], ); assert.deepEqual( filterPipelines(FILTER_PIPELINES, FILTERS({ presence: "no_runs" }), FILTER_RUNS, true).map((p) => p.id), [3], ); assert.deepEqual( filterPipelines(FILTER_PIPELINES, FILTERS({ lastRunStatuses: ["failed"] }), FILTER_RUNS, true).map((p) => p.id), [2], ); }); test("a run facet is skipped while the run history is unknown, and the name still applies", () => { assert.deepEqual( filterPipelines(FILTER_PIPELINES, FILTERS({ presence: "has_runs" }), FILTER_RUNS, false).map((p) => p.id), [1, 2, 3], ); assert.deepEqual( filterPipelines(FILTER_PIPELINES, FILTERS({ presence: "has_runs", name: "Beta" }), FILTER_RUNS, false).map((p) => p.id), [2], ); }); test("pipelineFilterCount counts one per active facet", () => { assert.equal(pipelineFilterCount(FILTERS()), 0); assert.equal(pipelineFilterCount(FILTERS({ name: "a" })), 1); assert.equal(pipelineFilterCount(FILTERS({ presence: "has_runs", lastRunStatuses: ["failed"], name: "a" })), 3); }); test("empty states use the app's three exact sentences and only offer a reset when pipelines exist", () => { assert.deepEqual(pipelineListEmptyState([], [], FILTERS()), { message: "No pipelines are defined on this server.", clearAction: null, }); assert.deepEqual(pipelineListEmptyState(FILTER_PIPELINES, [], FILTERS({ name: "zzz" })), { message: 'No pipelines match "zzz".', clearAction: "Clear filters", }); assert.deepEqual(pipelineListEmptyState(FILTER_PIPELINES, [], FILTERS({ presence: "has_runs" })), { message: "No pipelines match the current filters.", clearAction: "Clear filters", }); // A name narrowing combined with a run facet is no longer name-only. assert.equal( pipelineListEmptyState(FILTER_PIPELINES, [], FILTERS({ name: "zzz", presence: "has_runs" })).message, "No pipelines match the current filters.", ); assert.equal(pipelineListEmptyState(FILTER_PIPELINES, FILTER_PIPELINES, FILTERS()), null); }); // --------------------------------------------------------------------------- // List row and detail text // --------------------------------------------------------------------------- test("pipelineSubtitle is 'id {id} · v{version} · {N stages}' with a singular stage", () => { assert.equal(pipelineSubtitle({ id: 20, version: 10, stages: 5 }), "id 20 · v10 · 5 stages"); assert.equal(pipelineSubtitle({ id: 3, version: 1, stages: 1 }), "id 3 · v1 · 1 stage"); assert.equal(pipelineSubtitle({ id: 4, version: 2, graph: { stages: [{}, {}] } }), "id 4 · v2 · 2 stages"); }); test("pipelineUpdatedLine is 'updated d MMM HH:mm' or null for a bad timestamp", () => { const line = pipelineUpdatedLine({ updated_at: "2026-10-17T21:57:31Z" }); assert.ok(LOCAL_STAMP.test(line.replace(/^updated /, "")), line); assert.equal(pipelineUpdatedLine({ updated_at: "" }), null); assert.equal(pipelineUpdatedLine({ updated_at: "nope" }), null); }); test("the detail tabs are Summary / Graph / Source in order", () => { assert.deepEqual(PIPELINE_DETAIL_TABS.map((tab) => tab.label), ["Summary", "Graph", "Source"]); }); test("summaryFields render the app's rows; latest version is v{n} or blank, never a dash", () => { const fields = summaryFields({ content_hash: "abc123", latest_version: 10, version_created_at: "2026-10-18T23:41:08Z", created_at: "2026-10-17T21:57:31Z", updated_at: "2026-10-18T23:41:08Z", }); assert.deepEqual(fields.map((f) => f.key), [ "Content hash", "Latest version", "Version created", "Pipeline created", "Pipeline updated", ]); assert.equal(fields[0].value, "abc123"); assert.equal(fields[0].monospace, true); assert.equal(fields[1].value, "v10"); for (const field of fields.slice(2)) assert.ok(LOCAL_STAMP.test(field.value), field.value); const blank = summaryFields({ content_hash: "", latest_version: 0 }); assert.equal(blank[1].value, ""); assert.equal(blank[2].value, ""); }); test("versionRow shows v{n} plus its stage count and created time", () => { const row = versionRow({ version: 2, stages: 1, created_at: "2026-10-18T19:04:13Z" }); assert.equal(row.versionLabel, "v2"); assert.equal(row.stagesText, "1 stage"); assert.ok(LOCAL_STAMP.test(row.createdText)); assert.equal(versionRow({ version: 3, stages: 4 }).createdText, null); }); test("the graph note is the browser wording and leaves the graph to P6", () => { const note = graphNote(); assert.match(note, /Mouse wheel to zoom, drag to pan, click a stage for details/); assert.match(note, /Edges are labelled with their outcome/); });