|
| 1 | +/** @fileoverview Tests for editable package.json functionality. */ |
| 2 | +import fs from 'node:fs' |
| 3 | +import os from 'node:os' |
| 4 | +import path from 'node:path' |
| 5 | + |
| 6 | +import { describe, expect, it } from 'vitest' |
| 7 | + |
| 8 | +import { |
| 9 | + getEditablePackageJsonClass, |
| 10 | + toEditablePackageJson, |
| 11 | + toEditablePackageJsonSync, |
| 12 | +} from '../../registry/dist/lib/packages.js' |
| 13 | + |
| 14 | +describe('toEditablePackageJson', () => { |
| 15 | + it('should convert package.json to editable instance', async () => { |
| 16 | + const pkg = { name: 'test-editable', version: '1.0.0' } |
| 17 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-')) |
| 18 | + |
| 19 | + try { |
| 20 | + const editable = await toEditablePackageJson(pkg, { path: tmpDir }) |
| 21 | + expect(editable).toBeDefined() |
| 22 | + expect(editable.content.name).toBe('test-editable') |
| 23 | + expect(editable.content.version).toBe('1.0.0') |
| 24 | + } finally { |
| 25 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + it('should handle without path option', async () => { |
| 30 | + const pkg = { name: 'test-no-path', version: '1.0.0' } |
| 31 | + const editable = await toEditablePackageJson(pkg) |
| 32 | + expect(editable).toBeDefined() |
| 33 | + expect(editable.content.name).toBe('test-no-path') |
| 34 | + }) |
| 35 | + |
| 36 | + it('should normalize when normalize option is true', async () => { |
| 37 | + const pkg = { name: 'test-normalize', version: '1.0.0' } |
| 38 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-')) |
| 39 | + |
| 40 | + try { |
| 41 | + const editable = await toEditablePackageJson(pkg, { |
| 42 | + path: tmpDir, |
| 43 | + normalize: true, |
| 44 | + }) |
| 45 | + expect(editable).toBeDefined() |
| 46 | + expect(editable.content.name).toBe('test-normalize') |
| 47 | + } finally { |
| 48 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | + it('should handle dependencies', async () => { |
| 53 | + const pkg = { |
| 54 | + name: 'test-deps', |
| 55 | + version: '1.0.0', |
| 56 | + dependencies: { |
| 57 | + lodash: '^4.17.21', |
| 58 | + }, |
| 59 | + } |
| 60 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-')) |
| 61 | + |
| 62 | + try { |
| 63 | + const editable = await toEditablePackageJson(pkg, { path: tmpDir }) |
| 64 | + expect(editable.content.dependencies).toBeDefined() |
| 65 | + expect(editable.content.dependencies.lodash).toBe('^4.17.21') |
| 66 | + } finally { |
| 67 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 68 | + } |
| 69 | + }) |
| 70 | + |
| 71 | + it('should handle scripts', async () => { |
| 72 | + const pkg = { |
| 73 | + name: 'test-scripts', |
| 74 | + version: '1.0.0', |
| 75 | + scripts: { |
| 76 | + test: 'vitest', |
| 77 | + build: 'tsc', |
| 78 | + }, |
| 79 | + } |
| 80 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-')) |
| 81 | + |
| 82 | + try { |
| 83 | + const editable = await toEditablePackageJson(pkg, { path: tmpDir }) |
| 84 | + expect(editable.content.scripts).toBeDefined() |
| 85 | + expect(editable.content.scripts.test).toBe('vitest') |
| 86 | + } finally { |
| 87 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 88 | + } |
| 89 | + }) |
| 90 | +}) |
| 91 | + |
| 92 | +describe('toEditablePackageJsonSync', () => { |
| 93 | + it('should convert package.json to editable instance synchronously', () => { |
| 94 | + const pkg = { name: 'test-editable-sync', version: '1.0.0' } |
| 95 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-')) |
| 96 | + |
| 97 | + try { |
| 98 | + const editable = toEditablePackageJsonSync(pkg, { path: tmpDir }) |
| 99 | + expect(editable).toBeDefined() |
| 100 | + expect(editable.content.name).toBe('test-editable-sync') |
| 101 | + expect(editable.content.version).toBe('1.0.0') |
| 102 | + } finally { |
| 103 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + it('should handle without path option', () => { |
| 108 | + const pkg = { name: 'test-no-path-sync', version: '1.0.0' } |
| 109 | + const editable = toEditablePackageJsonSync(pkg) |
| 110 | + expect(editable).toBeDefined() |
| 111 | + expect(editable.content.name).toBe('test-no-path-sync') |
| 112 | + }) |
| 113 | + |
| 114 | + it('should normalize when normalize option is true', () => { |
| 115 | + const pkg = { name: 'test-normalize-sync', version: '1.0.0' } |
| 116 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-')) |
| 117 | + |
| 118 | + try { |
| 119 | + const editable = toEditablePackageJsonSync(pkg, { |
| 120 | + path: tmpDir, |
| 121 | + normalize: true, |
| 122 | + }) |
| 123 | + expect(editable).toBeDefined() |
| 124 | + expect(editable.content.name).toBe('test-normalize-sync') |
| 125 | + } finally { |
| 126 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + it('should handle node_modules paths', () => { |
| 131 | + const pkg = { name: 'test-node-modules', version: '1.0.0' } |
| 132 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-')) |
| 133 | + const nodeModulesPath = path.join(tmpDir, 'node_modules', 'test-package') |
| 134 | + fs.mkdirSync(nodeModulesPath, { recursive: true }) |
| 135 | + |
| 136 | + try { |
| 137 | + const editable = toEditablePackageJsonSync(pkg, { |
| 138 | + path: nodeModulesPath, |
| 139 | + normalize: true, |
| 140 | + }) |
| 141 | + expect(editable).toBeDefined() |
| 142 | + expect(editable.content.name).toBe('test-node-modules') |
| 143 | + } finally { |
| 144 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 145 | + } |
| 146 | + }) |
| 147 | + |
| 148 | + it('should preserve repository for non-node_modules paths', () => { |
| 149 | + const pkg = { |
| 150 | + name: 'test-repository', |
| 151 | + version: '1.0.0', |
| 152 | + repository: 'github:user/repo', |
| 153 | + } |
| 154 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-')) |
| 155 | + |
| 156 | + try { |
| 157 | + const editable = toEditablePackageJsonSync(pkg, { |
| 158 | + path: tmpDir, |
| 159 | + normalize: true, |
| 160 | + }) |
| 161 | + expect(editable).toBeDefined() |
| 162 | + expect(editable.content.repository).toBeDefined() |
| 163 | + } finally { |
| 164 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 165 | + } |
| 166 | + }) |
| 167 | +}) |
| 168 | + |
| 169 | +describe('EditablePackageJson static methods', () => { |
| 170 | + it('should create package.json with static create method', async () => { |
| 171 | + const EditablePackageJson = getEditablePackageJsonClass() |
| 172 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-')) |
| 173 | + |
| 174 | + try { |
| 175 | + const editable = await EditablePackageJson.create(tmpDir) |
| 176 | + expect(editable).toBeDefined() |
| 177 | + expect(editable.content).toBeDefined() |
| 178 | + } finally { |
| 179 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 180 | + } |
| 181 | + }) |
| 182 | + |
| 183 | + it('should load existing package.json with static load method', async () => { |
| 184 | + const EditablePackageJson = getEditablePackageJsonClass() |
| 185 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-')) |
| 186 | + const pkgPath = path.join(tmpDir, 'package.json') |
| 187 | + fs.writeFileSync( |
| 188 | + pkgPath, |
| 189 | + JSON.stringify({ name: 'test-load', version: '1.0.0' }), |
| 190 | + ) |
| 191 | + |
| 192 | + try { |
| 193 | + const editable = await EditablePackageJson.load(tmpDir) |
| 194 | + expect(editable).toBeDefined() |
| 195 | + expect(editable.content.name).toBe('test-load') |
| 196 | + } finally { |
| 197 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 198 | + } |
| 199 | + }) |
| 200 | + |
| 201 | + it('should normalize package.json with static normalize method', async () => { |
| 202 | + const EditablePackageJson = getEditablePackageJsonClass() |
| 203 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-')) |
| 204 | + const pkgPath = path.join(tmpDir, 'package.json') |
| 205 | + fs.writeFileSync( |
| 206 | + pkgPath, |
| 207 | + JSON.stringify({ name: 'test-normalize', version: '1.0.0' }), |
| 208 | + ) |
| 209 | + |
| 210 | + try { |
| 211 | + const editable = await EditablePackageJson.normalize(tmpDir) |
| 212 | + expect(editable).toBeDefined() |
| 213 | + expect(editable.content.name).toBe('test-normalize') |
| 214 | + } finally { |
| 215 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 216 | + } |
| 217 | + }) |
| 218 | + |
| 219 | + it('should prepare package.json with static prepare method', async () => { |
| 220 | + const EditablePackageJson = getEditablePackageJsonClass() |
| 221 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-')) |
| 222 | + const pkgPath = path.join(tmpDir, 'package.json') |
| 223 | + fs.writeFileSync( |
| 224 | + pkgPath, |
| 225 | + JSON.stringify({ name: 'test-prepare', version: '1.0.0' }), |
| 226 | + ) |
| 227 | + |
| 228 | + try { |
| 229 | + const editable = await EditablePackageJson.prepare(tmpDir, {}) |
| 230 | + expect(editable).toBeDefined() |
| 231 | + expect(editable.content.name).toBe('test-prepare') |
| 232 | + } finally { |
| 233 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 234 | + } |
| 235 | + }) |
| 236 | + |
| 237 | + it('should handle fix method', async () => { |
| 238 | + const EditablePackageJson = getEditablePackageJsonClass() |
| 239 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-')) |
| 240 | + const pkgPath = path.join(tmpDir, 'package.json') |
| 241 | + fs.writeFileSync( |
| 242 | + pkgPath, |
| 243 | + JSON.stringify({ name: 'test-fix', version: '1.0.0' }), |
| 244 | + ) |
| 245 | + |
| 246 | + try { |
| 247 | + const editable = await EditablePackageJson.load(tmpDir) |
| 248 | + await editable.fix() |
| 249 | + expect(editable.content.name).toBe('test-fix') |
| 250 | + } finally { |
| 251 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 252 | + } |
| 253 | + }) |
| 254 | + |
| 255 | + it('should handle load with create option', async () => { |
| 256 | + const EditablePackageJson = getEditablePackageJsonClass() |
| 257 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'socket-test-')) |
| 258 | + |
| 259 | + try { |
| 260 | + const editable = await EditablePackageJson.load(tmpDir, { create: true }) |
| 261 | + expect(editable).toBeDefined() |
| 262 | + } finally { |
| 263 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 264 | + } |
| 265 | + }) |
| 266 | +}) |
0 commit comments