apply.spec.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { Context } from 'cordis'
  2. import { describe, expect, it, vi } from 'vitest'
  3. import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
  4. import { LocaleService } from '@deepseek-ai/dsh-client-locale/client'
  5. import { usePinnedBrowserLanguages } from '@deepseek-ai/dsh-client-test-runtime'
  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/WorkspaceBrowser.tsx'
  9. import { WorkspacePicker } from '../src/client/WorkspacePicker.tsx'
  10. // The service reads its initial locale from the browser; these specs assert
  11. // the shipped Chinese copy, so they state the browser they assume.
  12. usePinnedBrowserLanguages('zh-CN')
  13. async function bench() {
  14. const ctx = new Context()
  15. await ctx.plugin(SlotsService).await()
  16. const create = vi.fn(async (input: { name: string } | { path: string }) => ({
  17. workspaceId: 'ws-new' as never,
  18. path: 'name' in input ? `/projects/${input.name}` : input.path,
  19. title: 'new', sessionIds: [], createdAt: '0', updatedAt: '0',
  20. }))
  21. const startSession = vi.fn()
  22. const rename = vi.fn(async () => ({}))
  23. const insertSessionBefore = vi.fn(async () => ({}))
  24. const open = vi.fn()
  25. const clear = vi.fn()
  26. const search = vi.fn(async () => ({
  27. ok: true as const,
  28. value: { items: [{ sessionId: 'session' as never, snippet: 'match' }], hasMore: false },
  29. }))
  30. const renameSession = vi.fn(async (title: string) => ({ ok: true, value: { title, seq: 1 } }))
  31. const binding = vi.fn(() => ({ session: { rename: renameSession } }))
  32. const fork = vi.fn(async () => 'forked' as never)
  33. ctx.provide('workspaces', {
  34. create, startSession, rename, insertSessionBefore,
  35. } as never)
  36. ctx.provide('sessions', { open, clear, search, searchResultLimit: 20, binding, fork } as never)
  37. const locale = new LocaleService(ctx)
  38. ctx.provide('locale', locale)
  39. return {
  40. ctx, slots: ctx.get('slots') as SlotsService, locale, create, startSession, rename,
  41. insertSessionBefore, open, clear, search, renameSession, binding, fork,
  42. }
  43. }
  44. type HoleName = 'sidebar.workspaces' | 'conversation.hero.workspace' | 'conversation.empty.workspace'
  45. /** Declare any subset of the holes with a single root registration ('root' is a single slot). */
  46. function declare(slots: SlotsService, ...names: HoleName[]): () => void {
  47. const children = Object.fromEntries(names.map(name => [name, { kind: 'single', scope: 'root' }]))
  48. return slots.register({ name: 'root', children } as never, () => null)
  49. }
  50. describe('ui-workspace apply', () => {
  51. it('declares the services it drives', () => {
  52. expect(inject).toEqual(['slots', 'sessions', 'workspaces', 'locale'])
  53. })
  54. it('registers browser and pickers for declarations arriving before or after apply', async () => {
  55. const before = await bench()
  56. declare(before.slots, 'sidebar.workspaces')
  57. await before.ctx.plugin({ inject: [...inject], apply }).await()
  58. expect(before.slots.entries('sidebar.workspaces')[0]!.component).toBe(WorkspaceBrowser)
  59. // Copy rides the standard locale seat: the entry declares the namespace
  60. // and apply registered both dictionaries.
  61. expect(before.slots.entries('sidebar.workspaces')[0]!.locale).toBe('workspace')
  62. expect(before.locale.bind('workspace')('session.new')).toBe('新会话')
  63. const after = await bench()
  64. await after.ctx.plugin({ inject: [...inject], apply }).await()
  65. declare(after.slots, 'conversation.hero.workspace', 'conversation.empty.workspace')
  66. await Promise.resolve()
  67. expect(after.slots.entries('conversation.hero.workspace')[0]!.component).toBe(WorkspacePicker)
  68. // expect(after.slots.entries('conversation.empty.workspace')[0]!.component).toBe(WorkspacePicker)
  69. })
  70. it('routes browser actions and picker creation to the services', async () => {
  71. const b = await bench()
  72. declare(b.slots, 'sidebar.workspaces', 'conversation.hero.workspace')
  73. await b.ctx.plugin({ inject: [...inject], apply }).await()
  74. const browser = (b.slots.entries('sidebar.workspaces')[0]!.inject as () => WorkspaceBrowserInjected)()
  75. // Both arms delegate to the runtime's shared New Session action.
  76. browser.startSession('ws' as never)
  77. expect(b.startSession).toHaveBeenCalledWith('ws')
  78. browser.startSession()
  79. expect(b.startSession).toHaveBeenLastCalledWith(undefined)
  80. browser.open('session' as never)
  81. expect(b.open).toHaveBeenCalledWith('session')
  82. const signal = new AbortController().signal
  83. await expect(browser.searchSessions('match', signal)).resolves.toEqual({
  84. items: [{ sessionId: 'session', snippet: 'match' }],
  85. hasMore: false,
  86. })
  87. expect(b.search).toHaveBeenCalledWith('match', signal)
  88. expect(browser.searchResultLimit).toBe(20)
  89. await browser.renameSession('session' as never, 'renamed session')
  90. expect(b.binding).toHaveBeenCalledWith('session')
  91. expect(b.renameSession).toHaveBeenCalledWith('renamed session')
  92. browser.forkSession('session' as never)
  93. await vi.waitFor(() => {
  94. expect(b.open).toHaveBeenCalledWith('forked')
  95. })
  96. expect(b.fork).toHaveBeenCalledWith({ sessionId: 'session', increaseTitle: true })
  97. await browser.renameWorkspace('ws' as never, 'renamed')
  98. expect(b.rename).toHaveBeenCalledWith('ws', 'renamed')
  99. await browser.insertSessionBefore('ws' as never, 's1' as never, 's2' as never)
  100. expect(b.insertSessionBefore).toHaveBeenCalledWith('ws', 's1', 's2')
  101. await browser.createWorkspace({ path: '/tmp/browser-project' })
  102. expect(b.create).toHaveBeenCalledWith({ path: '/tmp/browser-project' })
  103. const picker = (b.slots.entries('conversation.hero.workspace')[0]!.inject as () => WorkspacePickerInjected)()
  104. await picker.createWorkspace({ path: '/tmp/project' })
  105. expect(b.create).toHaveBeenCalledWith({ path: '/tmp/project' })
  106. })
  107. it('declares the two directory-flow holes and reports their occupancy per surface', async () => {
  108. const b = await bench()
  109. declare(b.slots, 'sidebar.workspaces', 'conversation.hero.workspace')
  110. await b.ctx.plugin({ inject: [...inject], apply }).await()
  111. // Registration declared the child holes (declaration = render authorization).
  112. expect(b.slots.spec('sidebar.workspaces.directoryFlow')).toMatchObject({ kind: 'single' })
  113. expect(b.slots.spec('conversation.hero.workspace.directoryFlow')).toMatchObject({ kind: 'single' })
  114. const browser = (b.slots.entries('sidebar.workspaces')[0]!.inject as () => WorkspaceBrowserInjected)()
  115. const picker = (b.slots.entries('conversation.hero.workspace')[0]!.inject as () => WorkspacePickerInjected)()
  116. expect(browser.hooks.directoryFlow.getSnapshot()).toBe(false)
  117. expect(picker.hooks.directoryFlow.getSnapshot()).toBe(false)
  118. // A flow occupant flips exactly its own surface, and the source notifies.
  119. const notified = vi.fn()
  120. const unsubscribe = browser.hooks.directoryFlow.subscribe(notified)
  121. const dispose = b.slots.register({ name: 'sidebar.workspaces.directoryFlow' } as never, () => null)
  122. expect(browser.hooks.directoryFlow.getSnapshot()).toBe(true)
  123. expect(picker.hooks.directoryFlow.getSnapshot()).toBe(false)
  124. await Promise.resolve()
  125. expect(notified).toHaveBeenCalled()
  126. dispose()
  127. expect(browser.hooks.directoryFlow.getSnapshot()).toBe(false)
  128. unsubscribe()
  129. })
  130. it('rejects the browser search callback on a runtime business error', async () => {
  131. const b = await bench()
  132. b.search.mockImplementationOnce(async () => ({
  133. ok: false,
  134. error: { code: 'internal', message: 'index unavailable', details: {} },
  135. }) as never)
  136. declare(b.slots, 'sidebar.workspaces')
  137. await b.ctx.plugin({ inject: [...inject], apply }).await()
  138. const browser = (b.slots.entries('sidebar.workspaces')[0]!.inject as () => WorkspaceBrowserInjected)()
  139. await expect(browser.searchSessions('needle', new AbortController().signal))
  140. .rejects.toThrow('index unavailable')
  141. })
  142. it('unregisters every entry on teardown', async () => {
  143. const b = await bench()
  144. declare(b.slots, 'sidebar.workspaces', 'conversation.hero.workspace', 'conversation.empty.workspace')
  145. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  146. await fiber.await()
  147. await fiber.dispose()
  148. expect(b.slots.entries('sidebar.workspaces')).toHaveLength(0)
  149. expect(b.slots.entries('conversation.hero.workspace')).toHaveLength(0)
  150. // expect(b.slots.entries('conversation.empty.workspace')).toHaveLength(0)
  151. })
  152. })