@@ -5,7 +5,18 @@ jest.mock('nanoid', () => ({
55 nanoid : jest . fn ( ( ) => 'mock-id-123' ) ,
66} ) )
77
8- import { createRequest , createResponse , createNotification , generateId , McpError } from '../src/shared/utils.js'
8+ import {
9+ createRequest ,
10+ createResponse ,
11+ createNotification ,
12+ generateId ,
13+ McpError ,
14+ isRequest ,
15+ isResponse ,
16+ isNotification ,
17+ isNode ,
18+ isBrowser ,
19+ } from '../src/shared/utils.js'
920import { ErrorCode } from '../src/types.js'
1021
1122describe ( 'Utils' , ( ) => {
@@ -94,5 +105,117 @@ describe('Utils', () => {
94105 throw new McpError ( ErrorCode . INTERNAL_ERROR , 'Something went wrong' )
95106 } ) . toThrow ( McpError )
96107 } )
108+
109+ it ( 'should serialize to JSON correctly' , ( ) => {
110+ const error = new McpError ( ErrorCode . INVALID_PARAMS , 'Invalid parameters' , { field : 'name' } )
111+ const json = error . toJSON ( )
112+
113+ expect ( json ) . toEqual ( {
114+ code : ErrorCode . INVALID_PARAMS ,
115+ message : 'Invalid parameters' ,
116+ data : { field : 'name' } ,
117+ } )
118+ } )
119+
120+ it ( 'should serialize to JSON without data when not provided' , ( ) => {
121+ const error = new McpError ( ErrorCode . INTERNAL_ERROR , 'Internal error' )
122+ const json = error . toJSON ( )
123+
124+ expect ( json ) . toEqual ( {
125+ code : ErrorCode . INTERNAL_ERROR ,
126+ message : 'Internal error' ,
127+ } )
128+ expect ( 'data' in json ) . toBe ( false )
129+ } )
130+ } )
131+
132+ describe ( 'isRequest' , ( ) => {
133+ it ( 'should return true for valid request' , ( ) => {
134+ const request = { jsonrpc : '2.0' , id : '123' , method : 'test' }
135+ expect ( isRequest ( request ) ) . toBe ( true )
136+ } )
137+
138+ it ( 'should return false for response' , ( ) => {
139+ const response = { jsonrpc : '2.0' , id : '123' , result : { } }
140+ expect ( isRequest ( response ) ) . toBe ( false )
141+ } )
142+
143+ it ( 'should return false for notification' , ( ) => {
144+ const notification = { jsonrpc : '2.0' , method : 'test' }
145+ expect ( isRequest ( notification ) ) . toBe ( false )
146+ } )
147+
148+ it ( 'should return false for invalid objects' , ( ) => {
149+ expect ( isRequest ( null ) ) . toBe ( false )
150+ expect ( isRequest ( undefined ) ) . toBe ( false )
151+ expect ( isRequest ( { } ) ) . toBe ( false )
152+ expect ( isRequest ( { jsonrpc : '1.0' , id : '1' , method : 'test' } ) ) . toBe ( false )
153+ } )
154+ } )
155+
156+ describe ( 'isResponse' , ( ) => {
157+ it ( 'should return true for valid response with result' , ( ) => {
158+ const response = { jsonrpc : '2.0' , id : '123' , result : { data : 'test' } }
159+ expect ( isResponse ( response ) ) . toBe ( true )
160+ } )
161+
162+ it ( 'should return true for valid response with error' , ( ) => {
163+ const response = { jsonrpc : '2.0' , id : '123' , error : { code : - 32600 , message : 'Invalid' } }
164+ expect ( isResponse ( response ) ) . toBe ( true )
165+ } )
166+
167+ it ( 'should return false for request' , ( ) => {
168+ const request = { jsonrpc : '2.0' , id : '123' , method : 'test' }
169+ expect ( isResponse ( request ) ) . toBe ( false )
170+ } )
171+
172+ it ( 'should return false for notification' , ( ) => {
173+ const notification = { jsonrpc : '2.0' , method : 'test' }
174+ expect ( isResponse ( notification ) ) . toBe ( false )
175+ } )
176+
177+ it ( 'should return false for invalid objects' , ( ) => {
178+ expect ( isResponse ( null ) ) . toBe ( false )
179+ expect ( isResponse ( undefined ) ) . toBe ( false )
180+ expect ( isResponse ( { } ) ) . toBe ( false )
181+ } )
182+ } )
183+
184+ describe ( 'isNotification' , ( ) => {
185+ it ( 'should return true for valid notification' , ( ) => {
186+ const notification = { jsonrpc : '2.0' , method : 'test' }
187+ expect ( isNotification ( notification ) ) . toBe ( true )
188+ } )
189+
190+ it ( 'should return true for notification with params' , ( ) => {
191+ const notification = { jsonrpc : '2.0' , method : 'test' , params : { key : 'value' } }
192+ expect ( isNotification ( notification ) ) . toBe ( true )
193+ } )
194+
195+ it ( 'should return false for request' , ( ) => {
196+ const request = { jsonrpc : '2.0' , id : '123' , method : 'test' }
197+ expect ( isNotification ( request ) ) . toBe ( false )
198+ } )
199+
200+ it ( 'should return false for response' , ( ) => {
201+ const response = { jsonrpc : '2.0' , id : '123' , result : { } }
202+ expect ( isNotification ( response ) ) . toBe ( false )
203+ } )
204+
205+ it ( 'should return false for invalid objects' , ( ) => {
206+ expect ( isNotification ( null ) ) . toBe ( false )
207+ expect ( isNotification ( undefined ) ) . toBe ( false )
208+ expect ( isNotification ( { } ) ) . toBe ( false )
209+ } )
210+ } )
211+
212+ describe ( 'Environment detection' , ( ) => {
213+ it ( 'should detect Node.js environment' , ( ) => {
214+ expect ( isNode ( ) ) . toBe ( true )
215+ } )
216+
217+ it ( 'should not detect browser environment in Node.js' , ( ) => {
218+ expect ( isBrowser ( ) ) . toBe ( false )
219+ } )
97220 } )
98221} )
0 commit comments