Open History to see the last commit
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 };
41}
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 const listWorkflowRuns = () => {};
59 const listPullRequestsAssociatedWithCommit = () => {};
60 const github = {
61 paginate: async (method, input) => {
62 if (method === listPullRequestsAssociatedWithCommit) {
63 return [
64 {
65 number: 5350,
66 state: "open",
67 head: { sha: "head-sha", ref: "feature-branch", repo: null },
68 },
69 ];
70 }
71 if (method === listWorkflowRunArtifacts) {
72 return [
73 {
74 name: "thread-transfer-results",
75 expired: false,
76 runId: input.run_id,
77 },
78 ];
79 }
80 if (method === listWorkflowRuns) {
81 return [{ id: 1, head_sha: "base-sha" }];
82 }
83 throw new Error("unexpected pagination call");
84 },
85 rest: {
86 actions: { listWorkflowRunArtifacts, listWorkflowRuns },
87 pulls: {
88 get: async () => ({
89 data: {
90 head: { sha: "head-sha" },
91 base: { sha: "base-sha", ref: "main" },
92 },
93 }),
94 },
95 repos: { listPullRequestsAssociatedWithCommit },
96 },
97 };
98 await resolve({
99 github,
100 context: {
101 repo: { owner: "pingdotgg", repo: "t3code" },
102 payload: {
103 workflow_run: {
104 id: 2,
105 event: "pull_request",
106 workflow_id: 3,
107 head_sha: "head-sha",
108 head_branch: "feature-branch",
109 head_repository: { full_name: "pingdotgg/t3code" },
110 conclusion: "success",
111 pull_requests: [],
112 },
113 },
114 },
115 core: {
116 info: () => {},
117 setOutput: (key, value) => {
118 outputs[key] = value;
119 },
120 },
121 });
122
123 assert.equal(outputs.publish, "true");
124 assert.equal(outputs.pull_number, "5350");
125 assert.equal(outputs.pr_artifact, "true");
126 assert.equal(outputs.baseline_run_id, "1");
127 assert.equal(outputs.baseline_matches_base, "true");
128});
129
130test("does not guess when a fallback commit belongs to multiple PRs", async () => {
131 const outputs = {};
132 const listPullRequestsAssociatedWithCommit = () => {};
133 let fetchedPull = false;
134 await resolve({
135 github: {
136 paginate: async (method) => {
137 assert.equal(method, listPullRequestsAssociatedWithCommit);
138 return [5350, 5351].map((number) => ({
139 number,
140 state: "open",
141 head: {
142 sha: "head-sha",
143 ref: "feature-branch",
144 repo: { full_name: "pingdotgg/t3code" },
145 },
146 }));
147 },
148 rest: {
149 actions: {},
150 pulls: {
151 get: async () => {
152 fetchedPull = true;
153 },
154 },
155 repos: { listPullRequestsAssociatedWithCommit },
156 },
157 },
158 context: {
159 repo: { owner: "pingdotgg", repo: "t3code" },
160 payload: {
161 workflow_run: {
162 id: 2,
163 event: "pull_request",
164 workflow_id: 3,
165 head_sha: "head-sha",
166 head_branch: "feature-branch",
167 head_repository: { full_name: "pingdotgg/t3code" },
168 conclusion: "success",
169 pull_requests: [],
170 },
171 },
172 },
173 core: {
174 info: () => {},
175 setOutput: (key, value) => {
176 outputs[key] = value;
177 },
178 },
179 });
180
181 assert.equal(outputs.publish, "false");
182 assert.equal(fetchedPull, false);
183});
184
185test("skips a missing PR commit but preserves other lookup failures", async () => {
186 for (const [status, message, shouldSkip] of [
187 [422, "No commit found for SHA: old-head", true],
188 [422, "Validation Failed", false],
189 [403, "API rate limit exceeded", false],
190 ]) {
191 const outputs = {};
192 const error = Object.assign(new Error(message), {
193 status,
194 response: { data: { message } },
195 });
196 const run = resolve({
197 github: {
198 paginate: async () => {
199 throw error;
200 },
201 rest: { repos: { listPullRequestsAssociatedWithCommit: () => {} } },
202 },
203 context: {
204 repo: { owner: "pingdotgg", repo: "t3code" },
205 payload: {
206 workflow_run: { event: "pull_request", head_sha: "old-head", pull_requests: [] },
207 },
208 },
209 core: {
210 info: () => {},
211 setOutput: (key, value) => {
212 outputs[key] = value;
213 },
214 },
215 });
216 if (shouldSkip) {
217 await run;
218 assert.equal(outputs.publish, "false");
219 } else {
220 await assert.rejects(run, (caught) => caught === error);
221 assert.deepEqual(outputs, {});
222 }
223 }
224});
225
226test("does not report a passing run that skipped the server tests", async () => {
227 const outputs = {};
228 const listWorkflowRunArtifacts = () => {};
229 await resolve({
230 github: {
231 paginate: async (method) => {
232 assert.equal(method, listWorkflowRunArtifacts);
233 return [];
234 },
235 rest: {
236 actions: { listWorkflowRunArtifacts },
237 pulls: {
238 get: async () => ({
239 data: { head: { sha: "head-sha" }, base: { sha: "base-sha", ref: "main" } },
240 }),
241 },
242 },
243 },
244 context: {
245 repo: { owner: "pingdotgg", repo: "t3code" },
246 payload: {
247 workflow_run: {
248 id: 2,
249 event: "pull_request",
250 workflow_id: 3,
251 head_sha: "head-sha",
252 conclusion: "success",
253 pull_requests: [{ number: 5350 }],
254 },
255 },
256 },
257 core: {
258 info: () => {},
259 setOutput: (key, value) => {
260 outputs[key] = value;
261 },
262 },
263 });
264
265 assert.equal(outputs.publish, "false");
266});
267
268test("does not publish a stale result after the PR head advances", async () => {
269 let listedComments = false;
270 const info = [];
271 const published = await upsertCommentForCurrentHead(
272 {
273 paginate: async () => {
274 listedComments = true;
275 return [];
276 },
277 rest: {
278 issues: {
279 listComments: () => {},
280 createComment: () => {
281 throw new Error("must not create a stale comment");
282 },
283 updateComment: () => {
284 throw new Error("must not update a stale comment");
285 },
286 },
287 pulls: {
288 get: async () => ({ data: { head: { sha: "new-head-sha" } } }),
289 },
290 },
291 },
292 { repo: { owner: "pingdotgg", repo: "t3code" } },
293 { info: (message) => info.push(message) },
294 5350,
295 "old-head-sha",
296 "stale body",
297 );
298
299 assert.equal(published, false);
300 assert.equal(listedComments, false);
301 assert.deepEqual(info, ["Skipping stale CI result old-head-sha; PR head is new-head-sha."]);
302});
303
304test("preserves a successful result when a same-SHA rerun has no artifact", async () => {
305 let updatedComment = false;
306 const sha = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
307 const published = await upsertCommentForCurrentHead(
308 {
309 paginate: async () => [
310 {
311 id: 1,
312 user: { login: "github-actions[bot]" },
313 body: `<!-- t3-thread-transfer-report -->\n<!-- t3-thread-transfer-result-sha:${sha} -->`,
314 },
315 ],
316 rest: {
317 issues: {
318 listComments: () => {},
319 createComment: () => {
320 updatedComment = true;
321 },
322 updateComment: () => {
323 updatedComment = true;
324 },
325 },
326 pulls: {
327 get: async () => ({ data: { head: { sha } } }),
328 },
329 },
330 },
331 { repo: { owner: "pingdotgg", repo: "t3code" } },
332 { info: () => {} },
333 5350,
334 sha,
335 "missing artifact warning",
336 { preserveResultSha: sha },
337 );
338
339 assert.equal(published, true);
340 assert.equal(updatedComment, false);
341});