Skip to content

Commit 6c809bd

Browse files
committed
test: new elicitation format for jsonUtils
1 parent fc939eb commit 6c809bd

File tree

1 file changed

+110
-1
lines changed

1 file changed

+110
-1
lines changed

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

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
updateValueAtPath,
55
getValueAtPath,
66
} from "../jsonUtils";
7-
import type { JsonValue } from "../jsonUtils";
7+
import type { JsonValue, JsonSchemaType } from "../jsonUtils";
88

99
describe("getDataType", () => {
1010
test("should return 'string' for string values", () => {
@@ -317,3 +317,112 @@ describe("getValueAtPath", () => {
317317
expect(getValueAtPath(obj, ["users", "1", "name"])).toBe("Jane");
318318
});
319319
});
320+
321+
describe("JsonSchemaType elicitation field support", () => {
322+
const sampleSchema: JsonSchemaType = {
323+
type: "object",
324+
title: "User Info",
325+
description: "User information form",
326+
properties: {
327+
name: {
328+
type: "string",
329+
title: "Full Name",
330+
description: "Your full name",
331+
minLength: 2,
332+
maxLength: 50,
333+
pattern: "^[A-Za-z\\s]+$",
334+
},
335+
email: {
336+
type: "string",
337+
format: "email",
338+
title: "Email Address",
339+
},
340+
age: {
341+
type: "integer",
342+
minimum: 18,
343+
maximum: 120,
344+
default: 25,
345+
},
346+
role: {
347+
type: "string",
348+
enum: ["admin", "user", "guest"],
349+
enumNames: ["Administrator", "User", "Guest"],
350+
},
351+
},
352+
required: ["name", "email"],
353+
};
354+
355+
test("should parse JsonSchemaType with elicitation fields", () => {
356+
const schemaString = JSON.stringify(sampleSchema);
357+
const result = tryParseJson(schemaString);
358+
359+
expect(result.success).toBe(true);
360+
expect(result.data).toEqual(sampleSchema);
361+
});
362+
363+
test("should update schema properties with new validation fields", () => {
364+
const updated = updateValueAtPath(
365+
sampleSchema,
366+
["properties", "name", "minLength"],
367+
5,
368+
);
369+
370+
expect(getValueAtPath(updated, ["properties", "name", "minLength"])).toBe(
371+
5,
372+
);
373+
});
374+
375+
test("should handle enum and enumNames fields", () => {
376+
const schema = {
377+
type: "string" as const,
378+
enum: ["option1", "option2"],
379+
enumNames: ["Option 1", "Option 2"],
380+
};
381+
382+
expect(getValueAtPath(schema, ["enum", "0"])).toBe("option1");
383+
expect(getValueAtPath(schema, ["enumNames", "1"])).toBe("Option 2");
384+
});
385+
386+
test("should handle validation constraints", () => {
387+
const numberSchema = {
388+
type: "number" as const,
389+
minimum: 0,
390+
maximum: 100,
391+
default: 50,
392+
};
393+
394+
expect(getValueAtPath(numberSchema, ["minimum"])).toBe(0);
395+
expect(getValueAtPath(numberSchema, ["maximum"])).toBe(100);
396+
expect(getValueAtPath(numberSchema, ["default"])).toBe(50);
397+
});
398+
399+
test("should handle string format and pattern fields", () => {
400+
const stringSchema = {
401+
type: "string" as const,
402+
format: "email",
403+
pattern: "^[a-z]+@[a-z]+\\.[a-z]+$",
404+
minLength: 5,
405+
maxLength: 100,
406+
};
407+
408+
expect(getValueAtPath(stringSchema, ["format"])).toBe("email");
409+
expect(getValueAtPath(stringSchema, ["pattern"])).toBe(
410+
"^[a-z]+@[a-z]+\\.[a-z]+$",
411+
);
412+
expect(getValueAtPath(stringSchema, ["minLength"])).toBe(5);
413+
});
414+
415+
test("should handle title and description fields", () => {
416+
const schema = {
417+
type: "boolean" as const,
418+
title: "Accept Terms",
419+
description: "Do you accept the terms and conditions?",
420+
default: false,
421+
};
422+
423+
expect(getValueAtPath(schema, ["title"])).toBe("Accept Terms");
424+
expect(getValueAtPath(schema, ["description"])).toBe(
425+
"Do you accept the terms and conditions?",
426+
);
427+
});
428+
});

0 commit comments

Comments
 (0)