"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const runQuirrel_1 = require("../../api/test/runQuirrel");
const util_1 = require("./util");
const __1 = require("..");
const errorLogMock = jest.fn();
const logMock = jest.fn();
const receiverMock = jest.fn();
global.console = {
    ...global.console,
    error: errorLogMock,
    log: logMock,
};
describe("client logger", () => {
    let quirrelBaseUrl;
    let server;
    beforeAll(async () => {
        server = await (0, runQuirrel_1.run)("Mock");
        quirrelBaseUrl = (0, util_1.getAddress)(server.server);
    });
    afterAll(async () => {
        server.teardown();
    });
    beforeEach(() => {
        errorLogMock.mockReset();
        logMock.mockReset();
    });
    it("works without passing a custom logger function", async () => {
        const quirrel = new __1.QuirrelClient({
            route: "routeName",
            async handler(payload) {
                if (payload.result === "fail")
                    throw new Error("yey,errors");
            },
            config: {
                quirrelBaseUrl,
                applicationBaseUrl: "http://localhost",
            },
        });
        await quirrel.respondTo(JSON.stringify({ result: "success" }), {});
        expect(logMock).toHaveBeenCalledTimes(1);
        expect(logMock).toHaveBeenLastCalledWith("Received job to routeName", {
            result: "success",
        });
        expect(errorLogMock).toHaveBeenCalledTimes(0);
        await quirrel.respondTo(JSON.stringify({ result: "fail" }), {});
        expect(logMock).toHaveBeenCalledTimes(2);
        expect(logMock).toHaveBeenLastCalledWith("Received job to routeName", {
            result: "fail",
        });
        expect(errorLogMock).toHaveBeenCalledTimes(1);
        expect(errorLogMock).toHaveBeenLastCalledWith("Error in job at routeName", { result: "fail" }, new Error("yey,errors"));
    });
    it("allows passing a custom logger function", async () => {
        const receivedJob = jest.fn();
        const processingError = jest.fn();
        const quirrel = new __1.QuirrelClient({
            route: "",
            async handler(payload) {
                if (payload.result === "fail")
                    throw new Error("fail");
            },
            config: {
                quirrelBaseUrl,
                applicationBaseUrl: "http://localhost",
            },
            options: {
                logger: {
                    receivedJob,
                    processingError,
                },
            },
        });
        await quirrel.respondTo(JSON.stringify({ result: "success" }), {});
        expect(receivedJob).toBeCalledTimes(1);
        expect(processingError).toBeCalledTimes(0);
        expect(logMock).toHaveBeenCalledTimes(0);
        expect(errorLogMock).toHaveBeenCalledTimes(0);
        await quirrel.respondTo(JSON.stringify({ result: "fail" }), {});
        expect(receivedJob).toBeCalledTimes(2);
        expect(processingError).toBeCalledTimes(1);
        expect(logMock).toHaveBeenCalledTimes(0);
        expect(errorLogMock).toHaveBeenCalledTimes(0);
    });
});
//# sourceMappingURL=logger.test.js.map