apply.spec.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 { LocaleService } from '@deepseek-ai/dsh-client-locale/client'
  12. import { apply as themeApply, inject as themeInject, ThemeService } from '@deepseek-ai/dsh-client-ui-theme/client'
  13. import { apply, inject, LayoutService } from '@deepseek-ai/dsh-client-ui-layout/client'
  14. import { apply as nodeApply } from '@deepseek-ai/dsh-client-ui-layout'
  15. import * as invariant from '@deepseek-ai/dsh-client-ui-layout/invariant'
  16. async function bench() {
  17. const ctx = new Context()
  18. const slotsFiber = ctx.plugin(SlotsService)
  19. // Theme now injects ['slots', 'locale'] (it registers its Appearance
  20. // settings row); seat a real locale service so the theme fiber activates.
  21. ctx.provide('locale', new LocaleService(ctx))
  22. await ctx.plugin({ inject: themeInject, apply: themeApply }).await()
  23. await slotsFiber.await()
  24. return { ctx, slots: ctx.get('slots') as SlotsService }
  25. }
  26. describe('ui-layout client apply', () => {
  27. it('declares its service dependencies', () => {
  28. expect(inject).toEqual(['slots', 'theme'])
  29. })
  30. it('provides ctx.layout and registers AppFrame into root with the four child declarations', async () => {
  31. const { ctx, slots } = await bench()
  32. const fiber = ctx.plugin({ inject: [...inject], apply })
  33. await fiber.await()
  34. expect(ctx.get('layout')).toBeInstanceOf(LayoutService)
  35. // The one register() call occupied 'root'…
  36. expect(slots.entries('root')).toHaveLength(1)
  37. // …and declared the four children in the ledger.
  38. expect(slots.spec('sidebar')).toEqual({ kind: 'single', scope: 'root' })
  39. expect(slots.spec('conversation')).toEqual({ kind: 'single', scope: 'session' })
  40. expect(slots.spec('details')).toEqual({ kind: 'single', scope: 'session' })
  41. expect(slots.spec('conversation.empty')).toEqual({ kind: 'single', scope: 'root' })
  42. })
  43. it('injects no business face and attaches the layout actions', async () => {
  44. const { ctx, slots } = await bench()
  45. const fiber = ctx.plugin({ inject: [...inject], apply })
  46. await fiber.await()
  47. const actions = {
  48. setSidebar: vi.fn(), setDetails: vi.fn(), toggleSidebar: vi.fn(), openDetails: vi.fn(), closeDetails: vi.fn(),
  49. }
  50. const injected = (slots.entries('root')[0]!.inject as (actions: never) => object)(actions as never)
  51. expect(injected).toEqual({})
  52. const layout = ctx.get('layout') as LayoutService
  53. layout.toggleSidebar()
  54. expect(actions.toggleSidebar).toHaveBeenCalledOnce()
  55. })
  56. it('theme presenter applies the initial snapshot, follows theme/change, and unwinds on dispose', async () => {
  57. const { ctx } = await bench()
  58. const fiber = ctx.plugin({ inject: [...inject], apply })
  59. await fiber.await()
  60. // Initial getter application: jsdom has no matchMedia, system resolves light.
  61. expect(document.body.hasAttribute('data-ds-dark-theme')).toBe(false)
  62. const theme = ctx.get('theme') as ThemeService
  63. theme.setTheme('dark')
  64. expect(document.body.hasAttribute('data-ds-dark-theme')).toBe(true)
  65. await fiber.dispose()
  66. expect(document.body.hasAttribute('data-ds-dark-theme')).toBe(false)
  67. // Listener is off: further theme changes no longer reach the body.
  68. theme.setTheme('light')
  69. theme.setTheme('dark')
  70. expect(document.body.hasAttribute('data-ds-dark-theme')).toBe(false)
  71. })
  72. it('teardown unwinds the service, the root registration, and the child declarations', async () => {
  73. const { ctx, slots } = await bench()
  74. const fiber = ctx.plugin({ inject: [...inject], apply })
  75. await fiber.await()
  76. await fiber.dispose()
  77. expect(ctx.get('layout')).toBeUndefined()
  78. expect(slots.entries('root')).toHaveLength(0)
  79. expect(slots.spec('sidebar')).toBeUndefined()
  80. expect(slots.spec('conversation.empty')).toBeUndefined()
  81. // The built-in root declaration survives entry teardown (runtime-owned).
  82. expect(slots.spec('root')).toEqual({ kind: 'single', scope: 'root' })
  83. })
  84. })
  85. describe('node half + invariant companion', () => {
  86. it('node apply is an intentional no-op (loader-managed lifecycle only)', () => {
  87. nodeApply()
  88. expect(true).toBe(true) // reaching here without throw is the contract
  89. })
  90. it('invariant companion registers under the package name', async () => {
  91. const register = vi.fn().mockReturnValue(() => {})
  92. const ctx = { invariants: { register } } as never
  93. // The /invariant subpath types live in lib/types (build product); assert
  94. // the surface so the call stays typed where lint runs without a build.
  95. const dispose = await (invariant as { apply: (ctx: never) => Promise<() => void> }).apply(ctx)
  96. expect(register).toHaveBeenCalledWith('@deepseek-ai/dsh-client-ui-layout', expect.any(Function))
  97. // The installer is the declared no-op — calling it must not throw.
  98. expect(() => { (register.mock.calls[0]![1] as (c: never) => void)(undefined as never) }).not.toThrow()
  99. expect(dispose).toBeTypeOf('function')
  100. })
  101. })