|
| 1 | +import { promises as fs } from 'node:fs' |
| 2 | +import os from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | + |
| 5 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest' |
| 6 | + |
| 7 | +import { |
| 8 | + clearDlx, |
| 9 | + clearDlxSync, |
| 10 | + dlxDirExists, |
| 11 | + dlxDirExistsAsync, |
| 12 | + ensureDlxDir, |
| 13 | + ensureDlxDirSync, |
| 14 | + getDlxInstalledPackageDir, |
| 15 | + getDlxPackageDir, |
| 16 | + getDlxPackageJsonPath, |
| 17 | + getDlxPackageNodeModulesDir, |
| 18 | + isDlxPackageInstalled, |
| 19 | + isDlxPackageInstalledAsync, |
| 20 | + listDlxPackages, |
| 21 | + listDlxPackagesAsync, |
| 22 | + removeDlxPackage, |
| 23 | + removeDlxPackageSync, |
| 24 | +} from '../../registry/dist/lib/dlx.js' |
| 25 | + |
| 26 | +// Test package names. |
| 27 | +const TEST_PKG = 'test-package' |
| 28 | +const TEST_PKG_2 = 'test-package-2' |
| 29 | + |
| 30 | +describe('dlx module', () => { |
| 31 | + let originalSocketDir: string | undefined |
| 32 | + let testDlxDir: string |
| 33 | + |
| 34 | + beforeEach(async () => { |
| 35 | + // Create a temporary DLX directory for testing. |
| 36 | + const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'socket-dlx-test-')) |
| 37 | + testDlxDir = path.join(tmpDir, '_dlx') |
| 38 | + |
| 39 | + // Override the Socket directory for testing. |
| 40 | + originalSocketDir = process.env['SOCKET_USER_DIR'] |
| 41 | + process.env['SOCKET_USER_DIR'] = tmpDir |
| 42 | + |
| 43 | + await ensureDlxDir() |
| 44 | + }) |
| 45 | + |
| 46 | + afterEach(async () => { |
| 47 | + // Clean up all packages first. |
| 48 | + try { |
| 49 | + const packages = await listDlxPackagesAsync() |
| 50 | + await Promise.all(packages.map(pkg => removeDlxPackage(pkg))) |
| 51 | + } catch { |
| 52 | + // Ignore cleanup errors. |
| 53 | + } |
| 54 | + |
| 55 | + // Restore original environment. |
| 56 | + if (originalSocketDir === undefined) { |
| 57 | + delete process.env['SOCKET_USER_DIR'] |
| 58 | + } else { |
| 59 | + process.env['SOCKET_USER_DIR'] = originalSocketDir |
| 60 | + } |
| 61 | + |
| 62 | + // Clean up test directory. |
| 63 | + try { |
| 64 | + const parentDir = path.dirname(testDlxDir) |
| 65 | + await fs.rm(parentDir, { recursive: true, force: true }) |
| 66 | + } catch { |
| 67 | + // Ignore cleanup errors. |
| 68 | + } |
| 69 | + }) |
| 70 | + |
| 71 | + describe('clearDlx', () => { |
| 72 | + it('should remove all packages', async () => { |
| 73 | + // Create test packages. |
| 74 | + await fs.mkdir(getDlxPackageDir(TEST_PKG), { recursive: true }) |
| 75 | + await fs.mkdir(getDlxPackageDir(TEST_PKG_2), { recursive: true }) |
| 76 | + |
| 77 | + await clearDlx() |
| 78 | + |
| 79 | + const packages = await listDlxPackagesAsync() |
| 80 | + expect(packages).toEqual([]) |
| 81 | + }) |
| 82 | + |
| 83 | + it('should handle empty dlx directory', async () => { |
| 84 | + await expect(clearDlx()).resolves.toBeUndefined() |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + describe('clearDlxSync', () => { |
| 89 | + it('should remove all packages synchronously', () => { |
| 90 | + // Create test packages synchronously. |
| 91 | + const { mkdirSync } = require('node:fs') |
| 92 | + mkdirSync(getDlxPackageDir(TEST_PKG), { recursive: true }) |
| 93 | + mkdirSync(getDlxPackageDir(TEST_PKG_2), { recursive: true }) |
| 94 | + |
| 95 | + clearDlxSync() |
| 96 | + |
| 97 | + const packages = listDlxPackages() |
| 98 | + expect(packages).toEqual([]) |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + describe('dlxDirExists and dlxDirExistsAsync', () => { |
| 103 | + it('should return true when dlx directory exists', () => { |
| 104 | + expect(dlxDirExists()).toBe(true) |
| 105 | + }) |
| 106 | + |
| 107 | + it('should return true when dlx directory exists asynchronously', async () => { |
| 108 | + expect(await dlxDirExistsAsync()).toBe(true) |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + describe('ensureDlxDir and ensureDlxDirSync', () => { |
| 113 | + it('should create dlx directory if it does not exist', async () => { |
| 114 | + await fs.rm(testDlxDir, { recursive: true, force: true }) |
| 115 | + |
| 116 | + await ensureDlxDir() |
| 117 | + |
| 118 | + expect(dlxDirExists()).toBe(true) |
| 119 | + }) |
| 120 | + |
| 121 | + it('should create dlx directory synchronously if it does not exist', async () => { |
| 122 | + await fs.rm(testDlxDir, { recursive: true, force: true }) |
| 123 | + |
| 124 | + ensureDlxDirSync() |
| 125 | + |
| 126 | + expect(dlxDirExists()).toBe(true) |
| 127 | + }) |
| 128 | + |
| 129 | + it('should not error if directory already exists', async () => { |
| 130 | + await expect(ensureDlxDir()).resolves.toBeUndefined() |
| 131 | + expect(() => ensureDlxDirSync()).not.toThrow() |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + describe('getDlxInstalledPackageDir', () => { |
| 136 | + it('should return correct path for installed package', () => { |
| 137 | + const dir = getDlxInstalledPackageDir(TEST_PKG) |
| 138 | + expect(dir).toContain('_dlx') |
| 139 | + expect(dir).toContain('node_modules') |
| 140 | + expect(dir).toContain(TEST_PKG) |
| 141 | + }) |
| 142 | + }) |
| 143 | + |
| 144 | + describe('getDlxPackageDir', () => { |
| 145 | + it('should return correct path for package', () => { |
| 146 | + const dir = getDlxPackageDir(TEST_PKG) |
| 147 | + expect(dir).toContain('_dlx') |
| 148 | + expect(dir).toContain(TEST_PKG) |
| 149 | + expect(dir).not.toContain('node_modules') |
| 150 | + }) |
| 151 | + }) |
| 152 | + |
| 153 | + describe('getDlxPackageJsonPath', () => { |
| 154 | + it('should return correct path for package.json', () => { |
| 155 | + const jsonPath = getDlxPackageJsonPath(TEST_PKG) |
| 156 | + expect(jsonPath).toContain('_dlx') |
| 157 | + expect(jsonPath).toContain('node_modules') |
| 158 | + expect(jsonPath).toContain(TEST_PKG) |
| 159 | + expect(jsonPath).toContain('package.json') |
| 160 | + }) |
| 161 | + }) |
| 162 | + |
| 163 | + describe('getDlxPackageNodeModulesDir', () => { |
| 164 | + it('should return correct path for node_modules', () => { |
| 165 | + const dir = getDlxPackageNodeModulesDir(TEST_PKG) |
| 166 | + expect(dir).toContain('_dlx') |
| 167 | + expect(dir).toContain(TEST_PKG) |
| 168 | + expect(dir).toContain('node_modules') |
| 169 | + }) |
| 170 | + }) |
| 171 | + |
| 172 | + describe('isDlxPackageInstalled and isDlxPackageInstalledAsync', () => { |
| 173 | + it('should return true when package is installed', async () => { |
| 174 | + await fs.mkdir(getDlxInstalledPackageDir(TEST_PKG), { recursive: true }) |
| 175 | + |
| 176 | + expect(isDlxPackageInstalled(TEST_PKG)).toBe(true) |
| 177 | + expect(await isDlxPackageInstalledAsync(TEST_PKG)).toBe(true) |
| 178 | + }) |
| 179 | + |
| 180 | + it('should return false when package is not installed', () => { |
| 181 | + expect(isDlxPackageInstalled('non-existent-package')).toBe(false) |
| 182 | + }) |
| 183 | + |
| 184 | + it('should return false when package is not installed asynchronously', async () => { |
| 185 | + expect(await isDlxPackageInstalledAsync('non-existent-package')).toBe( |
| 186 | + false, |
| 187 | + ) |
| 188 | + }) |
| 189 | + }) |
| 190 | + |
| 191 | + describe('listDlxPackages and listDlxPackagesAsync', () => { |
| 192 | + it('should list all installed packages', async () => { |
| 193 | + await fs.mkdir(getDlxPackageDir(TEST_PKG), { recursive: true }) |
| 194 | + await fs.mkdir(getDlxPackageDir(TEST_PKG_2), { recursive: true }) |
| 195 | + |
| 196 | + const packages = listDlxPackages() |
| 197 | + const packagesAsync = await listDlxPackagesAsync() |
| 198 | + |
| 199 | + expect(packages).toEqual([TEST_PKG, TEST_PKG_2]) |
| 200 | + expect(packagesAsync).toEqual([TEST_PKG, TEST_PKG_2]) |
| 201 | + }) |
| 202 | + |
| 203 | + it('should return empty array when no packages installed', () => { |
| 204 | + const packages = listDlxPackages() |
| 205 | + expect(packages).toEqual([]) |
| 206 | + }) |
| 207 | + |
| 208 | + it('should return empty array when no packages installed asynchronously', async () => { |
| 209 | + const packages = await listDlxPackagesAsync() |
| 210 | + expect(packages).toEqual([]) |
| 211 | + }) |
| 212 | + |
| 213 | + it('should return empty array when dlx directory does not exist', async () => { |
| 214 | + await fs.rm(testDlxDir, { recursive: true, force: true }) |
| 215 | + |
| 216 | + const packages = listDlxPackages() |
| 217 | + const packagesAsync = await listDlxPackagesAsync() |
| 218 | + |
| 219 | + expect(packages).toEqual([]) |
| 220 | + expect(packagesAsync).toEqual([]) |
| 221 | + }) |
| 222 | + }) |
| 223 | + |
| 224 | + describe('removeDlxPackage and removeDlxPackageSync', () => { |
| 225 | + it('should remove package directory', async () => { |
| 226 | + await fs.mkdir(getDlxPackageDir(TEST_PKG), { recursive: true }) |
| 227 | + |
| 228 | + await removeDlxPackage(TEST_PKG) |
| 229 | + |
| 230 | + expect(isDlxPackageInstalled(TEST_PKG)).toBe(false) |
| 231 | + }) |
| 232 | + |
| 233 | + it('should remove package directory synchronously', async () => { |
| 234 | + const { mkdirSync } = require('node:fs') |
| 235 | + mkdirSync(getDlxPackageDir(TEST_PKG), { recursive: true }) |
| 236 | + |
| 237 | + removeDlxPackageSync(TEST_PKG) |
| 238 | + |
| 239 | + expect(isDlxPackageInstalled(TEST_PKG)).toBe(false) |
| 240 | + }) |
| 241 | + |
| 242 | + it('should throw error with cause when removal fails', async () => { |
| 243 | + // Try to remove a non-existent package (force: true means it won't error). |
| 244 | + // Instead, try to remove with invalid path characters. |
| 245 | + await expect(removeDlxPackage('\0invalid')).rejects.toThrow( |
| 246 | + /Failed to remove DLX package/, |
| 247 | + ) |
| 248 | + }) |
| 249 | + |
| 250 | + it('should throw error with cause when sync removal fails', () => { |
| 251 | + expect(() => removeDlxPackageSync('\0invalid')).toThrow( |
| 252 | + /Failed to remove DLX package/, |
| 253 | + ) |
| 254 | + }) |
| 255 | + }) |
| 256 | +}) |
0 commit comments