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