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