browser-plugin.client.spec.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // @vitest-environment jsdom
  2. import { Context, Service } from '@deepseek-ai/cordis'
  3. import { afterEach, describe, expect, it, vi } from 'vitest'
  4. import { cleanup } from '@testing-library/react'
  5. import { LocaleRuntime } from '@deepseek-ai/dsh-client-locale/client'
  6. import { SlotRegistry } from '@deepseek-ai/dsh-client-ui-renderer/client'
  7. import { resolveSlotLabel } from '@deepseek-ai/dsh-client-ui-slots'
  8. import { usePinnedBrowserLanguages } from '@deepseek-ai/dsh-client-test-runtime'
  9. import { apply, inject, NS } from '../src/client/index.ts'
  10. import { PluginInventorySettingsTab } from '../src/client/PluginInventorySettingsTab.tsx'
  11. import type { PluginEntryId } from '@deepseek-ai/dsh-api-remotes/client'
  12. import type { PluginInventorySettingsTabInjected } from '../src/client/PluginInventorySettingsTab.tsx'
  13. import { apply as hostApply } from '../src/index.ts'
  14. usePinnedBrowserLanguages('zh-CN')
  15. afterEach(cleanup)
  16. const EMPTY = { entries: [] }
  17. type ListResult =
  18. | { readonly ok: true; readonly value: typeof EMPTY }
  19. | { readonly ok: false; readonly error: { readonly code: string; readonly message: string } }
  20. async function bench() {
  21. const ctx = new Context()
  22. await ctx.plugin(SlotRegistry).await()
  23. const locale = new LocaleRuntime(ctx)
  24. ctx.provide('locale', locale)
  25. class RemoteService extends Service {
  26. constructor(serviceCtx: Context) {
  27. super(serviceCtx, 'remote')
  28. }
  29. }
  30. new RemoteService(ctx)
  31. const list = vi.fn<() => Promise<ListResult>>()
  32. .mockResolvedValue({ ok: true, value: EMPTY })
  33. ctx.provide('remote.pluginInventory', { list })
  34. const listBundles = vi.fn().mockResolvedValue({ ok: true, value: [] })
  35. const managed = {
  36. listBundles, listPlugins: vi.fn().mockResolvedValue({ ok: true, value: [] }),
  37. setPluginEnabled: vi.fn().mockResolvedValue({ ok: true, value: { changed: true, application: 'applied', message: 'plugin' } }),
  38. setBundleEnabled: vi.fn().mockResolvedValue({ ok: true, value: { changed: true, application: 'applied', message: 'bundle' } }),
  39. installBundle: vi.fn().mockResolvedValue({ ok: true, value: { changed: true, application: 'restart-required', message: 'install' } }),
  40. removeBundle: vi.fn().mockResolvedValue({ ok: true, value: { changed: true, application: 'applied', message: 'remove' } }),
  41. }
  42. ctx.provide('remote.pluginManager', managed)
  43. return { ctx, slots: ctx.get('slots') as SlotRegistry, locale, list, listBundles, managed }
  44. }
  45. function declare(slots: SlotRegistry): () => void {
  46. return slots.register({
  47. name: 'root',
  48. children: { 'settings.plugins.tab': { kind: 'list', scope: 'root' } },
  49. } as never, () => null)
  50. }
  51. describe('ui-settings-plugin-inventory browser plugin', () => {
  52. it('keeps the host Loader entry inert', () => {
  53. expect(hostApply).not.toThrow()
  54. })
  55. it('declares only the services used by the Settings Remote contribution', () => {
  56. expect(inject).toEqual(['slots', 'locale', 'remote', 'remote.pluginInventory', 'remote.pluginManager'])
  57. })
  58. it('registers a localized tab without reading the Remote eagerly', async () => {
  59. const b = await bench()
  60. declare(b.slots)
  61. await b.ctx.plugin({ inject: [...inject], apply }).await()
  62. const entry = b.slots.entries('settings.plugins.tab')[0]!
  63. expect(entry.component).toBe(PluginInventorySettingsTab)
  64. expect(entry.options).toMatchObject({ id: 'all', order: 10 })
  65. expect(entry.locale).toBe(NS)
  66. expect(resolveSlotLabel(entry.options.label)).toBe('插件列表')
  67. expect(b.list).not.toHaveBeenCalled()
  68. const injected = (entry.inject as unknown as () => PluginInventorySettingsTabInjected)()
  69. await expect(injected.list()).resolves.toEqual(EMPTY)
  70. expect(b.list).toHaveBeenCalledOnce()
  71. await expect(injected.management?.listBundles()).resolves.toEqual([])
  72. expect(b.listBundles).toHaveBeenCalledOnce()
  73. const manager = injected.management!
  74. await expect(manager.listPlugins()).resolves.toEqual([])
  75. await expect(manager.setPluginEnabled('include:plugin' as PluginEntryId, false)).resolves.toMatchObject({ message: 'plugin' })
  76. expect(b.managed.setPluginEnabled).toHaveBeenCalledWith('include:plugin', false)
  77. await expect(manager.setBundleEnabled('extra', false)).resolves.toMatchObject({ message: 'bundle' })
  78. expect(b.managed.setBundleEnabled).toHaveBeenCalledWith('extra', false)
  79. await expect(manager.installBundle('extra@1', { enabled: false })).resolves.toMatchObject({ application: 'restart-required' })
  80. expect(b.managed.installBundle).toHaveBeenCalledWith('extra@1', { enabled: false })
  81. await expect(manager.removeBundle('extra')).resolves.toMatchObject({ message: 'remove' })
  82. expect(b.managed.removeBundle).toHaveBeenCalledWith('extra')
  83. b.listBundles.mockResolvedValueOnce({ ok: false, error: new Error('manager unavailable') })
  84. await expect(manager.listBundles()).rejects.toThrow('manager unavailable')
  85. b.list.mockResolvedValueOnce({ ok: false, error: { code: 'REMOTE_ERROR', message: 'unavailable' } })
  86. await expect(injected.list()).rejects.toThrow('pluginInventory.list failed: REMOTE_ERROR: unavailable')
  87. // Shipped preset names resolve over the agent-preset dictionaries the
  88. // real plugin registers; user-authored metadata stays untranslated.
  89. b.locale.register('settings.agentPreset', 'zh', { presetStandardName: '标准模式' } as never)
  90. expect(injected.presetName({ id: 'standard', trust: 'system', isDefault: true, rows: [] })).toBe('标准模式')
  91. expect(injected.presetName({ id: 'mine', trust: 'user', name: '我自己的', isDefault: false, rows: [] })).toBe('我自己的')
  92. await b.ctx.fiber.dispose()
  93. })
  94. it('follows locale and recovers across late declaration and declarer reload', async () => {
  95. const b = await bench()
  96. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  97. await fiber.await()
  98. expect(b.slots.entries('settings.plugins.tab')).toHaveLength(0)
  99. const stop = declare(b.slots)
  100. await vi.waitFor(() => { expect(b.slots.entries('settings.plugins.tab')).toHaveLength(1) })
  101. b.locale.setLocale('en')
  102. expect(resolveSlotLabel(b.slots.entries('settings.plugins.tab')[0]!.options.label)).toBe('Plugin list')
  103. stop()
  104. expect(b.slots.entries('settings.plugins.tab')).toHaveLength(0)
  105. declare(b.slots)
  106. await vi.waitFor(() => {
  107. expect(b.slots.entries('settings.plugins.tab')[0]?.component).toBe(PluginInventorySettingsTab)
  108. })
  109. await fiber.dispose()
  110. expect(b.slots.entries('settings.plugins.tab')).toHaveLength(0)
  111. expect(() => b.locale.register(NS, 'zh', {})).not.toThrow()
  112. await b.ctx.fiber.dispose()
  113. })
  114. })