apply.spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 four child declarations (chrome seats + the section list). */
  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. } as const
  30. describe('ui-settings apply', () => {
  31. it('declares only the slot registry (a pure composition face, no locale)', () => {
  32. expect(inject).toEqual(['slots'])
  33. })
  34. it('registers the shell and declares the four child slots, before or after the declaration', async () => {
  35. const before = await bench()
  36. declare(before.slots)
  37. await before.ctx.plugin({ inject: [...inject], apply }).await()
  38. expect(before.slots.entries('sidebar.settings')[0]!.component).toBe(SettingsRoot)
  39. for (const [name, spec] of Object.entries(CHILD_SPECS)) {
  40. expect(before.slots.spec(name as never)).toEqual(spec)
  41. }
  42. const after = await bench()
  43. await after.ctx.plugin({ inject: [...inject], apply }).await()
  44. expect(after.slots.entries('sidebar.settings')).toHaveLength(0)
  45. declare(after.slots)
  46. await Promise.resolve()
  47. expect(after.slots.entries('sidebar.settings')[0]!.component).toBe(SettingsRoot)
  48. // The self-inflicted ledger notifications hit the duplicate guard.
  49. expect(after.slots.entries('sidebar.settings')).toHaveLength(1)
  50. })
  51. it('projects the section ledger into ordered nav rows with option defaults', async () => {
  52. const b = await bench()
  53. declare(b.slots)
  54. await b.ctx.plugin({ inject: [...inject], apply }).await()
  55. const injected = injectedOf(b.slots)
  56. // The shell ships no sections of its own — registrants fill the ledger.
  57. expect(injected.sections()).toEqual([])
  58. b.slots.register({ name: 'settings.section', id: 'z', order: 20, label: 'Z' } as never, () => null)
  59. // No order and no label: both projection defaults apply.
  60. b.slots.register({ name: 'settings.section', id: 'a' } as never, () => null)
  61. expect(injected.sections()).toEqual([
  62. { id: 'a', order: 0, label: '' },
  63. { id: 'z', order: 20, label: 'Z' },
  64. ])
  65. expect(injected.sectionsVersion()).toBe(b.slots.getVersion('settings.section'))
  66. const listener = vi.fn()
  67. const off = injected.subscribeSections(listener)
  68. b.slots.register({ name: 'settings.section', id: 'b', order: 1, label: 'B' } as never, () => null)
  69. await Promise.resolve()
  70. expect(listener).toHaveBeenCalled()
  71. off()
  72. })
  73. it('re-registers after an HMR collapse re-declares the slot (stale disposer must not block)', async () => {
  74. const b = await bench()
  75. const redeclare = declare(b.slots)
  76. await b.ctx.plugin({ inject: [...inject], apply }).await()
  77. expect(b.slots.entries('sidebar.settings')).toHaveLength(1)
  78. // Declarer unload: the cascade removes our entry and every child
  79. // declaration while our local disposer variable goes stale.
  80. redeclare()
  81. expect(b.slots.entries('sidebar.settings')).toHaveLength(0)
  82. expect(b.slots.spec('settings.trigger')).toBeUndefined()
  83. declare(b.slots)
  84. await Promise.resolve()
  85. expect(b.slots.entries('sidebar.settings')[0]!.component).toBe(SettingsRoot)
  86. for (const [name, spec] of Object.entries(CHILD_SPECS)) {
  87. expect(b.slots.spec(name as never)).toEqual(spec)
  88. }
  89. })
  90. it('unregisters the shell and collapses all four child slots on teardown', async () => {
  91. const b = await bench()
  92. declare(b.slots)
  93. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  94. await fiber.await()
  95. await fiber.dispose()
  96. expect(b.slots.entries('sidebar.settings')).toHaveLength(0)
  97. for (const name of Object.keys(CHILD_SPECS)) {
  98. expect(b.slots.spec(name as never)).toBeUndefined()
  99. }
  100. })
  101. })