Files
sweet/www/utils/screenshot_test.ts
T
zhizhengand李嘉图·M·路 dd1721bce6 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>
2025-06-13 20:39:10 +02:00

50 lines
1.8 KiB
TypeScript

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");
});