11import { registerGetPackageSchemaTool } from '../../tools/get-package-schema.js' ;
2+ import { fetchPackageSchema } from '@walkeros/core' ;
23
3- // Mock fetch globally
4- const mockFetch = jest . fn ( ) ;
5- global . fetch = mockFetch ;
4+ jest . mock ( '@walkeros/core' , ( ) => ( {
5+ fetchPackageSchema : jest . fn ( ) ,
6+ } ) ) ;
7+
8+ const mockFetchPackageSchema = fetchPackageSchema as jest . MockedFunction <
9+ typeof fetchPackageSchema
10+ > ;
611
712function createMockServer ( ) {
813 const tools : Record < string , { config : unknown ; handler : Function } > = { } ;
@@ -30,41 +35,23 @@ describe('get-package-schema tool', () => {
3035 } ) ;
3136
3237 it ( 'should fetch package.json then walkerOS.json from jsdelivr' , async ( ) => {
33- const mockPkgJson = {
34- name : '@walkeros/web-destination-snowplow' ,
38+ mockFetchPackageSchema . mockResolvedValue ( {
39+ packageName : '@walkeros/web-destination-snowplow' ,
3540 version : '0.0.12' ,
36- walkerOS : {
37- type : 'destination' ,
38- platform : 'web' ,
39- schema : './dist/dev/walkerOS.json' ,
40- } ,
41- } ;
42- const mockWalkerOSJson = {
41+ type : 'destination' ,
42+ platform : 'web' ,
4343 schemas : { settings : { type : 'object' , properties : { } } } ,
4444 examples : { mapping : { } } ,
45- } ;
46-
47- mockFetch
48- . mockResolvedValueOnce ( {
49- ok : true ,
50- json : ( ) => Promise . resolve ( mockPkgJson ) ,
51- } )
52- . mockResolvedValueOnce ( {
53- ok : true ,
54- json : ( ) => Promise . resolve ( mockWalkerOSJson ) ,
55- } ) ;
45+ } ) ;
5646
5747 const tool = mockServer . getTool ( 'get-package-schema' ) ;
5848 const result = await tool . handler ( {
5949 package : '@walkeros/web-destination-snowplow' ,
6050 } ) ;
6151
62- expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 ) ;
63- expect ( mockFetch ) . toHaveBeenCalledWith (
64- expect . stringContaining (
65- 'cdn.jsdelivr.net/npm/@walkeros/web-destination-snowplow' ,
66- ) ,
67- expect . objectContaining ( { signal : expect . any ( AbortSignal ) } ) ,
52+ expect ( mockFetchPackageSchema ) . toHaveBeenCalledWith (
53+ '@walkeros/web-destination-snowplow' ,
54+ { version : undefined } ,
6855 ) ;
6956
7057 const content = JSON . parse ( result . content [ 0 ] . text ) ;
@@ -74,64 +61,60 @@ describe('get-package-schema tool', () => {
7461 } ) ;
7562
7663 it ( 'should use default schema path when walkerOS field is missing' , async ( ) => {
77- mockFetch
78- . mockResolvedValueOnce ( {
79- ok : true ,
80- json : ( ) => Promise . resolve ( { name : 'some-pkg' , version : '1.0.0' } ) ,
81- } )
82- . mockResolvedValueOnce ( {
83- ok : true ,
84- json : ( ) => Promise . resolve ( { schemas : { settings : { } } } ) ,
85- } ) ;
64+ mockFetchPackageSchema . mockResolvedValue ( {
65+ packageName : 'some-pkg' ,
66+ version : '1.0.0' ,
67+ type : undefined ,
68+ platform : undefined ,
69+ schemas : { settings : { } } ,
70+ examples : { } ,
71+ } ) ;
8672
8773 const tool = mockServer . getTool ( 'get-package-schema' ) ;
88- await tool . handler ( { package : 'some-pkg' } ) ;
74+ const result = await tool . handler ( { package : 'some-pkg' } ) ;
8975
90- expect ( mockFetch ) . toHaveBeenNthCalledWith (
91- 2 ,
92- expect . stringContaining ( 'dist/dev/walkerOS.json' ) ,
93- expect . objectContaining ( { signal : expect . any ( AbortSignal ) } ) ,
94- ) ;
76+ expect ( mockFetchPackageSchema ) . toHaveBeenCalledWith ( 'some-pkg' , {
77+ version : undefined ,
78+ } ) ;
79+ const content = JSON . parse ( result . content [ 0 ] . text ) ;
80+ expect ( content . package ) . toBe ( 'some-pkg' ) ;
9581 } ) ;
9682
9783 it ( 'should return error when package not found' , async ( ) => {
98- mockFetch . mockResolvedValueOnce ( { ok : false , status : 404 } ) ;
84+ mockFetchPackageSchema . mockRejectedValue (
85+ new Error ( 'Package "nonexistent" not found on npm (HTTP 404)' ) ,
86+ ) ;
9987
10088 const tool = mockServer . getTool ( 'get-package-schema' ) ;
10189 const result = await tool . handler ( { package : 'nonexistent' } ) ;
10290 expect ( result . isError ) . toBe ( true ) ;
10391 } ) ;
10492
10593 it ( 'should return error when walkerOS.json not found' , async ( ) => {
106- mockFetch
107- . mockResolvedValueOnce ( {
108- ok : true ,
109- json : ( ) => Promise . resolve ( { name : 'pkg' , version : '1.0.0' } ) ,
110- } )
111- . mockResolvedValueOnce ( { ok : false , status : 404 } ) ;
94+ mockFetchPackageSchema . mockRejectedValue (
95+ new Error ( 'walkerOS.json not found at dist/dev/walkerOS.json (HTTP 404)' ) ,
96+ ) ;
11297
11398 const tool = mockServer . getTool ( 'get-package-schema' ) ;
11499 const result = await tool . handler ( { package : 'pkg' } ) ;
115100 expect ( result . isError ) . toBe ( true ) ;
116101 } ) ;
117102
118103 it ( 'should support version parameter' , async ( ) => {
119- mockFetch
120- . mockResolvedValueOnce ( {
121- ok : true ,
122- json : ( ) => Promise . resolve ( { name : 'pkg' , version : '2.0.0' } ) ,
123- } )
124- . mockResolvedValueOnce ( {
125- ok : true ,
126- json : ( ) => Promise . resolve ( { schemas : { } } ) ,
127- } ) ;
104+ mockFetchPackageSchema . mockResolvedValue ( {
105+ packageName : 'pkg' ,
106+ version : '2.0.0' ,
107+ type : undefined ,
108+ platform : undefined ,
109+ schemas : { } ,
110+ examples : { } ,
111+ } ) ;
128112
129113 const tool = mockServer . getTool ( 'get-package-schema' ) ;
130114 await tool . handler ( { package : 'pkg' , version : '2.0.0' } ) ;
131115
132- expect ( mockFetch ) . toHaveBeenCalledWith (
133- expect . stringContaining ( 'pkg@2.0.0' ) ,
134- expect . objectContaining ( { signal : expect . any ( AbortSignal ) } ) ,
135- ) ;
116+ expect ( mockFetchPackageSchema ) . toHaveBeenCalledWith ( 'pkg' , {
117+ version : '2.0.0' ,
118+ } ) ;
136119 } ) ;
137120} ) ;
0 commit comments