| 1 | import path from 'node:path'; |
| 2 | import { createDatabase, type |
| 3 | import { and, eq |
| 4 | import { migrate } |
| 5 | import { |
| 6 | afterAll, |
| 7 | beforeAll, |
| 8 | beforeEach, |
| 9 | describe, |
| 10 | expect, |
| 11 | it, |
| 12 | vi, |
| 13 | } from 'vitest'; |
| 14 | |
| 15 | import { publishEvent } |
| 16 | import { closeIssue } |
| 17 | import type { MailService |
| 18 | import { IssuesService } |
| 19 | import { NotificationsService } |
| 20 | import { OutboxService } |
| 21 | import { RepositoryAccessService } |
| 22 | import { IssueReferencesService } |
| 23 | import { UsersService } |
| 24 | import { NotifierService } |
| 25 | import type { WebhookFanoutService |
| 26 | |
| 27 | const CONNECTION = process |
| 28 | const noWebhooks = { |
| 29 | handle: async () |
| 30 | } as unknown as |
| 31 | const MIGRATIONS = path |
| 32 | import.meta.dirname, |
| 33 | '../../../../../packages/db/drizzle', |
| 34 | ); |
| 35 | |
| 36 | const OWNER = ' |
| 37 | const ALICE = ' |
| 38 | const BOB = ' |
| 39 | const CAROL = ' |
| 40 | const USERS = [ |
| 41 | const ORGANIZATION = ' |
| 42 | |
| 43 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 44 | describe.skipIf(!CONNECTION |
| 45 | let db: |
| 46 | let pool: |
| 47 | let issues: |
| 48 | let notifier: |
| 49 | let outbox: |
| 50 | let inbox: |
| 51 | let sendThreadEmail: |
| 52 | let repository: |
| 53 | let secret: |
| 54 | |
| 55 | const ref = |
| 56 | username: 'ntf-owner', |
| 57 | repo: 'app', |
| 58 | number, |
| 59 | requesterId, |
| 60 | }); |
| 61 | |
| 62 | const open = |
| 63 | body: string |
| 64 | authorId = OWNER, |
| 65 | assignees: string[] |
| 66 | ) => |
| 67 | issues.createIssue({ |
| 68 | username: 'ntf-owner', |
| 69 | repo: 'app', |
| 70 | requesterId: authorId, |
| 71 | body: { title: ' |
| 72 | }); |
| 73 | |
| 74 | /** Notifications as each user's inbox holds them, keyed by user id. */ |
| 75 | async function notified |
| 76 | const rows = |
| 77 | .select({ |
| 78 | userId: schema.notification |
| 79 | reason: schema.notification |
| 80 | eventType: schema.notification |
| 81 | unread: schema.notification |
| 82 | }) |
| 83 | .from(schema.notification |
| 84 | .where(eq(schema |
| 85 | return Object.fromEntries |
| 86 | } |
| 87 | |
| 88 | const emailsTo = |
| 89 | sendThreadEmail.mock.calls |
| 90 | |
| 91 | beforeAll(async () |
| 92 | ({ db, pool |
| 93 | await migrate(db |
| 94 | }); |
| 95 | |
| 96 | beforeEach(async () |
| 97 | const access = |
| 98 | issues = new |
| 99 | db, |
| 100 | new UsersService(db |
| 101 | access, |
| 102 | new IssueReferencesService(db |
| 103 | ); |
| 104 | sendThreadEmail = vi. |
| 105 | notifier = new |
| 106 | sendThreadEmail, |
| 107 | } as unknown as |
| 108 | outbox = new |
| 109 | inbox = new |
| 110 | |
| 111 | await db.delete |
| 112 | await db |
| 113 | .delete(schema.organization |
| 114 | .where(eq(schema |
| 115 | await db.insert |
| 116 | USERS.map((id |
| 117 | const name = |
| 118 | return { |
| 119 | id, |
| 120 | name, |
| 121 | email: `ntf-${name |
| 122 | username: `ntf-${name |
| 123 | emailVerified: true, |
| 124 | }; |
| 125 | }), |
| 126 | ); |
| 127 | [repository, secret] |
| 128 | .insert(schema.repository |
| 129 | .values([ |
| 130 | { name: 'app', |
| 131 | { name: 'secret', |
| 132 | ]) |
| 133 | .returning(); |
| 134 | }); |
| 135 | |
| 136 | afterAll(async () |
| 137 | await db.delete |
| 138 | await db |
| 139 | .delete(schema.organization |
| 140 | .where(eq(schema |
| 141 | await pool?.end |
| 142 | }); |
| 143 | |
| 144 | it('notifies mentioned and assigned people when an issue opens, and subscribes them', |
| 145 | const issue = |
| 146 | 'ntf-bob', |
| 147 | ]); |
| 148 | await outbox.drain |
| 149 | |
| 150 | expect(await notified |
| 151 | [ALICE]: { |
| 152 | [BOB]: { |
| 153 | }); |
| 154 | expect(emailsTo().sort |
| 155 | ['ntf-alice@example.com', 'thread-opened' |
| 156 | ['ntf-bob@example.com', 'thread-opened' |
| 157 | ]); |
| 158 | expect(sendThreadEmail.mock |
| 159 | threadId: issue.id |
| 160 | subject: '[ntf-owner/app] Bell (#1)', |
| 161 | context: { |
| 162 | actor: 'ntf-owner', |
| 163 | path: '/ntf-owner/app/issues/1', |
| 164 | number: 1, |
| 165 | }, |
| 166 | }); |
| 167 | |
| 168 | const subscribers = |
| 169 | .select({ userId: |
| 170 | .from(schema.issueSubscription |
| 171 | .where(eq(schema |
| 172 | expect(subscribers.map |
| 173 | [ALICE, BOB, |
| 174 | ); |
| 175 | }); |
| 176 | |
| 177 | it('notifies the author and earlier commenters about a comment, never the commenter', |
| 178 | const issue = |
| 179 | await issues.createComment |
| 180 | ...ref(CAROL, |
| 181 | body: 'Same here.', |
| 182 | }); |
| 183 | await outbox.drain |
| 184 | expect(await notified |
| 185 | [OWNER]: { |
| 186 | }); |
| 187 | |
| 188 | await issues.createComment |
| 189 | await outbox.drain |
| 190 | expect(await notified |
| 191 | [OWNER]: { |
| 192 | [CAROL]: { |
| 193 | reason: 'subscribed', |
| 194 | eventType: 'issue.commented', |
| 195 | unread: true, |
| 196 | }, |
| 197 | }); |
| 198 | expect(sendThreadEmail.mock |
| 199 | template: 'thread-comment', |
| 200 | context: { body: ' |
| 201 | }); |
| 202 | }); |
| 203 | |
| 204 | it('follows watching and ignoring, and an unsubscribe outlasts commenting but not a mention', |
| 205 | await inbox.setWatch |
| 206 | username: 'ntf-owner', |
| 207 | repo: 'app', |
| 208 | requesterId: ALICE, |
| 209 | level: 'all', |
| 210 | }); |
| 211 | await inbox.setWatch |
| 212 | username: 'ntf-owner', |
| 213 | repo: 'app', |
| 214 | requesterId: BOB, |
| 215 | level: 'ignore', |
| 216 | }); |
| 217 | const issue = |
| 218 | await outbox.drain |
| 219 | expect(await notified |
| 220 | [ALICE]: { |
| 221 | }); |
| 222 | |
| 223 | await inbox.setSubscription |
| 224 | ...ref(ALICE, |
| 225 | subscribed: false, |
| 226 | }); |
| 227 | await issues.createComment |
| 228 | await issues.createComment |
| 229 | ...ref(OWNER, |
| 230 | body: 'hm indeed', |
| 231 | }); |
| 232 | await outbox.drain |
| 233 | expect((await notified |
| 234 | |
| 235 | await issues.createComment |
| 236 | ...ref(OWNER, |
| 237 | body: '@ntf-alice ping', |
| 238 | }); |
| 239 | await outbox.drain |
| 240 | expect((await notified |
| 241 | reason: 'mentioned', |
| 242 | }); |
| 243 | await expect( |
| 244 | inbox.getSubscription(ref |
| 245 | ).resolves.toEqual( |
| 246 | |
| 247 | await inbox.setWatch |
| 248 | username: 'ntf-owner', |
| 249 | repo: 'app', |
| 250 | requesterId: ALICE, |
| 251 | level: 'participating', |
| 252 | }); |
| 253 | await expect( |
| 254 | inbox.getWatch({ |
| 255 | username: 'ntf-owner', |
| 256 | repo: 'app', |
| 257 | requesterId: ALICE, |
| 258 | }), |
| 259 | ).resolves.toEqual( |
| 260 | }); |
| 261 | |
| 262 | it('never notifies or subscribes someone mentioned in a repository they cannot read', |
| 263 | const hidden = |
| 264 | username: 'ntf-owner', |
| 265 | repo: 'secret', |
| 266 | requesterId: OWNER, |
| 267 | body: { title: ' |
| 268 | }); |
| 269 | await outbox.drain |
| 270 | |
| 271 | expect(await notified |
| 272 | const subscribed = |
| 273 | .select() |
| 274 | .from(schema.issueSubscription |
| 275 | .where( |
| 276 | and( |
| 277 | eq(schema.issueSubscription |
| 278 | eq(schema.issueSubscription |
| 279 | ), |
| 280 | ); |
| 281 | expect(subscribed).toEqual |
| 282 | expect(secret.visibility |
| 283 | }); |
| 284 | |
| 285 | it('notifies about assigning, closing, reopening and merging', |
| 286 | const issue = |
| 287 | await issues.setIssueAssignees |
| 288 | ...ref(OWNER, |
| 289 | usernames: ['ntf-alice'], |
| 290 | }); |
| 291 | await outbox.drain |
| 292 | expect(await notified |
| 293 | [ALICE]: { |
| 294 | reason: 'assigned', |
| 295 | eventType: 'issue.assigned', |
| 296 | unread: true, |
| 297 | }, |
| 298 | [CAROL]: { |
| 299 | }); |
| 300 | |
| 301 | await issues.closeIssue |
| 302 | await issues.reopenIssue |
| 303 | await outbox.drain |
| 304 | expect( |
| 305 | sendThreadEmail.mock.calls |
| 306 | .map(([, email |
| 307 | .slice(-4), |
| 308 | ).toEqual(['closed' |
| 309 | |
| 310 | await closeIssue(db |
| 311 | await outbox.drain |
| 312 | expect((await notified |
| 313 | eventType: 'pull_request.merged', |
| 314 | }); |
| 315 | }); |
| 316 | |
| 317 | it('leaves webhook-only events out of the inbox', |
| 318 | const issue = |
| 319 | await outbox.drain |
| 320 | const before = |
| 321 | sendThreadEmail.mockClear(); |
| 322 | |
| 323 | await issues.updateIssue |
| 324 | ...ref(CAROL, |
| 325 | body: { title: ' |
| 326 | }); |
| 327 | await outbox.drain |
| 328 | |
| 329 | expect(await notified |
| 330 | expect(sendThreadEmail).not |
| 331 | }); |
| 332 | |
| 333 | it('notifies about a review and a reply to one of its comments', |
| 334 | const pull = |
| 335 | { |
| 336 | repository, |
| 337 | title: 'Bell', |
| 338 | body: null, |
| 339 | authorId: OWNER, |
| 340 | isPullRequest: true, |
| 341 | }, |
| 342 | async (tx, |
| 343 | await tx.insert |
| 344 | issueId: row.id |
| 345 | baseRepositoryId: repository.id |
| 346 | baseRef: 'main', |
| 347 | headRepositoryId: repository.id |
| 348 | headRef: 'bell', |
| 349 | headSha: 'aaaa', |
| 350 | }); |
| 351 | }, |
| 352 | ); |
| 353 | const [{ id: |
| 354 | .select({ id: |
| 355 | .from(schema.pullRequest |
| 356 | .where(eq(schema |
| 357 | const [review] |
| 358 | .insert(schema.pullRequestReview |
| 359 | .values({ |
| 360 | pullRequestId, |
| 361 | authorId: ALICE, |
| 362 | state: 'changes_requested', |
| 363 | body: 'Nearly.', |
| 364 | commitSha: 'aaaa', |
| 365 | submittedAt: new Date |
| 366 | }) |
| 367 | .returning(); |
| 368 | const line = |
| 369 | pullRequestId, |
| 370 | path: 'src/bell.tsx', |
| 371 | side: 'additions' as |
| 372 | line: 3, |
| 373 | commitSha: 'aaaa', |
| 374 | }; |
| 375 | const [comment] |
| 376 | .insert(schema.pullRequestReviewComment |
| 377 | .values({ |
| 378 | ...line, |
| 379 | reviewId: review.id |
| 380 | authorId: ALICE, |
| 381 | body: 'ask @ntf-carol', |
| 382 | }) |
| 383 | .returning(); |
| 384 | const [reply] |
| 385 | .insert(schema.pullRequestReviewComment |
| 386 | .values({ |
| 387 | ...line, |
| 388 | inReplyToId: comment.id |
| 389 | authorId: OWNER, |
| 390 | body: 'Done.', |
| 391 | }) |
| 392 | .returning(); |
| 393 | await publishEvent(db |
| 394 | type: 'pull_request.reviewed', |
| 395 | repositoryId: repository.id |
| 396 | actorId: ALICE, |
| 397 | payload: { issueId: pull |
| 398 | }); |
| 399 | await outbox.drain |
| 400 | |
| 401 | expect(await notified |
| 402 | [OWNER]: { |
| 403 | reason: 'author', |
| 404 | eventType: 'pull_request.reviewed', |
| 405 | unread: true, |
| 406 | }, |
| 407 | [CAROL]: { |
| 408 | reason: 'mentioned', |
| 409 | eventType: 'pull_request.reviewed', |
| 410 | unread: true, |
| 411 | }, |
| 412 | }); |
| 413 | expect(sendThreadEmail.mock |
| 414 | template: 'thread-review', |
| 415 | context: { |
| 416 | state: 'changes_requested', |
| 417 | body: 'Nearly.', |
| 418 | commentCount: 1, |
| 419 | path: '/ntf-owner/app/pulls/1', |
| 420 | }, |
| 421 | }); |
| 422 | |
| 423 | sendThreadEmail.mockClear(); |
| 424 | await publishEvent(db |
| 425 | type: 'pull_request.review_commented', |
| 426 | repositoryId: repository.id |
| 427 | actorId: OWNER, |
| 428 | payload: { issueId: pull |
| 429 | }); |
| 430 | await outbox.drain |
| 431 | expect(emailsTo().sort |
| 432 | ['ntf-alice@example.com', 'thread-comment' |
| 433 | ['ntf-carol@example.com', 'thread-comment' |
| 434 | ]); |
| 435 | expect(sendThreadEmail.mock |
| 436 | body: 'Done.', |
| 437 | file: 'src/bell.tsx', |
| 438 | }); |
| 439 | }); |
| 440 | |
| 441 | it('emails only verified addresses, keeps line breaks out of the subject, and stops reading mentions after fifty', |
| 442 | await db |
| 443 | .update(schema.user |
| 444 | .set({ emailVerified: |
| 445 | .where(eq(schema |
| 446 | const issue = |
| 447 | username: 'ntf-owner', |
| 448 | repo: 'app', |
| 449 | requesterId: OWNER, |
| 450 | body: { |
| 451 | title: 'Line\r\nBcc: everyone@example.com', |
| 452 | body: '@ntf-alice @ntf-bob', |
| 453 | }, |
| 454 | }); |
| 455 | await outbox.drain |
| 456 | |
| 457 | expect(Object.keys |
| 458 | [ALICE, BOB] |
| 459 | ); |
| 460 | expect(emailsTo()).toEqual |
| 461 | expect(sendThreadEmail.mock |
| 462 | '[ntf-owner/app] Line Bcc: everyone@example.com (#1)', |
| 463 | ); |
| 464 | |
| 465 | const crowd = |
| 466 | { length: 50 }, |
| 467 | (_, index) |
| 468 | ).join(' ' |
| 469 | await issues.createComment |
| 470 | ...ref(OWNER, |
| 471 | body: `${crowd} |
| 472 | }); |
| 473 | await outbox.drain |
| 474 | expect((await notified |
| 475 | }); |
| 476 | |
| 477 | it('pages a team only for its own organization, and only from a member', |
| 478 | await db.insert |
| 479 | id: ORGANIZATION, |
| 480 | name: 'Acme', |
| 481 | slug: 'ntf-acme', |
| 482 | createdAt: new Date |
| 483 | }); |
| 484 | await db.insert |
| 485 | id: 'member_ntf_owner', |
| 486 | organizationId: ORGANIZATION, |
| 487 | userId: OWNER, |
| 488 | role: 'owner', |
| 489 | createdAt: new Date |
| 490 | }); |
| 491 | await db.insert |
| 492 | id: 'team_ntf', |
| 493 | name: 'Core Team', |
| 494 | organizationId: ORGANIZATION, |
| 495 | createdAt: new Date |
| 496 | }); |
| 497 | await db.insert |
| 498 | { id: 'tm_ntf_alice', |
| 499 | { id: 'tm_ntf_bob', |
| 500 | ]); |
| 501 | await db |
| 502 | .update(schema.repository |
| 503 | .set({ organizationId: |
| 504 | .where(eq(schema |
| 505 | const org = |
| 506 | |
| 507 | const fromMember = |
| 508 | ...org, |
| 509 | requesterId: OWNER, |
| 510 | body: { title: ' |
| 511 | }); |
| 512 | const fromOutsider = |
| 513 | ...org, |
| 514 | requesterId: CAROL, |
| 515 | body: { title: ' |
| 516 | }); |
| 517 | await outbox.drain |
| 518 | |
| 519 | expect( |
| 520 | Object.values(await |
| 521 | ).toEqual(['team_mentioned' |
| 522 | expect(await notified |
| 523 | }); |
| 524 | |
| 525 | it('skips an event whose comment was deleted before it was handled', |
| 526 | const issue = |
| 527 | const comment = |
| 528 | ...ref(CAROL, |
| 529 | body: 'oops', |
| 530 | }); |
| 531 | await issues.deleteComment |
| 532 | ...ref(CAROL, |
| 533 | commentId: comment.id |
| 534 | }); |
| 535 | await outbox.drain |
| 536 | |
| 537 | expect(await notified |
| 538 | }); |
| 539 | |
| 540 | it('lists, counts and marks the inbox, hiding threads the user can no longer read', |
| 541 | const first = |
| 542 | const hidden = |
| 543 | username: 'ntf-owner', |
| 544 | repo: 'secret', |
| 545 | requesterId: OWNER, |
| 546 | body: { title: ' |
| 547 | }); |
| 548 | await db.insert |
| 549 | repositoryId: secret.id |
| 550 | userId: ALICE, |
| 551 | role: 'read', |
| 552 | acceptedAt: new Date |
| 553 | }); |
| 554 | await issues.createComment |
| 555 | username: 'ntf-owner', |
| 556 | repo: 'secret', |
| 557 | number: hidden.number |
| 558 | requesterId: OWNER, |
| 559 | body: '@ntf-alice now you can see this', |
| 560 | }); |
| 561 | const second = |
| 562 | await outbox.drain |
| 563 | |
| 564 | const page = |
| 565 | expect(page.notifications |
| 566 | second.number, |
| 567 | hidden.number, |
| 568 | ]); |
| 569 | expect(page.notifications |
| 570 | reason: 'mentioned', |
| 571 | actorUsername: 'ntf-owner', |
| 572 | unread: true, |
| 573 | repository: { owner: ' |
| 574 | thread: { title: ' |
| 575 | }); |
| 576 | const rest = |
| 577 | limit: 2, |
| 578 | cursor: page.nextCursor |
| 579 | }); |
| 580 | expect(rest.notifications |
| 581 | first.number, |
| 582 | ]); |
| 583 | expect(rest.nextCursor |
| 584 | await expect(inbox |
| 585 | |
| 586 | await db |
| 587 | .delete(schema.repositoryCollaborator |
| 588 | .where(eq(schema |
| 589 | await expect(inbox |
| 590 | |
| 591 | await inbox.setUnread |
| 592 | await expect(inbox |
| 593 | notifications: [{ thread: |
| 594 | }); |
| 595 | await inbox.markAllRead |
| 596 | await expect(inbox |
| 597 | await expect( |
| 598 | inbox.setUnread(BOB |
| 599 | ).rejects.toThrow( |
| 600 | }); |
| 601 | |
| 602 | it('retries a failing event and sets it aside after five attempts', |
| 603 | const failing = |
| 604 | db, |
| 605 | { |
| 606 | handle: () => |
| 607 | } as unknown as |
| 608 | noWebhooks, |
| 609 | { pool }, |
| 610 | ); |
| 611 | const issue = |
| 612 | const pending = |
| 613 | db |
| 614 | .select() |
| 615 | .from(schema.outboxEvent |
| 616 | .where(eq(schema |
| 617 | |
| 618 | await failing.drain |
| 619 | expect( |
| 620 | (await pending()). |
| 621 | ).toMatchObject({ |
| 622 | attempts: 1, |
| 623 | lastError: 'mail server on fire', |
| 624 | processedAt: null, |
| 625 | }); |
| 626 | |
| 627 | for (let attempt |
| 628 | const [event] |
| 629 | expect(event.attempts |
| 630 | expect(event.processedAt |
| 631 | }); |
| 632 | |
| 633 | it('drains past a failing event in one run', |
| 634 | const first = |
| 635 | for (let i |
| 636 | const flaky = |
| 637 | db, |
| 638 | { |
| 639 | handle: (event: |
| 640 | event.payload.issueId |
| 641 | ? Promise.reject( |
| 642 | : Promise.resolve() |
| 643 | } as unknown as |
| 644 | noWebhooks, |
| 645 | { pool }, |
| 646 | ); |
| 647 | |
| 648 | await flaky.drain |
| 649 | const pending = |
| 650 | .select() |
| 651 | .from(schema.outboxEvent |
| 652 | .where( |
| 653 | and( |
| 654 | eq(schema.outboxEvent |
| 655 | isNull(schema.outboxEvent |
| 656 | ), |
| 657 | ); |
| 658 | expect(pending).toMatchObject |
| 659 | expect(pending[0] |
| 660 | }); |
| 661 | |
| 662 | it('drains on NOTIFY without polling', |
| 663 | await outbox.onApplicationBootstrap |
| 664 | await outbox.drain |
| 665 | try { |
| 666 | const issue = |
| 667 | await vi.waitFor |
| 668 | expect(await notified |
| 669 | }); |
| 670 | } finally { |
| 671 | await outbox.onApplicationShutdown |
| 672 | } |
| 673 | // a listener handed back to the pool would still hear this and drain |
| 674 | const drain = |
| 675 | for (let i |
| 676 | await pool.query |
| 677 | await new Promise |
| 678 | expect(drain).not |
| 679 | }); |
| 680 | }); |