apply.client.spec.ts 9.8 KB

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