add image scale [not tested]
This commit is contained in:
117
image-scale/main_test.ts
Normal file
117
image-scale/main_test.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import { assertEquals, assertRejects } from "@std/assert";
|
||||
import { basename, join } from "@std/path";
|
||||
import { getOutputPath, scaleImage } from "./main.ts";
|
||||
|
||||
const testDir = await Deno.makeTempDir();
|
||||
|
||||
Deno.test("getOutputPath - changes extension to jpg", () => {
|
||||
assertEquals(
|
||||
getOutputPath("/path/to/image.png"),
|
||||
"/path/to/image.jpg",
|
||||
);
|
||||
assertEquals(
|
||||
getOutputPath("/path/to/image.jpeg"),
|
||||
"/path/to/image.jpg",
|
||||
);
|
||||
assertEquals(
|
||||
getOutputPath("/path/to/image.jpg"),
|
||||
"/path/to/image.jpg",
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("getOutputPath - with output directory", () => {
|
||||
assertEquals(
|
||||
getOutputPath("/path/to/image.png", "/output/dir"),
|
||||
"/output/dir/image.jpg",
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("scaleImage - rejects with invalid minPixels", async () => {
|
||||
await assertRejects(
|
||||
() => scaleImage("test.png", "output.jpg", 0),
|
||||
Error,
|
||||
"minPixels must be a positive number",
|
||||
);
|
||||
|
||||
await assertRejects(
|
||||
() => scaleImage("test.png", "output.jpg", -100),
|
||||
Error,
|
||||
"minPixels must be a positive number",
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("scaleImage - processes PNG image", async () => {
|
||||
// Create a simple test PNG image (100x50 pixels)
|
||||
const inputPath = join(testDir, "test_input.png");
|
||||
const outputPath = join(testDir, "test_output.jpg");
|
||||
|
||||
// Create a test image using sharp
|
||||
await Deno.writeFile(
|
||||
inputPath,
|
||||
await (await import("sharp")).default({
|
||||
create: {
|
||||
width: 100,
|
||||
height: 50,
|
||||
channels: 3,
|
||||
background: { r: 255, g: 0, b: 0 },
|
||||
},
|
||||
})
|
||||
.png()
|
||||
.toBuffer(),
|
||||
);
|
||||
|
||||
// Scale the image with min dimension of 200px
|
||||
await scaleImage(inputPath, outputPath, 200);
|
||||
|
||||
// Verify output file exists
|
||||
const outputStat = await Deno.stat(outputPath);
|
||||
assertEquals(outputStat.isFile, true);
|
||||
|
||||
// Verify the output dimensions
|
||||
const metadata = await (await import("sharp")).default(outputPath).metadata();
|
||||
// Original: 100x50, min dimension is 50
|
||||
// Scale factor: 200/50 = 4
|
||||
// New dimensions: 400x200
|
||||
assertEquals(metadata.height, 200);
|
||||
assertEquals(metadata.width, 400);
|
||||
});
|
||||
|
||||
Deno.test("scaleImage - processes JPG image", async () => {
|
||||
const inputPath = join(testDir, "test_input2.jpg");
|
||||
const outputPath = join(testDir, "test_output2.jpg");
|
||||
|
||||
// Create a test image (80x120 pixels)
|
||||
await Deno.writeFile(
|
||||
inputPath,
|
||||
await (await import("sharp")).default({
|
||||
create: {
|
||||
width: 80,
|
||||
height: 120,
|
||||
channels: 3,
|
||||
background: { r: 0, g: 255, b: 0 },
|
||||
},
|
||||
})
|
||||
.jpeg()
|
||||
.toBuffer(),
|
||||
);
|
||||
|
||||
// Scale the image with min dimension of 240px
|
||||
await scaleImage(inputPath, outputPath, 240);
|
||||
|
||||
// Verify output file exists
|
||||
const outputStat = await Deno.stat(outputPath);
|
||||
assertEquals(outputStat.isFile, true);
|
||||
|
||||
// Verify the output dimensions
|
||||
const metadata = await (await import("sharp")).default(outputPath).metadata();
|
||||
// Original: 80x120, min dimension is 80
|
||||
// Scale factor: 240/80 = 3
|
||||
// New dimensions: 240x360
|
||||
assertEquals(metadata.width, 240);
|
||||
assertEquals(metadata.height, 360);
|
||||
});
|
||||
|
||||
// Cleanup after all tests
|
||||
addEventListener("beforeunload", async () => {
|
||||
await Deno.remove(testDir, { recursive: true });
|
||||
});
|
||||
Reference in New Issue
Block a user