health-inbox.test.ts
Language TypeScript4.3 KB98 lines
/*** /health reports the Gmail setup.** Here and not on the board: `wrangler dev` has no Cloudflare Access in front* of it, so the Worker cannot identify a local request and correctly refuses* it, which looks like a broken setup rather than a working guard. /health is the one deliberately identity-free route, so* it is the only place a Gmail credential can be verified before deploying.** The bound this must hold: mailbox names and error reasons, never mail.*/import { describe, expect, it } from 'vitest'import worker from '../src/index.js'const kv = (store: Record<string, string>) => ({get: async (k: string) => store[k] ?? null,put: async (k: string, v: string) => void (store[k] = v),})const INBOX = JSON.stringify({syncedAt: '2026-09-01T06:00:00.000Z',mailboxes: [{mailbox: 'hello@meridiandisplay.example',ok: true,error: null,messages: [{ id: 'm1', threadId: 't1', mailbox: 'hello@meridiandisplay.example', sender: 'news@upscaler.vendor.example',senderName: 'Upscaler Labs', subject: 'SECRET-SUBJECT-LINE', date: '2026-09-01T00:00:00.000Z', labels: [] },],canSend: true,},{ mailbox: 'orderdesk@meridiandisplay.example', ok: true, error: null, messages: [], canSend: false },{ mailbox: 'studio@meridiandisplay.example', ok: true, error: null, messages: [], canSend: null },{ mailbox: 'accounts@meridiandisplay.example', ok: false, error: 'unauthorized_client', messages: [], canSend: null },],})const env = (store: Record<string, string> = {}) =>({ DASHBOARD: kv({ 'dashboard:payload': JSON.stringify({ generatedAt: new Date().toISOString() }), ...store }) }) as neverconst get = async (e: unknown) =>(await worker.fetch(new Request('http://x/health'), e as never)).json() as Promise<Record<string, never>>describe('/health inbox block', () => {it('names which mailboxes read and which failed, with the reason', async () => {const body = (await get(env({ 'inbox:messages': INBOX }))) as unknown as {inbox: { read: string[]; failed: { mailbox: string; error: string }[] }}expect(body.inbox.read).toContain('hello@meridiandisplay.example')expect(body.inbox.failed).toEqual([{ mailbox: 'accounts@meridiandisplay.example', error: 'unauthorized_client' },])})it('carries no mail: no sender, no subject, no counts', async () => {// The endpoint is identity-free, so anything it holds is readable by anyone// who can reach it. Mailbox names and failure reasons are ours; the contents// are not.const raw = JSON.stringify(await get(env({ 'inbox:messages': INBOX })))expect(raw).not.toContain('SECRET-SUBJECT-LINE')expect(raw).not.toContain('news@upscaler.vendor.example')expect(raw).not.toContain('Upscaler Labs')})it('is null before the first sync, which is not the same as no mailboxes', async () => {const body = (await get(env())) as unknown as { inbox: unknown }expect(body.inbox).toBeNull()})})/*** Whether each mailbox can send.** /health is the only identity-free route, which is what makes it the only* place a credential can be checked before deploying, and "does every mailbox* token carry the send scope?" is exactly that kind of question.* The bound is unchanged: mailbox names and booleans, never mail.*/describe('/health reports send capability', () => {it('carries canSend per mailbox, keeping true, false and null apart', async () => {const body = (await get(env({ 'inbox:messages': INBOX }))) as unknown as {inbox: { canSend: Record<string, boolean | null> }}expect(body.inbox.canSend['hello@meridiandisplay.example']).toBe(true)expect(body.inbox.canSend['orderdesk@meridiandisplay.example']).toBe(false)// null is "Google reported no scopes", not "cannot send". They need// different responses and must not render the same.expect(body.inbox.canSend['studio@meridiandisplay.example']).toBeNull()expect(body.inbox.canSend['accounts@meridiandisplay.example']).toBeNull()})it('still carries no mail alongside it', async () => {const raw = JSON.stringify(await get(env({ 'inbox:messages': INBOX })))expect(raw).not.toContain('SECRET-SUBJECT-LINE')expect(raw).not.toContain('news@upscaler.vendor.example')})})