-
-
Notifications
You must be signed in to change notification settings - Fork 307
test: add coverage for critical resolveDependencyTree
#1911
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,48 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import type { PackumentVersion } from '../../../../shared/types' | ||
| import { describe, expect, it, vi, beforeEach } from 'vitest' | ||
| import type { Packument, PackumentVersion } from '../../../../shared/types' | ||
|
|
||
| // Mock Nitro globals before importing the module | ||
| vi.stubGlobal('defineCachedFunction', (fn: Function) => fn) | ||
| vi.stubGlobal('$fetch', vi.fn()) | ||
|
|
||
| const { TARGET_PLATFORM, matchesPlatform, resolveVersion } = | ||
| const mockFetchNpmPackage = vi.fn<(name: string) => Promise<Packument | null>>() | ||
| vi.stubGlobal('fetchNpmPackage', mockFetchNpmPackage) | ||
|
|
||
| const { TARGET_PLATFORM, matchesPlatform, resolveVersion, resolveDependencyTree } = | ||
| await import('../../../../server/utils/dependency-resolver') | ||
|
|
||
| /** | ||
| * Helper to build a minimal Packument for mocking. | ||
| */ | ||
| function makePackument( | ||
| name: string, | ||
| versions: Array<{ | ||
| version: string | ||
| deps?: Record<string, string> | ||
| optionalDeps?: Record<string, string> | ||
| os?: string[] | ||
| cpu?: string[] | ||
| libc?: string[] | ||
| unpackedSize?: number | ||
| deprecated?: string | ||
| }>, | ||
| ): Packument { | ||
| const versionsMap: Record<string, PackumentVersion> = {} | ||
| for (const v of versions) { | ||
| versionsMap[v.version] = { | ||
| version: v.version, | ||
| dependencies: v.deps, | ||
| optionalDependencies: v.optionalDeps, | ||
| os: v.os, | ||
| cpu: v.cpu, | ||
| ...(v.libc ? { libc: v.libc } : {}), | ||
| dist: { unpackedSize: v.unpackedSize }, | ||
| ...(v.deprecated ? { deprecated: v.deprecated } : {}), | ||
| } as unknown as PackumentVersion | ||
| } | ||
| return { name, versions: versionsMap } as Packument | ||
| } | ||
|
|
||
| describe('dependency-resolver', () => { | ||
| describe('TARGET_PLATFORM', () => { | ||
| it('is configured for linux-x64-glibc', () => { | ||
|
|
@@ -149,4 +184,230 @@ describe('dependency-resolver', () => { | |
| expect(resolveVersion('^2.0.0-beta.0', versions)).toBe('2.0.0') | ||
| }) | ||
| }) | ||
|
|
||
| describe('resolveDependencyTree', () => { | ||
| beforeEach(() => { | ||
| mockFetchNpmPackage.mockReset() | ||
| }) | ||
|
|
||
| it('resolves a single package with no dependencies', async () => { | ||
| mockFetchNpmPackage.mockResolvedValue( | ||
| makePackument('root', [{ version: '1.0.0', unpackedSize: 5000 }]), | ||
| ) | ||
|
|
||
| const result = await resolveDependencyTree('root', '1.0.0') | ||
|
|
||
| expect(result.size).toBe(1) | ||
| const pkg = result.get('[email protected]') | ||
| expect(pkg).toEqual({ name: 'root', version: '1.0.0', size: 5000, optional: false }) | ||
| }) | ||
|
|
||
| it('resolves direct dependencies', async () => { | ||
| mockFetchNpmPackage.mockImplementation(async (name: string) => { | ||
| if (name === 'root') | ||
| return makePackument('root', [ | ||
| { | ||
| version: '1.0.0', | ||
| deps: { 'dep-a': '^1.0.0', 'dep-b': '^2.0.0' }, | ||
| unpackedSize: 1000, | ||
| }, | ||
| ]) | ||
| if (name === 'dep-a') | ||
| return makePackument('dep-a', [{ version: '1.2.0', unpackedSize: 2000 }]) | ||
| if (name === 'dep-b') | ||
| return makePackument('dep-b', [{ version: '2.1.0', unpackedSize: 3000 }]) | ||
| return null | ||
| }) | ||
|
|
||
| const result = await resolveDependencyTree('root', '1.0.0') | ||
|
|
||
| expect(result.size).toBe(3) | ||
| expect(result.get('[email protected]')).toMatchObject({ name: 'root', version: '1.0.0' }) | ||
| expect(result.get('[email protected]')).toMatchObject({ | ||
| name: 'dep-a', | ||
| version: '1.2.0', | ||
| size: 2000, | ||
| optional: false, | ||
| }) | ||
| expect(result.get('[email protected]')).toMatchObject({ | ||
| name: 'dep-b', | ||
| version: '2.1.0', | ||
| size: 3000, | ||
| optional: false, | ||
| }) | ||
| }) | ||
|
|
||
| it('resolves transitive dependencies (A → B → C)', async () => { | ||
| mockFetchNpmPackage.mockImplementation(async (name: string) => { | ||
| if (name === 'a') return makePackument('a', [{ version: '1.0.0', deps: { b: '^1.0.0' } }]) | ||
| if (name === 'b') return makePackument('b', [{ version: '1.0.0', deps: { c: '^1.0.0' } }]) | ||
| if (name === 'c') return makePackument('c', [{ version: '1.0.0' }]) | ||
| return null | ||
| }) | ||
|
|
||
| const result = await resolveDependencyTree('a', '1.0.0') | ||
|
|
||
| expect(result.size).toBe(3) | ||
| expect(result.has('[email protected]')).toBe(true) | ||
| expect(result.has('[email protected]')).toBe(true) | ||
| expect(result.has('[email protected]')).toBe(true) | ||
| }) | ||
|
|
||
| it('handles circular dependencies without infinite loop', async () => { | ||
| mockFetchNpmPackage.mockImplementation(async (name: string) => { | ||
| if (name === 'a') return makePackument('a', [{ version: '1.0.0', deps: { b: '^1.0.0' } }]) | ||
| if (name === 'b') return makePackument('b', [{ version: '1.0.0', deps: { a: '^1.0.0' } }]) | ||
| return null | ||
| }) | ||
|
|
||
| const result = await resolveDependencyTree('a', '1.0.0') | ||
|
|
||
| expect(result.size).toBe(2) | ||
| expect(result.has('[email protected]')).toBe(true) | ||
| expect(result.has('[email protected]')).toBe(true) | ||
| }) | ||
|
|
||
| it('marks optional dependencies with optional: true', async () => { | ||
| mockFetchNpmPackage.mockImplementation(async (name: string) => { | ||
| if (name === 'root') | ||
| return makePackument('root', [ | ||
| { version: '1.0.0', optionalDeps: { 'opt-dep': '^1.0.0' } }, | ||
| ]) | ||
| if (name === 'opt-dep') | ||
| return makePackument('opt-dep', [{ version: '1.0.0', unpackedSize: 500 }]) | ||
| return null | ||
| }) | ||
|
|
||
| const result = await resolveDependencyTree('root', '1.0.0') | ||
|
|
||
| expect(result.size).toBe(2) | ||
| expect(result.get('[email protected]')).toMatchObject({ optional: true }) | ||
| }) | ||
|
|
||
| it('skips dependencies that do not match the target platform', async () => { | ||
| mockFetchNpmPackage.mockImplementation(async (name: string) => { | ||
| if (name === 'root') | ||
| return makePackument('root', [ | ||
| { version: '1.0.0', deps: { 'darwin-only': '^1.0.0', 'linux-ok': '^1.0.0' } }, | ||
| ]) | ||
| if (name === 'darwin-only') | ||
| return makePackument('darwin-only', [{ version: '1.0.0', os: ['darwin'] }]) | ||
| if (name === 'linux-ok') | ||
| return makePackument('linux-ok', [{ version: '1.0.0', os: ['linux'] }]) | ||
| return null | ||
| }) | ||
|
|
||
| const result = await resolveDependencyTree('root', '1.0.0') | ||
|
|
||
| expect(result.has('[email protected]')).toBe(false) | ||
| expect(result.has('[email protected]')).toBe(true) | ||
| }) | ||
|
|
||
| it('skips dependencies with unresolvable version ranges', async () => { | ||
| mockFetchNpmPackage.mockImplementation(async (name: string) => { | ||
| if (name === 'root') | ||
| return makePackument('root', [{ version: '1.0.0', deps: { missing: '^99.0.0' } }]) | ||
| if (name === 'missing') return makePackument('missing', [{ version: '1.0.0' }]) | ||
| return null | ||
| }) | ||
|
|
||
| const result = await resolveDependencyTree('root', '1.0.0') | ||
|
|
||
| expect(result.size).toBe(1) | ||
| expect(result.has('[email protected]')).toBe(true) | ||
| }) | ||
|
|
||
| it('continues resolving when fetchPackument fails for a dependency', async () => { | ||
| mockFetchNpmPackage.mockImplementation(async (name: string) => { | ||
| if (name === 'root') | ||
| return makePackument('root', [ | ||
| { version: '1.0.0', deps: { broken: '^1.0.0', healthy: '^1.0.0' } }, | ||
| ]) | ||
| if (name === 'broken') return null | ||
| if (name === 'healthy') return makePackument('healthy', [{ version: '1.0.0' }]) | ||
| return null | ||
| }) | ||
|
|
||
| const result = await resolveDependencyTree('root', '1.0.0') | ||
|
|
||
| expect(result.size).toBe(2) | ||
| expect(result.has('[email protected]')).toBe(true) | ||
| expect(result.has('[email protected]')).toBe(true) | ||
| expect(result.has('[email protected]')).toBe(false) | ||
| }) | ||
|
|
||
| it('assigns depth and path when trackDepth is enabled', async () => { | ||
| mockFetchNpmPackage.mockImplementation(async (name: string) => { | ||
| if (name === 'root') | ||
| return makePackument('root', [{ version: '1.0.0', deps: { mid: '^1.0.0' } }]) | ||
| if (name === 'mid') | ||
| return makePackument('mid', [{ version: '1.0.0', deps: { leaf: '^1.0.0' } }]) | ||
| if (name === 'leaf') return makePackument('leaf', [{ version: '1.0.0' }]) | ||
| return null | ||
| }) | ||
|
|
||
| const result = await resolveDependencyTree('root', '1.0.0', { trackDepth: true }) | ||
|
|
||
| expect(result.get('[email protected]')).toMatchObject({ | ||
| depth: 'root', | ||
| path: ['[email protected]'], | ||
| }) | ||
| expect(result.get('[email protected]')).toMatchObject({ | ||
| depth: 'direct', | ||
| path: ['[email protected]', '[email protected]'], | ||
| }) | ||
| expect(result.get('[email protected]')).toMatchObject({ | ||
| depth: 'transitive', | ||
| path: ['[email protected]', '[email protected]', '[email protected]'], | ||
| }) | ||
| }) | ||
|
|
||
| it('does not include depth/path when trackDepth is not enabled', async () => { | ||
| mockFetchNpmPackage.mockResolvedValue(makePackument('root', [{ version: '1.0.0' }])) | ||
|
|
||
| const result = await resolveDependencyTree('root', '1.0.0') | ||
|
|
||
| const pkg = result.get('[email protected]')! | ||
| expect(pkg.depth).toBeUndefined() | ||
| expect(pkg.path).toBeUndefined() | ||
| }) | ||
|
|
||
| it('includes deprecated field on deprecated versions', async () => { | ||
| mockFetchNpmPackage.mockResolvedValue( | ||
| makePackument('root', [{ version: '1.0.0', deprecated: 'Use v2 instead' }]), | ||
| ) | ||
|
|
||
| const result = await resolveDependencyTree('root', '1.0.0') | ||
|
|
||
| expect(result.get('[email protected]')).toMatchObject({ deprecated: 'Use v2 instead' }) | ||
| }) | ||
|
|
||
| it('defaults size to 0 when unpackedSize is missing', async () => { | ||
| mockFetchNpmPackage.mockResolvedValue(makePackument('root', [{ version: '1.0.0' }])) | ||
|
|
||
| const result = await resolveDependencyTree('root', '1.0.0') | ||
|
|
||
| expect(result.get('[email protected]')!.size).toBe(0) | ||
| }) | ||
|
|
||
| it('deduplicates the same name@version appearing via multiple paths', async () => { | ||
| // root → a, root → b, both a and b depend on [email protected] | ||
| mockFetchNpmPackage.mockImplementation(async (name: string) => { | ||
| if (name === 'root') | ||
| return makePackument('root', [{ version: '1.0.0', deps: { a: '^1.0.0', b: '^1.0.0' } }]) | ||
| if (name === 'a') | ||
| return makePackument('a', [{ version: '1.0.0', deps: { shared: '^1.0.0' } }]) | ||
| if (name === 'b') | ||
| return makePackument('b', [{ version: '1.0.0', deps: { shared: '^1.0.0' } }]) | ||
| if (name === 'shared') return makePackument('shared', [{ version: '1.0.0' }]) | ||
| return null | ||
| }) | ||
|
|
||
| const result = await resolveDependencyTree('root', '1.0.0') | ||
|
|
||
| // root + a + b + shared (only once) | ||
| expect(result.size).toBe(4) | ||
| expect(result.has('[email protected]')).toBe(true) | ||
| }) | ||
| }) | ||
| }) | ||
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.
Model fetch failures with rejected promises, not
nullvalues.Line 326 currently returns
nullforbroken, but production failure is a thrown error handled infetchPackument’scatch. This test therefore misses the real failure-handling path it describes.Suggested tweak