File
Blob: tests/worker/sites/cover.test.ts
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | |
| 3 | import { GRADIENT_PRESETS, isGradientPreset, parseUploadCoverUrl } from "@/shared/page-cover"; |
| 4 | import { |
| 5 | createSiteCoverHash, |
| 6 | isOgImageType, |
| 7 | parseGradient, |
| 8 | SITE_COVER_HEIGHT, |
| 9 | SITE_COVER_WIDTH, |
| 10 | } from "@/worker/sites/cover"; |
| 11 | |
| 12 | describe("Sites cover helpers", () => { |
| 13 | it("creates a stable hash that changes when cover inputs change", async () => { |
| 14 | const first = await createSiteCoverHash(GRADIENT_PRESETS[0]); |
| 15 | const repeat = await createSiteCoverHash(GRADIENT_PRESETS[0]); |
| 16 | const changed = await createSiteCoverHash(GRADIENT_PRESETS[1]); |
| 17 | |
| 18 | expect(first).toBe(repeat); |
| 19 | expect(first).not.toBe(changed); |
| 20 | expect(first).toMatch(/^[A-Za-z0-9_-]+$/); |
| 21 | }); |
| 22 | |
| 23 | it("parses only exact upload cover URLs", () => { |
| 24 | expect(parseUploadCoverUrl("/uploads/upload_abc-123")).toBe("upload_abc-123"); |
| 25 | expect(parseUploadCoverUrl("/uploads/")).toBeNull(); |
| 26 | expect(parseUploadCoverUrl("/uploads/abc/extra")).toBeNull(); |
| 27 | expect(parseUploadCoverUrl("https://example.com/uploads/abc")).toBeNull(); |
| 28 | }); |
| 29 | |
| 30 | it("classifies only OG-safe upload content types", () => { |
| 31 | expect(isOgImageType("image/png")).toBe(true); |
| 32 | expect(isOgImageType("image/jpeg; charset=binary")).toBe(true); |
| 33 | expect(isOgImageType("image/webp")).toBe(true); |
| 34 | expect(isOgImageType("image/gif")).toBe(true); |
| 35 | expect(isOgImageType("image/heic")).toBe(false); |
| 36 | expect(isOgImageType("application/pdf")).toBe(false); |
| 37 | }); |
| 38 | |
| 39 | it("parses every shared gradient preset", () => { |
| 40 | for (const preset of GRADIENT_PRESETS) { |
| 41 | const parsed = parseGradient(preset); |
| 42 | expect(isGradientPreset(preset)).toBe(true); |
| 43 | expect(parsed?.stops.length).toBeGreaterThanOrEqual(2); |
| 44 | expect(parsed?.stops[0].position).toBeGreaterThanOrEqual(0); |
| 45 | expect(parsed?.stops.at(-1)?.position).toBeLessThanOrEqual(100); |
| 46 | } |
| 47 | }); |
| 48 | |
| 49 | it("rejects unsupported gradient CSS and clamps explicit stop positions", () => { |
| 50 | expect(parseGradient("radial-gradient(#000 0%, #fff 100%)")).toBeNull(); |
| 51 | expect(parseGradient("linear-gradient(to right, #000 0%, #fff 100%)")).toBeNull(); |
| 52 | expect(parseGradient("linear-gradient(90deg, red 0%, blue 100%)")).toBeNull(); |
| 53 | expect(parseGradient("linear-gradient(90deg, #000, #fff)")).toBeNull(); |
| 54 | |
| 55 | const parsed = parseGradient("linear-gradient(90deg, #000 -10%, #ffffff 110%)"); |
| 56 | expect(parsed?.stops.map((stop) => stop.position)).toEqual([0, 100]); |
| 57 | }); |
| 58 | |
| 59 | it("keeps generated cover dimensions fixed", () => { |
| 60 | expect(SITE_COVER_WIDTH).toBe(1200); |
| 61 | expect(SITE_COVER_HEIGHT).toBe(630); |
| 62 | }); |
| 63 | }); |