|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the license change detection logic used in app/pages/package/[[org]]/[name].vue. |
| 5 | + * |
| 6 | + * The `licenseChanged` computed detects when the license of the currently |
| 7 | + * viewed version differs from the package-level (latest) license, so an |
| 8 | + * amber "changed" badge can be shown to alert users. |
| 9 | + */ |
| 10 | + |
| 11 | +type LicenseValue = string | { type?: string } | undefined | null |
| 12 | + |
| 13 | +function normalize(l: LicenseValue): string { |
| 14 | + if (!l) return '' |
| 15 | + return typeof l === 'string' ? l : (l as { type?: string })?.type ?? '' |
| 16 | +} |
| 17 | + |
| 18 | +function licenseChanged( |
| 19 | + currentLicense: LicenseValue, |
| 20 | + packageLicense: LicenseValue, |
| 21 | +): boolean { |
| 22 | + if (!currentLicense || !packageLicense) return false |
| 23 | + return normalize(currentLicense) !== normalize(packageLicense) |
| 24 | +} |
| 25 | + |
| 26 | +describe('licenseChanged detection', () => { |
| 27 | + it('returns false when both licenses are the same string', () => { |
| 28 | + expect(licenseChanged('MIT', 'MIT')).toBe(false) |
| 29 | + }) |
| 30 | + |
| 31 | + it('returns true when a non-latest version has a different license', () => { |
| 32 | + // e.g. old version had GPL, latest has MIT |
| 33 | + expect(licenseChanged('GPL-2.0-only', 'MIT')).toBe(true) |
| 34 | + }) |
| 35 | + |
| 36 | + it('returns false when current license is missing', () => { |
| 37 | + expect(licenseChanged(null, 'MIT')).toBe(false) |
| 38 | + expect(licenseChanged(undefined, 'MIT')).toBe(false) |
| 39 | + }) |
| 40 | + |
| 41 | + it('returns false when package license is missing', () => { |
| 42 | + expect(licenseChanged('MIT', null)).toBe(false) |
| 43 | + expect(licenseChanged('MIT', undefined)).toBe(false) |
| 44 | + }) |
| 45 | + |
| 46 | + it('returns false when both licenses are missing', () => { |
| 47 | + expect(licenseChanged(null, null)).toBe(false) |
| 48 | + }) |
| 49 | + |
| 50 | + it('normalizes object-shaped licenses for comparison', () => { |
| 51 | + // Some old package.json use { type: "MIT" } instead of "MIT" |
| 52 | + expect(licenseChanged({ type: 'MIT' }, 'MIT')).toBe(false) |
| 53 | + expect(licenseChanged('MIT', { type: 'MIT' })).toBe(false) |
| 54 | + expect(licenseChanged({ type: 'GPL-2.0' }, { type: 'MIT' })).toBe(true) |
| 55 | + }) |
| 56 | + |
| 57 | + it('returns true for Apache-2.0 changed to MIT (real-world case)', () => { |
| 58 | + expect(licenseChanged('Apache-2.0', 'MIT')).toBe(true) |
| 59 | + }) |
| 60 | + |
| 61 | + it('returns false for complex SPDX expression that matches', () => { |
| 62 | + expect(licenseChanged('MIT OR Apache-2.0', 'MIT OR Apache-2.0')).toBe(false) |
| 63 | + }) |
| 64 | +}) |
0 commit comments