Peekaboo/tests/unit/utils/format-preprocessing.test.ts
Peter Steinberger ab882069b4 fix: Add defensive validation for invalid image formats with automatic PNG fallback
Implements robust handling for invalid image formats (like 'bmp', 'gif', 'webp') that bypass schema validation:

- Added defensive format validation in image tool handler
- Automatic path correction to ensure file extensions match actual format used
- Warning messages in response when format fallback occurs
- Comprehensive unit and integration test coverage for edge cases

This ensures invalid formats automatically fall back to PNG as requested, preventing
Swift CLI rejection and incorrect file extensions in output paths.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-08 07:44:17 +01:00

87 lines
No EOL
3.4 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { buildSwiftCliArgs } from "../../../src/utils/image-cli-args";
describe("Format Preprocessing in Swift CLI Args", () => {
it("should use preprocessed format in Swift CLI arguments", async () => {
// Import and test the schema preprocessing
const { imageToolSchema } = await import("../../../src/types/index.js");
// Test that invalid format gets preprocessed correctly
const inputWithInvalidFormat = { format: "bmp", path: "/tmp/test.png" };
const preprocessedInput = imageToolSchema.parse(inputWithInvalidFormat);
// Verify preprocessing worked
expect(preprocessedInput.format).toBe("png");
// Test that buildSwiftCliArgs uses the preprocessed format
const swiftArgs = buildSwiftCliArgs(preprocessedInput, "/tmp/test.png");
// Should contain --format png, not --format bmp
expect(swiftArgs).toContain("--format");
const formatIndex = swiftArgs.indexOf("--format");
expect(swiftArgs[formatIndex + 1]).toBe("png");
expect(swiftArgs).not.toContain("bmp");
});
it("should handle various invalid formats consistently", async () => {
const { imageToolSchema } = await import("../../../src/types/index.js");
const invalidFormats = ["bmp", "gif", "webp", "tiff", "xyz"];
for (const invalidFormat of invalidFormats) {
const input = { format: invalidFormat, path: "/tmp/test.png" };
const preprocessedInput = imageToolSchema.parse(input);
// All should be preprocessed to png
expect(preprocessedInput.format).toBe("png");
const swiftArgs = buildSwiftCliArgs(preprocessedInput, "/tmp/test.png");
const formatIndex = swiftArgs.indexOf("--format");
// All should result in --format png
expect(swiftArgs[formatIndex + 1]).toBe("png");
expect(swiftArgs).not.toContain(invalidFormat);
}
});
it("should pass through valid formats correctly", async () => {
const { imageToolSchema } = await import("../../../src/types/index.js");
const validCases = [
{ input: "png", expected: "png" },
{ input: "PNG", expected: "png" },
{ input: "jpg", expected: "jpg" },
{ input: "JPG", expected: "jpg" },
{ input: "jpeg", expected: "jpg" },
{ input: "JPEG", expected: "jpg" },
];
for (const { input, expected } of validCases) {
const inputObj = { format: input, path: "/tmp/test.png" };
const preprocessedInput = imageToolSchema.parse(inputObj);
expect(preprocessedInput.format).toBe(expected);
const swiftArgs = buildSwiftCliArgs(preprocessedInput, "/tmp/test.png");
const formatIndex = swiftArgs.indexOf("--format");
expect(swiftArgs[formatIndex + 1]).toBe(expected);
}
});
it("should handle data format for Swift CLI (converts to png)", async () => {
const { imageToolSchema } = await import("../../../src/types/index.js");
const input = { format: "data", path: "/tmp/test.png" };
const preprocessedInput = imageToolSchema.parse(input);
expect(preprocessedInput.format).toBe("data");
// buildSwiftCliArgs should convert data format to png for Swift CLI
const swiftArgs = buildSwiftCliArgs(preprocessedInput, "/tmp/test.png");
const formatIndex = swiftArgs.indexOf("--format");
// Should be converted to png for Swift CLI
expect(swiftArgs[formatIndex + 1]).toBe("png");
});
});