apply.client.spec.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /** What the browser half registers, and that it all leaves with the fiber. */
  2. import { Context } from '@deepseek-ai/cordis'
  3. import { describe, expect, it, vi } from 'vitest'
  4. import { resolveSlotLabel } from '@deepseek-ai/dsh-client-ui-slots'
  5. import { SlotRegistry } from '@deepseek-ai/dsh-client-ui-renderer/client'
  6. import { LocaleRuntime } from '@deepseek-ai/dsh-client-locale/client'
  7. import { TestRemote } from '@deepseek-ai/dsh-client-test-runtime'
  8. import { apply as settingsApply, inject as settingsInject } from '@deepseek-ai/dsh-client-ui-settings/client'
  9. import { apply, inject } from '@deepseek-ai/dsh-client-ui-settings-plugins/client'
  10. import type {
  11. ConfigurablePluginsTabFace, PluginsSettingsSectionInjected,
  12. } from '@deepseek-ai/dsh-client-ui-settings-plugins/client'
  13. // These specs assert the shipped Chinese copy. The lane has no jsdom `window`,
  14. // so browser-language detection never runs and a fresh LocaleRuntime opens on
  15. // FALLBACK_LOCALE (en); bench stages zh explicitly on the locale instead.
  16. /**
  17. * @param served - namespaces the Host describes; omitted answers a failed read,
  18. * which is what most of these specs want (no card has anything to render).
  19. */
  20. async function bench(served?: string[]) {
  21. const ctx = new Context()
  22. await ctx.plugin(SlotRegistry).await()
  23. const locale = new LocaleRuntime(ctx)
  24. locale.setLocale('zh')
  25. ctx.provide('locale', locale)
  26. const describeCredentials = vi.fn(() => Promise.resolve({ ok: false, error: { code: 'internal', message: 'no provider', details: {} } }))
  27. const describeSettings = vi.fn(() => Promise.resolve(served === undefined
  28. ? { ok: false, error: { code: 'internal', message: 'no provider', details: {} } }
  29. : {
  30. ok: true,
  31. value: {
  32. writable: true,
  33. hasDocument: true,
  34. namespaces: served.map(ns => ({
  35. ns, schema: {}, value: {}, applies: 'live', secrets: [], revision: 0,
  36. })),
  37. },
  38. }))
  39. const remote = new TestRemote(ctx, {
  40. credentials: { describe: describeCredentials, set: vi.fn() },
  41. settings: { describe: describeSettings },
  42. })
  43. ctx.provide('connection', { isLoopback: true, api: {} } as never)
  44. await ctx.plugin({ inject: [...settingsInject], apply: settingsApply }).await()
  45. return { ctx, slots: ctx.get('slots') as SlotRegistry, describeCredentials, describeSettings, remote }
  46. }
  47. function declareRoot(slots: SlotRegistry): () => void {
  48. return slots.register({
  49. name: 'root',
  50. children: { 'settings.section': { kind: 'list', scope: 'root' } },
  51. } as never, () => null)
  52. }
  53. describe('ui-settings-plugins apply', () => {
  54. it('declares the services it uses', () => {
  55. expect(inject).toEqual(['slots', 'locale', 'remote', 'remote.credentials', 'settingsScope'])
  56. })
  57. it('registers one Plugins section and declares the tab and card slots', async () => {
  58. const { ctx, slots } = await bench()
  59. declareRoot(slots)
  60. await ctx.plugin({ inject: [...inject], apply }).await()
  61. const section = slots.entries('settings.section')[0]!
  62. expect(section.options).toMatchObject({ id: 'plugins', order: 15 })
  63. // The nav label is a locale-following thunk; owners resolve it at read time.
  64. expect(resolveSlotLabel(section.options.label)).toBe('插件')
  65. expect(slots.spec('settings.plugins.tab')).toMatchObject({ kind: 'list', scope: 'root' })
  66. const tab = slots.entries('settings.plugins.tab')[0]!
  67. expect(tab.options).toMatchObject({ id: 'configurable', order: 0 })
  68. expect(resolveSlotLabel(tab.options.label)).toBe('插件配置')
  69. expect(slots.spec('settings.plugin.item')).toMatchObject({ kind: 'keyed', scope: 'root' })
  70. })
  71. it('injects a live tab projection, the card directory, and one business face per card', async () => {
  72. const { ctx, slots } = await bench()
  73. declareRoot(slots)
  74. await ctx.plugin({ inject: [...inject], apply }).await()
  75. const section = slots.entries('settings.section')[0]!
  76. const sectionFace = (section.inject as unknown as () => PluginsSettingsSectionInjected)()
  77. const initialTabs = sectionFace.hooks.tabs.getSnapshot()
  78. expect(initialTabs).toEqual([
  79. { id: 'configurable', order: 0, label: '插件配置' },
  80. ])
  81. expect(sectionFace.hooks.tabs.getSnapshot()).toBe(initialTabs)
  82. const listener = vi.fn()
  83. const unsubscribe = sectionFace.hooks.tabs.subscribe(listener)
  84. slots.register({ name: 'settings.plugins.tab', id: 'plain' } as never, () => null)
  85. expect(sectionFace.hooks.tabs.getSnapshot()).toEqual([
  86. { id: 'configurable', order: 0, label: '插件配置' },
  87. { id: 'plain', order: 0, label: '' },
  88. ])
  89. unsubscribe()
  90. const tab = slots.entries('settings.plugins.tab')[0]!
  91. const tabFace = (tab.inject as unknown as () => ConfigurablePluginsTabFace)()
  92. expect(Object.keys(tabFace.hooks)).toEqual(['configurablePlugins'])
  93. for (const entry of slots.entries('settings.plugin.item')) {
  94. const face = (entry as { inject?: () => unknown }).inject?.() as { hooks: Record<string, unknown> }
  95. // Each card injects exactly one snapshot store plus its own actions.
  96. expect(Object.keys(face.hooks)).toHaveLength(1)
  97. }
  98. })
  99. it('keys each card it ships on the settings namespace that card edits', async () => {
  100. const { ctx, slots } = await bench()
  101. declareRoot(slots)
  102. await ctx.plugin({ inject: [...inject], apply }).await()
  103. expect(slots.entries('settings.plugin.item').map(entry => entry.options.key))
  104. .toEqual(['shell', 'agent-loop', 'web-search-deepseek'])
  105. })
  106. it('dispatches the served namespaces its cards claim, and no others', async () => {
  107. // ui-theme is served but belongs to another surface, and a deployment
  108. // composing no PowerShell/POSIX executor serves no `bash` at all.
  109. const { ctx, slots } = await bench(['agent-loop', 'ui-theme', 'web-search-deepseek'])
  110. declareRoot(slots)
  111. await ctx.plugin({ inject: [...inject], apply }).await()
  112. const tab = slots.entries('settings.plugins.tab')[0]!
  113. const face = (tab.inject as unknown as () => ConfigurablePluginsTabFace)()
  114. await vi.waitFor(() => {
  115. expect(face.hooks.configurablePlugins.getSnapshot().namespaces)
  116. .toEqual(['agent-loop', 'web-search-deepseek'])
  117. })
  118. })
  119. it('re-reads the served namespaces when the Host commits a settings document', async () => {
  120. // Which namespaces the Host serves is a registration fact the wire never
  121. // announces on its own, so the tab rides the invalidation that can
  122. // accompany a changed composition.
  123. const { ctx, slots, describeSettings, remote } = await bench(['bash'])
  124. declareRoot(slots)
  125. await ctx.plugin({ inject: [...inject], apply }).await()
  126. await vi.waitFor(() => { expect(describeSettings).toHaveBeenCalled() })
  127. describeSettings.mockClear()
  128. remote.emit('settings/document-updated', ['bash', 1])
  129. await vi.waitFor(() => { expect(describeSettings).toHaveBeenCalled() })
  130. })
  131. it('re-reads the served namespaces after a reconnect', async () => {
  132. const { ctx, slots, describeSettings } = await bench(['bash'])
  133. declareRoot(slots)
  134. await ctx.plugin({ inject: [...inject], apply }).await()
  135. await vi.waitFor(() => { expect(describeSettings).toHaveBeenCalled() })
  136. describeSettings.mockClear()
  137. ctx.emit('connection/reset')
  138. await vi.waitFor(() => { expect(describeSettings).toHaveBeenCalled() })
  139. })
  140. it('re-reads the credential when the Host reports the watched reference changed', async () => {
  141. const { ctx, slots, describeCredentials, remote } = await bench()
  142. declareRoot(slots)
  143. await ctx.plugin({ inject: [...inject], apply }).await()
  144. await vi.waitFor(() => { expect(describeCredentials).toHaveBeenCalled() })
  145. describeCredentials.mockClear()
  146. // A key written on another surface changes no settings section, so this
  147. // event is the only thing that reaches the card.
  148. remote.emit('credentials/reference-updated', ['DEEPSEEK_API_KEY'])
  149. await vi.waitFor(() => { expect(describeCredentials).toHaveBeenCalledTimes(1) })
  150. })
  151. it('ignores a credential change for a reference no card watches', async () => {
  152. const { ctx, slots, describeCredentials, remote } = await bench()
  153. declareRoot(slots)
  154. await ctx.plugin({ inject: [...inject], apply }).await()
  155. await vi.waitFor(() => { expect(describeCredentials).toHaveBeenCalled() })
  156. describeCredentials.mockClear()
  157. remote.emit('credentials/reference-updated', ['SOME_OTHER_KEY'])
  158. await Promise.resolve()
  159. expect(describeCredentials).not.toHaveBeenCalled()
  160. })
  161. it('registers into a declaration that arrives after apply', async () => {
  162. const { ctx, slots } = await bench()
  163. await ctx.plugin({ inject: [...inject], apply }).await()
  164. declareRoot(slots)
  165. await vi.waitFor(() => { expect(slots.entries('settings.section')).toHaveLength(1) })
  166. })
  167. it('collapses every contribution on teardown', async () => {
  168. const { ctx, slots } = await bench()
  169. declareRoot(slots)
  170. const fiber = ctx.plugin({ inject: [...inject], apply })
  171. await fiber.await()
  172. expect(slots.entries('settings.plugin.item')).toHaveLength(3)
  173. await fiber.dispose()
  174. expect(slots.entries('settings.section')).toHaveLength(0)
  175. expect(slots.spec('settings.plugins.tab')).toBeUndefined()
  176. expect(slots.spec('settings.plugin.item')).toBeUndefined()
  177. })
  178. })