Update ToolsTab tests to require JSON fields and adjust default value generation for optional types
This commit is contained in:
parent
69c294c0ae
commit
a11ee99818
3 changed files with 48 additions and 19 deletions
|
|
@ -644,6 +644,7 @@ describe("ToolsTab", () => {
|
||||||
description: "Tool with JSON parameters",
|
description: "Tool with JSON parameters",
|
||||||
inputSchema: {
|
inputSchema: {
|
||||||
type: "object" as const,
|
type: "object" as const,
|
||||||
|
required: ["config", "data"], // Make them required so they render as form fields
|
||||||
properties: {
|
properties: {
|
||||||
config: {
|
config: {
|
||||||
type: "object" as const,
|
type: "object" as const,
|
||||||
|
|
@ -693,15 +694,16 @@ describe("ToolsTab", () => {
|
||||||
callTool: mockCallTool,
|
callTool: mockCallTool,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Find JSON editor textareas
|
// Find JSON editor textareas (should have one for each required field: config and data)
|
||||||
const textareas = screen.getAllByRole("textbox");
|
const textareas = screen.getAllByRole("textbox");
|
||||||
|
expect(textareas.length).toBe(2);
|
||||||
|
|
||||||
// Enter valid JSON in the first textarea
|
// Enter valid JSON in each textarea
|
||||||
fireEvent.change(textareas[0], {
|
fireEvent.change(textareas[0], {
|
||||||
target: {
|
target: { value: '{ "setting": "value" }' },
|
||||||
value:
|
});
|
||||||
'{ "config": { "setting": "value" }, "data": ["item1", "item2"] }',
|
fireEvent.change(textareas[1], {
|
||||||
},
|
target: { value: '["item1", "item2"]' },
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wait for debounced updates
|
// Wait for debounced updates
|
||||||
|
|
@ -799,9 +801,16 @@ describe("ToolsTab", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
const textareas = screen.getAllByRole("textbox");
|
const textareas = screen.getAllByRole("textbox");
|
||||||
|
expect(textareas.length).toBe(2);
|
||||||
|
|
||||||
// Clear the textarea (empty JSON should be valid)
|
// Clear both textareas (empty JSON should be valid)
|
||||||
fireEvent.change(textareas[0], { target: { value: "" } });
|
fireEvent.change(textareas[0], { target: { value: "{}" } });
|
||||||
|
fireEvent.change(textareas[1], { target: { value: "[]" } });
|
||||||
|
|
||||||
|
// Wait for debounced updates
|
||||||
|
await act(async () => {
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 350));
|
||||||
|
});
|
||||||
|
|
||||||
// Try to run the tool
|
// Try to run the tool
|
||||||
const runButton = screen.getByRole("button", { name: /run tool/i });
|
const runButton = screen.getByRole("button", { name: /run tool/i });
|
||||||
|
|
|
||||||
|
|
@ -39,12 +39,12 @@ describe("generateDefaultValue", () => {
|
||||||
).toBe(false);
|
).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("generates default array", () => {
|
test("generates undefined for optional array", () => {
|
||||||
expect(generateDefaultValue({ type: "array" })).toEqual([]);
|
expect(generateDefaultValue({ type: "array" })).toBe(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("generates default empty object", () => {
|
test("generates undefined for optional object", () => {
|
||||||
expect(generateDefaultValue({ type: "object" })).toEqual({});
|
expect(generateDefaultValue({ type: "object" })).toBe(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("generates default null for unknown types", () => {
|
test("generates default null for unknown types", () => {
|
||||||
|
|
@ -52,12 +52,32 @@ describe("generateDefaultValue", () => {
|
||||||
expect(generateDefaultValue({ type: "unknown" })).toBe(undefined);
|
expect(generateDefaultValue({ type: "unknown" })).toBe(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("generates empty array for non-required array", () => {
|
test("generates empty array for required array", () => {
|
||||||
expect(generateDefaultValue({ type: "array" })).toEqual([]);
|
const parentSchema = { required: ["testArray"] };
|
||||||
|
expect(
|
||||||
|
generateDefaultValue({ type: "array" }, "testArray", parentSchema),
|
||||||
|
).toEqual([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("generates empty object for non-required object", () => {
|
test("generates undefined for non-required array", () => {
|
||||||
expect(generateDefaultValue({ type: "object" })).toEqual({});
|
const parentSchema = { required: ["otherField"] };
|
||||||
|
expect(
|
||||||
|
generateDefaultValue({ type: "array" }, "testArray", parentSchema),
|
||||||
|
).toBe(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("generates empty object for required object", () => {
|
||||||
|
const parentSchema = { required: ["testObject"] };
|
||||||
|
expect(
|
||||||
|
generateDefaultValue({ type: "object" }, "testObject", parentSchema),
|
||||||
|
).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("generates undefined for non-required object", () => {
|
||||||
|
const parentSchema = { required: ["otherField"] };
|
||||||
|
expect(
|
||||||
|
generateDefaultValue({ type: "object" }, "testObject", parentSchema),
|
||||||
|
).toBe(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("generates undefined for non-required primitive types", () => {
|
test("generates undefined for non-required primitive types", () => {
|
||||||
|
|
|
||||||
|
|
@ -109,9 +109,9 @@ export function generateDefaultValue(
|
||||||
case "boolean":
|
case "boolean":
|
||||||
return isRequired ? false : undefined;
|
return isRequired ? false : undefined;
|
||||||
case "array":
|
case "array":
|
||||||
return [];
|
return isRequired ? [] : undefined;
|
||||||
case "object": {
|
case "object": {
|
||||||
if (!schema.properties) return {};
|
if (!schema.properties) return isRequired ? {} : undefined;
|
||||||
|
|
||||||
const obj: JsonObject = {};
|
const obj: JsonObject = {};
|
||||||
// Only include properties that are required according to the schema's required array
|
// Only include properties that are required according to the schema's required array
|
||||||
|
|
@ -123,7 +123,7 @@ export function generateDefaultValue(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return obj;
|
return isRequired ? obj : Object.keys(obj).length > 0 ? obj : undefined;
|
||||||
}
|
}
|
||||||
case "null":
|
case "null":
|
||||||
return null;
|
return null;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue