settings-controller.host.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from '@deepseek-ai/cordis'
  3. import z from '@deepseek-ai/schemastery'
  4. import { settingsNamespace } from '@deepseek-ai/dsh-settings'
  5. import type { SettingsDescriptor, SettingsNamespace } from '@deepseek-ai/dsh-settings'
  6. import { TypertRemoteFailure, remoteMethods } from '@deepseek-ai/dsh-typert-protocol'
  7. import SettingsController from '../src/index.ts'
  8. import { MemorySettings } from '../../../settings/settings/tests/memory.ts'
  9. const NS = settingsNamespace('ui-test')
  10. const Profile = z.object({
  11. preference: z.union(['light', 'dark']).default('light'),
  12. apiKey: z.string().role('secret'),
  13. })
  14. /** A provider that reports a local document, for the `hasDocument` fact. */
  15. class DocumentSettings extends MemorySettings {
  16. override get documentPath(): string | undefined {
  17. return '/deployment/settings.yaml'
  18. }
  19. }
  20. /** A provider whose read forgets the namespace its write just committed. */
  21. class VanishingSettings extends MemorySettings {
  22. override describe(): SettingsDescriptor[] {
  23. return []
  24. }
  25. }
  26. /**
  27. * A provider whose descriptor omits the secret-slot list. `secrets` is optional
  28. * on the descriptor, so a foreign provider may leave it out even under
  29. * `redactSecrets`, and the view still has to declare an empty list.
  30. */
  31. class SlotlessSettings extends MemorySettings {
  32. override describe(): SettingsDescriptor[] {
  33. return [{
  34. ns: NS,
  35. schema: Profile.toJSON(),
  36. value: { preference: 'light' },
  37. applies: 'live',
  38. revision: 0,
  39. } as unknown as SettingsDescriptor]
  40. }
  41. }
  42. /** A provider that refuses every write the way a read-only backing store would. */
  43. class RefusingSettings extends MemorySettings {
  44. override mutate(ns: SettingsNamespace): Promise<void> {
  45. return Promise.reject(new Error(`settings "${ns}" is read-only in this deployment`))
  46. }
  47. }
  48. /** A provider that refuses with a bare string, the way some storage clients do. */
  49. class LiteralRefusingSettings extends MemorySettings {
  50. override async mutate(): Promise<void> {
  51. throw 'the document is locked'
  52. }
  53. }
  54. async function boot(
  55. provider: typeof MemorySettings = MemorySettings,
  56. options: { doc?: Record<string, unknown>; base?: { preference: 'light' | 'dark' } } = {},
  57. ): Promise<{ controller: SettingsController; ctx: Context }> {
  58. const ctx = new Context()
  59. await ctx.plugin(provider, options.doc === undefined ? {} : { doc: options.doc })
  60. ctx.settings.register(NS, Profile, options.base === undefined ? {} : { base: options.base })
  61. await ctx.plugin(SettingsController)
  62. return { controller: ctx.settingsController, ctx }
  63. }
  64. describe('the settings Remote namespace a configuration page calls', () => {
  65. it('publishes the settings namespace from its own service key', async () => {
  66. const { controller } = await boot()
  67. expect(controller.typertRemote.serviceKey).toBe('settingsController')
  68. expect(controller.typertRemote.namespace).toBe('settings')
  69. expect(remoteMethods(controller)).toEqual([
  70. { method: 'describe', invocation: { kind: 'direct' } },
  71. { method: 'update', invocation: { kind: 'direct' } },
  72. { method: 'replace', invocation: { kind: 'direct' } },
  73. { method: 'mutate', invocation: { kind: 'direct' } },
  74. ])
  75. })
  76. it('reports the actionable configuration error while no settings provider is mounted', async () => {
  77. const ctx = new Context()
  78. await ctx.plugin(SettingsController)
  79. const calls: Array<() => unknown> = [
  80. () => ctx.settingsController.describe(),
  81. () => ctx.settingsController.update('ui-test', {}, undefined),
  82. () => ctx.settingsController.replace('ui-test', {}, undefined),
  83. () => ctx.settingsController.mutate('ui-test', [], undefined),
  84. ]
  85. for (const call of calls) {
  86. const failure = await Promise.resolve().then(call).catch((error: unknown) => error)
  87. expect(failure).toBeInstanceOf(TypertRemoteFailure)
  88. expect((failure as TypertRemoteFailure).failure).toEqual({
  89. code: 'internal',
  90. message: 'settings service is absent: this deployment does not mount a settings provider (e.g. @deepseek-ai/dsh-settings-file) in its composition',
  91. details: {},
  92. })
  93. }
  94. })
  95. it('mounts the credentials namespace beside its own', async () => {
  96. const ctx = new Context()
  97. await ctx.plugin(MemorySettings)
  98. ctx.settings.register(NS, Profile)
  99. const fiber = ctx.plugin(SettingsController)
  100. await fiber.await()
  101. expect(ctx.get('credentialsController')).toBeDefined()
  102. await fiber.dispose()
  103. expect(ctx.get('settingsController')).toBeUndefined()
  104. expect(ctx.get('credentialsController')).toBeUndefined()
  105. })
  106. it('describes every namespace redacted, with the deployment facts around them', async () => {
  107. const { controller } = await boot(DocumentSettings, { doc: { 'ui-test': { apiKey: 'sk-stored' } } })
  108. const value = controller.describe()
  109. expect(value).toMatchObject({ writable: true, hasDocument: true })
  110. const [view] = value.namespaces
  111. expect(view?.ns).toBe('ui-test')
  112. // The secret never rides; its slot reports only that one is stored.
  113. expect(JSON.stringify(value)).not.toContain('sk-stored')
  114. expect(view?.secrets).toEqual([{ path: ['apiKey'], set: true }])
  115. // Redaction removes the field rather than replacing it, so the layer that
  116. // stored a secret comes back empty instead of carrying a placeholder.
  117. expect(view?.user).toEqual({})
  118. })
  119. it('reports a read-only provider and omits the layers it has none of', async () => {
  120. const { controller } = await boot(class extends MemorySettings {
  121. override get writable(): boolean {
  122. return false
  123. }
  124. })
  125. const value = controller.describe()
  126. expect(value).toMatchObject({ writable: false, hasDocument: false })
  127. const [view] = value.namespaces
  128. // No composition base was declared and no user section is stored, so
  129. // neither optional layer appears at all.
  130. expect(view && 'base' in view).toBe(false)
  131. expect(view && 'user' in view).toBe(false)
  132. })
  133. it('declares an empty slot list when the provider names no secrets', async () => {
  134. const { controller } = await boot(SlotlessSettings)
  135. const [view] = controller.describe().namespaces
  136. expect(view?.secrets).toEqual([])
  137. })
  138. it('carries the composition base layer when the registrant declared one', async () => {
  139. const { controller } = await boot(MemorySettings, { base: { preference: 'dark' } })
  140. const [view] = controller.describe().namespaces
  141. expect(view?.base).toEqual({ preference: 'dark' })
  142. })
  143. it('applies path-addressed edits and answers with the namespace it just wrote', async () => {
  144. const { controller } = await boot()
  145. const view = await controller.mutate('ui-test', [{ op: 'set', path: ['preference'], value: 'dark' }], undefined)
  146. expect(view).toMatchObject({ ns: 'ui-test', user: { preference: 'dark' } })
  147. expect(view.revision).toBeGreaterThan(0)
  148. })
  149. it('supports merge updates and wholesale replacement on the Remote namespace', async () => {
  150. const { controller } = await boot(MemorySettings, {
  151. doc: { 'ui-test': { preference: 'dark', apiKey: 'sk-stored' } },
  152. })
  153. const updated = await controller.update('ui-test', { preference: 'light' }, undefined)
  154. expect(updated.user).toEqual({ preference: 'light' })
  155. expect(updated.secrets).toEqual([{ path: ['apiKey'], set: true }])
  156. const replaced = await controller.replace('ui-test', {}, updated.revision)
  157. expect(replaced.value).toEqual({ preference: 'light' })
  158. expect(replaced.user).toEqual({})
  159. expect(replaced.secrets).toEqual([{ path: ['apiKey'], set: false }])
  160. })
  161. it('refuses a stale write as settings-conflict carrying both revisions', async () => {
  162. const { controller } = await boot()
  163. const held = controller.describe().namespaces[0]!.revision
  164. await controller.mutate('ui-test', [{ op: 'set', path: ['preference'], value: 'dark' }], held)
  165. const failure = await controller
  166. .mutate('ui-test', [{ op: 'set', path: ['preference'], value: 'light' }], held)
  167. .catch((error: unknown) => error)
  168. expect(failure).toBeInstanceOf(TypertRemoteFailure)
  169. const { code, details } = (failure as TypertRemoteFailure).failure
  170. expect(code).toBe('settings-conflict')
  171. expect(details).toMatchObject({ ns: 'ui-test', expected: held })
  172. })
  173. it('answers a malformed namespace exactly as an unregistered one', async () => {
  174. const { controller } = await boot()
  175. for (const ns of ['Not A Namespace', 'unregistered']) {
  176. const failure = await controller.mutate(ns, [{ op: 'unset', path: ['preference'] }], undefined)
  177. .catch((error: unknown) => error)
  178. expect((failure as TypertRemoteFailure).failure).toMatchObject({
  179. code: 'settings-rejected',
  180. details: { ns },
  181. })
  182. }
  183. })
  184. it('reports an empty namespace as bad-request', async () => {
  185. const { controller } = await boot()
  186. for (const call of [
  187. () => controller.update('', {}, undefined),
  188. () => controller.replace('', {}, undefined),
  189. () => controller.mutate('', [], undefined),
  190. ]) {
  191. const failure = await call().catch((error: unknown) => error)
  192. expect(failure).toBeInstanceOf(TypertRemoteFailure)
  193. expect((failure as TypertRemoteFailure).failure).toMatchObject({ code: 'bad-request' })
  194. }
  195. })
  196. it('reports a refused write as settings-rejected carrying the seam message', async () => {
  197. const { controller } = await boot(RefusingSettings)
  198. const failure = await controller.mutate('ui-test', [{ op: 'unset', path: ['preference'] }], undefined)
  199. .catch((error: unknown) => error)
  200. const { code, message } = (failure as TypertRemoteFailure).failure
  201. expect(code).toBe('settings-rejected')
  202. expect(message).toContain('read-only in this deployment')
  203. })
  204. it('stringifies a refusal that is not an Error', async () => {
  205. const { controller } = await boot(LiteralRefusingSettings)
  206. const failure = await controller.mutate('ui-test', [{ op: 'unset', path: ['preference'] }], undefined)
  207. .catch((error: unknown) => error)
  208. expect((failure as TypertRemoteFailure).failure.message).toBe('the document is locked')
  209. })
  210. it('reports a namespace disposed between the write and its read-back', async () => {
  211. const { controller } = await boot(VanishingSettings)
  212. const failure = await controller.mutate('ui-test', [{ op: 'set', path: ['preference'], value: 'dark' }], undefined)
  213. .catch((error: unknown) => error)
  214. const { code, message } = (failure as TypertRemoteFailure).failure
  215. expect(code).toBe('internal')
  216. expect(message).toContain('was disposed after the mutate')
  217. })
  218. })