launch-environment.spec.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { describe, expect, it, vi } from 'vitest'
  2. import { Context } from '@deepseek-ai/cordis'
  3. import {
  4. createLaunchEnvironmentSnapshot, DSH_LAUNCH_ENVIRONMENT_KEY, launchEnvironmentOf,
  5. } from '../src/index.ts'
  6. const layered = createLaunchEnvironmentSnapshot([
  7. { source: 'process', values: { SHARED: 'from-process', ONLY_PROCESS: 'p' } },
  8. { source: 'project-env', path: '/work/.env', values: { SHARED: 'from-project', ONLY_PROJECT: 'j' } },
  9. { source: 'user-env', path: '/home/.dsh/.env', values: { SHARED: 'from-user', ONLY_USER: 'u' } },
  10. ])
  11. describe('createLaunchEnvironmentSnapshot', () => {
  12. it('resolves across every layer, most trusted first, and reports the winning source', () => {
  13. expect(layered.get('SHARED')).toEqual({ value: 'from-process', source: 'process' })
  14. expect(layered.get('ONLY_PROJECT')).toEqual({ value: 'j', source: 'project-env', path: '/work/.env' })
  15. expect(layered.get('ONLY_USER')).toEqual({ value: 'u', source: 'user-env', path: '/home/.dsh/.env' })
  16. expect(layered.get('ABSENT')).toBeUndefined()
  17. })
  18. it('filters layers without changing their trust order', () => {
  19. // The point of getFrom: a routing field that must never come from a
  20. // project directory cannot be reached by reordering, only by listing it.
  21. expect(layered.getFrom('ONLY_PROJECT', ['process', 'user-env'])).toBeUndefined()
  22. expect(layered.getFrom('SHARED', ['user-env', 'process']))
  23. .toEqual({ value: 'from-process', source: 'process' })
  24. expect(layered.getFrom('SHARED', [])).toBeUndefined()
  25. })
  26. it('copies each layer, so a later mutation of the source object cannot change it', () => {
  27. const values: Record<string, string> = { KEY: 'first' }
  28. const snapshot = createLaunchEnvironmentSnapshot([{ source: 'process', values }])
  29. values.KEY = 'second'
  30. values.LATE = 'added'
  31. expect(snapshot.get('KEY')).toEqual({ value: 'first', source: 'process' })
  32. expect(snapshot.get('LATE')).toBeUndefined()
  33. })
  34. it('keeps an empty value as a present value, for its owner to judge', () => {
  35. const snapshot = createLaunchEnvironmentSnapshot([{ source: 'process', values: { EMPTY: '' } }])
  36. expect(snapshot.get('EMPTY')).toEqual({ value: '', source: 'process' })
  37. })
  38. it('orders lookups canonically regardless of construction order', () => {
  39. const reversed = createLaunchEnvironmentSnapshot([
  40. { source: 'user-env', path: '/u', values: { K: 'u' } },
  41. { source: 'process', values: { K: 'p' } },
  42. ])
  43. expect(reversed.get('K')).toEqual({ value: 'p', source: 'process' })
  44. })
  45. })
  46. describe('launchEnvironmentOf', () => {
  47. it('returns the launcher snapshot when the product CLI provided one', () => {
  48. const ctx = new Context()
  49. ctx.provide(DSH_LAUNCH_ENVIRONMENT_KEY, layered)
  50. expect(launchEnvironmentOf(ctx)).toBe(layered)
  51. })
  52. it('falls back to the inherited environment as the only layer', () => {
  53. vi.stubEnv('DSH_ENV_SPEC_FALLBACK', 'ambient')
  54. try {
  55. const snapshot = launchEnvironmentOf(new Context())
  56. expect(snapshot.get('DSH_ENV_SPEC_FALLBACK')).toEqual({ value: 'ambient', source: 'process' })
  57. } finally {
  58. vi.unstubAllEnvs()
  59. }
  60. })
  61. })