Add tests to match.

This commit is contained in:
Matt Cowger 2025-12-02 20:15:10 +00:00
parent 5f14a2fa66
commit a3810024c6
2 changed files with 55 additions and 4 deletions

View file

@ -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;
}

View file

@ -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", () => {