|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + getRepoQueueTrendSnapshot, |
| 4 | + persistRepoGithubTotalsSnapshot, |
| 5 | + persistSignalSnapshot, |
| 6 | + upsertPullRequestFromGitHub, |
| 7 | + upsertRepositoryFromGitHub, |
| 8 | +} from "../../src/db/repositories"; |
| 9 | +import { generateSignalSnapshots } from "../../src/queue/processors"; |
| 10 | +import type { QueueTrendReport } from "../../src/services/queue-trends"; |
| 11 | +import type { RepoGithubTotalsSnapshotRecord } from "../../src/types"; |
| 12 | +import { createTestEnv } from "../helpers/d1"; |
| 13 | + |
| 14 | +const REPO = "owner/trend-history"; |
| 15 | +const FIXTURE_NOW_MS = Date.parse("2026-07-31T12:00:00.000Z"); |
| 16 | + |
| 17 | +describe("signal-snapshot queue-trend history window (#10020)", () => { |
| 18 | + beforeEach(() => { |
| 19 | + vi.useFakeTimers({ now: FIXTURE_NOW_MS }); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + vi.useRealTimers(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("REGRESSION: time-bounded queue-health history lets the 30-day window resolve duplicate and stale deltas", async () => { |
| 27 | + const env = createTestEnv(); |
| 28 | + await upsertRepositoryFromGitHub( |
| 29 | + env, |
| 30 | + { name: "trend-history", full_name: REPO, private: false, owner: { login: "owner" }, default_branch: "main" }, |
| 31 | + 801, |
| 32 | + ); |
| 33 | + await env.DB.prepare("update repositories set is_registered = 1 where full_name = ?").bind(REPO).run(); |
| 34 | + await upsertPullRequestFromGitHub(env, REPO, { |
| 35 | + number: 1, |
| 36 | + title: "Open fix", |
| 37 | + state: "open", |
| 38 | + user: { login: "miner" }, |
| 39 | + author_association: "NONE", |
| 40 | + labels: [], |
| 41 | + body: "Fixes #1", |
| 42 | + created_at: atDaysAgo(40), |
| 43 | + updated_at: atDaysAgo(0), |
| 44 | + }); |
| 45 | + |
| 46 | + // 130 queue-health rows across ~33 days at four/day — under the old listSignalSnapshots(limit 100) |
| 47 | + // only ~25 days remained and the 30-day baseline stayed null. |
| 48 | + let id = 0; |
| 49 | + for (let day = 32; day >= 0; day -= 1) { |
| 50 | + for (let slot = 0; slot < 4; slot += 1) { |
| 51 | + if (id >= 130) break; |
| 52 | + const daysAgo = day + slot / 4; |
| 53 | + await persistSignalSnapshot(env, { |
| 54 | + id: `qh-${id}`, |
| 55 | + signalType: "queue-health", |
| 56 | + targetKey: REPO, |
| 57 | + repoFullName: REPO, |
| 58 | + generatedAt: atDaysAgo(daysAgo), |
| 59 | + payload: { |
| 60 | + signals: { |
| 61 | + openPullRequests: 10 + Math.floor(daysAgo), |
| 62 | + stalePullRequests: 1 + Math.floor(daysAgo / 10), |
| 63 | + collisionClusters: 1 + Math.floor((32 - day) / 8), |
| 64 | + }, |
| 65 | + }, |
| 66 | + }); |
| 67 | + id += 1; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + for (const daysAgo of [33, 30, 14, 7, 0]) { |
| 72 | + await persistRepoGithubTotalsSnapshot(env, totals(daysAgo, { |
| 73 | + openIssues: 10 + daysAgo, |
| 74 | + openPrs: 4 + Math.floor(daysAgo / 5), |
| 75 | + merged: 20 - Math.floor(daysAgo / 3), |
| 76 | + closed: 5, |
| 77 | + })); |
| 78 | + } |
| 79 | + |
| 80 | + await generateSignalSnapshots(env, REPO); |
| 81 | + |
| 82 | + const snapshot = await getRepoQueueTrendSnapshot(env, REPO); |
| 83 | + const report = snapshot?.payload as unknown as QueueTrendReport; |
| 84 | + const window30 = report?.windows.find((window) => window.windowDays === 30); |
| 85 | + expect(window30).toMatchObject({ |
| 86 | + status: "ready", |
| 87 | + duplicateTrend: expect.any(Number), |
| 88 | + stalePullRequestRateDelta: expect.any(Number), |
| 89 | + }); |
| 90 | + expect(window30?.duplicateTrend).not.toBeNull(); |
| 91 | + expect(window30?.stalePullRequestRateDelta).not.toBeNull(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("a repo with no queue-health history still persists a trend (map-miss ?? [] arm) with unavailable windows when totals are missing", async () => { |
| 95 | + const env = createTestEnv(); |
| 96 | + await upsertRepositoryFromGitHub( |
| 97 | + env, |
| 98 | + { name: "empty-history", full_name: "owner/empty-history", private: false, owner: { login: "owner" }, default_branch: "main" }, |
| 99 | + 802, |
| 100 | + ); |
| 101 | + |
| 102 | + await generateSignalSnapshots(env, "owner/empty-history"); |
| 103 | + |
| 104 | + const snapshot = await getRepoQueueTrendSnapshot(env, "owner/empty-history"); |
| 105 | + const report = snapshot?.payload as unknown as QueueTrendReport; |
| 106 | + expect(report).toMatchObject({ |
| 107 | + status: "unavailable", |
| 108 | + windows: [ |
| 109 | + expect.objectContaining({ windowDays: 7, status: "unavailable" }), |
| 110 | + expect.objectContaining({ windowDays: 14, status: "unavailable" }), |
| 111 | + expect.objectContaining({ windowDays: 30, status: "unavailable" }), |
| 112 | + ], |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +function totals( |
| 118 | + daysAgo: number, |
| 119 | + values: { openIssues: number; openPrs: number; merged: number; closed: number }, |
| 120 | +): RepoGithubTotalsSnapshotRecord { |
| 121 | + return { |
| 122 | + id: `totals-${daysAgo}-${REPO}`, |
| 123 | + repoFullName: REPO, |
| 124 | + openIssuesTotal: values.openIssues, |
| 125 | + openPullRequestsTotal: values.openPrs, |
| 126 | + mergedPullRequestsTotal: values.merged, |
| 127 | + closedUnmergedPullRequestsTotal: values.closed, |
| 128 | + labelsTotal: 0, |
| 129 | + sourceKind: "test", |
| 130 | + fetchedAt: atDaysAgo(daysAgo), |
| 131 | + payload: {}, |
| 132 | + }; |
| 133 | +} |
| 134 | + |
| 135 | +function atDaysAgo(daysAgo: number): string { |
| 136 | + return new Date(FIXTURE_NOW_MS - daysAgo * 24 * 60 * 60 * 1000).toISOString(); |
| 137 | +} |
0 commit comments