-
-
Notifications
You must be signed in to change notification settings - Fork 452
fix: don't flag a license change when there is no previous version #2792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jp-knj
wants to merge
1
commit into
npmx-dev:main
Choose a base branch
from
jp-knj:fix/2720-license-change-no-previous-version
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
210 changes: 210 additions & 0 deletions
210
test/unit/server/api/registry/license-change/pkg.get.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| import { describe, expect, it, vi, beforeEach, afterAll } from 'vitest' | ||
| import { createError, type H3Event } from 'h3' | ||
| import type { Packument, PackumentVersion } from '#shared/types/npm-registry' | ||
|
|
||
| const fetchNpmPackageMock = vi.fn() | ||
| vi.stubGlobal('fetchNpmPackage', fetchNpmPackageMock) | ||
| vi.stubGlobal('defineCachedEventHandler', (fn: Function) => fn) | ||
|
|
||
| let routerParam: string | undefined | ||
| let queryParams: Record<string, string | number> = {} | ||
|
|
||
| vi.stubGlobal('getRouterParam', (_event: unknown, _name: string) => routerParam) | ||
| vi.stubGlobal('getQuery', () => queryParams) | ||
| vi.stubGlobal('createError', createError) | ||
|
|
||
| const handler = (await import('#server/api/registry/license-change/[...pkg].get')).default | ||
|
|
||
| function makePackument(opts: { | ||
| versions: Record<string, Partial<PackumentVersion>> | ||
| time: Record<string, string> | ||
| }): Packument { | ||
| return { | ||
| 'dist-tags': {}, | ||
| 'versions': Object.fromEntries( | ||
| Object.entries(opts.versions).map(([v, data]) => [v, { version: v, ...data }]), | ||
| ), | ||
| 'time': opts.time, | ||
| } as Packument | ||
| } | ||
|
|
||
| const fakeEvent = {} as H3Event | ||
|
|
||
| afterAll(() => { | ||
| vi.unstubAllGlobals() | ||
| }) | ||
|
|
||
| describe('license-change API', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| routerParam = undefined | ||
| queryParams = {} | ||
| }) | ||
|
|
||
| it('throws 400 when package name param is missing', async () => { | ||
| routerParam = undefined | ||
| await expect(handler(fakeEvent)).rejects.toMatchObject({ statusCode: 400 }) | ||
| }) | ||
|
|
||
| it('reports no change for a new package with a single version (issue #2720)', async () => { | ||
| routerParam = 'vsxtools' | ||
|
|
||
| fetchNpmPackageMock.mockResolvedValue( | ||
| makePackument({ | ||
| versions: { '0.0.1': { license: 'MIT' } }, | ||
| time: { '0.0.1': '2024-01-01T00:00:00Z' }, | ||
| }), | ||
| ) | ||
|
|
||
| const result = await handler(fakeEvent) | ||
| expect(result.change).toBeNull() | ||
| }) | ||
|
|
||
| it('reports a change when the license changed between versions', async () => { | ||
| routerParam = 'my-pkg' | ||
|
|
||
| fetchNpmPackageMock.mockResolvedValue( | ||
| makePackument({ | ||
| versions: { | ||
| '1.0.0': { license: 'MIT' }, | ||
| '2.0.0': { license: 'ISC' }, | ||
| }, | ||
| time: { | ||
| '1.0.0': '2024-01-01T00:00:00Z', | ||
| '2.0.0': '2024-06-01T00:00:00Z', | ||
| }, | ||
| }), | ||
| ) | ||
|
|
||
| const result = await handler(fakeEvent) | ||
| expect(result.change).toEqual({ from: 'MIT', to: 'ISC' }) | ||
| }) | ||
|
|
||
| it('reports no change when the license is unchanged between versions', async () => { | ||
| routerParam = 'my-pkg' | ||
|
|
||
| fetchNpmPackageMock.mockResolvedValue( | ||
| makePackument({ | ||
| versions: { | ||
| '1.0.0': { license: 'MIT' }, | ||
| '2.0.0': { license: 'MIT' }, | ||
| }, | ||
| time: { | ||
| '1.0.0': '2024-01-01T00:00:00Z', | ||
| '2.0.0': '2024-06-01T00:00:00Z', | ||
| }, | ||
| }), | ||
| ) | ||
|
|
||
| const result = await handler(fakeEvent) | ||
| expect(result.change).toBeNull() | ||
| }) | ||
|
|
||
| it('defaults to the latest (chronologically newest) version', async () => { | ||
| routerParam = 'my-pkg' | ||
| queryParams = {} | ||
|
|
||
| fetchNpmPackageMock.mockResolvedValue( | ||
| makePackument({ | ||
| versions: { | ||
| '1.0.0': { license: 'MIT' }, | ||
| '2.0.0': { license: 'MIT' }, | ||
| '3.0.0': { license: 'Apache-2.0' }, | ||
| }, | ||
| time: { | ||
| '1.0.0': '2024-01-01T00:00:00Z', | ||
| '2.0.0': '2024-06-01T00:00:00Z', | ||
| '3.0.0': '2025-01-01T00:00:00Z', | ||
| }, | ||
| }), | ||
| ) | ||
|
|
||
| const result = await handler(fakeEvent) | ||
| expect(result.change).toEqual({ from: 'MIT', to: 'Apache-2.0' }) | ||
| }) | ||
|
|
||
| it('compares the requested version against its predecessor', async () => { | ||
| routerParam = 'my-pkg' | ||
| queryParams = { version: '2.0.0' } | ||
|
|
||
| fetchNpmPackageMock.mockResolvedValue( | ||
| makePackument({ | ||
| versions: { | ||
| '1.0.0': { license: 'MIT' }, | ||
| '2.0.0': { license: 'ISC' }, | ||
| '3.0.0': { license: 'Apache-2.0' }, | ||
| }, | ||
| time: { | ||
| '1.0.0': '2024-01-01T00:00:00Z', | ||
| '2.0.0': '2024-06-01T00:00:00Z', | ||
| '3.0.0': '2025-01-01T00:00:00Z', | ||
| }, | ||
| }), | ||
| ) | ||
|
|
||
| const result = await handler(fakeEvent) | ||
| expect(result.change).toEqual({ from: 'MIT', to: 'ISC' }) | ||
| }) | ||
|
|
||
| it('reports no change when the requested version is the oldest', async () => { | ||
| routerParam = 'my-pkg' | ||
| queryParams = { version: '1.0.0' } | ||
|
|
||
| fetchNpmPackageMock.mockResolvedValue( | ||
| makePackument({ | ||
| versions: { | ||
| '1.0.0': { license: 'MIT' }, | ||
| '2.0.0': { license: 'ISC' }, | ||
| }, | ||
| time: { | ||
| '1.0.0': '2024-01-01T00:00:00Z', | ||
| '2.0.0': '2024-06-01T00:00:00Z', | ||
| }, | ||
| }), | ||
| ) | ||
|
|
||
| const result = await handler(fakeEvent) | ||
| expect(result.change).toBeNull() | ||
| }) | ||
|
|
||
| it('reports no change when the requested version is not found', async () => { | ||
| routerParam = 'my-pkg' | ||
| queryParams = { version: '9.9.9' } | ||
|
|
||
| fetchNpmPackageMock.mockResolvedValue( | ||
| makePackument({ | ||
| versions: { | ||
| '1.0.0': { license: 'MIT' }, | ||
| '2.0.0': { license: 'ISC' }, | ||
| }, | ||
| time: { | ||
| '1.0.0': '2024-01-01T00:00:00Z', | ||
| '2.0.0': '2024-06-01T00:00:00Z', | ||
| }, | ||
| }), | ||
| ) | ||
|
|
||
| const result = await handler(fakeEvent) | ||
| expect(result.change).toBeNull() | ||
| }) | ||
|
|
||
| it('normalizes object-shaped licenses by extracting the type field', async () => { | ||
| routerParam = 'my-pkg' | ||
|
|
||
| fetchNpmPackageMock.mockResolvedValue( | ||
| makePackument({ | ||
| versions: { | ||
| '1.0.0': { license: { type: 'MIT' } as never }, | ||
| '2.0.0': { license: { type: 'Apache-2.0', url: 'https://example.com' } as never }, | ||
| }, | ||
| time: { | ||
| '1.0.0': '2024-01-01T00:00:00Z', | ||
| '2.0.0': '2024-06-01T00:00:00Z', | ||
| }, | ||
| }), | ||
| ) | ||
|
|
||
| const result = await handler(fakeEvent) | ||
| expect(result.change).toEqual({ from: 'MIT', to: 'Apache-2.0' }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win
Add test coverage for license objects.
The test suite thoroughly validates the guard logic for single-version, multi-version, and version-not-found cases. However, it doesn't cover the scenario where a license is an object (e.g.,
{ type: 'MIT' }) rather than a string. According toshared/types/npm-registry.ts,PackumentLicensecan bestring | { type: string; url?: string }.Adding a test case for object licenses would ensure the handler normalizes them correctly (extracting the
typefield) and prevent regressions.📋 Suggested test case for license objects
🤖 Prompt for AI Agents