1- import { describe , expect , it , vi } from 'vitest'
1+ import { beforeEach , describe , expect , it , vi } from 'vitest'
22
3- import { fetchPurlDeepScore } from './fetch-purl-deep-score.mts'
4- import { setupStandardOutputMocks } from '../../../test/helpers/mock-setup.mts'
53import {
64 createErrorResult ,
75 createSuccessResult ,
86} from '../../../test/helpers/mocks.mts'
97
10- // Mock the dependencies.
11- setupStandardOutputMocks ( )
8+ import type { PurlDataResponse } from './fetch-purl-deep-score.mts'
129
13- vi . mock ( '../../utils/socket/api.mts' , ( ) => ( {
10+ vi . mock ( '@socketsecurity/lib/logger' , ( ) => ( {
11+ logger : {
12+ info : vi . fn ( ) ,
13+ } ,
14+ } ) )
15+
16+ vi . mock ( '../../utils/socket/api.mjs' , ( ) => ( {
1417 queryApiSafeJson : vi . fn ( ) ,
1518} ) )
1619
1720describe ( 'fetchPurlDeepScore' , ( ) => {
21+ beforeEach ( ( ) => {
22+ vi . clearAllMocks ( )
23+ } )
24+
1825 it ( 'fetches purl deep score successfully' , async ( ) => {
19- const { queryApiSafeJson } = await import ( '../../utils/socket/api.mts' )
26+ const { fetchPurlDeepScore } = await import ( './fetch-purl-deep-score.mts' )
27+ const { queryApiSafeJson } = await vi . importMock ( '../../utils/socket/api.mjs' )
2028 const mockQueryApi = vi . mocked ( queryApiSafeJson )
2129
22- const mockData = {
30+ const mockData : PurlDataResponse = {
2331 purl : 'pkg:npm/lodash@4.17.21' ,
2432 self : {
2533 purl : 'pkg:npm/lodash@4.17.21' ,
@@ -67,26 +75,14 @@ describe('fetchPurlDeepScore', () => {
6775 'the deep package scores' ,
6876 )
6977 expect ( result . ok ) . toBe ( true )
70- expect ( result . data ) . toEqual ( mockData )
71- } )
72-
73- it ( 'handles SDK setup failure' , async ( ) => {
74- const { queryApiSafeJson } = await import ( '../../utils/socket/api.mts' )
75- const mockQueryApi = vi . mocked ( queryApiSafeJson )
76-
77- const error = createErrorResult ( 'Failed to fetch purl score' , {
78- code : 1 ,
79- cause : 'Configuration error' ,
80- } )
81- mockQueryApi . mockResolvedValue ( error )
82-
83- const result = await fetchPurlDeepScore ( 'pkg:npm/express@4.18.2' )
84-
85- expect ( result ) . toEqual ( error )
78+ if ( result . ok ) {
79+ expect ( result . data ) . toEqual ( mockData )
80+ }
8681 } )
8782
8883 it ( 'handles API call failure' , async ( ) => {
89- const { queryApiSafeJson } = await import ( '../../utils/socket/api.mts' )
84+ const { fetchPurlDeepScore } = await import ( './fetch-purl-deep-score.mts' )
85+ const { queryApiSafeJson } = await vi . importMock ( '../../utils/socket/api.mjs' )
9086 const mockQueryApi = vi . mocked ( queryApiSafeJson )
9187
9288 mockQueryApi . mockResolvedValue (
@@ -96,28 +92,17 @@ describe('fetchPurlDeepScore', () => {
9692 const result = await fetchPurlDeepScore ( 'pkg:npm/nonexistent@1.0.0' )
9793
9894 expect ( result . ok ) . toBe ( false )
99- expect ( result . code ) . toBe ( 404 )
100- } )
101-
102- it ( 'passes custom SDK options' , async ( ) => {
103- const { queryApiSafeJson } = await import ( '../../utils/socket/api.mts' )
104- const mockQueryApi = vi . mocked ( queryApiSafeJson )
105-
106- mockQueryApi . mockResolvedValue ( createSuccessResult ( { } ) )
107-
108- await fetchPurlDeepScore ( 'pkg:npm/react@18.0.0' )
109-
110- expect ( mockQueryApi ) . toHaveBeenCalledWith (
111- 'purl/score/pkg%3Anpm%2Freact%4018.0.0' ,
112- 'the deep package scores' ,
113- )
95+ if ( ! result . ok ) {
96+ expect ( result . code ) . toBe ( 404 )
97+ }
11498 } )
11599
116100 it ( 'handles different purl formats' , async ( ) => {
117- const { queryApiSafeJson } = await import ( '../../utils/socket/api.mts' )
101+ const { fetchPurlDeepScore } = await import ( './fetch-purl-deep-score.mts' )
102+ const { queryApiSafeJson } = await vi . importMock ( '../../utils/socket/api.mjs' )
118103 const mockQueryApi = vi . mocked ( queryApiSafeJson )
119104
120- mockQueryApi . mockResolvedValue ( createSuccessResult ( { } ) )
105+ mockQueryApi . mockResolvedValue ( createSuccessResult ( { } as PurlDataResponse ) )
121106
122107 const purl = 'pkg:npm/lodash@4.17.21'
123108 await fetchPurlDeepScore ( purl )
@@ -129,10 +114,11 @@ describe('fetchPurlDeepScore', () => {
129114 } )
130115
131116 it ( 'handles low score packages' , async ( ) => {
132- const { queryApiSafeJson } = await import ( '../../utils/socket/api.mts' )
117+ const { fetchPurlDeepScore } = await import ( './fetch-purl-deep-score.mts' )
118+ const { queryApiSafeJson } = await vi . importMock ( '../../utils/socket/api.mjs' )
133119 const mockQueryApi = vi . mocked ( queryApiSafeJson )
134120
135- const lowScoreData = {
121+ const lowScoreData : PurlDataResponse = {
136122 purl : 'pkg:npm/vulnerable@0.1.0' ,
137123 self : {
138124 purl : 'pkg:npm/vulnerable@0.1.0' ,
@@ -183,19 +169,8 @@ describe('fetchPurlDeepScore', () => {
183169 const result = await fetchPurlDeepScore ( 'pkg:npm/vulnerable@0.1.0' )
184170
185171 expect ( result . ok ) . toBe ( true )
186- expect ( result . data . self . score . overall ) . toBeLessThan ( 30 )
187- } )
188-
189- it ( 'uses null prototype for options' , async ( ) => {
190- const { queryApiSafeJson } = await import ( '../../utils/socket/api.mts' )
191- const mockQueryApi = vi . mocked ( queryApiSafeJson )
192-
193- mockQueryApi . mockResolvedValue ( createSuccessResult ( { } ) )
194-
195- // This tests that the function properly uses __proto__: null.
196- await fetchPurlDeepScore ( 'pkg:npm/test@1.0.0' )
197-
198- // The function should work without prototype pollution issues.
199- expect ( mockQueryApi ) . toHaveBeenCalled ( )
172+ if ( result . ok ) {
173+ expect ( result . data . self . score . overall ) . toBeLessThan ( 30 )
174+ }
200175 } )
201176} )
0 commit comments