| 1 | import { describe, expect, it } from 'vitest'; |
| 2 | |
| 3 | import type { WebhookBody |
| 4 | import { renderWebhookBody as |
| 5 | import { messageOf } |
| 6 | |
| 7 | const repository = { |
| 8 | id: 'repo_1', |
| 9 | fullName: 'ada/app', |
| 10 | description: null, |
| 11 | visibility: 'public' as |
| 12 | defaultBranch: 'main', |
| 13 | htmlUrl: 'https://ghost.test/ada/app', |
| 14 | }; |
| 15 | const thread = { |
| 16 | id: 'issue_1', |
| 17 | number: 7, |
| 18 | title: 'Bell count', |
| 19 | body: 'The bell should count', |
| 20 | state: 'open' as |
| 21 | author: null, |
| 22 | htmlUrl: 'https://ghost.test/ada/app/issues/7', |
| 23 | }; |
| 24 | const body = ( |
| 25 | event: 'issue.opened', |
| 26 | repository, |
| 27 | sender: { id: ' |
| 28 | createdAt: '2026-09-30T00:00:00.000Z', |
| 29 | ...extra, |
| 30 | }); |
| 31 | const commit = ( |
| 32 | sha: `${n} |
| 33 | authorName: 'Ada', |
| 34 | authorEmail: 'ada@example.com', |
| 35 | committedAt: '2026-09-30T00:00:00.000Z', |
| 36 | subject: `Commit ${n |
| 37 | body: '', |
| 38 | }); |
| 39 | const opened = body |
| 40 | const sender = { |
| 41 | name: 'Ghost', |
| 42 | iconUrl: 'https://ghost.test/icons/icon-192.png', |
| 43 | }; |
| 44 | const send = ( |
| 45 | render(url, sent |
| 46 | |
| 47 | describe('render', () |
| 48 | it.each([ |
| 49 | ['https://hooks.slack.com/services/T0/B0/x', 'slack' |
| 50 | ['https://discord.com/api/webhooks/1/x', 'discord' |
| 51 | ['https://discordapp.com/api/webhooks/1/x', 'discord' |
| 52 | ['https://canary.discord.com/api/webhooks/1/x', 'discord' |
| 53 | ['https://chat.googleapis.com/v1/spaces/AAA/messages?key=k', 'google chat' |
| 54 | ['https://contoso.webhook.office.com/webhookb2/x', 'teams' |
| 55 | [ |
| 56 | 'https://default1.environment.api.powerplatform.com/powerautomate/automations/direct/workflows/x/triggers/manual/paths/invoke', |
| 57 | 'teams', |
| 58 | ], |
| 59 | // Slack Workflow Builder wants its own variables, not a message |
| 60 | ['https://hooks.slack.com/triggers/T0/1/x', 'ghost' |
| 61 | ['https://discord.com/channels/1/2', 'ghost' |
| 62 | ['https://prod-1.westus.logic.azure.com/workflows/x/triggers', 'ghost' |
| 63 | ['https://example.com/hooks.slack.com/services/x', 'ghost' |
| 64 | ['https://93.184.216.34/hook', 'ghost' |
| 65 | ])('sends %s as %s', (url |
| 66 | const sent = |
| 67 | const format = |
| 68 | 'embeds' in sent |
| 69 | ? 'discord' |
| 70 | : 'attachments' in sent |
| 71 | ? 'teams' |
| 72 | : 'unfurl_links' in sent |
| 73 | ? 'slack' |
| 74 | : 'text' in sent |
| 75 | ? 'google chat' |
| 76 | : 'ghost'; |
| 77 | expect(format).toBe |
| 78 | }); |
| 79 | |
| 80 | it('sends everything else Ghost’s own JSON', |
| 81 | expect(JSON.parse |
| 82 | opened, |
| 83 | ); |
| 84 | expect(JSON.parse |
| 85 | }); |
| 86 | |
| 87 | it('keeps user text from pinging a channel or forming a link', |
| 88 | const loud = |
| 89 | issue: { ...thread |
| 90 | }); |
| 91 | |
| 92 | expect( |
| 93 | JSON.parse(send |
| 94 | ).toEqual({ |
| 95 | username: 'Ghost', |
| 96 | icon_url: 'https://ghost.test/icons/icon-192.png', |
| 97 | text: [ |
| 98 | '[ada/app] <https://ghost.test/ada/app/issues/7|ada opened issue #7: <!channel> & <users/all> @everyone>', |
| 99 | '>The bell should count', |
| 100 | ].join('\n' |
| 101 | unfurl_links: false, |
| 102 | }); |
| 103 | expect( |
| 104 | JSON.parse(send |
| 105 | ).toEqual({ |
| 106 | username: 'Ghost', |
| 107 | avatar_url: 'https://ghost.test/icons/icon-192.png', |
| 108 | allowed_mentions: { parse: [] |
| 109 | embeds: [ |
| 110 | { |
| 111 | title: 'ada opened issue #7: <!channel> & <users/all> @everyone', |
| 112 | url: 'https://ghost.test/ada/app/issues/7', |
| 113 | description: 'The bell should count', |
| 114 | footer: { text: ' |
| 115 | }, |
| 116 | ], |
| 117 | }); |
| 118 | expect( |
| 119 | JSON.parse(send |
| 120 | .text, |
| 121 | ).toBe( |
| 122 | [ |
| 123 | '*ada/app*', |
| 124 | '<https://ghost.test/ada/app/issues/7|ada opened issue #7: ‹!channel› & ‹users/all› @everyone>', |
| 125 | 'The bell should count', |
| 126 | ].join('\n' |
| 127 | ); |
| 128 | }); |
| 129 | |
| 130 | it('sends a ping without a link, as a card in Teams', |
| 131 | const ping = |
| 132 | event: 'ping' as |
| 133 | webhook: { |
| 134 | id: 'whk_1', |
| 135 | url: 'https://x.webhook.office.com/a', |
| 136 | events: ['push'], |
| 137 | }, |
| 138 | organization: { slug: ' |
| 139 | createdAt: '2026-09-30T00:00:00.000Z', |
| 140 | }; |
| 141 | const card = |
| 142 | .attachments[0].content |
| 143 | |
| 144 | expect(card.body |
| 145 | 'acme', |
| 146 | 'Ghost webhook connected', |
| 147 | 'Events: push', |
| 148 | ]); |
| 149 | expect(card.actions |
| 150 | expect( |
| 151 | JSON.parse(send |
| 152 | ).toEqual({ |
| 153 | title: 'Ghost webhook connected', |
| 154 | description: 'Events: push', |
| 155 | footer: { text: ' |
| 156 | }); |
| 157 | expect( |
| 158 | JSON.parse(send |
| 159 | ).toBe('[acme] Ghost webhook connected\n |
| 160 | expect( |
| 161 | JSON.parse( |
| 162 | send('https://chat.googleapis.com/v1/spaces/A/messages', |
| 163 | ...ping, |
| 164 | organization: undefined, |
| 165 | repository: { fullName: ' |
| 166 | }), |
| 167 | ).text, |
| 168 | ).toBe('*ada/app*\n |
| 169 | }); |
| 170 | |
| 171 | it('adds an Open button in Teams when there is somewhere to go', |
| 172 | const card = |
| 173 | send('https://x.webhook.office.com/a', |
| 174 | ).attachments[0]. |
| 175 | expect(card.actions |
| 176 | { type: 'Action.OpenUrl', |
| 177 | ]); |
| 178 | expect(card.body |
| 179 | }); |
| 180 | }); |
| 181 | |
| 182 | describe('messageOf', () |
| 183 | const pull = |
| 184 | const release = |
| 185 | id: 'release_1', |
| 186 | tagName: 'v1', |
| 187 | name: null, |
| 188 | body: 'Notes', |
| 189 | htmlUrl: 'https://ghost.test/ada/app/releases/tag/v1', |
| 190 | }; |
| 191 | const label = |
| 192 | id: 'label_1', |
| 193 | name: 'bug', |
| 194 | description: null, |
| 195 | color: 'ff0000', |
| 196 | }; |
| 197 | const bob = |
| 198 | const comment = |
| 199 | const review = |
| 200 | id: 'prr_1', |
| 201 | state: 'changes_requested' as |
| 202 | body: 'Almost', |
| 203 | dismissalMessage: 'Stale', |
| 204 | }; |
| 205 | |
| 206 | it.each<[ |
| 207 | [ |
| 208 | { issue: thread |
| 209 | 'ada opened issue #7: Bell count', |
| 210 | thread.htmlUrl, |
| 211 | 'The bell should count', |
| 212 | ], |
| 213 | [ |
| 214 | { event: 'issue.edited', |
| 215 | 'ada edited issue #7: Bell count', |
| 216 | thread.htmlUrl, |
| 217 | null, |
| 218 | ], |
| 219 | [ |
| 220 | { event: 'issue.closed', |
| 221 | 'ada closed pull request #7: Bell count', |
| 222 | pull.htmlUrl, |
| 223 | null, |
| 224 | ], |
| 225 | [ |
| 226 | { event: 'issue.reopened', |
| 227 | 'ada reopened issue #7: Bell count', |
| 228 | thread.htmlUrl, |
| 229 | null, |
| 230 | ], |
| 231 | [ |
| 232 | { event: 'issue.assigned', |
| 233 | 'ada assigned bob to issue #7: Bell count', |
| 234 | thread.htmlUrl, |
| 235 | null, |
| 236 | ], |
| 237 | [ |
| 238 | { event: 'issue.unassigned', |
| 239 | 'ada unassigned bob from issue #7: Bell count', |
| 240 | thread.htmlUrl, |
| 241 | null, |
| 242 | ], |
| 243 | [ |
| 244 | { event: 'issue.labeled', |
| 245 | 'ada added the label bug to issue #7: Bell count', |
| 246 | thread.htmlUrl, |
| 247 | null, |
| 248 | ], |
| 249 | [ |
| 250 | { event: 'issue.unlabeled', |
| 251 | 'ada removed the label bug from issue #7: Bell count', |
| 252 | thread.htmlUrl, |
| 253 | null, |
| 254 | ], |
| 255 | [ |
| 256 | { event: 'issue.commented', |
| 257 | 'ada commented on pull request #7: Bell count', |
| 258 | pull.htmlUrl, |
| 259 | 'On it', |
| 260 | ], |
| 261 | [ |
| 262 | { event: 'issue.comment_edited', |
| 263 | 'ada edited a comment on issue #7: Bell count', |
| 264 | thread.htmlUrl, |
| 265 | 'On it', |
| 266 | ], |
| 267 | [ |
| 268 | { event: 'issue.comment_deleted', |
| 269 | 'ada deleted a comment on issue #7: Bell count', |
| 270 | thread.htmlUrl, |
| 271 | null, |
| 272 | ], |
| 273 | [ |
| 274 | { event: 'pull_request.ready_for_review', |
| 275 | 'ada marked pull request #7: Bell count ready for review', |
| 276 | pull.htmlUrl, |
| 277 | null, |
| 278 | ], |
| 279 | [ |
| 280 | { event: 'pull_request.converted_to_draft', |
| 281 | 'ada converted pull request #7: Bell count to a draft', |
| 282 | pull.htmlUrl, |
| 283 | null, |
| 284 | ], |
| 285 | [ |
| 286 | { event: 'pull_request.merged', |
| 287 | 'ada merged pull request #7: Bell count', |
| 288 | pull.htmlUrl, |
| 289 | null, |
| 290 | ], |
| 291 | [ |
| 292 | { event: 'pull_request.reviewed', |
| 293 | 'ada requested changes on pull request #7: Bell count', |
| 294 | pull.htmlUrl, |
| 295 | 'Almost', |
| 296 | ], |
| 297 | [ |
| 298 | { event: 'pull_request.review_dismissed', |
| 299 | 'ada dismissed a review on pull request #7: Bell count', |
| 300 | pull.htmlUrl, |
| 301 | 'Stale', |
| 302 | ], |
| 303 | [ |
| 304 | { |
| 305 | event: 'pull_request.review_commented', |
| 306 | pullRequest: pull, |
| 307 | comment: { ...comment |
| 308 | }, |
| 309 | 'ada commented on bell.ts in pull request #7: Bell count', |
| 310 | pull.htmlUrl, |
| 311 | 'On it', |
| 312 | ], |
| 313 | [ |
| 314 | { event: 'label.created', |
| 315 | 'ada created the label bug', |
| 316 | `${repository.htmlUrl} |
| 317 | null, |
| 318 | ], |
| 319 | [ |
| 320 | { event: 'label.edited', |
| 321 | 'ada edited the label bug', |
| 322 | `${repository.htmlUrl} |
| 323 | null, |
| 324 | ], |
| 325 | [ |
| 326 | { event: 'label.deleted', |
| 327 | 'ada deleted the label bug', |
| 328 | `${repository.htmlUrl} |
| 329 | null, |
| 330 | ], |
| 331 | [ |
| 332 | { event: 'release.created', |
| 333 | 'ada created the release v1', |
| 334 | release.htmlUrl, |
| 335 | 'Notes', |
| 336 | ], |
| 337 | [ |
| 338 | { event: 'release.published', |
| 339 | 'ada published the release One', |
| 340 | release.htmlUrl, |
| 341 | 'Notes', |
| 342 | ], |
| 343 | [ |
| 344 | { event: 'release.edited', |
| 345 | 'ada edited the release v1', |
| 346 | release.htmlUrl, |
| 347 | 'Notes', |
| 348 | ], |
| 349 | [ |
| 350 | { |
| 351 | event: 'release.deleted', |
| 352 | release: { id: ' |
| 353 | }, |
| 354 | 'ada deleted the release v1', |
| 355 | `${repository.htmlUrl} |
| 356 | null, |
| 357 | ], |
| 358 | [ |
| 359 | { event: 'star.created' |
| 360 | 'ada starred ada/app', |
| 361 | repository.htmlUrl, |
| 362 | null, |
| 363 | ], |
| 364 | [ |
| 365 | { event: 'star.deleted' |
| 366 | 'ada unstarred ada/app', |
| 367 | repository.htmlUrl, |
| 368 | null, |
| 369 | ], |
| 370 | [ |
| 371 | { event: 'watch.started' |
| 372 | 'ada started watching ada/app', |
| 373 | repository.htmlUrl, |
| 374 | null, |
| 375 | ], |
| 376 | [ |
| 377 | { |
| 378 | event: 'fork.created', |
| 379 | fork: { |
| 380 | ...repository, |
| 381 | fullName: 'bob/app', |
| 382 | htmlUrl: 'https://ghost.test/bob/app', |
| 383 | }, |
| 384 | }, |
| 385 | 'ada forked ada/app to bob/app', |
| 386 | 'https://ghost.test/bob/app', |
| 387 | null, |
| 388 | ], |
| 389 | [ |
| 390 | { event: 'repository.edited' |
| 391 | 'ada edited the settings of ada/app', |
| 392 | repository.htmlUrl, |
| 393 | null, |
| 394 | ], |
| 395 | [ |
| 396 | { event: 'repository.transferred', |
| 397 | 'ada transferred ada/app from bob', |
| 398 | repository.htmlUrl, |
| 399 | null, |
| 400 | ], |
| 401 | [ |
| 402 | { event: 'member.added' |
| 403 | 'ada accepted the invitation to collaborate on ada/app', |
| 404 | repository.htmlUrl, |
| 405 | null, |
| 406 | ], |
| 407 | [ |
| 408 | { event: 'member.removed', |
| 409 | 'ada removed bob as a collaborator', |
| 410 | repository.htmlUrl, |
| 411 | null, |
| 412 | ], |
| 413 | [ |
| 414 | { |
| 415 | event: 'push', |
| 416 | ref: 'refs/heads/main', |
| 417 | created: false, |
| 418 | deleted: false, |
| 419 | commits: [commit(1 |
| 420 | }, |
| 421 | 'ada pushed 1 commit to main', |
| 422 | `${repository.htmlUrl} |
| 423 | `${'1'.repeat |
| 424 | ], |
| 425 | [ |
| 426 | { |
| 427 | event: 'push', |
| 428 | ref: 'refs/tags/v1', |
| 429 | created: true, |
| 430 | deleted: false, |
| 431 | commits: [], |
| 432 | }, |
| 433 | 'ada created tag v1', |
| 434 | `${repository.htmlUrl} |
| 435 | null, |
| 436 | ], |
| 437 | [ |
| 438 | { |
| 439 | event: 'push', |
| 440 | ref: 'refs/heads/old', |
| 441 | created: false, |
| 442 | deleted: true, |
| 443 | commits: [], |
| 444 | }, |
| 445 | 'ada deleted branch old', |
| 446 | repository.htmlUrl, |
| 447 | null, |
| 448 | ], |
| 449 | [ |
| 450 | { event: 'issue.opened', |
| 451 | 'Someone opened issue #7: Bell count', |
| 452 | thread.htmlUrl, |
| 453 | 'The bell should count', |
| 454 | ], |
| 455 | ])('describes %o', (extra |
| 456 | expect(messageOf(body |
| 457 | context: 'ada/app', |
| 458 | summary, |
| 459 | url, |
| 460 | excerpt, |
| 461 | }); |
| 462 | }); |
| 463 | |
| 464 | it('lists the first commits of a big push and counts the rest', |
| 465 | const { excerpt |
| 466 | body({ |
| 467 | event: 'push', |
| 468 | ref: 'refs/heads/main', |
| 469 | commits: [1, 2, |
| 470 | }), |
| 471 | ); |
| 472 | expect(excerpt?.split |
| 473 | expect(excerpt).toMatch |
| 474 | }); |
| 475 | |
| 476 | it('cuts a long excerpt short', |
| 477 | const { excerpt |
| 478 | body({ issue: |
| 479 | ); |
| 480 | expect(excerpt).toHaveLength |
| 481 | expect(excerpt?.endsWith |
| 482 | }); |
| 483 | }); |