Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
pr-size.test.ts12.3 KB · 452 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/**
 * Block J32 — PR size distribution metric. Pure rollup tests.
 */

import { describe, it, expect } from "bun:test";
import {
  PR_SIZE_CLASSES,
  DEFAULT_TOP_N,
  DEFAULT_WINDOW_DAYS,
  VALID_WINDOWS,
  parseWindow,
  classifyPrSize,
  computePrSizeStats,
  summarisePrSizes,
  bucketPrSizes,
  topLargestPrs,
  buildPrSizeReport,
  __internal,
  type PrSizeInput,
  type PrSizeStat,
} from "../lib/pr-size";

const DAY = 24 * 60 * 60 * 1000;

describe("pr-size — re-exports from J25", () => {
  it("parseWindow accepts canonical windows", () => {
    expect(parseWindow("7")).toBe(7);
    expect(parseWindow(undefined)).toBe(DEFAULT_WINDOW_DAYS);
  });
  it("VALID_WINDOWS includes the default", () => {
    expect(VALID_WINDOWS).toContain(DEFAULT_WINDOW_DAYS);
  });
});

describe("pr-size — classifyPrSize", () => {
  it("maps to the five classes by boundary", () => {
    expect(classifyPrSize(0)).toBe("xs");
    expect(classifyPrSize(10)).toBe("xs"); // inclusive
    expect(classifyPrSize(11)).toBe("s");
    expect(classifyPrSize(50)).toBe("s"); // inclusive
    expect(classifyPrSize(51)).toBe("m");
    expect(classifyPrSize(250)).toBe("m");
    expect(classifyPrSize(251)).toBe("l");
    expect(classifyPrSize(1000)).toBe("l");
    expect(classifyPrSize(1001)).toBe("xl");
    expect(classifyPrSize(99_999)).toBe("xl");
  });
  it("defaults bogus values to xs", () => {
    expect(classifyPrSize(Number.NaN)).toBe("xs");
    expect(classifyPrSize(-5)).toBe("xs");
  });
  it("has exactly five ordered classes", () => {
    expect(PR_SIZE_CLASSES).toHaveLength(5);
    expect(PR_SIZE_CLASSES.map((c) => c.key)).toEqual([
      "xs",
      "s",
      "m",
      "l",
      "xl",
    ]);
  });
});

describe("pr-size — computePrSizeStats + window", () => {
  const now = new Date("2025-04-01T00:00:00Z").getTime();
  const prs: PrSizeInput[] = [
    {
      id: "m",
      number: 1,
      title: "merged recent",
      state: "merged",
      createdAt: new Date(now - 60 * DAY),
      mergedAt: new Date(now - 5 * DAY), // anchors on mergedAt
      additions: 30,
      deletions: 10,
      files: 3,
    },
    {
      id: "o",
      number: 2,
      title: "open recent",
      state: "open",
      createdAt: new Date(now - 3 * DAY),
      additions: 300,
      deletions: 50,
      files: 10,
    },
    {
      id: "old",
      number: 3,
      title: "old open",
      state: "open",
      createdAt: new Date(now - 90 * DAY),
      additions: 5,
      deletions: 5,
      files: 1,
    },
    {
      id: "bogus",
      number: 4,
      title: "bad date",
      state: "open",
      createdAt: "not-a-date",
      additions: 10,
      deletions: 10,
      files: 1,
    },
  ];

  it("filters to the window", () => {
    const out = computePrSizeStats(prs, 30, now);
    expect(out.map((s) => s.id).sort()).toEqual(["m", "o"]);
  });

  it("computes linesChanged + sizeClass", () => {
    const out = computePrSizeStats(prs, 0, now);
    const m = out.find((s) => s.id === "m")!;
    const o = out.find((s) => s.id === "o")!;
    expect(m.linesChanged).toBe(40);
    expect(m.sizeClass).toBe("s");
    expect(o.linesChanged).toBe(350);
    expect(o.sizeClass).toBe("l");
  });

  it("window=0 keeps everything parseable", () => {
    const out = computePrSizeStats(prs, 0, now);
    expect(out.map((s) => s.id).sort()).toEqual(["m", "o", "old"]);
  });

  it("drops PRs with unparseable createdAt", () => {
    const out = computePrSizeStats(prs, 0, now);
    expect(out.some((s) => s.id === "bogus")).toBe(false);
  });

  it("anchors merged PRs on mergedAt so recent merges with old createdAt land in window", () => {
    const far: PrSizeInput = {
      id: "z",
      number: 99,
      title: "ancient PR, recent merge",
      state: "merged",
      createdAt: new Date(now - 365 * DAY),
      mergedAt: new Date(now - 2 * DAY),
      additions: 100,
      deletions: 0,
      files: 1,
    };
    const out = computePrSizeStats([far], 7, now);
    expect(out).toHaveLength(1);
  });

  it("treats negative / NaN line counts as zero", () => {
    const pr: PrSizeInput = {
      id: "neg",
      number: 1,
      title: "garbage numbers",
      state: "open",
      createdAt: new Date(now),
      additions: -10,
      deletions: Number.NaN,
      files: 2,
    };
    const [stat] = computePrSizeStats([pr], 0, now);
    expect(stat!.linesChanged).toBe(0);
    expect(stat!.sizeClass).toBe("xs");
  });
});

describe("pr-size — summarisePrSizes", () => {
  it("zero stats", () => {
    const s = summarisePrSizes([]);
    expect(s.total).toBe(0);
    expect(s.medianLines).toBe(0);
    expect(s.p90Lines).toBe(0);
    expect(s.smallPrRatio).toBe(0);
  });

  it("computes percentiles over a uniform series", () => {
    const stats: PrSizeStat[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((n) => ({
      id: String(n),
      number: n,
      title: "t",
      state: "merged",
      isDraft: false,
      createdAt: new Date(),
      additions: n,
      deletions: 0,
      files: 1,
      linesChanged: n,
      sizeClass: "xs",
    }));
    const s = summarisePrSizes(stats);
    expect(s.total).toBe(10);
    expect(s.medianLines).toBe(6); // round(5.5)
    expect(s.meanLines).toBe(6); // round(5.5)
    expect(s.p90Lines).toBe(9); // round(9.1)
    expect(s.largestLines).toBe(10);
    expect(s.smallestLines).toBe(1);
    expect(s.smallPrRatio).toBe(100); // all xs
  });

  it("classifies merged / open / draft separately", () => {
    const mk = (
      over: Partial<PrSizeStat> & { id: string }
    ): PrSizeStat => ({
      id: over.id,
      number: 1,
      title: "t",
      state: "open",
      isDraft: false,
      createdAt: new Date(),
      additions: 5,
      deletions: 5,
      files: 1,
      linesChanged: 10,
      sizeClass: "xs",
      ...over,
    });
    const stats = [
      mk({ id: "merged", state: "merged" }),
      mk({ id: "open", state: "open", isDraft: false }),
      mk({ id: "draft", state: "open", isDraft: true }),
      mk({ id: "closed", state: "closed" }),
    ];
    const s = summarisePrSizes(stats);
    expect(s.merged).toBe(1);
    expect(s.open).toBe(1); // draft excluded
  });

  it("smallPrRatio rounds to one decimal", () => {
    // 1 small PR, 2 large PRs → ratio = 33.3%
    const mk = (linesChanged: number, cls: any): PrSizeStat => ({
      id: String(linesChanged),
      number: linesChanged,
      title: "t",
      state: "merged",
      isDraft: false,
      createdAt: new Date(),
      additions: linesChanged,
      deletions: 0,
      files: 1,
      linesChanged,
      sizeClass: cls,
    });
    const s = summarisePrSizes([
      mk(5, "xs"),
      mk(500, "l"),
      mk(500, "l"),
    ]);
    expect(s.smallPrRatio).toBe(33.3);
  });
});

describe("pr-size — bucketPrSizes", () => {
  it("distributes into the five class buckets with zero-count defaults", () => {
    const mk = (
      id: string,
      linesChanged: number,
      sizeClass: any
    ): PrSizeStat => ({
      id,
      number: 1,
      title: "t",
      state: "merged",
      isDraft: false,
      createdAt: new Date(),
      additions: linesChanged,
      deletions: 0,
      files: 1,
      linesChanged,
      sizeClass,
    });
    const stats = [
      mk("a", 5, "xs"),
      mk("b", 20, "s"),
      mk("c", 100, "m"),
      mk("d", 500, "l"),
      mk("e", 2000, "xl"),
      mk("f", 2, "xs"),
    ];
    const b = bucketPrSizes(stats);
    const by = Object.fromEntries(b.map((x) => [x.key, x]));
    expect(by.xs!.count).toBe(2);
    expect(by.s!.count).toBe(1);
    expect(by.m!.count).toBe(1);
    expect(by.l!.count).toBe(1);
    expect(by.xl!.count).toBe(1);
    expect(by.xs!.bytes).toBe(7);
  });

  it("returns all five buckets for empty input", () => {
    const b = bucketPrSizes([]);
    expect(b).toHaveLength(5);
    expect(b.every((x) => x.count === 0 && x.bytes === 0)).toBe(true);
  });
});

describe("pr-size — topLargestPrs", () => {
  const mk = (
    id: string,
    linesChanged: number,
    number: number
  ): PrSizeStat => ({
    id,
    number,
    title: "t",
    state: "merged",
    isDraft: false,
    createdAt: new Date(),
    additions: linesChanged,
    deletions: 0,
    files: 1,
    linesChanged,
    sizeClass: "xs",
  });

  it("returns sorted-desc by linesChanged", () => {
    const out = topLargestPrs([
      mk("a", 10, 1),
      mk("b", 1000, 2),
      mk("c", 50, 3),
    ]);
    expect(out.map((s) => s.id)).toEqual(["b", "c", "a"]);
  });

  it("tie-breaks by PR number descending", () => {
    const out = topLargestPrs([
      mk("older", 100, 1),
      mk("newer", 100, 2),
    ]);
    expect(out[0]!.id).toBe("newer");
    expect(out[1]!.id).toBe("older");
  });

  it("honours limit", () => {
    const stats = Array.from({ length: 50 }, (_, i) => mk(String(i), i, i));
    expect(topLargestPrs(stats, 5)).toHaveLength(5);
  });

  it("defaults invalid limits to DEFAULT_TOP_N", () => {
    const stats = Array.from({ length: 50 }, (_, i) => mk(String(i), i, i));
    expect(topLargestPrs(stats, 0)).toHaveLength(DEFAULT_TOP_N);
    expect(topLargestPrs(stats, -5)).toHaveLength(DEFAULT_TOP_N);
  });

  it("never mutates input", () => {
    const stats = [mk("a", 10, 1), mk("b", 1000, 2)];
    const copy = [...stats];
    topLargestPrs(stats);
    expect(stats).toEqual(copy);
  });
});

describe("pr-size — buildPrSizeReport", () => {
  const now = new Date("2025-04-01T00:00:00Z").getTime();
  const prs: PrSizeInput[] = [
    {
      id: "a",
      number: 1,
      title: "tiny",
      state: "merged",
      createdAt: new Date(now - DAY),
      mergedAt: new Date(now - DAY),
      additions: 2,
      deletions: 1,
      files: 1,
    },
    {
      id: "b",
      number: 2,
      title: "huge",
      state: "merged",
      createdAt: new Date(now - 2 * DAY),
      mergedAt: new Date(now - 2 * DAY),
      additions: 800,
      deletions: 400,
      files: 50,
    },
    {
      id: "c",
      number: 3,
      title: "open",
      state: "open",
      createdAt: new Date(now - 3 * DAY),
      additions: 40,
      deletions: 0,
      files: 2,
    },
  ];

  it("builds a full report", () => {
    const r = buildPrSizeReport({ prs, windowDays: 30, now });
    expect(r.windowDays).toBe(30);
    expect(r.now).toBe(now);
    expect(r.perPr).toHaveLength(3);
    expect(r.summary.total).toBe(3);
    expect(r.summary.merged).toBe(2);
    expect(r.summary.open).toBe(1);
    expect(r.buckets).toHaveLength(5);
    expect(r.largest[0]!.id).toBe("b");
  });

  it("defaults now to Date.now when omitted", () => {
    const before = Date.now();
    const r = buildPrSizeReport({ prs: [], windowDays: 30 });
    const after = Date.now();
    expect(r.now).toBeGreaterThanOrEqual(before);
    expect(r.now).toBeLessThanOrEqual(after);
  });

  it("defaults windowDays to DEFAULT_WINDOW_DAYS when omitted", () => {
    const r = buildPrSizeReport({ prs: [] });
    expect(r.windowDays).toBe(DEFAULT_WINDOW_DAYS);
  });

  it("honours topN", () => {
    const r = buildPrSizeReport({ prs, windowDays: 30, now, topN: 1 });
    expect(r.largest).toHaveLength(1);
    expect(r.largest[0]!.id).toBe("b");
  });
});

describe("pr-size — routes", () => {
  it("GET /:o/:r/insights/pr-size returns 200 or 404 (never 500)", async () => {
    const { default: app } = await import("../app");
    const res = await app.request("/alice/repo/insights/pr-size");
    expect([200, 404]).toContain(res.status);
  });
  it("ignores bogus window values", async () => {
    const { default: app } = await import("../app");
    const res = await app.request(
      "/alice/repo/insights/pr-size?window=abc&top=xyz"
    );
    expect([200, 404]).toContain(res.status);
  });
});

describe("pr-size — __internal parity", () => {
  it("re-exports every helper", () => {
    expect(__internal.PR_SIZE_CLASSES).toBe(PR_SIZE_CLASSES);
    expect(__internal.DEFAULT_TOP_N).toBe(DEFAULT_TOP_N);
    expect(__internal.classifyPrSize).toBe(classifyPrSize);
    expect(__internal.computePrSizeStats).toBe(computePrSizeStats);
    expect(__internal.summarisePrSizes).toBe(summarisePrSizes);
    expect(__internal.bucketPrSizes).toBe(bucketPrSizes);
    expect(__internal.topLargestPrs).toBe(topLargestPrs);
    expect(__internal.buildPrSizeReport).toBe(buildPrSizeReport);
    expect(typeof __internal.toTime).toBe("function");
    expect(typeof __internal.anchorTime).toBe("function");
    expect(typeof __internal.percentile).toBe("function");
    expect(typeof __internal.safeLines).toBe("function");
  });
});