Skip to content

Commit 3ba28a2

Browse files
committed
Add comprehensive tests for packages module
Adds new test coverage for provenance, editable, and additional package functions. Improves overall coverage from 75.7% to 79.23%.
1 parent b66f996 commit 3ba28a2

3 files changed

Lines changed: 552 additions & 0 deletions

File tree

test/registry/packages-additional.test.mts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,66 @@ describe('packages module additional coverage', () => {
160160
const instance = new EditablePackageJson()
161161
expect(instance).toBeDefined()
162162
})
163+
164+
it('should have static methods', () => {
165+
const EditablePackageJson = getEditablePackageJsonClass()
166+
expect(typeof EditablePackageJson.create).toBe('function')
167+
expect(typeof EditablePackageJson.load).toBe('function')
168+
expect(Array.isArray(EditablePackageJson.fixSteps)).toBe(true)
169+
expect(Array.isArray(EditablePackageJson.normalizeSteps)).toBe(true)
170+
expect(Array.isArray(EditablePackageJson.prepareSteps)).toBe(true)
171+
})
172+
173+
it('should have instance methods', () => {
174+
const EditablePackageJson = getEditablePackageJsonClass()
175+
const instance = new EditablePackageJson()
176+
expect(typeof instance.fromContent).toBe('function')
177+
expect(typeof instance.update).toBe('function')
178+
expect(typeof instance.save).toBe('function')
179+
expect(typeof instance.saveSync).toBe('function')
180+
})
181+
182+
it('should create instance with fromContent', () => {
183+
const EditablePackageJson = getEditablePackageJsonClass()
184+
const instance = new EditablePackageJson()
185+
const pkg = { name: 'test-pkg', version: '1.0.0' }
186+
instance.fromContent(pkg)
187+
expect(instance.content.name).toBe('test-pkg')
188+
expect(instance.content.version).toBe('1.0.0')
189+
})
190+
191+
it('should update content with update method', () => {
192+
const EditablePackageJson = getEditablePackageJsonClass()
193+
const instance = new EditablePackageJson()
194+
instance.fromContent({ name: 'test', version: '1.0.0' })
195+
instance.update({ version: '2.0.0', description: 'Updated' })
196+
expect(instance.content.version).toBe('2.0.0')
197+
expect(instance.content.description).toBe('Updated')
198+
})
199+
200+
it('should parse JSON string with fromJSON', () => {
201+
const EditablePackageJson = getEditablePackageJsonClass()
202+
const instance = new EditablePackageJson()
203+
const json = JSON.stringify({ name: 'json-test', version: '1.0.0' })
204+
instance.fromJSON(json)
205+
expect(instance.content.name).toBe('json-test')
206+
})
207+
208+
it('should get filename property', () => {
209+
const EditablePackageJson = getEditablePackageJsonClass()
210+
const instance = new EditablePackageJson()
211+
instance.create('/test/path')
212+
const filename = instance.filename
213+
expect(filename).toContain('package.json')
214+
})
215+
216+
it('should handle willSave check', () => {
217+
const EditablePackageJson = getEditablePackageJsonClass()
218+
const instance = new EditablePackageJson()
219+
instance.fromContent({ name: 'test', version: '1.0.0' })
220+
const willSave = instance.willSave()
221+
expect(typeof willSave).toBe('boolean')
222+
})
163223
})
164224

165225
describe('pkgJsonToEditable', () => {
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
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

Comments
 (0)