| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import type { G1tEvent } from "@g1t/contracts"; |
| 5 | |
| 6 | import { activityLine, alignWeeks, contributorHref, count, languageBar, licenseLabel, peak } from "./about.ts"; |
| 7 | |
| 8 | test("a license is named by its SPDX id, or not at all", () => { |
| 9 | assert.equal(licenseLabel({ spdxId: "MIT", name: "MIT License", path: "LICENSE" }), "MIT license"); |
| 10 | assert.equal(licenseLabel({ spdxId: null, name: "Other", path: "COPYING" }), "View license"); |
| 11 | }); |
| 12 | |
| 13 | test("counts read as words", () => { |
| 14 | assert.equal(count(1, "star"), "1 star"); |
| 15 | assert.equal(count(0, "star"), "0 stars"); |
| 16 | assert.equal(count(1234, "star"), "1.2k stars"); |
| 17 | assert.equal(count(2000, "star"), "2k stars"); |
| 18 | assert.equal(count(15_400, "star"), "15k stars"); |
| 19 | assert.equal(count(3, "watching", "watching"), "3 watching"); |
| 20 | }); |
| 21 | |
| 22 | test("small languages are gathered as Other", () => { |
| 23 | const rust = { name: "Rust", color: "#dea584", bytes: 900, percent: 90 }; |
| 24 | const shell = { name: "Shell", color: "#89e051", bytes: 95, percent: 9.5 }; |
| 25 | const nix = { name: "Nix", color: "#7e7eff", bytes: 5, percent: 0.5 }; |
| 26 | assert.deepEqual(languageBar([rust, shell]), [rust, shell]); |
| 27 | const bar = languageBar([rust, shell, nix]); |
| 28 | assert.equal(bar.length, 3); |
| 29 | assert.equal(bar[2].name, "Other"); |
| 30 | assert.equal(bar[2].percent, 0.5); |
| 31 | assert.deepEqual(languageBar([rust, { ...nix, percent: 0 }]), [rust], "nothing to show for nothing"); |
| 32 | assert.deepEqual(languageBar([]), []); |
| 33 | }); |
| 34 | |
| 35 | test("only accounts lead to a profile", () => { |
| 36 | const base = { name: "ada", commits: 1, firstAt: "", lastAt: "", weeks: [] }; |
| 37 | assert.equal(contributorHref({ ...base, kind: "user", username: "ada" }), "/u/ada"); |
| 38 | assert.equal(contributorHref({ ...base, kind: "author" }), null); |
| 39 | assert.equal(contributorHref({ ...base, kind: "g1t" }), null); |
| 40 | }); |
| 41 | |
| 42 | test("weeks line up with the repository's", () => { |
| 43 | const all = [ |
| 44 | { week: "2026-09-21", commits: 2 }, |
| 45 | { week: "2026-09-28", commits: 0 }, |
| 46 | { week: "2026-10-05", commits: 5 }, |
| 47 | ]; |
| 48 | assert.deepEqual(alignWeeks(all, [{ week: "2026-10-05", commits: 3 }]), [0, 0, 3]); |
| 49 | assert.equal(peak(all), 5); |
| 50 | assert.equal(peak([]), 1); |
| 51 | }); |
| 52 | |
| 53 | function event(type: string, data: Record<string, unknown>): G1tEvent { |
| 54 | return { id: "evt_1", type, source: "repos", time: "2026-10-07T10:00:00Z", repoId: "rep_1", actor: "usr_1", data } as unknown as G1tEvent; |
| 55 | } |
| 56 | |
| 57 | test("pushes, tags, merges and renames are activity", () => { |
| 58 | assert.deepEqual(activityLine(event("git.push", { ref: "refs/heads/main", before: "a", after: "b", defaultBranch: true })), { |
| 59 | kind: "push", |
| 60 | branch: "main", |
| 61 | commit: "b", |
| 62 | before: "a", |
| 63 | created: false, |
| 64 | defaultBranch: true, |
| 65 | }); |
| 66 | assert.equal((activityLine(event("git.push", { ref: "refs/heads/feat", after: "b", defaultBranch: false })) as { created: boolean }).created, true); |
| 67 | assert.deepEqual(activityLine(event("git.push", { ref: "refs/tags/v1.0.0", after: "c", defaultBranch: false })), { kind: "tag", tag: "v1.0.0", commit: "c" }); |
| 68 | assert.equal(activityLine(event("git.push", { ref: "refs/pull/1/head", after: "c", defaultBranch: false })), null); |
| 69 | assert.deepEqual(activityLine(event("pull.merged", { number: 7, commit: "d" })), { kind: "merge", number: 7, commit: "d" }); |
| 70 | assert.deepEqual(activityLine(event("branch.renamed", { from: "master", to: "main" })), { kind: "renamed", from: "master", to: "main" }); |
| 71 | assert.equal(activityLine(event("repo.default_branch_changed", { from: "master", to: "main", renamed: true })), null); |
| 72 | assert.equal(activityLine(event("issue.opened", { number: 1 })), null); |
| 73 | }); |