| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import type { BranchDrifts, Commit } from "@g1t/contracts"; |
| 5 | |
| 6 | import { activeBranches, branchesToRead } from "./branches.ts"; |
| 7 | |
| 8 | const commit = (hash: string, at: string, message = `${hash}\n\nbody`): Commit => ({ |
| 9 | hash, |
| 10 | treeHash: `t${hash}`, |
| 11 | message, |
| 12 | author: { name: "Ada", email: "ada@example.com" }, |
| 13 | parents: [], |
| 14 | authoredAt: at, |
| 15 | }); |
| 16 | |
| 17 | const branches = [ |
| 18 | { name: "main", hash: "m3" }, |
| 19 | { name: "old", hash: "o1" }, |
| 20 | { name: "fix", hash: "f2" }, |
| 21 | { name: "idea", hash: "i1" }, |
| 22 | ]; |
| 23 | const pulls = [{ branch: "fix", number: 7, title: "Fix it", checkStatus: "passed" as const, status: "open" as const }]; |
| 24 | |
| 25 | test("branches with an open pull request are read first, the default branch never", () => { |
| 26 | const read = branchesToRead({ defaultBranch: "main", branches, pulls }, 2); |
| 27 | assert.deepEqual(read.reading.map((b) => b.name), ["fix", "old"]); |
| 28 | assert.equal(read.total, 3); |
| 29 | assert.equal(read.mainHead, "m3"); |
| 30 | }); |
| 31 | |
| 32 | test("no default branch head, nothing to measure against", () => { |
| 33 | assert.equal(branchesToRead({ defaultBranch: "trunk", branches, pulls: [] }, 10).mainHead, null); |
| 34 | }); |
| 35 | |
| 36 | test("each branch gets its measured commit and drift, newest first, by head hash", () => { |
| 37 | const measured: BranchDrifts = { |
| 38 | base: commit("m3", "2026-10-08T00:00:00Z"), |
| 39 | branches: [ |
| 40 | { head: "f2", commit: commit("f2", "2026-10-07T00:00:00Z"), drift: { ahead: 2, behind: 1 } }, |
| 41 | { head: "o1", commit: commit("o1", "2026-01-01T00:00:00Z"), drift: null }, |
| 42 | { head: "i1", commit: commit("i1", "2026-10-08T00:00:00Z", "one line"), drift: { ahead: 1, behind: 0 } }, |
| 43 | ], |
| 44 | }; |
| 45 | const { reading } = branchesToRead({ defaultBranch: "main", branches, pulls }, 10); |
| 46 | const shown = activeBranches(reading, measured, { pulls, previews: [{ number: 7, url: "https://fix.g1t.page" }] }); |
| 47 | assert.deepEqual(shown.map((b) => b.name), ["idea", "fix", "old"]); |
| 48 | assert.deepEqual(shown[1], { |
| 49 | name: "fix", |
| 50 | commit: { hash: "f2", message: "f2", author: "Ada", at: "2026-10-07T00:00:00Z" }, |
| 51 | drift: { ahead: 2, behind: 1 }, |
| 52 | pull: { number: 7, title: "Fix it", checkStatus: "passed", draft: false }, |
| 53 | preview: "https://fix.g1t.page", |
| 54 | }); |
| 55 | assert.equal(shown[2]?.drift, null); |
| 56 | }); |
| 57 | |
| 58 | test("when repos could not answer, the branches still show, without commits or counts", () => { |
| 59 | const { reading } = branchesToRead({ defaultBranch: "main", branches, pulls: [] }, 10); |
| 60 | const shown = activeBranches(reading, null, { pulls: [], previews: [] }); |
| 61 | assert.equal(shown.length, 3); |
| 62 | assert.ok(shown.every((b) => b.commit == null && b.drift == null && b.pull == null)); |
| 63 | }); |