@crvouga/mockingbird-notifications 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +383 -2
- package/dist/chunk-3HF2NSFV.js +714 -0
- package/dist/chunk-3HF2NSFV.js.map +7 -0
- package/dist/chunk-4QVURMVP.js +400 -0
- package/dist/chunk-4QVURMVP.js.map +7 -0
- package/dist/chunk-FBJ2DRHH.js +235 -0
- package/dist/chunk-FBJ2DRHH.js.map +7 -0
- package/dist/chunk-GHIEYY5B.js +1177 -0
- package/dist/chunk-GHIEYY5B.js.map +7 -0
- package/dist/cli.js +112 -0
- package/dist/cli.js.map +7 -0
- package/dist/index.d.ts +530 -0
- package/dist/index.js +98 -0
- package/dist/index.js.map +7 -0
- package/dist/server.d.ts +262 -0
- package/dist/server.js +17 -0
- package/dist/server.js.map +7 -0
- package/dist/ui.d.ts +599 -0
- package/dist/ui.js +2598 -0
- package/dist/ui.js.map +7 -0
- package/package.json +92 -4
package/README.md
CHANGED
|
@@ -1,3 +1,384 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @crvouga/mockingbird-notifications
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Local emulators. Real API contracts. Part of [Mockingbird](https://github.com/crvouga/mockingbird).
|
|
4
|
+
|
|
5
|
+
A notification test harness for a Mockingbird fleet. It reads the same emulators your application
|
|
6
|
+
sends through and shows every SMS, WhatsApp message, email, push and chat message the way the
|
|
7
|
+
recipient would see it: a phone's Messages app, a phone lock screen (iOS) or notification shade
|
|
8
|
+
(Android), a webmail inbox, and a team chat window.
|
|
9
|
+
Automated tests get the same data from a JSON API and a small client.
|
|
10
|
+
|
|
11
|
+
| Channel | Emulators read | Shown as |
|
|
12
|
+
| --- | --- | --- |
|
|
13
|
+
| SMS | Twilio (Messages and Verify codes), Amazon SNS (direct `PhoneNumber` and `sms` topic subscribers), Customer.io, Mailosaur | Messages app: threads per sender, bubbles, one-time code suggestion |
|
|
14
|
+
| WhatsApp | Twilio (Messages to a `whatsapp:` address, and Verify with `Channel=whatsapp`) | The same phone, as a WhatsApp chat. The channel is `sms` with `sms.via: "whatsapp"`; `to` is the bare number |
|
|
15
|
+
| Email | Resend, Amazon SES (`SendEmail` and MIME `SendRawEmail`), Amazon SNS (`email` / `email-json` subscribers), Customer.io, Mailosaur, Twilio Verify email | Webmail: inbox per recipient (to, cc, bcc), rendered HTML, attachments, headers |
|
|
16
|
+
| Push | Firebase Cloud Messaging | iOS lock screen or Android shade, chosen from the device token's platform; title, body, image, data, badge, sound |
|
|
17
|
+
| Chat | Slack (`chat.postMessage`, `chat.postEphemeral` and incoming webhooks) | Team chat window: a channel list, bot messages with mrkdwn, Block Kit blocks, attachments and reactions |
|
|
18
|
+
|
|
19
|
+
The [Notifications guide](https://mockingbird.chrisvouga.dev/docs/notifications) on the docs site
|
|
20
|
+
splits this README into pages, and the
|
|
21
|
+
[lab ordering example](https://mockingbird.chrisvouga.dev/examples/medical-testing) runs it live.
|
|
22
|
+
|
|
23
|
+
The harness only reads. It sends `GET` requests to each emulator's admin routes (`/__admin/outbox`
|
|
24
|
+
or `/__admin/state/...`) and never changes emulator state. The device art is generic: no vendor
|
|
25
|
+
logos.
|
|
26
|
+
|
|
27
|
+
## Install
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
npm install -D @crvouga/mockingbird-notifications
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Node 22 or newer. Install it next to `@crvouga/mockingbird-fleet` and the emulator packages your
|
|
34
|
+
application uses.
|
|
35
|
+
|
|
36
|
+
## Run it against your fleet
|
|
37
|
+
|
|
38
|
+
The recommended way: one flag on the fleet starts the harness with it, on a page that also has
|
|
39
|
+
every emulator's admin UI (`@crvouga/mockingbird-fleet` brings this package with it, so a project
|
|
40
|
+
that runs a fleet installs nothing more):
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
mockingbird-fleet serve --config mockingbird.json --dashboard # dashboard: http://127.0.0.1:8790
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Or run it on its own: start the fleet as usual, then point the harness at the fleet controller or its ready file:
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
mockingbird-fleet serve --config mockingbird.json --ready-file ready.json
|
|
50
|
+
mockingbird-notifications serve --manifest ready.json # http://127.0.0.1:8790
|
|
51
|
+
# or, with the controller URL from the ready file:
|
|
52
|
+
mockingbird-notifications serve --fleet http://127.0.0.1:9000/__admin/fleet
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
With `--fleet`, the harness asks the controller what each entry is, so aliases work (`push` with
|
|
56
|
+
`target: "fcm"`), and it follows every namespace the fleet has. With `--manifest`, the file is
|
|
57
|
+
re-read on every sync, so a restarted fleet on new ports is picked up. Entries that are not
|
|
58
|
+
messaging emulators (Stripe, Postgres, …) are skipped. Emulators outside a fleet can be named
|
|
59
|
+
directly:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
mockingbird-notifications serve --service twilio=http://127.0.0.1:8798 --service resend=http://127.0.0.1:8794
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
| Flag | Meaning |
|
|
66
|
+
| --- | --- |
|
|
67
|
+
| `--fleet <url>` | Fleet controller `adminBase`. |
|
|
68
|
+
| `--manifest <file>` | The fleet's `--ready-file`. |
|
|
69
|
+
| `--service <name=url>` | One emulator (repeatable). The name is the service (`twilio`, `fcm`, …) or any alias; an alias is identified from the emulator's health route. |
|
|
70
|
+
| `--port`, `--host` | Listener. Default `127.0.0.1:8790`; `--port 0` picks a free port. |
|
|
71
|
+
| `--namespace <name>` | Watch this namespace in the background (repeatable). Default `default`, or every fleet namespace with `--fleet`. Any namespace the UI or an API call asks for is watched from then on. |
|
|
72
|
+
| `--poll <ms>` | Background poll interval, default 750. `0` reads only when asked. |
|
|
73
|
+
| `--admin-key-env <VAR>` | Environment variable with the admin key (default `MOCKINGBIRD_ADMIN_KEY`), sent as `x-mockingbird-admin-key`. |
|
|
74
|
+
| `--tab <name>` | The tab the page opens on: `notifications` (default) or `emulators`. |
|
|
75
|
+
| `--no-emulators` | Serve the notifications UI alone, without the Emulators tab. |
|
|
76
|
+
| `--ready-json` | Print `{"url": …}` once listening. |
|
|
77
|
+
|
|
78
|
+
### The dashboard
|
|
79
|
+
|
|
80
|
+
The page has two tabs. **Emulators** is the admin UI of every HTTP emulator the harness knows,
|
|
81
|
+
messaging or not (Stripe, Cognito, …). **Notifications** is the devices, and its tab counts what
|
|
82
|
+
arrives while you are on the other one. `?tab=emulators` and `?tab=notifications` open a tab; every
|
|
83
|
+
notifications link (`?view=sms&to=…`) still opens the devices.
|
|
84
|
+
|
|
85
|
+
The harness fronts each emulator's admin API on its own port under `/admin/apis/<name>/`, so the
|
|
86
|
+
browser reaches only the harness, and it adds its admin key (`adminKey`, or `adminKeys` by emulator
|
|
87
|
+
name) to requests that carry none. Anyone who can open the page can therefore change emulator
|
|
88
|
+
state: it listens on `127.0.0.1` unless you pass `--host`. `GET /api/admin` lists what the page
|
|
89
|
+
mounts. From code, `createServer` turns this on (`admin: false` turns it off, `defaultTab` picks
|
|
90
|
+
the tab); a harness you serve yourself takes `admin: adminGateway()` from `./server`.
|
|
91
|
+
|
|
92
|
+
## The UI
|
|
93
|
+
|
|
94
|
+
`/` shows four devices side by side. Each has a recipient picker (phone number, push token,
|
|
95
|
+
mailbox, chat channel) that follows the newest arrival unless **Follow new** is off. The **Timeline** tab lists
|
|
96
|
+
everything in one table; double-click any message, or click a timeline row, to inspect the
|
|
97
|
+
normalized record and the emulator's raw one. Read state is kept per browser.
|
|
98
|
+
|
|
99
|
+
The **channel tabs** under the top bar (`All | Text | Push | Email | Chat`) pick one device or all of them. A tab whose device is not on screen counts what arrived there. When the harness has less than 800px to itself (a phone, a side panel, a narrow embed) there is no room for two devices, so it shows one at a time and the tabs switch between them; this follows the width of the element the UI is mounted on, not the window. Every device has the same header above it, so the frames always line up along the top.
|
|
100
|
+
|
|
101
|
+
Links open one device for a screenshot, for example in a Playwright test:
|
|
102
|
+
|
|
103
|
+
| Query | Effect |
|
|
104
|
+
| --- | --- |
|
|
105
|
+
| `?namespace=worker-3` | Show that namespace. |
|
|
106
|
+
| `?view=sms\|push\|email\|chat&to=<recipient>` | Show only that device, opened on that recipient (an SMS link opens the newest thread; a chat recipient is a channel such as `#alerts`). Without `to`, it selects that channel's tab. |
|
|
107
|
+
| `?id=<notification id>` | Open that notification on its device. |
|
|
108
|
+
| `?view=timeline` | Open the timeline. |
|
|
109
|
+
| `?follow=0` | Do not jump to new arrivals. |
|
|
110
|
+
|
|
111
|
+
The devices behave like the real apps, in a generic style with no vendor marks:
|
|
112
|
+
|
|
113
|
+
- **Messages:** swipe a conversation left for Hide Alerts and Delete, or right for Pin and
|
|
114
|
+
Unread. Swipe the open conversation left to see each message's time, and long-press a message
|
|
115
|
+
for tapbacks and Copy. One-time codes show above the keyboard.
|
|
116
|
+
- **Lock screen (iOS):** notifications group by app. Tap a group to open it, swipe a card left for
|
|
117
|
+
Options and Clear, and long-press it for a preview.
|
|
118
|
+
- **Notification shade (Android):** tap a card to expand it, swipe it either way to dismiss it,
|
|
119
|
+
long-press it for its settings, and use Clear all.
|
|
120
|
+
- **WhatsApp:** a message sent to a `whatsapp:` address is its own thread, labelled WhatsApp, and
|
|
121
|
+
opens as a WhatsApp chat on the same phone.
|
|
122
|
+
- **Chat:** channels on the left, and each message with its bot name, time, mrkdwn text, Block Kit
|
|
123
|
+
blocks, attachments and reactions.
|
|
124
|
+
- **Mail:** hover a row for Archive, Delete and Mark as read, then Undo from the toast. Star rows,
|
|
125
|
+
and open a message to read its HTML.
|
|
126
|
+
|
|
127
|
+
Every gesture also works with a click or the keyboard. Dismissing, archiving or deleting only hides
|
|
128
|
+
the message in this browser; the harness keeps every record, and a reload brings it back.
|
|
129
|
+
|
|
130
|
+
**Sound** (off by default; the top bar's speaker toggles it, and `?sound=1` turns it on): new
|
|
131
|
+
arrivals play a synthesized chime in each platform's style, and phones vibrate where the browser
|
|
132
|
+
allows. **Motion:** arrivals, swipes, stacks and screen changes are animated. A viewer's
|
|
133
|
+
reduced-motion setting turns animation off, and so does the `motion: "reduce"` option.
|
|
134
|
+
|
|
135
|
+
## End-to-end tests
|
|
136
|
+
|
|
137
|
+
The UI is built to be driven by Playwright, Cypress or any WebDriver client. It gives tests three
|
|
138
|
+
things:
|
|
139
|
+
|
|
140
|
+
1. **A ready signal.** The root, `[data-testid="notifications-app"]`, gets `data-ready="true"`
|
|
141
|
+
once its first read is drawn. It also reports `data-count`, `data-version`, `data-view`,
|
|
142
|
+
`data-namespace`, `data-connected`, `data-sound` and `data-motion`.
|
|
143
|
+
2. **A `data-testid` on everything a user can see or press.** `TEST_IDS` names them all, and its
|
|
144
|
+
typings say what each one marks.
|
|
145
|
+
3. **The notification's fields on each element that shows it.** Every bubble, thread, card, row,
|
|
146
|
+
open email and timeline row carries `data-notification-id`, `data-channel`, `data-service`,
|
|
147
|
+
`data-source`, `data-to`, `data-from`, `data-status`, `data-created-at` and `data-unread`. A
|
|
148
|
+
bubble that holds a one-time code also has `data-code`. Use these to find an element by what the
|
|
149
|
+
test knows instead of by its position.
|
|
150
|
+
|
|
151
|
+
Wait for the API first, then open a link to the message and assert on what the user would see:
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import { NotificationClient, selectors, TEST_IDS } from "@crvouga/mockingbird-notifications"
|
|
155
|
+
import { expect, test } from "@playwright/test"
|
|
156
|
+
|
|
157
|
+
const notifications = new NotificationClient("http://127.0.0.1:8790")
|
|
158
|
+
|
|
159
|
+
test("the invitation email reaches Ada's inbox", async ({ page }) => {
|
|
160
|
+
// …the app sends the invitation…
|
|
161
|
+
const [invite] = await notifications.waitFor({ channel: "email", to: "ada@example.com", contains: "invited" })
|
|
162
|
+
if (!invite) throw new Error("no invitation")
|
|
163
|
+
|
|
164
|
+
await page.goto(notifications.link({ id: invite.id })) // opens the inbox on that email
|
|
165
|
+
await page.locator(selectors.ready()).waitFor()
|
|
166
|
+
|
|
167
|
+
await expect(page.getByTestId(TEST_IDS.emailSubject)).toHaveText("You're invited")
|
|
168
|
+
// The HTML body is a same-origin iframe.
|
|
169
|
+
const body = page.frameLocator(selectors.testId(TEST_IDS.emailBody))
|
|
170
|
+
await expect(body.getByRole("link", { name: "Accept" })).toBeVisible()
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
test("the sign-in code is offered above the keyboard", async ({ page }) => {
|
|
174
|
+
const code = await notifications.waitForCode("+12025550123")
|
|
175
|
+
const [text] = await notifications.list({ channel: "sms", to: "+12025550123", contains: code })
|
|
176
|
+
await page.goto(notifications.link({ id: text?.id }))
|
|
177
|
+
await expect(page.locator(selectors.smsBubble({ id: text?.id }))).toHaveAttribute("data-code", code)
|
|
178
|
+
await expect(page.getByTestId(TEST_IDS.smsCode)).toHaveAttribute("data-code", code)
|
|
179
|
+
})
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
`selectors` builds the common CSS selectors: `ready()`, `testId(id)`, `notification(id)`,
|
|
183
|
+
`smsBubble({from, id})`, `smsThread(from)`, `pushNotification(id)`, `emailRow(id)`,
|
|
184
|
+
`chatMessage(id)`, `chatChannel(to)`, `channelTab(value)` and `timelineRow(id)`.
|
|
185
|
+
|
|
186
|
+
| Device | Main test ids | State attributes |
|
|
187
|
+
| --- | --- | --- |
|
|
188
|
+
| Top bar | `namespace`, `search`, `view-devices`, `view-timeline`, `follow`, `sound-toggle`, `connection`, `source`, `notice`, `toast`, `toast-undo` | `source`: `data-name`, `data-service`, `data-ok`, `data-count`. `connection`: `data-state`. |
|
|
189
|
+
| Channel tabs | `channel-tabs`, `channel-tab`, `channel-tab-unread` | Root: `data-layout` (`grid` or `tabs`), `data-tab` (`all`, `sms`, `push`, `email`, `chat`). `channel-tab`: `data-value`, `aria-selected`, `data-unread`. |
|
|
190
|
+
| Messages | `sms-device`, `sms-recipient`, `sms-thread` (`-name`, `-preview`, `-time`, `-unread`), `sms-conversation`, `sms-back`, `sms-bubble`, `sms-code`, `sms-menu`, `sms-menu-tapback`, `sms-tapback`, `sms-empty` | `sms-device`: `data-screen` (`threads` or `conversation`), `data-recipient`, `data-thread`. `sms-thread`: `data-pinned`, `data-muted`. |
|
|
191
|
+
| Push | `push-device`, `push-recipient`, `push-skin`, `push-group`, `push-group-more`, `push-notification` (`-title`, `-body`, `-app`, `-time`, `-image`, `-data`, `-meta`), `push-clear`, `push-options`, `push-preview`, `push-settings`, `push-clear-all`, `push-empty` | `push-device`: `data-skin` (`ios` or `android`), `data-platform`. `push-notification`: `data-expanded`, `data-app`, `data-stacked`. |
|
|
192
|
+
| Mail | `email-device`, `email-recipient`, `email-row` (`-from`, `-subject`, `-snippet`, `-date`, `-star`, `-archive`, `-delete`), `email-open`, `email-subject`, `email-from-name`, `email-from-address`, `email-to`, `email-details`, `email-detail`, `email-body`, `email-text`, `email-attachment`, `email-empty` | `email-device`: `data-screen` (`inbox` or `message`). `email-detail`: `data-field`. `email-attachment`: `data-filename`, `data-content-type`, `data-size`. |
|
|
193
|
+
| Chat | `chat-device`, `chat-recipient`, `chat-workspace`, `chat-channel`, `chat-title`, `chat-message` (`-author`, `-time`, `-text`, `-blocks`, `-attachments`, `-reaction`), `chat-unread-count`, `chat-empty` | `chat-device`: `data-recipient`, `data-workspace`. `chat-channel`: `data-to`, `data-unread`, `aria-current`. `chat-message`: `data-method`, `data-ephemeral`, `data-thread`. |
|
|
194
|
+
| Timeline | `timeline`, `timeline-row`, `inspector`, `inspector-json`, `inspector-field`, `inspector-close`, `inspector-show-on-device` | `inspector-json` holds the whole record, `raw` included, as JSON. |
|
|
195
|
+
|
|
196
|
+
A WhatsApp message carries `data-via="whatsapp"` on its thread, bubble and timeline row; `sms-thread-via` is the label in the thread list, and `sms-device` and `sms-conversation` report `data-via` while a WhatsApp chat is open.
|
|
197
|
+
|
|
198
|
+
Controls are real buttons, selects and inputs with accessible names, so `getByRole` works as well.
|
|
199
|
+
New arrivals are drawn live, so a test can keep a page open while the app sends. When several tests
|
|
200
|
+
share a harness, give each one its own namespace (`?namespace=`) or open a deep link to one
|
|
201
|
+
recipient: a link with `to=` stays on that recipient instead of following new arrivals. Set
|
|
202
|
+
`?follow=0` on any other link that should stay put.
|
|
203
|
+
|
|
204
|
+
[`e2e/ui.e2e.ts`](https://github.com/crvouga/mockingbird/blob/main/packages/notifications/e2e/ui.e2e.ts)
|
|
205
|
+
in this repository is a complete, passing example with real Twilio, Resend, FCM and Slack emulators.
|
|
206
|
+
|
|
207
|
+
## Embed the UI
|
|
208
|
+
|
|
209
|
+
`@crvouga/mockingbird-notifications/ui` mounts the same UI on any element, for example as a tab in
|
|
210
|
+
your own development shell. It reads an in-process harness directly, so the emulators, the harness
|
|
211
|
+
and the UI can all run in one browser tab with no server:
|
|
212
|
+
|
|
213
|
+
```ts
|
|
214
|
+
import { createNotificationHarness } from "@crvouga/mockingbird-notifications"
|
|
215
|
+
import { mountNotifications } from "@crvouga/mockingbird-notifications/ui"
|
|
216
|
+
import { createRuntime as resend } from "@crvouga/mockingbird-service-resend"
|
|
217
|
+
import { createRuntime as twilio } from "@crvouga/mockingbird-service-twilio"
|
|
218
|
+
|
|
219
|
+
const harness = createNotificationHarness({ services: { twilio: twilio(), resend: resend() } })
|
|
220
|
+
const panel = document.getElementById("notifications") as HTMLElement
|
|
221
|
+
const ui = mountNotifications(panel, { harness, title: "My app notifications" })
|
|
222
|
+
await ui.ready
|
|
223
|
+
// ui.show({ channel: "sms", to: "+12025550123" }); ui.setSound(true); ui.setNamespace("worker-3")
|
|
224
|
+
ui.destroy()
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
Its styles are scoped to the UI's root (`.mb-notifications`) and added once to the document, or to
|
|
228
|
+
the shadow root that holds `panel`. Options: `harness`, or `fetch` and `baseUrl` for a harness on
|
|
229
|
+
another origin; `channels` (which devices to show); `layout` (`auto`, `tabs` or `grid`) and `tabs` (show the
|
|
230
|
+
channel tabs); `chrome` (show the top bar); `scale` (draw the devices smaller, `0.5` to `1`); `sound`; `motion`
|
|
231
|
+
(`auto`, `full` or `reduce`); `onArrive`; and `syncUrl`, which reads and writes `?view=`-style
|
|
232
|
+
links on the page. The [lab ordering example](https://github.com/crvouga/mockingbird/tree/main/packages/examples/medical-testing)
|
|
233
|
+
mounts it as its Notifications tab.
|
|
234
|
+
|
|
235
|
+
`mountDashboard` puts a tab bar over that UI and an emulators panel you draw:
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
import { createNotificationHarness } from "@crvouga/mockingbird-notifications"
|
|
239
|
+
import { mountDashboard } from "@crvouga/mockingbird-notifications/ui"
|
|
240
|
+
import { createRuntime as twilio } from "@crvouga/mockingbird-service-twilio"
|
|
241
|
+
|
|
242
|
+
const harness = createNotificationHarness({ services: { twilio: twilio() } })
|
|
243
|
+
const dashboard = mountDashboard(document.getElementById("lab") as HTMLElement, {
|
|
244
|
+
title: "Acme lab",
|
|
245
|
+
emulators: {
|
|
246
|
+
// may be async; may return a dispose function
|
|
247
|
+
mount: (panel) => {
|
|
248
|
+
panel.textContent = "Your emulators' admin UI goes here"
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
notifications: { harness }, // what mountNotifications takes
|
|
252
|
+
syncUrl: true, // read and write ?tab=
|
|
253
|
+
})
|
|
254
|
+
dashboard.show("notifications")
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Tabs are your own `tabs` (`{ id, label, mount }`), then Emulators, then Notifications. A tab is
|
|
258
|
+
drawn the first time it is shown and stays mounted; the Notifications tab counts what arrives while
|
|
259
|
+
another is open. Other options: `initialTab`, `onTabChange`, `theme` (`dark`, `light`, `auto`) and
|
|
260
|
+
`injectStyles`. The handle has `show(id)`, `active()`, `tabs()`, `aside`, `notifications` and
|
|
261
|
+
`destroy()`. Give the element a height. The shell is plain DOM with no inline script, so it runs
|
|
262
|
+
under a strict Content-Security-Policy.
|
|
263
|
+
|
|
264
|
+
## Usage
|
|
265
|
+
|
|
266
|
+
`NotificationClient` talks to a running harness:
|
|
267
|
+
|
|
268
|
+
```ts
|
|
269
|
+
import { NotificationClient } from "@crvouga/mockingbird-notifications"
|
|
270
|
+
|
|
271
|
+
const notifications = new NotificationClient("http://127.0.0.1:8790", { namespace: "worker-3" })
|
|
272
|
+
|
|
273
|
+
// The app texts a sign-in code; the test reads it the way the user would.
|
|
274
|
+
const code = await notifications.waitForCode("+12025550123")
|
|
275
|
+
|
|
276
|
+
// The app emails an invitation.
|
|
277
|
+
const [invite] = await notifications.waitFor({ channel: "email", to: "ada@example.com", contains: "invited" })
|
|
278
|
+
console.log(invite?.email?.subject, invite?.email?.html)
|
|
279
|
+
|
|
280
|
+
// A link to the inbox opened on that email, e.g. for Playwright's `page.goto(url)` and a screenshot.
|
|
281
|
+
const url = notifications.link({ id: invite?.id })
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Or run the harness inside the test process, against servers or in-process runtimes:
|
|
285
|
+
|
|
286
|
+
```ts
|
|
287
|
+
import { createNotificationHarness } from "@crvouga/mockingbird-notifications"
|
|
288
|
+
import { createRuntime as twilio } from "@crvouga/mockingbird-service-twilio"
|
|
289
|
+
|
|
290
|
+
const harness = createNotificationHarness({ services: { twilio: twilio() }, pollIntervalMs: 0 })
|
|
291
|
+
// …the app sends through the same runtime…
|
|
292
|
+
const [sms] = await harness.waitFor({ channel: "sms", to: "+12025550123" })
|
|
293
|
+
harness.close()
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
`createServer` from `@crvouga/mockingbird-notifications/server` serves the same harness over
|
|
297
|
+
`node:http` and takes the CLI's options (`fleet`, `manifest` as a path or object, `services`,
|
|
298
|
+
`namespaces`, `pollIntervalMs`, `adminKey`, `port`, `host`).
|
|
299
|
+
|
|
300
|
+
A read always reflects sends that finished before it started: `list`, `waitFor` and every API read
|
|
301
|
+
sync the emulators first instead of waiting for the next poll.
|
|
302
|
+
|
|
303
|
+
## HTTP API
|
|
304
|
+
|
|
305
|
+
All routes are `GET`, except the emulators' own admin APIs under `/admin/apis/`. Filters: `channel` (`sms`, `email`, `push`, `chat`), `service`, `source` (the fleet
|
|
306
|
+
entry name), `namespace` (default `default`), `to`, `contains` (case-insensitive text), `since`
|
|
307
|
+
(ISO-8601 or epoch ms).
|
|
308
|
+
|
|
309
|
+
| Route | Answer |
|
|
310
|
+
| --- | --- |
|
|
311
|
+
| `/api/notifications` | `{namespace, version, notifications}`, oldest first (`order=newest` reverses). |
|
|
312
|
+
| `/api/notifications/latest` | The newest match, or 404. |
|
|
313
|
+
| `/api/notifications/{id}` | One notification. |
|
|
314
|
+
| `/api/wait?timeoutMs=&count=` | `{notifications}` once `count` (default 1) match; 408 with what was found after `timeoutMs` (default 10000, at most 120000). |
|
|
315
|
+
| `/api/events?namespace=` | Server-sent events: `hello`, `change` (`{added, version}`) and `sources` every 2 s. |
|
|
316
|
+
| `/api/sources` | Each emulator: name, service, admin URL, `ok`, last error and count. |
|
|
317
|
+
| `/api/namespaces` | Namespaces watched. |
|
|
318
|
+
| `/api/admin` | What the dashboard mounts: `{admin, emulators, fleet}`. |
|
|
319
|
+
| `/admin/apis/{name}/…` | That emulator's admin API, any method, forwarded unchanged. |
|
|
320
|
+
| `/admin/ui.js`, `/admin/ui` | The emulators' admin UI: the script the page loads, and the UI as a page of its own. |
|
|
321
|
+
|
|
322
|
+
A notification:
|
|
323
|
+
|
|
324
|
+
```jsonc
|
|
325
|
+
{
|
|
326
|
+
"id": "twilio:default:SMb6c1…", // <fleet entry>:<namespace>:<provider id>[#mailbox]
|
|
327
|
+
"providerId": "SMb6c1…",
|
|
328
|
+
"channel": "sms", // sms | email | push | chat
|
|
329
|
+
"source": "twilio", "service": "twilio", "namespace": "default",
|
|
330
|
+
"to": "+12025550123", // phone, mailbox (lower case), push token, or chat channel
|
|
331
|
+
"from": { "name": null, "address": "+15005550006" },
|
|
332
|
+
"createdAt": "2026-11-10T06:39:06.886Z", // on the emulator's clock
|
|
333
|
+
"preview": "Your code is 123456",
|
|
334
|
+
"status": "queued",
|
|
335
|
+
"sms": { "body": "Your code is 123456", "code": null, "media": [] }, // + "via": "whatsapp" for WhatsApp
|
|
336
|
+
"email": null, // {subject, html, text, cc, bcc, replyTo, attachments, headers}
|
|
337
|
+
"push": null, // {title, body, imageUrl, app, platform, data, badge, sound}
|
|
338
|
+
"chat": null, // {text, workspace, method, blocks, attachments, threadTs, ephemeral, edited, reactions, names}
|
|
339
|
+
"raw": { /* the emulator's own record */ }
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
An email to several recipients appears once in each mailbox (`to`, `cc` and `bcc`). When Resend
|
|
344
|
+
forwards to Mailosaur (`--forward-to-inbox`), the Mailosaur copy is hidden so the message shows once.
|
|
345
|
+
|
|
346
|
+
## API
|
|
347
|
+
|
|
348
|
+
| Export | Kind | Description |
|
|
349
|
+
| --- | --- | --- |
|
|
350
|
+
| `createNotificationHarness` | function | The harness as a Fetch handler: `fetch`, `sync(namespace?)`, `list(filter)`, `waitFor(filter, {timeoutMs, count})`, `sources()`, `close()`. Options: `fleet`, `manifest`, `services`, `namespaces`, `pollIntervalMs`, `adminKey`, `adminKeys`, `admin`, `defaultTab`, `fetch`, `title`. |
|
|
351
|
+
| `NotificationClient` | class | HTTP client for a running harness: `list`, `latest`, `waitFor`, `waitForCode`, `sources`, `link`. Options: `namespace`, `fetch`. |
|
|
352
|
+
| `matches` | function | `(notification, filter)` → whether it matches, as the API filters. |
|
|
353
|
+
| `parseMime` | function | The MIME reader used for SES raw email: headers, subject, addresses, text, HTML, attachments. |
|
|
354
|
+
| `SERVICE_KINDS`, `SERVICE_LABELS` | values | The emulators the harness reads, and their display names. |
|
|
355
|
+
| `CHANNELS` | value | Every channel, in device order: `sms`, `push`, `email`, `chat`. |
|
|
356
|
+
| `TEST_IDS`, `selectors` | values | Every `data-testid` the UI renders, and CSS selector builders for browser tests. Also exported from `./ui`. |
|
|
357
|
+
| `mountNotifications` (`./ui`) | function | `(element, {harness, …})` → `{ready, show, setNamespace, setSound, render, destroy}`. Mounts the UI on an element. |
|
|
358
|
+
| `mountDashboard` (`./ui`) | function | `(element, {emulators, notifications, tabs, …})` → `{show, active, tabs, aside, notifications, destroy}`. A tab bar over an emulators panel and the notifications UI. Also `DASHBOARD_STYLES` and `DASHBOARD_ROOT_CLASS`. |
|
|
359
|
+
| `createApp`, `STYLES`, `ROOT_CLASS` (`./ui`) | browser | The UI without the style injection and embedded defaults, its scoped stylesheet, and its root class. |
|
|
360
|
+
| `createServer`, `manifestFile`, `DEFAULT_PORT` (`./server`) | Node | Serve the harness over `node:http`; `manifestFile(path)` re-reads a fleet ready file. Port 8790. |
|
|
361
|
+
| `adminGateway` (`./server`) | Node | The harness's `admin` option: fronts every emulator's admin API and admin UI under `/admin/`. `createServer` passes it by default. |
|
|
362
|
+
|
|
363
|
+
## Not covered
|
|
364
|
+
|
|
365
|
+
- Customer.io renders templates on its side and the emulator keeps no body, so its email shows the
|
|
366
|
+
template id and `message_data`. In-app (`inbox_message`) sends are not shown.
|
|
367
|
+
- SNS mobile push (platform endpoints) is not emulated, and SNS topic subscribers are matched by
|
|
368
|
+
topic only.
|
|
369
|
+
- Twilio MMS media is not in the emulator's outbox, so bubbles show text only.
|
|
370
|
+
- Chat reads Slack only. Discord, Microsoft Teams and Telegram have no emulator, and neither do
|
|
371
|
+
other push providers (APNs direct, Expo, OneSignal, Web Push) or email providers (SendGrid,
|
|
372
|
+
Postmark, Mailgun).
|
|
373
|
+
- WhatsApp templates, media and read receipts: a WhatsApp message is its text, as Twilio's
|
|
374
|
+
Messages API accepted it.
|
|
375
|
+
- Replying from a device. The harness shows what was sent; inbound traffic goes through each
|
|
376
|
+
emulator's own admin routes.
|
|
377
|
+
|
|
378
|
+
## Develop
|
|
379
|
+
|
|
380
|
+
```sh
|
|
381
|
+
bun run --cwd packages/notifications dev # a fleet of every messaging emulator, sample traffic, harness on :8790
|
|
382
|
+
bun run --cwd packages/notifications e2e # the browser tests (bunx playwright install chromium first)
|
|
383
|
+
bun run --cwd packages/notifications storybook # every UI state, from fixtures
|
|
384
|
+
```
|