Skip to content

Commit

Permalink
add parse.test
Browse files Browse the repository at this point in the history
  • Loading branch information
holdenmatt committed Dec 22, 2023
1 parent 1a42fb2 commit f603112
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"scripts": {
"build": "tsup --dts",
"test": "jest --verbose",
"test": "jest",
"publish": "np"
},
"tsup": {
Expand Down
56 changes: 56 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
findFunction,
parseArguments,
InvalidFunctionName,
InvalidFunctionArguments,
} from "../src/utils/parse";
import { z } from "zod";

jest.mock("../src/utils/logging", () => ({
logger: {
error: jest.fn(),
},
}));

describe("Function Parsing", () => {
const testSchema = z.object({
param1: z.string(),
param2: z.number(),
});

const functions = [
{
name: "testFunction",
description: "A test function",
schema: testSchema,
},
];

describe("findFunction", () => {
it("finds the correct function definition", () => {
const func = findFunction("testFunction", functions);
expect(func).toEqual(functions[0]);
});

it("throws an error for a non-existent function name", () => {
expect(() => findFunction("nonExistentFunction", functions)).toThrow(
InvalidFunctionName,
);
});
});

describe("parseArguments", () => {
it("correctly parses and validates function arguments", () => {
const args = JSON.stringify({ param1: "value1", param2: 42 });
const parsedArgs = parseArguments("testFunction", args, testSchema);
expect(parsedArgs).toEqual({ param1: "value1", param2: 42 });
});

it("throws an error for invalid function arguments", () => {
const invalidArgs = JSON.stringify({ param1: "value1", param2: "notANumber" });
expect(() => parseArguments("testFunction", invalidArgs, testSchema)).toThrow(
InvalidFunctionArguments,
);
});
});
});

0 comments on commit f603112

Please sign in to comment.