bash-env.spec.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { homedir } from 'node:os'
  2. import { join, resolve } from 'node:path'
  3. import { afterEach, describe, expect, it, vi } from 'vitest'
  4. import { Context } from 'cordis'
  5. import { CallId } from '@deepseek-ai/dsh-llm'
  6. import type { Agent } from '@deepseek-ai/dsh-agent'
  7. import type { ToolExecution } from '@deepseek-ai/dsh-tools'
  8. import { BashEnvRegistry } from '@deepseek-ai/dsh-tool-bash'
  9. afterEach(() => vi.unstubAllEnvs())
  10. function execution(sessionId?: string): ToolExecution {
  11. return {
  12. callId: CallId('bash-env-call'),
  13. name: 'bash',
  14. arguments: { command: 'true' },
  15. ...(sessionId === undefined
  16. ? {}
  17. : { agent: { session: { header: { version: 0, id: sessionId, createdAt: 0 } } } as Agent }),
  18. }
  19. }
  20. describe('BashEnvRegistry', () => {
  21. it('collects unconditional shell facts and the current agent session id', () => {
  22. const ctx = new Context()
  23. const registry = new BashEnvRegistry(ctx, { dshHome: './test-dsh-home' })
  24. expect(registry.collect(execution())).toEqual({
  25. DSH_HOME: resolve('./test-dsh-home'),
  26. DSH_SHELL: '1',
  27. })
  28. expect(registry.collect(execution('session-a'))).toEqual({
  29. DSH_HOME: resolve('./test-dsh-home'),
  30. DSH_SESSION_ID: 'session-a',
  31. DSH_SHELL: '1',
  32. })
  33. })
  34. it('resolves DSH_HOME from the ambient override or the user-home default', () => {
  35. vi.stubEnv('DSH_HOME', './ambient-dsh-home')
  36. const fromEnvironment = new BashEnvRegistry(new Context())
  37. expect(fromEnvironment.collect(execution()).DSH_HOME).toBe(resolve('./ambient-dsh-home'))
  38. vi.stubEnv('DSH_HOME', undefined)
  39. const fromDefault = new BashEnvRegistry(new Context())
  40. expect(fromDefault.collect(execution()).DSH_HOME).toBe(join(homedir(), '.dsh'))
  41. })
  42. it('collects declared contributor variables and omits unavailable values', () => {
  43. const ctx = new Context()
  44. const registry = new BashEnvRegistry(ctx, { dshHome: './test-dsh-home' })
  45. registry.register({
  46. name: 'optional-session-fact',
  47. variables: {
  48. DSH_SESSION_OPTIONAL: { description: 'Optional session-scoped test fact.' },
  49. },
  50. resolve: exec => exec.agent === undefined ? {} : { DSH_SESSION_OPTIONAL: exec.agent.session.header.id },
  51. })
  52. registry.register({
  53. name: 'always-available-fact',
  54. variables: {
  55. DSH_ALWAYS_AVAILABLE: { description: 'Always-available test fact.' },
  56. },
  57. resolve: () => ({ DSH_ALWAYS_AVAILABLE: 'yes' }),
  58. })
  59. expect(registry.collect(execution())).not.toHaveProperty('DSH_SESSION_OPTIONAL')
  60. expect(registry.collect(execution()).DSH_ALWAYS_AVAILABLE).toBe('yes')
  61. expect(registry.collect(execution('session-b')).DSH_SESSION_OPTIONAL).toBe('session-b')
  62. expect(registry.list()).toEqual([
  63. {
  64. contributor: 'always-available-fact',
  65. description: 'Always-available test fact.',
  66. key: 'DSH_ALWAYS_AVAILABLE',
  67. },
  68. {
  69. contributor: 'optional-session-fact',
  70. description: 'Optional session-scoped test fact.',
  71. key: 'DSH_SESSION_OPTIONAL',
  72. },
  73. ])
  74. })
  75. it('rejects duplicate variable ownership at registration time', () => {
  76. const ctx = new Context()
  77. const registry = new BashEnvRegistry(ctx, { dshHome: './test-dsh-home' })
  78. registry.register({
  79. name: 'first',
  80. variables: { DSH_SHARED: { description: 'First owner.' } },
  81. resolve: () => ({ DSH_SHARED: 'first' }),
  82. })
  83. expect(() => registry.register({
  84. name: 'second',
  85. variables: { DSH_SHARED: { description: 'Second owner.' } },
  86. resolve: () => ({ DSH_SHARED: 'second' }),
  87. })).toThrow(/DSH_SHARED.*first.*second|DSH_SHARED.*second.*first/)
  88. })
  89. it('rejects duplicate contributor names and malformed declarations', () => {
  90. const registry = new BashEnvRegistry(new Context(), { dshHome: './test-dsh-home' })
  91. registry.register({
  92. name: 'declared',
  93. variables: { DSH_DECLARED: { description: 'Declared fact.' } },
  94. resolve: () => ({}),
  95. })
  96. expect(() => registry.register({
  97. name: 'declared',
  98. variables: { DSH_ANOTHER: { description: 'Another fact.' } },
  99. resolve: () => ({}),
  100. })).toThrow(/already registered/)
  101. expect(() => registry.register({
  102. name: ' ',
  103. variables: { DSH_BLANK_NAME: { description: 'Blank owner.' } },
  104. resolve: () => ({}),
  105. })).toThrow(/name must be non-empty/)
  106. expect(() => registry.register({
  107. name: 'invalid-key',
  108. variables: { dsh_invalid: { description: 'Invalid key.' } } as unknown as Record<'DSH_INVALID', { description: string }>,
  109. resolve: () => ({}),
  110. })).toThrow(/invalid key/)
  111. expect(() => registry.register({
  112. name: 'reserved-key',
  113. variables: { DSH_HOME: { description: 'Reserved key.' } },
  114. resolve: () => ({}),
  115. })).toThrow(/reserved key/)
  116. expect(() => registry.register({
  117. name: 'blank-description',
  118. variables: { DSH_BLANK_DESCRIPTION: { description: ' ' } },
  119. resolve: () => ({}),
  120. })).toThrow(/must describe/)
  121. })
  122. it('rejects undeclared variables returned by a contributor', () => {
  123. const ctx = new Context()
  124. const registry = new BashEnvRegistry(ctx, { dshHome: './test-dsh-home' })
  125. registry.register({
  126. name: 'drifted-provider',
  127. variables: { DSH_DECLARED: { description: 'Declared fact.' } },
  128. resolve: () => ({ DSH_UNDECLARED: 'bad' }),
  129. })
  130. expect(() => registry.collect(execution())).toThrow(/drifted-provider.*DSH_UNDECLARED/)
  131. })
  132. it('rejects non-string values returned by a contributor', () => {
  133. const registry = new BashEnvRegistry(new Context(), { dshHome: './test-dsh-home' })
  134. registry.register({
  135. name: 'wrong-value-type',
  136. variables: { DSH_STRING: { description: 'String fact.' } },
  137. resolve: () => ({ DSH_STRING: 42 }) as unknown as Record<'DSH_STRING', string>,
  138. })
  139. expect(() => registry.collect(execution())).toThrow(/wrong-value-type.*non-string.*DSH_STRING/)
  140. })
  141. it('removes an effect-scoped contributor when its plugin is disposed', async () => {
  142. const ctx = new Context()
  143. const registry = new BashEnvRegistry(ctx, { dshHome: './test-dsh-home' })
  144. const fiber = await ctx.plugin({
  145. inject: ['bashEnv'],
  146. apply(inner: Context) {
  147. inner.bashEnv.register({
  148. name: 'temporary',
  149. variables: { DSH_TEMPORARY: { description: 'Temporary fact.' } },
  150. resolve: () => ({ DSH_TEMPORARY: 'present' }),
  151. })
  152. },
  153. })
  154. expect(registry.collect(execution()).DSH_TEMPORARY).toBe('present')
  155. await fiber.dispose()
  156. expect(registry.collect(execution())).not.toHaveProperty('DSH_TEMPORARY')
  157. })
  158. it('returns an explicit contributor disposer', () => {
  159. const registry = new BashEnvRegistry(new Context(), { dshHome: './test-dsh-home' })
  160. const dispose = registry.register({
  161. name: 'explicit-disposal',
  162. variables: { DSH_EXPLICIT_DISPOSAL: { description: 'Explicitly disposed fact.' } },
  163. resolve: () => ({ DSH_EXPLICIT_DISPOSAL: 'present' }),
  164. })
  165. expect(registry.collect(execution()).DSH_EXPLICIT_DISPOSAL).toBe('present')
  166. dispose()
  167. expect(registry.collect(execution())).not.toHaveProperty('DSH_EXPLICIT_DISPOSAL')
  168. })
  169. })