1
0

browser-plugin.client.spec.tsx 5.1 KB

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