95 lines
3.4 KiB
TypeScript
95 lines
3.4 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import type { AnalyticsEvent } from "@/lib/analytics-types";
|
|
|
|
/**
|
|
* สเปกของอันดับ "ยอดนิยม" — ต้องไม่จัดอันดับก่อนมีข้อมูลครบ 6 เดือน
|
|
*/
|
|
|
|
const readEventsMock = vi.fn<() => Promise<AnalyticsEvent[]>>();
|
|
|
|
vi.mock("@/lib/analytics.server", () => ({
|
|
readEvents: () => readEventsMock(),
|
|
appendEvent: async () => {},
|
|
}));
|
|
|
|
const DAY = 24 * 60 * 60 * 1000;
|
|
const NOW = Date.parse("2026-11-01T00:00:00.000Z");
|
|
|
|
function event(daysAgo: number, type: AnalyticsEvent["type"], slug: string): AnalyticsEvent {
|
|
return {
|
|
type,
|
|
at: new Date(NOW - daysAgo * DAY).toISOString(),
|
|
visitor: "v",
|
|
locale: "th",
|
|
slug,
|
|
};
|
|
}
|
|
|
|
async function compute(events: AnalyticsEvent[]) {
|
|
readEventsMock.mockResolvedValue(events);
|
|
vi.resetModules();
|
|
const { computePopularity } = await import("@/lib/popularity.server");
|
|
return computePopularity(NOW);
|
|
}
|
|
|
|
describe("computePopularity", () => {
|
|
beforeEach(() => {
|
|
readEventsMock.mockReset();
|
|
});
|
|
|
|
it("ข้อมูลแค่ 2 วัน → ready=false และไม่คืนอันดับ", async () => {
|
|
const state = await compute([event(2, "cinema_view", "a"), event(1, "cinema_view", "b")]);
|
|
expect(state.ready).toBe(false);
|
|
expect(state.slugs).toEqual([]);
|
|
expect(state.history_days).toBe(2);
|
|
expect(state.days_remaining).toBe(178);
|
|
});
|
|
|
|
it("ข้อมูลครบ 180 วัน → ready=true และเริ่มจัดอันดับ", async () => {
|
|
const state = await compute([
|
|
event(200, "cinema_view", "old"), // เก่ากว่า 6 เดือน = ใช้เป็นหลักฐานประวัติ
|
|
event(10, "cinema_view", "a"),
|
|
event(10, "cinema_view", "a"),
|
|
event(9, "cinema_select", "b"),
|
|
]);
|
|
expect(state.ready).toBe(true);
|
|
expect(state.slugs).toEqual(["a", "b"]);
|
|
expect(state.days_remaining).toBe(0);
|
|
});
|
|
|
|
it("event เก่ากว่า 90 วันไม่ถูกนับคะแนน แต่ยังนับประวัติ", async () => {
|
|
const state = await compute([
|
|
event(200, "cinema_view", "long-ago"),
|
|
event(1, "cinema_view", "fresh"),
|
|
]);
|
|
expect(state.ready).toBe(true);
|
|
expect(state.slugs).toEqual(["fresh"]);
|
|
});
|
|
|
|
it("cinema_select ได้น้ำหนักมากกว่า cinema_view", async () => {
|
|
const state = await compute([
|
|
event(200, "cinema_view", "seed"),
|
|
event(1, "cinema_view", "viewed"),
|
|
event(1, "cinema_select", "clicked"),
|
|
]);
|
|
expect(state.slugs[0]).toBe("clicked");
|
|
});
|
|
|
|
it("ไม่นับ event ที่ไม่มี slug (เช่น search/near_me)", async () => {
|
|
const state = await compute([
|
|
event(200, "cinema_view", "seed"),
|
|
{ type: "search", at: new Date(NOW).toISOString(), visitor: "v", locale: "th", query: "x" },
|
|
{ type: "near_me", at: new Date(NOW).toISOString(), visitor: "v", locale: "th" },
|
|
]);
|
|
expect(state.slugs).toEqual([]);
|
|
});
|
|
|
|
it("ไม่มีข้อมูลเลย → history 0 วัน", async () => {
|
|
const state = await compute([]);
|
|
expect(state.ready).toBe(false);
|
|
expect(state.history_days).toBe(0);
|
|
expect(state.total_events).toBe(0);
|
|
});
|
|
});
|