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