Merge remote-tracking branch 'origin/master' into feat/web-message-feedback-ui
Adapt to two contract changes master introduced: - The generated Remote face now wraps every business result in RemoteResult, folding carrier failures into an ok:false branch instead of rejecting. The controller reads that envelope at its three call sites and maps a carrier failure onto the same settled shape the controls already render; three specs cover the new branch. - Client packages split their tsconfig into host and client halves, and the host aggregate now compiles any test not named *.client.spec.*. Rename this package's specs to the client convention and drop the ../connection project reference, which pointed at a solution file that no longer carries the client sources. Keep master's mount loop with its rollback-on-failure in api-remotes and add messageFeedbackRemote to it.
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
// @vitest-environment jsdom
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { Context } from '@deepseek-ai/cordis'
|
||||
import { stubSettingsScope, type StubSettingsScope } from '@deepseek-ai/dsh-client-test-runtime'
|
||||
import type { ThemeSettings, ThemeSnapshot } from '@deepseek-ai/dsh-client-ui-theme/client'
|
||||
import { ThemeService } from '@deepseek-ai/dsh-client-ui-theme/client'
|
||||
|
||||
const make = (host = stubSettingsScope<ThemeSettings>()): {
|
||||
ctx: Context
|
||||
theme: ThemeService
|
||||
events: ThemeSnapshot[]
|
||||
host: StubSettingsScope<ThemeSettings>
|
||||
} => {
|
||||
const ctx = new Context()
|
||||
const events: ThemeSnapshot[] = []
|
||||
ctx.on('theme/change', (snapshot) => { events.push(snapshot) })
|
||||
return { ctx, theme: new ThemeService(ctx, host.scope), events, host }
|
||||
}
|
||||
|
||||
describe('ThemeService', () => {
|
||||
it('defaults to the system preference resolved against prefers-color-scheme', () => {
|
||||
const { theme } = make()
|
||||
const snapshot = theme.getTheme()
|
||||
expect(snapshot.preference).toBe('system')
|
||||
// jsdom matchMedia is absent; system resolves to light.
|
||||
expect(snapshot.active.id).toBe('light')
|
||||
expect(snapshot.active.colorScheme).toBe('light')
|
||||
expect(snapshot.themes.map(t => t.id)).toEqual(['light', 'dark'])
|
||||
})
|
||||
|
||||
it('setTheme switches, writes through the scope, republishes, and keeps DOM untouched', () => {
|
||||
const { theme, events, host } = make()
|
||||
theme.setTheme('dark')
|
||||
expect(theme.getTheme().preference).toBe('dark')
|
||||
expect(theme.getTheme().active.colorScheme).toBe('dark')
|
||||
expect(host.set).toHaveBeenCalledWith('preference', 'dark')
|
||||
expect(events).toHaveLength(1)
|
||||
expect(events[0]).toBe(theme.getTheme())
|
||||
// The service never touches presentation state.
|
||||
expect(document.body.hasAttribute('data-ds-dark-theme')).toBe(false)
|
||||
// Same-value set is a no-op (no extra event).
|
||||
theme.setTheme('dark')
|
||||
expect(events).toHaveLength(1)
|
||||
expect(host.set).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('adopts a published Host section without writing it back', () => {
|
||||
const { theme, events, host } = make()
|
||||
host.publish({ status: 'ready', value: { preference: 'dark' }, revision: 1, writable: true })
|
||||
expect(theme.getTheme().preference).toBe('dark')
|
||||
expect(events).toHaveLength(1)
|
||||
expect(host.set).not.toHaveBeenCalled()
|
||||
host.publish({ value: { preference: 'dark' }, revision: 2 })
|
||||
expect(events).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('adopts a section already standing at construction', () => {
|
||||
const host = stubSettingsScope<ThemeSettings>()
|
||||
host.publish({ status: 'ready', value: { preference: 'dark' }, revision: 1, writable: true })
|
||||
const { theme } = make(host)
|
||||
expect(theme.getTheme().preference).toBe('dark')
|
||||
})
|
||||
|
||||
it('throws on unknown setTheme ids, duplicate registration, and the system id', () => {
|
||||
const { theme } = make()
|
||||
expect(() => { theme.setTheme('sepia') }).toThrow('not registered')
|
||||
expect(() => theme.register({ id: 'light', colorScheme: 'light', tokens: {} })).toThrow('already registered')
|
||||
expect(() => theme.register({ id: 'system', colorScheme: 'light', tokens: {} })).toThrow('preference')
|
||||
})
|
||||
|
||||
it('registered themes join the snapshot; disposing the active one resets to default', () => {
|
||||
const { theme, events, host } = make()
|
||||
const dispose = theme.register({ id: 'sepia', colorScheme: 'light', tokens: { '--dsw-alias-bg-base': 'red' } })
|
||||
expect(theme.getTheme().themes.map(t => t.id)).toEqual(['light', 'dark', 'sepia'])
|
||||
theme.setTheme('sepia')
|
||||
expect(theme.getTheme().active.tokens['--dsw-alias-bg-base']).toBe('red')
|
||||
dispose()
|
||||
expect(theme.getTheme().preference).toBe('system')
|
||||
expect(theme.getTheme().themes.map(t => t.id)).toEqual(['light', 'dark'])
|
||||
// Custom ids are in-process extension themes; only the built-in product
|
||||
// preferences cross the Host settings schema.
|
||||
expect(host.set).not.toHaveBeenCalled()
|
||||
// register + set + dispose = three publishes; disposer is idempotent.
|
||||
expect(events.length).toBe(3)
|
||||
dispose()
|
||||
expect(events.length).toBe(3)
|
||||
})
|
||||
|
||||
it('disposing an inactive theme keeps the active preference', () => {
|
||||
const { theme } = make()
|
||||
const dispose = theme.register({ id: 'sepia', colorScheme: 'light', tokens: {} })
|
||||
theme.setTheme('dark')
|
||||
dispose()
|
||||
expect(theme.getTheme().preference).toBe('dark')
|
||||
})
|
||||
|
||||
it('revision increases monotonically across every publish', () => {
|
||||
const { theme, events } = make()
|
||||
theme.setTheme('dark')
|
||||
theme.setTheme('light')
|
||||
const dispose = theme.register({ id: 'sepia', colorScheme: 'dark', tokens: {} })
|
||||
dispose()
|
||||
expect(events.map(e => e.revision)).toEqual([1, 2, 3, 4])
|
||||
})
|
||||
|
||||
it('context dispose releases the scope subscription', async () => {
|
||||
const { ctx, host } = make()
|
||||
expect(host.listenerCount()).toBe(1)
|
||||
await ctx.fiber.dispose()
|
||||
expect(host.listenerCount()).toBe(0)
|
||||
})
|
||||
|
||||
describe('prefers-color-scheme resolution (stubbed matchMedia)', () => {
|
||||
type Listener = () => void
|
||||
const stubMedia = (initialMatches: boolean) => {
|
||||
const listeners = new Set<Listener>()
|
||||
const media = {
|
||||
matches: initialMatches,
|
||||
addEventListener: (_: 'change', fn: Listener) => { listeners.add(fn) },
|
||||
removeEventListener: (_: 'change', fn: Listener) => { listeners.delete(fn) },
|
||||
flip() {
|
||||
this.matches = !this.matches
|
||||
for (const fn of listeners) fn()
|
||||
},
|
||||
listenerCount: () => listeners.size,
|
||||
}
|
||||
vi.stubGlobal('matchMedia', () => media)
|
||||
return media
|
||||
}
|
||||
|
||||
afterEach(() => { vi.unstubAllGlobals() })
|
||||
|
||||
it('system resolves against the media query and follows OS flips', () => {
|
||||
const media = stubMedia(true)
|
||||
const { theme, events } = make()
|
||||
expect(theme.getTheme().preference).toBe('system')
|
||||
expect(theme.getTheme().active.id).toBe('dark')
|
||||
media.flip()
|
||||
expect(theme.getTheme().active.id).toBe('light')
|
||||
expect(events).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('OS flips do not republish while a concrete preference is set', () => {
|
||||
const media = stubMedia(false)
|
||||
const { theme, events } = make()
|
||||
theme.setTheme('light')
|
||||
expect(events).toHaveLength(1)
|
||||
media.flip()
|
||||
expect(events).toHaveLength(1)
|
||||
expect(theme.getTheme().active.id).toBe('light')
|
||||
})
|
||||
|
||||
it('context dispose releases the media listener', async () => {
|
||||
const media = stubMedia(false)
|
||||
const { ctx } = make()
|
||||
expect(media.listenerCount()).toBe(1)
|
||||
await ctx.fiber.dispose()
|
||||
expect(media.listenerCount()).toBe(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user