test: align remaining action tests with Drizzle mocks

EOF

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
SimoandCursor committed 2026-08-01 13:17:35 +02:00
1 parent ed9c23c702
commit 096f55b394
4 files changed
+156 -112

No files matched your search

File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+19 -21
View File
@@ -1,9 +1,12 @@
import { beforeEach, describe, expect, it, vi } from "vitest"; import { beforeEach, describe, expect, it, vi } from "vitest";
const create = vi.hoisted(() => vi.fn()); const insertValues = vi.hoisted(() => vi.fn());
vi.mock("@/lib/prisma", () => ({ vi.mock("@/lib/db", () => ({
prisma: { staffActivities: { create } }, db: {
insert: vi.fn(() => ({ values: insertValues })),
},
StaffActivities: {},
})); }));
vi.mock("@/env", () => ({ env: {} })); vi.mock("@/env", () => ({ env: {} }));
@@ -12,8 +15,7 @@ vi.mock("next/headers", () => ({
headers: () => headers: () =>
new Promise((resolve) => new Promise((resolve) =>
resolve({ resolve({
get: (key: string) => get: (key) => (key === "x-real-client-ip" ? "192.168.1.1" : null),
key === "x-real-client-ip" ? "192.168.1.1" : null,
}), }),
), ),
})); }));
@@ -22,43 +24,39 @@ import { logStaffActivity } from "./staff-activity";
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
insertValues.mockResolvedValue([{ insertId: 1 }]);
}); });
describe("logStaffActivity", () => { describe("logStaffActivity", () => {
it("creates a staff activity entry with ip", async () => { it("creates a staff activity entry with ip", async () => {
create.mockResolvedValue({ id: 1 });
await logStaffActivity({ await logStaffActivity({
staffId: 1, staffId: 1,
action: "test_action", action: "test_action",
description: "Test action performed", description: "Test action performed",
}); });
expect(create).toHaveBeenCalledWith( expect(insertValues).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
data: expect.objectContaining({ userId: BigInt(1),
userId: BigInt(1), action: "test_action",
action: "test_action", description: "Test action performed",
description: "Test action performed", ipAddress: "192.168.1.1",
ipAddress: "192.168.1.1",
}),
}), }),
); );
}); });
it("truncates action to 50 chars", async () => { it("truncates action to 50 chars", async () => {
create.mockResolvedValue({ id: 1 });
const longAction = "a".repeat(100); const longAction = "a".repeat(100);
await logStaffActivity({ await logStaffActivity({
staffId: 1, staffId: 1,
action: longAction, action: longAction,
description: "test", description: "test",
}); });
const call = create.mock.calls[0][0]; const call = insertValues.mock.calls[0][0];
expect(call.data.action.length).toBe(50); expect(call.action.length).toBe(50);
expect(call.data.action).toBe("a".repeat(50)); expect(call.action).toBe("a".repeat(50));
}); });
it("includes target type and target id when provided", async () => { it("includes target type and target id when provided", async () => {
create.mockResolvedValue({ id: 1 });
await logStaffActivity({ await logStaffActivity({
staffId: 1, staffId: 1,
action: "ban", action: "ban",
@@ -66,13 +64,13 @@ describe("logStaffActivity", () => {
targetType: "user", targetType: "user",
targetId: 42, targetId: 42,
}); });
const data = create.mock.calls[0][0].data; const data = insertValues.mock.calls[0][0];
expect(data.targetType).toBe("user"); expect(data.targetType).toBe("user");
expect(data.targetId).toBe(BigInt(42)); expect(data.targetId).toBe(BigInt(42));
}); });
it("does not throw on prisma error", async () => { it("does not throw on db error", async () => {
create.mockRejectedValue(new Error("DB down")); insertValues.mockRejectedValue(new Error("DB down"));
await expect( await expect(
logStaffActivity({ logStaffActivity({
staffId: 1, staffId: 1,