| 1 | import { execFile, execFileSync } from 'node:child_process'; |
| 2 | import { mkdtempSync, rmSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { promisify } |
| 6 | import type { INestApplication |
| 7 | import request from 'supertest |
| 8 | import { afterAll, beforeAll |
| 9 | |
| 10 | import { hasBackends, signUp |
| 11 | |
| 12 | describe.skipIf(!hasBackends |
| 13 | let app: |
| 14 | let origin: |
| 15 | let author: |
| 16 | let reviewer: |
| 17 | let bystander: |
| 18 | let work: |
| 19 | const owner = |
| 20 | const other = |
| 21 | let repo: |
| 22 | |
| 23 | const api = |
| 24 | const pull = |
| 25 | `/api/repositories/${owner}/${ |
| 26 | const git = |
| 27 | execFileSync( |
| 28 | 'git', |
| 29 | ['-c', 'user.name=E2E' |
| 30 | { cwd: work, |
| 31 | ).trim(); |
| 32 | // The server runs in this process, so anything that talks to it must not block the event loop. |
| 33 | const remote = |
| 34 | promisify(execFile)( |
| 35 | 'git', |
| 36 | [ |
| 37 | verb, |
| 38 | '-q', |
| 39 | `${origin.replace( |
| 40 | ...refs, |
| 41 | ], |
| 42 | { cwd: work, |
| 43 | ); |
| 44 | const push = |
| 45 | const review = |
| 46 | api().post(pull |
| 47 | |
| 48 | beforeAll(async () |
| 49 | ({ app, origin |
| 50 | author = await |
| 51 | reviewer = await |
| 52 | const bystanderName = |
| 53 | bystander = { |
| 54 | ...(await signUp |
| 55 | username: bystanderName, |
| 56 | }; |
| 57 | |
| 58 | repo = ( |
| 59 | await api() |
| 60 | .post('/api/repositories') |
| 61 | .set('cookie', |
| 62 | .send({ name: |
| 63 | .expect(201) |
| 64 | ).body.slug; |
| 65 | work = mkdtempSync( |
| 66 | git('init', |
| 67 | writeFileSync(path.join |
| 68 | // Twenty lines changed at both ends diff as two hunks. |
| 69 | const lines = |
| 70 | writeFileSync(path.join |
| 71 | git('add', |
| 72 | git('commit', |
| 73 | git('checkout', |
| 74 | writeFileSync(path.join |
| 75 | writeFileSync( |
| 76 | path.join(work |
| 77 | `${['first', ... |
| 78 | ); |
| 79 | git('commit', |
| 80 | await push('main |
| 81 | |
| 82 | await api() |
| 83 | .post(`/api/repositories/${owner |
| 84 | .set('cookie', |
| 85 | .send({ title: |
| 86 | .expect(201) |
| 87 | .expect(({ body |
| 88 | }, 120_000); |
| 89 | |
| 90 | afterAll(async () |
| 91 | await app?.close |
| 92 | if (work) rmSync |
| 93 | }); |
| 94 | |
| 95 | it('refuses to merge a draft, and lets only the author or a writer mark it ready', |
| 96 | const merge = |
| 97 | .post(pull(' |
| 98 | .set('cookie', |
| 99 | .send({}) |
| 100 | .expect(409); |
| 101 | expect(merge.body |
| 102 | expect((await api |
| 103 | |
| 104 | await api().post |
| 105 | const ready = |
| 106 | .post(pull(' |
| 107 | .set('cookie', |
| 108 | .expect(201); |
| 109 | expect(ready.body |
| 110 | // A second call changes nothing and records nothing. |
| 111 | await api().post |
| 112 | |
| 113 | const { body |
| 114 | expect(body.draft |
| 115 | }); |
| 116 | |
| 117 | it('rejects reviews that say nothing, verdicts on your own request, and lines outside the diff', |
| 118 | await review(author |
| 119 | await review(reviewer |
| 120 | 400, |
| 121 | ); |
| 122 | for (const [path |
| 123 | ['missing.txt', 'additions' |
| 124 | ['a.txt', 'additions' |
| 125 | ['a.txt', 'deletions' |
| 126 | ] as const) |
| 127 | const stray = |
| 128 | state: 'commented', |
| 129 | comments: [{ path |
| 130 | }).expect(400) |
| 131 | expect(stray.body |
| 132 | } |
| 133 | }); |
| 134 | |
| 135 | it('keeps pending comments private until the review is submitted, then records what they mention', |
| 136 | await api() |
| 137 | .post(`/api/repositories/${owner |
| 138 | .set('cookie', |
| 139 | .send({ title: |
| 140 | .expect(201); |
| 141 | const mentions = |
| 142 | ( |
| 143 | await api() |
| 144 | .get(`/api/repositories/${owner |
| 145 | .expect(200) |
| 146 | ).body.timeline. |
| 147 | (item: { |
| 148 | ); |
| 149 | |
| 150 | const added = |
| 151 | .post(pull(' |
| 152 | .set('cookie', |
| 153 | .send({ path: |
| 154 | .expect(201); |
| 155 | expect(added.body |
| 156 | state: null, |
| 157 | comments: [{ line: |
| 158 | }); |
| 159 | const pendingId = |
| 160 | |
| 161 | const { body: |
| 162 | .get(pull(' |
| 163 | .set('cookie', |
| 164 | .expect(200); |
| 165 | expect(others.review |
| 166 | await api() |
| 167 | .patch(pull(` |
| 168 | .set('cookie', |
| 169 | .send({ body: |
| 170 | .expect(404); |
| 171 | await api() |
| 172 | .post(pull(` |
| 173 | .set('cookie', |
| 174 | .send({ body: |
| 175 | .expect(409); |
| 176 | expect(await mentions |
| 177 | |
| 178 | // Discarding throws the batch away; the next comment starts a new one. |
| 179 | await api() |
| 180 | .delete(pull(' |
| 181 | .set('cookie', |
| 182 | .expect(200); |
| 183 | expect( |
| 184 | ( |
| 185 | await api() |
| 186 | .get(pull(' |
| 187 | .set('cookie', |
| 188 | .expect(200) |
| 189 | ).body.review, |
| 190 | ).toBeNull(); |
| 191 | await api() |
| 192 | .post(pull(' |
| 193 | .set('cookie', |
| 194 | .send({ path: |
| 195 | .expect(201); |
| 196 | |
| 197 | const submitted = |
| 198 | state: 'changes_requested', |
| 199 | body: 'Needs work', |
| 200 | }).expect(201) |
| 201 | expect(submitted.body |
| 202 | kind: 'review', |
| 203 | state: 'changes_requested', |
| 204 | authorUsername: other, |
| 205 | comments: [ |
| 206 | { |
| 207 | path: 'a.txt', |
| 208 | side: 'additions', |
| 209 | line: 2, |
| 210 | body: 'See #2', |
| 211 | authorUsername: other, |
| 212 | diffHunk: '@@ -1,1 +1,2 @@\n one\n+two |
| 213 | replies: [], |
| 214 | }, |
| 215 | ], |
| 216 | }); |
| 217 | expect( |
| 218 | ( |
| 219 | await api() |
| 220 | .get(pull(' |
| 221 | .set('cookie', |
| 222 | .expect(200) |
| 223 | ).body.review, |
| 224 | ).toBeNull(); |
| 225 | expect(await mentions |
| 226 | }); |
| 227 | |
| 228 | it('threads replies, and limits editing and deleting to the author or a writer', |
| 229 | const { body |
| 230 | .get(`/api/repositories/${owner |
| 231 | .expect(200); |
| 232 | const [thread] |
| 233 | (item: { |
| 234 | ).comments; |
| 235 | |
| 236 | const first = |
| 237 | .post(pull(` |
| 238 | .set('cookie', |
| 239 | .send({ body: |
| 240 | .expect(201); |
| 241 | expect(first.body |
| 242 | // A reply to a reply joins the same thread. |
| 243 | await api() |
| 244 | .post(pull(` |
| 245 | .set('cookie', |
| 246 | .send({ body: |
| 247 | .expect(201); |
| 248 | |
| 249 | await api() |
| 250 | .patch(pull(` |
| 251 | .set('cookie', |
| 252 | .send({ body: |
| 253 | .expect(403); |
| 254 | const edited = |
| 255 | .patch(pull(` |
| 256 | .set('cookie', |
| 257 | .send({ body: |
| 258 | .expect(200); |
| 259 | expect(edited.body |
| 260 | body: 'Edited by a writer', |
| 261 | authorUsername: other, |
| 262 | }); |
| 263 | |
| 264 | const reviewId = |
| 265 | (item: { |
| 266 | ).id; |
| 267 | await api() |
| 268 | .patch(pull(` |
| 269 | .set('cookie', |
| 270 | .send({ body: |
| 271 | .expect(403); |
| 272 | await api() |
| 273 | .patch(pull(` |
| 274 | .set('cookie', |
| 275 | .send({ body: |
| 276 | .expect(200); |
| 277 | |
| 278 | const { body: |
| 279 | .get(`/api/repositories/${owner |
| 280 | .expect(200); |
| 281 | const reviewed = |
| 282 | (item: { |
| 283 | ); |
| 284 | expect(reviewed.body |
| 285 | expect( |
| 286 | reviewed.comments[0] |
| 287 | ).toEqual(['Fixed' |
| 288 | |
| 289 | await api() |
| 290 | .delete(pull(` |
| 291 | .set('cookie', |
| 292 | .expect(403); |
| 293 | await api() |
| 294 | .delete(pull(` |
| 295 | .set('cookie', |
| 296 | .expect(200); |
| 297 | }); |
| 298 | |
| 299 | it('drops a comment-only review once its last comment is deleted', |
| 300 | const { body |
| 301 | state: 'commented', |
| 302 | comments: [ |
| 303 | { path: 'a.txt', |
| 304 | ], |
| 305 | }).expect(201) |
| 306 | await api() |
| 307 | .delete(pull(` |
| 308 | .set('cookie', |
| 309 | .expect(200); |
| 310 | await api() |
| 311 | .patch(pull(` |
| 312 | .set('cookie', |
| 313 | .send({ body: |
| 314 | .expect(404); |
| 315 | }); |
| 316 | |
| 317 | it('counts each reviewer’s latest verdict until a writer dismisses it', |
| 318 | const approval = |
| 319 | state: 'approved', |
| 320 | }).expect(201) |
| 321 | expect((await api |
| 322 | // signUp gives no avatar |
| 323 | { username: other, |
| 324 | ]); |
| 325 | |
| 326 | await api() |
| 327 | .post(pull(` |
| 328 | .set('cookie', |
| 329 | .send({ message: |
| 330 | .expect(403); |
| 331 | const dismissed = |
| 332 | .post(pull(` |
| 333 | .set('cookie', |
| 334 | .send({ message: |
| 335 | .expect(201); |
| 336 | expect(dismissed.body |
| 337 | dismissedByUsername: owner, |
| 338 | dismissalMessage: 'Stale', |
| 339 | }); |
| 340 | await api() |
| 341 | .post(pull(` |
| 342 | .set('cookie', |
| 343 | .send({ message: |
| 344 | .expect(409); |
| 345 | |
| 346 | // The older request for changes does not come back. |
| 347 | expect((await api |
| 348 | }); |
| 349 | |
| 350 | it('turns a request back into a draft for the author or a writer, and records it', |
| 351 | await api() |
| 352 | .post(pull(' |
| 353 | .set('cookie', |
| 354 | .expect(403); |
| 355 | const draft = |
| 356 | .post(pull(' |
| 357 | .set('cookie', |
| 358 | .expect(201); |
| 359 | expect(draft.body |
| 360 | await api() |
| 361 | .post(pull(' |
| 362 | .set('cookie', |
| 363 | .send({}) |
| 364 | .expect(409); |
| 365 | await api().post |
| 366 | |
| 367 | const { body |
| 368 | .get(`/api/repositories/${owner |
| 369 | .expect(200); |
| 370 | const events = |
| 371 | .filter((item: |
| 372 | .map((item: |
| 373 | expect(events).toEqual |
| 374 | 'opened', |
| 375 | 'ready_for_review', |
| 376 | 'converted_to_draft', |
| 377 | 'ready_for_review', |
| 378 | ]); |
| 379 | }); |
| 380 | |
| 381 | it('comments on a range of lines, which must start before it ends and sit inside the diff', |
| 382 | const range = |
| 383 | review(bystander.cookie |
| 384 | state: 'commented', |
| 385 | comments: [ |
| 386 | { |
| 387 | path: 'a.txt', |
| 388 | side: 'additions', |
| 389 | line, |
| 390 | startLine, |
| 391 | body: 'Both lines', |
| 392 | }, |
| 393 | ], |
| 394 | }); |
| 395 | await range(2, |
| 396 | await range(3, |
| 397 | const { body |
| 398 | expect(body.comments |
| 399 | startLine: 1, |
| 400 | startSide: null, |
| 401 | line: 2, |
| 402 | }); |
| 403 | }); |
| 404 | |
| 405 | it('applies a suggestion as a commit on the head branch for someone who can write to it', |
| 406 | const { body |
| 407 | state: 'commented', |
| 408 | comments: [ |
| 409 | { |
| 410 | path: 'a.txt', |
| 411 | side: 'additions', |
| 412 | line: 2, |
| 413 | body: 'Shout it:\n```suggestion\nTWO |
| 414 | }, |
| 415 | ], |
| 416 | }).expect(201) |
| 417 | const suggestion = |
| 418 | const plain = |
| 419 | await review(bystander |
| 420 | state: 'commented', |
| 421 | comments: [ |
| 422 | { |
| 423 | path: 'a.txt', |
| 424 | side: 'deletions', |
| 425 | line: 1, |
| 426 | body: '```suggestion\nx\n``` |
| 427 | }, |
| 428 | ], |
| 429 | }).expect(201) |
| 430 | ).body.comments[ |
| 431 | |
| 432 | await api() |
| 433 | .post(pull(` |
| 434 | .set('cookie', |
| 435 | .expect(403); |
| 436 | await api() |
| 437 | .post(pull(` |
| 438 | .set('cookie', |
| 439 | .expect(400); |
| 440 | |
| 441 | const applied = |
| 442 | .post(pull(` |
| 443 | .set('cookie', |
| 444 | .expect(201); |
| 445 | expect((await api |
| 446 | applied.body.commitSha |
| 447 | ); |
| 448 | const commits = |
| 449 | expect(commits.body |
| 450 | sha: applied.body |
| 451 | subject: `Apply suggestion from ${bystander |
| 452 | }); |
| 453 | const patch = |
| 454 | .get(pull(' |
| 455 | .query({ path: |
| 456 | .expect(200); |
| 457 | expect(patch.text |
| 458 | |
| 459 | // The branch has moved past the commit the suggestion was made on. |
| 460 | await api() |
| 461 | .post(pull(` |
| 462 | .set('cookie', |
| 463 | .expect(409); |
| 464 | }); |
| 465 | |
| 466 | it('keeps a range inside one hunk, so it never covers lines the diff hides', |
| 467 | const { body |
| 468 | state: 'commented', |
| 469 | comments: [ |
| 470 | { |
| 471 | path: 'b.txt', |
| 472 | side: 'additions', |
| 473 | startLine: 1, |
| 474 | line: 20, |
| 475 | body: 'All of it', |
| 476 | }, |
| 477 | ], |
| 478 | }).expect(400) |
| 479 | expect(body.message |
| 480 | }); |
| 481 | |
| 482 | it('closes reviews once the request is merged', |
| 483 | await api() |
| 484 | .post(pull(' |
| 485 | .set('cookie', |
| 486 | .send({}) |
| 487 | .expect(201); |
| 488 | await review(reviewer |
| 489 | }); |
| 490 | |
| 491 | it('names the files a conflicting request clashes on, and refuses to merge it', |
| 492 | await remote('fetch |
| 493 | git('checkout', |
| 494 | git('checkout', |
| 495 | writeFileSync(path.join |
| 496 | git('commit', |
| 497 | git('checkout', |
| 498 | writeFileSync(path.join |
| 499 | git('commit', |
| 500 | await push('main |
| 501 | |
| 502 | const { body: |
| 503 | .post(`/api/repositories/${owner |
| 504 | .set('cookie', |
| 505 | .send({ title: |
| 506 | .expect(201); |
| 507 | const at = |
| 508 | |
| 509 | const { body |
| 510 | expect(body).toMatchObject |
| 511 | const merge = |
| 512 | .post(`${at |
| 513 | .set('cookie', |
| 514 | .send({}) |
| 515 | .expect(409); |
| 516 | expect(merge.body |
| 517 | }); |
| 518 | }); |