apply.client.spec.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { Context } from '@deepseek-ai/cordis'
  2. import { describe, expect, it, vi } from 'vitest'
  3. import { SlotRegistry } from '@deepseek-ai/dsh-client-ui-renderer/client'
  4. import { LocaleRuntime } from '@deepseek-ai/dsh-client-locale/client'
  5. import { apply, inject } from '@deepseek-ai/dsh-client-ui-workspace/client'
  6. import type { WorkspaceBrowserInjected, WorkspacePickerInjected } from '@deepseek-ai/dsh-client-ui-workspace/client'
  7. import { WorkspaceBrowser } from '../src/client/rows/WorkspaceBrowser.tsx'
  8. import { WorkspacePicker } from '../src/client/WorkspacePicker.tsx'
  9. async function bench() {
  10. const ctx = new Context()
  11. await ctx.plugin(SlotRegistry).await()
  12. const create = vi.fn(async (input: { name: string } | { path: string }) => ({
  13. workspaceId: 'ws-new' as never,
  14. path: 'name' in input ? `/projects/${input.name}` : input.path,
  15. title: 'new', sessionIds: [], createdAt: '0', updatedAt: '0',
  16. }))
  17. const rename = vi.fn(async () => ({}))
  18. const insertSessionBefore = vi.fn(async () => ({}))
  19. const open = vi.fn()
  20. const clear = vi.fn()
  21. const search = vi.fn(async () => ({
  22. ok: true as const,
  23. value: { items: [{ sessionId: 'session' as never, snippet: 'match' }], hasMore: false },
  24. }))
  25. const renameSession = vi.fn(async (title: string) => ({ ok: true, value: { title, seq: 1 } }))
  26. const binding = vi.fn(() => ({ session: { rename: renameSession } }))
  27. const fork = vi.fn(async () => 'forked' as never)
  28. const subscribe = () => () => {}
  29. ctx.provide('workspaces', {
  30. list: {
  31. getSnapshot: () => ({
  32. items: [], archivedSessionIds: [], state: 'idle', phase: 'ready', error: null,
  33. }),
  34. subscribe,
  35. },
  36. create,
  37. rename,
  38. delete: vi.fn(async () => undefined),
  39. insertBefore: vi.fn(async () => undefined),
  40. archiveSession: vi.fn(async () => undefined),
  41. insertSessionBefore,
  42. } as never)
  43. ctx.provide('sessions', {
  44. list: {
  45. getSnapshot: () => ({
  46. ids: [], byId: {}, current: undefined, phase: 'ready',
  47. subagentsByParent: {}, jobsBySession: {}, currentAddress: undefined,
  48. }),
  49. subscribe,
  50. },
  51. create: vi.fn(async () => 'created' as never),
  52. open,
  53. clear,
  54. search,
  55. searchResultLimit: 20,
  56. binding,
  57. fork,
  58. } as never)
  59. ctx.provide('connection', {
  60. hostDescription: { getSnapshot: () => undefined, subscribe: () => () => {} },
  61. } as never)
  62. const locale = new LocaleRuntime(ctx)
  63. // These specs assert the shipped Chinese copy. There is no jsdom `window`
  64. // in this lane, so browser-language detection never runs and the locale
  65. // comes from FALLBACK_LOCALE (en): state the asserted locale explicitly.
  66. locale.setLocale('zh')
  67. ctx.provide('locale', locale)
  68. return {
  69. ctx, slots: ctx.get('slots') as SlotRegistry, locale, create, rename,
  70. insertSessionBefore, open, clear, search, renameSession, binding, fork,
  71. }
  72. }
  73. type HoleName = 'sidebar.workspaces' | 'conversation.hero.workspace' | 'conversation.empty.workspace'
  74. /** Declare any subset of the holes with a single root registration ('root' is a single slot). */
  75. function declare(slots: SlotRegistry, ...names: HoleName[]): () => void {
  76. const children = Object.fromEntries(names.map(name => [name, { kind: 'single', scope: 'root' }]))
  77. return slots.register({ name: 'root', children } as never, () => null)
  78. }
  79. describe('ui-workspace apply', () => {
  80. it('declares the services it drives', () => {
  81. expect(inject).toEqual([
  82. 'slots', 'sessions', 'workspaces', 'locale', 'connection',
  83. ])
  84. })
  85. it('registers browser and pickers for declarations arriving before or after apply', async () => {
  86. const before = await bench()
  87. declare(before.slots, 'sidebar.workspaces')
  88. await before.ctx.plugin({ inject: [...inject], apply }).await()
  89. expect(before.slots.entries('sidebar.workspaces')[0]!.component).toBe(WorkspaceBrowser)
  90. // Copy rides the standard locale seat: the entry declares the namespace
  91. // and apply registered both dictionaries.
  92. expect(before.slots.entries('sidebar.workspaces')[0]!.locale).toBe('workspace')
  93. expect(before.locale.bind('workspace')('session.new')).toBe('新会话')
  94. const after = await bench()
  95. await after.ctx.plugin({ inject: [...inject], apply }).await()
  96. declare(after.slots, 'conversation.hero.workspace', 'conversation.empty.workspace')
  97. await Promise.resolve()
  98. expect(after.slots.entries('conversation.hero.workspace')[0]!.component).toBe(WorkspacePicker)
  99. // expect(after.slots.entries('conversation.empty.workspace')[0]!.component).toBe(WorkspacePicker)
  100. })
  101. it('routes browser actions and picker creation to the services', async () => {
  102. const b = await bench()
  103. declare(b.slots, 'sidebar.workspaces', 'conversation.hero.workspace')
  104. await b.ctx.plugin({ inject: [...inject], apply }).await()
  105. const startSession = vi.spyOn(b.ctx.uiWorkspace, 'startSession').mockImplementation(() => undefined)
  106. const browser = (b.slots.entries('sidebar.workspaces')[0]!.inject as () => WorkspaceBrowserInjected)()
  107. // Both arms delegate to the shared Session navigation action.
  108. browser.startSession('ws' as never)
  109. expect(startSession).toHaveBeenCalledWith('ws')
  110. browser.startSession()
  111. expect(startSession).toHaveBeenLastCalledWith(undefined)
  112. browser.open('session' as never)
  113. expect(b.open).toHaveBeenCalledWith('session')
  114. const signal = new AbortController().signal
  115. await expect(browser.searchSessions('match', signal)).resolves.toEqual({
  116. items: [{ sessionId: 'session', snippet: 'match' }],
  117. hasMore: false,
  118. })
  119. expect(b.search).toHaveBeenCalledWith('match', signal)
  120. expect(browser.searchResultLimit).toBe(20)
  121. await browser.renameSession('session' as never, 'renamed session')
  122. expect(b.binding).toHaveBeenCalledWith('session')
  123. expect(b.renameSession).toHaveBeenCalledWith('renamed session')
  124. browser.forkSession('session' as never)
  125. await vi.waitFor(() => {
  126. expect(b.open).toHaveBeenCalledWith('forked')
  127. })
  128. expect(b.fork).toHaveBeenCalledWith({ sessionId: 'session', increaseTitle: true })
  129. await browser.renameWorkspace('ws' as never, 'renamed')
  130. expect(b.rename).toHaveBeenCalledWith('ws', 'renamed')
  131. await browser.insertSessionBefore('ws' as never, 's1' as never, 's2' as never)
  132. expect(b.insertSessionBefore).toHaveBeenCalledWith('ws', 's1', 's2')
  133. await browser.createWorkspace({ path: '/tmp/browser-project' })
  134. expect(b.create).toHaveBeenCalledWith({ path: '/tmp/browser-project' })
  135. const picker = (b.slots.entries('conversation.hero.workspace')[0]!.inject as () => WorkspacePickerInjected)()
  136. await picker.createWorkspace({ path: '/tmp/project' })
  137. expect(b.create).toHaveBeenCalledWith({ path: '/tmp/project' })
  138. })
  139. it('declares the two directory-flow holes and reports their occupancy per surface', async () => {
  140. const b = await bench()
  141. declare(b.slots, 'sidebar.workspaces', 'conversation.hero.workspace')
  142. await b.ctx.plugin({ inject: [...inject], apply }).await()
  143. // Registration declared the child holes (declaration = render authorization).
  144. expect(b.slots.spec('sidebar.workspaces.directoryFlow')).toMatchObject({ kind: 'single' })
  145. expect(b.slots.spec('conversation.hero.workspace.directoryFlow')).toMatchObject({ kind: 'single' })
  146. const browser = (b.slots.entries('sidebar.workspaces')[0]!.inject as () => WorkspaceBrowserInjected)()
  147. const picker = (b.slots.entries('conversation.hero.workspace')[0]!.inject as () => WorkspacePickerInjected)()
  148. expect(browser.hooks.directoryFlow.getSnapshot()).toBe(false)
  149. expect(browser.hooks.hostDescription.getSnapshot()).toBeUndefined()
  150. expect(picker.hooks.directoryFlow.getSnapshot()).toBe(false)
  151. // A flow occupant flips exactly its own surface, and the source notifies.
  152. const notified = vi.fn()
  153. const unsubscribe = browser.hooks.directoryFlow.subscribe(notified)
  154. const dispose = b.slots.register({ name: 'sidebar.workspaces.directoryFlow' } as never, () => null)
  155. expect(browser.hooks.directoryFlow.getSnapshot()).toBe(true)
  156. expect(picker.hooks.directoryFlow.getSnapshot()).toBe(false)
  157. await Promise.resolve()
  158. expect(notified).toHaveBeenCalled()
  159. dispose()
  160. expect(browser.hooks.directoryFlow.getSnapshot()).toBe(false)
  161. unsubscribe()
  162. })
  163. it('rejects the browser search callback on a Session Controller business error', async () => {
  164. const b = await bench()
  165. b.search.mockImplementationOnce(async () => ({
  166. ok: false,
  167. error: { code: 'internal', message: 'index unavailable', details: {} },
  168. }) as never)
  169. declare(b.slots, 'sidebar.workspaces')
  170. await b.ctx.plugin({ inject: [...inject], apply }).await()
  171. const browser = (b.slots.entries('sidebar.workspaces')[0]!.inject as () => WorkspaceBrowserInjected)()
  172. await expect(browser.searchSessions('needle', new AbortController().signal))
  173. .rejects.toThrow('index unavailable')
  174. })
  175. it('unregisters every entry on teardown', async () => {
  176. const b = await bench()
  177. declare(b.slots, 'sidebar.workspaces', 'conversation.hero.workspace', 'conversation.empty.workspace')
  178. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  179. await fiber.await()
  180. await fiber.dispose()
  181. expect(b.slots.entries('sidebar.workspaces')).toHaveLength(0)
  182. expect(b.slots.entries('conversation.hero.workspace')).toHaveLength(0)
  183. // expect(b.slots.entries('conversation.empty.workspace')).toHaveLength(0)
  184. })
  185. })