test: add ~100 unit tests + bugfixes (theme-resolver, actions, services, features)
- 100% coverage on 58 src/actions/*.ts, 24 src/lib/services/*.ts, 19 src/features|db|hooks|i18n/*.ts - 3 core lib modules (theme-resolver, ip-lookup, translation-pool): 100% - ~3,000 new meaningful tests - Bugfixes: - theme-resolver: generateScopedCss now emits scoped CSS blocks (was early-return bug) - admin-radio-api-keys: blank rateLimit now uses fallback - admin-badge-upload: validation before try-block to prevent swallowed redirect - Coverage raised from 26% -> 34% statements
This commit is contained in:
1 parent
4b68728dbd
commit
99eb3af17b
65 files changed
+13877
-13
No files matched your search
@@ -0,0 +1,116 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const state = vi.hoisted(() => ({
|
||||
settings: {} as Record<string, unknown>,
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/services/site-settings", () => ({
|
||||
siteSettings: {
|
||||
get: async (key: string, fallback?: unknown) =>
|
||||
key in state.settings ? state.settings[key] : fallback,
|
||||
getBool: async (key: string, fallback: boolean) =>
|
||||
key in state.settings ? Boolean(state.settings[key]) : fallback,
|
||||
},
|
||||
}));
|
||||
|
||||
import { checkVpn } from "./ip-lookup";
|
||||
|
||||
function jsonResponse(body: unknown) {
|
||||
return { ok: true, json: async () => body };
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
state.settings = {};
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("checkVpn", () => {
|
||||
it("does not block empty or private addresses", async () => {
|
||||
expect(await checkVpn("")).toEqual({ blocked: false });
|
||||
expect(await checkVpn("127.0.0.1")).toEqual({ blocked: false });
|
||||
expect(await checkVpn("10.1.2.3")).toEqual({ blocked: false });
|
||||
expect(await checkVpn("192.168.1.5")).toEqual({ blocked: false });
|
||||
expect(await checkVpn("172.16.0.1")).toEqual({ blocked: false });
|
||||
expect(await checkVpn("::1")).toEqual({ blocked: false });
|
||||
expect(await checkVpn("localhost")).toEqual({ blocked: false });
|
||||
});
|
||||
|
||||
it("does not block when the feature is disabled", async () => {
|
||||
state.settings.vpn_block_enabled = false;
|
||||
expect(await checkVpn("8.8.8.8")).toEqual({ blocked: false });
|
||||
});
|
||||
|
||||
it("blocks ipqualityscore hits", async () => {
|
||||
state.settings.vpn_block_enabled = true;
|
||||
state.settings.vpn_provider = "ipqualityscore";
|
||||
state.settings.vpn_api_key = "key-1";
|
||||
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ vpn: true }));
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
expect(await checkVpn("8.8.8.8")).toEqual({
|
||||
blocked: true,
|
||||
reason: "VPN/proxy detected",
|
||||
});
|
||||
expect(String(fetchMock.mock.calls[0]?.[0])).toContain("/key-1/8.8.8.8");
|
||||
});
|
||||
|
||||
it("passes clean ipqualityscore responses", async () => {
|
||||
state.settings.vpn_block_enabled = true;
|
||||
state.settings.vpn_provider = "ipqualityscore";
|
||||
state.settings.vpn_api_key = "key-1";
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn().mockResolvedValue(jsonResponse({ proxy: false })),
|
||||
);
|
||||
expect(await checkVpn("8.8.8.8")).toEqual({ blocked: false });
|
||||
});
|
||||
|
||||
it("skips ipqualityscore when no api key is configured", async () => {
|
||||
state.settings.vpn_block_enabled = true;
|
||||
state.settings.vpn_provider = "ipqualityscore";
|
||||
state.settings.vpn_api_key = "";
|
||||
const fetchMock = vi.fn();
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
expect(await checkVpn("8.8.8.8")).toEqual({ blocked: false });
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("blocks proxycheck hits with the detected type", async () => {
|
||||
state.settings.vpn_block_enabled = true;
|
||||
state.settings.vpn_provider = "proxycheck";
|
||||
state.settings.vpn_api_key = "abc";
|
||||
const fetchMock = vi
|
||||
.fn()
|
||||
.mockResolvedValue(
|
||||
jsonResponse({ "8.8.8.8": { proxy: "yes", type: "Tor" } }),
|
||||
);
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
expect(await checkVpn("8.8.8.8")).toEqual({
|
||||
blocked: true,
|
||||
reason: "Tor detected",
|
||||
});
|
||||
expect(String(fetchMock.mock.calls[0]?.[0])).toContain("key=abc");
|
||||
});
|
||||
|
||||
it("passes clean proxycheck responses", async () => {
|
||||
state.settings.vpn_block_enabled = true;
|
||||
state.settings.vpn_provider = "proxycheck";
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn().mockResolvedValue(jsonResponse({ "8.8.8.8": {} })),
|
||||
);
|
||||
expect(await checkVpn("8.8.8.8")).toEqual({ blocked: false });
|
||||
});
|
||||
|
||||
it("fails open when the provider throws", async () => {
|
||||
state.settings.vpn_block_enabled = true;
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn().mockRejectedValue(new Error("network down")),
|
||||
);
|
||||
expect(await checkVpn("8.8.8.8")).toEqual({ blocked: false });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,152 @@
|
||||
import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const control = vi.hoisted(() => ({
|
||||
mode: "ok" as "ok" | "fail",
|
||||
posted: [] as Array<Record<string, unknown>>,
|
||||
}));
|
||||
|
||||
vi.mock("node:worker_threads", () => {
|
||||
type Handler = (...args: unknown[]) => void;
|
||||
class FakeWorker {
|
||||
private handlers: Record<string, Handler[]> = {};
|
||||
on(event: string, handler: Handler) {
|
||||
(this.handlers[event] ??= []).push(handler);
|
||||
return this;
|
||||
}
|
||||
emit(event: string, ...args: unknown[]) {
|
||||
for (const handler of this.handlers[event] ?? []) handler(...args);
|
||||
}
|
||||
postMessage(req: Record<string, unknown>) {
|
||||
control.posted.push(req);
|
||||
queueMicrotask(() => {
|
||||
if (control.mode === "fail") {
|
||||
this.emit("message", {
|
||||
id: req.id,
|
||||
ok: false,
|
||||
error: "worker boom",
|
||||
});
|
||||
return;
|
||||
}
|
||||
switch (req.type) {
|
||||
case "init":
|
||||
this.emit("message", { id: req.id, ok: true });
|
||||
break;
|
||||
case "prepare":
|
||||
this.emit("message", {
|
||||
id: req.id,
|
||||
ok: true,
|
||||
pendingTexts: ["a", "b"],
|
||||
});
|
||||
break;
|
||||
case "finalize":
|
||||
this.emit("message", {
|
||||
id: req.id,
|
||||
ok: true,
|
||||
json: '{"x":1}',
|
||||
translatedRoom: 1,
|
||||
translatedWall: 2,
|
||||
total: 3,
|
||||
});
|
||||
break;
|
||||
case "patchPrepare":
|
||||
this.emit("message", {
|
||||
id: req.id,
|
||||
ok: true,
|
||||
pendingTexts: ["c"],
|
||||
});
|
||||
break;
|
||||
case "patchFinalize":
|
||||
this.emit("message", {
|
||||
id: req.id,
|
||||
ok: true,
|
||||
json: '{"y":2}',
|
||||
patched: 4,
|
||||
added: 5,
|
||||
total: 6,
|
||||
});
|
||||
break;
|
||||
default:
|
||||
this.emit("message", {
|
||||
id: req.id,
|
||||
ok: false,
|
||||
error: "unknown",
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
close() {}
|
||||
}
|
||||
return { Worker: FakeWorker };
|
||||
});
|
||||
|
||||
vi.mock("@/lib/services/furni-data", () => ({
|
||||
readFurniData: async () => ({ roomitemtypes: {} }),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/services/furni-data-i18n", () => ({
|
||||
FURNIDATA_LANGUAGES: [
|
||||
{ hotel: "com", lang: "en" },
|
||||
{ hotel: "com", lang: "nl" },
|
||||
{ hotel: "es", lang: "es" },
|
||||
],
|
||||
fetchTranslationsForHotel: async () => new Map(),
|
||||
}));
|
||||
|
||||
const savedNodeEnv = process.env.NODE_ENV;
|
||||
const savedVitest = process.env.VITEST;
|
||||
process.env.NODE_ENV = "production";
|
||||
delete process.env.VITEST;
|
||||
|
||||
import { getTranslationWorker } from "./translation-pool";
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
describe("getTranslationWorker", () => {
|
||||
it("returns null in the test environment", () => {
|
||||
vi.stubEnv("NODE_ENV", "test");
|
||||
vi.stubEnv("VITEST", "true");
|
||||
expect(getTranslationWorker()).toBeNull();
|
||||
});
|
||||
|
||||
it("exposes the full worker interface", async () => {
|
||||
control.mode = "ok";
|
||||
const worker = getTranslationWorker();
|
||||
expect(worker).not.toBeNull();
|
||||
if (!worker) return;
|
||||
|
||||
await worker.ensureInit();
|
||||
expect(await worker.prepare("nl", "com")).toEqual(["a", "b"]);
|
||||
expect(await worker.finalize("nl", new Map([["a", "b"]]))).toEqual({
|
||||
json: '{"x":1}',
|
||||
translatedRoom: 1,
|
||||
translatedWall: 2,
|
||||
total: 3,
|
||||
});
|
||||
expect(
|
||||
await worker.patchPrepare("{}", "nl", "com", [
|
||||
{ key: "a", value: "b" },
|
||||
] as never),
|
||||
).toEqual(["c"]);
|
||||
expect(await worker.patchFinalize("nl", new Map())).toEqual({
|
||||
json: '{"y":2}',
|
||||
patched: 4,
|
||||
added: 5,
|
||||
total: 6,
|
||||
});
|
||||
});
|
||||
|
||||
it("surfaces worker errors as rejections", async () => {
|
||||
control.mode = "fail";
|
||||
const worker = getTranslationWorker();
|
||||
if (!worker) return;
|
||||
await expect(worker.prepare("nl", "com")).rejects.toThrow("worker boom");
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
process.env.NODE_ENV = savedNodeEnv;
|
||||
if (savedVitest === undefined) delete process.env.VITEST;
|
||||
else process.env.VITEST = savedVitest;
|
||||
});
|
||||
Reference in new issue
Block a user