1
1
import { render , screen , fireEvent , act } from "@testing-library/react" ;
2
- import { describe , it , expect , jest } from "@jest/globals" ;
3
2
import "@testing-library/jest-dom" ;
3
+ import { describe , it , jest , beforeEach } from "@jest/globals" ;
4
4
import ToolsTab from "../ToolsTab" ;
5
5
import { Tool } from "@modelcontextprotocol/sdk/types.js" ;
6
6
import { Tabs } from "@/components/ui/tabs" ;
7
- import * as schemaUtils from "@/utils/schemaUtils" ;
8
-
9
- // Mock the schemaUtils module
10
- // Note: hasOutputSchema checks if a tool's output schema validator has been compiled and cached
11
- // by cacheToolOutputSchemas. In these tests, we mock it to avoid needing to call
12
- // cacheToolOutputSchemas for every test that uses tools with output schemas.
13
- // This keeps the tests focused on the component's behavior rather than schema compilation.
14
- jest . mock ( "@/utils/schemaUtils" , ( ) => ( {
15
- ...jest . requireActual ( "@/utils/schemaUtils" ) ,
16
- hasOutputSchema : jest . fn ( ) ,
17
- validateToolOutput : jest . fn ( ) ,
18
- } ) ) ;
7
+ import { cacheToolOutputSchemas } from "@/utils/schemaUtils" ;
19
8
20
9
describe ( "ToolsTab" , ( ) => {
21
10
beforeEach ( ( ) => {
22
- jest . clearAllMocks ( ) ;
23
- // Reset to default behavior
24
- ( schemaUtils . hasOutputSchema as jest . Mock ) . mockImplementation (
25
- ( toolName ) => {
26
- // Only tools with outputSchema property should return true
27
- return false ;
28
- } ,
29
- ) ;
30
- ( schemaUtils . validateToolOutput as jest . Mock ) . mockReturnValue ( {
31
- isValid : true ,
32
- error : null ,
33
- } ) ;
11
+ // Clear the output schema cache before each test
12
+ cacheToolOutputSchemas ( [ ] ) ;
34
13
} ) ;
35
14
36
15
const mockTools : Tool [ ] = [
@@ -250,8 +229,8 @@ describe("ToolsTab", () => {
250
229
} ;
251
230
252
231
it ( "should display structured content when present" , ( ) => {
253
- // Mock hasOutputSchema to return true for this tool
254
- ( schemaUtils . hasOutputSchema as jest . Mock ) . mockReturnValue ( true ) ;
232
+ // Cache the tool's output schema so hasOutputSchema returns true
233
+ cacheToolOutputSchemas ( [ toolWithOutputSchema ] ) ;
255
234
256
235
const structuredResult = {
257
236
content : [ ] ,
@@ -272,13 +251,7 @@ describe("ToolsTab", () => {
272
251
} ) ;
273
252
274
253
it ( "should show validation error for invalid structured content" , ( ) => {
275
- // Mock hasOutputSchema to return true for this tool
276
- ( schemaUtils . hasOutputSchema as jest . Mock ) . mockReturnValue ( true ) ;
277
- // Mock the validation to fail
278
- ( schemaUtils . validateToolOutput as jest . Mock ) . mockReturnValue ( {
279
- isValid : false ,
280
- error : "temperature must be number" ,
281
- } ) ;
254
+ cacheToolOutputSchemas ( [ toolWithOutputSchema ] ) ;
282
255
283
256
const invalidResult = {
284
257
content : [ ] ,
@@ -296,8 +269,7 @@ describe("ToolsTab", () => {
296
269
} ) ;
297
270
298
271
it ( "should show error when tool with output schema doesn't return structured content" , ( ) => {
299
- // Mock hasOutputSchema to return true for this tool
300
- ( schemaUtils . hasOutputSchema as jest . Mock ) . mockReturnValue ( true ) ;
272
+ cacheToolOutputSchemas ( [ toolWithOutputSchema ] ) ;
301
273
302
274
const resultWithoutStructured = {
303
275
content : [ { type : "text" , text : "some result" } ] ,
@@ -317,8 +289,7 @@ describe("ToolsTab", () => {
317
289
} ) ;
318
290
319
291
it ( "should show unstructured content title when both structured and unstructured exist" , ( ) => {
320
- // Mock hasOutputSchema to return true for this tool
321
- ( schemaUtils . hasOutputSchema as jest . Mock ) . mockReturnValue ( true ) ;
292
+ cacheToolOutputSchemas ( [ toolWithOutputSchema ] ) ;
322
293
323
294
const resultWithBoth = {
324
295
content : [ { type : "text" , text : '{"temperature": 25}' } ] ,
@@ -350,8 +321,7 @@ describe("ToolsTab", () => {
350
321
} ) ;
351
322
352
323
it ( "should show compatibility check when tool has output schema" , ( ) => {
353
- // Mock hasOutputSchema to return true for this tool
354
- ( schemaUtils . hasOutputSchema as jest . Mock ) . mockReturnValue ( true ) ;
324
+ cacheToolOutputSchemas ( [ toolWithOutputSchema ] ) ;
355
325
356
326
const compatibleResult = {
357
327
content : [ { type : "text" , text : '{"temperature": 25}' } ] ,
0 commit comments