413d5ab863
$backendThemes only lived in memory, so a relaunch on a skin from ~/.hermes/skins painted the default until the next skin.changed. Hydrate the registry from localStorage like user themes, and write it back on every change.
134 lines
5.1 KiB
TypeScript
134 lines
5.1 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { $backendThemes, $pendingSkinApply, __resetBackendSkinSync, ingestBackendSkin } from './backend-sync'
|
|
|
|
const skin = (name: string) => ({
|
|
name,
|
|
colors: { background: '#101020', ui_accent: '#ff33aa', banner_text: '#eeeeee' }
|
|
})
|
|
|
|
describe('ingestBackendSkin', () => {
|
|
beforeEach(() => {
|
|
window.localStorage.clear()
|
|
__resetBackendSkinSync()
|
|
})
|
|
|
|
it('registers a converted skin without applying when apply=false', () => {
|
|
ingestBackendSkin(skin('neon'), { apply: false })
|
|
|
|
expect($backendThemes.get().neon?.name).toBe('neon')
|
|
expect($pendingSkinApply.get()).toBeNull()
|
|
})
|
|
|
|
it('applies a new skin name once', () => {
|
|
ingestBackendSkin(skin('neon'), { apply: true })
|
|
|
|
expect($pendingSkinApply.get()).toBe('neon')
|
|
})
|
|
|
|
it('does not re-apply the same skin name', () => {
|
|
ingestBackendSkin(skin('neon'), { apply: true })
|
|
$pendingSkinApply.set(null)
|
|
ingestBackendSkin(skin('neon'), { apply: true })
|
|
|
|
expect($pendingSkinApply.get()).toBeNull()
|
|
})
|
|
|
|
it('applies again when the skin name changes', () => {
|
|
ingestBackendSkin(skin('neon'), { apply: true })
|
|
$pendingSkinApply.set(null)
|
|
ingestBackendSkin(skin('forest'), { apply: true })
|
|
|
|
expect($pendingSkinApply.get()).toBe('forest')
|
|
})
|
|
|
|
it('seed does not paint, but a later same-name skin.changed applies (missed-activation recovery)', () => {
|
|
// Connect while display.skin is already neon: seed records the baseline
|
|
// without painting (never stomp the persisted desktop theme on connect).
|
|
ingestBackendSkin(skin('neon'), { apply: false }) // gateway.ready seed
|
|
expect($pendingSkinApply.get()).toBeNull()
|
|
|
|
// The activation event was missed (skin set while disconnected / backend
|
|
// restarted). Hermes re-affirms it — `hermes config set display.skin neon`
|
|
// or a `hermes skin set` recolor. That explicit event must repaint even
|
|
// though the name matches the seed.
|
|
ingestBackendSkin(skin('neon'), { apply: true })
|
|
expect($pendingSkinApply.get()).toBe('neon')
|
|
|
|
// Once applied, a repeat same-name event is a no-op again...
|
|
$pendingSkinApply.set(null)
|
|
ingestBackendSkin(skin('neon'), { apply: true })
|
|
expect($pendingSkinApply.get()).toBeNull()
|
|
|
|
// ...and a genuine switch still applies.
|
|
ingestBackendSkin(skin('forest'), { apply: true }) // Hermes authored a new skin
|
|
expect($pendingSkinApply.get()).toBe('forest')
|
|
})
|
|
|
|
it('a reconnect re-seed after a real apply does not downgrade the applied baseline', () => {
|
|
ingestBackendSkin(skin('neon'), { apply: true }) // applied for real
|
|
$pendingSkinApply.set(null)
|
|
|
|
ingestBackendSkin(skin('neon'), { apply: false }) // reconnect: gateway.ready re-seed
|
|
ingestBackendSkin(skin('neon'), { apply: true }) // repeat event (e.g. in-place recolor)
|
|
|
|
// Already painted once — the repeat must not re-apply (protects a manual
|
|
// desktop-side theme switch from being snapped back after a reconnect).
|
|
expect($pendingSkinApply.get()).toBeNull()
|
|
})
|
|
|
|
it('never registers default in the backend store (desktop keeps its own palette)', () => {
|
|
ingestBackendSkin(skin('default'), { apply: true })
|
|
|
|
expect($backendThemes.get().default).toBeUndefined()
|
|
})
|
|
|
|
it('does not apply default on the connect-time seed', () => {
|
|
ingestBackendSkin(skin('default'), { apply: false })
|
|
|
|
expect($pendingSkinApply.get()).toBeNull()
|
|
})
|
|
|
|
it('applies a runtime switch back to default (repaints the desktop to its own default)', () => {
|
|
ingestBackendSkin(skin('neon'), { apply: false }) // gateway.ready seed on some skin
|
|
ingestBackendSkin(skin('default'), { apply: true }) // Hermes switched back to default
|
|
|
|
expect($pendingSkinApply.get()).toBe('default')
|
|
})
|
|
|
|
it('does not shadow a built-in name but can still apply it', () => {
|
|
ingestBackendSkin(skin('mono'), { apply: true })
|
|
|
|
expect($backendThemes.get().mono).toBeUndefined()
|
|
expect($pendingSkinApply.get()).toBe('mono')
|
|
})
|
|
|
|
it('ignores empty payloads', () => {
|
|
ingestBackendSkin(undefined, { apply: true })
|
|
ingestBackendSkin({ name: '' }, { apply: true })
|
|
|
|
expect($pendingSkinApply.get()).toBeNull()
|
|
})
|
|
|
|
it('hydrates the registry from storage on load, so a persisted pick resolves before the gateway connects', async () => {
|
|
ingestBackendSkin(skin('neon'), { apply: false })
|
|
expect(JSON.parse(window.localStorage.getItem('hermes-desktop-backend-themes-v1') ?? '{}').neon?.name).toBe('neon')
|
|
|
|
// Relaunch: a fresh module instance with the same storage.
|
|
vi.resetModules()
|
|
const fresh = await import('./backend-sync')
|
|
|
|
expect(fresh.$backendThemes.get().neon?.name).toBe('neon')
|
|
})
|
|
|
|
it('drops junk and built-in names from the cached registry', async () => {
|
|
const mono = { name: 'mono', label: 'x', colors: { background: '#000', foreground: '#fff', primary: '#f0f' } }
|
|
window.localStorage.setItem('hermes-desktop-backend-themes-v1', JSON.stringify({ mono, bad: { name: 'bad' } }))
|
|
|
|
vi.resetModules()
|
|
const fresh = await import('./backend-sync')
|
|
|
|
expect(fresh.$backendThemes.get()).toEqual({})
|
|
})
|
|
})
|