paths.spec.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { homedir } from 'node:os'
  2. import { join, resolve } from 'node:path'
  3. import { describe, expect, it } from 'vitest'
  4. import {
  5. DEFAULT_DSH_HOME_DISPLAY,
  6. DSH_HOME_DIR_NAME,
  7. SESSIONS_DIR_NAME,
  8. defaultDshHome,
  9. dshHomeDisplay,
  10. expandHomePath,
  11. resolveDshHome,
  12. resolveSessionsRoot,
  13. } from '@deepseek-ai/dsh-paths'
  14. describe('dsh path helpers', () => {
  15. it('owns the shared default DSH home directory name', () => {
  16. expect(DSH_HOME_DIR_NAME).toBe('.dsh')
  17. expect(DEFAULT_DSH_HOME_DISPLAY).toBe('~/.dsh')
  18. expect(defaultDshHome()).toBe(join(homedir(), '.dsh'))
  19. })
  20. it('expands tilde paths without changing non-tilde paths', () => {
  21. expect(expandHomePath('~')).toBe(homedir())
  22. expect(expandHomePath('~/.dsh')).toBe(join(homedir(), '.dsh'))
  23. expect(expandHomePath('~\\.dsh')).toBe(join(homedir(), '.dsh'))
  24. expect(expandHomePath('/tmp/.dsh')).toBe('/tmp/.dsh')
  25. expect(expandHomePath('~other/.dsh')).toBe('~other/.dsh')
  26. })
  27. it('resolves explicit path before DSH_HOME and the default', () => {
  28. const envHome = join(homedir(), 'env-dsh')
  29. expect(resolveDshHome('/tmp/explicit-dsh', { DSH_HOME: '~/env-dsh' })).toBe(resolve('/tmp/explicit-dsh'))
  30. expect(resolveDshHome(undefined, { DSH_HOME: '~/env-dsh' })).toBe(envHome)
  31. expect(resolveDshHome(undefined, {})).toBe(defaultDshHome())
  32. })
  33. it('treats an empty or whitespace-only DSH_HOME as unset', () => {
  34. expect(resolveDshHome(undefined, { DSH_HOME: '' })).toBe(defaultDshHome())
  35. expect(resolveDshHome(undefined, { DSH_HOME: ' ' })).toBe(defaultDshHome())
  36. })
  37. it('resolves the session store under the home it was given, by the same precedence', () => {
  38. expect(SESSIONS_DIR_NAME).toBe('sessions')
  39. expect(resolveSessionsRoot('/tmp/explicit-dsh', { DSH_HOME: '~/env-dsh' }))
  40. .toBe(join(resolve('/tmp/explicit-dsh'), 'sessions'))
  41. expect(resolveSessionsRoot(undefined, { DSH_HOME: '~/env-dsh' }))
  42. .toBe(join(homedir(), 'env-dsh', 'sessions'))
  43. expect(resolveSessionsRoot(undefined, {})).toBe(join(defaultDshHome(), 'sessions'))
  44. })
  45. it('labels a resolved home by whether it is the default root', () => {
  46. expect(dshHomeDisplay(resolve(defaultDshHome()))).toBe('~/.dsh')
  47. expect(dshHomeDisplay('/some/other/root')).toBe('$DSH_HOME')
  48. })
  49. })