chore: refactor sreenshot and add test (#3032)
- Refactor screenshot functionality into testable utility functions - Add comprehensive test suite to catch regressions early - Ensure functionality doesn't break unexpectedly without notification --patch-1 based on review, make throw error naturally --------- Co-authored-by: 李嘉图·M·路 <146103794+Ricardo-M-Zheng@users.noreply.github.com>
This commit is contained in:
+46
-22
@@ -1,32 +1,56 @@
|
||||
import { launch } from "@astral/astral";
|
||||
import { Image } from "imagescript";
|
||||
|
||||
if (Deno.args.length !== 2) {
|
||||
throw new Error("Usage: screenshot <url> <id>");
|
||||
export function validateArgs(args: string[]): [string, string] {
|
||||
if (args.length !== 2) {
|
||||
throw new Error("Usage: screenshot <url> <id>");
|
||||
}
|
||||
return [args[0], args[1]];
|
||||
}
|
||||
|
||||
const [url, id] = Deno.args;
|
||||
const parsedUrl = new URL(url);
|
||||
if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") {
|
||||
throw new Error("Invalid URL");
|
||||
export function validateUrl(url: string): URL {
|
||||
const parsedUrl = new URL(url);
|
||||
if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") {
|
||||
throw new Error("Invalid URL");
|
||||
}
|
||||
return parsedUrl;
|
||||
}
|
||||
|
||||
const browser = await launch();
|
||||
const page = await browser.newPage(url);
|
||||
await page.waitForNetworkIdle();
|
||||
const raw = await page.screenshot();
|
||||
export function generateFilePaths(
|
||||
id: string,
|
||||
): { image2x: string; image1x: string } {
|
||||
return {
|
||||
image2x: `./www/static/showcase/${id}2x.jpg`,
|
||||
image1x: `./www/static/showcase/${id}1x.jpg`,
|
||||
};
|
||||
}
|
||||
|
||||
await browser.close();
|
||||
export async function captureScreenshot(
|
||||
url: string,
|
||||
id: string,
|
||||
): Promise<void> {
|
||||
const browser = await launch();
|
||||
try {
|
||||
const page = await browser.newPage(url);
|
||||
await page.waitForNetworkIdle();
|
||||
const raw = await page.screenshot();
|
||||
|
||||
// convert to jpeg
|
||||
const image2x = await Image.decode(raw);
|
||||
await Deno.writeFile(
|
||||
`./www/static/showcase/${id}2x.jpg`,
|
||||
await image2x.encodeJPEG(80),
|
||||
);
|
||||
const image2x = await Image.decode(raw);
|
||||
const { image2x: path2x, image1x: path1x } = generateFilePaths(id);
|
||||
|
||||
const image1x = image2x.resize(image2x.width / 2, Image.RESIZE_AUTO);
|
||||
await Deno.writeFile(
|
||||
`./www/static/showcase/${id}1x.jpg`,
|
||||
await image1x.encodeJPEG(80),
|
||||
);
|
||||
await Deno.writeFile(path2x, await image2x.encodeJPEG(80));
|
||||
const image1x = image2x.resize(image2x.width / 2, Image.RESIZE_AUTO);
|
||||
await Deno.writeFile(path1x, await image1x.encodeJPEG(80));
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
}
|
||||
|
||||
// only run the script if it's the main module
|
||||
if (import.meta.main) {
|
||||
const [url, id] = validateArgs(Deno.args);
|
||||
validateUrl(url);
|
||||
await captureScreenshot(url, id);
|
||||
// deno-lint-ignore no-console
|
||||
console.log(`Screenshot saved as ${id}1x.jpg and ${id}2x.jpg`);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { assertEquals, assertThrows } from "jsr:@std/assert";
|
||||
import { generateFilePaths, validateArgs, validateUrl } from "./screenshot.ts";
|
||||
|
||||
Deno.test("validateArgs - accepts 2 arguments", () => {
|
||||
const result = validateArgs(["https://example.com", "test-id"]);
|
||||
assertEquals(result, ["https://example.com", "test-id"]);
|
||||
});
|
||||
|
||||
Deno.test("validateArgs - should throw error for incorrect number of arguments", () => {
|
||||
assertThrows(
|
||||
() => validateArgs(["only-one"]),
|
||||
Error,
|
||||
"Usage: screenshot <url> <id>",
|
||||
);
|
||||
assertThrows(
|
||||
() => validateArgs(["one", "two", "three"]),
|
||||
Error,
|
||||
"Usage: screenshot <url> <id>",
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("validateUrl - should accept valid HTTP URLs", () => {
|
||||
const url = validateUrl("http://example.com");
|
||||
assertEquals(url.protocol, "http:");
|
||||
assertEquals(url.hostname, "example.com");
|
||||
});
|
||||
|
||||
Deno.test("validateUrl - should accept valid HTTPS URLs", () => {
|
||||
const url = validateUrl("https://example.com");
|
||||
assertEquals(url.protocol, "https:");
|
||||
assertEquals(url.hostname, "example.com");
|
||||
});
|
||||
|
||||
Deno.test("validateUrl - should reject invalid protocols", () => {
|
||||
assertThrows(() => validateUrl("ftp://example.com"), Error, "Invalid URL");
|
||||
assertThrows(() => validateUrl("file:///path/to/file"), Error, "Invalid URL");
|
||||
});
|
||||
|
||||
Deno.test("generateFilePaths - should generate correct file paths", () => {
|
||||
const paths = generateFilePaths("test-id");
|
||||
assertEquals(paths.image2x, "./www/static/showcase/test-id2x.jpg");
|
||||
assertEquals(paths.image1x, "./www/static/showcase/test-id1x.jpg");
|
||||
});
|
||||
|
||||
Deno.test("generateFilePaths - should handle special characters in ID", () => {
|
||||
const paths = generateFilePaths("test-id_123");
|
||||
assertEquals(paths.image2x, "./www/static/showcase/test-id_1232x.jpg");
|
||||
assertEquals(paths.image1x, "./www/static/showcase/test-id_1231x.jpg");
|
||||
});
|
||||
Reference in New Issue
Block a user