- บทความจากหลังบ้าน: /for-film-lovers, /for-cinemas (ArticlePage + ArticleEditor แบบบล็อกย่อหน้า/รูป) - แชร์: ปุ่มบนการ์ดและท้ายหน้ารายละเอียด · ลิงก์ย่อ /c/<slug> (302) · ธีมแก้วตาม DESIGN.md - PWA: manifest/sw/offline + ถอดการล็อกซูม (WCAG 1.4.4) + touch target ≥44px - แอนิเมชัน reveal: IntersectionObserver + gate ด้วย html.fx-motion · เคารพ prefers-reduced-motion - ค้นหา: normalize/ตัดวรรณยุกต์ + AND หลายคำ + จัดอันดับ · รองรับ /?q= จาก SearchAction - SEO: pageMeta (canonical/OG/Twitter) + JSON-LD (WebSite/Organization/BreadcrumbList/MovieTheater/Article) - ไม่ฝังโดเมนในโค้ด: origin มาจาก request (x-forwarded-host/proto) ทั้ง canonical, sitemap, robots, ลิงก์แชร์ - กัน AI: robots.txt (Nitro route, บล็อก AI 21 ตัว) + ai.txt + OG image 1200x630 - เอกสาร: README สรุปโปรเจกต์, DESIGN.md (motion/PWA/SEO/ข้อมูล), architecture.html, admin-guide - tests: 135 ผ่าน (16 ไฟล์)
184 lines
7.4 KiB
TypeScript
184 lines
7.4 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { CINEMAS, DEFAULT_CATEGORY, type Cinema } from "./cinemas";
|
|
import {
|
|
buildSearchIndex,
|
|
FIELD_WEIGHTS,
|
|
filterCinemas,
|
|
foldSearchText,
|
|
normalizeSearchText,
|
|
scoreCinema,
|
|
searchTokens,
|
|
} from "./search";
|
|
|
|
describe("normalizeSearchText", () => {
|
|
it("ตัดช่องว่างหัวท้าย ลดตัวพิมพ์ และแปลงตัวคั่นเป็นช่องว่าง", () => {
|
|
expect(normalizeSearchText(" HOUSE · สามย่าน ")).toBe("house สามย่าน");
|
|
expect(normalizeSearchText("DOC CLUB & FRIENDS")).toBe("doc club friends");
|
|
});
|
|
|
|
it("ตัดอักขระล่องหนที่ติดมากับการคัดลอก", () => {
|
|
expect(normalizeSearchText("เฮ้า\u200Bส์")).toBe("เฮ้าส์");
|
|
});
|
|
});
|
|
|
|
describe("foldSearchText", () => {
|
|
it("ตัดวรรณยุกต์ภาษาอังกฤษ (Française → francaise)", () => {
|
|
expect(foldSearchText("Française")).toBe("francaise");
|
|
});
|
|
|
|
it("ตัดวรรณยุกต์ไทย (เฮ้าส์ → เฮาส)", () => {
|
|
expect(foldSearchText("เฮ้าส์")).toBe("เฮาส");
|
|
});
|
|
});
|
|
|
|
describe("searchTokens", () => {
|
|
it("แยกหลายคำ และคืนอาร์เรย์ว่างเมื่อไม่มีคำค้น", () => {
|
|
expect(searchTokens(" สามย่าน house ")).toEqual(["สามย่าน", "house"]);
|
|
expect(searchTokens(" ")).toEqual([]);
|
|
expect(searchTokens("·")).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("filterCinemas", () => {
|
|
it("คืนทุกโรงหนังเมื่อเลือกหมวดทั้งหมดและไม่พิมพ์ค้นหา", () => {
|
|
const result = filterCinemas(CINEMAS, {
|
|
category: DEFAULT_CATEGORY,
|
|
query: "",
|
|
});
|
|
expect(result.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("กรองตามหมวดหมู่จาก tags", () => {
|
|
const result = filterCinemas(CINEMAS, {
|
|
category: "อาร์ตเฮาส์",
|
|
query: "",
|
|
});
|
|
expect(result.length).toBeGreaterThan(0);
|
|
expect(result.every((cinema) => cinema.tags.includes("อาร์ตเฮาส์"))).toBe(
|
|
true,
|
|
);
|
|
});
|
|
|
|
it("ค้นหาแบบไม่สนตัวพิมพ์และตัดช่องว่างหัวท้าย", () => {
|
|
const result = filterCinemas(CINEMAS, {
|
|
category: DEFAULT_CATEGORY,
|
|
query: " house ",
|
|
});
|
|
expect(result.map((cinema) => cinema.slug)).toContain(
|
|
"house-cinema-samyan",
|
|
);
|
|
});
|
|
|
|
it("คืนอาร์เรย์ว่างเมื่อไม่พบคำค้น", () => {
|
|
expect(
|
|
filterCinemas(CINEMAS, {
|
|
category: DEFAULT_CATEGORY,
|
|
query: "ไม่มีโรงนี้แน่นอน",
|
|
}),
|
|
).toEqual([]);
|
|
});
|
|
|
|
// ── ค้นด้วยข้อความที่แสดงบนการ์ดจริง ────────────────────────────────
|
|
it("ค้นด้วยชื่อไทยที่แสดงบนการ์ด (name_th) ได้", () => {
|
|
const result = filterCinemas(CINEMAS, {
|
|
category: DEFAULT_CATEGORY,
|
|
query: "เซ็นจูรี่มูฟวี่พลาซ่า",
|
|
});
|
|
expect(result.map((cinema) => cinema.slug)).toContain(
|
|
"century-the-movie-plaza",
|
|
);
|
|
});
|
|
|
|
it("ค้นด้วยชื่อไทยที่พิมพ์ไม่ครบ (คำนำหน้า) ได้", () => {
|
|
const result = filterCinemas(CINEMAS, {
|
|
category: DEFAULT_CATEGORY,
|
|
query: "เซ็นจูรี่",
|
|
});
|
|
expect(result.length).toBeGreaterThan(1);
|
|
expect(
|
|
result.every((cinema) => (cinema.name_th ?? "").includes("เซ็นจูรี่")),
|
|
).toBe(true);
|
|
});
|
|
|
|
it("ค้นชื่ออังกฤษแบบไม่ใส่วรรณยุกต์ก็เจอ", () => {
|
|
expect(
|
|
filterCinemas(CINEMAS, {
|
|
category: DEFAULT_CATEGORY,
|
|
query: "francaise",
|
|
}).map((cinema) => cinema.slug),
|
|
).toContain("alliance-francaise-de-bangkok");
|
|
});
|
|
|
|
it("ค้นด้วยชื่อหมวดที่แสดงบนการ์ดได้ (ทั้งคีย์ไทยและป้ายภาษาอังกฤษ)", () => {
|
|
const th = filterCinemas(CINEMAS, {
|
|
category: DEFAULT_CATEGORY,
|
|
query: "อาร์ตเฮาส์",
|
|
});
|
|
expect(th.map((cinema) => cinema.slug)).toContain("house-cinema-samyan");
|
|
|
|
const en = filterCinemas(CINEMAS, {
|
|
category: DEFAULT_CATEGORY,
|
|
query: "arthouse",
|
|
tagLabels: { อาร์ตเฮาส์: "Arthouse" },
|
|
});
|
|
expect(en.map((cinema) => cinema.slug)).toContain("house-cinema-samyan");
|
|
});
|
|
|
|
it("ค้นหลายคำสลับลำดับได้ (ทุกคำต้องเจอ)", () => {
|
|
const result = filterCinemas(CINEMAS, {
|
|
category: DEFAULT_CATEGORY,
|
|
query: "samyan HOUSE",
|
|
});
|
|
expect(result.map((cinema) => cinema.slug)).toEqual([
|
|
"house-cinema-samyan",
|
|
]);
|
|
});
|
|
|
|
it("ค้นข้อความที่คัดลอกมาจากการ์ดทั้งบรรทัด (มีตัวคั่น ·) ได้", () => {
|
|
const result = filterCinemas(CINEMAS, {
|
|
category: DEFAULT_CATEGORY,
|
|
query: "HOUSE สามย่าน · อาร์ตเฮาส์",
|
|
});
|
|
expect(result.map((cinema) => cinema.slug)).toContain(
|
|
"house-cinema-samyan",
|
|
);
|
|
});
|
|
|
|
it("เรียงผลลัพธ์ที่ตรงที่สุดก่อน", () => {
|
|
const result = filterCinemas(CINEMAS, {
|
|
category: DEFAULT_CATEGORY,
|
|
query: "cineplex",
|
|
});
|
|
expect(result[0]?.slug).toBe("thana-cineplex");
|
|
});
|
|
});
|
|
|
|
describe("scoreCinema", () => {
|
|
const house = CINEMAS.find(
|
|
(cinema) => cinema.slug === "house-cinema-samyan",
|
|
) as Cinema;
|
|
|
|
it("คืน 0 เมื่อไม่มีคำค้น หรือคำค้นไม่ตรงเลย", () => {
|
|
expect(scoreCinema(house, [])).toBe(0);
|
|
expect(scoreCinema(house, searchTokens("zzzz"))).toBe(0);
|
|
});
|
|
|
|
it("ชื่อที่ตรงทั้งคำได้คะแนนสูงกว่าการเจอในรายละเอียด", () => {
|
|
const byName = scoreCinema(house, searchTokens("house"));
|
|
const byTag = scoreCinema(house, searchTokens("อาร์ตเฮาส์"));
|
|
expect(byName).toBeGreaterThan(byTag);
|
|
expect(byName).toBeGreaterThanOrEqual(FIELD_WEIGHTS.names);
|
|
});
|
|
});
|
|
|
|
describe("buildSearchIndex", () => {
|
|
it("เตรียมฟิลด์ชื่อ/สถานที่/หมวดไว้เทียบ และไม่เก็บค่าว่าง", () => {
|
|
const index = buildSearchIndex(CINEMAS[0], { อาร์ตเฮาส์: "Arthouse" });
|
|
expect(index.length).toBeGreaterThan(0);
|
|
expect(index.every((field) => field.exact !== "")).toBe(true);
|
|
expect(index.some((field) => field.exact.includes("arthouse"))).toBe(true);
|
|
expect(index.some((field) => field.group === "names")).toBe(true);
|
|
});
|
|
});
|