apply.client.spec.ts 10 KB

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