import { afterEach, describe, expect, it, vi } from "vitest"; import { createTenantSessionRecoveryCoordinator, tenantSessionRecovery, } from "@/lib/tenant-session-recovery"; import { auditApi } from "./audit"; afterEach(() => { vi.restoreAllMocks(); vi.unstubAllGlobals(); }); describe("auditApi.exportAgentActionsCsv", () => { it("initiates tenant-session recovery for a direct CSV export", async () => { const reload = vi.fn(); const recovery = createTenantSessionRecoveryCoordinator(reload); vi.spyOn(tenantSessionRecovery, "recoverIfNeeded").mockImplementation(recovery.recoverIfNeeded); vi.stubGlobal("fetch", vi.fn().mockResolvedValue( new Response(JSON.stringify({ error: "tenant_session_invalid" }), { status: 401, headers: { "Content-Type": "application/json" }, }), )); const request = auditApi.exportAgentActionsCsv("company-1"); await vi.waitFor(() => expect(reload).toHaveBeenCalledTimes(1)); let settled = false; void request.then( () => { settled = true; }, () => { settled = true; }, ); await Promise.resolve(); expect(settled).toBe(false); }); });