desktop-boot.spec.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // @vitest-environment jsdom
  2. import { afterEach, expect, it, vi } from 'vitest'
  3. const boot = vi.hoisted(() => ({
  4. run: vi.fn(),
  5. applyIndexInjections: vi.fn(async () => {}),
  6. }))
  7. vi.mock('@deepseek-ai/dsh-client-web', () => ({
  8. AppWebEntry: class { run = boot.run },
  9. applyIndexInjections: boot.applyIndexInjections,
  10. }))
  11. afterEach(() => {
  12. vi.unstubAllGlobals()
  13. vi.clearAllMocks()
  14. vi.resetModules()
  15. document.body.replaceChildren()
  16. })
  17. it('forwards client initialization failures to native recovery', async () => {
  18. document.body.innerHTML = '<div id="root"></div>'
  19. const gate = Promise.withResolvers<undefined>()
  20. const failed = vi.fn(async () => {})
  21. vi.stubGlobal('__DSH_BOOT_READY__', gate)
  22. vi.stubGlobal('dshDesktopBoot', { ready: async () => ({ injections: [], streamBaseUrl: 'http://127.0.0.1:3080' }), failed })
  23. vi.stubGlobal('__DSH_TRANSPORT__', undefined)
  24. await import('../src/main.ts')
  25. await gate.promise
  26. expect(boot.applyIndexInjections).toHaveBeenCalledWith([], expect.any(Function))
  27. const report = boot.run.mock.calls[0]![0] as (reason: unknown) => void
  28. report(new Error('client plugin activation failed'))
  29. expect(failed).toHaveBeenCalledWith('client plugin activation failed')
  30. })
  31. it('rejects the shared boot wait when the Desktop Host cannot start', async () => {
  32. document.body.innerHTML = '<div id="root"></div>'
  33. const gate = Promise.withResolvers<undefined>()
  34. const failure = new Error('Host failed')
  35. const rejected = expect(gate.promise).rejects.toBe(failure)
  36. vi.stubGlobal('__DSH_BOOT_READY__', gate)
  37. vi.stubGlobal('dshDesktopBoot', { ready: async () => { throw failure }, failed: vi.fn() })
  38. await import('../src/main.ts')
  39. await rejected
  40. expect(boot.applyIndexInjections).not.toHaveBeenCalled()
  41. expect(boot.run).toHaveBeenCalledWith(expect.any(Function))
  42. })
  43. it('leaves browser failure presentation with the Web boot kernel', async () => {
  44. document.body.innerHTML = '<div id="root"></div>'
  45. vi.stubGlobal('dshDesktopBoot', undefined)
  46. await import('../src/main.ts')
  47. expect(boot.run).toHaveBeenCalledWith(undefined)
  48. })
  49. it.each(['root', 'readiness'])('reports missing %s before client plugin startup', async (missing) => {
  50. if (missing !== 'root') document.body.innerHTML = '<div id="root"></div>'
  51. const failed = vi.fn(async () => {})
  52. vi.stubGlobal('dshDesktopBoot', { ready: vi.fn(), failed })
  53. vi.stubGlobal('__DSH_BOOT_READY__', undefined)
  54. await import('../src/main.ts')
  55. expect(failed).toHaveBeenCalledWith(missing === 'root'
  56. ? 'web app: missing #root' : 'desktop web: boot readiness is missing')
  57. expect(boot.run).not.toHaveBeenCalled()
  58. })