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 // regression: #20094 runs were compared as timestamp text, picking the older run
24 it.each([
25 ["2026-10-16T12:00:00+02:00", "2026-10-16T10:30:00Z"],
26 ["2026-10-16T12:00:00.00009Z", "2026-10-16T12:00:00.0001Z"],
27 ["2024-03-01T01:00:00Z", "2024-02-29T23:30:00-02:00"],
28 ])("keeps the newer instant between %s and %s in either listing order", (older, newer) => {
29 const oldRun = entry("build", "success", { at: older });
30 const newRun = entry("build", "failure", { at: newer });
31
32 expect(
33 [
34 [oldRun, newRun],
35 [newRun, oldRun],
36 ].map((runs) => dedupeChecks(runs)[0]?.status),
37 ).toEqual(["failure", "failure"]);
38 });
39
40 it.each([
41 ["2026-10-16T12:00:00Z", "2026-10-16T14:00:00+02:00"],
42 ["2026-10-16T24:00:00.000Z", "2026-10-17T00:00:00Z"],
43 ["2026-10-16T12:00Z", "2026-10-16T12:00:00Z"],
44 ])("takes the last copy for equal instants %s and %s", (first, second) => {
45 const forward = dedupeChecks([
46 entry("build", "success", { at: first }),
47 entry("build", "failure", { at: second }),
48 ]);
49 const reversed = dedupeChecks([
50 entry("build", "success", { at: second }),
51 entry("build", "failure", { at: first }),
52 ]);
53
54 expect([forward[0]?.status, reversed[0]?.status]).toEqual(["failure", "failure"]);
55 });
56
57 it.each([
58 ["2026-10-16", "2026-10-15T23:00:00-02:00", "success"],
59 ["2026-10-16T00:00:00", "2026-10-15T23:00:00-02:00", "success"],
60 ["2026-10-16T24:00:00.0001Z", "2026-10-17T00:00:00Z", "failure"],
61 ])("uses text fallback for incomplete or invalid timestamp %s", (malformed, valid, expected) => {
62 const malformedRun = entry("build", "success", { at: malformed });
63 const validRun = entry("build", "failure", { at: valid });
64
65 expect(
66 [
67 [malformedRun, validRun],
68 [validRun, malformedRun],
69 ].map((runs) => dedupeChecks(runs)[0]?.status),
70 ).toEqual([expected, expected]);
71 });
72
73 it.each([
74 ["2026-02-30T00:00:00Z", "2026-03-01T00:00:00Z"],
75 ["1900-02-29T00:00:00Z", "1900-03-01T00:00:00Z"],
76 ["2026-04-31T00:00:00.0001-02:00", "2026-05-01T00:00:00Z"],
77 ])("uses text fallback for impossible date %s", (malformed, valid) => {
78 const malformedRun = entry("build", "success", { at: malformed });
79 const validRun = entry("build", "failure", { at: valid });
80
81 expect(
82 [
83 [malformedRun, validRun],
84 [validRun, malformedRun],
85 ].map((runs) => dedupeChecks(runs)[0]?.status),
86 ).toEqual(["failure", "failure"]);
87 });
88
89 it("holds a check at the place it first appeared, so a re-run does not reshuffle the list", () => {
90 const checks = dedupeChecks([
91 entry("lint", "success", { workflowName: "CI", at: "2026-08-11T16:00:00Z" }),
92 entry("test", "success", { workflowName: "CI", at: "2026-08-11T16:00:00Z" }),
93 entry("lint", "failure", { workflowName: "CI", at: "2026-08-11T18:00:00Z" }),
94 ]);
95
96 expect(checks.map((check) => [check.name, check.status])).toEqual([
97 ["lint", "failure"],
98 ["test", "success"],
99 ]);
100 });
101
102 it("puts failed and cancelled checks first, preserving each group's order after reruns", () => {
103 const checks = dedupeChecks([
104 entry("lint", "failure", { at: "2026-08-11T16:00:00Z" }),
105 entry("integration", "cancelled"),
106 entry("types", "pending"),
107 entry("unit", "failure"),
108 entry("format", "success"),
109 entry("lint", "success", { at: "2026-08-11T18:00:00Z" }),
110 ]);
111
112 expect(checks.map((check) => [check.name, check.status])).toEqual([
113 ["integration", "cancelled"],
114 ["unit", "failure"],
115 ["lint", "success"],
116 ["types", "pending"],
117 ["format", "success"],
118 ]);
119 });
120
121 it("keeps distinct workflow and check pairs even when their names join to the same text", () => {
122 const checks = dedupeChecks([
123 entry("Linux tests", "success", {
124 workflowName: "Build",
125 at: "2026-08-11T16:00:00Z",
126 }),
127 entry("tests", "success", {
128 workflowName: "Build Linux",
129 at: "2026-08-11T17:00:00Z",
130 }),
131 entry("Linux tests", "failure", {
132 workflowName: "Build",
133 at: "2026-08-11T18:00:00Z",
134 }),
135 ]);
136
137 expect(checks.map((check) => [check.name, check.status])).toEqual([
138 ["Linux tests", "failure"],
139 ["tests", "success"],
140 ]);
141 });
142
143 it("loses a run that never said when it happened to one that did, whichever came first", () => {
144 const undated = dedupeChecks([
145 entry("build", "success", { at: "2026-08-11T16:00:00Z" }),
146 entry("build", "pending"),
147 ]);
148 const dated = dedupeChecks([
149 entry("build", "pending"),
150 entry("build", "success", { at: "2026-08-11T16:00:00Z" }),
151 ]);
152
153 expect([undated[0]?.status, dated[0]?.status]).toEqual(["success", "success"]);
154 });
155
156 it("takes the last copy when neither run is dated, which is how an update is listed", () => {
157 const checks = dedupeChecks([entry("build", "pending"), entry("build", "failure")]);
158
159 expect(checks.map((check) => check.status)).toEqual(["failure"]);
160 });
161
162 it("keeps two workflows that name a job the same thing, and says which is which", () => {
163 const checks = dedupeChecks([
164 entry("build", "success", { workflowName: "CI", at: "2026-08-11T16:00:00Z" }),
165 entry("build", "failure", { workflowName: "Release", at: "2026-08-11T16:00:00Z" }),
166 ]);
167
168 expect(checks.map((check) => [check.name, check.status])).toEqual([
169 ["Release / build", "failure"],
170 ["CI / build", "success"],
171 ]);
172 });
173
174 it("leaves a colliding check with no workflow of its own unqualified", () => {
175 // An app-provided check run belongs to no workflow, which GitHub reports as an empty name.
176 const checks = dedupeChecks([
177 entry("build", "success", { workflowName: "", at: "2026-08-11T16:00:00Z" }),
178 entry("build", "failure", { workflowName: "CI", at: "2026-08-11T16:00:00Z" }),
179 ]);
180
181 expect(checks.map((check) => check.name)).toEqual(["CI / build", "build"]);
182 });
183});