|
| 1 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 2 | +import { getCriticalSystemPaths, isCriticalSystemDirectory, isUserHomeDirectory } from '../safe-paths.js'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | + |
| 6 | +describe('safe-paths', () => { |
| 7 | + const originalPlatform = process.platform; |
| 8 | + |
| 9 | + afterEach(() => { |
| 10 | + Object.defineProperty(process, 'platform', { |
| 11 | + value: originalPlatform |
| 12 | + }); |
| 13 | + }); |
| 14 | + |
| 15 | + describe('getCriticalSystemPaths', () => { |
| 16 | + it('returns Windows paths on win32', () => { |
| 17 | + Object.defineProperty(process, 'platform', { |
| 18 | + value: 'win32' |
| 19 | + }); |
| 20 | + |
| 21 | + const paths = getCriticalSystemPaths(); |
| 22 | + |
| 23 | + expect(paths).toContain('C:\\'); |
| 24 | + expect(paths.some(p => p.includes('Windows'))).toBe(true); |
| 25 | + expect(paths.some(p => p.includes('Program Files'))).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + it('returns macOS paths on darwin', () => { |
| 29 | + Object.defineProperty(process, 'platform', { |
| 30 | + value: 'darwin' |
| 31 | + }); |
| 32 | + |
| 33 | + const paths = getCriticalSystemPaths(); |
| 34 | + |
| 35 | + expect(paths).toContain('/'); |
| 36 | + expect(paths).toContain('/System'); |
| 37 | + expect(paths).toContain('/Library'); |
| 38 | + expect(paths).toContain('/Applications'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns Linux paths on linux', () => { |
| 42 | + Object.defineProperty(process, 'platform', { |
| 43 | + value: 'linux' |
| 44 | + }); |
| 45 | + |
| 46 | + const paths = getCriticalSystemPaths(); |
| 47 | + |
| 48 | + expect(paths).toContain('/'); |
| 49 | + expect(paths).toContain('/usr'); |
| 50 | + expect(paths).toContain('/etc'); |
| 51 | + expect(paths).toContain('/var'); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('isCriticalSystemDirectory', () => { |
| 56 | + it('detects root directory as critical', () => { |
| 57 | + const isRootCritical = isCriticalSystemDirectory('/'); |
| 58 | + expect(isRootCritical).toBe(true); |
| 59 | + }); |
| 60 | + |
| 61 | + it('detects subdirectories of critical paths', () => { |
| 62 | + if (process.platform === 'darwin' || process.platform === 'linux') { |
| 63 | + expect(isCriticalSystemDirectory('/usr/local')).toBe(true); |
| 64 | + expect(isCriticalSystemDirectory('/etc/config')).toBe(true); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + it('allows safe project directories', () => { |
| 69 | + const homeDir = os.homedir(); |
| 70 | + const projectDir = path.join(homeDir, 'projects', 'my-app'); |
| 71 | + |
| 72 | + expect(isCriticalSystemDirectory(projectDir)).toBe(false); |
| 73 | + }); |
| 74 | + |
| 75 | + it('normalizes paths correctly', () => { |
| 76 | + if (process.platform === 'darwin' || process.platform === 'linux') { |
| 77 | + expect(isCriticalSystemDirectory('/usr/../usr')).toBe(true); |
| 78 | + } |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('isUserHomeDirectory', () => { |
| 83 | + it('detects user home directory', () => { |
| 84 | + const homeDir = os.homedir(); |
| 85 | + expect(isUserHomeDirectory(homeDir)).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + it('allows subdirectories of home', () => { |
| 89 | + const homeDir = os.homedir(); |
| 90 | + const subDir = path.join(homeDir, 'projects'); |
| 91 | + |
| 92 | + expect(isUserHomeDirectory(subDir)).toBe(false); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('cross-platform compatibility', () => { |
| 97 | + it('handles paths with different separators', () => { |
| 98 | + const userDir = path.join(os.homedir(), 'projects', 'app'); |
| 99 | + |
| 100 | + const result = isCriticalSystemDirectory(userDir); |
| 101 | + |
| 102 | + expect(typeof result).toBe('boolean'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('resolves relative paths', () => { |
| 106 | + const relativePath = './some/project'; |
| 107 | + const resolvedCheck = isCriticalSystemDirectory(relativePath); |
| 108 | + |
| 109 | + expect(typeof resolvedCheck).toBe('boolean'); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
0 commit comments