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

thread-transfer-report.test.cjs

349 lines9.3 KB

1const assert = require("node:assert/strict");
2const test = require("node:test");
3​
4const {
5 resolve,
6 upsertCommentForCurrentHead,
7 validateResult,
8} = require("./thread-transfer-report.cjs");
9​
10function result(overrides = {}) {
11 const observed = {
12 totalWireBytes: 2_200_000,
13 threadSnapshotWireBytes: 1_950_000,
14 threadSnapshotDecodedBytes: 9_100_000,
15 measuredTurnWebSocketWireBytes: 250_000,
16 measuredTurnWebSocketDecodedBytes: 1_150_000,
17 measuredTurnWebSocketMessages: 15,
18 };
19 const ceiling = {
20 totalWireBytes: 2_900_000,
21 threadSnapshotWireBytes: 2_600_000,
22 measuredTurnWebSocketWireBytes: 320_000,
23 measuredTurnWebSocketDecodedBytes: 1_550_000,
24 measuredTurnWebSocketMessages: 20,
25 };
26 return {
27 schemaVersion: 1,
28 scenario: {
29 id: "thread-transfer-v1",
30 historyTurns: 10,
31 historyCommandToolsPerTurn: 5,
32 historyMcpResultBytes: 900_000,
33 measuredCommandTools: 20,
34 measuredMcpResultBytes: 1_100_000,
35 },
36 providers: {
37 codex: { observed: { ...observed, ...overrides }, ceiling },
38 claudeAgent: { observed, ceiling },
39 },
40 };
42​
43test("validates the fixed artifact schema", () => {
44 assert.equal(validateResult(result()).schemaVersion, 1);
45 assert.throws(
46 () => validateResult({ ...result(), injectedMarkdown: "@everyone" }),
47 /unexpected fields/,
48 );
49 assert.throws(
50 () => validateResult(result({ totalWireBytes: "lots" })),
51 /non-negative safe integer/,
52 );
53});
54​
55test("resolves a fallback PR with a redacted head repo and exact main baseline", async () => {
56 const outputs = {};
57 const listWorkflowRunArtifacts = () => {};
58 // The exact base run sits far past the newest page of main CI runs.
59 const mainRuns = Array.from({ length: 1_000 }, (_, index) => ({
60 id: 1_000 - index,
61 head_sha: index === 900 ? "base-sha" : `main-sha-${index}`,
62 }));
63 const listWorkflowRuns = async ({ head_sha, per_page }) => ({
64 data: {
65 workflow_runs: mainRuns
66 .filter((run) => head_sha === undefined || run.head_sha === head_sha)
67 .slice(0, per_page),
68 },
69 });
70 const listPullRequestsAssociatedWithCommit = () => {};
71 const github = {
72 paginate: async (method, input) => {
73 if (method === listPullRequestsAssociatedWithCommit) {
74 return [
75 {
76 number: 5350,
77 state: "open",
78 head: { sha: "head-sha", ref: "feature-branch", repo: null },
79 },
80 ];
81 }
82 if (method === listWorkflowRunArtifacts) {
83 return [
84 {
85 name: "thread-transfer-results",
86 expired: false,
87 runId: input.run_id,
88 },
89 ];
90 }
91 throw new Error("unexpected pagination call");
92 },
93 rest: {
94 actions: { listWorkflowRunArtifacts, listWorkflowRuns },
95 pulls: {
96 get: async () => ({
97 data: {
98 head: { sha: "head-sha" },
99 base: { sha: "base-sha", ref: "main" },
100 },
101 }),
102 },
103 repos: { listPullRequestsAssociatedWithCommit },
104 },
105 };
106 await resolve({
107 github,
108 context: {
109 repo: { owner: "pingdotgg", repo: "t3code" },
110 payload: {
111 workflow_run: {
112 id: 2,
113 event: "pull_request",
114 workflow_id: 3,
115 head_sha: "head-sha",
116 head_branch: "feature-branch",
117 head_repository: { full_name: "pingdotgg/t3code" },
118 conclusion: "success",
119 pull_requests: [],
120 },
121 },
122 },
123 core: {
124 info: () => {},
125 setOutput: (key, value) => {
126 outputs[key] = value;
127 },
128 },
129 });
130​
131 assert.equal(outputs.publish, "true");
132 assert.equal(outputs.pull_number, "5350");
133 assert.equal(outputs.pr_artifact, "true");
134 assert.equal(outputs.baseline_run_id, "100");
135 assert.equal(outputs.baseline_matches_base, "true");
136});
137​
138test("does not guess when a fallback commit belongs to multiple PRs", async () => {
139 const outputs = {};
140 const listPullRequestsAssociatedWithCommit = () => {};
141 let fetchedPull = false;
142 await resolve({
143 github: {
144 paginate: async (method) => {
145 assert.equal(method, listPullRequestsAssociatedWithCommit);
146 return [5350, 5351].map((number) => ({
147 number,
148 state: "open",
149 head: {
150 sha: "head-sha",
151 ref: "feature-branch",
152 repo: { full_name: "pingdotgg/t3code" },
153 },
154 }));
155 },
156 rest: {
157 actions: {},
158 pulls: {
159 get: async () => {
160 fetchedPull = true;
161 },
162 },
163 repos: { listPullRequestsAssociatedWithCommit },
164 },
165 },
166 context: {
167 repo: { owner: "pingdotgg", repo: "t3code" },
168 payload: {
169 workflow_run: {
170 id: 2,
171 event: "pull_request",
172 workflow_id: 3,
173 head_sha: "head-sha",
174 head_branch: "feature-branch",
175 head_repository: { full_name: "pingdotgg/t3code" },
176 conclusion: "success",
177 pull_requests: [],
178 },
179 },
180 },
181 core: {
182 info: () => {},
183 setOutput: (key, value) => {
184 outputs[key] = value;
185 },
186 },
187 });
188​
189 assert.equal(outputs.publish, "false");
190 assert.equal(fetchedPull, false);
191});
192​
193test("skips a missing PR commit but preserves other lookup failures", async () => {
194 for (const [status, message, shouldSkip] of [
195 [422, "No commit found for SHA: old-head", true],
196 [422, "Validation Failed", false],
197 [403, "API rate limit exceeded", false],
198 ]) {
199 const outputs = {};
200 const error = Object.assign(new Error(message), {
201 status,
202 response: { data: { message } },
203 });
204 const run = resolve({
205 github: {
206 paginate: async () => {
207 throw error;
208 },
209 rest: { repos: { listPullRequestsAssociatedWithCommit: () => {} } },
210 },
211 context: {
212 repo: { owner: "pingdotgg", repo: "t3code" },
213 payload: {
214 workflow_run: { event: "pull_request", head_sha: "old-head", pull_requests: [] },
215 },
216 },
217 core: {
218 info: () => {},
219 setOutput: (key, value) => {
220 outputs[key] = value;
221 },
222 },
223 });
224 if (shouldSkip) {
225 await run;
226 assert.equal(outputs.publish, "false");
227 } else {
228 await assert.rejects(run, (caught) => caught === error);
229 assert.deepEqual(outputs, {});
230 }
231 }
232});
233​
234test("does not report a passing run that skipped the server tests", async () => {
235 const outputs = {};
236 const listWorkflowRunArtifacts = () => {};
237 await resolve({
238 github: {
239 paginate: async (method) => {
240 assert.equal(method, listWorkflowRunArtifacts);
241 return [];
242 },
243 rest: {
244 actions: { listWorkflowRunArtifacts },
245 pulls: {
246 get: async () => ({
247 data: { head: { sha: "head-sha" }, base: { sha: "base-sha", ref: "main" } },
248 }),
249 },
250 },
251 },
252 context: {
253 repo: { owner: "pingdotgg", repo: "t3code" },
254 payload: {
255 workflow_run: {
256 id: 2,
257 event: "pull_request",
258 workflow_id: 3,
259 head_sha: "head-sha",
260 conclusion: "success",
261 pull_requests: [{ number: 5350 }],
262 },
263 },
264 },
265 core: {
266 info: () => {},
267 setOutput: (key, value) => {
268 outputs[key] = value;
269 },
270 },
271 });
272​
273 assert.equal(outputs.publish, "false");
274});
275​
276test("does not publish a stale result after the PR head advances", async () => {
277 let listedComments = false;
278 const info = [];
279 const published = await upsertCommentForCurrentHead(
280 {
281 paginate: async () => {
282 listedComments = true;
283 return [];
284 },
285 rest: {
286 issues: {
287 listComments: () => {},
288 createComment: () => {
289 throw new Error("must not create a stale comment");
290 },
291 updateComment: () => {
292 throw new Error("must not update a stale comment");
293 },
294 },
295 pulls: {
296 get: async () => ({ data: { head: { sha: "new-head-sha" } } }),
297 },
298 },
299 },
300 { repo: { owner: "pingdotgg", repo: "t3code" } },
301 { info: (message) => info.push(message) },
302 5350,
303 "old-head-sha",
304 "stale body",
305 );
306​
307 assert.equal(published, false);
308 assert.equal(listedComments, false);
309 assert.deepEqual(info, ["Skipping stale CI result old-head-sha; PR head is new-head-sha."]);
310});
311​
312test("preserves a successful result when a same-SHA rerun has no artifact", async () => {
313 let updatedComment = false;
314 const sha = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
315 const published = await upsertCommentForCurrentHead(
316 {
317 paginate: async () => [
318 {
319 id: 1,
320 user: { login: "github-actions[bot]" },
321 body: `<!-- t3-thread-transfer-report -->\n<!-- t3-thread-transfer-result-sha:${sha} -->`,
322 },
323 ],
324 rest: {
325 issues: {
326 listComments: () => {},
327 createComment: () => {
328 updatedComment = true;
329 },
330 updateComment: () => {
331 updatedComment = true;
332 },
333 },
334 pulls: {
335 get: async () => ({ data: { head: { sha } } }),
336 },
337 },
338 },
339 { repo: { owner: "pingdotgg", repo: "t3code" } },
340 { info: () => {} },
341 5350,
342 sha,
343 "missing artifact warning",
344 { preserveResultSha: sha },
345 );
346​
347 assert.equal(published, true);
348 assert.equal(updatedComment, false);
349});