apply.client.spec.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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-runtime/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({ rpcId: 'c', result: { ok: false, error: {} } }))
  27. const describeSettings = vi.fn(() => Promise.resolve(served === undefined
  28. ? { rpcId: 's', result: { ok: false, error: {} } }
  29. : {
  30. rpcId: 's',
  31. result: {
  32. ok: true,
  33. value: {
  34. writable: true,
  35. hasDocument: true,
  36. namespaces: served.map(ns => ({
  37. ns, schema: {}, value: {}, applies: 'live', secrets: [], revision: 0,
  38. })),
  39. },
  40. },
  41. }))
  42. // The section binds its scopes through the Settings surface's service, and
  43. // forwarded Host events reach it through the same `$dispatch` handoff the
  44. // connection sink makes.
  45. new TestRemote(ctx)
  46. ctx.provide('connection', {
  47. isLoopback: true,
  48. api: {
  49. settings: { describe: describeSettings },
  50. credentials: { describe: describeCredentials },
  51. },
  52. } as never)
  53. await ctx.plugin({ inject: [...settingsInject], apply: settingsApply }).await()
  54. return { ctx, slots: ctx.get('slots') as SlotRegistry, describeCredentials, describeSettings }
  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('declares the services it uses', () => {
  64. expect(inject).toEqual(['slots', 'locale', 'connection', 'remote', 'settingsScope'])
  65. })
  66. it('registers one Plugins section and declares the tab and card slots', async () => {
  67. const { ctx, slots } = await bench()
  68. declareRoot(slots)
  69. await ctx.plugin({ inject: [...inject], apply }).await()
  70. const section = slots.entries('settings.section')[0]!
  71. expect(section.options).toMatchObject({ id: 'plugins', order: 15 })
  72. // The nav label is a locale-following thunk; owners resolve it at read time.
  73. expect(resolveSlotLabel(section.options.label)).toBe('插件')
  74. expect(slots.spec('settings.plugins.tab')).toMatchObject({ kind: 'list', scope: 'root' })
  75. const tab = slots.entries('settings.plugins.tab')[0]!
  76. expect(tab.options).toMatchObject({ id: 'configurable', order: 0 })
  77. expect(resolveSlotLabel(tab.options.label)).toBe('插件配置')
  78. expect(slots.spec('settings.plugin.item')).toMatchObject({ kind: 'keyed', scope: 'root' })
  79. })
  80. it('injects a live tab projection, the card directory, and one business face per card', async () => {
  81. const { ctx, slots } = await bench()
  82. declareRoot(slots)
  83. await ctx.plugin({ inject: [...inject], apply }).await()
  84. const section = slots.entries('settings.section')[0]!
  85. const sectionFace = (section.inject as unknown as () => PluginsSettingsSectionInjected)()
  86. const initialTabs = sectionFace.hooks.tabs.getSnapshot()
  87. expect(initialTabs).toEqual([
  88. { id: 'configurable', order: 0, label: '插件配置' },
  89. ])
  90. expect(sectionFace.hooks.tabs.getSnapshot()).toBe(initialTabs)
  91. const listener = vi.fn()
  92. const unsubscribe = sectionFace.hooks.tabs.subscribe(listener)
  93. slots.register({ name: 'settings.plugins.tab', id: 'plain' } as never, () => null)
  94. expect(sectionFace.hooks.tabs.getSnapshot()).toEqual([
  95. { id: 'configurable', order: 0, label: '插件配置' },
  96. { id: 'plain', order: 0, label: '' },
  97. ])
  98. unsubscribe()
  99. const tab = slots.entries('settings.plugins.tab')[0]!
  100. const tabFace = (tab.inject as unknown as () => ConfigurablePluginsTabFace)()
  101. expect(Object.keys(tabFace.hooks)).toEqual(['configurablePlugins'])
  102. for (const entry of slots.entries('settings.plugin.item')) {
  103. const face = (entry as { inject?: () => unknown }).inject?.() as { hooks: Record<string, unknown> }
  104. // Each card injects exactly one snapshot store plus its own actions.
  105. expect(Object.keys(face.hooks)).toHaveLength(1)
  106. }
  107. })
  108. it('keys each card it ships on the settings namespace that card edits', async () => {
  109. const { ctx, slots } = await bench()
  110. declareRoot(slots)
  111. await ctx.plugin({ inject: [...inject], apply }).await()
  112. expect(slots.entries('settings.plugin.item').map(entry => entry.options.key))
  113. .toEqual(['shell', 'agent-loop', 'web-search-deepseek'])
  114. })
  115. it('dispatches the served namespaces its cards claim, and no others', async () => {
  116. // ui-theme is served but belongs to another surface, and a deployment
  117. // composing no PowerShell/POSIX executor serves no `bash` at all.
  118. const { ctx, slots } = await bench(['agent-loop', 'ui-theme', 'web-search-deepseek'])
  119. declareRoot(slots)
  120. await ctx.plugin({ inject: [...inject], apply }).await()
  121. const tab = slots.entries('settings.plugins.tab')[0]!
  122. const face = (tab.inject as unknown as () => ConfigurablePluginsTabFace)()
  123. await vi.waitFor(() => {
  124. expect(face.hooks.configurablePlugins.getSnapshot().namespaces)
  125. .toEqual(['agent-loop', 'web-search-deepseek'])
  126. })
  127. })
  128. it('re-reads the served namespaces when the Host commits a settings document', async () => {
  129. // Which namespaces the Host serves is a registration fact the wire never
  130. // announces on its own, so the tab rides the invalidation that can
  131. // accompany a changed composition.
  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.remote.$dispatch('settings/document-updated', ['bash', 1])
  138. await vi.waitFor(() => { expect(describeSettings).toHaveBeenCalled() })
  139. })
  140. it('re-reads the served namespaces after a reconnect', async () => {
  141. const { ctx, slots, describeSettings } = await bench(['bash'])
  142. declareRoot(slots)
  143. await ctx.plugin({ inject: [...inject], apply }).await()
  144. await vi.waitFor(() => { expect(describeSettings).toHaveBeenCalled() })
  145. describeSettings.mockClear()
  146. ctx.emit('connection/reset')
  147. await vi.waitFor(() => { expect(describeSettings).toHaveBeenCalled() })
  148. })
  149. it('re-reads the credential when the Host reports the watched reference changed', async () => {
  150. const { ctx, slots, describeCredentials } = await bench()
  151. declareRoot(slots)
  152. await ctx.plugin({ inject: [...inject], apply }).await()
  153. await vi.waitFor(() => { expect(describeCredentials).toHaveBeenCalled() })
  154. describeCredentials.mockClear()
  155. // A key written on another surface changes no settings section, so this
  156. // event is the only thing that reaches the card.
  157. ctx.remote.$dispatch('credentials/updated', ['DEEPSEEK_API_KEY'])
  158. await vi.waitFor(() => { expect(describeCredentials).toHaveBeenCalledTimes(1) })
  159. })
  160. it('ignores a credential change for a reference no card watches', async () => {
  161. const { ctx, slots, describeCredentials } = await bench()
  162. declareRoot(slots)
  163. await ctx.plugin({ inject: [...inject], apply }).await()
  164. await vi.waitFor(() => { expect(describeCredentials).toHaveBeenCalled() })
  165. describeCredentials.mockClear()
  166. ctx.remote.$dispatch('credentials/updated', ['SOME_OTHER_KEY'])
  167. await Promise.resolve()
  168. expect(describeCredentials).not.toHaveBeenCalled()
  169. })
  170. it('registers into a declaration that arrives after apply', async () => {
  171. const { ctx, slots } = await bench()
  172. await ctx.plugin({ inject: [...inject], apply }).await()
  173. declareRoot(slots)
  174. await vi.waitFor(() => { expect(slots.entries('settings.section')).toHaveLength(1) })
  175. })
  176. it('collapses every contribution on teardown', async () => {
  177. const { ctx, slots } = await bench()
  178. declareRoot(slots)
  179. const fiber = ctx.plugin({ inject: [...inject], apply })
  180. await fiber.await()
  181. expect(slots.entries('settings.plugin.item')).toHaveLength(3)
  182. await fiber.dispose()
  183. expect(slots.entries('settings.section')).toHaveLength(0)
  184. expect(slots.spec('settings.plugins.tab')).toBeUndefined()
  185. expect(slots.spec('settings.plugin.item')).toBeUndefined()
  186. })
  187. })