validation.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { Type } from "typebox";
  2. import { describe, expect, it } from "vitest";
  3. import type { Tool, ToolCall } from "../src/types.ts";
  4. import { validateToolArguments } from "../src/utils/validation.ts";
  5. function createToolCallWithPlainSchema(
  6. schema: Tool["parameters"],
  7. value: unknown,
  8. ): {
  9. tool: Tool;
  10. toolCall: ToolCall;
  11. } {
  12. const tool: Tool = {
  13. name: "echo",
  14. description: "Echo tool",
  15. parameters: {
  16. type: "object",
  17. properties: {
  18. value: schema,
  19. },
  20. required: ["value"],
  21. } as Tool["parameters"],
  22. };
  23. const toolCall: ToolCall = {
  24. type: "toolCall",
  25. id: "tool-1",
  26. name: "echo",
  27. arguments: { value },
  28. };
  29. return { tool, toolCall };
  30. }
  31. describe("validateToolArguments", () => {
  32. it("still validates when Function constructor is unavailable", () => {
  33. const originalFunction = globalThis.Function;
  34. const tool: Tool = {
  35. name: "echo",
  36. description: "Echo tool",
  37. parameters: Type.Object({
  38. count: Type.Number(),
  39. }),
  40. };
  41. const toolCall: ToolCall = {
  42. type: "toolCall",
  43. id: "tool-1",
  44. name: "echo",
  45. arguments: { count: "42" as unknown as number },
  46. };
  47. globalThis.Function = (() => {
  48. throw new EvalError("Code generation from strings disallowed for this context");
  49. }) as unknown as FunctionConstructor;
  50. try {
  51. expect(validateToolArguments(tool, toolCall)).toEqual({ count: 42 });
  52. } finally {
  53. globalThis.Function = originalFunction;
  54. }
  55. });
  56. it("coerces serialized plain JSON schemas with AJV-compatible primitive rules", () => {
  57. const passingCases: Array<{
  58. schema: Tool["parameters"];
  59. input: unknown;
  60. expected: unknown;
  61. }> = [
  62. { schema: { type: "number" } as Tool["parameters"], input: "42", expected: 42 },
  63. { schema: { type: "number" } as Tool["parameters"], input: true, expected: 1 },
  64. { schema: { type: "number" } as Tool["parameters"], input: null, expected: 0 },
  65. { schema: { type: "integer" } as Tool["parameters"], input: "42", expected: 42 },
  66. { schema: { type: "boolean" } as Tool["parameters"], input: "true", expected: true },
  67. { schema: { type: "boolean" } as Tool["parameters"], input: "false", expected: false },
  68. { schema: { type: "boolean" } as Tool["parameters"], input: 1, expected: true },
  69. { schema: { type: "boolean" } as Tool["parameters"], input: 0, expected: false },
  70. { schema: { type: "string" } as Tool["parameters"], input: null, expected: "" },
  71. { schema: { type: "string" } as Tool["parameters"], input: true, expected: "true" },
  72. { schema: { type: "null" } as Tool["parameters"], input: "", expected: null },
  73. { schema: { type: "null" } as Tool["parameters"], input: 0, expected: null },
  74. { schema: { type: "null" } as Tool["parameters"], input: false, expected: null },
  75. {
  76. schema: { type: ["number", "string"] } as Tool["parameters"],
  77. input: "1",
  78. expected: "1",
  79. },
  80. {
  81. schema: { type: ["boolean", "number"] } as Tool["parameters"],
  82. input: "1",
  83. expected: 1,
  84. },
  85. ];
  86. for (const testCase of passingCases) {
  87. const { tool, toolCall } = createToolCallWithPlainSchema(testCase.schema, testCase.input);
  88. expect(validateToolArguments(tool, toolCall)).toEqual({ value: testCase.expected });
  89. }
  90. });
  91. it("rejects invalid coercions for serialized plain JSON schemas", () => {
  92. const failingCases: Array<{
  93. schema: Tool["parameters"];
  94. input: unknown;
  95. }> = [
  96. { schema: { type: "boolean" } as Tool["parameters"], input: "1" },
  97. { schema: { type: "boolean" } as Tool["parameters"], input: "0" },
  98. { schema: { type: "null" } as Tool["parameters"], input: "null" },
  99. { schema: { type: "integer" } as Tool["parameters"], input: "42.1" },
  100. ];
  101. for (const testCase of failingCases) {
  102. const { tool, toolCall } = createToolCallWithPlainSchema(testCase.schema, testCase.input);
  103. expect(() => validateToolArguments(tool, toolCall)).toThrow("Validation failed");
  104. }
  105. });
  106. });