apply.client.spec.ts 5.9 KB

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