apply.spec.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /** Ownerless-copy registrations: the four seats, the dictionaries, thunked labels, and HMR recovery. */
  2. import { Context } from 'cordis'
  3. import { describe, expect, it, vi } from 'vitest'
  4. import { resolveSlotLabel } from '@deepseek-ai/dsh-client-ui-slots'
  5. import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
  6. import { LocaleService } from '@deepseek-ai/dsh-client-locale/client'
  7. import { apply, inject } from '@deepseek-ai/dsh-client-ui-settings-general/client'
  8. import { CloseLabel, HeaderContent, TriggerContent } from '../src/client/chrome.tsx'
  9. import { GeneralSection } from '../src/client/GeneralSection.tsx'
  10. /** The four seats this plugin fills (slot name → expected component). */
  11. const SEATS = [
  12. ['settings.trigger', TriggerContent],
  13. ['settings.header', HeaderContent],
  14. ['settings.close', CloseLabel],
  15. ['settings.section', GeneralSection],
  16. ] as const
  17. async function bench() {
  18. const ctx = new Context()
  19. await ctx.plugin(SlotsService).await()
  20. const locale = new LocaleService(ctx)
  21. ctx.provide('locale', locale)
  22. return { ctx, slots: ctx.get('slots') as SlotsService, locale }
  23. }
  24. /** Declare the shell's four child slots the way ui-settings' entry does. */
  25. function declare(slots: SlotsService): () => void {
  26. return slots.register(
  27. {
  28. name: 'root',
  29. children: {
  30. 'settings.trigger': { kind: 'single', scope: 'root' },
  31. 'settings.header': { kind: 'single', scope: 'root' },
  32. 'settings.close': { kind: 'single', scope: 'root' },
  33. 'settings.section': { kind: 'list', scope: 'root' },
  34. },
  35. } as never,
  36. () => null,
  37. )
  38. }
  39. function generalEntry(slots: SlotsService) {
  40. return slots.entries('settings.section').find(e => e.component === GeneralSection)
  41. }
  42. describe('ui-settings-general apply', () => {
  43. it('declares the services it uses', () => {
  44. expect(inject).toEqual(['slots', 'locale'])
  45. })
  46. it('fills all four seats for declarations before or after apply', async () => {
  47. const before = await bench()
  48. declare(before.slots)
  49. await before.ctx.plugin({ inject: [...inject], apply }).await()
  50. for (const [name, component] of SEATS) {
  51. expect(before.slots.entries(name)[0]!.component).toBe(component)
  52. }
  53. const entry = generalEntry(before.slots)!
  54. expect(entry.options).toMatchObject({ id: 'general', order: 0 })
  55. // The nav label is a locale-following thunk; owners resolve at read time.
  56. expect(resolveSlotLabel(entry.options.label)).toBe('通用设置')
  57. expect(before.slots.spec('settings.general.item')).toEqual({ kind: 'list', scope: 'root' })
  58. expect(before.slots.entries('settings.general.item')).toEqual([])
  59. // Copy rides the standard locale seat: every seat declares the namespace.
  60. for (const [name] of SEATS) {
  61. expect(before.slots.entries(name)[0]!.locale).toBe('settings')
  62. }
  63. const after = await bench()
  64. await after.ctx.plugin({ inject: [...inject], apply }).await()
  65. for (const [name] of SEATS) expect(after.slots.entries(name)).toHaveLength(0)
  66. declare(after.slots)
  67. await Promise.resolve()
  68. for (const [name, component] of SEATS) {
  69. expect(after.slots.entries(name)[0]!.component).toBe(component)
  70. // The self-inflicted ledger notifications hit the duplicate guard.
  71. expect(after.slots.entries(name)).toHaveLength(1)
  72. }
  73. await vi.waitFor(() => {
  74. expect(after.slots.spec('settings.general.item')).toEqual({ kind: 'list', scope: 'root' })
  75. })
  76. })
  77. it('registers the zh/en settings dictionaries and frees the seats on teardown', async () => {
  78. const b = await bench()
  79. declare(b.slots)
  80. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  81. await fiber.await()
  82. expect(b.locale.bind('settings')('title')).toBe('设置')
  83. b.locale.setLocale('en')
  84. expect(b.locale.bind('settings')('close')).toBe('Close')
  85. b.locale.setLocale('zh')
  86. await fiber.dispose()
  87. // The (ns, locale) seats are free again — the dictionary disposer ran.
  88. expect(() => b.locale.register('settings', 'zh', {})).not.toThrow()
  89. expect(() => b.locale.register('settings', 'en', {})).not.toThrow()
  90. })
  91. it('the nav label thunk follows the active locale without re-registration', async () => {
  92. const b = await bench()
  93. declare(b.slots)
  94. await b.ctx.plugin({ inject: [...inject], apply }).await()
  95. const zhVersions = SEATS.map(([name]) => b.slots.getVersion(name))
  96. b.locale.setLocale('en')
  97. // No ledger churn: freshness rides the thunk (and the renderer's locale
  98. // subscription), not re-registration.
  99. SEATS.forEach(([name], i) => {
  100. expect(b.slots.getVersion(name)).toBe(zhVersions[i]!)
  101. expect(b.slots.entries(name)).toHaveLength(1)
  102. })
  103. expect(resolveSlotLabel(generalEntry(b.slots)!.options.label)).toBe('General')
  104. b.locale.setLocale('zh')
  105. expect(resolveSlotLabel(generalEntry(b.slots)!.options.label)).toBe('通用设置')
  106. })
  107. it('re-registers after an HMR collapse of the declaring chain (stale disposers must not block)', async () => {
  108. const b = await bench()
  109. const redeclare = declare(b.slots)
  110. await b.ctx.plugin({ inject: [...inject], apply }).await()
  111. // Declarer unload: the cascade removes every seat entry and the item
  112. // declaration while our local disposers go stale.
  113. redeclare()
  114. for (const [name] of SEATS) expect(b.slots.entries(name)).toHaveLength(0)
  115. expect(b.slots.spec('settings.general.item')).toBeUndefined()
  116. declare(b.slots)
  117. await Promise.resolve()
  118. for (const [name, component] of SEATS) {
  119. expect(b.slots.entries(name)[0]!.component).toBe(component)
  120. }
  121. expect(b.slots.entries('settings.general.item')).toEqual([])
  122. expect(b.slots.spec('settings.general.item')).toEqual({ kind: 'list', scope: 'root' })
  123. // The recovered registrations still ride the locale path.
  124. b.locale.setLocale('en')
  125. expect(resolveSlotLabel(generalEntry(b.slots)!.options.label)).toBe('General')
  126. b.locale.setLocale('zh')
  127. })
  128. it('removes every seat and the item declaration on teardown', async () => {
  129. const b = await bench()
  130. declare(b.slots)
  131. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  132. await fiber.await()
  133. expect(b.slots.spec('settings.general.item')).toBeDefined()
  134. await fiber.dispose()
  135. for (const [name] of SEATS) expect(b.slots.entries(name)).toHaveLength(0)
  136. expect(b.slots.spec('settings.general.item')).toBeUndefined()
  137. })
  138. })