apply.spec.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // @vitest-environment jsdom
  2. // Client apply wiring under the terminal register form: ctx.layout provided,
  3. // ONE register() call declares the four child slots + seats the store factory
  4. // + wires the panel actions through the inject hook; teardown cascades
  5. // (service unprovided + declarations gone + registration cleared). Node half
  6. // and the invariant companion ride along — one-line surfaces the aggregate
  7. // coverage gate still requires exercised.
  8. import { Context } from 'cordis'
  9. import { describe, expect, it, vi } from 'vitest'
  10. import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
  11. import { apply, inject, LayoutService } from '@deepseek-ai/dsh-client-ui-layout/client'
  12. import { apply as nodeApply } from '@deepseek-ai/dsh-client-ui-layout'
  13. import * as invariant from '@deepseek-ai/dsh-client-ui-layout/invariant'
  14. async function bench() {
  15. const ctx = new Context()
  16. const slotsFiber = ctx.plugin(SlotsService)
  17. await slotsFiber.await()
  18. return { ctx, slots: ctx.get('slots') as SlotsService }
  19. }
  20. describe('ui-layout client apply', () => {
  21. it('declares its service dependencies', () => {
  22. expect(inject).toContain('slots')
  23. })
  24. it('provides ctx.layout and registers AppFrame into root with the four child declarations', async () => {
  25. const { ctx, slots } = await bench()
  26. const fiber = ctx.plugin({ inject: ['slots'], apply })
  27. await fiber.await()
  28. expect(ctx.get('layout')).toBeInstanceOf(LayoutService)
  29. // The one register() call occupied 'root'…
  30. expect(slots.entries('root')).toHaveLength(1)
  31. // …and declared the four children in the ledger.
  32. expect(slots.spec('sidebar')).toEqual({ kind: 'single', scope: 'root' })
  33. expect(slots.spec('conversation')).toEqual({ kind: 'single', scope: 'session' })
  34. expect(slots.spec('details')).toEqual({ kind: 'single', scope: 'session' })
  35. expect(slots.spec('conversation.empty')).toEqual({ kind: 'single', scope: 'root' })
  36. })
  37. it('teardown unwinds the service, the root registration, and the child declarations', async () => {
  38. const { ctx, slots } = await bench()
  39. const fiber = ctx.plugin({ inject: ['slots'], apply })
  40. await fiber.await()
  41. await fiber.dispose()
  42. expect(ctx.get('layout')).toBeUndefined()
  43. expect(slots.entries('root')).toHaveLength(0)
  44. expect(slots.spec('sidebar')).toBeUndefined()
  45. expect(slots.spec('conversation.empty')).toBeUndefined()
  46. // The built-in root declaration survives entry teardown (runtime-owned).
  47. expect(slots.spec('root')).toEqual({ kind: 'single', scope: 'root' })
  48. })
  49. })
  50. describe('node half + invariant companion', () => {
  51. it('node apply is an intentional no-op (loader-managed lifecycle only)', () => {
  52. nodeApply()
  53. expect(true).toBe(true) // reaching here without throw is the contract
  54. })
  55. it('invariant companion registers under the package name', async () => {
  56. const register = vi.fn().mockReturnValue(() => {})
  57. const ctx = { invariants: { register } } as never
  58. // The /invariant subpath types live in lib/types (build product); assert
  59. // the surface so the call stays typed where lint runs without a build.
  60. const dispose = await (invariant as { apply: (ctx: never) => Promise<() => void> }).apply(ctx)
  61. expect(register).toHaveBeenCalledWith('@deepseek-ai/dsh-client-ui-layout', expect.any(Function))
  62. // The installer is the declared no-op — calling it must not throw.
  63. expect(() => { (register.mock.calls[0]![1] as (c: never) => void)(undefined as never) }).not.toThrow()
  64. expect(dispose).toBeTypeOf('function')
  65. })
  66. })