Open History to see the last commit
1import type { PullRequestCheck, PullRequestCheckStatus } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import { dedupeChecks } from "./pullRequestChecks.ts";
5
6function entry(
7 name: string,
8 status: PullRequestCheckStatus,
9 extra: { readonly workflowName?: string | null; readonly at?: string | null } = {},
10): {
11 readonly check: PullRequestCheck;
12 readonly workflowName: string | null;
13 readonly at: string | null;
14} {
15 return {
16 check: { name, status, description: null, url: null },
17 workflowName: extra.workflowName ?? null,
18 at: extra.at ?? null,
19 };
20}
21
22describe("dedupeChecks", () => {
23 it.each([
24 ["2026-10-16T12:00:00Z", "2026-10-16T12:00:00.500Z"],
25 ["2026-10-16T12:00:00+02:00", "2026-10-16T10:30:00Z"],
26 ["2026-10-16T12:00:00.0001Z", "2026-10-16T12:00:00.0002Z"],
27 ["2026-10-16T12:00:00.0001z", "2026-10-16T12:00:00.0002z"],
28 ["2026-10-16T12:00:00.00009Z", "2026-10-16T12:00:00.0001Z"],
29 ["2024-02-28T23:59:59Z", "2024-02-29T00:00:00.0001Z"],
30 ["2000-02-28T23:59:59Z", "2000-02-29T00:00:00Z"],
31 ["2024-03-01T01:00:00Z", "2024-02-29T23:30:00-02:00"],
32 ["2026-10-16t12:00:00z", "2026-10-16t12:00:00.0001z"],
33 ["+010000-01-01T00:00:00Z", "+010000-01-01T00:00:00.0001Z"],
34 ])("keeps the newer instant between %s and %s in either listing order", (older, newer) => {
35 const oldRun = entry("build", "success", { at: older });
36 const newRun = entry("build", "failure", { at: newer });
37
38 expect(
39 [
40 [oldRun, newRun],
41 [newRun, oldRun],
42 ].map((runs) => dedupeChecks(runs)[0]?.status),
43 ).toEqual(["failure", "failure"]);
44 });
45
46 it.each([
47 ["2026-10-16T12:00:00Z", "2026-10-16T12:00:00.000Z"],
48 ["2026-10-16T12:00:00Z", "2026-10-16T14:00:00+02:00"],
49 ["2026-10-16T24:00:00.000Z", "2026-10-17T00:00:00Z"],
50 ["2026-10-16T12:00Z", "2026-10-16T12:00:00Z"],
51 ])("takes the last copy for equal instants %s and %s", (first, second) => {
52 const forward = dedupeChecks([
53 entry("build", "success", { at: first }),
54 entry("build", "failure", { at: second }),
55 ]);
56 const reversed = dedupeChecks([
57 entry("build", "success", { at: second }),
58 entry("build", "failure", { at: first }),
59 ]);
60
61 expect([forward[0]?.status, reversed[0]?.status]).toEqual(["failure", "failure"]);
62 });
63
64 it("keeps the newest run of a check the host listed twice", () => {
65 const checks = dedupeChecks([
66 entry("Prepare PR size config", "success", {
67 workflowName: "PR Size",
68 at: "2026-08-11T16:06:25Z",
69 }),
70 entry("Prepare PR size config", "pending", {
71 workflowName: "PR Size",
72 at: "2026-08-11T17:01:04Z",
73 }),
74 ]);
75
76 expect(checks.map((check) => [check.name, check.status])).toEqual([
77 ["Prepare PR size config", "pending"],
78 ]);
79 });
80
81 it("preserves text ordering when a host supplies an unparseable timestamp", () => {
82 expect(
83 dedupeChecks([
84 entry("build", "success", { at: "invalid-a" }),
85 entry("build", "failure", { at: "invalid-b" }),
86 ]).map((check) => check.status),
87 ).toEqual(["failure"]);
88 });
89
90 it.each([
91 ["2026-10-16", "2026-10-15T23:00:00-02:00", "success"],
92 ["2026-10-16T00:00:00", "2026-10-15T23:00:00-02:00", "success"],
93 ["2026-10-16T24:00:00.0001Z", "2026-10-17T00:00:00Z", "failure"],
94 ])("uses text fallback for incomplete or invalid timestamp %s", (malformed, valid, expected) => {
95 const malformedRun = entry("build", "success", { at: malformed });
96 const validRun = entry("build", "failure", { at: valid });
97
98 expect(
99 [
100 [malformedRun, validRun],
101 [validRun, malformedRun],
102 ].map((runs) => dedupeChecks(runs)[0]?.status),
103 ).toEqual([expected, expected]);
104 });
105
106 it.each([
107 ["2026-02-30T00:00:00Z", "2026-03-01T00:00:00Z"],
108 ["2026-02-29T00:00:00Z", "2026-03-01T00:00:00Z"],
109 ["1900-02-29T00:00:00Z", "1900-03-01T00:00:00Z"],
110 ["2026-04-31T00:00:00.0001-02:00", "2026-05-01T00:00:00Z"],
111 ])("uses text fallback for impossible date %s", (malformed, valid) => {
112 const malformedRun = entry("build", "success", { at: malformed });
113 const validRun = entry("build", "failure", { at: valid });
114
115 expect(
116 [
117 [malformedRun, validRun],
118 [validRun, malformedRun],
119 ].map((runs) => dedupeChecks(runs)[0]?.status),
120 ).toEqual(["failure", "failure"]);
121 });
122
123 it("holds a check at the place it first appeared, so a re-run does not reshuffle the list", () => {
124 const checks = dedupeChecks([
125 entry("lint", "success", { workflowName: "CI", at: "2026-08-11T16:00:00Z" }),
126 entry("test", "success", { workflowName: "CI", at: "2026-08-11T16:00:00Z" }),
127 entry("lint", "failure", { workflowName: "CI", at: "2026-08-11T18:00:00Z" }),
128 ]);
129
130 expect(checks.map((check) => [check.name, check.status])).toEqual([
131 ["lint", "failure"],
132 ["test", "success"],
133 ]);
134 });
135
136 it("puts failed and cancelled checks first, preserving each group's order after reruns", () => {
137 const checks = dedupeChecks([
138 entry("lint", "failure", { at: "2026-08-11T16:00:00Z" }),
139 entry("integration", "cancelled"),
140 entry("types", "pending"),
141 entry("unit", "failure"),
142 entry("format", "success"),
143 entry("lint", "success", { at: "2026-08-11T18:00:00Z" }),
144 ]);
145
146 expect(checks.map((check) => [check.name, check.status])).toEqual([
147 ["integration", "cancelled"],
148 ["unit", "failure"],
149 ["lint", "success"],
150 ["types", "pending"],
151 ["format", "success"],
152 ]);
153 });
154
155 it("keeps distinct workflow and check pairs even when their names join to the same text", () => {
156 const checks = dedupeChecks([
157 entry("Linux tests", "success", {
158 workflowName: "Build",
159 at: "2026-08-11T16:00:00Z",
160 }),
161 entry("tests", "success", {
162 workflowName: "Build Linux",
163 at: "2026-08-11T17:00:00Z",
164 }),
165 entry("Linux tests", "failure", {
166 workflowName: "Build",
167 at: "2026-08-11T18:00:00Z",
168 }),
169 ]);
170
171 expect(checks.map((check) => [check.name, check.status])).toEqual([
172 ["Linux tests", "failure"],
173 ["tests", "success"],
174 ]);
175 });
176
177 it("loses a run that never said when it happened to one that did, whichever came first", () => {
178 const undated = dedupeChecks([
179 entry("build", "success", { at: "2026-08-11T16:00:00Z" }),
180 entry("build", "pending"),
181 ]);
182 const dated = dedupeChecks([
183 entry("build", "pending"),
184 entry("build", "success", { at: "2026-08-11T16:00:00Z" }),
185 ]);
186
187 expect([undated[0]?.status, dated[0]?.status]).toEqual(["success", "success"]);
188 });
189
190 it("takes the last copy when neither run is dated, which is how an update is listed", () => {
191 const checks = dedupeChecks([entry("build", "pending"), entry("build", "failure")]);
192
193 expect(checks.map((check) => check.status)).toEqual(["failure"]);
194 });
195
196 it("keeps two workflows that name a job the same thing, and says which is which", () => {
197 const checks = dedupeChecks([
198 entry("build", "success", { workflowName: "CI", at: "2026-08-11T16:00:00Z" }),
199 entry("build", "failure", { workflowName: "Release", at: "2026-08-11T16:00:00Z" }),
200 ]);
201
202 expect(checks.map((check) => [check.name, check.status])).toEqual([
203 ["Release / build", "failure"],
204 ["CI / build", "success"],
205 ]);
206 });
207
208 it("leaves a colliding check with no workflow of its own unqualified", () => {
209 // An app-provided check run belongs to no workflow, which GitHub reports as an empty name.
210 const checks = dedupeChecks([
211 entry("build", "success", { workflowName: "", at: "2026-08-11T16:00:00Z" }),
212 entry("build", "failure", { workflowName: "CI", at: "2026-08-11T16:00:00Z" }),
213 ]);
214
215 expect(checks.map((check) => check.name)).toEqual(["CI / build", "build"]);
216 });
217});