pushRuntime.test.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { mkdtempSync, rmSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { afterEach, describe, expect, it, vi } from 'vitest'
  5. import { createNodeServer, type ApplicationRuntime } from '../../src/server/app.js'
  6. import type { ServerConfig } from '../../src/server/config.js'
  7. import { PushCoordinator } from '../../src/server/pushCoordinator.js'
  8. import { createAuthenticatedApplication } from './authenticatedApplication.js'
  9. const roots: string[] = []
  10. const runtimes: ApplicationRuntime[] = []
  11. afterEach(() => {
  12. for (const runtime of runtimes.splice(0)) runtime.close()
  13. for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true })
  14. })
  15. describe('dynamic push runtime', () => {
  16. it('starts and stops background observers when Web configuration changes', async () => {
  17. const home = mkdtempSync(join(tmpdir(), 'yaoyao-push-runtime-'))
  18. roots.push(home)
  19. const config: ServerConfig = {
  20. host: '127.0.0.1', port: 8800, upstream: new URL('http://127.0.0.1:9119'),
  21. allowedHosts: new Set(), home, mediaRoot: home, attachmentsRoot: home, imagesRoot: home,
  22. mediaOwner: 'tester', allowInsecureLan: false, insecureLan: false, production: false,
  23. }
  24. const push = new PushCoordinator({
  25. home,
  26. autoFlush: false,
  27. providerFactory: () => ({ send: async () => ({ disposition: 'success', status: 200 }) }),
  28. fcmProviderFactory: () => ({ send: async () => ({ disposition: 'success', status: 200 }) }),
  29. })
  30. const runtime = createAuthenticatedApplication({ config, push })
  31. const chatStart = vi.spyOn(runtime.chatPushJobs, 'start').mockImplementation(() => undefined)
  32. const chatStop = vi.spyOn(runtime.chatPushJobs, 'stop').mockImplementation(() => undefined)
  33. expect(runtime).not.toHaveProperty('groupPushEvents')
  34. const node = createNodeServer(runtime)
  35. expect(chatStop).toHaveBeenCalledTimes(1)
  36. await push.configureAPNs({
  37. keyFile: '/private/unused.p8', keyId: 'KEY1234567', teamId: 'TEAM123456',
  38. topic: 'cn.samien.yaoyao.hermes', environments: ['development'],
  39. })
  40. expect(chatStart).toHaveBeenCalledTimes(1)
  41. await push.configureAPNs()
  42. expect(chatStop).toHaveBeenCalledTimes(2)
  43. await push.configureFCM({
  44. serviceAccountFile: '/private/unused.json',
  45. projectId: 'yaoyao-test-project',
  46. packageName: 'cn.samien.yaoyao.hermes',
  47. })
  48. expect(chatStart).toHaveBeenCalledTimes(2)
  49. await push.configureFCM()
  50. expect(chatStop).toHaveBeenCalledTimes(3)
  51. await node.close()
  52. })
  53. })