apply.spec.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /** ui-theme apply wiring: service provision, settings dictionaries riding the
  2. * locale service, declaration-aware Appearance row registration, snapshot
  3. * projection into the row store, and HMR collapse recovery. */
  4. import { Context } from 'cordis'
  5. import { describe, expect, it } from 'vitest'
  6. import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
  7. import { LocaleService } from '@deepseek-ai/dsh-client-locale/client'
  8. import { usePinnedBrowserLanguages } from '@deepseek-ai/dsh-client-test-runtime'
  9. import { apply, inject, SETTINGS_NS } from '@deepseek-ai/dsh-client-ui-theme/client'
  10. import type { AppearanceRowInjected, ThemeService } from '@deepseek-ai/dsh-client-ui-theme/client'
  11. import { AppearanceRow } from '../src/client/AppearanceRow.tsx'
  12. import type { createAppearanceRowStore } from '../src/client/settings-store.ts'
  13. // The service reads its initial locale from the browser; these specs assert
  14. // the shipped Chinese copy, so they state the browser they assume.
  15. usePinnedBrowserLanguages('zh-CN')
  16. const SLOT = 'settings.general.item'
  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. /** Stand in for the settings shell: declare the General item slot from root. */
  25. function declareItems(slots: SlotsService): () => void {
  26. return slots.register(
  27. { name: 'root', children: { [SLOT]: { kind: 'list', scope: 'root' } } } as never,
  28. () => null,
  29. )
  30. }
  31. /** Mirror the framework's inject choreography: bake a real instance from the
  32. * declared handle and hand its actions to the entry's inject factory. */
  33. function faceOf(slots: SlotsService) {
  34. const entry = slots.entries(SLOT).find(e => e.component === AppearanceRow)!
  35. const handle = entry.store as ReturnType<typeof createAppearanceRowStore>
  36. const instance = handle.create()
  37. const face = (entry.inject as unknown as (a: typeof instance.actions) => AppearanceRowInjected)(instance.actions)
  38. return { entry, instance, face }
  39. }
  40. describe('ui-theme apply', () => {
  41. it('declares the slot and locale services', () => {
  42. expect(inject).toEqual(['slots', 'locale'])
  43. })
  44. it('provides the service, registers localized copy, and registers the row (declaration before or after apply)', async () => {
  45. const before = await bench()
  46. declareItems(before.slots)
  47. await before.ctx.plugin({ inject: [...inject], apply }).await()
  48. expect(before.locale.bind(SETTINGS_NS)('appearance.title')).toBe('外观')
  49. before.locale.setLocale('en')
  50. expect(before.locale.bind(SETTINGS_NS)('appearance.title')).toBe('Appearance')
  51. const entry = before.slots.entries(SLOT).find(e => e.component === AppearanceRow)!
  52. expect(entry.options).toMatchObject({ id: 'appearance', order: 10 })
  53. const after = await bench()
  54. const fiber = after.ctx.plugin({ inject: [...inject], apply })
  55. await fiber.await()
  56. expect(after.slots.entries(SLOT)).toHaveLength(0)
  57. declareItems(after.slots)
  58. await Promise.resolve()
  59. expect(after.slots.entries(SLOT).some(e => e.component === AppearanceRow)).toBe(true)
  60. })
  61. it('projects service snapshots into the row store and routes face writes back', async () => {
  62. const b = await bench()
  63. declareItems(b.slots)
  64. await b.ctx.plugin({ inject: [...inject], apply }).await()
  65. const theme = b.ctx.get('theme') as ThemeService
  66. // An event ahead of any inject hits the unbound-actions arm.
  67. theme.setTheme('dark')
  68. const { instance, face } = faceOf(b.slots)
  69. // The inject-time re-sync sealed the init window: the mirror is current.
  70. expect(instance.getSnapshot().preference).toBe('dark')
  71. // Copy rides the standard locale seat: the entry declares the namespace.
  72. expect(b.slots.entries(SLOT).find(e => e.component === AppearanceRow)!.locale).toBe(SETTINGS_NS)
  73. face.setTheme('system')
  74. expect(theme.getTheme().preference).toBe('system')
  75. expect(instance.getSnapshot().preference).toBe('system')
  76. })
  77. it('recovers after an HMR collapse of the declaring entry (stale disposer must not block)', async () => {
  78. const b = await bench()
  79. const host = declareItems(b.slots)
  80. await b.ctx.plugin({ inject: [...inject], apply }).await()
  81. expect(b.slots.entries(SLOT)).toHaveLength(1)
  82. // Collapse: the declarer dies, the cascade removes our entry while the
  83. // apply closure still holds its (now stale) disposer.
  84. host()
  85. expect(b.slots.entries(SLOT)).toHaveLength(0)
  86. declareItems(b.slots)
  87. await Promise.resolve()
  88. expect(b.slots.entries(SLOT).some(e => e.component === AppearanceRow)).toBe(true)
  89. })
  90. it('teardown removes the row and the dictionaries; teardown without a declaration is quiet', async () => {
  91. const b = await bench()
  92. declareItems(b.slots)
  93. const fiber = b.ctx.plugin({ inject: [...inject], apply })
  94. await fiber.await()
  95. expect(b.slots.entries(SLOT)).toHaveLength(1)
  96. await fiber.dispose()
  97. expect(b.slots.entries(SLOT)).toHaveLength(0)
  98. // Dictionary disposal: translation falls back to the bare key.
  99. expect(b.locale.bind(SETTINGS_NS)('appearance.title')).toBe('appearance.title')
  100. // Never-declared bench: the effect disposer's dispose arm stays undefined.
  101. const quiet = await bench()
  102. const f2 = quiet.ctx.plugin({ inject: [...inject], apply })
  103. await f2.await()
  104. await f2.dispose()
  105. expect(quiet.slots.entries(SLOT)).toHaveLength(0)
  106. })
  107. })