1
0

paths.spec.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /** Display, durable, and temporary path rules. */
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { describe, expect, it } from 'vitest'
  5. import { compareDisplay, displayPathOf, durablePathOf, isTemporaryPath, temporaryRoots } from '../src/paths.ts'
  6. const cwd = '/home/u/proj/pkg'
  7. const root = '/home/u/proj'
  8. const home = '/home/u'
  9. describe('displayPathOf', () => {
  10. it('prefers cwd-relative, then repository-relative, then home, then absolute', () => {
  11. expect(displayPathOf('/home/u/proj/pkg/src/a.ts', cwd, root, home)).toBe('src/a.ts')
  12. expect(displayPathOf('/home/u/proj/other/b.ts', cwd, root, home)).toBe('../other/b.ts')
  13. expect(displayPathOf('/home/u/.zshrc', cwd, root, home)).toBe('~/.zshrc')
  14. expect(displayPathOf('/etc/hosts', cwd, root, home)).toBe('/etc/hosts')
  15. expect(displayPathOf('/home/u/.zshrc', cwd, root, '')).toBe('/home/u/.zshrc')
  16. })
  17. })
  18. describe('durablePathOf', () => {
  19. it('keeps cwd-relative paths relative and everything else absolute', () => {
  20. expect(durablePathOf('/home/u/proj/pkg/a.ts', cwd)).toBe('a.ts')
  21. expect(durablePathOf('/home/u/proj/b.ts', cwd)).toBe('/home/u/proj/b.ts')
  22. })
  23. })
  24. describe('temporary paths', () => {
  25. it('matches the platform temp roots in raw and canonical form and skips missing candidates', () => {
  26. const roots = temporaryRoots()
  27. expect(roots).toContain('/tmp')
  28. expect(isTemporaryPath(join(tmpdir(), 'scratch.txt'), roots)).toBe(true)
  29. expect(isTemporaryPath('/tmp/x', roots)).toBe(true)
  30. expect(isTemporaryPath('/tmpfoo/x', roots)).toBe(false)
  31. expect(isTemporaryPath('/home/u/x', roots)).toBe(false)
  32. expect(temporaryRoots(['/definitely/missing/root'])).toEqual(['/definitely/missing/root'])
  33. })
  34. })
  35. describe('compareDisplay', () => {
  36. it('orders by code units so parent and absolute paths lead', () => {
  37. const sorted = [{ display: 'src/b' }, { display: '~/x' }, { display: '../a' }, { display: '/etc/h' }, { display: 'src/a' }].sort(compareDisplay)
  38. expect(sorted.map(file => file.display)).toEqual(['../a', '/etc/h', 'src/a', 'src/b', '~/x'])
  39. expect(compareDisplay({ display: 'a' }, { display: 'a' })).toBe(0)
  40. })
  41. })