From dd1721bce65578934c5d8a5ca3d546080121bbfc Mon Sep 17 00:00:00 2001 From: zhizheng Date: Sat, 14 Jun 2025 02:39:10 +0800 Subject: [PATCH] chore: refactor sreenshot and add test (#3032) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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> --- deno.json | 1 + deno.lock | 36 +++++++++++++++++++ www/utils/screenshot.ts | 68 ++++++++++++++++++++++++------------ www/utils/screenshot_test.ts | 49 ++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 22 deletions(-) create mode 100644 www/utils/screenshot_test.ts diff --git a/deno.json b/deno.json index 3bd5a3dc..4294f604 100644 --- a/deno.json +++ b/deno.json @@ -55,6 +55,7 @@ "@preact/signals": "npm:@preact/signals@^2.0.4", "esbuild": "npm:esbuild@0.25.4", "esbuild-wasm": "npm:esbuild-wasm@0.25.4", + "@std/assert": "jsr:@std/assert@^1.0.16", "@std/crypto": "jsr:@std/crypto@1", "@std/datetime": "jsr:@std/datetime@^0.225.2", "@std/encoding": "jsr:@std/encoding@1", diff --git a/deno.lock b/deno.lock index b9249d49..e16da495 100644 --- a/deno.lock +++ b/deno.lock @@ -13,6 +13,7 @@ "jsr:@luca/esbuild-deno-loader@0.11": "0.11.1", "jsr:@marvinh-test/fresh-island@*": "0.0.1", "jsr:@marvinh-test/fresh-island@^0.0.1": "0.0.1", + "jsr:@std/assert@*": "1.0.13", "jsr:@std/assert@0.221": "0.221.0", "jsr:@std/assert@^1.0.13": "1.0.13", "jsr:@std/async@1": "1.0.13", @@ -1820,6 +1821,38 @@ "https://deno.land/std@0.143.0/datetime/mod.ts": "dcab9ae7be83cbf74b7863e83bd16e7c646a8dea2f019092905630eb7a545739", "https://deno.land/std@0.143.0/datetime/tokenizer.ts": "7381e28f6ab51cb504c7e132be31773d73ef2f3e1e50a812736962b9df1e8c47", "https://deno.land/std@0.143.0/http/cookie.ts": "526f27762fad7bf84fbe491de7eba7c406057501eec6edcad7884a16b242fddf", + "https://deno.land/std@0.208.0/assert/_constants.ts": "8a9da298c26750b28b326b297316cdde860bc237533b07e1337c021379e6b2a9", + "https://deno.land/std@0.208.0/assert/_diff.ts": "58e1461cc61d8eb1eacbf2a010932bf6a05b79344b02ca38095f9b805795dc48", + "https://deno.land/std@0.208.0/assert/_format.ts": "a69126e8a469009adf4cf2a50af889aca364c349797e63174884a52ff75cf4c7", + "https://deno.land/std@0.208.0/assert/assert.ts": "9a97dad6d98c238938e7540736b826440ad8c1c1e54430ca4c4e623e585607ee", + "https://deno.land/std@0.208.0/assert/assert_almost_equals.ts": "e15ca1f34d0d5e0afae63b3f5d975cbd18335a132e42b0c747d282f62ad2cd6c", + "https://deno.land/std@0.208.0/assert/assert_array_includes.ts": "6856d7f2c3544bc6e62fb4646dfefa3d1df5ff14744d1bca19f0cbaf3b0d66c9", + "https://deno.land/std@0.208.0/assert/assert_equals.ts": "d8ec8a22447fbaf2fc9d7c3ed2e66790fdb74beae3e482855d75782218d68227", + "https://deno.land/std@0.208.0/assert/assert_exists.ts": "407cb6b9fb23a835cd8d5ad804e2e2edbbbf3870e322d53f79e1c7a512e2efd7", + "https://deno.land/std@0.208.0/assert/assert_false.ts": "0ccbcaae910f52c857192ff16ea08bda40fdc79de80846c206bfc061e8c851c6", + "https://deno.land/std@0.208.0/assert/assert_greater.ts": "ae2158a2d19313bf675bf7251d31c6dc52973edb12ac64ac8fc7064152af3e63", + "https://deno.land/std@0.208.0/assert/assert_greater_or_equal.ts": "1439da5ebbe20855446cac50097ac78b9742abe8e9a43e7de1ce1426d556e89c", + "https://deno.land/std@0.208.0/assert/assert_instance_of.ts": "3aedb3d8186e120812d2b3a5dea66a6e42bf8c57a8bd927645770bd21eea554c", + "https://deno.land/std@0.208.0/assert/assert_is_error.ts": "c21113094a51a296ffaf036767d616a78a2ae5f9f7bbd464cd0197476498b94b", + "https://deno.land/std@0.208.0/assert/assert_less.ts": "aec695db57db42ec3e2b62e97e1e93db0063f5a6ec133326cc290ff4b71b47e4", + "https://deno.land/std@0.208.0/assert/assert_less_or_equal.ts": "5fa8b6a3ffa20fd0a05032fe7257bf985d207b85685fdbcd23651b70f928c848", + "https://deno.land/std@0.208.0/assert/assert_match.ts": "c4083f80600bc190309903c95e397a7c9257ff8b5ae5c7ef91e834704e672e9b", + "https://deno.land/std@0.208.0/assert/assert_not_equals.ts": "9f1acab95bd1f5fc9a1b17b8027d894509a745d91bac1718fdab51dc76831754", + "https://deno.land/std@0.208.0/assert/assert_not_instance_of.ts": "0c14d3dfd9ab7a5276ed8ed0b18c703d79a3d106102077ec437bfe7ed912bd22", + "https://deno.land/std@0.208.0/assert/assert_not_match.ts": "3796a5b0c57a1ce6c1c57883dd4286be13a26f715ea662318ab43a8491a13ab0", + "https://deno.land/std@0.208.0/assert/assert_not_strict_equals.ts": "4cdef83df17488df555c8aac1f7f5ec2b84ad161b6d0645ccdbcc17654e80c99", + "https://deno.land/std@0.208.0/assert/assert_object_match.ts": "d8fc2867cfd92eeacf9cea621e10336b666de1874a6767b5ec48988838370b54", + "https://deno.land/std@0.208.0/assert/assert_rejects.ts": "45c59724de2701e3b1f67c391d6c71c392363635aad3f68a1b3408f9efca0057", + "https://deno.land/std@0.208.0/assert/assert_strict_equals.ts": "b1f538a7ea5f8348aeca261d4f9ca603127c665e0f2bbfeb91fa272787c87265", + "https://deno.land/std@0.208.0/assert/assert_string_includes.ts": "b821d39ebf5cb0200a348863c86d8c4c4b398e02012ce74ad15666fc4b631b0c", + "https://deno.land/std@0.208.0/assert/assert_throws.ts": "63784e951475cb7bdfd59878cd25a0931e18f6dc32a6077c454b2cd94f4f4bcd", + "https://deno.land/std@0.208.0/assert/assertion_error.ts": "4d0bde9b374dfbcbe8ac23f54f567b77024fb67dbb1906a852d67fe050d42f56", + "https://deno.land/std@0.208.0/assert/equal.ts": "9f1a46d5993966d2596c44e5858eec821859b45f783a5ee2f7a695dfc12d8ece", + "https://deno.land/std@0.208.0/assert/fail.ts": "c36353d7ae6e1f7933d45f8ea51e358c8c4b67d7e7502028598fe1fea062e278", + "https://deno.land/std@0.208.0/assert/mod.ts": "37c49a26aae2b254bbe25723434dc28cd7532e444cf0b481a97c045d110ec085", + "https://deno.land/std@0.208.0/assert/unimplemented.ts": "d56fbeecb1f108331a380f72e3e010a1f161baa6956fd0f7cf3e095ae1a4c75a", + "https://deno.land/std@0.208.0/assert/unreachable.ts": "4600dc0baf7d9c15a7f7d234f00c23bca8f3eba8b140286aaca7aa998cf9a536", + "https://deno.land/std@0.208.0/fmt/colors.ts": "34b3f77432925eb72cf0bfb351616949746768620b8e5ead66da532f93d10ba2", "https://deno.land/std@0.93.0/_util/assert.ts": "2f868145a042a11d5ad0a3c748dcf580add8a0dbc0e876eaa0026303a5488f58", "https://deno.land/std@0.93.0/_util/os.ts": "e282950a0eaa96760c0cf11e7463e66babd15ec9157d4c9ed49cc0925686f6a7", "https://deno.land/std@0.93.0/fs/walk.ts": "8d37f2164a7397668842a7cb5d53b9e7bcd216462623b1b96abe519f76d7f8b9", @@ -1870,6 +1903,8 @@ "https://esm.sh/@docsearch/js@3.5.2/es2020/js.mjs": "9b278cf3c0b26feded7d8efeac8e2b50f76bbafcf173a95002944bcc3482830a", "https://esm.sh/@docsearch/js@3.5.2?target=es2020": "4bad084f771a1923fe042ece62a9078f482f8642cb0b1acb890905e58586fee7", "https://raw.githubusercontent.com/denoland/ga4/main/mod.ts": "36f72ba1c90b5ebdb811427f367cd95fa6772d2de2fb45d6e57550501ee6d476", + "https://raw.githubusercontent.com/denoland/std/main/_tools/check_docs.ts": "59c29d6a5de45d04c5cab1078c4aacbed9edd0a6b83a6469f55318665e5be6b2", + "https://raw.githubusercontent.com/denoland/std/main/_tools/utils.ts": "c2e38ed7e7a9a8c0fbaf8d70aa808fb02f2cbb5e71ef18d634feb4b479c6a001", "https://raw.githubusercontent.com/denoland/std/refs/heads/main/_tools/check_docs.ts": "59c29d6a5de45d04c5cab1078c4aacbed9edd0a6b83a6469f55318665e5be6b2", "https://raw.githubusercontent.com/denoland/std/refs/heads/main/_tools/utils.ts": "c2e38ed7e7a9a8c0fbaf8d70aa808fb02f2cbb5e71ef18d634feb4b479c6a001" }, @@ -1881,6 +1916,7 @@ "jsr:@fresh/plugin-tailwind@^0.0.1-alpha.7", "jsr:@luca/esbuild-deno-loader@0.11", "jsr:@marvinh-test/fresh-island@^0.0.1", + "jsr:@std/assert@^1.0.16", "jsr:@std/async@^1.0.13", "jsr:@std/cli@^1.0.19", "jsr:@std/collections@^1.0.11", diff --git a/www/utils/screenshot.ts b/www/utils/screenshot.ts index 973ad44d..8a966400 100644 --- a/www/utils/screenshot.ts +++ b/www/utils/screenshot.ts @@ -1,32 +1,56 @@ import { launch } from "@astral/astral"; import { Image } from "imagescript"; -if (Deno.args.length !== 2) { - throw new Error("Usage: screenshot "); +export function validateArgs(args: string[]): [string, string] { + if (args.length !== 2) { + throw new Error("Usage: screenshot "); + } + 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 { + 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`); +} diff --git a/www/utils/screenshot_test.ts b/www/utils/screenshot_test.ts new file mode 100644 index 00000000..900891b0 --- /dev/null +++ b/www/utils/screenshot_test.ts @@ -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 ", + ); + assertThrows( + () => validateArgs(["one", "two", "three"]), + Error, + "Usage: screenshot ", + ); +}); + +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"); +});