Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestChecks.test.ts

219 lines7.6 KB

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