apply.client.spec.ts 10 KB

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