/*
Unit tests for image sitemap 0150 covering its 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 { test, expect } from "vitest";
import {
harvestContentImage,
generateImageSitemapXml,
isHtmlRedirectPage,
isSyntheticPreviewUrl,
type SitemapImageEntry,
} from "../image-sitemap.ts";
const SITE = "https://example.org";
test("harvests a hero data-content-image and absolutizes the /_astro src", () => {
const html = `
`;
const r = harvestContentImage(html, SITE);
expect(r.loc).toBe("https://example.org/de/about");
expect(r.imageUrls).toEqual(["https://example.org/_astro/hero-1.BEybXadf.webp"]);
expect(r.title).toBe("A photo");
});
test("harvests the authored x-content-image head signal", () => {
const html = `
`;
const r = harvestContentImage(html, SITE);
expect(r.imageUrls).toEqual(["https://cdn.example.org/lead.webp"]);
expect(r.title).toBe("Lead alt");
});
test("excludes RFC-0150 preview screenshots and og-image fallbacks", () => {
expect(isSyntheticPreviewUrl("https://example.org/preview/de/home.png")).toBe(true);
expect(isSyntheticPreviewUrl("https://example.org/og-image.png")).toBe(true);
expect(isSyntheticPreviewUrl("https://example.org/_astro/hero.abc.webp")).toBe(false);
const html = `
`;
const r = harvestContentImage(html, SITE);
expect(r.imageUrls).toEqual([]);
});
test("reports multiple distinct content images (uniqueness violation surface)", () => {
const html = `
`;
const r = harvestContentImage(html, SITE);
expect(r.imageUrls.length).toBe(2);
});
test("deduplicates the same image referenced twice", () => {
const html = `
`;
const r = harvestContentImage(html, SITE);
expect(r.imageUrls).toEqual(["https://example.org/_astro/a.1.webp"]);
});
test("returns null loc when no canonical link present", () => {
const r = harvestContentImage(`
`, SITE);
expect(r.loc).toBe(null);
});
test("detects meta-refresh stubs but not soft window.location redirects on content pages", () => {
expect(isHtmlRedirectPage(``)).toBe(true);
// Full content pages carry a soft language redirect (RFC-0159) — NOT a stub.
expect(
isHtmlRedirectPage(`real content`),
).toBe(false);
expect(isHtmlRedirectPage(`real content`)).toBe(false);
});
test("formats a valid image sitemap with escaping", () => {
const entries: SitemapImageEntry[] = [
{
loc: "https://example.org/a?x=1&y=2",
imageUrl: "https://example.org/_astro/a.webp",
title: "T & ",
},
];
const xml = generateImageSitemapXml(entries);
expect(xml).toMatch(/xmlns:image="http:\/\/www\.google\.com\/schemas\/sitemap-image\/1\.1"/);
expect(xml).toMatch(/https:\/\/example\.org\/_astro\/a\.webp<\/image:loc>/);
expect(xml).toMatch(/https:\/\/example\.org\/a\?x=1&y=2<\/loc>/);
expect(xml).toMatch(/T & <b><\/image:title>/);
});
test("empty entries still yields a valid urlset", () => {
const xml = generateImageSitemapXml([]);
expect(xml).toMatch(//);
});