|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +// Mutable state read by the hoisted vi.mock factories below. Each test mutates |
| 4 | +// it and then `vi.resetModules() + dynamic import` re-evaluates pythonSetup.js |
| 5 | +// with the new platform/arch — `HOST_ARCH`, `IS_DARWIN`, and `PYTHON_CANDIDATES` |
| 6 | +// are all computed at module-load time, so the only way to exercise both |
| 7 | +// arm64 and x86_64 host paths is a fresh module per scenario. |
| 8 | +const mockState = { |
| 9 | + arch: 'arm64', |
| 10 | + platform: 'darwin', |
| 11 | + homedir: '/Users/test', |
| 12 | + presentPaths: new Set(), |
| 13 | + archByPath: new Map(), |
| 14 | + execShouldFail: false, |
| 15 | +}; |
| 16 | + |
| 17 | +vi.mock('node:os', async () => { |
| 18 | + const actual = await vi.importActual('node:os'); |
| 19 | + return { |
| 20 | + ...actual, |
| 21 | + arch: () => mockState.arch, |
| 22 | + platform: () => mockState.platform, |
| 23 | + homedir: () => mockState.homedir, |
| 24 | + }; |
| 25 | +}); |
| 26 | + |
| 27 | +vi.mock('node:fs', async () => { |
| 28 | + const actual = await vi.importActual('node:fs'); |
| 29 | + return { ...actual, existsSync: (p) => mockState.presentPaths.has(p) }; |
| 30 | +}); |
| 31 | + |
| 32 | +vi.mock('node:child_process', async () => { |
| 33 | + const actual = await vi.importActual('node:child_process'); |
| 34 | + const util = await vi.importActual('node:util'); |
| 35 | + // `promisify(execFile)` uses execFile's util.promisify.custom symbol so the |
| 36 | + // resolved value is `{ stdout, stderr }` (not a bare string). Honor that on |
| 37 | + // the mock or `probePythonArch`'s `.stdout` read returns undefined. |
| 38 | + const fakeExecFile = () => {}; |
| 39 | + fakeExecFile[util.promisify.custom] = (bin, args) => new Promise((resolve, reject) => { |
| 40 | + if (mockState.execShouldFail) { |
| 41 | + reject(new Error('spawn failed')); |
| 42 | + return; |
| 43 | + } |
| 44 | + const probeArg = args?.[1] || ''; |
| 45 | + if (probeArg.includes('platform.machine')) { |
| 46 | + const a = mockState.archByPath.get(bin) ?? mockState.arch; |
| 47 | + resolve({ stdout: `${a}\n`, stderr: '' }); |
| 48 | + } else { |
| 49 | + resolve({ stdout: '', stderr: '' }); |
| 50 | + } |
| 51 | + }); |
| 52 | + return { ...actual, execFile: fakeExecFile }; |
| 53 | +}); |
| 54 | + |
| 55 | +vi.mock('./fileUtils.js', () => ({ PATHS: { data: '/data' } })); |
| 56 | + |
| 57 | +const loadModule = async () => { |
| 58 | + vi.resetModules(); |
| 59 | + return await import('./pythonSetup.js'); |
| 60 | +}; |
| 61 | + |
| 62 | +const resetState = () => { |
| 63 | + mockState.arch = 'arm64'; |
| 64 | + mockState.platform = 'darwin'; |
| 65 | + mockState.homedir = '/Users/test'; |
| 66 | + mockState.presentPaths = new Set(); |
| 67 | + mockState.archByPath = new Map(); |
| 68 | + mockState.execShouldFail = false; |
| 69 | +}; |
| 70 | + |
| 71 | +describe('HOST_ARCH', () => { |
| 72 | + beforeEach(resetState); |
| 73 | + |
| 74 | + it('reports arm64 on Apple Silicon Node', async () => { |
| 75 | + mockState.arch = 'arm64'; |
| 76 | + const { HOST_ARCH } = await loadModule(); |
| 77 | + expect(HOST_ARCH).toBe('arm64'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('normalizes os.arch x64 → x86_64 so the Python convention matches', async () => { |
| 81 | + mockState.arch = 'x64'; |
| 82 | + const { HOST_ARCH } = await loadModule(); |
| 83 | + expect(HOST_ARCH).toBe('x86_64'); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +describe('probePythonArch', () => { |
| 88 | + beforeEach(resetState); |
| 89 | + |
| 90 | + it('returns the trimmed platform.machine() output', async () => { |
| 91 | + mockState.archByPath.set('/x/python3', 'arm64'); |
| 92 | + const { probePythonArch } = await loadModule(); |
| 93 | + await expect(probePythonArch('/x/python3')).resolves.toBe('arm64'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('returns null when the subprocess errors', async () => { |
| 97 | + mockState.execShouldFail = true; |
| 98 | + const { probePythonArch } = await loadModule(); |
| 99 | + await expect(probePythonArch('/missing/python3')).resolves.toBeNull(); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('isArchMismatch', () => { |
| 104 | + beforeEach(resetState); |
| 105 | + |
| 106 | + it('returns false on non-darwin (mlx wheels are arm64-only on macOS; other OSes are out of scope)', async () => { |
| 107 | + mockState.platform = 'linux'; |
| 108 | + mockState.archByPath.set('/x/python3', 'x86_64'); |
| 109 | + const { isArchMismatch } = await loadModule(); |
| 110 | + await expect(isArchMismatch('/x/python3')).resolves.toBe(false); |
| 111 | + }); |
| 112 | + |
| 113 | + it('returns false when interpreter arch matches HOST_ARCH on darwin', async () => { |
| 114 | + mockState.archByPath.set('/x/python3', 'arm64'); |
| 115 | + const { isArchMismatch } = await loadModule(); |
| 116 | + await expect(isArchMismatch('/x/python3')).resolves.toBe(false); |
| 117 | + }); |
| 118 | + |
| 119 | + it('returns true when interpreter arch differs from HOST_ARCH on darwin', async () => { |
| 120 | + mockState.archByPath.set('/x/python3', 'x86_64'); |
| 121 | + const { isArchMismatch } = await loadModule(); |
| 122 | + await expect(isArchMismatch('/x/python3')).resolves.toBe(true); |
| 123 | + }); |
| 124 | + |
| 125 | + it('returns false when the probe fails (no interpreter to compare)', async () => { |
| 126 | + mockState.execShouldFail = true; |
| 127 | + const { isArchMismatch } = await loadModule(); |
| 128 | + await expect(isArchMismatch('/x/python3')).resolves.toBe(false); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +describe('detectPython on darwin/arm64', () => { |
| 133 | + beforeEach(resetState); |
| 134 | + |
| 135 | + it('prefers an arm64 candidate over an earlier-listed x86_64 candidate', async () => { |
| 136 | + // /opt/anaconda3 (index 4 in PYTHON_CANDIDATES) is listed before |
| 137 | + // /opt/homebrew (index 9). The arm64-preference branch must override |
| 138 | + // first-present-wins so mlx wheels load correctly. |
| 139 | + mockState.presentPaths.add('/opt/anaconda3/bin/python3'); |
| 140 | + mockState.presentPaths.add('/opt/homebrew/bin/python3'); |
| 141 | + mockState.archByPath.set('/opt/anaconda3/bin/python3', 'x86_64'); |
| 142 | + mockState.archByPath.set('/opt/homebrew/bin/python3', 'arm64'); |
| 143 | + const { detectPython } = await loadModule(); |
| 144 | + await expect(detectPython()).resolves.toBe('/opt/homebrew/bin/python3'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('returns the sole present candidate without paying the arch-probe cost', async () => { |
| 148 | + // The `present.length > 1` guard skips the parallel probe when there is |
| 149 | + // nothing to choose between. |
| 150 | + mockState.presentPaths.add('/opt/anaconda3/bin/python3'); |
| 151 | + mockState.archByPath.set('/opt/anaconda3/bin/python3', 'x86_64'); |
| 152 | + const { detectPython } = await loadModule(); |
| 153 | + await expect(detectPython()).resolves.toBe('/opt/anaconda3/bin/python3'); |
| 154 | + }); |
| 155 | + |
| 156 | + it('falls back to the first present candidate when no candidate is arm64', async () => { |
| 157 | + mockState.presentPaths.add('/opt/anaconda3/bin/python3'); |
| 158 | + mockState.presentPaths.add('/usr/local/bin/python3'); |
| 159 | + mockState.archByPath.set('/opt/anaconda3/bin/python3', 'x86_64'); |
| 160 | + mockState.archByPath.set('/usr/local/bin/python3', 'x86_64'); |
| 161 | + const { detectPython } = await loadModule(); |
| 162 | + await expect(detectPython()).resolves.toBe('/opt/anaconda3/bin/python3'); |
| 163 | + }); |
| 164 | +}); |
| 165 | + |
| 166 | +describe('detectArm64Python', () => { |
| 167 | + beforeEach(resetState); |
| 168 | + |
| 169 | + it('returns null on non-darwin', async () => { |
| 170 | + mockState.platform = 'linux'; |
| 171 | + mockState.presentPaths.add('/usr/bin/python3'); |
| 172 | + mockState.archByPath.set('/usr/bin/python3', 'arm64'); |
| 173 | + const { detectArm64Python } = await loadModule(); |
| 174 | + await expect(detectArm64Python()).resolves.toBeNull(); |
| 175 | + }); |
| 176 | + |
| 177 | + it('returns null on Intel macs (HOST_ARCH !== arm64)', async () => { |
| 178 | + mockState.arch = 'x64'; |
| 179 | + mockState.presentPaths.add('/usr/local/bin/python3'); |
| 180 | + mockState.archByPath.set('/usr/local/bin/python3', 'x86_64'); |
| 181 | + const { detectArm64Python } = await loadModule(); |
| 182 | + await expect(detectArm64Python()).resolves.toBeNull(); |
| 183 | + }); |
| 184 | + |
| 185 | + it('returns null when no present candidate reports arm64', async () => { |
| 186 | + mockState.presentPaths.add('/opt/anaconda3/bin/python3'); |
| 187 | + mockState.archByPath.set('/opt/anaconda3/bin/python3', 'x86_64'); |
| 188 | + const { detectArm64Python } = await loadModule(); |
| 189 | + await expect(detectArm64Python()).resolves.toBeNull(); |
| 190 | + }); |
| 191 | + |
| 192 | + it('returns the first arm64 candidate by PYTHON_CANDIDATES order', async () => { |
| 193 | + mockState.presentPaths.add('/opt/anaconda3/bin/python3'); |
| 194 | + mockState.presentPaths.add('/opt/homebrew/bin/python3'); |
| 195 | + mockState.presentPaths.add('/usr/local/bin/python3'); |
| 196 | + mockState.archByPath.set('/opt/anaconda3/bin/python3', 'x86_64'); |
| 197 | + mockState.archByPath.set('/opt/homebrew/bin/python3', 'arm64'); |
| 198 | + mockState.archByPath.set('/usr/local/bin/python3', 'arm64'); |
| 199 | + const { detectArm64Python } = await loadModule(); |
| 200 | + await expect(detectArm64Python()).resolves.toBe('/opt/homebrew/bin/python3'); |
| 201 | + }); |
| 202 | +}); |
0 commit comments