| 1 | import path from 'node:path'; |
| 2 | import { createDatabase, type |
| 3 | import { and, eq |
| 4 | import { migrate } |
| 5 | import { afterAll, beforeAll |
| 6 | |
| 7 | import { InvalidCursorError } |
| 8 | import { PullRequestReopenError } |
| 9 | import { IssuesService } |
| 10 | import { RepositoryAccessService } |
| 11 | import { UsersService } |
| 12 | import { IssueReferencesService } |
| 13 | |
| 14 | const CONNECTION = process |
| 15 | const MIGRATIONS = path |
| 16 | import.meta.dirname, |
| 17 | '../../../../../packages/db/drizzle', |
| 18 | ); |
| 19 | |
| 20 | const OWNER = ' |
| 21 | const STRANGER = ' |
| 22 | |
| 23 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 24 | describe.skipIf(!CONNECTION |
| 25 | let db: |
| 26 | let pool: |
| 27 | let references: |
| 28 | let issues: |
| 29 | let repository: |
| 30 | let secret: |
| 31 | |
| 32 | const open = |
| 33 | issues.open( |
| 34 | { repository, title |
| 35 | async () => |
| 36 | ); |
| 37 | |
| 38 | const referencesTo = |
| 39 | db |
| 40 | .select() |
| 41 | .from(schema.issueReference |
| 42 | .where(eq(schema |
| 43 | |
| 44 | beforeAll(async () |
| 45 | ({ db, pool |
| 46 | await migrate(db |
| 47 | |
| 48 | references = new |
| 49 | issues = new |
| 50 | db, |
| 51 | new UsersService(db |
| 52 | new RepositoryAccessService(db |
| 53 | references, |
| 54 | ); |
| 55 | }); |
| 56 | |
| 57 | beforeEach(async () |
| 58 | await db |
| 59 | .delete(schema.user |
| 60 | .where(inArray(schema |
| 61 | await db.insert |
| 62 | { |
| 63 | id: OWNER, |
| 64 | name: 'Owner', |
| 65 | email: 'refspec-owner@example.com', |
| 66 | username: 'refspec-owner', |
| 67 | }, |
| 68 | { |
| 69 | id: STRANGER, |
| 70 | name: 'Stranger', |
| 71 | email: 'refspec-stranger@example.com', |
| 72 | username: 'refspec-stranger', |
| 73 | }, |
| 74 | ]); |
| 75 | [repository, secret] |
| 76 | .insert(schema.repository |
| 77 | .values([ |
| 78 | { name: 'app', |
| 79 | { name: 'secret', |
| 80 | ]) |
| 81 | .returning(); |
| 82 | }); |
| 83 | |
| 84 | afterAll(async () |
| 85 | await db |
| 86 | .delete(schema.user |
| 87 | .where(inArray(schema |
| 88 | await pool?.end |
| 89 | }); |
| 90 | |
| 91 | it('numbers issues and pull requests from one sequence', |
| 92 | const issue = |
| 93 | const pull = |
| 94 | |
| 95 | expect([issue.number |
| 96 | }); |
| 97 | |
| 98 | it('pages issues by comment count, oldest first on a tie, and rejects a cursor from another sort', |
| 99 | for (const [i |
| 100 | const issue = |
| 101 | await db |
| 102 | .update(schema.issue |
| 103 | .set({ commentCount |
| 104 | .where(eq(schema |
| 105 | } |
| 106 | const list = |
| 107 | issues.getIssues({ |
| 108 | username: 'refspec-owner', |
| 109 | repo: 'app', |
| 110 | requesterId: OWNER, |
| 111 | query: { sort: ' |
| 112 | }); |
| 113 | |
| 114 | const first = |
| 115 | const second = |
| 116 | expect( |
| 117 | [first, second] |
| 118 | ).toEqual([ |
| 119 | [2, 4], |
| 120 | [1, 3], |
| 121 | ]); |
| 122 | expect(second.nextCursor |
| 123 | |
| 124 | const created = |
| 125 | await expect(list |
| 126 | InvalidCursorError, |
| 127 | ); |
| 128 | }); |
| 129 | |
| 130 | it('records same-repository and cross-repository references, skipping itself and unknown numbers', |
| 131 | const target = |
| 132 | const elsewhere = |
| 133 | { |
| 134 | repository: secret, |
| 135 | title: 'secret target', |
| 136 | body: null, |
| 137 | authorId: OWNER, |
| 138 | isPullRequest: false, |
| 139 | }, |
| 140 | async () => |
| 141 | ); |
| 142 | const source = |
| 143 | 'source', |
| 144 | 'fixes #1, see #2 and #99, and refspec-owner/secret#1', |
| 145 | ); |
| 146 | |
| 147 | const rows = |
| 148 | .select() |
| 149 | .from(schema.issueReference |
| 150 | .where(eq(schema |
| 151 | expect(rows.map |
| 152 | [ |
| 153 | [target.id, |
| 154 | [elsewhere.id, |
| 155 | ].sort(), |
| 156 | ); |
| 157 | }); |
| 158 | |
| 159 | it('resolves nothing in a repository the author cannot read', |
| 160 | const hidden = |
| 161 | { |
| 162 | repository: secret, |
| 163 | title: 'hidden', |
| 164 | body: null, |
| 165 | authorId: OWNER, |
| 166 | isPullRequest: false, |
| 167 | }, |
| 168 | async () => |
| 169 | ); |
| 170 | await references.record |
| 171 | db, |
| 172 | { |
| 173 | type: 'comment', |
| 174 | id: 'ic_stranger', |
| 175 | repository, |
| 176 | issueId: null, |
| 177 | actorId: STRANGER, |
| 178 | }, |
| 179 | 'refspec-owner/secret#1', |
| 180 | ); |
| 181 | |
| 182 | expect(await referencesTo |
| 183 | }); |
| 184 | |
| 185 | it('keeps a surviving reference on edit, updates its closing flag and drops removed ones', |
| 186 | const first = |
| 187 | const second = |
| 188 | const source = |
| 189 | const [before] |
| 190 | |
| 191 | await issues.updateIssue |
| 192 | username: 'refspec-owner', |
| 193 | repo: 'app', |
| 194 | number: source.number |
| 195 | requesterId: OWNER, |
| 196 | body: { body: ' |
| 197 | }); |
| 198 | |
| 199 | const [after] |
| 200 | expect(after).toMatchObject |
| 201 | expect(await referencesTo |
| 202 | }); |
| 203 | |
| 204 | it('forgets a deleted comment’s references', |
| 205 | const target = |
| 206 | const source = |
| 207 | const ref = |
| 208 | username: 'refspec-owner', |
| 209 | repo: 'app', |
| 210 | number: source.number |
| 211 | }; |
| 212 | const comment = |
| 213 | ...ref, |
| 214 | requesterId: OWNER, |
| 215 | body: 'relates to #1', |
| 216 | }); |
| 217 | expect(await referencesTo |
| 218 | |
| 219 | await issues.deleteComment |
| 220 | ...ref, |
| 221 | requesterId: OWNER, |
| 222 | commentId: comment.id |
| 223 | }); |
| 224 | expect(await referencesTo |
| 225 | }); |
| 226 | |
| 227 | it('shows one mention per source issue, and hides private sources from other viewers', |
| 228 | const target = |
| 229 | const source = |
| 230 | await issues.createComment |
| 231 | username: 'refspec-owner', |
| 232 | repo: 'app', |
| 233 | number: source.number |
| 234 | requesterId: OWNER, |
| 235 | body: 'again #1', |
| 236 | }); |
| 237 | await references.record |
| 238 | db, |
| 239 | { |
| 240 | type: 'issue', |
| 241 | id: 'issue_private_source', |
| 242 | repository: secret, |
| 243 | issueId: null, |
| 244 | actorId: OWNER, |
| 245 | }, |
| 246 | 'refspec-owner/app#1', |
| 247 | ); |
| 248 | |
| 249 | const owner = |
| 250 | const stranger = |
| 251 | userId: STRANGER, |
| 252 | }); |
| 253 | |
| 254 | expect(owner).toHaveLength |
| 255 | expect(stranger).toEqual |
| 256 | expect.objectContaining({ |
| 257 | sourceType: 'issue', |
| 258 | repository: { username: ' |
| 259 | source: { |
| 260 | number: source.number |
| 261 | title: 'source', |
| 262 | state: 'open', |
| 263 | isPullRequest: false, |
| 264 | }, |
| 265 | }), |
| 266 | ]); |
| 267 | }); |
| 268 | |
| 269 | it('closes issues a commit promises to, but never a pull request or a repository the pusher cannot write to', |
| 270 | const issue = |
| 271 | const pull = |
| 272 | |
| 273 | await references.closeFromCommits |
| 274 | repository, |
| 275 | actorId: STRANGER, |
| 276 | commits: [commit(' |
| 277 | }); |
| 278 | expect(await stateOf |
| 279 | |
| 280 | await references.closeFromCommits |
| 281 | repository, |
| 282 | actorId: OWNER, |
| 283 | commits: [commit(' |
| 284 | }); |
| 285 | expect(await stateOf |
| 286 | expect(await stateOf |
| 287 | |
| 288 | const [event] |
| 289 | .select() |
| 290 | .from(schema.issueEvent |
| 291 | .where( |
| 292 | and( |
| 293 | eq(schema.issueEvent |
| 294 | eq(schema.issueEvent |
| 295 | ), |
| 296 | ); |
| 297 | expect(event).toMatchObject |
| 298 | |
| 299 | expect(await references |
| 300 | expect.objectContaining({ |
| 301 | sourceType: 'commit', |
| 302 | commitSha: 'aaaa', |
| 303 | source: null, |
| 304 | }), |
| 305 | expect.objectContaining({ |
| 306 | sourceType: 'commit', |
| 307 | commitSha: 'bbbb', |
| 308 | source: null, |
| 309 | }), |
| 310 | ]); |
| 311 | }); |
| 312 | |
| 313 | it('keeps a pull request in step when it is closed as an issue, and refuses to reopen it', |
| 314 | const pull = |
| 315 | await db.insert |
| 316 | issueId: pull.id |
| 317 | baseRepositoryId: repository.id |
| 318 | baseRef: 'main', |
| 319 | headRepositoryId: repository.id |
| 320 | headRef: 'feature', |
| 321 | headSha: 'cccc', |
| 322 | }); |
| 323 | const ref = |
| 324 | username: 'refspec-owner', |
| 325 | repo: 'app', |
| 326 | number: pull.number |
| 327 | requesterId: OWNER, |
| 328 | }; |
| 329 | |
| 330 | await issues.closeIssue |
| 331 | const [row] |
| 332 | .select({ state: |
| 333 | .from(schema.pullRequest |
| 334 | .where(eq(schema |
| 335 | expect(row.state |
| 336 | |
| 337 | await expect(issues |
| 338 | PullRequestReopenError, |
| 339 | ); |
| 340 | }); |
| 341 | |
| 342 | it('lists issues without pull requests', |
| 343 | await open('issue |
| 344 | await open('pr |
| 345 | |
| 346 | const { issues: |
| 347 | username: 'refspec-owner', |
| 348 | repo: 'app', |
| 349 | requesterId: OWNER, |
| 350 | query: {}, |
| 351 | }); |
| 352 | expect(listed.map |
| 353 | }); |
| 354 | |
| 355 | it('puts mentions into the timeline', |
| 356 | await open('target |
| 357 | await open('source |
| 358 | |
| 359 | const { timeline |
| 360 | username: 'refspec-owner', |
| 361 | repo: 'app', |
| 362 | number: 1, |
| 363 | requesterId: OWNER, |
| 364 | }); |
| 365 | expect(timeline.map |
| 366 | }); |
| 367 | |
| 368 | function stateOf(issueId |
| 369 | return db |
| 370 | .select({ state: |
| 371 | .from(schema.issue |
| 372 | .where(eq(schema |
| 373 | .then(([row |
| 374 | } |
| 375 | }); |
| 376 | |
| 377 | function commit(sha: |
| 378 | return { |
| 379 | sha, |
| 380 | subject, |
| 381 | body: '', |
| 382 | authorName: 'a', |
| 383 | authorEmail: 'a@example.com', |
| 384 | committedAt: new Date |
| 385 | authoredAt: new Date |
| 386 | committerName: 'a', |
| 387 | committerEmail: 'a@example.com', |
| 388 | }; |
| 389 | } |