apply.spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 { apply, inject } from '@deepseek-ai/dsh-client-ui-workspace/client'
  5. import type { WorkspaceBrowserInjected, WorkspacePickerInjected } from '@deepseek-ai/dsh-client-ui-workspace/client'
  6. import { WorkspaceBrowser } from '../src/client/WorkspaceBrowser.tsx'
  7. import { WorkspacePicker } from '../src/client/WorkspacePicker.tsx'
  8. async function bench() {
  9. const ctx = new Context()
  10. await ctx.plugin(SlotsService).await()
  11. const create = vi.fn(async (input: { name: string } | { path: string }) => ({
  12. workspaceId: 'ws-new' as never,
  13. path: 'name' in input ? `/projects/${input.name}` : input.path,
  14. title: 'new', sessionIds: [], createdAt: '0', updatedAt: '0',
  15. }))
  16. const pickDirectory = vi.fn(async () => '/tmp/picked')
  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. ctx.provide('workspaces', {
  23. create, pickDirectory, startSession, rename, insertSessionBefore,
  24. } as never)
  25. ctx.provide('sessions', { open, clear } as never)
  26. return { ctx, slots: ctx.get('slots') as SlotsService, create, pickDirectory, startSession, rename, insertSessionBefore, open, clear }
  27. }
  28. type HoleName = 'sidebar.workspaces' | 'conversation.hero.workspace' | 'conversation.empty.workspace'
  29. /** Declare any subset of the holes with a single root registration ('root' is a single slot). */
  30. function declare(slots: SlotsService, ...names: HoleName[]): () => void {
  31. const children = Object.fromEntries(names.map(name => [name, { kind: 'single', scope: 'root' }]))
  32. return slots.register({ name: 'root', children } as never, () => null)
  33. }
  34. describe('ui-workspace apply', () => {
  35. it('declares the services it drives', () => {
  36. expect(inject).toEqual(['slots', 'sessions', 'workspaces'])
  37. })
  38. it('registers browser and pickers for declarations arriving before or after apply', async () => {
  39. const before = await bench()
  40. declare(before.slots, 'sidebar.workspaces')
  41. await before.ctx.plugin({ inject: [...inject], apply }).await()
  42. expect(before.slots.entries('sidebar.workspaces')[0]!.component).toBe(WorkspaceBrowser)
  43. const after = await bench()
  44. await after.ctx.plugin({ inject: [...inject], apply }).await()
  45. declare(after.slots, 'conversation.hero.workspace', 'conversation.empty.workspace')
  46. await Promise.resolve()
  47. expect(after.slots.entries('conversation.hero.workspace')[0]!.component).toBe(WorkspacePicker)
  48. // expect(after.slots.entries('conversation.empty.workspace')[0]!.component).toBe(WorkspacePicker)
  49. })
  50. it('routes browser actions and picker creation to the services', async () => {
  51. const b = await bench()
  52. declare(b.slots, 'sidebar.workspaces', 'conversation.hero.workspace')
  53. await b.ctx.plugin({ inject: [...inject], apply }).await()
  54. const browser = (b.slots.entries('sidebar.workspaces')[0]!.inject as () => WorkspaceBrowserInjected)()
  55. // Both arms delegate to the runtime's shared New Session action.
  56. browser.startSession('ws' as never)
  57. expect(b.startSession).toHaveBeenCalledWith('ws')
  58. browser.startSession()
  59. expect(b.startSession).toHaveBeenLastCalledWith(undefined)
  60. browser.open('session' as never)
  61. expect(b.open).toHaveBeenCalledWith('session')
  62. await browser.renameWorkspace('ws' as never, 'renamed')
  63. expect(b.rename).toHaveBeenCalledWith('ws', 'renamed')
  64. await browser.insertSessionBefore('ws' as never, 's1' as never, 's2' as never)
  65. expect(b.insertSessionBefore).toHaveBeenCalledWith('ws', 's1', 's2')
  66. await browser.createWorkspace({ name: 'project' })
  67. expect(b.create).toHaveBeenCalledWith({ name: 'project' })
  68. await browser.pickDirectory()
  69. expect(b.pickDirectory).toHaveBeenCalledOnce()
  70. const picker = (b.slots.entries('conversation.hero.workspace')[0]!.inject as () => WorkspacePickerInjected)()
  71. await picker.createWorkspace({ path: '/tmp/project' })
  72. expect(b.create).toHaveBeenCalledWith({ path: '/tmp/project' })
  73. await picker.pickDirectory()
  74. expect(b.pickDirectory).toHaveBeenCalledTimes(2)
  75. })
  76. it('unregisters every entry on teardown', async () => {
  77. const b = await bench()
  78. declare(b.slots, 'sidebar.workspaces', 'conversation.hero.workspace', 'conversation.empty.workspace')
  79. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  80. await fiber.await()
  81. await fiber.dispose()
  82. expect(b.slots.entries('sidebar.workspaces')).toHaveLength(0)
  83. expect(b.slots.entries('conversation.hero.workspace')).toHaveLength(0)
  84. // expect(b.slots.entries('conversation.empty.workspace')).toHaveLength(0)
  85. })
  86. })