apply.spec.tsx 2.8 KB

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