backgroundActivation.test.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // @vitest-environment node
  2. import { mkdtempSync, rmSync } from 'node:fs'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { afterEach, expect, it, vi } from 'vitest'
  6. import { createNodeServer } from '../../src/server/app'
  7. import { loadServerConfig } from '../../src/server/config'
  8. import { PushCoordinator } from '../../src/server/pushCoordinator'
  9. import { WorkspaceMemorySynthesis } from '../../src/server/workspaceMemorySynthesis'
  10. import { OpenVikingSessionSync } from '../../src/server/openVikingSessionSync'
  11. import { createAuthenticatedApplication } from './authenticatedApplication'
  12. afterEach(() => vi.restoreAllMocks())
  13. function fixture(deferBackground: boolean) {
  14. const home = mkdtempSync(join(tmpdir(), 'yaoyao-background-activation-'))
  15. const memory = vi.spyOn(WorkspaceMemorySynthesis.prototype, 'start').mockImplementation(() => {})
  16. const memorySync = vi.spyOn(OpenVikingSessionSync.prototype, 'start').mockImplementation(() => {})
  17. const push = new PushCoordinator({ home, autoFlush: false,
  18. fcmProviderFactory: () => ({ send: async () => ({ disposition: 'success', status: 200 }) }) })
  19. const runtime = createAuthenticatedApplication({ deferBackground, push,
  20. config: loadServerConfig({ HERMES_YAOYAO_HOME: home, HERMES_YAOYAO_ALLOWED_HOSTS: '127.0.0.1' }) })
  21. const starts = {
  22. memory, memorySync,
  23. workspace: vi.spyOn(runtime.workspaceRuntime, 'start').mockImplementation(() => {}),
  24. routines: vi.spyOn(runtime.workspaceRoutines, 'start').mockImplementation(() => {}),
  25. grok: vi.spyOn(runtime.grokAuth, 'start').mockImplementation(() => {}),
  26. }
  27. const localVm = vi.spyOn(runtime.localVm, 'start').mockResolvedValue(undefined)
  28. const node = createNodeServer(runtime, { deferBackground })
  29. const listen = () => new Promise<void>(resolve => node.server.listen(0, '127.0.0.1', resolve))
  30. const close = async () => { try { await node.close() } finally { rmSync(home, { recursive: true, force: true }) } }
  31. return { runtime, push, node, starts, localVm, listen, close }
  32. }
  33. it('keeps environment detection idle after listening and starts all background work once on activation', async () => {
  34. const f = fixture(true)
  35. try {
  36. await f.listen()
  37. for (const start of Object.values(f.starts)) expect(start).not.toHaveBeenCalled()
  38. expect(f.localVm).not.toHaveBeenCalled()
  39. f.node.startBackground()
  40. f.node.startBackground()
  41. for (const start of Object.values(f.starts)) expect(start).toHaveBeenCalledTimes(1)
  42. expect(f.localVm).toHaveBeenCalledTimes(1)
  43. const address = f.node.server.address()
  44. expect(f.localVm).toHaveBeenCalledWith(`http://127.0.0.1:${typeof address === 'object' && address?.port}`)
  45. } finally { await f.close() }
  46. f.node.startBackground()
  47. f.runtime.startBackground()
  48. for (const start of Object.values(f.starts)) expect(start).toHaveBeenCalledTimes(1)
  49. })
  50. it('holds local VM startup until listening when activated before the HTTP server is ready', async () => {
  51. const f = fixture(true)
  52. try {
  53. f.node.startBackground()
  54. f.node.startBackground()
  55. for (const start of Object.values(f.starts)) expect(start).toHaveBeenCalledTimes(1)
  56. expect(f.localVm).not.toHaveBeenCalled()
  57. await f.listen()
  58. expect(f.localVm).toHaveBeenCalledTimes(1)
  59. f.node.startBackground()
  60. expect(f.localVm).toHaveBeenCalledTimes(1)
  61. } finally { await f.close() }
  62. })
  63. it('preserves automatic background startup for ordinary servers', async () => {
  64. const f = fixture(false)
  65. try {
  66. for (const start of Object.values(f.starts)) expect(start).toHaveBeenCalledTimes(1)
  67. expect(f.localVm).not.toHaveBeenCalled()
  68. await f.listen()
  69. expect(f.localVm).toHaveBeenCalledTimes(1)
  70. f.node.startBackground()
  71. for (const start of Object.values(f.starts)) expect(start).toHaveBeenCalledTimes(1)
  72. } finally { await f.close() }
  73. })