| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import type { G1tEvent } from "@g1t/contracts"; |
| 5 | |
| 6 | import { |
| 7 | type ActivityItem, |
| 8 | type Need, |
| 9 | DELETED_USER, |
| 10 | FEED_EVENT_TYPES, |
| 11 | PROJECT_FEED_EVENT_TYPES, |
| 12 | eventItem, |
| 13 | projectFeed, |
| 14 | pushItem, |
| 15 | actorIds, |
| 16 | ageBuckets, |
| 17 | dailyBuckets, |
| 18 | firstPassRate, |
| 19 | formatSpan, |
| 20 | greetingFor, |
| 21 | groupActivity, |
| 22 | hourIn, |
| 23 | issueToMerge, |
| 24 | median, |
| 25 | nameActor, |
| 26 | nextSeen, |
| 27 | pipelineStage, |
| 28 | queuedNumbers, |
| 29 | runHealth, |
| 30 | rankNeeds, |
| 31 | readCookie, |
| 32 | sparkPoints, |
| 33 | stuckMinutes, |
| 34 | waitedFor, |
| 35 | } from "./mission.ts"; |
| 36 | |
| 37 | const MIN = 60_000; |
| 38 | const HOUR = 60 * MIN; |
| 39 | const DAY = 24 * HOUR; |
| 40 | const NOW = Date.parse("2026-10-04T18:00:00Z"); |
| 41 | |
| 42 | test("a first visit has nothing to count from and starts the clock", () => { |
| 43 | assert.deepEqual(nextSeen(null, NOW), { since: null, value: `0.${NOW}` }); |
| 44 | assert.deepEqual(nextSeen("garbage", NOW), { since: null, value: `0.${NOW}` }); |
| 45 | }); |
| 46 | |
| 47 | test("views within a visit keep counting from the visit before", () => { |
| 48 | const visitStart = NOW - 5 * MIN; |
| 49 | const before = NOW - 3 * DAY; |
| 50 | assert.deepEqual(nextSeen(`${before}.${visitStart}`, NOW), { since: before, value: `${before}.${NOW}` }); |
| 51 | // A first visit, refreshed: still nothing to count from. |
| 52 | assert.deepEqual(nextSeen(`0.${visitStart}`, NOW), { since: null, value: `0.${NOW}` }); |
| 53 | }); |
| 54 | |
| 55 | test("a long gap starts a new visit, counting from the last view", () => { |
| 56 | const last = NOW - 2 * HOUR; |
| 57 | assert.deepEqual(nextSeen(`${NOW - DAY}.${last}`, NOW), { since: last, value: `${last}.${NOW}` }); |
| 58 | }); |
| 59 | |
| 60 | test("a cookie from the future is ignored", () => { |
| 61 | assert.equal(nextSeen(`0.${NOW + DAY}`, NOW).since, null); |
| 62 | }); |
| 63 | |
| 64 | test("cookies are read by name", () => { |
| 65 | assert.equal(readCookie("a=1; g1t_seen=0.5; b=2", "g1t_seen"), "0.5"); |
| 66 | assert.equal(readCookie("g1t_tz=America%2FLos_Angeles", "g1t_tz"), "America/Los_Angeles"); |
| 67 | assert.equal(readCookie(null, "x"), null); |
| 68 | assert.equal(readCookie("x=1", "y"), null); |
| 69 | }); |
| 70 | |
| 71 | test("greetings follow the hour, in the viewer's zone", () => { |
| 72 | assert.equal(greetingFor(8), "Good morning"); |
| 73 | assert.equal(greetingFor(13), "Good afternoon"); |
| 74 | assert.equal(greetingFor(20), "Good evening"); |
| 75 | assert.equal(greetingFor(2), "Good evening"); |
| 76 | assert.equal(hourIn(NOW, "UTC"), 18); |
| 77 | assert.equal(hourIn(NOW, "America/Los_Angeles"), 11); |
| 78 | assert.equal(hourIn(NOW, "Not/AZone"), 18); |
| 79 | }); |
| 80 | |
| 81 | const repo = { namespace: "acme", name: "api" }; |
| 82 | const item = (id: string, at: number, actor: string | null, verb: ActivityItem["verb"], number: number | null, other = repo): ActivityItem => ({ |
| 83 | id, |
| 84 | at, |
| 85 | repo: other, |
| 86 | actor, |
| 87 | verb, |
| 88 | number, |
| 89 | }); |
| 90 | |
| 91 | test("one person's burst in one project reads as one line", () => { |
| 92 | const groups = groupActivity([ |
| 93 | item("1", NOW, "g1t", "landed", 4), |
| 94 | item("2", NOW - 5 * MIN, "g1t", "started", 5), |
| 95 | item("3", NOW - 10 * MIN, "g1t", "started", 6), |
| 96 | item("4", NOW - 12 * MIN, "g1t", "landed", 3), |
| 97 | item("5", NOW - 20 * MIN, "ana", "opened_issue", 7), |
| 98 | item("6", NOW - 25 * MIN, "g1t", "landed", 2), |
| 99 | ]); |
| 100 | assert.equal(groups.length, 3); |
| 101 | assert.equal(groups[0].count, 4); |
| 102 | assert.deepEqual(groups[0].parts.map((p) => [p.verb, p.numbers]), [ |
| 103 | ["landed", [4, 3]], |
| 104 | ["started", [5, 6]], |
| 105 | ]); |
| 106 | assert.equal(groups[0].from, NOW - 12 * MIN); |
| 107 | assert.equal(groups[1].actor, "ana"); |
| 108 | }); |
| 109 | |
| 110 | test("a burst breaks across projects and long gaps", () => { |
| 111 | const other = { namespace: "acme", name: "web" }; |
| 112 | const groups = groupActivity([ |
| 113 | item("1", NOW, "g1t", "landed", 1), |
| 114 | item("2", NOW - MIN, "g1t", "landed", 2, other), |
| 115 | item("3", NOW - 2 * HOUR, "g1t", "landed", 3, other), |
| 116 | ]); |
| 117 | assert.equal(groups.length, 3); |
| 118 | }); |
| 119 | |
| 120 | test("needs are ranked by urgency, then by how long they waited", () => { |
| 121 | const need = (key: string, kind: Need["kind"], at: number): Need => ({ key, kind, at, title: key, detail: "", to: "/", action: "Open", where: null }); |
| 122 | const ranked = rankNeeds([ |
| 123 | need("r1", "review", NOW - HOUR), |
| 124 | need("d", "deploy", NOW), |
| 125 | need("r2", "review", NOW - DAY), |
| 126 | need("r2", "review", NOW - DAY), |
| 127 | ]); |
| 128 | assert.deepEqual(ranked.map((n) => n.key), ["d", "r2", "r1"]); |
| 129 | }); |
| 130 | |
| 131 | test("a running run quiet for ten minutes is stuck", () => { |
| 132 | assert.equal(stuckMinutes({ status: "running", updatedAt: new Date(NOW - 14 * MIN).toISOString() }, NOW), 14); |
| 133 | assert.equal(stuckMinutes({ status: "running", updatedAt: new Date(NOW - 2 * MIN).toISOString() }, NOW), null); |
| 134 | assert.equal(stuckMinutes({ status: "queued", updatedAt: new Date(NOW - HOUR).toISOString() }, NOW), null); |
| 135 | }); |
| 136 | |
| 137 | test("daily buckets put each point on its day, oldest first", () => { |
| 138 | const buckets = dailyBuckets( |
| 139 | [{ at: NOW }, { at: NOW - DAY }, { at: NOW - DAY, value: 2 }, { at: NOW - 10 * DAY }], |
| 140 | 7, |
| 141 | NOW, |
| 142 | ); |
| 143 | assert.deepEqual(buckets, [0, 0, 0, 0, 0, 3, 1]); |
| 144 | }); |
| 145 | |
| 146 | test("median, pass rates and spans", () => { |
| 147 | assert.equal(median([]), null); |
| 148 | assert.equal(median([3, 1, 2]), 2); |
| 149 | assert.equal(median([4, 1, 2, 3]), 2.5); |
| 150 | const rate = firstPassRate([ |
| 151 | { repo: "a", number: 1, at: 2, passed: true }, |
| 152 | { repo: "a", number: 1, at: 1, passed: false }, |
| 153 | { repo: "a", number: 2, at: 1, passed: true }, |
| 154 | ]); |
| 155 | assert.deepEqual(rate, { rate: 0.5, of: 2 }); |
| 156 | assert.deepEqual(firstPassRate([]), { rate: null, of: 0 }); |
| 157 | // Workflow runs: cancelled, skipped and unfinished ones don't count; a pass on a second attempt isn't a first-try pass. |
| 158 | const runs = runHealth([ |
| 159 | { attempt: 1, status: "completed", conclusion: "success" }, |
| 160 | { attempt: 2, status: "completed", conclusion: "success" }, |
| 161 | { attempt: 1, status: "completed", conclusion: "failure" }, |
| 162 | { attempt: 1, status: "completed", conclusion: "cancelled" }, |
| 163 | { attempt: 1, status: "in_progress", conclusion: null }, |
| 164 | ]); |
| 165 | assert.equal(runs.checkRuns, 3); |
| 166 | assert.equal(runs.passRate, 2 / 3); |
| 167 | assert.deepEqual(runs.firstPass, { rate: 1 / 3, of: 3 }); |
| 168 | assert.deepEqual(runHealth([]), { passRate: null, firstPass: { rate: null, of: 0 }, checkRuns: 0 }); |
| 169 | assert.equal(formatSpan(null), "—"); |
| 170 | assert.equal(formatSpan(30 * MIN), "30m"); |
| 171 | assert.equal(formatSpan(5 * HOUR), "5h"); |
| 172 | assert.equal(formatSpan(2 * DAY + 4 * HOUR), "2d 4h"); |
| 173 | }); |
| 174 | |
| 175 | test("issue to merge pairs a merge with the issue it resolved", () => { |
| 176 | const spans = issueToMerge( |
| 177 | [{ repo: "a", number: 1, at: 100 }, { repo: "b", number: 1, at: 50 }], |
| 178 | [{ repo: "a", issue: 1, at: 400 }, { repo: "a", issue: null, at: 500 }, { repo: "c", issue: 1, at: 900 }], |
| 179 | ); |
| 180 | assert.deepEqual(spans, [300]); |
| 181 | }); |
| 182 | |
| 183 | test("sparkline points span the box, zero on the floor", () => { |
| 184 | assert.deepEqual(sparkPoints([], 100, 20), []); |
| 185 | const points = sparkPoints([0, 5, 10], 100, 20, 2); |
| 186 | assert.deepEqual(points, [ |
| 187 | [2, 18], |
| 188 | [50, 10], |
| 189 | [98, 2], |
| 190 | ]); |
| 191 | assert.deepEqual(sparkPoints([0, 0], 10, 10, 1), [ |
| 192 | [1, 9], |
| 193 | [9, 9], |
| 194 | ]); |
| 195 | assert.deepEqual(sparkPoints([3], 10, 10, 1), [[5, 1]]); |
| 196 | }); |
| 197 | |
| 198 | test("a pull request's place in the pipeline", () => { |
| 199 | const queued = queuedNumbers([ |
| 200 | { number: 9, state: "waiting" }, |
| 201 | { number: 8, state: "landed" }, |
| 202 | ]); |
| 203 | assert.equal(pipelineStage({ status: "merged", checkStatus: null, number: 1 }, undefined, queued), "landed"); |
| 204 | assert.equal(pipelineStage({ status: "open", checkStatus: "passed", number: 9 }, undefined, queued), "queue"); |
| 205 | assert.equal(pipelineStage({ status: "draft", checkStatus: null, number: 2 }, undefined, queued), "working"); |
| 206 | assert.equal(pipelineStage({ status: "open", checkStatus: "running", number: 3 }, undefined, queued), "checking"); |
| 207 | assert.equal(pipelineStage({ status: "open", checkStatus: "passed", number: 4 }, { kind: "revise" }, queued), "working"); |
| 208 | assert.equal(pipelineStage({ status: "open", checkStatus: "passed", number: 5 }, { kind: "review" }, queued), "reviewing"); |
| 209 | assert.equal(pipelineStage({ status: "open", checkStatus: "failed", number: 6 }, undefined, queued), "reviewing"); |
| 210 | }); |
| 211 | |
| 212 | test("open issues by age", () => { |
| 213 | const buckets = ageBuckets( |
| 214 | [new Date(NOW - HOUR).toISOString(), new Date(NOW - 3 * DAY).toISOString(), new Date(NOW - 90 * DAY).toISOString()], |
| 215 | NOW, |
| 216 | ); |
| 217 | assert.deepEqual(buckets.map((b) => b.count), [1, 1, 0, 1]); |
| 218 | }); |
| 219 | |
| 220 | test("the feed names people, not account ids", () => { |
| 221 | const actors = ["usr_b51a1a09", "g1t", null, "usr_b51a1a09", "usr_g1t_agent", "g1t_policy", "usr_gone"]; |
| 222 | assert.deepEqual(actorIds(actors), ["usr_b51a1a09", "usr_gone"]); |
| 223 | const names = { usr_b51a1a09: "syntaqx" }; |
| 224 | assert.equal(nameActor("usr_b51a1a09", names), "syntaqx"); |
| 225 | assert.equal(nameActor("ada", names), "ada"); |
| 226 | assert.equal(nameActor(null, names), null); |
| 227 | assert.equal(nameActor("usr_g1t_agent", names), "g1t"); |
| 228 | // Unknown to the lookup: the account is gone. No lookup at all: only "someone". |
| 229 | assert.equal(nameActor("usr_gone", names), DELETED_USER); |
| 230 | assert.equal(nameActor("usr_gone", null), "someone"); |
| 231 | }); |
| 232 | |
| 233 | test("waits read in the largest whole unit", () => { |
| 234 | assert.equal(waitedFor(0), "0 min"); |
| 235 | assert.equal(waitedFor(45.7), "45 min"); |
| 236 | assert.equal(waitedFor(60), "1 h"); |
| 237 | assert.equal(waitedFor(1022), "17 h"); |
| 238 | assert.equal(waitedFor(24 * 60), "1 d"); |
| 239 | assert.equal(waitedFor(3 * 24 * 60 - 1), "2 d"); |
| 240 | }); |
| 241 | |
| 242 | // --- A project's feed ----------------------------------------------------------- |
| 243 | |
| 244 | const REPO = { namespace: "flagon-io", name: "g1t" }; |
| 245 | const event = (type: string, data: Record<string, unknown>, id = `evt_${type}`, actor: string | null = "usr_ada"): G1tEvent => |
| 246 | ({ id, type, source: "test", time: "2026-10-08T12:00:00Z", repoId: "rep_1", actor, data }) as unknown as G1tEvent; |
| 247 | const deployment = (state: string, production = true) => |
| 248 | event("deployment_status.created", { |
| 249 | repoId: "rep_1", |
| 250 | deployment: { id: "dep_9", environment: production ? "production" : "preview", production_environment: production }, |
| 251 | deploymentStatus: { state }, |
| 252 | }); |
| 253 | |
| 254 | test("the feed asks the log only for the kinds it shows, and pushes for one project", () => { |
| 255 | for (const type of ["session.appended", "queue.changed", "pull.updated", "pull.mergecheck", "workflow.completed"]) { |
| 256 | assert.ok(!(PROJECT_FEED_EVENT_TYPES as readonly string[]).includes(type), type); |
| 257 | } |
| 258 | assert.ok((PROJECT_FEED_EVENT_TYPES as readonly string[]).includes("git.push")); |
| 259 | assert.ok(!(FEED_EVENT_TYPES as readonly string[]).includes("git.push")); |
| 260 | // Every kind asked for makes a line of some events. |
| 261 | for (const type of FEED_EVENT_TYPES) assert.ok(type.includes("."), type); |
| 262 | }); |
| 263 | |
| 264 | test("a production deploy that finished is a line, linked to it on the current path", () => { |
| 265 | const up = eventItem(deployment("success"), REPO); |
| 266 | assert.equal(up?.verb, "deployed"); |
| 267 | assert.equal(up?.to, "/flagon-io/g1t/deployments/dep_9"); |
| 268 | assert.equal(eventItem(deployment("failure"), REPO)?.verb, "deploy_failed"); |
| 269 | assert.equal(eventItem(deployment("in_progress"), REPO), null); |
| 270 | assert.equal(eventItem(deployment("success", false), REPO), null); |
| 271 | }); |
| 272 | |
| 273 | test("a project's feed has its pushes to the default branch, but not the ones that only landed a pull request", () => { |
| 274 | const events = [ |
| 275 | event("pull.merged", { pullId: "pul_1", repoId: "rep_1", number: 5, commit: "aaaaaaa1111" }, "evt_1"), |
| 276 | event("git.push", { repoId: "rep_1", ref: "refs/heads/main", after: "aaaaaaa1111", defaultBranch: true }, "evt_2"), |
| 277 | event("git.push", { repoId: "rep_1", ref: "refs/heads/main", after: "bbbbbbb2222", defaultBranch: true }, "evt_3"), |
| 278 | event("git.push", { repoId: "rep_1", ref: "refs/heads/topic", after: "ccccccc3333", defaultBranch: false }, "evt_4"), |
| 279 | event("session.appended", { repoId: "rep_1", number: 5 }, "evt_5"), |
| 280 | ]; |
| 281 | const feed = projectFeed(events, REPO); |
| 282 | assert.deepEqual( |
| 283 | feed.map((item) => [item.verb, item.text ?? item.number]), |
| 284 | [ |
| 285 | ["landed", 5], |
| 286 | ["pushed", "bbbbbbb"], |
| 287 | ], |
| 288 | ); |
| 289 | assert.equal(feed[1]?.to, "/flagon-io/g1t/commit/bbbbbbb2222"); |
| 290 | assert.equal(pushItem(events[4]!, REPO, new Set()), null); |
| 291 | }); |