Yash Gupta

You cannot test a push notification with a mock

Browser and mobile push for a shared team inbox, so an agent knows a customer has replied without keeping a tab open. The feature is ordinary. What it taught me is that the usual way of proving this works, a fake token and an assertion on the payload, cannot work at all, and the three real bugs it would have missed.

Role
Built the backend, found the frontend bugs
Stack
NestJS, PostgreSQL, Firebase Cloud Messaging, service workers
Status
Live in production, 8 Oct 2026
Harness
52 of 52, real browser, real provider

What it does

Agents share an inbox. A push goes out when a customer replies, to whoever that conversation is assigned to, or to every available member when it is unassigned, and it is skipped when an automated flow already handled the message. Assignment pushes too, singly and as one summary for a bulk action.

Device tokens are registered and removed through the API, stale ones are pruned on a schedule, and the notification history is readable and markable as read.

Why the obvious test is worthless

You would reasonably prove this works by sending to a made-up token and asserting on what was built. I set out to do that, and it cannot be done: the provider validates the token before it looks at the payload. A fake token is rejected, every time, with the same error whatever you put in the message. A dry-run mode has the same problem. The test passes or fails on the token and tells you nothing about the thing you wrote.

So proving a push works needs a real token, which needs a real browser that has really granted permission. The harness does that: it builds the branch, runs it against throwaway containers, launches a real Chrome, grants notification permission over the debugging protocol, collects the token the page registers, and sends through it to real Google infrastructure.

Two details cost an hour each and are the sort of thing nobody writes down. The automation library must not launch the browser itself, because the flags it adds by default to make automation deterministic disable the background networking that push depends on; it has to attach to a browser you started. And testing background delivery means navigating away first, then coming back and reading the notifications the service worker registered, plus a log the worker writes for itself, because by definition nothing is watching at the moment it arrives.

The three bugs it found

None of these would have failed a unit test, and the first two were live.

Every background notification appeared twice

The messaging library displays a notification automatically when the payload carries the standard notification fields. The service worker also had a handler that displayed one. Both ran. In the foreground only the handler runs, so the duplicate appeared exclusively when the tab was not in focus, which is the case nobody looks at while developing. The fix is to return early when the library has already handled it.

Stale tokens were pruned five and a half hours early

The cleanup deleted tokens older than a cut-off, built by passing a JavaScript date into a raw SQL fragment. The binding did not carry the timezone the way the surrounding code assumed, so in India the cut-off landed five and a half hours in the past and the job deleted tokens that were still perfectly good. The symptom is a user who silently stops receiving notifications and has no idea why. Replacing the raw fragment with the query builder's own comparison fixed it.

Every notification click led to a page that does not exist

The backend sent a click target shaped like a path segment. The frontend has no such route, only a query parameter. So clicking any notification, from the tray, the notification centre or the toast, opened a 404. The feature worked right up to the moment it was useful.

With all three fixed the harness runs 52 of 52.

The one I wrote up instead of changing

Reading the mobile path, I formed a theory about how the same click target behaves there, based on how that platform matches it. I could not test it: the mobile application is not on my machine, the right fix depends on which field that client actually reads, and the only honest check is on a device I did not have.

So I wrote it up: what I knew, what I had inferred, what I was unsure of, and the single test that would settle it, and handed it to the person who could run that test. It was a deliberate decision not to change code on an inference.

Shipping a guess into the one path you cannot observe is how you turn a theory into a real bug. On that path the write-up was worth more than the change would have been.

What is not done

  • The harness is a local script needing Docker, a built checkout and a real browser. It cannot run in continuous integration, because it uses live credentials and a human-granted permission, which is a real limit on how often it gets run.
  • Nothing measures delivery end to end. A provider accepting a message is not the same as a person seeing it, and only the first of those is easy to observe.
  • The theory above is still a theory, and settling it needs a device test rather than more reading.

What I took from it

  • When a dependency validates in a fixed order, a mock can only exercise the part before the first check. Find out what that order is before deciding what a test proves.
  • Bugs live in the states you do not develop in: the backgrounded tab, the second device, the click an hour later.
  • Do not hand a raw date to raw SQL. The driver's assumptions and yours differ by exactly the offset you forgot.
  • An unverifiable finding should be written down precisely, not fixed speculatively.