| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { ToolCallDelta } from ".."; |
| 3 | import { |
| 4 | getBooleanArg, |
| 5 | getNumberArg, |
| 6 | getOptionalStringArg, |
| 7 | getStringArg, |
| 8 | safeParseToolCallArgs, |
| 9 | } from "./parseArgs"; |
| 10 | |
| 11 | describe("safeParseToolCallArgs", () => { |
| 12 | it("should parse valid JSON arguments", () => { |
| 13 | const toolCall: ToolCallDelta = { |
| 14 | id: "1", |
| 15 | function: { |
| 16 | name: "testFunction", |
| 17 | arguments: '{"name": "test", "value": 123}', |
| 18 | }, |
| 19 | }; |
| 20 | |
| 21 | const result = safeParseToolCallArgs(toolCall); |
| 22 | expect(result).toEqual({ name: "test", value: 123 }); |
| 23 | }); |
| 24 | |
| 25 | it("should parse valid JSON arguments with whitespace", () => { |
| 26 | const toolCall: ToolCallDelta = { |
| 27 | id: "1", |
| 28 | function: { |
| 29 | name: "testFunction", |
| 30 | arguments: ' {"name": "test", "value": 123} ', |
| 31 | }, |
| 32 | }; |
| 33 | |
| 34 | const result = safeParseToolCallArgs(toolCall); |
| 35 | expect(result).toEqual({ name: "test", value: 123 }); |
| 36 | }); |
| 37 | |
| 38 | it("should return empty object for invalid JSON", () => { |
| 39 | const toolCall: ToolCallDelta = { |
| 40 | id: "1", |
| 41 | function: { |
| 42 | name: "testFunction", |
| 43 | arguments: '{"name": "test", value: 123', // Invalid JSON |
| 44 | }, |
| 45 | }; |
| 46 | |
| 47 | const result = safeParseToolCallArgs(toolCall); |
| 48 | expect(result).toEqual({}); |
| 49 | }); |
| 50 | |
| 51 | it("should return empty object when arguments is empty", () => { |
| 52 | const toolCall: ToolCallDelta = { |
| 53 | id: "1", |
| 54 | function: { |
| 55 | name: "testFunction", |
| 56 | arguments: "", |
| 57 | }, |
| 58 | }; |
| 59 | |
| 60 | const result = safeParseToolCallArgs(toolCall); |
| 61 | expect(result).toEqual({}); |
| 62 | }); |
| 63 | |
| 64 | it("should return empty object when arguments is null or undefined", () => { |
| 65 | const toolCall1: ToolCallDelta = { |
| 66 | id: "1", |
| 67 | function: { |
| 68 | name: "testFunction", |
| 69 | arguments: null as any, |
| 70 | }, |
| 71 | }; |
| 72 | |
| 73 | const toolCall2: ToolCallDelta = { |
| 74 | id: "1", |
| 75 | function: { |
| 76 | name: "testFunction", |
| 77 | arguments: undefined, |
| 78 | }, |
| 79 | }; |
| 80 | |
| 81 | expect(safeParseToolCallArgs(toolCall1)).toEqual({}); |
| 82 | expect(safeParseToolCallArgs(toolCall2)).toEqual({}); |
| 83 | }); |
| 84 | |
| 85 | it("should handle case when function is undefined", () => { |
| 86 | const toolCall: ToolCallDelta = { |
| 87 | id: "1", |
| 88 | function: undefined, |
| 89 | }; |
| 90 | |
| 91 | const result = safeParseToolCallArgs(toolCall); |
| 92 | expect(result).toEqual({}); |
| 93 | }); |
| 94 | }); |
| 95 | |
| 96 | describe("getStringArg", () => { |
| 97 | it("should return string argument when valid", () => { |
| 98 | const args = { name: "test" }; |
| 99 | const result = getStringArg(args, "name"); |
| 100 | expect(result).toBe("test"); |
| 101 | }); |
| 102 | |
| 103 | it("should throw error when argument is missing", () => { |
| 104 | const args = { otherArg: "test" }; |
| 105 | expect(() => getStringArg(args, "name")).toThrowError( |
| 106 | "`name` argument is required and must not be empty or whitespace-only. (type string)", |
| 107 | ); |
| 108 | }); |
| 109 | |
| 110 | it("should throw error when argument is not a string", () => { |
| 111 | const args = { name: 123 }; |
| 112 | expect(() => getStringArg(args, "name")).toThrowError( |
| 113 | "`name` argument is required and must not be empty or whitespace-only. (type string)", |
| 114 | ); |
| 115 | }); |
| 116 | |
| 117 | it("should throw error when argument is empty and empty not allowed", () => { |
| 118 | const args = { name: "" }; |
| 119 | expect(() => getStringArg(args, "name")).toThrowError( |
| 120 | "Argument name must not be empty or whitespace-only", |
| 121 | ); |
| 122 | |
| 123 | const argsWithSpace = { name: " " }; |
| 124 | expect(() => getStringArg(argsWithSpace, "name")).toThrowError( |
| 125 | "Argument name must not be empty or whitespace-only", |
| 126 | ); |
| 127 | }); |
| 128 | |
| 129 | it("should accept empty string when allowEmpty is true", () => { |
| 130 | const args = { name: "" }; |
| 131 | const result = getStringArg(args, "name", true); |
| 132 | expect(result).toBe(""); |
| 133 | |
| 134 | const argsWithSpace = { name: " " }; |
| 135 | const resultWithSpace = getStringArg(argsWithSpace, "name", true); |
| 136 | expect(resultWithSpace).toBe(" "); |
| 137 | }); |
| 138 | |
| 139 | it("should handle null or undefined args", () => { |
| 140 | expect(() => getStringArg(null, "name")).toThrowError( |
| 141 | "`name` argument is required and must not be empty or whitespace-only. (type string)", |
| 142 | ); |
| 143 | expect(() => getStringArg(undefined, "name")).toThrowError( |
| 144 | "`name` argument is required and must not be empty or whitespace-only. (type string)", |
| 145 | ); |
| 146 | }); |
| 147 | }); |
| 148 | |
| 149 | describe("getOptionalStringArg", () => { |
| 150 | it("should return undefined when argument is not provided", () => { |
| 151 | const args = { otherArg: "test" }; |
| 152 | const result = getOptionalStringArg(args, "name"); |
| 153 | expect(result).toBeUndefined(); |
| 154 | }); |
| 155 | |
| 156 | it("should return string argument when valid", () => { |
| 157 | const args = { name: "test" }; |
| 158 | const result = getOptionalStringArg(args, "name"); |
| 159 | expect(result).toBe("test"); |
| 160 | }); |
| 161 | |
| 162 | it("should throw error when argument is not a string", () => { |
| 163 | const args = { name: 123 }; |
| 164 | expect(() => getOptionalStringArg(args, "name")).toThrowError( |
| 165 | "`name` argument is required and must not be empty or whitespace-only. (type string)", |
| 166 | ); |
| 167 | }); |
| 168 | |
| 169 | it("should throw error when argument is empty and empty not allowed", () => { |
| 170 | const args = { name: "" }; |
| 171 | expect(() => getOptionalStringArg(args, "name")).toThrowError( |
| 172 | "Argument name must not be empty or whitespace-only", |
| 173 | ); |
| 174 | }); |
| 175 | |
| 176 | it("should accept empty string when allowEmpty is true", () => { |
| 177 | const args = { name: "" }; |
| 178 | const result = getOptionalStringArg(args, "name", true); |
| 179 | expect(result).toBe(""); |
| 180 | }); |
| 181 | |
| 182 | it("should handle null or undefined args", () => { |
| 183 | expect(getOptionalStringArg(null, "name")).toBeUndefined(); |
| 184 | expect(getOptionalStringArg(undefined, "name")).toBeUndefined(); |
| 185 | }); |
| 186 | }); |
| 187 | |
| 188 | describe("getBooleanArg", () => { |
| 189 | it("should return boolean argument when valid", () => { |
| 190 | const argsTrue = { flag: true }; |
| 191 | const argsFalse = { flag: false }; |
| 192 | |
| 193 | expect(getBooleanArg(argsTrue, "flag")).toBe(true); |
| 194 | expect(getBooleanArg(argsFalse, "flag")).toBe(false); |
| 195 | }); |
| 196 | |
| 197 | it("should return undefined when argument is not provided and not required", () => { |
| 198 | const args = { otherArg: true }; |
| 199 | const result = getBooleanArg(args, "flag"); |
| 200 | expect(result).toBeUndefined(); |
| 201 | }); |
| 202 | |
| 203 | it("should throw error when argument is not provided and required", () => { |
| 204 | const args = { otherArg: true }; |
| 205 | expect(() => getBooleanArg(args, "flag", true)).toThrowError( |
| 206 | "Argument `flag` is required (type boolean)", |
| 207 | ); |
| 208 | }); |
| 209 | |
| 210 | it('should handle string "true" and "false" values', () => { |
| 211 | const argsStringTrue = { flag: "true" }; |
| 212 | const argsStringFalse = { flag: "false" }; |
| 213 | const argsStringTrueUppercase = { flag: "TRUE" }; |
| 214 | const argsStringFalseUppercase = { flag: "FALSE" }; |
| 215 | |
| 216 | expect(getBooleanArg(argsStringTrue, "flag")).toBe(true); |
| 217 | expect(getBooleanArg(argsStringFalse, "flag")).toBe(false); |
| 218 | expect(getBooleanArg(argsStringTrueUppercase, "flag")).toBe(true); |
| 219 | expect(getBooleanArg(argsStringFalseUppercase, "flag")).toBe(false); |
| 220 | }); |
| 221 | |
| 222 | it("should throw error when argument is not a boolean or valid boolean string", () => { |
| 223 | const argsNumber = { flag: 1 }; |
| 224 | const argsInvalidString = { flag: "yes" }; |
| 225 | const argsObject = { flag: {} }; |
| 226 | |
| 227 | expect(() => getBooleanArg(argsNumber, "flag")).toThrowError( |
| 228 | "Argument `flag` must be a boolean true or false", |
| 229 | ); |
| 230 | expect(() => getBooleanArg(argsInvalidString, "flag")).toThrowError( |
| 231 | "Argument `flag` must be a boolean true or false", |
| 232 | ); |
| 233 | expect(() => getBooleanArg(argsObject, "flag")).toThrowError( |
| 234 | "Argument `flag` must be a boolean true or false", |
| 235 | ); |
| 236 | }); |
| 237 | |
| 238 | it("should handle null or undefined args", () => { |
| 239 | expect(getBooleanArg(null, "flag")).toBeUndefined(); |
| 240 | expect(getBooleanArg(undefined, "flag")).toBeUndefined(); |
| 241 | |
| 242 | expect(() => getBooleanArg(null, "flag", true)).toThrowError( |
| 243 | "Argument `flag` is required (type boolean)", |
| 244 | ); |
| 245 | expect(() => getBooleanArg(undefined, "flag", true)).toThrowError( |
| 246 | "Argument `flag` is required (type boolean)", |
| 247 | ); |
| 248 | }); |
| 249 | }); |
| 250 | |
| 251 | describe("getNumberArg", () => { |
| 252 | it("should return number argument when valid", () => { |
| 253 | const args = { count: 42 }; |
| 254 | const result = getNumberArg(args, "count"); |
| 255 | expect(result).toBe(42); |
| 256 | }); |
| 257 | |
| 258 | it("should parse string numbers", () => { |
| 259 | const args = { count: "42" }; |
| 260 | const result = getNumberArg(args, "count"); |
| 261 | expect(result).toBe(42); |
| 262 | }); |
| 263 | |
| 264 | it("should floor decimal numbers", () => { |
| 265 | const args = { count: 42.7 }; |
| 266 | const result = getNumberArg(args, "count"); |
| 267 | expect(result).toBe(42); |
| 268 | }); |
| 269 | |
| 270 | it("should parse and floor string decimal numbers", () => { |
| 271 | const args = { count: "42.7" }; |
| 272 | const result = getNumberArg(args, "count"); |
| 273 | expect(result).toBe(42); |
| 274 | }); |
| 275 | |
| 276 | it("should throw error when argument is missing", () => { |
| 277 | const args = { otherArg: 42 }; |
| 278 | expect(() => getNumberArg(args, "count")).toThrowError( |
| 279 | "Argument `count` is required (type number)", |
| 280 | ); |
| 281 | }); |
| 282 | |
| 283 | it("should throw error when argument is not a number", () => { |
| 284 | const argsString = { count: "not-a-number" }; |
| 285 | const argsBoolean = { count: true }; |
| 286 | const argsObject = { count: {} }; |
| 287 | |
| 288 | expect(() => getNumberArg(argsString, "count")).toThrowError( |
| 289 | "Argument `count` must be a valid number", |
| 290 | ); |
| 291 | expect(() => getNumberArg(argsBoolean, "count")).toThrowError( |
| 292 | "Argument `count` must be a valid number", |
| 293 | ); |
| 294 | expect(() => getNumberArg(argsObject, "count")).toThrowError( |
| 295 | "Argument `count` must be a valid number", |
| 296 | ); |
| 297 | }); |
| 298 | |
| 299 | it("should throw error when argument is NaN", () => { |
| 300 | const args = { count: NaN }; |
| 301 | expect(() => getNumberArg(args, "count")).toThrowError( |
| 302 | "Argument `count` must be a valid number", |
| 303 | ); |
| 304 | }); |
| 305 | |
| 306 | it("should handle negative numbers", () => { |
| 307 | const args = { count: -5 }; |
| 308 | const result = getNumberArg(args, "count"); |
| 309 | expect(result).toBe(-5); |
| 310 | }); |
| 311 | |
| 312 | it("should handle negative string numbers", () => { |
| 313 | const args = { count: "-10" }; |
| 314 | const result = getNumberArg(args, "count"); |
| 315 | expect(result).toBe(-10); |
| 316 | }); |
| 317 | |
| 318 | it("should handle zero", () => { |
| 319 | const args = { count: 0 }; |
| 320 | const result = getNumberArg(args, "count"); |
| 321 | expect(result).toBe(0); |
| 322 | }); |
| 323 | |
| 324 | it("should handle null or undefined args", () => { |
| 325 | expect(() => getNumberArg(null, "count")).toThrowError( |
| 326 | "Argument `count` is required (type number)", |
| 327 | ); |
| 328 | expect(() => getNumberArg(undefined, "count")).toThrowError( |
| 329 | "Argument `count` is required (type number)", |
| 330 | ); |
| 331 | }); |
| 332 | }); |