From a3810024c697d0e2a1d83ddeeccd6cd5eef8cfb6 Mon Sep 17 00:00:00 2001 From: Matt Cowger Date: Tue, 2 Dec 2025 20:15:10 +0000 Subject: [PATCH] Add tests to match. --- core/tools/parseArgs.ts | 8 +++--- core/tools/parseArgs.vitest.ts | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/core/tools/parseArgs.ts b/core/tools/parseArgs.ts index 14885c979..fa6e25b85 100644 --- a/core/tools/parseArgs.ts +++ b/core/tools/parseArgs.ts @@ -42,16 +42,16 @@ export function getStringArg( } } - if (!allowEmpty && !value.trim()) { - throw new Error(`Argument ${argName} must not be empty or whitespace-only`); - } - if (typeof value !== "string") { throw new Error( `\`${argName}\` argument is required${allowEmpty ? "" : " and must not be empty or whitespace-only"}. (type string)`, ); } + if (!allowEmpty && !value.trim()) { + throw new Error(`Argument ${argName} must not be empty or whitespace-only`); + } + return value; } diff --git a/core/tools/parseArgs.vitest.ts b/core/tools/parseArgs.vitest.ts index 71bfc0cc5..c3d0f6428 100644 --- a/core/tools/parseArgs.vitest.ts +++ b/core/tools/parseArgs.vitest.ts @@ -144,6 +144,57 @@ describe("getStringArg", () => { "`name` argument is required and must not be empty or whitespace-only. (type string)", ); }); + + it("should convert parsed JSON object to string for contents parameter", () => { + // This simulates the case where JSON.parse() has converted a JSON string into an object + const args = { contents: { key: "value", number: 123 } }; + const result = getStringArg(args, "contents"); + expect(result).toBe('{"key":"value","number":123}'); + }); + + it("should convert nested JSON object to string for contents parameter", () => { + const args = { + contents: { + user: { + name: "John", + details: { + age: 30, + preferences: ["coding", "reading"], + }, + }, + }, + }; + const result = getStringArg(args, "contents"); + const expected = + '{"user":{"name":"John","details":{"age":30,"preferences":["coding","reading"]}}}'; + expect(result).toBe(expected); + }); + + it("should convert JSON array to string for contents parameter", () => { + const args = { contents: ["item1", "item2", { key: "value" }] }; + const result = getStringArg(args, "contents"); + expect(result).toBe('["item1","item2",{"key":"value"}]'); + }); + + it("should throw error when non-contents parameter is an object", () => { + const args = { filename: { invalid: "object" } }; + expect(() => getStringArg(args, "filename")).toThrowError( + "Argument `filename` must be a string, not an object", + ); + }); + + it("should handle contents parameter that is already a string", () => { + const args = { contents: "already a string" }; + const result = getStringArg(args, "contents"); + expect(result).toBe("already a string"); + }); + + it("should handle contents parameter that is null", () => { + const args = { contents: null }; + expect(() => getStringArg(args, "contents")).toThrowError( + "`contents` argument is required and must not be empty or whitespace-only. (type string)", + ); + }); }); describe("getOptionalStringArg", () => {