88 lines
3.0 KiB
TypeScript
88 lines
3.0 KiB
TypeScript
import { revalidatePath } from "next/cache";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const { execute, staff } = vi.hoisted(() => ({
|
|
execute: vi.fn(),
|
|
staff: { id: 1, rank: 7, username: "admin" },
|
|
}));
|
|
|
|
vi.mock("@/features/housekeeping/domains/people/services/mutations", () => ({
|
|
createPeopleMutationInvocation: vi.fn((staff, correlationId) => ({
|
|
expectedActorId: staff.id,
|
|
correlationId,
|
|
legacy: true,
|
|
})),
|
|
peopleMutationService: { execute },
|
|
}));
|
|
vi.mock("@/lib/admin/guard", () => ({
|
|
requirePermission: vi.fn(async () => staff),
|
|
requirePermissionRateLimited: vi.fn(async () => staff),
|
|
}));
|
|
vi.mock("@/lib/permissions", () => ({
|
|
PERMS: {
|
|
SETTINGS_EDIT: "admin.settings.edit",
|
|
USERS_EDIT: "admin.users.edit",
|
|
WORDFILTER_EDIT: "admin.wordfilter.edit",
|
|
},
|
|
}));
|
|
vi.mock("@/lib/safe-action-shared", () => ({
|
|
actionOk: (data?: unknown) => ({ ok: true, data: data ?? {} }),
|
|
actionError: (error: string) => ({ ok: false, error }),
|
|
}));
|
|
vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
|
|
|
|
import { disbandGuild } from "./admin-guilds";
|
|
import { addWhitelist } from "./admin-ip";
|
|
import { createTeam, deleteTeam } from "./admin-teams";
|
|
import { deleteWord } from "./admin-wordfilter";
|
|
|
|
const form = (data: Record<string, string>) =>
|
|
({ get: (key: string) => data[key] ?? null }) as FormData;
|
|
const failure = (code: string) => ({
|
|
ok: false as const,
|
|
error: { code, messageKey: "errors.housekeeping.dependencyUnavailable" },
|
|
correlationId: "wrapper-failure",
|
|
});
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe("legacy wrapper failure compatibility", () => {
|
|
it("keeps guild persistence failures throwing while a missing guild remains a no-op", async () => {
|
|
execute.mockResolvedValueOnce(failure("DEPENDENCY_UNAVAILABLE"));
|
|
await expect(disbandGuild(form({ id: "9" }))).rejects.toThrow();
|
|
expect(revalidatePath).not.toHaveBeenCalled();
|
|
|
|
execute.mockResolvedValueOnce(failure("NOT_FOUND"));
|
|
await expect(disbandGuild(form({ id: "9" }))).resolves.toBeUndefined();
|
|
expect(revalidatePath).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("keeps IP and team persistence failures throwing", async () => {
|
|
execute.mockResolvedValueOnce(failure("DEPENDENCY_UNAVAILABLE"));
|
|
await expect(
|
|
addWhitelist(form({ ipAddress: "192.0.2.1" })),
|
|
).rejects.toThrow();
|
|
|
|
execute.mockResolvedValueOnce(failure("DEPENDENCY_UNAVAILABLE"));
|
|
await expect(createTeam(form({ rankName: "Moderator" }))).rejects.toThrow();
|
|
expect(revalidatePath).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("keeps already-gone team and word-filter deletes successful", async () => {
|
|
execute.mockResolvedValueOnce(failure("NOT_FOUND"));
|
|
await expect(deleteTeam(form({ id: "42" }))).resolves.toBeUndefined();
|
|
|
|
execute.mockResolvedValueOnce(failure("NOT_FOUND"));
|
|
await expect(deleteWord({ id: "42" })).resolves.toEqual({
|
|
ok: true,
|
|
data: {},
|
|
});
|
|
expect(revalidatePath).toHaveBeenCalledWith("/ase/people/staff/teams");
|
|
expect(revalidatePath).toHaveBeenCalledWith(
|
|
"/ase/people/moderation/word-filter",
|
|
);
|
|
});
|
|
});
|