|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import * as fs from 'fs'; |
| 3 | + |
| 4 | +vi.mock('fs', async () => { |
| 5 | + const actual = await vi.importActual<typeof import('fs')>('fs'); |
| 6 | + return { |
| 7 | + ...actual, |
| 8 | + existsSync: vi.fn(), |
| 9 | + lstatSync: vi.fn() |
| 10 | + }; |
| 11 | +}); |
| 12 | +vi.mock('../../src/main/config/settings', () => ({ |
| 13 | + userSettings: { |
| 14 | + getValue: vi.fn(() => null), |
| 15 | + setValue: vi.fn() |
| 16 | + }, |
| 17 | + SettingType: { |
| 18 | + condaPath: 'condaPath', |
| 19 | + pythonPath: 'pythonPath', |
| 20 | + envType: 'envType' |
| 21 | + } |
| 22 | +})); |
| 23 | +vi.mock('../../src/main/config/appdata', () => ({ |
| 24 | + appData: { |
| 25 | + discoveredPythonPaths: [], |
| 26 | + condaPath: null |
| 27 | + } |
| 28 | +})); |
| 29 | + |
| 30 | +import { |
| 31 | + environmentSatisfiesRequirements, |
| 32 | + validateCondaChannels, |
| 33 | + validateNewPythonEnvironmentName, |
| 34 | + validatePythonEnvironmentInstallDirectory |
| 35 | +} from '../../src/main/env'; |
| 36 | + |
| 37 | +const mockFs = vi.mocked(fs); |
| 38 | + |
| 39 | +describe('validateNewPythonEnvironmentName', () => { |
| 40 | + beforeEach(() => { |
| 41 | + mockFs.existsSync = vi.fn(() => false); |
| 42 | + }); |
| 43 | + |
| 44 | + it.each([ |
| 45 | + ['valid-name', true], |
| 46 | + ['valid_name_123', true], |
| 47 | + ['MyEnv', true], |
| 48 | + ['', false], |
| 49 | + [' ', false], |
| 50 | + ['name with spaces', false], |
| 51 | + ['name/slash', false], |
| 52 | + ['name.dot', false], |
| 53 | + ['name@at', false] |
| 54 | + ])('"%s" → valid: %s', (name, expected) => { |
| 55 | + expect(validateNewPythonEnvironmentName(name).valid).toBe(expected); |
| 56 | + }); |
| 57 | + |
| 58 | + it('returns invalid when directory already exists', () => { |
| 59 | + mockFs.existsSync = vi.fn(() => true); |
| 60 | + const result = validateNewPythonEnvironmentName('existing-env'); |
| 61 | + expect(result.valid).toBe(false); |
| 62 | + expect(result.message).toMatch(/already exists/i); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('validatePythonEnvironmentInstallDirectory', () => { |
| 67 | + it('returns invalid for non-existent directory', () => { |
| 68 | + mockFs.existsSync = vi.fn(() => false); |
| 69 | + const result = validatePythonEnvironmentInstallDirectory('/nonexistent'); |
| 70 | + expect(result.valid).toBe(false); |
| 71 | + expect(result.message).toMatch(/does not exist/i); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns invalid for a file (not directory)', () => { |
| 75 | + mockFs.existsSync = vi.fn(() => true); |
| 76 | + mockFs.lstatSync = vi.fn(() => ({ isDirectory: () => false } as fs.Stats)); |
| 77 | + const result = validatePythonEnvironmentInstallDirectory('/some/file.txt'); |
| 78 | + expect(result.valid).toBe(false); |
| 79 | + expect(result.message).toMatch(/not a directory/i); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns valid for existing directory', () => { |
| 83 | + mockFs.existsSync = vi.fn(() => true); |
| 84 | + mockFs.lstatSync = vi.fn(() => ({ isDirectory: () => true } as fs.Stats)); |
| 85 | + const result = validatePythonEnvironmentInstallDirectory('/valid/dir'); |
| 86 | + expect(result.valid).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it('returns invalid on fs error', () => { |
| 90 | + mockFs.existsSync = vi.fn(() => { |
| 91 | + throw new Error('permission denied'); |
| 92 | + }); |
| 93 | + const result = validatePythonEnvironmentInstallDirectory('/bad/path'); |
| 94 | + expect(result.valid).toBe(false); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('environmentSatisfiesRequirements', () => { |
| 99 | + it('returns true when jupyterlab version satisfies minimum', () => { |
| 100 | + const env = { |
| 101 | + name: 'test', |
| 102 | + path: '/env', |
| 103 | + versions: { jupyterlab: '4.4.7' } |
| 104 | + } as any; |
| 105 | + expect(environmentSatisfiesRequirements(env)).toBe(true); |
| 106 | + }); |
| 107 | + |
| 108 | + it('returns false when jupyterlab version too old', () => { |
| 109 | + const env = { |
| 110 | + name: 'test', |
| 111 | + path: '/env', |
| 112 | + versions: { jupyterlab: '2.9.9' } |
| 113 | + } as any; |
| 114 | + expect(environmentSatisfiesRequirements(env)).toBe(false); |
| 115 | + }); |
| 116 | + |
| 117 | + it('returns false when version missing', () => { |
| 118 | + const env = { |
| 119 | + name: 'test', |
| 120 | + path: '/env', |
| 121 | + versions: {} |
| 122 | + } as any; |
| 123 | + expect(environmentSatisfiesRequirements(env)).toBe(false); |
| 124 | + }); |
| 125 | + |
| 126 | + it('uses custom requirements when provided', () => { |
| 127 | + const env = { |
| 128 | + name: 'test', |
| 129 | + path: '/env', |
| 130 | + versions: { mylib: '2.0.0' } |
| 131 | + } as any; |
| 132 | + const reqs = [ |
| 133 | + { |
| 134 | + name: 'mylib', |
| 135 | + moduleName: 'mylib', |
| 136 | + commands: ['--version'], |
| 137 | + versionRange: new (require('semver').Range)('^2.0.0'), |
| 138 | + pipCommand: 'mylib', |
| 139 | + condaCommand: 'mylib' |
| 140 | + } |
| 141 | + ]; |
| 142 | + expect(environmentSatisfiesRequirements(env, reqs)).toBe(true); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +describe('validateCondaChannels', () => { |
| 147 | + it('returns valid for standard channel string', () => { |
| 148 | + expect(validateCondaChannels('conda-forge defaults').valid).toBe(true); |
| 149 | + }); |
| 150 | + |
| 151 | + it('returns valid for empty string (no extra channels)', () => { |
| 152 | + expect(validateCondaChannels('').valid).toBe(true); |
| 153 | + }); |
| 154 | + |
| 155 | + it('returns invalid for channels with special chars', () => { |
| 156 | + const result = validateCondaChannels('conda-forge; rm -rf /'); |
| 157 | + expect(result.valid).toBe(false); |
| 158 | + }); |
| 159 | +}); |
0 commit comments