| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import type { CheckItem } from "@g1t/contracts"; |
| 5 | |
| 6 | import { checkDetail, checksHeadline, checksTally, detailsLink, took } from "./commit-checks.ts"; |
| 7 | |
| 8 | const item = (over: Partial<CheckItem>): CheckItem => ({ |
| 9 | kind: "check_run", |
| 10 | id: "cr_1", |
| 11 | name: "lint", |
| 12 | state: "success", |
| 13 | description: null, |
| 14 | detailsUrl: null, |
| 15 | url: null, |
| 16 | app: null, |
| 17 | startedAt: null, |
| 18 | completedAt: null, |
| 19 | ...over, |
| 20 | }); |
| 21 | |
| 22 | test("the headline says the worst of a commit's checks", () => { |
| 23 | assert.equal(checksHeadline({ state: "success" }), "All checks have passed"); |
| 24 | assert.equal(checksHeadline({ state: "failure" }), "Some checks were not successful"); |
| 25 | assert.equal(checksHeadline({ state: "pending" }), "Some checks haven't completed yet"); |
| 26 | }); |
| 27 | |
| 28 | test("the tally counts each kind, worst first", () => { |
| 29 | assert.equal(checksTally({ total: 2, successful: 2, failed: 0, pending: 0, skipped: 0 }), "2 successful checks"); |
| 30 | assert.equal(checksTally({ total: 1, successful: 1, failed: 0, pending: 0, skipped: 0 }), "1 successful check"); |
| 31 | assert.equal( |
| 32 | checksTally({ total: 4, successful: 2, failed: 1, pending: 1, skipped: 0 }), |
| 33 | "1 failing, 1 in progress and 2 successful checks", |
| 34 | ); |
| 35 | }); |
| 36 | |
| 37 | test("a check run says how it went and how long it took", () => { |
| 38 | const started = "2026-10-07T14:00:00Z"; |
| 39 | assert.equal(took(started, "2026-10-07T14:00:25Z"), "25s"); |
| 40 | assert.equal(took(started, "2026-10-07T14:01:12Z"), "1m 12s"); |
| 41 | assert.equal(took(started, null), ""); |
| 42 | assert.equal(checkDetail(item({ startedAt: started, completedAt: "2026-10-07T14:00:25Z" })), "Successful in 25s"); |
| 43 | assert.equal( |
| 44 | checkDetail(item({ state: "failure", startedAt: started, completedAt: "2026-10-07T14:00:12Z", description: "2 problems" })), |
| 45 | "Failing after 12s — 2 problems", |
| 46 | ); |
| 47 | assert.equal(checkDetail(item({ state: "pending" })), "Queued"); |
| 48 | assert.equal(checkDetail(item({ state: "pending", startedAt: started })), "In progress"); |
| 49 | assert.equal(checkDetail(item({ kind: "status", description: "Deployment has completed" })), "Deployment has completed"); |
| 50 | assert.equal(checkDetail(item({ kind: "status", state: "skipped" })), "Skipped"); |
| 51 | }); |
| 52 | |
| 53 | test("details go to g1t's own page first, and elsewhere apart", () => { |
| 54 | assert.deepEqual(detailsLink(item({ url: "/acme/web/checks/cr_1", detailsUrl: "https://ci.example.com/1" })), { |
| 55 | href: "/acme/web/checks/cr_1", |
| 56 | external: false, |
| 57 | }); |
| 58 | assert.deepEqual(detailsLink(item({ detailsUrl: "https://g1t.sh/acme/web/actions/runs/run_1" })), { |
| 59 | href: "/acme/web/actions/runs/run_1", |
| 60 | external: false, |
| 61 | }); |
| 62 | assert.deepEqual(detailsLink(item({ detailsUrl: "https://vercel.com/acme/web/1" })), { href: "https://vercel.com/acme/web/1", external: true }); |
| 63 | assert.equal(detailsLink(item({ detailsUrl: "javascript:alert(1)" })), null); |
| 64 | assert.equal(detailsLink(item({})), null); |
| 65 | }); |