host.client.spec.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { Context } from '@deepseek-ai/cordis'
  2. import { describe, expect, it } from 'vitest'
  3. import type { IndexInjection } from '@deepseek-ai/dsh-host-webserver'
  4. import { SettingsProvider, settingsNamespace, type SettingsNamespace } from '@deepseek-ai/dsh-settings'
  5. import {
  6. DEFAULT_PREFERENCE, THEME_SETTINGS_NAMESPACE, apply,
  7. } from '@deepseek-ai/dsh-client-ui-theme'
  8. class MemorySettings extends SettingsProvider {
  9. readonly writable = true
  10. protected load(): Promise<Record<string, unknown>> { return Promise.resolve({}) }
  11. protected persist(_ns: SettingsNamespace, _section: Record<string, unknown>): Promise<void> {
  12. return Promise.resolve()
  13. }
  14. }
  15. /** Collect the injection table the way an index render or boot payload does. */
  16. function collect(ctx: Context): IndexInjection[] {
  17. const table: IndexInjection[] = []
  18. ctx.emit('webserver/index-inject', table)
  19. return table
  20. }
  21. /** Narrow the theme row and return its script body. */
  22. function scriptText(row: IndexInjection | undefined): string {
  23. if (row?.kind !== 'script') throw new Error('expected a script row')
  24. return row.text
  25. }
  26. describe('ui-theme host', () => {
  27. it('registers, validates, and disposes the durable theme namespace with its fiber', async () => {
  28. const ctx = new Context()
  29. await ctx.plugin(MemorySettings).await()
  30. const fiber = ctx.plugin({ apply })
  31. await fiber.await()
  32. const ns = settingsNamespace(THEME_SETTINGS_NAMESPACE)
  33. expect(ctx.settings.get(ns)).toEqual({ preference: DEFAULT_PREFERENCE })
  34. await ctx.settings.update(ns, { preference: 'dark' })
  35. expect(ctx.settings.get(ns)).toEqual({ preference: 'dark' })
  36. await expect(ctx.settings.update(ns, { preference: 'sepia' })).rejects.toThrow()
  37. await fiber.dispose()
  38. expect(ctx.settings.describe().map(row => row.ns)).not.toContain(ns)
  39. })
  40. it('answers each collection with the current durable preference until disposal', async () => {
  41. const ctx = new Context()
  42. await ctx.plugin(MemorySettings).await()
  43. const fiber = ctx.plugin({ apply })
  44. await fiber.await()
  45. const rows = collect(ctx)
  46. expect(rows).toHaveLength(1)
  47. expect(rows[0]).toMatchObject({ kind: 'script', placement: 'body' })
  48. expect(scriptText(rows[0])).toContain('const preference = "system"')
  49. await ctx.settings.update(settingsNamespace(THEME_SETTINGS_NAMESPACE), { preference: 'dark' })
  50. expect(scriptText(collect(ctx)[0])).toContain('const preference = "dark"')
  51. await fiber.dispose()
  52. expect(collect(ctx)).toEqual([])
  53. })
  54. it('uses the system preference without a settings provider', async () => {
  55. const ctx = new Context()
  56. await ctx.plugin({ apply }).await()
  57. expect(scriptText(collect(ctx)[0])).toContain('const preference = "system"')
  58. })
  59. it('falls back to the schema default while the theme namespace holds no section', async () => {
  60. // A settings provider whose namespace read comes back empty (registration
  61. // still pending or a provider without schema defaults).
  62. const ctx = new Context()
  63. ctx.provide('settings', { register: () => () => {}, get: () => undefined } as never)
  64. await ctx.plugin({ apply }).await()
  65. expect(scriptText(collect(ctx)[0])).toContain('const preference = "system"')
  66. })
  67. })