/** * ทดสอบ sitemap.xml — กันกรณี "crawler เปิดได้แต่ได้ไฟล์ผิด" * (ไม่มีโรงที่ hidden, URL เป็นโดเมน production, XML ถูกต้อง) */ import { describe, expect, it } from "vitest"; import type { Cinema } from "@/lib/cinemas"; import { SITE } from "@/lib/site"; /** origin ปลอมสำหรับทดสอบ (โค้ดจริงหาจาก request) */ const ORIGIN = "https://example.test"; import { buildSitemapXml, sitemapLocation } from "@/lib/sitemap"; function cinema(slug: string, hidden = false): Cinema { return { slug, name: slug, tags: [], location_label: "", coordinates: null, description: "", map_url: null, address_lines: [], transport: [], contacts: [], payments: [], photos: [], ...(hidden ? { hidden: true } : {}), } as Cinema; } describe("sitemapLocation", () => { it("หน้าแรกต้องมี / ปิดท้าย", () => { expect(sitemapLocation(ORIGIN, "/")).toBe(`${ORIGIN}/`); }); it("พาธอื่นต่อท้ายโดเมนตรง ๆ", () => { expect(sitemapLocation(ORIGIN, "/about")).toBe(`${ORIGIN}/about`); expect(sitemapLocation(ORIGIN, "about")).toBe(`${ORIGIN}/about`); }); }); describe("buildSitemapXml", () => { it("มีหน้า static ครบตาม SITE.staticPages", () => { const xml = buildSitemapXml(ORIGIN, []); for (const page of SITE.staticPages) { expect(xml).toContain(`${sitemapLocation(ORIGIN, page.path)}`); } expect(xml.startsWith('')).toBe(true); expect(xml).toContain(""); }); it("เพิ่มหน้าโรงหนัง และข้ามโรงที่ hidden", () => { const xml = buildSitemapXml(ORIGIN, [ cinema("visible-cinema"), cinema("secret", true), ]); expect(xml).toContain(`${ORIGIN}/cinema/visible-cinema`); expect(xml).not.toContain("secret"); }); it("ไม่ปล่อย localhost ลงใน sitemap", () => { expect(buildSitemapXml(ORIGIN, [cinema("x")])).not.toContain("localhost"); }); it("escape อักขระ XML ใน slug", () => { const xml = buildSitemapXml(ORIGIN, [cinema("a&b")]); expect(xml).toContain("a&b"); }); });