71 lines
2.3 KiB
TypeScript
71 lines
2.3 KiB
TypeScript
import { assert, assertEquals } from "@std/assert";
|
|
import { join } from "@std/path";
|
|
|
|
const CLI_PATH = join(import.meta.dirname!, "cli.ts");
|
|
const TEST_IMAGES = {
|
|
png: "./test-images/test-100x100.png",
|
|
jpg: "./test-images/test-100x100.jpg",
|
|
};
|
|
|
|
async function runCli(args: string[]): Promise<{ code: number; stdout: string; stderr: string }> {
|
|
const cmd = new Deno.Command("deno", {
|
|
args: ["run", "--allow-read", "--allow-write", "--allow-ffi", "--allow-env", CLI_PATH, ...args],
|
|
stdout: "piped",
|
|
stderr: "piped",
|
|
});
|
|
|
|
const output = await cmd.output();
|
|
|
|
return {
|
|
code: output.code,
|
|
stdout: new TextDecoder().decode(output.stdout),
|
|
stderr: new TextDecoder().decode(output.stderr),
|
|
};
|
|
}
|
|
|
|
Deno.test("CLI - should show help with --help flag", async () => {
|
|
const result = await runCli(["--help"]);
|
|
|
|
assertEquals(result.code, 0);
|
|
assert(result.stdout.includes("scale-image-fixer"));
|
|
assert(result.stdout.includes("--width"));
|
|
assert(result.stdout.includes("--height"));
|
|
assert(result.stdout.includes("--mode"));
|
|
});
|
|
|
|
Deno.test("CLI - should scale a single image", async () => {
|
|
const outputPath = "./test-images/cli-test-output.png";
|
|
const result = await runCli([TEST_IMAGES.png, "-w", "50", "-h", "50", "-o", outputPath]);
|
|
|
|
assertEquals(result.code, 0, `CLI failed: ${result.stderr}`);
|
|
assert(result.stdout.includes("Scaled:"));
|
|
|
|
// Verify output file exists
|
|
const stat = await Deno.stat(outputPath);
|
|
assert(stat.isFile);
|
|
|
|
// Clean up
|
|
await Deno.remove(outputPath);
|
|
});
|
|
|
|
Deno.test("CLI - should handle missing required arguments", async () => {
|
|
const result = await runCli([TEST_IMAGES.png]);
|
|
|
|
assertEquals(result.code, 1);
|
|
assert(result.stderr.includes("required") || result.stderr.includes("Error"));
|
|
});
|
|
|
|
Deno.test("CLI - should handle non-existent file", async () => {
|
|
const result = await runCli(["./non-existent.png", "-w", "100", "-h", "100"]);
|
|
|
|
assertEquals(result.code, 1);
|
|
assert(result.stderr.includes("not found") || result.stderr.includes("Error"));
|
|
});
|
|
|
|
Deno.test("CLI - should handle invalid mode", async () => {
|
|
const result = await runCli([TEST_IMAGES.png, "-w", "100", "-h", "100", "-m", "invalid"]);
|
|
|
|
assertEquals(result.code, 1);
|
|
assert(result.stderr.includes("Mode must be one of") || result.stderr.includes("invalid"));
|
|
});
|