Registrations declare a dictionary namespace (locale: NS) and the renderer
synthesizes a typed t prop for the entry's component from the installed
LocaleFace; the seat binding is re-derived per locale revision, so a language
switch hands out fresh t references and memoized consumers re-render through
ordinary shallow comparison. LocaleNamespaceMap is the declare-merge table
(namespace -> dictionary key union); TranslateNS<'ns'> is the
namespace-addressed translate type (namespace keys plus the shared common
vocabulary), carried by the t seat and by the locale service's typed bind.
LocaleService implements the face (lookup ns -> common -> zh -> key,
revision-carrying snapshots with subscriber isolation) and installs it
through the boot-once slots.installLocale seam, mirroring the renderer
install. The typed register(ns, {zh, en}) overload checks each dictionary
against the namespace's key union and requires every shipped locale, so a
missing or extra key and an unbalanced translation are compile errors.
Dictionary registration bumps the face revision without emitting
locale/change — the event now means exactly 'the active locale switched',
so registration-heavy boot cannot storm event listeners.
119 lines
5.1 KiB
TypeScript
119 lines
5.1 KiB
TypeScript
/** ui-theme apply wiring: service provision, settings dictionaries riding the
|
|
* locale service, declaration-aware Appearance row registration, snapshot
|
|
* projection into the row store, and HMR collapse recovery. */
|
|
import { Context } from 'cordis'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
|
|
import { LocaleService } from '@deepseek-ai/dsh-client-locale/client'
|
|
import { apply, inject, SETTINGS_NS } from '@deepseek-ai/dsh-client-ui-theme/client'
|
|
import type { AppearanceRowInjected, ThemeService } from '@deepseek-ai/dsh-client-ui-theme/client'
|
|
import { AppearanceRow } from '../src/client/AppearanceRow.tsx'
|
|
import type { createAppearanceRowStore } from '../src/client/settings-store.ts'
|
|
|
|
const SLOT = 'settings.general.item'
|
|
|
|
async function bench() {
|
|
const ctx = new Context()
|
|
await ctx.plugin(SlotsService).await()
|
|
const locale = new LocaleService(ctx)
|
|
ctx.provide('locale', locale)
|
|
return { ctx, slots: ctx.get('slots') as SlotsService, locale }
|
|
}
|
|
|
|
/** Stand in for the settings shell: declare the General item slot from root. */
|
|
function declareItems(slots: SlotsService): () => void {
|
|
return slots.register(
|
|
{ name: 'root', children: { [SLOT]: { kind: 'list', scope: 'root' } } } as never,
|
|
() => null,
|
|
)
|
|
}
|
|
|
|
/** Mirror the framework's inject choreography: bake a real instance from the
|
|
* declared handle and hand its actions to the entry's inject factory. */
|
|
function faceOf(slots: SlotsService) {
|
|
const entry = slots.entries(SLOT).find(e => e.component === AppearanceRow)!
|
|
const handle = entry.store as ReturnType<typeof createAppearanceRowStore>
|
|
const instance = handle.create()
|
|
const face = (entry.inject as unknown as (a: typeof instance.actions) => AppearanceRowInjected)(instance.actions)
|
|
return { entry, instance, face }
|
|
}
|
|
|
|
describe('ui-theme apply', () => {
|
|
it('declares the slot and locale services', () => {
|
|
expect(inject).toEqual(['slots', 'locale'])
|
|
})
|
|
|
|
it('provides the service, registers localized copy, and registers the row (declaration before or after apply)', async () => {
|
|
const before = await bench()
|
|
declareItems(before.slots)
|
|
await before.ctx.plugin({ inject: [...inject], apply }).await()
|
|
expect(before.locale.bind(SETTINGS_NS)('appearance.title')).toBe('外观')
|
|
before.locale.setLocale('en')
|
|
expect(before.locale.bind(SETTINGS_NS)('appearance.title')).toBe('Appearance')
|
|
const entry = before.slots.entries(SLOT).find(e => e.component === AppearanceRow)!
|
|
expect(entry.options).toMatchObject({ id: 'appearance', order: 10 })
|
|
|
|
const after = await bench()
|
|
const fiber = after.ctx.plugin({ inject: [...inject], apply })
|
|
await fiber.await()
|
|
expect(after.slots.entries(SLOT)).toHaveLength(0)
|
|
declareItems(after.slots)
|
|
await Promise.resolve()
|
|
expect(after.slots.entries(SLOT).some(e => e.component === AppearanceRow)).toBe(true)
|
|
})
|
|
|
|
it('projects service snapshots into the row store and routes face writes back', async () => {
|
|
const b = await bench()
|
|
declareItems(b.slots)
|
|
await b.ctx.plugin({ inject: [...inject], apply }).await()
|
|
const theme = b.ctx.get('theme') as ThemeService
|
|
// An event ahead of any inject hits the unbound-actions arm.
|
|
theme.setTheme('dark')
|
|
|
|
const { instance, face } = faceOf(b.slots)
|
|
// The inject-time re-sync sealed the init window: the mirror is current.
|
|
expect(instance.getSnapshot().preference).toBe('dark')
|
|
// Copy rides the standard locale seat: the entry declares the namespace.
|
|
expect(b.slots.entries(SLOT).find(e => e.component === AppearanceRow)!.locale).toBe(SETTINGS_NS)
|
|
|
|
face.setTheme('system')
|
|
expect(theme.getTheme().preference).toBe('system')
|
|
expect(instance.getSnapshot().preference).toBe('system')
|
|
})
|
|
|
|
it('recovers after an HMR collapse of the declaring entry (stale disposer must not block)', async () => {
|
|
const b = await bench()
|
|
const host = declareItems(b.slots)
|
|
await b.ctx.plugin({ inject: [...inject], apply }).await()
|
|
expect(b.slots.entries(SLOT)).toHaveLength(1)
|
|
|
|
// Collapse: the declarer dies, the cascade removes our entry while the
|
|
// apply closure still holds its (now stale) disposer.
|
|
host()
|
|
expect(b.slots.entries(SLOT)).toHaveLength(0)
|
|
|
|
declareItems(b.slots)
|
|
await Promise.resolve()
|
|
expect(b.slots.entries(SLOT).some(e => e.component === AppearanceRow)).toBe(true)
|
|
})
|
|
|
|
it('teardown removes the row and the dictionaries; teardown without a declaration is quiet', async () => {
|
|
const b = await bench()
|
|
declareItems(b.slots)
|
|
const fiber = b.ctx.plugin({ inject: [...inject], apply })
|
|
await fiber.await()
|
|
expect(b.slots.entries(SLOT)).toHaveLength(1)
|
|
await fiber.dispose()
|
|
expect(b.slots.entries(SLOT)).toHaveLength(0)
|
|
// Dictionary disposal: translation falls back to the bare key.
|
|
expect(b.locale.bind(SETTINGS_NS)('appearance.title')).toBe('appearance.title')
|
|
|
|
// Never-declared bench: the effect disposer's dispose arm stays undefined.
|
|
const quiet = await bench()
|
|
const f2 = quiet.ctx.plugin({ inject: [...inject], apply })
|
|
await f2.await()
|
|
await f2.dispose()
|
|
expect(quiet.slots.entries(SLOT)).toHaveLength(0)
|
|
})
|
|
})
|