paths.spec.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { mkdir, mkdtemp, realpath, rm, symlink, writeFile } from 'node:fs/promises'
  2. import { homedir, tmpdir } from 'node:os'
  3. import { join, resolve } from 'node:path'
  4. import { afterEach, describe, expect, it, vi } from 'vitest'
  5. import {
  6. DEFAULT_DSH_HOME_DISPLAY,
  7. DSH_HOME_DIR_NAME,
  8. canonicalizeWatchPath,
  9. defaultDshHome,
  10. dshHomeDisplay,
  11. dshHomePath,
  12. expandHomePath,
  13. resolveDshHome,
  14. } from '@deepseek-ai/dsh-paths'
  15. afterEach(() => {
  16. vi.unstubAllEnvs()
  17. })
  18. describe('dsh path helpers', () => {
  19. it('owns the shared default DSH home directory name', () => {
  20. expect(DSH_HOME_DIR_NAME).toBe('.dsh')
  21. expect(DEFAULT_DSH_HOME_DISPLAY).toBe('~/.dsh')
  22. expect(defaultDshHome()).toBe(join(homedir(), '.dsh'))
  23. })
  24. it('expands tilde paths without changing non-tilde paths', () => {
  25. expect(expandHomePath('~')).toBe(homedir())
  26. expect(expandHomePath('~/.dsh')).toBe(join(homedir(), '.dsh'))
  27. expect(expandHomePath('~\\.dsh')).toBe(join(homedir(), '.dsh'))
  28. expect(expandHomePath('/tmp/.dsh')).toBe('/tmp/.dsh')
  29. expect(expandHomePath('~other/.dsh')).toBe('~other/.dsh')
  30. })
  31. it('resolves explicit path before DSH_HOME and the default', () => {
  32. const envHome = join(homedir(), 'env-dsh')
  33. expect(resolveDshHome('/tmp/explicit-dsh', { DSH_HOME: '~/env-dsh' })).toBe(resolve('/tmp/explicit-dsh'))
  34. expect(resolveDshHome(undefined, { DSH_HOME: '~/env-dsh' })).toBe(envHome)
  35. expect(resolveDshHome(undefined, {})).toBe(defaultDshHome())
  36. })
  37. it('treats an empty or whitespace-only DSH_HOME as unset', () => {
  38. expect(resolveDshHome(undefined, { DSH_HOME: '' })).toBe(defaultDshHome())
  39. expect(resolveDshHome(undefined, { DSH_HOME: ' ' })).toBe(defaultDshHome())
  40. })
  41. it('joins child segments onto the resolved DSH_HOME', () => {
  42. vi.stubEnv('DSH_HOME', '~/env-dsh')
  43. expect(dshHomePath()).toBe(join(homedir(), 'env-dsh'))
  44. expect(dshHomePath('storages', 'cache')).toBe(join(homedir(), 'env-dsh', 'storages', 'cache'))
  45. })
  46. it('labels a resolved home by whether it is the default root', () => {
  47. expect(dshHomeDisplay(resolve(defaultDshHome()))).toBe('~/.dsh')
  48. expect(dshHomeDisplay('/some/other/root')).toBe('$DSH_HOME')
  49. })
  50. it('canonicalizes a watcher ancestor while preserving a missing suffix', async () => {
  51. const root = await mkdtemp(join(tmpdir(), 'dsh-watch-path-'))
  52. const target = join(root, 'target')
  53. const alias = join(root, 'alias')
  54. try {
  55. await mkdir(target)
  56. await symlink(target, alias, process.platform === 'win32' ? 'junction' : 'dir')
  57. await expect(canonicalizeWatchPath(join(alias, 'later', 'config.yml'))).resolves.toBe(
  58. join(await realpath(target), 'later', 'config.yml'),
  59. )
  60. const file = join(root, 'file')
  61. await writeFile(file, 'not a directory')
  62. await expect(canonicalizeWatchPath(join(file, 'child'))).rejects.toMatchObject({ code: 'ENOTDIR' })
  63. } finally {
  64. await rm(root, { recursive: true, force: true })
  65. }
  66. })
  67. })