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