|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { |
| 5 | + fetchAssignmentOperationEvents, |
| 6 | + mergeEventsById, |
| 7 | +} from "./assignmentOperationFetch.ts"; |
| 8 | +import { fetchProjectsWorkItems } from "./projectWorkItems.ts"; |
| 9 | + |
| 10 | +const REPO_OWNER = "a".repeat(64); |
| 11 | +const REPO_ADDRESS = `30617:${REPO_OWNER}:relay`; |
| 12 | +const ISSUE_ID = "1".repeat(64); |
| 13 | +const ASSIGNEE = "b".repeat(64); |
| 14 | + |
| 15 | +function makeIssue() { |
| 16 | + return { |
| 17 | + id: ISSUE_ID, |
| 18 | + kind: 1621, |
| 19 | + pubkey: REPO_OWNER, |
| 20 | + created_at: 100, |
| 21 | + content: "An issue", |
| 22 | + tags: [ |
| 23 | + ["a", REPO_ADDRESS], |
| 24 | + ["subject", "Fix the thing"], |
| 25 | + ], |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +/** Owner-signed assignment operation (kind:1, `t: assignment`). */ |
| 30 | +function makeAssignment(id, createdAt) { |
| 31 | + return { |
| 32 | + id, |
| 33 | + kind: 1, |
| 34 | + pubkey: REPO_OWNER, |
| 35 | + created_at: createdAt, |
| 36 | + content: "Assigned", |
| 37 | + tags: [ |
| 38 | + ["e", ISSUE_ID, "", "root"], |
| 39 | + ["a", REPO_ADDRESS], |
| 40 | + ["p", ASSIGNEE], |
| 41 | + ["t", "assignment"], |
| 42 | + ], |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +function makeComment(id, createdAt) { |
| 47 | + return { |
| 48 | + id, |
| 49 | + kind: 1, |
| 50 | + pubkey: REPO_OWNER, |
| 51 | + created_at: createdAt, |
| 52 | + content: `Comment ${id.slice(0, 4)}`, |
| 53 | + tags: [ |
| 54 | + ["e", ISSUE_ID, "", "root"], |
| 55 | + ["a", REPO_ADDRESS], |
| 56 | + ], |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +function eventId(index) { |
| 61 | + return index.toString(16).padStart(64, "0"); |
| 62 | +} |
| 63 | + |
| 64 | +// ── fetchAssignmentOperationEvents: pagination to exhaustion ──────────────── |
| 65 | + |
| 66 | +test("fetchAssignmentOperationEvents paginates past the page limit", async () => { |
| 67 | + // 1,203 operations — three pages at the 500-event page limit. The stub |
| 68 | + // honors `until` (inclusive) and `limit` like a relay: newest first. |
| 69 | + const operations = Array.from({ length: 1_203 }, (_, index) => |
| 70 | + makeAssignment(eventId(index + 1), 1_000_000 + index), |
| 71 | + ); |
| 72 | + const calls = []; |
| 73 | + const fetchEvents = async (filter) => { |
| 74 | + calls.push(filter); |
| 75 | + assert.deepEqual(filter["#t"], ["assignment", "unassignment"]); |
| 76 | + const until = filter.until ?? Number.POSITIVE_INFINITY; |
| 77 | + return operations |
| 78 | + .filter((event) => event.created_at <= until) |
| 79 | + .sort((left, right) => right.created_at - left.created_at) |
| 80 | + .slice(0, filter.limit); |
| 81 | + }; |
| 82 | + |
| 83 | + const events = await fetchAssignmentOperationEvents( |
| 84 | + [REPO_ADDRESS], |
| 85 | + fetchEvents, |
| 86 | + ); |
| 87 | + |
| 88 | + assert.equal(events.length, 1_203, "every operation must be loaded"); |
| 89 | + assert.ok(calls.length >= 3, "must page past the 500-event window"); |
| 90 | +}); |
| 91 | + |
| 92 | +test("fetchAssignmentOperationEvents stops when a page adds nothing unseen", async () => { |
| 93 | + // A degenerate relay that always returns the same full page must not loop |
| 94 | + // forever: a page with zero unseen events terminates pagination. |
| 95 | + const page = Array.from({ length: 500 }, (_, index) => |
| 96 | + makeAssignment(eventId(index + 1), 1_000), |
| 97 | + ); |
| 98 | + let callCount = 0; |
| 99 | + const fetchEvents = async () => { |
| 100 | + callCount += 1; |
| 101 | + return page; |
| 102 | + }; |
| 103 | + |
| 104 | + const events = await fetchAssignmentOperationEvents( |
| 105 | + [REPO_ADDRESS], |
| 106 | + fetchEvents, |
| 107 | + ); |
| 108 | + |
| 109 | + assert.equal(events.length, 500); |
| 110 | + assert.equal(callCount, 2, "second identical page must terminate the loop"); |
| 111 | +}); |
| 112 | + |
| 113 | +test("fetchAssignmentOperationEvents skips the relay for zero repositories", async () => { |
| 114 | + const events = await fetchAssignmentOperationEvents([], async () => { |
| 115 | + throw new Error("must not query the relay"); |
| 116 | + }); |
| 117 | + assert.deepEqual(events, []); |
| 118 | +}); |
| 119 | + |
| 120 | +test("mergeEventsById drops duplicates and keeps both sources", () => { |
| 121 | + const shared = makeAssignment(eventId(1), 10); |
| 122 | + const merged = mergeEventsById( |
| 123 | + [shared, makeComment(eventId(2), 11)], |
| 124 | + [shared, makeAssignment(eventId(3), 12)], |
| 125 | + ); |
| 126 | + assert.deepEqual( |
| 127 | + merged.map((event) => event.id), |
| 128 | + [eventId(1), eventId(2), eventId(3)], |
| 129 | + ); |
| 130 | +}); |
| 131 | + |
| 132 | +// ── Regression: assignment predating a full comment window ───────────────── |
| 133 | +// |
| 134 | +// The reduction in projectIssues.mjs is only as complete as the events it is |
| 135 | +// handed. The general comment fetch is bounded (2,000 shared across repos in |
| 136 | +// fetchProjectsWorkItems), so an old assignment operation can be evicted by |
| 137 | +// newer unrelated comments. The dedicated `#t`-filtered exhaustive query must |
| 138 | +// restore it. |
| 139 | + |
| 140 | +test("an assignment older than 500+ newer comments still reduces to an assignee", async () => { |
| 141 | + const issue = makeIssue(); |
| 142 | + // Operation at t=200, then 600 newer comments that fill the bounded window. |
| 143 | + const assignment = makeAssignment(eventId(9_999), 200); |
| 144 | + const comments = Array.from({ length: 600 }, (_, index) => |
| 145 | + makeComment(eventId(index + 1), 300 + index), |
| 146 | + ); |
| 147 | + |
| 148 | + const fetchEvents = async (filter) => { |
| 149 | + if (filter.kinds?.includes(1621)) return [issue]; |
| 150 | + if (filter["#t"]) return [assignment]; |
| 151 | + if (filter.kinds?.includes(1)) { |
| 152 | + // Bounded comment window: newest first, capped — the assignment |
| 153 | + // operation has been pushed out. |
| 154 | + return comments |
| 155 | + .sort((left, right) => right.created_at - left.created_at) |
| 156 | + .slice(0, 500); |
| 157 | + } |
| 158 | + return []; |
| 159 | + }; |
| 160 | + |
| 161 | + const result = await fetchProjectsWorkItems( |
| 162 | + [{ repositories: [{ repoAddress: REPO_ADDRESS }] }], |
| 163 | + fetchEvents, |
| 164 | + ); |
| 165 | + |
| 166 | + assert.equal(result.issues.items.length, 1); |
| 167 | + assert.deepEqual( |
| 168 | + result.issues.items[0].issue.assignees, |
| 169 | + [ASSIGNEE], |
| 170 | + "assignee evicted from the comment window must be restored by the dedicated assignment query", |
| 171 | + ); |
| 172 | +}); |
| 173 | + |
| 174 | +test("a failed assignment query surfaces as a failed section instead of silent loss", async () => { |
| 175 | + const issue = makeIssue(); |
| 176 | + const fetchEvents = async (filter) => { |
| 177 | + if (filter.kinds?.includes(1621)) return [issue]; |
| 178 | + if (filter["#t"]) throw new Error("relay hiccup"); |
| 179 | + return []; |
| 180 | + }; |
| 181 | + |
| 182 | + const result = await fetchProjectsWorkItems( |
| 183 | + [{ repositories: [{ repoAddress: REPO_ADDRESS }] }], |
| 184 | + fetchEvents, |
| 185 | + ); |
| 186 | + |
| 187 | + assert.ok(result.issues.failedSections.includes("assignments")); |
| 188 | +}); |
0 commit comments