Honest baseline after removing 45 gaming suppressions. Structural: lib/game plus root hooks extraction, CORS origin echo, DEV_ORIGINS layering. Fixes: error-cause binding, discord 5xx split, null-cache guard, co-located tests, rejecting-fetch coverage, naming and typing cleanup, socket controller, cache bound, cascade helpers, latch resets. Hardening: per-IP rate limit on API routes, deterministic-miss UX, retry cooldown plus cap. Suite 140 green, build plus lint clean.
109 lines
3.5 KiB
TypeScript
109 lines
3.5 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { getSteamDetails, getSteamReviewSummary } from "@/lib/game/steam";
|
|
import { steamdbRating } from "@/lib/game/game-format";
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe("steamdbRating", () => {
|
|
it("returns null for invalid input", () => {
|
|
expect(steamdbRating(5, 0)).toBeNull();
|
|
expect(steamdbRating(-1, 10)).toBeNull();
|
|
expect(steamdbRating(11, 10)).toBeNull();
|
|
expect(steamdbRating(Number.NaN, 10)).toBeNull();
|
|
});
|
|
|
|
it("pulls low-count ratios toward 50%", () => {
|
|
const rating = steamdbRating(9, 10);
|
|
expect(rating).not.toBeNull();
|
|
expect(rating as number).toBeLessThan(0.9);
|
|
expect(rating as number).toBeGreaterThan(0.5);
|
|
});
|
|
|
|
it("converges to the raw ratio with many reviews", () => {
|
|
const rating = steamdbRating(900000, 1000000);
|
|
expect(rating).not.toBeNull();
|
|
expect(Math.abs((rating as number) - 0.9)).toBeLessThan(0.01);
|
|
});
|
|
|
|
it("rates unanimous positives below 100%", () => {
|
|
const rating = steamdbRating(10, 10);
|
|
expect(rating).not.toBeNull();
|
|
expect(rating as number).toBeLessThan(1);
|
|
});
|
|
});
|
|
|
|
describe("getSteamDetails", () => {
|
|
it("returns entry data on success", async () => {
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(
|
|
async () =>
|
|
({
|
|
ok: true,
|
|
json: async () => ({ "100": { success: true, data: { name: "Game" } } }),
|
|
}) as Response,
|
|
),
|
|
);
|
|
await expect(getSteamDetails("100")).resolves.toEqual({ name: "Game" });
|
|
});
|
|
|
|
it("returns null when fetch rejects", async () => {
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(async () => {
|
|
throw new Error("down");
|
|
}),
|
|
);
|
|
await expect(getSteamDetails("100")).resolves.toBeNull();
|
|
});
|
|
|
|
it("returns null on failed entries and HTTP errors", async () => {
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(async () => ({ ok: true, json: async () => ({ "100": { success: false } }) })),
|
|
);
|
|
await expect(getSteamDetails("100")).resolves.toBeNull();
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(async () => ({ ok: false, status: 500 }) as Response),
|
|
);
|
|
await expect(getSteamDetails("100")).resolves.toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("getSteamReviewSummary", () => {
|
|
it("maps the query summary", async () => {
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(
|
|
async () =>
|
|
({
|
|
ok: true,
|
|
json: async () => ({
|
|
query_summary: {
|
|
review_score_desc: "Positive",
|
|
total_positive: 9,
|
|
total_negative: 1,
|
|
total_reviews: 10,
|
|
},
|
|
}),
|
|
}) as Response,
|
|
),
|
|
);
|
|
const summary = await getSteamReviewSummary("100");
|
|
expect(summary?.total).toBe(10);
|
|
expect(summary?.url).toContain("100");
|
|
});
|
|
|
|
it("returns null without a numeric total", async () => {
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(async () => ({ ok: true, json: async () => ({ query_summary: {} }) })),
|
|
);
|
|
await expect(getSteamReviewSummary("100")).resolves.toBeNull();
|
|
});
|
|
});
|