| 1 | import assert from "node:assert/strict"; |
| 2 | import { describe, it } from "node:test"; |
| 3 | |
| 4 | import type { RepoChange } from "@g1t/contracts"; |
| 5 | |
| 6 | import { arrivedOf, homeShows, type HomeRepo } from "./home-live.ts"; |
| 7 | |
| 8 | const repos: HomeRepo[] = [{ id: "r1", path: "acme/web", defaultBranch: "main" }]; |
| 9 | const change = (kind: string, extra: Partial<RepoChange> = {}): RepoChange => ({ repo_id: "r1", kind, at: "2026-10-15T00:00:00Z", ...extra }); |
| 10 | |
| 11 | describe("homeShows", () => { |
| 12 | it("takes a default branch's pushes and tags, not another branch's", () => { |
| 13 | assert.equal(homeShows(change("git.push", { ref: "refs/heads/main" }), repos), true); |
| 14 | assert.equal(homeShows(change("git.push", { ref: "refs/tags/v1.0.0" }), repos), true); |
| 15 | assert.equal(homeShows(change("git.push", { ref: "refs/heads/feature" }), repos), false); |
| 16 | }); |
| 17 | |
| 18 | it("takes any branch's push where the default branch is not known", () => { |
| 19 | assert.equal(homeShows({ ...change("git.push", { ref: "refs/heads/feature" }), repo_id: "other" }, repos), true); |
| 20 | }); |
| 21 | |
| 22 | it("takes what Home lists and leaves a commit's checks", () => { |
| 23 | for (const kind of ["pull.merged", "review.submitted", "issue.opened", "comment.created", "workflow.completed", "run.updated", "deployment_status.created", "release.published", "session.started", "session.ended"]) { |
| 24 | assert.equal(homeShows(change(kind), repos), true, kind); |
| 25 | } |
| 26 | for (const kind of ["status.created", "check_run.completed", "check_suite.completed", "checks.completed", "session.progressed", "git.deleted"]) { |
| 27 | assert.equal(homeShows(change(kind), repos), false, kind); |
| 28 | } |
| 29 | }); |
| 30 | }); |
| 31 | |
| 32 | describe("arrivedOf", () => { |
| 33 | it("finds nothing new on the first draw", () => { |
| 34 | const { arrived, seen } = arrivedOf(null, "last", ["a", "b"]); |
| 35 | assert.deepEqual([...arrived], []); |
| 36 | assert.deepEqual([...seen.keys], ["a", "b"]); |
| 37 | }); |
| 38 | |
| 39 | it("finds the rows that were not shown before", () => { |
| 40 | const first = arrivedOf(null, "last", ["a", "b"]).seen; |
| 41 | const { arrived } = arrivedOf(first, "last", ["c", "a", "b"]); |
| 42 | assert.deepEqual([...arrived], ["c"]); |
| 43 | }); |
| 44 | |
| 45 | it("does not count a row that went and came back as new", () => { |
| 46 | const first = arrivedOf(null, "last", ["a", "b"]).seen; |
| 47 | const second = arrivedOf(first, "last", ["b"]).seen; |
| 48 | assert.deepEqual([...arrivedOf(second, "last", ["a", "b"]).arrived], []); |
| 49 | }); |
| 50 | |
| 51 | it("starts over when the span switches", () => { |
| 52 | const first = arrivedOf(null, "last", ["a"]).seen; |
| 53 | assert.deepEqual([...arrivedOf(first, "7d", ["a", "b", "c"]).arrived], []); |
| 54 | }); |
| 55 | }); |