/* Unit tests for image sitemap covering isHtmlRedirectPage; isSyntheticPreviewUrl; harvestContentImage. It also guards the documented public contract and regression-sensitive edge cases. Not a contract source of truth — the module under test owns the authoritative behavior; fixtures are minimal scaffolding only. RFC-1311: Compass header sweep — declares the image suite's contract for test-file scaffold coverage. */ import { describe, expect, it } from "vitest"; import { generateImageSitemapXml, harvestContentImage, isHtmlRedirectPage, isSyntheticPreviewUrl, } from "../image-sitemap.ts"; describe("isHtmlRedirectPage", () => { it("detects meta-refresh redirect", () => { expect(isHtmlRedirectPage('')).toBe(true); }); it("returns false for normal pages", () => { expect(isHtmlRedirectPage("Hello")).toBe(false); }); // RFC-1285: bundled scripts that read window.location are not redirects — // pins the single-owner predicate against the broad duplicate it replaces. it("returns false for pages whose bundled scripts read window.location", () => { expect( isHtmlRedirectPage( '
x
', ), ).toBe(false); }); }); describe("isSyntheticPreviewUrl", () => { it("detects preview screenshots", () => { expect(isSyntheticPreviewUrl("https://example.com/preview/page.png")).toBe(true); }); it("detects og-image fallback", () => { expect(isSyntheticPreviewUrl("https://example.com/og-image.png")).toBe(true); }); it("returns false for content images", () => { expect(isSyntheticPreviewUrl("https://example.com/_astro/img.jpg")).toBe(false); }); }); describe("harvestContentImage", () => { it("extracts canonical URL from link tag", () => { const html = ''; const result = harvestContentImage(html, "https://example.com"); expect(result.loc).toBe("https://example.com/de/page"); }); it("returns null loc when no canonical", () => { const result = harvestContentImage("", "https://example.com"); expect(result.loc).toBeNull(); }); it("harvests img with data-content-image attribute", () => { const html = 'Hero'; const result = harvestContentImage(html, "https://example.com"); expect(result.imageUrls).toContain("https://example.com/_astro/hero.jpg"); expect(result.title).toBe("Hero"); }); it("harvests meta x-content-image", () => { const html = ''; const result = harvestContentImage(html, "https://example.com"); expect(result.imageUrls).toContain("https://example.com/img.jpg"); }); it("excludes synthetic preview URLs", () => { const html = 'OG'; const result = harvestContentImage(html, "https://example.com"); expect(result.imageUrls).toHaveLength(0); }); it("deduplicates identical URLs", () => { const html = ` A `; const result = harvestContentImage(html, "https://example.com"); expect(result.imageUrls).toHaveLength(1); }); it("falls back to og:image:alt for title", () => { const html = ` `; const result = harvestContentImage(html, "https://example.com"); expect(result.title).toBe("OG Alt"); }); }); describe("generateImageSitemapXml", () => { it("produces valid image sitemap XML", () => { const result = generateImageSitemapXml([ { loc: "https://example.com/page", imageUrl: "https://example.com/img.jpg", title: "Test" }, ]); expect(result).toContain('https://example.com/page"); expect(result).toContain("https://example.com/img.jpg"); expect(result).toContain("Test"); }); it("produces empty urlset for no entries", () => { const result = generateImageSitemapXml([]); expect(result).toContain(""); }); it("escapes XML in URLs", () => { const result = generateImageSitemapXml([ { loc: "https://example.com/a&b", imageUrl: "https://example.com/" }, ]); expect(result).toContain("a&b"); expect(result).toContain("<img>"); }); });