apply.spec.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /** Sidebar shell slot registration and its plain runtime/layout callbacks. */
  2. import { Context } from 'cordis'
  3. import { describe, expect, it, vi } from 'vitest'
  4. import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
  5. import { apply, inject } from '@deepseek-ai/dsh-client-ui-sidebar/client'
  6. import type { SidebarRootInjected } from '@deepseek-ai/dsh-client-ui-sidebar/client'
  7. async function bench(declare = true) {
  8. const ctx = new Context()
  9. await ctx.plugin(SlotsService).await()
  10. const layout = { toggleSidebar: vi.fn() }
  11. const workspaces = { startSession: vi.fn() }
  12. const sessions = { open: vi.fn(), clear: vi.fn() }
  13. ctx.provide('layout', layout)
  14. ctx.provide('sessions', sessions as never)
  15. ctx.provide('workspaces', workspaces as never)
  16. const slots = ctx.get('slots') as SlotsService
  17. if (declare) {
  18. slots.register(
  19. { name: 'root', children: { 'sidebar': { kind: 'single', scope: 'root' } } } as never,
  20. () => null,
  21. )
  22. }
  23. return { ctx, slots, layout, workspaces, sessions }
  24. }
  25. describe('ui-sidebar apply', () => {
  26. it('declares only the services it uses', () => {
  27. expect(inject).toEqual(['slots', 'layout', 'sessions', 'workspaces'])
  28. })
  29. it('registers the shell and declares the browsing-region hole', async () => {
  30. const b = await bench()
  31. await b.ctx.plugin({ inject: [...inject], apply }).await()
  32. expect(b.slots.entries('sidebar')).toHaveLength(1)
  33. expect(b.slots.spec('sidebar.workspaces')).toEqual({ kind: 'single', scope: 'root' })
  34. const injected = (b.slots.entries('sidebar')[0]!.inject as () => SidebarRootInjected)()
  35. expect(Object.keys(injected)).toEqual(['startSession', 'toggleSidebar'])
  36. // Both arms delegate to the runtime's shared New Session action.
  37. injected.startSession('workspace' as never)
  38. expect(b.workspaces.startSession).toHaveBeenCalledWith('workspace')
  39. injected.startSession()
  40. expect(b.workspaces.startSession).toHaveBeenLastCalledWith(undefined)
  41. injected.toggleSidebar()
  42. expect(b.layout.toggleSidebar).toHaveBeenCalledOnce()
  43. })
  44. it('fails when no live owner declared the sidebar slot', async () => {
  45. const b = await bench(false)
  46. await expect(b.ctx.plugin({ inject: [...inject], apply })).rejects.toThrow(/not declared/)
  47. })
  48. it('removes the entry and child declaration on teardown', async () => {
  49. const b = await bench()
  50. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  51. await fiber.await()
  52. await fiber.dispose()
  53. expect(b.slots.entries('sidebar')).toHaveLength(0)
  54. expect(b.slots.spec('sidebar.workspaces')).toBeUndefined()
  55. })
  56. })