apply.spec.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /** Settings shell registration: declaration-aware deferral, the ledger projections, and HMR recovery. */
  2. import { Context } from 'cordis'
  3. import { describe, expect, it, vi } from 'vitest'
  4. import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
  5. import { apply, inject } from '@deepseek-ai/dsh-client-ui-settings/client'
  6. import type { SettingsRootInjected } from '@deepseek-ai/dsh-client-ui-settings/client'
  7. import { SettingsRoot } from '../src/client/SettingsRoot.tsx'
  8. async function bench() {
  9. const ctx = new Context()
  10. await ctx.plugin(SlotsService).await()
  11. return { ctx, slots: ctx.get('slots') as SlotsService }
  12. }
  13. function declare(slots: SlotsService): () => void {
  14. return slots.register(
  15. { name: 'root', children: { 'sidebar.settings': { kind: 'single', scope: 'root' } } } as never,
  16. () => null,
  17. )
  18. }
  19. function injectedOf(slots: SlotsService): SettingsRootInjected {
  20. const entry = slots.entries('sidebar.settings')[0]!
  21. return (entry.inject as () => SettingsRootInjected)()
  22. }
  23. /** The shell's five child declarations (chrome, sections, and onboarding overlays). */
  24. const CHILD_SPECS = {
  25. 'settings.trigger': { kind: 'single', scope: 'root' },
  26. 'settings.header': { kind: 'single', scope: 'root' },
  27. 'settings.close': { kind: 'single', scope: 'root' },
  28. 'settings.section': { kind: 'list', scope: 'root' },
  29. 'settings.onboarding': { kind: 'list', scope: 'root' },
  30. } as const
  31. describe('ui-settings apply', () => {
  32. it('declares only the slot registry (a pure composition face, no locale)', () => {
  33. expect(inject).toEqual(['slots'])
  34. })
  35. it('registers the shell and declares the five child slots, before or after the declaration', async () => {
  36. const before = await bench()
  37. declare(before.slots)
  38. await before.ctx.plugin({ inject: [...inject], apply }).await()
  39. expect(before.slots.entries('sidebar.settings')[0]!.component).toBe(SettingsRoot)
  40. for (const [name, spec] of Object.entries(CHILD_SPECS)) {
  41. expect(before.slots.spec(name as never)).toEqual(spec)
  42. }
  43. const after = await bench()
  44. await after.ctx.plugin({ inject: [...inject], apply }).await()
  45. expect(after.slots.entries('sidebar.settings')).toHaveLength(0)
  46. declare(after.slots)
  47. await Promise.resolve()
  48. expect(after.slots.entries('sidebar.settings')[0]!.component).toBe(SettingsRoot)
  49. // The self-inflicted ledger notifications hit the duplicate guard.
  50. expect(after.slots.entries('sidebar.settings')).toHaveLength(1)
  51. })
  52. it('projects the section ledger into ordered nav rows with option defaults', async () => {
  53. const b = await bench()
  54. declare(b.slots)
  55. await b.ctx.plugin({ inject: [...inject], apply }).await()
  56. const { sections } = injectedOf(b.slots).hooks
  57. // The shell ships no sections of its own — registrants fill the ledger.
  58. expect(sections.getSnapshot()).toEqual([])
  59. b.slots.register({ name: 'settings.section', id: 'z', order: 20, label: 'Z' } as never, () => null)
  60. // No order and no label: both projection defaults apply.
  61. b.slots.register({ name: 'settings.section', id: 'a' } as never, () => null)
  62. const rows = sections.getSnapshot()
  63. expect(rows).toEqual([
  64. { id: 'a', order: 0, label: '' },
  65. { id: 'z', order: 20, label: 'Z' },
  66. ])
  67. // Snapshot identity is stable until the ledger moves (uSES contract).
  68. expect(sections.getSnapshot()).toBe(rows)
  69. const listener = vi.fn()
  70. const off = sections.subscribe(listener)
  71. b.slots.register({ name: 'settings.section', id: 'b', order: 1, label: 'B' } as never, () => null)
  72. await Promise.resolve()
  73. expect(listener).toHaveBeenCalled()
  74. expect(sections.getSnapshot()).not.toBe(rows)
  75. off()
  76. })
  77. it('re-registers after an HMR collapse re-declares the slot (stale disposer must not block)', async () => {
  78. const b = await bench()
  79. const redeclare = declare(b.slots)
  80. await b.ctx.plugin({ inject: [...inject], apply }).await()
  81. expect(b.slots.entries('sidebar.settings')).toHaveLength(1)
  82. // Declarer unload: the cascade removes our entry and every child
  83. // declaration while our local disposer variable goes stale.
  84. redeclare()
  85. expect(b.slots.entries('sidebar.settings')).toHaveLength(0)
  86. expect(b.slots.spec('settings.trigger')).toBeUndefined()
  87. declare(b.slots)
  88. await Promise.resolve()
  89. expect(b.slots.entries('sidebar.settings')[0]!.component).toBe(SettingsRoot)
  90. for (const [name, spec] of Object.entries(CHILD_SPECS)) {
  91. expect(b.slots.spec(name as never)).toEqual(spec)
  92. }
  93. })
  94. it('unregisters the shell and collapses all five child slots on teardown', async () => {
  95. const b = await bench()
  96. declare(b.slots)
  97. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  98. await fiber.await()
  99. await fiber.dispose()
  100. expect(b.slots.entries('sidebar.settings')).toHaveLength(0)
  101. for (const name of Object.keys(CHILD_SPECS)) {
  102. expect(b.slots.spec(name as never)).toBeUndefined()
  103. }
  104. })
  105. })