icons.test.ts
This file
Path
apps/web/test/icons.test.ts
62 lines · 2.0 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 1–62why, one step back1// The site has no logomark: wherever a file must stand for it (the tab's icon, a home screen's, the manifest's),2// it draws the repeat button emoji as text on the paper, and each raster is that drawing at its size.3import { describe, expect, it } from "@effect/vitest";45import svg from "../public/icons/icon.svg?raw";6import { themeColors } from "../src/features/shared/theme.ts";78const rasters: Readonly<Record<string, string>> = import.meta.glob(9 "../public/**/*.{png,ico}",10 { eager: true, import: "default", query: "?inline" }11);1213const REPEAT_BUTTON = "\u{1F501}";1415const bytesOf = (path: string) => {16 const url = rasters[path] ?? "";1718 return Uint8Array.from(19 atob(url.slice(url.indexOf(",") + 1)),20 (char) => char.codePointAt(0) ?? 021 );22};2324// A PNG's IHDR holds its width and height as big-endian words at bytes 16 and 20.25const pngSize = (path: string) => {26 const view = new DataView(bytesOf(path).buffer);2728 return [view.getUint32(16), view.getUint32(20)];29};3031// An ICO lists one 16-byte entry per image after its 6-byte header; a 0 byte means 256.32const icoSizes = (path: string) => {33 const bytes = bytesOf(path);34 const count = bytes[4] ?? 0;3536 return Array.from(37 { length: count },38 (_, index) => bytes[6 + index * 16] ?? 039 );40};4142describe("the site's icon", () => {43 it("draws the repeat button emoji as text, and no shape of its own", () => {44 expect(svg).toMatch(45 new RegExp(`<text\\b[^>]*>${REPEAT_BUTTON}</text>`, "u")46 );47 expect(svg).not.toMatch(48 /<(?:path|polygon|polyline|circle|ellipse|line)\b/u49 );50 });5152 it("lays the emoji on the light paper", () => {53 expect(svg).toContain(`fill="${themeColors.light}"`);54 });5556 it("keeps each raster at the size its place asks for", () => {57 expect(pngSize("../public/apple-touch-icon.png")).toStrictEqual([180, 180]);58 expect(pngSize("../public/icons/icon-192.png")).toStrictEqual([192, 192]);59 expect(pngSize("../public/icons/icon-512.png")).toStrictEqual([512, 512]);60 expect(icoSizes("../public/favicon.ico")).toStrictEqual([16, 32, 48]);61 });62});