pushProtocol.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { afterEach, describe, expect, it, vi } from 'vitest'
  2. import {
  3. getGroupPushSubscriptions,
  4. getPushCapabilities,
  5. getPushSystemStatus,
  6. setGroupPushSubscription,
  7. } from '@/api/push'
  8. import { clearApiSecurityContext, setApiCsrfToken } from '@/api/client'
  9. afterEach(() => {
  10. clearApiSecurityContext()
  11. vi.unstubAllGlobals()
  12. })
  13. describe('push client protocol', () => {
  14. it('uses the versioned push API and protects subscription mutations', async () => {
  15. const calls: Array<{ path: string; init?: RequestInit }> = []
  16. vi.stubGlobal('fetch', vi.fn(async (path: string, init?: RequestInit) => {
  17. calls.push({ path, init })
  18. const payload = path === '/api/app/push/v1/capabilities'
  19. ? { protocolVersion: 1, enabled: true, events: [], maximumSummaryCharacters: 180 }
  20. : path === '/api/app/push/v1/group-subscriptions'
  21. ? { subscriptions: [{ roomId: 'room-1', enabled: true }] }
  22. : path === '/api/app/system/push-status'
  23. ? { configured: true, healthy: true, registrationCount: 1, pendingCount: 0 }
  24. : { subscription: { roomId: 'room-1', enabled: false } }
  25. return new Response(JSON.stringify(payload), {
  26. status: 200,
  27. headers: { 'content-type': 'application/json' },
  28. })
  29. }))
  30. setApiCsrfToken('csrf-push')
  31. await expect(getPushCapabilities()).resolves.toMatchObject({ protocolVersion: 1, enabled: true })
  32. await expect(getGroupPushSubscriptions()).resolves.toEqual(new Set(['room-1']))
  33. await expect(setGroupPushSubscription('room-1', false)).resolves.toBe(false)
  34. await expect(getPushSystemStatus()).resolves.toMatchObject({ configured: true, registrationCount: 1 })
  35. expect(calls.map(call => call.path)).toEqual([
  36. '/api/app/push/v1/capabilities',
  37. '/api/app/push/v1/group-subscriptions',
  38. '/api/app/push/v1/group-subscriptions/room-1',
  39. '/api/app/system/push-status',
  40. ])
  41. expect(new Headers(calls[2]!.init?.headers).get('X-CSRF-Token')).toBe('csrf-push')
  42. })
  43. })