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