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,163 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const state = vi.hoisted(() => ({
|
||||
existingRows: [] as unknown[],
|
||||
maxRows: [] as unknown[],
|
||||
insertValues: vi.fn(async () => [{}]),
|
||||
fail: false,
|
||||
giveBadge: vi.fn(async () => undefined),
|
||||
requirePermission: vi.fn(),
|
||||
revalidatePath: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/admin/guard", () => ({
|
||||
requirePermission: state.requirePermission,
|
||||
}));
|
||||
vi.mock("@/lib/permissions", () => ({
|
||||
PERMS: { CATALOG_EDIT: "admin.catalog.edit" },
|
||||
}));
|
||||
vi.mock("@/lib/services/rcon", () => ({
|
||||
rcon: { giveBadge: state.giveBadge },
|
||||
}));
|
||||
vi.mock("next/cache", () => ({ revalidatePath: state.revalidatePath }));
|
||||
vi.mock("@/lib/db", async () => {
|
||||
const schema = await import("@/db/schema");
|
||||
const { createFakeDb } = await import("@/test/fake-db");
|
||||
const fake = createFakeDb((_table, projection) => {
|
||||
if (state.fail) throw new Error("db down");
|
||||
return "maxSlot" in projection ? state.maxRows : state.existingRows;
|
||||
});
|
||||
return {
|
||||
...schema,
|
||||
db: {
|
||||
...fake,
|
||||
insert: vi.fn(() => ({ values: state.insertValues })),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { giveBadge } from "./admin-badges";
|
||||
|
||||
function form(userId: string, code: string) {
|
||||
const f = new FormData();
|
||||
f.set("userId", userId);
|
||||
f.set("code", code);
|
||||
return f;
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
state.existingRows = [];
|
||||
state.maxRows = [];
|
||||
state.fail = false;
|
||||
state.insertValues.mockResolvedValue([{}]);
|
||||
state.giveBadge.mockResolvedValue(undefined);
|
||||
state.requirePermission.mockResolvedValue({ id: 1, rank: 7, username: "a" });
|
||||
});
|
||||
|
||||
describe("giveBadge", () => {
|
||||
it("grants via rcon and persists at the next free slot", async () => {
|
||||
state.maxRows = [{ maxSlot: 5 }];
|
||||
|
||||
await giveBadge(form("3", "ACH_Test"));
|
||||
|
||||
expect(state.requirePermission).toHaveBeenCalledWith(PERMS.CATALOG_EDIT);
|
||||
expect(state.giveBadge).toHaveBeenCalledWith(3, "ACH_Test");
|
||||
expect(state.insertValues).toHaveBeenCalledWith({
|
||||
userId: 3,
|
||||
slotId: 6,
|
||||
badgeCode: "ACH_Test",
|
||||
});
|
||||
expect(state.revalidatePath).toHaveBeenCalledWith("/admin/badges");
|
||||
});
|
||||
|
||||
it("uses slot 1 when the user has no badges yet", async () => {
|
||||
state.maxRows = [];
|
||||
|
||||
await giveBadge(form("3", "NEW"));
|
||||
|
||||
expect(state.insertValues).toHaveBeenCalledWith({
|
||||
userId: 3,
|
||||
slotId: 1,
|
||||
badgeCode: "NEW",
|
||||
});
|
||||
});
|
||||
|
||||
it("treats a null max slot as zero", async () => {
|
||||
state.maxRows = [{ maxSlot: null }];
|
||||
|
||||
await giveBadge(form("3", "NEW"));
|
||||
|
||||
expect(state.insertValues).toHaveBeenCalledWith({
|
||||
userId: 3,
|
||||
slotId: 1,
|
||||
badgeCode: "NEW",
|
||||
});
|
||||
});
|
||||
|
||||
it("does not persist a duplicate badge", async () => {
|
||||
state.existingRows = [{ id: 10 }];
|
||||
|
||||
await giveBadge(form("4", "DUP"));
|
||||
|
||||
expect(state.giveBadge).toHaveBeenCalledWith(4, "DUP");
|
||||
expect(state.insertValues).not.toHaveBeenCalled();
|
||||
expect(state.revalidatePath).toHaveBeenCalledWith("/admin/badges");
|
||||
});
|
||||
|
||||
it("returns early for a non-positive user id", async () => {
|
||||
await giveBadge(form("0", "X"));
|
||||
|
||||
expect(state.giveBadge).not.toHaveBeenCalled();
|
||||
expect(state.insertValues).not.toHaveBeenCalled();
|
||||
expect(state.revalidatePath).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns early for an empty code", async () => {
|
||||
await giveBadge(form("5", " "));
|
||||
|
||||
expect(state.giveBadge).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns early when the code field is absent", async () => {
|
||||
const f = new FormData();
|
||||
f.set("userId", "5");
|
||||
|
||||
await giveBadge(f);
|
||||
|
||||
expect(state.giveBadge).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("normalises and truncates the badge code to 32 characters", async () => {
|
||||
const longCode = "a".repeat(40);
|
||||
|
||||
await giveBadge(form("5", ` ${longCode} `));
|
||||
|
||||
expect(state.giveBadge).toHaveBeenCalledWith(5, "a".repeat(32));
|
||||
});
|
||||
|
||||
it("swallows persistence failures after the rcon grant", async () => {
|
||||
state.insertValues.mockRejectedValueOnce(new Error("db down"));
|
||||
|
||||
await expect(giveBadge(form("6", "BEST"))).resolves.toBeUndefined();
|
||||
|
||||
expect(state.giveBadge).toHaveBeenCalledWith(6, "BEST");
|
||||
expect(state.revalidatePath).toHaveBeenCalledWith("/admin/badges");
|
||||
});
|
||||
|
||||
it("swallows select failures and still revalidates", async () => {
|
||||
state.fail = true;
|
||||
|
||||
await expect(giveBadge(form("6", "BEST"))).resolves.toBeUndefined();
|
||||
|
||||
expect(state.revalidatePath).toHaveBeenCalledWith("/admin/badges");
|
||||
});
|
||||
|
||||
it("propagates a permission denial", async () => {
|
||||
state.requirePermission.mockRejectedValueOnce(new Error("redirect:/admin"));
|
||||
|
||||
await expect(giveBadge(form("1", "X"))).rejects.toThrow("redirect:/admin");
|
||||
expect(state.giveBadge).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user