host.client.spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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, 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 = THEME_SETTINGS_NAMESPACE
  33. expect(ctx.settings.get(ns)).toEqual({ preference: DEFAULT_PREFERENCE, fontSize: 14 })
  34. await ctx.settings.update(ns, { preference: 'dark', fontSize: 16 })
  35. expect(ctx.settings.get(ns)).toEqual({ preference: 'dark', fontSize: 16 })
  36. await expect(ctx.settings.update(ns, { preference: 'sepia' })).rejects.toThrow()
  37. await expect(ctx.settings.update(ns, { fontSize: 11 })).rejects.toThrow()
  38. await expect(ctx.settings.update(ns, { fontSize: 18 })).rejects.toThrow()
  39. await fiber.dispose()
  40. expect(ctx.settings.describe().map(row => row.ns)).not.toContain(ns)
  41. })
  42. it('answers each collection with the current durable preference until disposal', async () => {
  43. const ctx = new Context()
  44. await ctx.plugin(MemorySettings).await()
  45. const fiber = ctx.plugin({ apply })
  46. await fiber.await()
  47. const rows = collect(ctx)
  48. expect(rows).toHaveLength(1)
  49. expect(rows[0]).toMatchObject({ kind: 'script', placement: 'body' })
  50. expect(scriptText(rows[0])).toContain('const preference = "system"')
  51. expect(scriptText(rows[0])).toContain('"14px"')
  52. await ctx.settings.update(THEME_SETTINGS_NAMESPACE, { preference: 'dark', fontSize: 17 })
  53. expect(scriptText(collect(ctx)[0])).toContain('const preference = "dark"')
  54. expect(scriptText(collect(ctx)[0])).toContain('"17px"')
  55. await fiber.dispose()
  56. expect(collect(ctx)).toEqual([])
  57. })
  58. it('uses the system preference without a settings provider', async () => {
  59. const ctx = new Context()
  60. await ctx.plugin({ apply }).await()
  61. expect(scriptText(collect(ctx)[0])).toContain('const preference = "system"')
  62. })
  63. it('falls back to the schema default while the theme namespace holds no section', async () => {
  64. // A settings provider whose namespace read comes back empty (registration
  65. // still pending or a provider without schema defaults).
  66. const ctx = new Context()
  67. ctx.provide('settings', { register: () => () => {}, get: () => undefined } as never)
  68. await ctx.plugin({ apply }).await()
  69. expect(scriptText(collect(ctx)[0])).toContain('const preference = "system"')
  70. })
  71. })