add suggested tests from code review

This commit is contained in:
Michael Bolin 2025-12-01 10:07:10 -08:00
commit 4b20f15c72

View file

@ -47,6 +47,36 @@ describe("generateDefaultValue", () => {
expect(generateDefaultValue({ type: "object" })).toEqual({});
});
test("generates undefined for nested optional object", () => {
// When called WITH propertyName and parentSchema, and the property is NOT required,
// nested optional objects should return undefined
const parentSchema = {
type: "object" as const,
required: ["otherField"],
properties: {
optionalObject: { type: "object" as const },
otherField: { type: "string" as const },
},
};
expect(
generateDefaultValue({ type: "object" }, "optionalObject", parentSchema),
).toBe(undefined);
});
test("generates empty object for root-level object with all optional properties", () => {
// Root-level schema with properties but no required array
// This is the exact scenario from PR #926 - elicitation with all optional fields
const schema: JsonSchemaType = {
type: "object",
properties: {
optionalField1: { type: "string" },
optionalField2: { type: "number" },
},
// No required array - all fields are optional
};
expect(generateDefaultValue(schema)).toEqual({});
});
test("generates default null for unknown types", () => {
// @ts-expect-error Testing with invalid type
expect(generateDefaultValue({ type: "unknown" })).toBe(undefined);