|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import test from "node:test"; |
| 5 | + |
| 6 | +import { |
| 7 | + implementedCloseReport, |
| 8 | + readText, |
| 9 | + runApplyDecisionsForTest, |
| 10 | + tmpPrefix, |
| 11 | + withMockGh, |
| 12 | +} from "./helpers.ts"; |
| 13 | + |
| 14 | +test("apply-decisions archives records deleted after review instead of failing the run", () => { |
| 15 | + const root = mkdtempSync(tmpPrefix); |
| 16 | + try { |
| 17 | + const itemsDir = join(root, "items"); |
| 18 | + const closedDir = join(root, "closed"); |
| 19 | + const plansDir = join(root, "plans"); |
| 20 | + const reportPath = join(root, "apply-report.json"); |
| 21 | + mkdirSync(itemsDir, { recursive: true }); |
| 22 | + mkdirSync(plansDir, { recursive: true }); |
| 23 | + writeFileSync( |
| 24 | + join(itemsDir, "321.md"), |
| 25 | + implementedCloseReport({ action_taken: "proposed_close" }), |
| 26 | + "utf8", |
| 27 | + ); |
| 28 | + |
| 29 | + const ghMock = ` |
| 30 | +const rawArgs = process.argv.slice(2); |
| 31 | +const args = rawArgs[0] === "--repo" ? rawArgs.slice(2) : rawArgs; |
| 32 | +const path = args[1] || ""; |
| 33 | +if (args[0] === "api" && /\\/issues\\/321$/.test(path)) { |
| 34 | + console.error("gh: Not Found (HTTP 404)"); |
| 35 | + process.exit(1); |
| 36 | +} |
| 37 | +if (args[0] === "api" && path === "repos/openclaw/clawsweeper") { |
| 38 | + console.log(JSON.stringify({ full_name: "openclaw/clawsweeper" })); |
| 39 | + process.exit(0); |
| 40 | +} |
| 41 | +console.error("unexpected gh args", JSON.stringify(args)); |
| 42 | +process.exit(1); |
| 43 | +`; |
| 44 | + withMockGh(root, ghMock, () => { |
| 45 | + runApplyDecisionsForTest({ itemsDir, closedDir, plansDir, reportPath }); |
| 46 | + }); |
| 47 | + |
| 48 | + assert.equal(existsSync(join(itemsDir, "321.md")), false); |
| 49 | + assert.ok(existsSync(join(closedDir, "321.md"))); |
| 50 | + assert.match(readText(join(closedDir, "321.md")), /^action_taken: skipped_already_closed$/m); |
| 51 | + assert.deepEqual(JSON.parse(readText(reportPath)), [ |
| 52 | + { |
| 53 | + number: 321, |
| 54 | + action: "skipped_already_closed", |
| 55 | + reason: "item not found on GitHub", |
| 56 | + }, |
| 57 | + ]); |
| 58 | + } finally { |
| 59 | + rmSync(root, { recursive: true, force: true }); |
| 60 | + } |
| 61 | +}); |
| 62 | + |
| 63 | +test("apply-decisions keeps missing records queued during comment-only sync", () => { |
| 64 | + const root = mkdtempSync(tmpPrefix); |
| 65 | + try { |
| 66 | + const itemsDir = join(root, "items"); |
| 67 | + const closedDir = join(root, "closed"); |
| 68 | + const plansDir = join(root, "plans"); |
| 69 | + const reportPath = join(root, "apply-report.json"); |
| 70 | + mkdirSync(itemsDir, { recursive: true }); |
| 71 | + mkdirSync(plansDir, { recursive: true }); |
| 72 | + writeFileSync(join(itemsDir, "321.md"), implementedCloseReport(), "utf8"); |
| 73 | + |
| 74 | + const ghMock = ` |
| 75 | +const rawArgs = process.argv.slice(2); |
| 76 | +const args = rawArgs[0] === "--repo" ? rawArgs.slice(2) : rawArgs; |
| 77 | +const path = args[1] || ""; |
| 78 | +if (args[0] === "api" && /\\/issues\\/321$/.test(path)) { |
| 79 | + console.error("gh: Not Found (HTTP 404)"); |
| 80 | + process.exit(1); |
| 81 | +} |
| 82 | +if (args[0] === "api" && path === "repos/openclaw/clawsweeper") { |
| 83 | + console.log(JSON.stringify({ full_name: "openclaw/clawsweeper" })); |
| 84 | + process.exit(0); |
| 85 | +} |
| 86 | +console.error("unexpected gh args", JSON.stringify(args)); |
| 87 | +process.exit(1); |
| 88 | +`; |
| 89 | + withMockGh(root, ghMock, () => { |
| 90 | + runApplyDecisionsForTest({ |
| 91 | + itemsDir, |
| 92 | + closedDir, |
| 93 | + plansDir, |
| 94 | + reportPath, |
| 95 | + extraArgs: ["--sync-comments-only"], |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + assert.ok(existsSync(join(itemsDir, "321.md"))); |
| 100 | + assert.equal(existsSync(join(closedDir, "321.md")), false); |
| 101 | + assert.match(readText(join(itemsDir, "321.md")), /^action_taken: proposed_close$/m); |
| 102 | + assert.deepEqual(JSON.parse(readText(reportPath)), [ |
| 103 | + { |
| 104 | + number: 321, |
| 105 | + action: "skipped_already_closed", |
| 106 | + reason: "item not found on GitHub", |
| 107 | + }, |
| 108 | + ]); |
| 109 | + } finally { |
| 110 | + rmSync(root, { recursive: true, force: true }); |
| 111 | + } |
| 112 | +}); |
| 113 | + |
| 114 | +test("apply-decisions fails safely when a missing repository also returns 404", () => { |
| 115 | + const root = mkdtempSync(tmpPrefix); |
| 116 | + try { |
| 117 | + const itemsDir = join(root, "items"); |
| 118 | + const closedDir = join(root, "closed"); |
| 119 | + const plansDir = join(root, "plans"); |
| 120 | + const reportPath = join(root, "apply-report.json"); |
| 121 | + mkdirSync(itemsDir, { recursive: true }); |
| 122 | + mkdirSync(plansDir, { recursive: true }); |
| 123 | + writeFileSync(join(itemsDir, "321.md"), implementedCloseReport(), "utf8"); |
| 124 | + |
| 125 | + const ghMock = ` |
| 126 | +const rawArgs = process.argv.slice(2); |
| 127 | +const args = rawArgs[0] === "--repo" ? rawArgs.slice(2) : rawArgs; |
| 128 | +const path = args[1] || ""; |
| 129 | +if (args[0] === "api" && (/\\/issues\\/321$/.test(path) || path === "repos/openclaw/clawsweeper")) { |
| 130 | + console.error("gh: Not Found (HTTP 404)"); |
| 131 | + process.exit(1); |
| 132 | +} |
| 133 | +console.error("unexpected gh args", JSON.stringify(args)); |
| 134 | +process.exit(1); |
| 135 | +`; |
| 136 | + assert.throws( |
| 137 | + () => |
| 138 | + withMockGh(root, ghMock, () => { |
| 139 | + runApplyDecisionsForTest({ itemsDir, closedDir, plansDir, reportPath }); |
| 140 | + }), |
| 141 | + /Not Found/, |
| 142 | + ); |
| 143 | + |
| 144 | + assert.ok(existsSync(join(itemsDir, "321.md"))); |
| 145 | + assert.equal(existsSync(join(closedDir, "321.md")), false); |
| 146 | + } finally { |
| 147 | + rmSync(root, { recursive: true, force: true }); |
| 148 | + } |
| 149 | +}); |
0 commit comments