Skip to content
39 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { WorkflowRun } from "@g1t/contracts";
5
6import { badgeMarkdown, badgeState, badgeSvg, badgeUrl, textWidth } from "./badge.ts";
7
8const run = (status: WorkflowRun["status"], conclusion: WorkflowRun["conclusion"]) => ({ status, conclusion }) as WorkflowRun;
9
10test("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
19test("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 &lt;main&gt;: 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
29test("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 "[![CI main](https://g1t.sh/acme/web/actions/workflows/ci.yml/badge.svg)](https://g1t.sh/acme/web/actions?workflow=ci.yml)",
38 );
39});