settings.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /** The `bash` settings section layered over the executor's composition entry. */
  2. import { describe, expect, it } from 'vitest'
  3. import { Context } from '@deepseek-ai/cordis'
  4. import type { Fiber } from '@deepseek-ai/cordis'
  5. import { SettingsProvider } from '@deepseek-ai/dsh-settings'
  6. import type { SettingsNamespace } from '@deepseek-ai/dsh-settings'
  7. import LocalSubprocessRuntime from '@deepseek-ai/dsh-subprocess-local'
  8. import { SHELL_SETTINGS_NAMESPACE } from '@deepseek-ai/dsh-shell'
  9. import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local'
  10. /** The smallest real provider: one in-memory document, always writable. */
  11. class MemorySettings extends SettingsProvider {
  12. doc: Record<string, unknown> = {}
  13. get writable(): boolean {
  14. return true
  15. }
  16. protected load(): Promise<Record<string, unknown>> {
  17. return Promise.resolve(structuredClone(this.doc))
  18. }
  19. protected persist(ns: SettingsNamespace, section: Record<string, unknown>): Promise<void> {
  20. this.doc = { ...this.doc, [ns]: structuredClone(section) }
  21. return Promise.resolve()
  22. }
  23. }
  24. async function boot(config: ConstructorParameters<typeof LocalBashExecutor>[1] = {}): Promise<{
  25. ctx: Context
  26. settingsFiber: Fiber
  27. executorFiber: Fiber
  28. bash: LocalBashExecutor
  29. }> {
  30. const ctx = new Context()
  31. await ctx.plugin(LocalSubprocessRuntime)
  32. const settingsFiber = ctx.plugin(MemorySettings)
  33. await settingsFiber.await()
  34. const executorFiber = ctx.plugin(LocalBashExecutor, { timeoutMs: 60_000, ...config })
  35. await executorFiber.await()
  36. return { ctx, settingsFiber, executorFiber, bash: ctx.shell as LocalBashExecutor }
  37. }
  38. describe('bash settings section', () => {
  39. it('resolves the user layer over the composition entry', async () => {
  40. const bench = await boot()
  41. expect(bench.bash.config.timeoutMs).toBe(60_000)
  42. await bench.ctx.settings.update(SHELL_SETTINGS_NAMESPACE, { timeoutMs: 5_000 })
  43. expect(bench.bash.config.timeoutMs).toBe(5_000)
  44. await bench.ctx.fiber.dispose()
  45. })
  46. it('refuses a stored value the constructor would have rejected', async () => {
  47. const bench = await boot()
  48. await expect(bench.ctx.settings.update(SHELL_SETTINGS_NAMESPACE, { timeoutMs: 0 }))
  49. .rejects.toThrow(/positive finite/)
  50. expect(bench.bash.config.timeoutMs).toBe(60_000)
  51. await bench.ctx.fiber.dispose()
  52. })
  53. it('refuses a grace period longer than a timer can carry', async () => {
  54. const bench = await boot()
  55. await expect(bench.ctx.settings.update(SHELL_SETTINGS_NAMESPACE, { graceMs: Number.MAX_SAFE_INTEGER }))
  56. .rejects.toThrow(/graceMs must be no greater than/)
  57. await bench.ctx.fiber.dispose()
  58. })
  59. it('serves the stored section to every later read', async () => {
  60. const bench = await boot()
  61. await bench.ctx.settings.update(SHELL_SETTINGS_NAMESPACE, { maxOutputBytes: 1_024, cwd: '/tmp' })
  62. const spec = bench.bash.resolve({ command: 'true' })
  63. expect(spec.stdoutMaxBytes).toBe(1_024)
  64. expect(spec.workdir).toBe('/tmp')
  65. await bench.ctx.fiber.dispose()
  66. })
  67. it('falls back to the composition entry when the settings provider detaches', async () => {
  68. const bench = await boot()
  69. await bench.ctx.settings.update(SHELL_SETTINGS_NAMESPACE, { timeoutMs: 5_000 })
  70. expect(bench.bash.config.timeoutMs).toBe(5_000)
  71. await bench.settingsFiber.dispose()
  72. expect(bench.bash.config.timeoutMs).toBe(60_000)
  73. await bench.ctx.fiber.dispose()
  74. })
  75. it('keeps the composition entry when no settings provider is mounted', async () => {
  76. const ctx = new Context()
  77. await ctx.plugin(LocalSubprocessRuntime)
  78. await ctx.plugin(LocalBashExecutor, { timeoutMs: 1_234 })
  79. expect((ctx.shell as LocalBashExecutor).config.timeoutMs).toBe(1_234)
  80. await ctx.fiber.dispose()
  81. })
  82. it('releases the namespace when the executor unloads', async () => {
  83. const bench = await boot()
  84. expect(bench.ctx.settings.describe().map(row => String(row.ns))).toContain('shell')
  85. await bench.executorFiber.dispose()
  86. expect(bench.ctx.settings.describe().map(row => String(row.ns))).not.toContain('shell')
  87. await bench.ctx.fiber.dispose()
  88. })
  89. })