Files
pipelines/web/test/artifacts.test.mjs
T
alan 67120947c2 web: artifacts gallery, full-screen viewer and session export link (R8/R9)
Port the Android artifact derivation into a pure, DOM-free src/artifacts.js
(presentedArtifacts with the retired session.md hidden everywhere, grouping by
stage/visit, the header/summary/capture-note wording, kind and size formatting,
viewer paging and sessionExportArtifact) and unit-test it.

Wire it into src/app.js: a run-level artifacts gallery with per-(stage,visit)
groups, per-visit summaries and tiles, a full-screen viewer overlay (paging,
Escape/backdrop close) and the read-only session.html link. Every server string
is set with textContent; captures/ stays untracked.

Captured live against run 430 through test/ui-smoke.mjs:
captures/04-run-430-artifacts.png, 05-artifact-viewer.png,
06-session-export-link.png. PARITY.md marks R8 and R9 done.
2026-10-19 02:38:48 +01:00

298 lines
13 KiB
JavaScript

// Unit tests for the ported artifact derivation, run with `node --test`.
// Mirrors the cases in android/.../test/.../ArtifactGroupsTest.kt and the
// wording/formatting assertions around Format.kt, graph/ArtifactType.kt,
// graph/ArtifactViewer.kt and graph/SessionExport.kt.
import { test } from "node:test";
import assert from "node:assert/strict";
import {
ArtifactKind,
ARTIFACT_CAPTURE_PENDING,
LEGACY_SESSION_MARKDOWN_ARTIFACT,
artifactCaptureNotes,
artifactGroups,
artifactKind,
artifactKindLabel,
artifactsEmptyMessage,
artifactsHeader,
artifactsSummary,
artifactViewerPositionLabel,
displayVisit,
humanBytes,
pagedArtifacts,
pagedIndex,
presentedArtifacts,
sessionExportArtifact,
viewerIndexByUrl,
} from "../src/artifacts.js";
/** A minimal StageArtifact, named so tests read like the Android fixtures. */
function art(name, { contentType = "", sizeBytes = 0, url } = {}) {
return { name, content_type: contentType, size_bytes: sizeBytes, url: url ?? `/artifacts/1/1/${name}` };
}
/** A minimal RunStep with a recorded control visit and optional artifacts. */
function step(stage, sequence, { visit = 1, status = "completed", artifacts, skipped, truncated, exportHtml } = {}) {
const s = { stage, sequence, status, control: { visit, max_visits: 3 } };
if (artifacts !== undefined) s.artifacts = artifacts;
if (skipped !== undefined) s.artifacts_skipped = skipped;
if (truncated !== undefined) s.artifacts_truncated = truncated;
if (exportHtml !== undefined) s.session_export = { html: exportHtml };
return s;
}
test("presentedArtifacts hides the retired session.md and keeps everything else", () => {
const artifacts = [art("shot.png"), art(LEGACY_SESSION_MARKDOWN_ARTIFACT), art("session.html")];
const presented = presentedArtifacts({ artifacts });
assert.deepEqual(presented.map((a) => a.name), ["shot.png", "session.html"]);
assert.deepEqual(presentedArtifacts({}), []);
assert.deepEqual(presentedArtifacts({ artifacts: [] }), []);
assert.deepEqual(presentedArtifacts(undefined), []);
});
test("session.md is hidden from every listing: groups, header and summary", () => {
const onlyLegacy = [step("planner", 1, { artifacts: [art(LEGACY_SESSION_MARKDOWN_ARTIFACT)] })];
assert.deepEqual(artifactGroups(onlyLegacy), []);
assert.equal(artifactsHeader(onlyLegacy, false), "No artifacts recorded for this run.");
assert.equal(artifactsSummary(onlyLegacy[0]), "Artifacts · not recorded for this visit");
assert.deepEqual(artifactCaptureNotes(onlyLegacy[0]), []);
});
test("groups interleaved steps by stage then visit: [planner·1, planner·2, build·1]", () => {
const steps = [
step("planner", 1, { visit: 1, artifacts: [art("a.png")] }),
step("build", 2, { visit: 1, artifacts: [art("b.png")] }),
step("planner", 3, { visit: 2, artifacts: [art("c.png")] }),
];
const groups = artifactGroups(steps);
assert.deepEqual(
groups.map((g) => `${g.stageKey}·${g.visit}`),
["planner·1", "planner·2", "build·1"],
);
assert.deepEqual(groups[0].artifacts.map((a) => a.name), ["a.png"]);
});
test("duplicate names are kept, not deduped", () => {
const steps = [
step("planner", 1, {
artifacts: [
art("shot.png", { url: "/artifacts/1/1/shot-a.png" }),
art("shot.png", { url: "/artifacts/1/1/shot-b.png" }),
],
}),
];
const groups = artifactGroups(steps);
assert.equal(groups.length, 1);
assert.equal(groups[0].artifacts.length, 2);
assert.deepEqual(groups[0].artifacts.map((a) => a.url), [
"/artifacts/1/1/shot-a.png",
"/artifacts/1/1/shot-b.png",
]);
});
test("empty/null artifact visits and an empty run produce no group", () => {
assert.deepEqual(artifactGroups([]), []);
assert.deepEqual(artifactGroups(undefined), []);
const steps = [
step("planner", 1, {}), // no artifacts key
step("planner", 2, { artifacts: [] }), // empty capture
step("planner", 3, { artifacts: null }),
step("build", 4, { artifacts: [art("only.png")] }),
];
const groups = artifactGroups(steps);
assert.equal(groups.length, 1);
assert.equal(groups[0].stageKey, "build");
assert.equal(groups[0].visit, 1);
});
test("display visit number is control.visit when positive, else the position + 1", () => {
assert.equal(displayVisit(step("s", 1, { visit: 2 }), 0), 2);
assert.equal(displayVisit(step("s", 1, { visit: 0 }), 0), 1);
assert.equal(displayVisit(step("s", 1, { visit: 0 }), 4), 5);
assert.equal(displayVisit({ stage: "s", sequence: 1 }, 2), 3); // no control at all
const steps = [
step("planner", 1, { visit: 0, artifacts: [art("a")] }),
step("planner", 2, { visit: 0, artifacts: [art("b")] }),
];
assert.deepEqual(artifactGroups(steps).map((g) => g.visit), [1, 2]);
});
test("the run header counts exactly the tiles the gallery yields", () => {
const steps = [
step("planner", 1, { artifacts: [art("a.png"), art(LEGACY_SESSION_MARKDOWN_ARTIFACT)] }),
step("build", 2, { artifacts: [art("b.png"), art("c.png"), art("d.png")] }),
];
const tiles = artifactGroups(steps).reduce((n, g) => n + g.artifacts.length, 0);
assert.equal(tiles, 4);
assert.equal(artifactsHeader(steps, false), `Artifacts · ${tiles} recorded`);
assert.equal(artifactsHeader(steps, false), "Artifacts · 4 recorded");
});
test("the run header is singular for exactly one artifact and flags partial capture", () => {
assert.equal(artifactsHeader([step("s", 1, { artifacts: [art("one.png")] })], false), "Artifact · 1 recorded");
assert.equal(
artifactsHeader([step("s", 1, { artifacts: [art("a.png")], skipped: 2 })], false),
"Artifact · 1 recorded · partial capture",
);
assert.equal(
artifactsHeader([step("s", 1, { artifacts: [art("a.png")], truncated: 5 })], false),
"Artifact · 1 recorded · partial capture",
);
});
test("the run header empty wording matches running vs finished", () => {
assert.equal(artifactsHeader([], true), "Artifacts · capture pending");
assert.equal(artifactsHeader([], false), "No artifacts recorded for this run.");
assert.equal(artifactsEmptyMessage(true), `Artifacts · ${ARTIFACT_CAPTURE_PENDING}`);
assert.equal(artifactsEmptyMessage(false), "No artifacts recorded for this run.");
});
test("the per-visit summary uses the exact recorded/pending/not-recorded wording", () => {
assert.equal(
artifactsSummary(step("s", 1, { status: "completed", artifacts: [art("a"), art("b"), art("c")] })),
"Artifacts · 3 recorded",
);
assert.equal(
artifactsSummary(step("s", 1, { artifacts: [art("a")], truncated: 1 })),
"Artifacts · 1 recorded · partial capture",
);
assert.equal(
artifactsSummary(step("s", 1, { status: "running", artifacts: [] })),
"Artifacts · capture pending",
);
assert.equal(
artifactsSummary(step("s", 1, { status: "completed", artifacts: [] })),
"Artifacts · not recorded for this visit",
);
assert.equal(
artifactsSummary(step("s", 1, { status: "completed" })),
"Artifacts · not recorded for this visit",
);
});
test("capture notes use the exact wording and stay silent on an empty capture", () => {
const full = step("s", 1, { artifacts: [art("a")], skipped: 4, truncated: 7 });
assert.deepEqual(artifactCaptureNotes(full), [
"4 files excluded from artifact capture.",
"7 files omitted by artifact capture limits.",
]);
assert.deepEqual(artifactCaptureNotes(step("s", 1, { artifacts: [art("a")], skipped: 2 })), [
"2 files excluded from artifact capture.",
]);
assert.deepEqual(artifactCaptureNotes(step("s", 1, { artifacts: [art("a")] })), []);
assert.deepEqual(artifactCaptureNotes(step("s", 1, { artifacts: [], skipped: 4 })), []);
});
test("artifactKind classifies images, videos and every file kind", () => {
assert.equal(artifactKind("image/png", "shot.png"), ArtifactKind.Image);
assert.equal(artifactKind("video/mp4", "clip.mp4"), ArtifactKind.Video);
assert.equal(artifactKind("audio/mpeg", "sound.mp3"), ArtifactKind.Audio);
assert.equal(artifactKind("application/pdf", "report"), ArtifactKind.Pdf);
assert.equal(artifactKind("application/octet-stream", "data.pdf"), ArtifactKind.Pdf);
assert.equal(artifactKind("application/json", "payload"), ArtifactKind.Json);
assert.equal(artifactKind("text/plain", "config.json"), ArtifactKind.Json);
assert.equal(artifactKind("application/zip", "bundle"), ArtifactKind.Archive);
assert.equal(artifactKind("text/plain", "backup.tar.gz"), ArtifactKind.Archive);
assert.equal(artifactKind("text/csv", "rows"), ArtifactKind.Spreadsheet);
assert.equal(artifactKind("application/octet-stream", "sheet.xlsx"), ArtifactKind.Spreadsheet);
assert.equal(artifactKind("text/plain", "run.log"), ArtifactKind.Log);
assert.equal(artifactKind("application/xml", "layout"), ArtifactKind.Code);
assert.equal(artifactKind("text/plain", "main.go"), ArtifactKind.Code);
assert.equal(artifactKind("text/html; charset=utf-8", "session.html"), ArtifactKind.Document);
assert.equal(artifactKind("text/plain", "notes.txt"), ArtifactKind.Document);
assert.equal(artifactKind("application/octet-stream", "blob.bin"), ArtifactKind.Other);
assert.equal(artifactKind("", ""), ArtifactKind.Other);
});
test("artifactKindLabel names every kind in prose", () => {
assert.equal(artifactKindLabel(ArtifactKind.Image), "Image");
assert.equal(artifactKindLabel(ArtifactKind.Video), "Video");
assert.equal(artifactKindLabel(ArtifactKind.Audio), "Audio");
assert.equal(artifactKindLabel(ArtifactKind.Pdf), "PDF");
assert.equal(artifactKindLabel(ArtifactKind.Json), "JSON");
assert.equal(artifactKindLabel(ArtifactKind.Archive), "Archive");
assert.equal(artifactKindLabel(ArtifactKind.Spreadsheet), "Spreadsheet");
assert.equal(artifactKindLabel(ArtifactKind.Log), "Log");
assert.equal(artifactKindLabel(ArtifactKind.Code), "Code");
assert.equal(artifactKindLabel(ArtifactKind.Document), "Document");
assert.equal(artifactKindLabel(ArtifactKind.Other), "File");
assert.equal(artifactKindLabel("nonsense"), "File");
});
test("humanBytes matches the app's binary formatting", () => {
assert.equal(humanBytes(0), "0 B");
assert.equal(humanBytes(999), "999 B");
assert.equal(humanBytes(1024), "1.0 KiB");
assert.equal(humanBytes(1536), "1.5 KiB");
assert.equal(humanBytes(2 * 1024 * 1024), "2.0 MiB");
assert.equal(humanBytes(3 * 1024 * 1024 * 1024), "3.0 GiB");
});
test("viewer position label trims the subtype and falls back to 'artifact'", () => {
assert.equal(artifactViewerPositionLabel(0, 3, "image/png"), "1 of 3 · image/png");
assert.equal(artifactViewerPositionLabel(1, 3, "image/png; charset=binary"), "2 of 3 · image/png");
assert.equal(artifactViewerPositionLabel(4, 5, ""), "5 of 5 · artifact");
assert.equal(artifactViewerPositionLabel(0, 1, " "), "1 of 1 · artifact");
});
test("pagedArtifacts keeps only images and videos, in gallery order", () => {
const groups = [
{ stageKey: "reviewer", visit: 3, artifacts: [art("session.html", { contentType: "text/html" })] },
{
stageKey: "ui_reviewer",
visit: 2,
artifacts: [
art("shot.png", { contentType: "image/png" }),
art("shot.png.xml", { contentType: "application/xml" }),
art("clip.mp4", { contentType: "video/mp4" }),
art("second.png", { contentType: "image/png" }),
],
},
];
assert.deepEqual(pagedArtifacts(groups).map((a) => a.name), ["shot.png", "clip.mp4", "second.png"]);
assert.deepEqual(pagedArtifacts([]), []);
});
test("viewerIndexByUrl maps each media url to its position, first occurrence wins", () => {
const groups = [
{
stageKey: "s",
visit: 1,
artifacts: [
art("a.png", { contentType: "image/png", url: "/artifacts/1/1/a.png" }),
art("note.txt", { contentType: "text/plain", url: "/artifacts/1/1/note.txt" }),
art("b.png", { contentType: "image/png", url: "/artifacts/1/1/b.png" }),
art("a.png", { contentType: "image/png", url: "/artifacts/1/1/a.png" }),
],
},
];
const index = viewerIndexByUrl(groups);
assert.equal(index.get("/artifacts/1/1/a.png"), 0);
assert.equal(index.get("/artifacts/1/1/b.png"), 1);
assert.equal(index.has("/artifacts/1/1/note.txt"), false);
});
test("pagedIndex clamps at both ends and never wraps", () => {
assert.equal(pagedIndex(0, -1, 3), 0);
assert.equal(pagedIndex(1, -1, 3), 0);
assert.equal(pagedIndex(1, 1, 3), 2);
assert.equal(pagedIndex(2, 1, 3), 2);
assert.equal(pagedIndex(0, 0, 0), 0);
assert.equal(pagedIndex(0, 0, 1), 0);
});
test("sessionExportArtifact matches the export html to a step artifact by name", () => {
const exportStep = step("reviewer", 1, {
artifacts: [art("shot.png"), art("session.html")],
exportHtml: "session.html",
});
assert.equal(sessionExportArtifact(exportStep).name, "session.html");
// No session_export at all.
assert.equal(sessionExportArtifact(step("s", 1, { artifacts: [art("session.html")] })), null);
// Blank html.
assert.equal(sessionExportArtifact(step("s", 1, { artifacts: [art("session.html")], exportHtml: " " })), null);
// Export named but the file is missing from the listing.
assert.equal(sessionExportArtifact(step("s", 1, { artifacts: [art("shot.png")], exportHtml: "session.html" })), null);
assert.equal(sessionExportArtifact(undefined), null);
});