apply.spec.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /** locale apply wiring: service + dictionaries provision, declaration-aware
  2. * Language row registration, snapshot projection into the row store, and
  3. * recovery after an HMR collapse of the declaring entry. */
  4. import { Context } from 'cordis'
  5. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  6. import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
  7. import { apply, inject, SETTINGS_NS } from '@deepseek-ai/dsh-client-locale/client'
  8. import type { LanguageRowInjected, LocaleService } from '@deepseek-ai/dsh-client-locale/client'
  9. import { LanguageRow } from '../src/client/LanguageRow.tsx'
  10. import type { createLanguageRowStore } from '../src/client/settings-store.ts'
  11. const SLOT = 'settings.general.item'
  12. async function bench() {
  13. const ctx = new Context()
  14. await ctx.plugin(SlotsService).await()
  15. return { ctx, slots: ctx.get('slots') as SlotsService }
  16. }
  17. /** Stand in for the settings shell: declare the General item slot from root. */
  18. function declareItems(slots: SlotsService): () => void {
  19. return slots.register(
  20. { name: 'root', children: { [SLOT]: { kind: 'list', scope: 'root' } } } as never,
  21. () => null,
  22. )
  23. }
  24. /** Mirror the framework's inject choreography: bake a real instance from the
  25. * declared handle and hand its actions to the entry's inject factory. */
  26. function faceOf(slots: SlotsService) {
  27. const entry = slots.entries(SLOT).find(e => e.component === LanguageRow)!
  28. const handle = entry.store as ReturnType<typeof createLanguageRowStore>
  29. const instance = handle.create()
  30. const face = (entry.inject as unknown as (a: typeof instance.actions) => LanguageRowInjected)(instance.actions)
  31. return { entry, instance, face }
  32. }
  33. describe('locale apply', () => {
  34. // A fresh service opens in the browser's language, so these wiring specs
  35. // pin one to keep their zh baseline independent of the test environment.
  36. beforeEach(() => {
  37. vi.stubGlobal('navigator', { languages: ['zh-CN'], language: 'zh-CN' })
  38. })
  39. afterEach(() => {
  40. vi.unstubAllGlobals()
  41. })
  42. it('declares the slot service', () => {
  43. expect(inject).toEqual(['slots'])
  44. })
  45. it('provides the service with base + settings dictionaries and registers the row (declaration before or after apply)', async () => {
  46. const before = await bench()
  47. declareItems(before.slots)
  48. await before.ctx.plugin({ inject: [...inject], apply }).await()
  49. const locale = before.ctx.get('locale') as LocaleService
  50. // Base dictionaries are registered: the (ns, locale) seats are occupied.
  51. expect(() => locale.register('common', 'zh', {})).toThrow('already has locale')
  52. expect(() => locale.register('common', 'en', {})).toThrow('already has locale')
  53. expect(locale.bind(SETTINGS_NS)('language.title')).toBe('语言')
  54. const entry = before.slots.entries(SLOT).find(e => e.component === LanguageRow)!
  55. expect(entry.options).toMatchObject({ id: 'language', order: 0 })
  56. const after = await bench()
  57. const fiber = after.ctx.plugin({ inject: [...inject], apply })
  58. await fiber.await()
  59. expect(after.slots.entries(SLOT)).toHaveLength(0)
  60. declareItems(after.slots)
  61. await Promise.resolve()
  62. expect(after.slots.entries(SLOT).some(e => e.component === LanguageRow)).toBe(true)
  63. })
  64. it('projects service snapshots into the row store and routes face writes back', async () => {
  65. const b = await bench()
  66. declareItems(b.slots)
  67. await b.ctx.plugin({ inject: [...inject], apply }).await()
  68. const locale = b.ctx.get('locale') as LocaleService
  69. // An event ahead of any inject hits the unbound-actions arm.
  70. locale.setLocale('en')
  71. const { entry, instance, face } = faceOf(b.slots)
  72. // The inject-time re-sync sealed the init window: the mirror is current.
  73. expect(instance.getSnapshot().active).toBe('en')
  74. expect(instance.getSnapshot().options.map(o => o.id)).toEqual(['zh', 'en'])
  75. // Copy rides the standard locale seat: the entry declares the namespace.
  76. expect(entry.locale).toBe(SETTINGS_NS)
  77. expect(locale.bind(SETTINGS_NS)('language.title')).toBe('Language')
  78. face.setLocale('zh')
  79. expect(locale.getLocale().active).toBe('zh')
  80. expect(instance.getSnapshot().active).toBe('zh')
  81. expect(locale.bind(SETTINGS_NS)('language.title')).toBe('语言')
  82. })
  83. it('recovers after an HMR collapse of the declaring entry (stale disposer must not block)', async () => {
  84. const b = await bench()
  85. const host = declareItems(b.slots)
  86. await b.ctx.plugin({ inject: [...inject], apply }).await()
  87. expect(b.slots.entries(SLOT)).toHaveLength(1)
  88. // Collapse: the declarer dies, the cascade removes our entry while the
  89. // apply closure still holds its (now stale) disposer.
  90. host()
  91. expect(b.slots.entries(SLOT)).toHaveLength(0)
  92. declareItems(b.slots)
  93. await Promise.resolve()
  94. expect(b.slots.entries(SLOT).some(e => e.component === LanguageRow)).toBe(true)
  95. })
  96. it('teardown removes the row; teardown without a declaration is quiet', async () => {
  97. const b = await bench()
  98. declareItems(b.slots)
  99. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  100. await fiber.await()
  101. expect(b.slots.entries(SLOT)).toHaveLength(1)
  102. await fiber.dispose()
  103. expect(b.slots.entries(SLOT)).toHaveLength(0)
  104. // Never-declared bench: the effect disposer's dispose arm stays undefined.
  105. const quiet = await bench()
  106. const f2 = quiet.ctx.plugin({ inject: [...inject], apply })
  107. await f2.await()
  108. await f2.dispose()
  109. expect(quiet.slots.entries(SLOT)).toHaveLength(0)
  110. })
  111. })