import { beforeEach, expect, it, vi } from "vitest"; import { requirePermission } from "@/lib/admin/guard"; const { execute } = vi.hoisted(() => ({ execute: vi.fn() })); 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() })); vi.mock("@/lib/permissions", () => ({ PERMS: { USERS_EDIT: "admin.users.edit" }, })); vi.mock("@/lib/db", () => ({ db: {}, User: {}, UsersCurrency: {} })); vi.mock("@/lib/services/staff-activity", () => ({ logStaffActivity: vi.fn() })); import { bulkAdjustCurrency } from "./bulk-users"; beforeEach(() => { vi.clearAllMocks(); vi.mocked(requirePermission).mockResolvedValue({ id: 1, rank: 7, username: "admin", } as never); execute.mockResolvedValue({ ok: true, data: { before: { userIds: [7, 8] }, after: { completed: 2, total: 2, failedIds: [] }, }, correlationId: "bulk-adjust", }); }); it("keeps one ACL check while delegating a positive bulk adjustment", async () => { await expect( bulkAdjustCurrency({ userIds: [7, 8], amount: 25, type: "credits", }), ).resolves.toEqual({ ok: true, data: { adjusted: 2, total: 2, failedIds: [] }, }); expect(requirePermission).toHaveBeenCalledTimes(1); expect(execute).toHaveBeenCalledWith( expect.anything(), "users.bulk-currency", { userIds: [7, 8], amount: 25, type: "credits" }, ); }); it("keeps the pre-Task12 positive-adjust result when currency RCON throws after the database commit", async () => { execute.mockResolvedValueOnce({ ok: true, data: { before: { completed: 0, total: 1, failedIds: [] }, after: { completed: 1, total: 1, failedIds: [], externalSyncFailures: [ { userId: 7, reason: "Database applied; emulator sync failed. Do not retry automatically.", }, ], }, completion: { status: "partial", external: "failed", audit: "persisted", }, }, correlationId: "legacy-positive-adjust", completion: { status: "partial", external: "failed", audit: "persisted", }, }); await expect( bulkAdjustCurrency({ userIds: [7], amount: 25, type: "credits" }), ).resolves.toEqual({ ok: true, data: { adjusted: 0, total: 1, failedIds: [{ userId: 7, reason: "Database error" }], }, }); });