browser-plugin.client.spec.tsx 4.5 KB

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