mattrguerra/meridian-ops-boardmainPrivate on GitHub, published here in full

health-inbox.test.ts

Language TypeScript4.3 KB98 lines

apps/worker/test/health-inbox.test.ts

  1. /**
  2. * /health reports the Gmail setup.
  3. *
  4. * Here and not on the board: `wrangler dev` has no Cloudflare Access in front
  5. * of it, so the Worker cannot identify a local request and correctly refuses
  6. * it, which looks like a broken setup rather than a working guard. /health is the one deliberately identity-free route, so
  7. * it is the only place a Gmail credential can be verified before deploying.
  8. *
  9. * The bound this must hold: mailbox names and error reasons, never mail.
  10. */
  11. import { describe, expect, it } from 'vitest'
  12. import worker from '../src/index.js'
  13. const kv = (store: Record<string, string>) => ({
  14. get: async (k: string) => store[k] ?? null,
  15. put: async (k: string, v: string) => void (store[k] = v),
  16. })
  17. const INBOX = JSON.stringify({
  18. syncedAt: '2026-09-01T06:00:00.000Z',
  19. mailboxes: [
  20. {
  21. mailbox: 'hello@meridiandisplay.example',
  22. ok: true,
  23. error: null,
  24. messages: [
  25. { id: 'm1', threadId: 't1', mailbox: 'hello@meridiandisplay.example', sender: 'news@upscaler.vendor.example',
  26. senderName: 'Upscaler Labs', subject: 'SECRET-SUBJECT-LINE', date: '2026-09-01T00:00:00.000Z', labels: [] },
  27. ],
  28. canSend: true,
  29. },
  30. { mailbox: 'orderdesk@meridiandisplay.example', ok: true, error: null, messages: [], canSend: false },
  31. { mailbox: 'studio@meridiandisplay.example', ok: true, error: null, messages: [], canSend: null },
  32. { mailbox: 'accounts@meridiandisplay.example', ok: false, error: 'unauthorized_client', messages: [], canSend: null },
  33. ],
  34. })
  35. const env = (store: Record<string, string> = {}) =>
  36. ({ DASHBOARD: kv({ 'dashboard:payload': JSON.stringify({ generatedAt: new Date().toISOString() }), ...store }) }) as never
  37. const get = async (e: unknown) =>
  38. (await worker.fetch(new Request('http://x/health'), e as never)).json() as Promise<Record<string, never>>
  39. describe('/health inbox block', () => {
  40. it('names which mailboxes read and which failed, with the reason', async () => {
  41. const body = (await get(env({ 'inbox:messages': INBOX }))) as unknown as {
  42. inbox: { read: string[]; failed: { mailbox: string; error: string }[] }
  43. }
  44. expect(body.inbox.read).toContain('hello@meridiandisplay.example')
  45. expect(body.inbox.failed).toEqual([
  46. { mailbox: 'accounts@meridiandisplay.example', error: 'unauthorized_client' },
  47. ])
  48. })
  49. it('carries no mail: no sender, no subject, no counts', async () => {
  50. // The endpoint is identity-free, so anything it holds is readable by anyone
  51. // who can reach it. Mailbox names and failure reasons are ours; the contents
  52. // are not.
  53. const raw = JSON.stringify(await get(env({ 'inbox:messages': INBOX })))
  54. expect(raw).not.toContain('SECRET-SUBJECT-LINE')
  55. expect(raw).not.toContain('news@upscaler.vendor.example')
  56. expect(raw).not.toContain('Upscaler Labs')
  57. })
  58. it('is null before the first sync, which is not the same as no mailboxes', async () => {
  59. const body = (await get(env())) as unknown as { inbox: unknown }
  60. expect(body.inbox).toBeNull()
  61. })
  62. })
  63. /**
  64. * Whether each mailbox can send.
  65. *
  66. * /health is the only identity-free route, which is what makes it the only
  67. * place a credential can be checked before deploying, and "does every mailbox
  68. * token carry the send scope?" is exactly that kind of question.
  69. * The bound is unchanged: mailbox names and booleans, never mail.
  70. */
  71. describe('/health reports send capability', () => {
  72. it('carries canSend per mailbox, keeping true, false and null apart', async () => {
  73. const body = (await get(env({ 'inbox:messages': INBOX }))) as unknown as {
  74. inbox: { canSend: Record<string, boolean | null> }
  75. }
  76. expect(body.inbox.canSend['hello@meridiandisplay.example']).toBe(true)
  77. expect(body.inbox.canSend['orderdesk@meridiandisplay.example']).toBe(false)
  78. // null is "Google reported no scopes", not "cannot send". They need
  79. // different responses and must not render the same.
  80. expect(body.inbox.canSend['studio@meridiandisplay.example']).toBeNull()
  81. expect(body.inbox.canSend['accounts@meridiandisplay.example']).toBeNull()
  82. })
  83. it('still carries no mail alongside it', async () => {
  84. const raw = JSON.stringify(await get(env({ 'inbox:messages': INBOX })))
  85. expect(raw).not.toContain('SECRET-SUBJECT-LINE')
  86. expect(raw).not.toContain('news@upscaler.vendor.example')
  87. })
  88. })