shell.client.spec.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // @vitest-environment jsdom
  2. /**
  3. * Settings shell registration inside the assembled web client: the shell
  4. * occupies the `sidebar.settings` hole ui-sidebar declares, its ledger
  5. * projections read the product's real sections, its connection control is the
  6. * roster's Connection, and it survives a Loader rebuild of the declarer.
  7. */
  8. import { describe, expect, vi } from 'vitest'
  9. import type {} from '@deepseek-ai/dsh-client-ui-renderer/client'
  10. import { createClientTest, type TestClient, webApp } from '@deepseek-ai/dsh-client-test-runtime/src/assembly/index.ts'
  11. import { inject } from '../src/client/index.ts'
  12. import type { SettingsRootInjected } from '../src/client/shell-contract.ts'
  13. import { SettingsRoot } from '../src/client/SettingsRoot.tsx'
  14. const SELF = '@deepseek-ai/dsh-client-ui-settings-general'
  15. const SIDEBAR = '@deepseek-ai/dsh-client-ui-sidebar'
  16. const it = createClientTest({ roster: webApp })
  17. /** The whole roster's first boot pays the cold module transform of every plugin package. */
  18. const COLD_BOOT_TIMEOUT_MS = 60_000
  19. function injectedOf(c: TestClient): SettingsRootInjected {
  20. const entry = c.ctx.slots.entries('sidebar.settings')[0]!
  21. return (entry.inject as () => SettingsRootInjected)()
  22. }
  23. /** The shell's child declarations (chrome, actions, sections, and onboarding overlays). */
  24. const CHILD_SPECS = {
  25. 'settings.trigger': { kind: 'single', scope: 'root' },
  26. 'settings.header': { kind: 'single', scope: 'root' },
  27. 'settings.action': { kind: 'list', scope: 'root' },
  28. 'settings.close': { kind: 'single', scope: 'root' },
  29. 'settings.section': { kind: 'list', scope: 'root' },
  30. 'settings.onboarding': { kind: 'list', scope: 'root' },
  31. } as const
  32. const CHILD_NAMES = Object.keys(CHILD_SPECS) as Array<keyof typeof CHILD_SPECS>
  33. /**
  34. * Section ids the web-app roster registers, in nav order: this package, then
  35. * ui-settings-models, ui-settings-plugins, and ui-agent-preset. A plugin adding
  36. * a section changes this list.
  37. */
  38. const PRODUCT_SECTIONS: readonly string[] = ['general', 'models', 'plugins', 'agent-presets']
  39. /** Onboarding steps the web-app roster registers, in coordinator order; both come from ui-settings-models. */
  40. const PRODUCT_ONBOARDING: readonly { id: string; order: number }[] = [
  41. { id: 'welcome-notice', order: -100 },
  42. { id: 'deepseek-official', order: 0 },
  43. ]
  44. describe('ui-settings-general shell', () => {
  45. it('declares its services', () => {
  46. expect(inject).toEqual(['slots', 'locale', 'connection', 'remote', 'remote.settings', 'settingsScope'])
  47. })
  48. it('occupies sidebar.settings, declared by ui-sidebar, and declares every child slot', async ({ start }) => {
  49. const c = await start()
  50. expect(c.ctx.slots.entries('sidebar.settings').map(entry => entry.component)).toEqual([SettingsRoot])
  51. for (const name of CHILD_NAMES) expect(c.ctx.slots.spec(name)).toEqual(CHILD_SPECS[name])
  52. }, COLD_BOOT_TIMEOUT_MS)
  53. it('projects the section ledger: product sections in order, defaults for bare rows, stable snapshots', async ({ start }) => {
  54. const c = await start()
  55. const { sections } = injectedOf(c).hooks
  56. const product = sections.getSnapshot()
  57. expect(product.map(row => row.id)).toEqual(PRODUCT_SECTIONS)
  58. expect(product[0]).toEqual({ id: 'general', order: 0, label: expect.any(String) as string })
  59. c.ctx.slots.register({ name: 'settings.section', id: 'z', order: 1_000, label: 'Z' } as never, () => null)
  60. // No order and no label: both projection defaults apply, and order 0 sorts among the product rows.
  61. c.ctx.slots.register({ name: 'settings.section', id: 'a' } as never, () => null)
  62. const rows = sections.getSnapshot()
  63. expect(rows.at(-1)).toEqual({ id: 'z', order: 1_000, label: 'Z' })
  64. expect(rows.find(row => row.id === 'a')).toEqual({ id: 'a', order: 0, label: '' })
  65. expect(rows.map(row => row.order)).toEqual([...rows.map(row => row.order)].sort((x, y) => x - y))
  66. // Snapshot identity is stable until the ledger moves (uSES contract).
  67. expect(sections.getSnapshot()).toBe(rows)
  68. const listener = vi.fn()
  69. const off = sections.subscribe(listener)
  70. c.ctx.slots.register({ name: 'settings.section', id: 'b', order: 1, label: 'B' } as never, () => null)
  71. await Promise.resolve()
  72. expect(listener).toHaveBeenCalled()
  73. expect(sections.getSnapshot()).not.toBe(rows)
  74. off()
  75. })
  76. it('projects the roster Connection control without copying its state; reconnect opens a new $events generation', async ({ start }) => {
  77. const c = await start()
  78. const injected = injectedOf(c)
  79. expect(injected.hooks.connectionState).toBe(c.connection.state)
  80. expect(injected.hooks.connectionState.getSnapshot()).toBe('connected')
  81. injected.reconnect()
  82. await c.mock.streams.opened('$events', 2)
  83. await vi.waitFor(() => { expect(c.connection.state.getSnapshot()).toBe('connected') })
  84. })
  85. it('projects onboarding entries into stable coordinator order', async ({ start }) => {
  86. const c = await start()
  87. const { onboardingSteps } = injectedOf(c).hooks
  88. expect(onboardingSteps.getSnapshot()).toEqual(PRODUCT_ONBOARDING)
  89. c.ctx.slots.register({ name: 'settings.onboarding', id: 'credential', order: 0 } as never, () => null)
  90. c.ctx.slots.register({ name: 'settings.onboarding', id: 'welcome', order: -100 } as never, () => null)
  91. c.ctx.slots.register({ name: 'settings.onboarding', id: 'default-order' } as never, () => null)
  92. const steps = onboardingSteps.getSnapshot()
  93. expect(steps.filter(step => !PRODUCT_ONBOARDING.some(known => known.id === step.id))).toEqual([
  94. { id: 'welcome', order: -100 },
  95. { id: 'credential', order: 0 },
  96. { id: 'default-order', order: 0 },
  97. ])
  98. expect(onboardingSteps.getSnapshot()).toBe(steps)
  99. const listener = vi.fn()
  100. const off = onboardingSteps.subscribe(listener)
  101. c.ctx.slots.register({ name: 'settings.onboarding', id: 'later', order: 10 } as never, () => null)
  102. await Promise.resolve()
  103. expect(listener).toHaveBeenCalledOnce()
  104. off()
  105. })
  106. it('re-registers after the declarer reloads: the cascade removes the shell, the rebuilt declaration takes it back', async ({ start }) => {
  107. const c = await start()
  108. const before = c.ctx.slots.entries('sidebar.settings')[0]
  109. expect(before).toBeDefined()
  110. await c.reload(SIDEBAR)
  111. await c.flush()
  112. expect(c.ctx.slots.entries('sidebar.settings').map(entry => entry.component)).toEqual([SettingsRoot])
  113. expect(c.ctx.slots.entries('sidebar.settings')[0]).not.toBe(before)
  114. for (const name of CHILD_NAMES) expect(c.ctx.slots.spec(name)).toEqual(CHILD_SPECS[name])
  115. })
  116. it('unregisters the shell and collapses every child slot when its row unloads', async ({ start }) => {
  117. const c = await start()
  118. await c.unload(SELF)
  119. await c.flush()
  120. expect(c.ctx.slots.entries('sidebar.settings')).toHaveLength(0)
  121. for (const name of CHILD_NAMES) expect(c.ctx.slots.spec(name)).toBeUndefined()
  122. })
  123. })