Skip to content

Commit c701740

Browse files
committed
test: split apply policy tests
1 parent 929e605 commit c701740

5 files changed

Lines changed: 1935 additions & 1937 deletions

File tree

Lines changed: 384 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,384 @@
1+
import assert from "node:assert/strict";
2+
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3+
import { join } from "node:path";
4+
import test from "node:test";
5+
6+
import {
7+
implementedCloseReport,
8+
reportWithSyncedReviewComment,
9+
runApplyDecisionsForTest,
10+
tmpPrefix,
11+
withMockGh,
12+
} from "./helpers.ts";
13+
14+
test("apply-decisions retries legacy fixed close skips", () => {
15+
for (const actionTaken of ["skipped_maintainer_authored", "skipped_invalid_decision"]) {
16+
const root = mkdtempSync(tmpPrefix);
17+
try {
18+
const itemsDir = join(root, "items");
19+
const closedDir = join(root, "closed");
20+
const plansDir = join(root, "plans");
21+
const reportPath = join(root, "apply-report.json");
22+
const logPath = join(root, "gh.log");
23+
mkdirSync(itemsDir, { recursive: true });
24+
mkdirSync(plansDir, { recursive: true });
25+
const closeReport = implementedCloseReport({
26+
type: "pull_request",
27+
action_taken: actionTaken,
28+
author_association: "MEMBER",
29+
labels: JSON.stringify(["maintainer"]),
30+
}).replace(
31+
"## Close Comment\n\nClosing this because the requested behavior is already on main.\n",
32+
"## Close Comment\n\n_No close comment posted._\n",
33+
);
34+
const synced = reportWithSyncedReviewComment(closeReport, 321, "implemented_on_main");
35+
writeFileSync(join(itemsDir, "321.md"), synced.report, "utf8");
36+
37+
const ghMock = `
38+
const { appendFileSync } = require("fs");
39+
const logPath = ${JSON.stringify(logPath)};
40+
const comment = ${JSON.stringify(synced.comment)};
41+
const rawArgs = process.argv.slice(2);
42+
const args = rawArgs[0] === "--repo" ? rawArgs.slice(2) : rawArgs;
43+
appendFileSync(logPath, JSON.stringify(args) + "\\n");
44+
const path = args[1] || "";
45+
if (args[0] === "api" && args[1] === "-i" && /\\/issues\\/321\\/timeline(?:\\?|$)/.test(args[2] || "")) {
46+
console.log("HTTP/2 200\\n\\n[]");
47+
} else if (args[0] === "api" && /\\/issues\\/321\\/comments(?:\\?|$)/.test(path)) {
48+
console.log(JSON.stringify([[{
49+
id: 9321,
50+
html_url: "https://github.com/openclaw/clawsweeper/pull/321#issuecomment-9321",
51+
created_at: "2026-05-01T01:00:00Z",
52+
updated_at: "2026-05-01T01:00:00Z",
53+
user: { login: "clawsweeper[bot]" },
54+
body: comment
55+
}]]));
56+
} else if (args[0] === "api" && /\\/issues\\/321\\/timeline(?:\\?|$)/.test(path)) {
57+
console.log(JSON.stringify([[]]));
58+
} else if (args[0] === "api" && /\\/issues\\/321$/.test(path)) {
59+
if (args.includes("--method") && args.includes("PATCH")) {
60+
console.log(JSON.stringify({ state: "closed" }));
61+
} else {
62+
console.log(JSON.stringify({
63+
number: 321,
64+
title: "Render work plans",
65+
html_url: "https://github.com/openclaw/clawsweeper/pull/321",
66+
created_at: "2026-05-01T00:00:00Z",
67+
updated_at: "2026-05-01T00:00:00Z",
68+
closed_at: null,
69+
state: "open",
70+
locked: false,
71+
active_lock_reason: null,
72+
author_association: "MEMBER",
73+
user: { login: "maintainer" },
74+
labels: ["maintainer"],
75+
comments: 1,
76+
pull_request: { url: "https://api.github.com/repos/openclaw/clawsweeper/pulls/321" }
77+
}));
78+
}
79+
} else if (args[0] === "issue" && args[1] === "view") {
80+
console.log(JSON.stringify({ closedByPullRequestsReferences: [] }));
81+
} else if (args[0] === "api" && /\\/pulls\\/321$/.test(path)) {
82+
console.log(JSON.stringify({
83+
number: 321,
84+
html_url: "https://github.com/openclaw/clawsweeper/pull/321",
85+
state: "open",
86+
changed_files: 0,
87+
commits: 0,
88+
review_comments: 0,
89+
head: { sha: "head-sha", ref: "branch", repo: { full_name: "fork/clawsweeper" } },
90+
base: { sha: "base-sha", ref: "main", repo: { full_name: "openclaw/clawsweeper" } },
91+
user: { login: "maintainer" }
92+
}));
93+
} else if (args[0] === "api" && /\\/pulls\\/321\\/(files|commits|comments)(?:\\?|$)/.test(path)) {
94+
console.log(JSON.stringify([[]]));
95+
} else if (args[0] === "label" || args[0] === "issue") {
96+
console.log("");
97+
} else {
98+
console.error("unexpected gh args", JSON.stringify(args));
99+
process.exit(1);
100+
}
101+
`;
102+
withMockGh(root, ghMock, () => {
103+
runApplyDecisionsForTest({
104+
itemsDir,
105+
closedDir,
106+
plansDir,
107+
reportPath,
108+
extraArgs: ["--dry-run", "--apply-kind", "all", "--processed-limit", "2"],
109+
});
110+
});
111+
112+
assert.deepEqual(JSON.parse(readFileSync(reportPath, "utf8")), [
113+
{
114+
number: 321,
115+
action: "review_comment_synced",
116+
reason: "would update durable Codex review comment",
117+
},
118+
{
119+
number: 321,
120+
action: "closed",
121+
reason:
122+
"dry-run: would close as already implemented on main; dry-run: would post close-applied comment",
123+
},
124+
]);
125+
} finally {
126+
rmSync(root, { recursive: true, force: true });
127+
}
128+
}
129+
});
130+
131+
test("apply-decisions retries legacy kept-open close reports", () => {
132+
const root = mkdtempSync(tmpPrefix);
133+
try {
134+
const itemsDir = join(root, "items");
135+
const closedDir = join(root, "closed");
136+
const plansDir = join(root, "plans");
137+
const reportPath = join(root, "apply-report.json");
138+
mkdirSync(itemsDir, { recursive: true });
139+
mkdirSync(plansDir, { recursive: true });
140+
const closeReport = implementedCloseReport({
141+
type: "pull_request",
142+
action_taken: "kept_open",
143+
});
144+
const synced = reportWithSyncedReviewComment(closeReport, 321, "implemented_on_main");
145+
writeFileSync(join(itemsDir, "321.md"), synced.report, "utf8");
146+
147+
const ghMock = `
148+
const comment = ${JSON.stringify(synced.comment)};
149+
const rawArgs = process.argv.slice(2);
150+
const args = rawArgs[0] === "--repo" ? rawArgs.slice(2) : rawArgs;
151+
const path = args[1] || "";
152+
if (args[0] === "api" && args[1] === "-i" && /\\/issues\\/321\\/timeline(?:\\?|$)/.test(args[2] || "")) {
153+
console.log("HTTP/2 200\\n\\n[]");
154+
} else if (args[0] === "api" && /\\/issues\\/321\\/comments(?:\\?|$)/.test(path)) {
155+
console.log(JSON.stringify([[{
156+
id: 9321,
157+
html_url: "https://github.com/openclaw/clawsweeper/pull/321#issuecomment-9321",
158+
created_at: "2026-05-01T01:00:00Z",
159+
updated_at: "2026-05-01T01:00:00Z",
160+
user: { login: "clawsweeper[bot]" },
161+
body: comment
162+
}]]));
163+
} else if (args[0] === "api" && /\\/issues\\/321\\/timeline(?:\\?|$)/.test(path)) {
164+
console.log(JSON.stringify([[]]));
165+
} else if (args[0] === "api" && /\\/issues\\/321$/.test(path)) {
166+
console.log(JSON.stringify({
167+
number: 321,
168+
title: "Render work plans",
169+
html_url: "https://github.com/openclaw/clawsweeper/pull/321",
170+
created_at: "2026-05-01T00:00:00Z",
171+
updated_at: "2026-05-01T00:00:00Z",
172+
closed_at: null,
173+
state: "open",
174+
locked: false,
175+
active_lock_reason: null,
176+
author_association: "CONTRIBUTOR",
177+
user: { login: "reporter" },
178+
labels: [],
179+
comments: 1,
180+
pull_request: { url: "https://api.github.com/repos/openclaw/clawsweeper/pulls/321" }
181+
}));
182+
} else if (args[0] === "issue" && args[1] === "view") {
183+
console.log(JSON.stringify({ closedByPullRequestsReferences: [] }));
184+
} else if (args[0] === "api" && /\\/pulls\\/321$/.test(path)) {
185+
console.log(JSON.stringify({
186+
number: 321,
187+
html_url: "https://github.com/openclaw/clawsweeper/pull/321",
188+
state: "open",
189+
changed_files: 0,
190+
commits: 0,
191+
review_comments: 0,
192+
head: { sha: "head-sha", ref: "branch", repo: { full_name: "fork/clawsweeper" } },
193+
base: { sha: "base-sha", ref: "main", repo: { full_name: "openclaw/clawsweeper" } },
194+
user: { login: "reporter" }
195+
}));
196+
} else if (args[0] === "api" && /\\/pulls\\/321\\/(files|commits|comments)(?:\\?|$)/.test(path)) {
197+
console.log(JSON.stringify([[]]));
198+
} else if (args[0] === "label" || args[0] === "issue") {
199+
console.log("");
200+
} else {
201+
console.error("unexpected gh args", JSON.stringify(args));
202+
process.exit(1);
203+
}
204+
`;
205+
withMockGh(root, ghMock, () => {
206+
runApplyDecisionsForTest({
207+
itemsDir,
208+
closedDir,
209+
plansDir,
210+
reportPath,
211+
extraArgs: ["--dry-run", "--apply-kind", "all", "--processed-limit", "2"],
212+
});
213+
});
214+
215+
assert.deepEqual(JSON.parse(readFileSync(reportPath, "utf8")), [
216+
{
217+
number: 321,
218+
action: "review_comment_synced",
219+
reason: "would update durable Codex review comment",
220+
},
221+
{
222+
number: 321,
223+
action: "closed",
224+
reason:
225+
"dry-run: would close as already implemented on main; dry-run: would post close-applied comment",
226+
},
227+
]);
228+
} finally {
229+
rmSync(root, { recursive: true, force: true });
230+
}
231+
});
232+
233+
test("apply-decisions pair-closes issues blocked by closeable linked PRs", () => {
234+
const root = mkdtempSync(tmpPrefix);
235+
try {
236+
const itemsDir = join(root, "items");
237+
const closedDir = join(root, "closed");
238+
const plansDir = join(root, "plans");
239+
const reportPath = join(root, "apply-report.json");
240+
mkdirSync(itemsDir, { recursive: true });
241+
mkdirSync(plansDir, { recursive: true });
242+
const issueSynced = reportWithSyncedReviewComment(
243+
implementedCloseReport({
244+
repository: "openclaw/openclaw",
245+
number: 320,
246+
type: "issue",
247+
title: "Issue fixed by main",
248+
action_taken: "skipped_open_closing_pr",
249+
}),
250+
320,
251+
"implemented_on_main",
252+
);
253+
const pullSynced = reportWithSyncedReviewComment(
254+
implementedCloseReport({
255+
repository: "openclaw/openclaw",
256+
number: 321,
257+
type: "pull_request",
258+
title: "Obsolete linked PR",
259+
action_taken: "kept_open",
260+
}),
261+
321,
262+
"implemented_on_main",
263+
);
264+
writeFileSync(join(itemsDir, "320.md"), issueSynced.report, "utf8");
265+
writeFileSync(join(itemsDir, "321.md"), pullSynced.report, "utf8");
266+
267+
const ghMock = `
268+
const comments = {
269+
320: ${JSON.stringify(issueSynced.comment)},
270+
321: ${JSON.stringify(pullSynced.comment)}
271+
};
272+
const rawArgs = process.argv.slice(2);
273+
const args = rawArgs[0] === "--repo" ? rawArgs.slice(2) : rawArgs;
274+
const path = args[1] || "";
275+
const issueNumber = (path.match(/\\/issues\\/(\\d+)/) || [])[1];
276+
if (args[0] === "api" && args[1] === "-i" && /\\/issues\\/(320|321)\\/timeline(?:\\?|$)/.test(args[2] || "")) {
277+
console.log("HTTP/2 200\\n\\n[]");
278+
} else if (args[0] === "api" && /\\/issues\\/(320|321)\\/comments(?:\\?|$)/.test(path)) {
279+
const number = Number(issueNumber);
280+
console.log(JSON.stringify([[{
281+
id: 9000 + number,
282+
html_url: "https://github.com/openclaw/clawsweeper/issues/" + number + "#issuecomment-" + (9000 + number),
283+
created_at: "2026-05-01T01:00:00Z",
284+
updated_at: "2026-05-01T01:00:00Z",
285+
user: { login: "clawsweeper[bot]" },
286+
body: comments[number]
287+
}]]));
288+
} else if (args[0] === "api" && /\\/issues\\/(320|321)\\/timeline(?:\\?|$)/.test(path)) {
289+
console.log(JSON.stringify([[]]));
290+
} else if (args[0] === "api" && /\\/issues\\/320$/.test(path)) {
291+
console.log(JSON.stringify({
292+
number: 320,
293+
title: "Issue fixed by main",
294+
html_url: "https://github.com/openclaw/clawsweeper/issues/320",
295+
created_at: "2026-05-01T00:00:00Z",
296+
updated_at: "2026-05-01T00:00:00Z",
297+
closed_at: null,
298+
state: "open",
299+
locked: false,
300+
active_lock_reason: null,
301+
author_association: "CONTRIBUTOR",
302+
user: { login: "reporter" },
303+
labels: [],
304+
comments: 1,
305+
pull_request: null
306+
}));
307+
} else if (args[0] === "api" && /\\/issues\\/321$/.test(path)) {
308+
console.log(JSON.stringify({
309+
number: 321,
310+
title: "Obsolete linked PR",
311+
html_url: "https://github.com/openclaw/clawsweeper/pull/321",
312+
created_at: "2026-05-01T00:00:00Z",
313+
updated_at: "2026-05-01T00:00:00Z",
314+
closed_at: null,
315+
state: "open",
316+
locked: false,
317+
active_lock_reason: null,
318+
author_association: "CONTRIBUTOR",
319+
user: { login: "reporter" },
320+
labels: [],
321+
comments: 1,
322+
pull_request: { url: "https://api.github.com/repos/openclaw/clawsweeper/pulls/321" }
323+
}));
324+
} else if (args[0] === "issue" && args[1] === "view" && args[2] === "320") {
325+
console.log(JSON.stringify({ closedByPullRequestsReferences: [{ number: 321 }] }));
326+
} else if (args[0] === "issue" && args[1] === "view") {
327+
console.log(JSON.stringify({ closedByPullRequestsReferences: [] }));
328+
} else if (args[0] === "api" && /\\/pulls\\/321$/.test(path)) {
329+
console.log(JSON.stringify({
330+
number: 321,
331+
title: "Obsolete linked PR",
332+
html_url: "https://github.com/openclaw/clawsweeper/pull/321",
333+
state: "open",
334+
changed_files: 0,
335+
commits: 0,
336+
review_comments: 0,
337+
head: { sha: "head-sha", ref: "branch", repo: { full_name: "fork/clawsweeper" } },
338+
base: { sha: "base-sha", ref: "main", repo: { full_name: "openclaw/clawsweeper" } },
339+
user: { login: "reporter" }
340+
}));
341+
} else if (args[0] === "api" && /\\/pulls\\/321\\/(files|commits|comments)(?:\\?|$)/.test(path)) {
342+
console.log(JSON.stringify([[]]));
343+
} else if (args[0] === "label" || args[0] === "issue") {
344+
console.log("");
345+
} else {
346+
console.error("unexpected gh args", JSON.stringify(args));
347+
process.exit(1);
348+
}
349+
`;
350+
withMockGh(root, ghMock, () => {
351+
runApplyDecisionsForTest({
352+
itemsDir,
353+
closedDir,
354+
plansDir,
355+
reportPath,
356+
extraArgs: [
357+
"--target-repo",
358+
"openclaw/openclaw",
359+
"--dry-run",
360+
"--apply-kind",
361+
"all",
362+
"--processed-limit",
363+
"4",
364+
],
365+
});
366+
});
367+
368+
const report = JSON.parse(readFileSync(reportPath, "utf8")) as Array<{
369+
number: number;
370+
action: string;
371+
reason: string;
372+
}>;
373+
assert.equal(
374+
report.some((entry) => entry.number === 320 && entry.action === "closed"),
375+
true,
376+
);
377+
assert.equal(
378+
report.some((entry) => entry.number === 321 && entry.action === "closed"),
379+
true,
380+
);
381+
} finally {
382+
rmSync(root, { recursive: true, force: true });
383+
}
384+
});

0 commit comments

Comments
 (0)