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

pullRequestChecks.test.ts

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