All files / src/server/platform test-inbox.server.ts

100% Statements 7/7
100% Branches 3/3
100% Functions 3/3
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23                        2x 1x   1x 1x   2x 1x 1x    
// The test inbox (spec: "Test inbox for end-to-end tests"). Playwright runs
// apart from the Worker, so it reads the sign-in code from the emails the
// capture transport kept. Open only while capturing; production sends
// through Resend and gets a 404.
import { env } from 'cloudflare:workers'
import { capturedEmails, isCapturing, type MailerEnv } from './mailer.server'
 
/** GET /api/test/inbox?to=<address>: that address's emails, oldest first. */
export function testInboxResponse(
  request: Request,
  mailerEnv: Pick<MailerEnv, 'EMAIL_TRANSPORT'> = env,
): Response {
  if (!isCapturing(mailerEnv)) {
    return new Response('Not found', { status: 404 })
  }
  const to = new URL(request.url).searchParams.get('to')?.toLowerCase()
  const emails = capturedEmails
    .all()
    .filter((email) => email.to.toLowerCase() === to)
    .map(({ to: address, subject, text }) => ({ to: address, subject, text }))
  return Response.json(emails, { headers: { 'Cache-Control': 'no-store' } })
}