apply.spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 { describe, expect, it } 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. it('declares the slot service', () => {
  35. expect(inject).toEqual(['slots'])
  36. })
  37. it('provides the service with base + settings dictionaries and registers the row (declaration before or after apply)', async () => {
  38. const before = await bench()
  39. declareItems(before.slots)
  40. await before.ctx.plugin({ inject: [...inject], apply }).await()
  41. const locale = before.ctx.get('locale') as LocaleService
  42. // Base dictionaries are registered: the (ns, locale) seats are occupied.
  43. expect(() => locale.register('common', 'zh', {})).toThrow('already has locale')
  44. expect(() => locale.register('common', 'en', {})).toThrow('already has locale')
  45. expect(locale.bind(SETTINGS_NS)('language.title')).toBe('语言')
  46. const entry = before.slots.entries(SLOT).find(e => e.component === LanguageRow)!
  47. expect(entry.options).toMatchObject({ id: 'language', order: 0 })
  48. const after = await bench()
  49. const fiber = after.ctx.plugin({ inject: [...inject], apply })
  50. await fiber.await()
  51. expect(after.slots.entries(SLOT)).toHaveLength(0)
  52. declareItems(after.slots)
  53. await Promise.resolve()
  54. expect(after.slots.entries(SLOT).some(e => e.component === LanguageRow)).toBe(true)
  55. })
  56. it('projects service snapshots into the row store and routes face writes back', async () => {
  57. const b = await bench()
  58. declareItems(b.slots)
  59. await b.ctx.plugin({ inject: [...inject], apply }).await()
  60. const locale = b.ctx.get('locale') as LocaleService
  61. // An event ahead of any inject hits the unbound-actions arm.
  62. locale.setLocale('en')
  63. const { entry, instance, face } = faceOf(b.slots)
  64. // The inject-time re-sync sealed the init window: the mirror is current.
  65. expect(instance.getSnapshot().active).toBe('en')
  66. expect(instance.getSnapshot().options.map(o => o.id)).toEqual(['zh', 'en'])
  67. // Copy rides the standard locale seat: the entry declares the namespace.
  68. expect(entry.locale).toBe(SETTINGS_NS)
  69. expect(locale.bind(SETTINGS_NS)('language.title')).toBe('Language')
  70. face.setLocale('zh')
  71. expect(locale.getLocale().active).toBe('zh')
  72. expect(instance.getSnapshot().active).toBe('zh')
  73. expect(locale.bind(SETTINGS_NS)('language.title')).toBe('语言')
  74. })
  75. it('recovers after an HMR collapse of the declaring entry (stale disposer must not block)', async () => {
  76. const b = await bench()
  77. const host = declareItems(b.slots)
  78. await b.ctx.plugin({ inject: [...inject], apply }).await()
  79. expect(b.slots.entries(SLOT)).toHaveLength(1)
  80. // Collapse: the declarer dies, the cascade removes our entry while the
  81. // apply closure still holds its (now stale) disposer.
  82. host()
  83. expect(b.slots.entries(SLOT)).toHaveLength(0)
  84. declareItems(b.slots)
  85. await Promise.resolve()
  86. expect(b.slots.entries(SLOT).some(e => e.component === LanguageRow)).toBe(true)
  87. })
  88. it('teardown removes the row; teardown without a declaration is quiet', async () => {
  89. const b = await bench()
  90. declareItems(b.slots)
  91. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  92. await fiber.await()
  93. expect(b.slots.entries(SLOT)).toHaveLength(1)
  94. await fiber.dispose()
  95. expect(b.slots.entries(SLOT)).toHaveLength(0)
  96. // Never-declared bench: the effect disposer's dispose arm stays undefined.
  97. const quiet = await bench()
  98. const f2 = quiet.ctx.plugin({ inject: [...inject], apply })
  99. await f2.await()
  100. await f2.dispose()
  101. expect(quiet.slots.entries(SLOT)).toHaveLength(0)
  102. })
  103. })