preload-app.spec.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { afterEach, expect, it, vi } from 'vitest'
  2. import { DESKTOP_IPC, type DshDesktopStartupApi } from '../src/ipc.ts'
  3. const electron = vi.hoisted(() => ({
  4. contextBridge: { exposeInMainWorld: vi.fn() },
  5. ipcRenderer: { invoke: vi.fn(), on: vi.fn(), off: vi.fn() },
  6. }))
  7. vi.mock('electron', () => electron)
  8. afterEach(() => { vi.unstubAllGlobals(); vi.clearAllMocks(); vi.resetModules() })
  9. it.each(['dsh-app://app/index.html', 'https://shell/startup.html'])('exposes only the carrier marker to %s', async (url) => {
  10. vi.stubGlobal('location', new URL(url))
  11. await import('../src/preload-app.ts')
  12. expect(electron.contextBridge.exposeInMainWorld).toHaveBeenCalledWith('dshDesktop', { protocolVersion: 1 })
  13. })
  14. it('provides startup controls and a removable state subscription to shell documents', async () => {
  15. vi.stubGlobal('location', new URL('dsh-app://shell/startup.html'))
  16. await import('../src/preload-app.ts')
  17. const api = electron.contextBridge.exposeInMainWorld.mock.calls[0]?.[1] as DshDesktopStartupApi
  18. await api.locale()
  19. await api.backend.status()
  20. await api.disablePlugins()
  21. await api.resetConfiguration()
  22. await api.restart()
  23. expect(electron.ipcRenderer.invoke.mock.calls).toEqual([
  24. [DESKTOP_IPC.localeGet], [DESKTOP_IPC.backendStatus],
  25. [DESKTOP_IPC.pluginsDisableAll], [DESKTOP_IPC.configurationReset], [DESKTOP_IPC.applicationRestart],
  26. ])
  27. const listener = vi.fn()
  28. const dispose = api.backend.subscribe(listener)
  29. const handler = electron.ipcRenderer.on.mock.calls[0]?.[1] as (event: unknown, state: unknown) => void
  30. handler({}, { phase: 'error', message: 'startup failed' })
  31. expect(listener).toHaveBeenCalledWith({ phase: 'error', message: 'startup failed' })
  32. dispose()
  33. expect(electron.ipcRenderer.off).toHaveBeenCalledWith(DESKTOP_IPC.backendState, handler)
  34. expect(api).not.toHaveProperty('plugins')
  35. })