Skip to content

Commit a11ee99

Browse files
committed
Update ToolsTab tests to require JSON fields and adjust default value generation for optional types
1 parent 69c294c commit a11ee99

File tree

3 files changed

+48
-19
lines changed

3 files changed

+48
-19
lines changed

client/src/components/__tests__/ToolsTab.test.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ describe("ToolsTab", () => {
644644
description: "Tool with JSON parameters",
645645
inputSchema: {
646646
type: "object" as const,
647+
required: ["config", "data"], // Make them required so they render as form fields
647648
properties: {
648649
config: {
649650
type: "object" as const,
@@ -693,15 +694,16 @@ describe("ToolsTab", () => {
693694
callTool: mockCallTool,
694695
});
695696

696-
// Find JSON editor textareas
697+
// Find JSON editor textareas (should have one for each required field: config and data)
697698
const textareas = screen.getAllByRole("textbox");
699+
expect(textareas.length).toBe(2);
698700

699-
// Enter valid JSON in the first textarea
701+
// Enter valid JSON in each textarea
700702
fireEvent.change(textareas[0], {
701-
target: {
702-
value:
703-
'{ "config": { "setting": "value" }, "data": ["item1", "item2"] }',
704-
},
703+
target: { value: '{ "setting": "value" }' },
704+
});
705+
fireEvent.change(textareas[1], {
706+
target: { value: '["item1", "item2"]' },
705707
});
706708

707709
// Wait for debounced updates
@@ -799,9 +801,16 @@ describe("ToolsTab", () => {
799801
});
800802

801803
const textareas = screen.getAllByRole("textbox");
804+
expect(textareas.length).toBe(2);
805+
806+
// Clear both textareas (empty JSON should be valid)
807+
fireEvent.change(textareas[0], { target: { value: "{}" } });
808+
fireEvent.change(textareas[1], { target: { value: "[]" } });
802809

803-
// Clear the textarea (empty JSON should be valid)
804-
fireEvent.change(textareas[0], { target: { value: "" } });
810+
// Wait for debounced updates
811+
await act(async () => {
812+
await new Promise((resolve) => setTimeout(resolve, 350));
813+
});
805814

806815
// Try to run the tool
807816
const runButton = screen.getByRole("button", { name: /run tool/i });

client/src/utils/__tests__/schemaUtils.test.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,45 @@ describe("generateDefaultValue", () => {
3939
).toBe(false);
4040
});
4141

42-
test("generates default array", () => {
43-
expect(generateDefaultValue({ type: "array" })).toEqual([]);
42+
test("generates undefined for optional array", () => {
43+
expect(generateDefaultValue({ type: "array" })).toBe(undefined);
4444
});
4545

46-
test("generates default empty object", () => {
47-
expect(generateDefaultValue({ type: "object" })).toEqual({});
46+
test("generates undefined for optional object", () => {
47+
expect(generateDefaultValue({ type: "object" })).toBe(undefined);
4848
});
4949

5050
test("generates default null for unknown types", () => {
5151
// @ts-expect-error Testing with invalid type
5252
expect(generateDefaultValue({ type: "unknown" })).toBe(undefined);
5353
});
5454

55-
test("generates empty array for non-required array", () => {
56-
expect(generateDefaultValue({ type: "array" })).toEqual([]);
55+
test("generates empty array for required array", () => {
56+
const parentSchema = { required: ["testArray"] };
57+
expect(
58+
generateDefaultValue({ type: "array" }, "testArray", parentSchema),
59+
).toEqual([]);
60+
});
61+
62+
test("generates undefined for non-required array", () => {
63+
const parentSchema = { required: ["otherField"] };
64+
expect(
65+
generateDefaultValue({ type: "array" }, "testArray", parentSchema),
66+
).toBe(undefined);
5767
});
5868

59-
test("generates empty object for non-required object", () => {
60-
expect(generateDefaultValue({ type: "object" })).toEqual({});
69+
test("generates empty object for required object", () => {
70+
const parentSchema = { required: ["testObject"] };
71+
expect(
72+
generateDefaultValue({ type: "object" }, "testObject", parentSchema),
73+
).toEqual({});
74+
});
75+
76+
test("generates undefined for non-required object", () => {
77+
const parentSchema = { required: ["otherField"] };
78+
expect(
79+
generateDefaultValue({ type: "object" }, "testObject", parentSchema),
80+
).toBe(undefined);
6181
});
6282

6383
test("generates undefined for non-required primitive types", () => {

client/src/utils/schemaUtils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ export function generateDefaultValue(
109109
case "boolean":
110110
return isRequired ? false : undefined;
111111
case "array":
112-
return [];
112+
return isRequired ? [] : undefined;
113113
case "object": {
114-
if (!schema.properties) return {};
114+
if (!schema.properties) return isRequired ? {} : undefined;
115115

116116
const obj: JsonObject = {};
117117
// Only include properties that are required according to the schema's required array
@@ -123,7 +123,7 @@ export function generateDefaultValue(
123123
}
124124
}
125125
});
126-
return obj;
126+
return isRequired ? obj : Object.keys(obj).length > 0 ? obj : undefined;
127127
}
128128
case "null":
129129
return null;

0 commit comments

Comments
 (0)