// 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, isSystemArtifact, stageArtifacts, systemArtifacts, visitArtifacts, ARTIFACT_CAPTURE_PENDING, LEGACY_SESSION_MARKDOWN_ARTIFACT, artifactCaptureNotes, artifactGroups, artifactKind, artifactKindLabel, artifactsEmptyMessage, artifactsHeader, artifactsSummary, artifactViewerPositionLabel, displayVisit, humanBytes, isMarkdownArtifact, isViewerArtifact, 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("the turn's own files are system evidence, not stage artifacts", () => { const step = { artifacts: [ { name: "input.md", url: "/1" }, { name: "memory.md", url: "/2" }, { name: "output.md", url: "/3" }, { name: "session.html", url: "/4" }, { name: "public/notes.md", url: "/5" }, { name: "screenshot.png", url: "/6" }, ], }; assert.deepEqual( stageArtifacts(step).map((artifact) => artifact.name), ["public/notes.md", "screenshot.png"], ); assert.deepEqual( systemArtifacts(step).map((artifact) => artifact.name), ["input.md", "memory.md", "output.md", "session.html"], ); assert.equal(isSystemArtifact({ name: "output.md" }), true); assert.equal(isSystemArtifact({ name: "public/input.md" }), false); }); 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("a shared public file is attributed to the visit that first recorded it", () => { const steps = [ { sequence: 1, stage: "a", artifacts: [{ name: "input.md" }, { name: "public/a.txt" }] }, { sequence: 2, stage: "b", artifacts: [{ name: "output.md" }, { name: "public/a.txt" }, { name: "public/b.txt" }], }, ]; const groups = artifactGroups(steps); assert.deepEqual(groups.map((g) => g.artifacts.map((a) => a.name)), [["public/a.txt"], ["public/b.txt"]]); assert.equal( visitArtifacts(steps[1], steps).map((a) => a.name).join(","), "public/b.txt", ); assert.equal(artifactsHeader(steps, false), "Artifacts · 2 recorded"); }); 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("isMarkdownArtifact accepts markdown/text content types and md/txt names", () => { for (const [contentType, name] of [ ["text/markdown", "plan"], ["text/markdown; charset=utf-8", "plan"], ["text/plain", "notes"], ["application/octet-stream", "input.md"], ["", "memory.markdown"], ["", "output.txt"], ["text/html", "report.md"], ["text/plain", "run.log"], ]) { assert.equal(isMarkdownArtifact(contentType, name), true, `${contentType} ${name}`); } for (const [contentType, name] of [ ["text/html", "session.html"], ["text/csv", "rows.csv"], ["application/json", "payload.json"], ["text/x-python", "main.py"], ["application/xml", "shot.png.xml"], ["", ""], [undefined, undefined], ]) { assert.equal(isMarkdownArtifact(contentType, name), false, `${contentType} ${name}`); } }); test("isViewerArtifact includes media and markdown but not code, logs or other files", () => { for (const [contentType, name] of [ ["image/png", "shot.png"], ["video/mp4", "clip.mp4"], ["text/markdown", "input.md"], ["text/plain", "memory.txt"], ["", "output.md"], ]) { assert.equal(isViewerArtifact(contentType, name), true, `${contentType} ${name}`); } for (const [contentType, name] of [ ["text/html", "session.html"], ["text/csv", "rows.csv"], ["application/json", "payload.json"], ["text/x-python", "main.py"], ["text/plain", "run.log"], ["application/octet-stream", "blob.bin"], ["", ""], ]) { assert.equal(isViewerArtifact(contentType, name), false, `${contentType} ${name}`); } }); test("pagedArtifacts keeps images, videos and markdown in gallery order", () => { const groups = [ { stageKey: "reviewer", visit: 1, artifacts: [art("session.html", { contentType: "text/html" })] }, { stageKey: "implementer", visit: 2, artifacts: [ art("input.md", { contentType: "text/markdown" }), art("run.log", { contentType: "text/plain" }), art("shot.png", { contentType: "image/png" }), art("main.py", { contentType: "text/x-python" }), art("clip.mp4", { contentType: "video/mp4" }), art("memory.txt", { contentType: "text/plain" }), art("rows.csv", { contentType: "text/csv" }), art("output.md", { contentType: "application/octet-stream" }), ], }, ]; assert.deepEqual(pagedArtifacts(groups).map((a) => a.name), [ "input.md", "shot.png", "clip.mp4", "memory.txt", "output.md", ]); }); test("viewerIndexByUrl maps each viewer-selectable url, 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" }), art("session.html", { contentType: "text/html", url: "/artifacts/1/1/session.html" }), ], }, ]; const index = viewerIndexByUrl(groups); assert.equal(index.get("/artifacts/1/1/a.png"), 0); assert.equal(index.get("/artifacts/1/1/note.txt"), 1); assert.equal(index.get("/artifacts/1/1/b.png"), 2); assert.equal(index.has("/artifacts/1/1/session.html"), 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); });