|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { createApplyGuards } from "../dist/clawsweeper-apply-guards.js"; |
| 5 | + |
| 6 | +function asRecord(value) { |
| 7 | + return value && typeof value === "object" && !Array.isArray(value) ? value : {}; |
| 8 | +} |
| 9 | + |
| 10 | +function createGuards({ ghJson = () => ({}), ghPaged = () => [] } = {}) { |
| 11 | + return createApplyGuards({ |
| 12 | + asRecord, |
| 13 | + authorPrBudget: () => 10, |
| 14 | + authorPrBudgetAgeSkipReason: () => null, |
| 15 | + authorPrBudgetCloseEnabled: () => true, |
| 16 | + ghJson, |
| 17 | + ghPaged, |
| 18 | + isMaintainerAuthorAssociation: (value) => ["MEMBER", "OWNER", "COLLABORATOR"].includes(value), |
| 19 | + isMaintainerAuthored: () => false, |
| 20 | + isOlderThanDays: () => true, |
| 21 | + labelNames: (value) => |
| 22 | + Array.isArray(value) |
| 23 | + ? value.flatMap((label) => { |
| 24 | + if (typeof label === "string") return [label]; |
| 25 | + const name = asRecord(label).name; |
| 26 | + return typeof name === "string" ? [name] : []; |
| 27 | + }) |
| 28 | + : [], |
| 29 | + login: (value) => { |
| 30 | + const login = asRecord(value).login; |
| 31 | + return typeof login === "string" ? login : undefined; |
| 32 | + }, |
| 33 | + normalizeLabelName: (label) => label.trim().toLowerCase(), |
| 34 | + obsoleteFixPrAgeSkipReason: () => null, |
| 35 | + obsoleteFixPrCloseEnabled: () => true, |
| 36 | + protectedLabels: () => [], |
| 37 | + quoteGitHubSearchTerm: (term) => term, |
| 38 | + reportPrRating: () => ({ |
| 39 | + proofTier: "F", |
| 40 | + patchTier: "F", |
| 41 | + overallTier: "F", |
| 42 | + summary: "", |
| 43 | + nextSteps: [], |
| 44 | + }), |
| 45 | + reportRealBehaviorProof: () => ({ |
| 46 | + status: "missing", |
| 47 | + summary: "", |
| 48 | + evidenceKind: "not_applicable", |
| 49 | + needsContributorAction: true, |
| 50 | + }), |
| 51 | + staleVersionBugAgeSkipReason: () => null, |
| 52 | + staleVersionBugCloseEnabled: () => true, |
| 53 | + stringOrUndefined: (value) => (typeof value === "string" ? value : undefined), |
| 54 | + targetRepo: () => "openclaw/openclaw", |
| 55 | + unconfirmedProductDirectionAgeSkipReason: () => null, |
| 56 | + unconfirmedProductDirectionCloseEnabled: () => true, |
| 57 | + unsponsoredFeatureAgeSkipReason: () => null, |
| 58 | + unsponsoredFeatureCloseEnabled: () => true, |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +test("apply guard reads share a paged endpoint across guard functions", () => { |
| 63 | + const calls = []; |
| 64 | + const guards = createGuards({ |
| 65 | + ghPaged: (path) => { |
| 66 | + calls.push(path); |
| 67 | + return []; |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + guards.issueRecentHumanCommentBlockReasonSafe(42, 30); |
| 72 | + guards.stalledUnprovenProofRequestBlockReason(42); |
| 73 | + |
| 74 | + assert.equal( |
| 75 | + calls.filter((path) => path === "repos/openclaw/openclaw/issues/42/comments").length, |
| 76 | + 1, |
| 77 | + ); |
| 78 | + assert.equal( |
| 79 | + calls.filter((path) => path === "repos/openclaw/openclaw/issues/42/timeline").length, |
| 80 | + 1, |
| 81 | + ); |
| 82 | +}); |
| 83 | + |
| 84 | +test("apply guard reads share a JSON endpoint across guard functions", () => { |
| 85 | + const calls = []; |
| 86 | + const guards = createGuards({ |
| 87 | + ghJson: (args) => { |
| 88 | + calls.push(args); |
| 89 | + return { |
| 90 | + state: "open", |
| 91 | + created_at: "2025-01-01T00:00:00Z", |
| 92 | + labels: [], |
| 93 | + assignees: [], |
| 94 | + milestone: null, |
| 95 | + reactions: { total_count: 0 }, |
| 96 | + }; |
| 97 | + }, |
| 98 | + }); |
| 99 | + const item = { createdAt: "2025-01-01T00:00:00Z" }; |
| 100 | + |
| 101 | + guards.unsponsoredFeatureApplyBlockReasonSafe(42, item); |
| 102 | + guards.staleVersionBugApplyBlockReasonSafe(42, item); |
| 103 | + |
| 104 | + assert.equal( |
| 105 | + calls.filter( |
| 106 | + (args) => |
| 107 | + JSON.stringify(args) === JSON.stringify(["api", "repos/openclaw/openclaw/issues/42"]), |
| 108 | + ).length, |
| 109 | + 1, |
| 110 | + ); |
| 111 | +}); |
| 112 | + |
| 113 | +test("apply guard read cache resets between items", () => { |
| 114 | + const calls = []; |
| 115 | + const guards = createGuards({ |
| 116 | + ghPaged: (path) => { |
| 117 | + calls.push(path); |
| 118 | + return []; |
| 119 | + }, |
| 120 | + }); |
| 121 | + |
| 122 | + guards.issueRecentHumanCommentBlockReasonSafe(1, 30); |
| 123 | + guards.issueRecentHumanCommentBlockReasonSafe(1, 30); |
| 124 | + assert.equal(calls.length, 1); |
| 125 | + |
| 126 | + guards.resetGuardReadCache(); |
| 127 | + guards.issueRecentHumanCommentBlockReasonSafe(1, 30); |
| 128 | + guards.issueRecentHumanCommentBlockReasonSafe(2, 30); |
| 129 | + |
| 130 | + assert.deepEqual(calls, [ |
| 131 | + "repos/openclaw/openclaw/issues/1/comments", |
| 132 | + "repos/openclaw/openclaw/issues/1/comments", |
| 133 | + "repos/openclaw/openclaw/issues/2/comments", |
| 134 | + ]); |
| 135 | +}); |
| 136 | + |
| 137 | +test("apply guard read cache distinguishes full request arguments", () => { |
| 138 | + const calls = []; |
| 139 | + const guards = createGuards({ |
| 140 | + ghJson: (args) => { |
| 141 | + calls.push(args); |
| 142 | + const path = args[1]; |
| 143 | + if (path?.endsWith("/issues/42")) return { assignees: [] }; |
| 144 | + if (path?.endsWith("/pulls/42")) { |
| 145 | + return args.includes("--jq") |
| 146 | + ? { requested_reviewers: [], requested_teams: [] } |
| 147 | + : { |
| 148 | + created_at: "2025-01-01T00:00:00Z", |
| 149 | + mergeable: false, |
| 150 | + mergeable_state: "dirty", |
| 151 | + requested_reviewers: [], |
| 152 | + requested_teams: [], |
| 153 | + user: { login: "contributor" }, |
| 154 | + head: {}, |
| 155 | + }; |
| 156 | + } |
| 157 | + return {}; |
| 158 | + }, |
| 159 | + }); |
| 160 | + const item = { createdAt: "2025-01-01T00:00:00Z", labels: [] }; |
| 161 | + |
| 162 | + guards.unconfirmedProductDirectionApplyBlockReasonSafe(42, item, undefined, undefined); |
| 163 | + guards.lowSignalUnmergeablePrApplyBlockReasonSafe(42, 30); |
| 164 | + |
| 165 | + const pullCalls = calls.filter((args) => args[1] === "repos/openclaw/openclaw/pulls/42"); |
| 166 | + assert.equal(pullCalls.length, 2); |
| 167 | + assert.equal( |
| 168 | + pullCalls.some((args) => args.includes("--jq")), |
| 169 | + true, |
| 170 | + ); |
| 171 | + assert.equal( |
| 172 | + pullCalls.some((args) => !args.includes("--jq")), |
| 173 | + true, |
| 174 | + ); |
| 175 | +}); |
0 commit comments