Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Merge Actions runs: summaries, attempts and re-runs, graceful cancel, log downloads, badges (actions 0009) | 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; | |
| 3 | ||
| 4 | import type { WorkflowRun } from "@g1t/contracts"; | |
| 5 | ||
| 6 | import { badgeMarkdown, badgeState, badgeSvg, badgeUrl, textWidth } from "./badge.ts"; | |
| 7 | ||
| 8 | const run = (status: WorkflowRun["status"], conclusion: WorkflowRun["conclusion"]) => ({ status, conclusion }) as WorkflowRun; | |
| 9 | ||
| 10 | test("a badge says how the newest finished run went", () => { | |
| 11 | assert.equal(badgeState([]), "no status"); | |
| 12 | assert.equal(badgeState([run("in_progress", null), run("completed", "success")]), "passing"); | |
| 13 | assert.equal(badgeState([run("completed", "failure"), run("completed", "success")]), "failing"); | |
| 14 | assert.equal(badgeState([run("completed", "cancelled")]), "cancelled"); | |
| 15 | assert.equal(badgeState([run("completed", "skipped")]), "passing"); | |
| 16 | assert.equal(badgeState([run("queued", null)]), "no status"); | |
| 17 | }); | |
| 18 | ||
| 19 | test("the badge is an SVG with its label and state, escaped", () => { | |
| 20 | const svg = badgeSvg("CI <main>", "passing"); | |
| 21 | assert.match(svg, /^<svg xmlns="http:\/\/www.w3.org\/2000\/svg"/); | |
| 22 | assert.match(svg, /<title>CI <main>: passing<\/title>/); | |
| 23 | assert.match(svg, /fill="#2ea043"/); | |
| 24 | assert.doesNotMatch(svg, /<main>/); | |
| 25 | assert.ok(textWidth("failing") > textWidth("ill")); | |
| 26 | assert.match(badgeSvg("x".repeat(80), "failing"), /x{59}…/); | |
| 27 | }); | |
| 28 | ||
| 29 | test("its address and Markdown name the workflow file, and a branch or event when chosen", () => { | |
| 30 | assert.equal(badgeUrl("https://g1t.sh", "acme/web", "ci.yml"), "https://g1t.sh/acme/web/actions/workflows/ci.yml/badge.svg"); | |
| 31 | assert.equal( | |
| 32 | badgeUrl("https://g1t.sh", "acme/web", "ci.yml", { branch: "release/1.x", event: "push" }), | |
| 33 | "https://g1t.sh/acme/web/actions/workflows/ci.yml/badge.svg?branch=release%2F1.x&event=push", | |
| 34 | ); | |
| 35 | assert.equal( | |
| 36 | badgeMarkdown("https://g1t.sh", "acme/web", { name: "CI [main]", file: "ci.yml" }), | |
| 37 | "[](https://g1t.sh/acme/web/actions?workflow=ci.yml)", | |
| 38 | ); | |
| 39 | }); |
This file's history is long; its oldest lines are credited to the oldest commit read.