apply.spec.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 exposes the aggregate
  7. // coverage gate still requires exercised.
  8. import { Context } from '@deepseek-ai/cordis'
  9. import { stubSettingsScope } from '@deepseek-ai/dsh-client-test-runtime'
  10. import { beforeEach, describe, expect, it, vi } from 'vitest'
  11. import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
  12. import { LocaleService } from '@deepseek-ai/dsh-client-locale/client'
  13. import { apply as themeApply, inject as themeInject, ThemeService } from '@deepseek-ai/dsh-client-ui-theme/client'
  14. import { apply, inject, LayoutService } from '@deepseek-ai/dsh-client-ui-layout/client'
  15. import { apply as nodeApply } from '@deepseek-ai/dsh-client-ui-layout'
  16. import * as invariant from '@deepseek-ai/dsh-client-ui-layout/invariant'
  17. beforeEach(() => {
  18. document.head.querySelectorAll('meta[name="theme-color"]').forEach((node) => { node.remove() })
  19. })
  20. async function bench() {
  21. const ctx = new Context()
  22. const slotsFiber = ctx.plugin(SlotsService)
  23. // Theme registers its Appearance settings row and requires the connection
  24. // seam for persistence; model this bench as a remote, memory-only browser.
  25. ctx.provide('locale', new LocaleService(ctx))
  26. ctx.provide('connection', { api: { settings: {} }, isLoopback: false } as never)
  27. // ui-theme's Appearance row binds a durable scope through these two.
  28. ctx.provide('remote', { $on: () => () => {} } as never)
  29. ctx.provide('settingsScope', { bind: () => stubSettingsScope().scope } as never)
  30. await ctx.plugin({ inject: themeInject, apply: themeApply }).await()
  31. await slotsFiber.await()
  32. return { ctx, slots: ctx.get('slots') as SlotsService }
  33. }
  34. describe('ui-layout client apply', () => {
  35. it('declares its service dependencies', () => {
  36. expect(inject).toEqual(['slots', 'theme'])
  37. })
  38. it('provides ctx.layout and registers AppFrame into root with the three child declarations', async () => {
  39. const { ctx, slots } = await bench()
  40. const fiber = ctx.plugin({ inject: [...inject], apply })
  41. await fiber.await()
  42. expect(ctx.get('layout')).toBeInstanceOf(LayoutService)
  43. // The one register() call occupied 'root'…
  44. expect(slots.entries('root')).toHaveLength(1)
  45. // …and declared the three children in the ledger.
  46. expect(slots.spec('sidebar')).toEqual({ kind: 'single', scope: 'root' })
  47. expect(slots.spec('conversation')).toEqual({ kind: 'single', scope: 'session-maybe' })
  48. expect(slots.spec('details')).toEqual({ kind: 'single', scope: 'session' })
  49. })
  50. it('injects no business face and attaches the layout actions', async () => {
  51. const { ctx, slots } = await bench()
  52. const fiber = ctx.plugin({ inject: [...inject], apply })
  53. await fiber.await()
  54. const actions = {
  55. setSidebar: vi.fn(), setDetails: vi.fn(), toggleSidebar: vi.fn(), openDetails: vi.fn(), closeDetails: vi.fn(),
  56. }
  57. const injected = (slots.entries('root')[0]!.inject as (actions: never) => object)(actions as never)
  58. expect(injected).toEqual({})
  59. const layout = ctx.get('layout') as LayoutService
  60. layout.toggleSidebar()
  61. expect(actions.toggleSidebar).toHaveBeenCalledOnce()
  62. })
  63. it('theme presenter applies the initial snapshot, follows theme/change, and unwinds on dispose', async () => {
  64. const { ctx } = await bench()
  65. const fiber = ctx.plugin({ inject: [...inject], apply })
  66. await fiber.await()
  67. // Initial getter application: jsdom has no matchMedia, system resolves light.
  68. expect(document.documentElement.style.colorScheme).toBe('light')
  69. expect(document.body.hasAttribute('data-ds-dark-theme')).toBe(false)
  70. const themeColorMeta = document.head.querySelector<HTMLMetaElement>('meta[name="theme-color"]')
  71. expect(themeColorMeta).not.toBeNull()
  72. const theme = ctx.get('theme') as ThemeService
  73. theme.setTheme('dark')
  74. expect(document.documentElement.style.colorScheme).toBe('dark')
  75. expect(document.body.hasAttribute('data-ds-dark-theme')).toBe(true)
  76. expect(document.head.querySelector('meta[name="theme-color"]')).toBe(themeColorMeta)
  77. await fiber.dispose()
  78. expect(document.documentElement.style.colorScheme).toBe('')
  79. expect(document.body.hasAttribute('data-ds-dark-theme')).toBe(false)
  80. expect(themeColorMeta?.isConnected).toBe(false)
  81. // Listener is off: further theme changes no longer reach the document.
  82. theme.setTheme('light')
  83. theme.setTheme('dark')
  84. expect(document.documentElement.style.colorScheme).toBe('')
  85. expect(document.body.hasAttribute('data-ds-dark-theme')).toBe(false)
  86. })
  87. it('teardown unwinds the service, the root registration, and the child declarations', async () => {
  88. const { ctx, slots } = await bench()
  89. const fiber = ctx.plugin({ inject: [...inject], apply })
  90. await fiber.await()
  91. await fiber.dispose()
  92. expect(ctx.get('layout')).toBeUndefined()
  93. expect(slots.entries('root')).toHaveLength(0)
  94. expect(slots.spec('sidebar')).toBeUndefined()
  95. // The built-in root declaration survives entry teardown (runtime-owned).
  96. expect(slots.spec('root')).toEqual({ kind: 'single', scope: 'root' })
  97. })
  98. })
  99. describe('node half + invariant companion', () => {
  100. it('node apply is an intentional no-op (loader-managed lifecycle only)', () => {
  101. nodeApply()
  102. expect(true).toBe(true) // reaching here without throw is the contract
  103. })
  104. it('invariant companion registers under the package name', async () => {
  105. const register = vi.fn().mockReturnValue(() => {})
  106. const ctx = { invariants: { register } } as never
  107. // The /invariant subpath types live in lib/types (build product); assert
  108. // the API so the call stays typed where lint runs without a build.
  109. const dispose = await (invariant as { apply: (ctx: never) => Promise<() => void> }).apply(ctx)
  110. expect(register).toHaveBeenCalledWith('@deepseek-ai/dsh-client-ui-layout', expect.any(Function))
  111. // The installer is the declared no-op — calling it must not throw.
  112. expect(() => { (register.mock.calls[0]![1] as (c: never) => void)(undefined as never) }).not.toThrow()
  113. expect(dispose).toBeTypeOf('function')
  114. })
  115. })