apply.spec.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // @vitest-environment jsdom
  2. // Client apply wiring under the terminal register form: ctx.layout provided,
  3. // ONE register() call declares the three 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 three 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 three children in the ledger.
  38. expect(slots.spec('sidebar')).toEqual({ kind: 'single', scope: 'root' })
  39. expect(slots.spec('conversation')).toEqual({ kind: 'single', scope: 'session-maybe' })
  40. expect(slots.spec('details')).toEqual({ kind: 'single', scope: 'session' })
  41. })
  42. it('injects no business face and attaches the layout actions', async () => {
  43. const { ctx, slots } = await bench()
  44. const fiber = ctx.plugin({ inject: [...inject], apply })
  45. await fiber.await()
  46. const actions = {
  47. setSidebar: vi.fn(), setDetails: vi.fn(), toggleSidebar: vi.fn(), openDetails: vi.fn(), closeDetails: vi.fn(),
  48. }
  49. const injected = (slots.entries('root')[0]!.inject as (actions: never) => object)(actions as never)
  50. expect(injected).toEqual({})
  51. const layout = ctx.get('layout') as LayoutService
  52. layout.toggleSidebar()
  53. expect(actions.toggleSidebar).toHaveBeenCalledOnce()
  54. })
  55. it('theme presenter applies the initial snapshot, follows theme/change, and unwinds on dispose', async () => {
  56. const { ctx } = await bench()
  57. const fiber = ctx.plugin({ inject: [...inject], apply })
  58. await fiber.await()
  59. // Initial getter application: jsdom has no matchMedia, system resolves light.
  60. expect(document.documentElement.style.colorScheme).toBe('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.documentElement.style.colorScheme).toBe('dark')
  65. expect(document.body.hasAttribute('data-ds-dark-theme')).toBe(true)
  66. await fiber.dispose()
  67. expect(document.documentElement.style.colorScheme).toBe('')
  68. expect(document.body.hasAttribute('data-ds-dark-theme')).toBe(false)
  69. // Listener is off: further theme changes no longer reach the document.
  70. theme.setTheme('light')
  71. theme.setTheme('dark')
  72. expect(document.documentElement.style.colorScheme).toBe('')
  73. expect(document.body.hasAttribute('data-ds-dark-theme')).toBe(false)
  74. })
  75. it('teardown unwinds the service, the root registration, and the child declarations', async () => {
  76. const { ctx, slots } = await bench()
  77. const fiber = ctx.plugin({ inject: [...inject], apply })
  78. await fiber.await()
  79. await fiber.dispose()
  80. expect(ctx.get('layout')).toBeUndefined()
  81. expect(slots.entries('root')).toHaveLength(0)
  82. expect(slots.spec('sidebar')).toBeUndefined()
  83. // The built-in root declaration survives entry teardown (runtime-owned).
  84. expect(slots.spec('root')).toEqual({ kind: 'single', scope: 'root' })
  85. })
  86. })
  87. describe('node half + invariant companion', () => {
  88. it('node apply is an intentional no-op (loader-managed lifecycle only)', () => {
  89. nodeApply()
  90. expect(true).toBe(true) // reaching here without throw is the contract
  91. })
  92. it('invariant companion registers under the package name', async () => {
  93. const register = vi.fn().mockReturnValue(() => {})
  94. const ctx = { invariants: { register } } as never
  95. // The /invariant subpath types live in lib/types (build product); assert
  96. // the surface so the call stays typed where lint runs without a build.
  97. const dispose = await (invariant as { apply: (ctx: never) => Promise<() => void> }).apply(ctx)
  98. expect(register).toHaveBeenCalledWith('@deepseek-ai/dsh-client-ui-layout', expect.any(Function))
  99. // The installer is the declared no-op — calling it must not throw.
  100. expect(() => { (register.mock.calls[0]![1] as (c: never) => void)(undefined as never) }).not.toThrow()
  101. expect(dispose).toBeTypeOf('function')
  102. })
  103. })