apply.client.spec.ts 9.1 KB

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