|
| 1 | +/** @fileoverview Tests for package.json normalization utilities. */ |
| 2 | + |
| 3 | +import { describe, expect, it } from 'vitest' |
| 4 | + |
| 5 | +import { |
| 6 | + normalizePackageJson, |
| 7 | + resolveEscapedScope, |
| 8 | + resolveOriginalPackageName, |
| 9 | + unescapeScope, |
| 10 | +} from '../../../dist/packages/normalize.js' |
| 11 | + |
| 12 | +import type { PackageJson } from '../../../dist/packages.js' |
| 13 | + |
| 14 | +describe('normalizePackageJson', () => { |
| 15 | + it('should normalize basic package.json', () => { |
| 16 | + const pkg = { name: 'test-package' } |
| 17 | + const normalized = normalizePackageJson(pkg as PackageJson) |
| 18 | + expect(normalized.name).toBe('test-package') |
| 19 | + expect(normalized.version).toBe('0.0.0') |
| 20 | + }) |
| 21 | + |
| 22 | + it('should preserve existing version', () => { |
| 23 | + const pkg = { name: 'test-package', version: '1.2.3' } |
| 24 | + const normalized = normalizePackageJson(pkg as PackageJson) |
| 25 | + expect(normalized.version).toBe('1.2.3') |
| 26 | + }) |
| 27 | + |
| 28 | + it('should add default version when missing', () => { |
| 29 | + const pkg = { name: 'test-package' } |
| 30 | + const normalized = normalizePackageJson(pkg as PackageJson) |
| 31 | + expect(normalized.version).toBe('0.0.0') |
| 32 | + }) |
| 33 | + |
| 34 | + it('should preserve custom fields with preserve option', () => { |
| 35 | + const pkg = { name: 'test-package', custom: 'value' } |
| 36 | + const normalized = normalizePackageJson(pkg as PackageJson, { |
| 37 | + preserve: ['custom'], |
| 38 | + }) |
| 39 | + // Access custom field through Record type |
| 40 | + expect((normalized as Record<string, unknown>).custom).toBe('value') |
| 41 | + }) |
| 42 | + |
| 43 | + it('should handle dependencies normalization', () => { |
| 44 | + const pkg = { |
| 45 | + name: 'test-package', |
| 46 | + dependencies: { lodash: '^4.0.0' }, |
| 47 | + } |
| 48 | + const normalized = normalizePackageJson(pkg as PackageJson) |
| 49 | + expect(normalized.dependencies).toBeDefined() |
| 50 | + expect(normalized.dependencies?.lodash).toBe('^4.0.0') |
| 51 | + }) |
| 52 | + |
| 53 | + it('should handle empty package.json', () => { |
| 54 | + const pkg = {} |
| 55 | + const normalized = normalizePackageJson(pkg as PackageJson) |
| 56 | + expect(normalized.version).toBe('0.0.0') |
| 57 | + }) |
| 58 | +}) |
| 59 | + |
| 60 | +describe('resolveEscapedScope', () => { |
| 61 | + it('should resolve escaped scope from Socket registry name', () => { |
| 62 | + const result = resolveEscapedScope('angular__cli') |
| 63 | + expect(result).toBe('angular__') |
| 64 | + }) |
| 65 | + |
| 66 | + it('should return undefined for unscoped package', () => { |
| 67 | + const result = resolveEscapedScope('express') |
| 68 | + expect(result).toBeUndefined() |
| 69 | + }) |
| 70 | + |
| 71 | + it('should handle empty string', () => { |
| 72 | + const result = resolveEscapedScope('') |
| 73 | + expect(result).toBeUndefined() |
| 74 | + }) |
| 75 | + |
| 76 | + it('should handle package with scope only', () => { |
| 77 | + const result = resolveEscapedScope('scope__') |
| 78 | + expect(result).toBe('scope__') |
| 79 | + }) |
| 80 | + |
| 81 | + it('should not match double underscore within package name', () => { |
| 82 | + const result = resolveEscapedScope('foo__bar__baz') |
| 83 | + expect(result).toBe('foo__') |
| 84 | + }) |
| 85 | +}) |
| 86 | + |
| 87 | +describe('resolveOriginalPackageName', () => { |
| 88 | + it('should convert Socket registry name to original scoped name', () => { |
| 89 | + const result = resolveOriginalPackageName('angular__cli') |
| 90 | + expect(result).toBe('@angular/cli') |
| 91 | + }) |
| 92 | + |
| 93 | + it('should handle unscoped package names', () => { |
| 94 | + const result = resolveOriginalPackageName('express') |
| 95 | + expect(result).toBe('express') |
| 96 | + }) |
| 97 | + |
| 98 | + it('should strip Socket registry scope prefix', () => { |
| 99 | + const result = resolveOriginalPackageName('@socketregistry/angular__cli') |
| 100 | + expect(result).toBe('@angular/cli') |
| 101 | + }) |
| 102 | + |
| 103 | + it('should handle paths with remaining underscores', () => { |
| 104 | + // Only the first __ is converted to @/, remaining __ are preserved |
| 105 | + const result = resolveOriginalPackageName('angular__cli__packages') |
| 106 | + expect(result).toBe('@angular/cli__packages') |
| 107 | + }) |
| 108 | + |
| 109 | + it('should handle simple scoped package', () => { |
| 110 | + const result = resolveOriginalPackageName('types__node') |
| 111 | + expect(result).toBe('@types/node') |
| 112 | + }) |
| 113 | +}) |
| 114 | + |
| 115 | +describe('unescapeScope', () => { |
| 116 | + it('should convert escaped scope to standard npm scope format', () => { |
| 117 | + const result = unescapeScope('angular__') |
| 118 | + expect(result).toBe('@angular') |
| 119 | + }) |
| 120 | + |
| 121 | + it('should handle single delimiter', () => { |
| 122 | + const result = unescapeScope('scope__') |
| 123 | + expect(result).toBe('@scope') |
| 124 | + }) |
| 125 | + |
| 126 | + it('should handle types scope', () => { |
| 127 | + const result = unescapeScope('types__') |
| 128 | + expect(result).toBe('@types') |
| 129 | + }) |
| 130 | + |
| 131 | + it('should handle short scope names', () => { |
| 132 | + const result = unescapeScope('a__') |
| 133 | + expect(result).toBe('@a') |
| 134 | + }) |
| 135 | +}) |
0 commit comments