shell.spec.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /** Settings shell registration: slot declaration injection, the ledger projections, and HMR recovery. */
  2. import { Context } from '@deepseek-ai/cordis'
  3. import { describe, expect, it, vi } from 'vitest'
  4. import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
  5. import { apply, inject } from '../src/client/index.ts'
  6. import type { SettingsRootInjected } from '../src/client/shell-contract.ts'
  7. import { SettingsRoot } from '../src/client/SettingsRoot.tsx'
  8. async function bench() {
  9. const ctx = new Context()
  10. await ctx.plugin(SlotsService).await()
  11. // Copy machinery the shell only reads a revision from; the real locale
  12. // plugin would drag its own settings-row dependencies into this bench.
  13. ctx.provide('locale', {
  14. register: () => () => {},
  15. bind: () => (key: string) => key,
  16. getSnapshot: () => ({ active: 'zh', locales: [], revision: 0 }),
  17. subscribe: () => () => {},
  18. } as never)
  19. ctx.provide('connection', {
  20. api: { settings: { describe: async () => ({ result: { ok: false } }) } },
  21. isLoopback: false,
  22. } as never)
  23. ctx.provide('remote', { $on: () => () => {} } as never)
  24. return { ctx, slots: ctx.get('slots') as SlotsService }
  25. }
  26. function declare(slots: SlotsService): () => void {
  27. return slots.register(
  28. { name: 'root', children: { 'sidebar.settings': { kind: 'single', scope: 'root' } } } as never,
  29. () => null,
  30. )
  31. }
  32. function injectedOf(slots: SlotsService): SettingsRootInjected {
  33. const entry = slots.entries('sidebar.settings')[0]!
  34. return (entry.inject as () => SettingsRootInjected)()
  35. }
  36. /** The shell's child declarations (chrome, actions, sections, and onboarding overlays). */
  37. const CHILD_SPECS = {
  38. 'settings.trigger': { kind: 'single', scope: 'root' },
  39. 'settings.header': { kind: 'single', scope: 'root' },
  40. 'settings.action': { kind: 'list', scope: 'root' },
  41. 'settings.close': { kind: 'single', scope: 'root' },
  42. 'settings.section': { kind: 'list', scope: 'root' },
  43. 'settings.onboarding': { kind: 'list', scope: 'root' },
  44. } as const
  45. describe('ui-settings apply', () => {
  46. it('declares only the slot registry (a pure composition face, no locale)', () => {
  47. expect(inject).toEqual(['slots', 'locale', 'connection', 'remote'])
  48. })
  49. it('registers the shell and declares every child slot, before or after the declaration', async () => {
  50. const before = await bench()
  51. declare(before.slots)
  52. await before.ctx.plugin({ inject: [...inject], apply }).await()
  53. expect(before.slots.entries('sidebar.settings')[0]!.component).toBe(SettingsRoot)
  54. for (const name of Object.keys(CHILD_SPECS) as Array<keyof typeof CHILD_SPECS>) {
  55. expect(before.slots.spec(name)).toEqual(CHILD_SPECS[name])
  56. }
  57. const after = await bench()
  58. await after.ctx.plugin({ inject: [...inject], apply }).await()
  59. expect(after.slots.entries('sidebar.settings')).toHaveLength(0)
  60. declare(after.slots)
  61. await Promise.resolve()
  62. expect(after.slots.entries('sidebar.settings')[0]!.component).toBe(SettingsRoot)
  63. // The self-inflicted ledger notifications hit the duplicate guard.
  64. expect(after.slots.entries('sidebar.settings')).toHaveLength(1)
  65. })
  66. it('projects the section ledger into ordered nav rows with option defaults', async () => {
  67. const b = await bench()
  68. declare(b.slots)
  69. await b.ctx.plugin({ inject: [...inject], apply }).await()
  70. const { sections } = injectedOf(b.slots).hooks
  71. // This package registers the General section itself; every other section
  72. // arrives from a feature registrant.
  73. const GENERAL = { id: 'general', order: 0, label: 'general.nav' }
  74. expect(sections.getSnapshot()).toEqual([GENERAL])
  75. b.slots.register({ name: 'settings.section', id: 'z', order: 20, label: 'Z' } as never, () => null)
  76. // No order and no label: both projection defaults apply.
  77. b.slots.register({ name: 'settings.section', id: 'a' } as never, () => null)
  78. const rows = sections.getSnapshot()
  79. expect(rows).toEqual([
  80. GENERAL,
  81. { id: 'a', order: 0, label: '' },
  82. { id: 'z', order: 20, label: 'Z' },
  83. ])
  84. // Snapshot identity is stable until the ledger moves (uSES contract).
  85. expect(sections.getSnapshot()).toBe(rows)
  86. const listener = vi.fn()
  87. const off = sections.subscribe(listener)
  88. b.slots.register({ name: 'settings.section', id: 'b', order: 1, label: 'B' } as never, () => null)
  89. await Promise.resolve()
  90. expect(listener).toHaveBeenCalled()
  91. expect(sections.getSnapshot()).not.toBe(rows)
  92. off()
  93. })
  94. it('projects onboarding entries into stable coordinator order', async () => {
  95. const b = await bench()
  96. declare(b.slots)
  97. await b.ctx.plugin({ inject: [...inject], apply }).await()
  98. const { onboardingSteps } = injectedOf(b.slots).hooks
  99. b.slots.register({ name: 'settings.onboarding', id: 'credential', order: 0 } as never, () => null)
  100. b.slots.register({ name: 'settings.onboarding', id: 'welcome', order: -100 } as never, () => null)
  101. b.slots.register({ name: 'settings.onboarding', id: 'default-order' } as never, () => null)
  102. const steps = onboardingSteps.getSnapshot()
  103. expect(steps).toEqual([
  104. // This package's own onboarding page, registered by the same apply.
  105. { id: 'welcome-notice', order: -100 },
  106. { id: 'welcome', order: -100 },
  107. { id: 'credential', order: 0 },
  108. { id: 'default-order', order: 0 },
  109. ])
  110. expect(onboardingSteps.getSnapshot()).toBe(steps)
  111. const listener = vi.fn()
  112. const off = onboardingSteps.subscribe(listener)
  113. b.slots.register({ name: 'settings.onboarding', id: 'later', order: 10 } as never, () => null)
  114. await Promise.resolve()
  115. expect(listener).toHaveBeenCalledOnce()
  116. off()
  117. })
  118. it('re-registers after an HMR collapse re-declares the slot (stale disposer must not block)', async () => {
  119. const b = await bench()
  120. const redeclare = declare(b.slots)
  121. await b.ctx.plugin({ inject: [...inject], apply }).await()
  122. expect(b.slots.entries('sidebar.settings')).toHaveLength(1)
  123. // Declarer unload: the cascade removes our entry and every child
  124. // declaration while our local disposer variable goes stale.
  125. redeclare()
  126. expect(b.slots.entries('sidebar.settings')).toHaveLength(0)
  127. expect(b.slots.spec('settings.trigger')).toBeUndefined()
  128. declare(b.slots)
  129. await Promise.resolve()
  130. expect(b.slots.entries('sidebar.settings')[0]!.component).toBe(SettingsRoot)
  131. for (const name of Object.keys(CHILD_SPECS) as Array<keyof typeof CHILD_SPECS>) {
  132. expect(b.slots.spec(name)).toEqual(CHILD_SPECS[name])
  133. }
  134. })
  135. it('unregisters the shell and collapses every child slot on teardown', async () => {
  136. const b = await bench()
  137. declare(b.slots)
  138. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  139. await fiber.await()
  140. await fiber.dispose()
  141. expect(b.slots.entries('sidebar.settings')).toHaveLength(0)
  142. for (const name of Object.keys(CHILD_SPECS) as Array<keyof typeof CHILD_SPECS>) {
  143. expect(b.slots.spec(name)).toBeUndefined()
  144. }
  145. })
  146. })