plugin-manager.spec.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import { readFileSync } from 'node:fs'
  2. import { runInContext } from 'node:vm'
  3. import { JSDOM } from 'jsdom'
  4. import { expect, it, vi } from 'vitest'
  5. import { resolveDesktopLocale } from '../src/locale.ts'
  6. it('keeps disabled packages visible and offers recovery without a running backend', async () => {
  7. const dom = new JSDOM(readFileSync(new URL('../renderer/plugin-manager.html', import.meta.url), 'utf8'), { runScripts: 'outside-only' })
  8. let enabled = true
  9. let ready = false
  10. const disableAll = vi.fn(async () => { enabled = false; ready = true })
  11. const toggle = vi.fn(async (_name: string, active: boolean) => { enabled = active })
  12. const api = {
  13. locale: async () => resolveDesktopLocale('en'),
  14. backend: { status: async () => ready ? { phase: 'ready' } : { phase: 'error', message: 'plugin requires Cordis ^2.0.0' }, retry: vi.fn() },
  15. plugins: { list: async () => [{ name: 'example-plugin', version: '1.0.0', enabled }], disableAll, toggle },
  16. }
  17. Object.defineProperty(dom.window, 'dshDesktop', { value: api })
  18. try {
  19. runInContext(readFileSync(new URL('../renderer/plugin-manager.js', import.meta.url), 'utf8'), dom.getInternalVMContext())
  20. const document = dom.window.document
  21. await expect.poll(() => document.querySelector('#plugins li')?.textContent).toContain('example-plugin')
  22. expect(document.querySelector<HTMLElement>('#recovery')?.hidden).toBe(false)
  23. expect(document.querySelector('#startup-error')?.textContent).toBe('plugin requires Cordis ^2.0.0')
  24. document.querySelector<HTMLButtonElement>('#disable-all')?.click()
  25. await expect.poll(() => document.querySelector<HTMLElement>('#recovery')?.hidden).toBe(true)
  26. expect(disableAll).toHaveBeenCalledOnce()
  27. expect(document.querySelector('#plugins li')?.textContent).toMatchInlineSnapshot('"example-plugin1.0.0 · DisabledEnableUpdateRemove"')
  28. document.querySelector<HTMLButtonElement>('#plugins li button')?.click()
  29. await expect.poll(() => toggle.mock.calls).toEqual([['example-plugin', true]])
  30. await expect.poll(() => document.querySelector('#plugins li')?.textContent).toBe('example-plugin1.0.0DisableUpdateRemove')
  31. } finally { dom.window.close() }
  32. })