Elffuss BraidHub

kikocis/continue public

8cf316c34504 · 7.6 KiBRawPermalink
1import { describe, expect, it } from "vitest";
2import { ToolCallDelta } from "..";
3import {
4 getBooleanArg,
5 getOptionalStringArg,
6 getStringArg,
7 safeParseToolCallArgs,
8} from "./parseArgs";
9
10describe("safeParseToolCallArgs", () => {
11 it("should parse valid JSON arguments", () => {
12 const toolCall: ToolCallDelta = {
13 id: "1",
14 function: {
15 name: "testFunction",
16 arguments: '{"name": "test", "value": 123}',
17 },
18 };
19
20 const result = safeParseToolCallArgs(toolCall);
21 expect(result).toEqual({ name: "test", value: 123 });
22 });
23
24 it("should parse valid JSON arguments with whitespace", () => {
25 const toolCall: ToolCallDelta = {
26 id: "1",
27 function: {
28 name: "testFunction",
29 arguments: ' {"name": "test", "value": 123} ',
30 },
31 };
32
33 const result = safeParseToolCallArgs(toolCall);
34 expect(result).toEqual({ name: "test", value: 123 });
35 });
36
37 it("should return empty object for invalid JSON", () => {
38 const toolCall: ToolCallDelta = {
39 id: "1",
40 function: {
41 name: "testFunction",
42 arguments: '{"name": "test", value: 123', // Invalid JSON
43 },
44 };
45
46 const result = safeParseToolCallArgs(toolCall);
47 expect(result).toEqual({});
48 });
49
50 it("should return empty object when arguments is empty", () => {
51 const toolCall: ToolCallDelta = {
52 id: "1",
53 function: {
54 name: "testFunction",
55 arguments: "",
56 },
57 };
58
59 const result = safeParseToolCallArgs(toolCall);
60 expect(result).toEqual({});
61 });
62
63 it("should return empty object when arguments is null or undefined", () => {
64 const toolCall1: ToolCallDelta = {
65 id: "1",
66 function: {
67 name: "testFunction",
68 arguments: null as any,
69 },
70 };
71
72 const toolCall2: ToolCallDelta = {
73 id: "1",
74 function: {
75 name: "testFunction",
76 arguments: undefined,
77 },
78 };
79
80 expect(safeParseToolCallArgs(toolCall1)).toEqual({});
81 expect(safeParseToolCallArgs(toolCall2)).toEqual({});
82 });
83
84 it("should handle case when function is undefined", () => {
85 const toolCall: ToolCallDelta = {
86 id: "1",
87 function: undefined,
88 };
89
90 const result = safeParseToolCallArgs(toolCall);
91 expect(result).toEqual({});
92 });
93});
94
95describe("getStringArg", () => {
96 it("should return string argument when valid", () => {
97 const args = { name: "test" };
98 const result = getStringArg(args, "name");
99 expect(result).toBe("test");
100 });
101
102 it("should throw error when argument is missing", () => {
103 const args = { otherArg: "test" };
104 expect(() => getStringArg(args, "name")).toThrowError(
105 "`name` argument is required and must not be empty. (type string)",
106 );
107 });
108
109 it("should throw error when argument is not a string", () => {
110 const args = { name: 123 };
111 expect(() => getStringArg(args, "name")).toThrowError(
112 "`name` argument is required and must not be empty. (type string)",
113 );
114 });
115
116 it("should throw error when argument is empty and empty not allowed", () => {
117 const args = { name: "" };
118 expect(() => getStringArg(args, "name")).toThrowError(
119 "Argument name must not be empty",
120 );
121
122 const argsWithSpace = { name: " " };
123 expect(() => getStringArg(argsWithSpace, "name")).toThrowError(
124 "Argument name must not be empty",
125 );
126 });
127
128 it("should accept empty string when allowEmpty is true", () => {
129 const args = { name: "" };
130 const result = getStringArg(args, "name", true);
131 expect(result).toBe("");
132
133 const argsWithSpace = { name: " " };
134 const resultWithSpace = getStringArg(argsWithSpace, "name", true);
135 expect(resultWithSpace).toBe(" ");
136 });
137
138 it("should handle null or undefined args", () => {
139 expect(() => getStringArg(null, "name")).toThrowError(
140 "`name` argument is required and must not be empty. (type string)",
141 );
142 expect(() => getStringArg(undefined, "name")).toThrowError(
143 "`name` argument is required and must not be empty. (type string)",
144 );
145 });
146});
147
148describe("getOptionalStringArg", () => {
149 it("should return undefined when argument is not provided", () => {
150 const args = { otherArg: "test" };
151 const result = getOptionalStringArg(args, "name");
152 expect(result).toBeUndefined();
153 });
154
155 it("should return string argument when valid", () => {
156 const args = { name: "test" };
157 const result = getOptionalStringArg(args, "name");
158 expect(result).toBe("test");
159 });
160
161 it("should throw error when argument is not a string", () => {
162 const args = { name: 123 };
163 expect(() => getOptionalStringArg(args, "name")).toThrowError(
164 "`name` argument is required and must not be empty. (type string)",
165 );
166 });
167
168 it("should throw error when argument is empty and empty not allowed", () => {
169 const args = { name: "" };
170 expect(() => getOptionalStringArg(args, "name")).toThrowError(
171 "Argument name must not be empty",
172 );
173 });
174
175 it("should accept empty string when allowEmpty is true", () => {
176 const args = { name: "" };
177 const result = getOptionalStringArg(args, "name", true);
178 expect(result).toBe("");
179 });
180
181 it("should handle null or undefined args", () => {
182 expect(getOptionalStringArg(null, "name")).toBeUndefined();
183 expect(getOptionalStringArg(undefined, "name")).toBeUndefined();
184 });
185});
186
187describe("getBooleanArg", () => {
188 it("should return boolean argument when valid", () => {
189 const argsTrue = { flag: true };
190 const argsFalse = { flag: false };
191
192 expect(getBooleanArg(argsTrue, "flag")).toBe(true);
193 expect(getBooleanArg(argsFalse, "flag")).toBe(false);
194 });
195
196 it("should return undefined when argument is not provided and not required", () => {
197 const args = { otherArg: true };
198 const result = getBooleanArg(args, "flag");
199 expect(result).toBeUndefined();
200 });
201
202 it("should throw error when argument is not provided and required", () => {
203 const args = { otherArg: true };
204 expect(() => getBooleanArg(args, "flag", true)).toThrowError(
205 "Argument `flag` is required (type boolean)",
206 );
207 });
208
209 it('should handle string "true" and "false" values', () => {
210 const argsStringTrue = { flag: "true" };
211 const argsStringFalse = { flag: "false" };
212 const argsStringTrueUppercase = { flag: "TRUE" };
213 const argsStringFalseUppercase = { flag: "FALSE" };
214
215 expect(getBooleanArg(argsStringTrue, "flag")).toBe(true);
216 expect(getBooleanArg(argsStringFalse, "flag")).toBe(false);
217 expect(getBooleanArg(argsStringTrueUppercase, "flag")).toBe(true);
218 expect(getBooleanArg(argsStringFalseUppercase, "flag")).toBe(false);
219 });
220
221 it("should throw error when argument is not a boolean or valid boolean string", () => {
222 const argsNumber = { flag: 1 };
223 const argsInvalidString = { flag: "yes" };
224 const argsObject = { flag: {} };
225
226 expect(() => getBooleanArg(argsNumber, "flag")).toThrowError(
227 "Argument `flag` must be a boolean true or false",
228 );
229 expect(() => getBooleanArg(argsInvalidString, "flag")).toThrowError(
230 "Argument `flag` must be a boolean true or false",
231 );
232 expect(() => getBooleanArg(argsObject, "flag")).toThrowError(
233 "Argument `flag` must be a boolean true or false",
234 );
235 });
236
237 it("should handle null or undefined args", () => {
238 expect(getBooleanArg(null, "flag")).toBeUndefined();
239 expect(getBooleanArg(undefined, "flag")).toBeUndefined();
240
241 expect(() => getBooleanArg(null, "flag", true)).toThrowError(
242 "Argument `flag` is required (type boolean)",
243 );
244 expect(() => getBooleanArg(undefined, "flag", true)).toThrowError(
245 "Argument `flag` is required (type boolean)",
246 );
247 });
248});