import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { logToolInvocation } from "./audit.js";
describe("logToolInvocation", () => {
let consoleSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
});
afterEach(() => {
consoleSpy.mockRestore();
});
it("logs success invocation", () => {
logToolInvocation("get_aws_caller_identity", true);
expect(consoleSpy).toHaveBeenCalledTimes(1);
const call = consoleSpy.mock.calls[0][0];
expect(call).toContain("[MCP-AWS-AUDIT]");
expect(call).toContain("get_aws_caller_identity");
const parsed = JSON.parse(call.replace("[MCP-AWS-AUDIT] ", ""));
expect(parsed.tool).toBe("get_aws_caller_identity");
expect(parsed.success).toBe(true);
expect(parsed.ts).toBeDefined();
});
it("logs failed invocation", () => {
logToolInvocation("list_s3_buckets", false);
const call = consoleSpy.mock.calls[0][0];
const parsed = JSON.parse(call.replace("[MCP-AWS-AUDIT] ", ""));
expect(parsed.success).toBe(false);
expect(parsed.tool).toBe("list_s3_buckets");
});
});