apply.spec.ts 8.1 KB

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