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
+134 -90

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