// This file was auto-generated by Fern from our API Definition.

import * as Intercom from "../../src/api/index";
import { IntercomClient } from "../../src/Client";
import { mockServerPool } from "../mock-server/MockServerPool";

describe("MessagesClient", () => {
    test("create (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            message_type: "email",
            subject: "Thanks for everything",
            body: "Hello there",
            template: "plain",
            from: { type: "admin", id: 394051 },
            to: { type: "user", id: "536e564f316c83104c000020" },
        };
        const rawResponseBody = {
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        };
        server
            .mockEndpoint()
            .post("/messages")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.messages.create({
            message_type: "email",
            subject: "Thanks for everything",
            body: "Hello there",
            template: "plain",
            from: {
                type: "admin",
                id: 394051,
            },
            to: {
                type: "user",
                id: "536e564f316c83104c000020",
            },
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        });
    });

    test("create (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            message_type: "email",
            subject: "Thanks for everything",
            body: "Hello there",
            template: "plain",
            from: { type: "admin", id: 394051 },
            to: { type: "user", id: "536e564f316c83104c000020" },
        };
        const rawResponseBody = {
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        };
        server
            .mockEndpoint()
            .post("/messages")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.messages.create({
            message_type: "email",
            subject: "Thanks for everything",
            body: "Hello there",
            template: "plain",
            from: {
                type: "admin",
                id: 394051,
            },
            to: {
                type: "user",
                id: "536e564f316c83104c000020",
            },
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        });
    });

    test("create (3)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            message_type: "email",
            subject: "Thanks for everything",
            body: "Hello there",
            template: "plain",
            from: { type: "admin", id: 394051 },
            to: { type: "user", id: "536e564f316c83104c000020" },
        };
        const rawResponseBody = {
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        };
        server
            .mockEndpoint()
            .post("/messages")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.messages.create({
            message_type: "email",
            subject: "Thanks for everything",
            body: "Hello there",
            template: "plain",
            from: {
                type: "admin",
                id: 394051,
            },
            to: {
                type: "user",
                id: "536e564f316c83104c000020",
            },
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        });
    });

    test("create (4)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            message_type: "inapp",
            subject: "heyy",
            body: "Hello there",
            template: "plain",
            from: { type: "admin", id: 394051 },
            to: { type: "user", id: "6762f23b1bb69f9f2193bc1a" },
            created_at: 1590000000,
            create_conversation_without_contact_reply: true,
        };
        const rawResponseBody = {
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        };
        server
            .mockEndpoint()
            .post("/messages")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.messages.create({
            message_type: "inapp",
            subject: "heyy",
            body: "Hello there",
            template: "plain",
            from: {
                type: "admin",
                id: 394051,
            },
            to: {
                type: "user",
                id: "6762f23b1bb69f9f2193bc1a",
            },
            created_at: 1590000000,
            create_conversation_without_contact_reply: true,
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        });
    });

    test("create (5)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            message_type: "email",
            subject: "Thanks for everything",
            body: "hey there",
            template: "plain",
            from: { type: "admin", id: 394051 },
            to: { type: "user", id: "536e564f316c83104c000020" },
            created_at: 1590000000,
            create_conversation_without_contact_reply: true,
        };
        const rawResponseBody = {
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        };
        server
            .mockEndpoint()
            .post("/messages")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.messages.create({
            message_type: "email",
            subject: "Thanks for everything",
            body: "hey there",
            template: "plain",
            from: {
                type: "admin",
                id: 394051,
            },
            to: {
                type: "user",
                id: "536e564f316c83104c000020",
            },
            created_at: 1590000000,
            create_conversation_without_contact_reply: true,
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        });
    });

    test("create (6)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            message_type: "email",
            subject: "heyy",
            body: "Hello there",
            template: "plain",
            from: { type: "admin", id: 394051 },
            to: { type: "user", id: "6762f23d1bb69f9f2193bc1c" },
            created_at: 1590000000,
            create_conversation_without_contact_reply: true,
        };
        const rawResponseBody = {
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        };
        server
            .mockEndpoint()
            .post("/messages")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.messages.create({
            message_type: "email",
            subject: "heyy",
            body: "Hello there",
            template: "plain",
            from: {
                type: "admin",
                id: 394051,
            },
            to: {
                type: "user",
                id: "6762f23d1bb69f9f2193bc1c",
            },
            created_at: 1590000000,
            create_conversation_without_contact_reply: true,
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        });
    });

    test("create (7)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            message_type: "email",
            subject: "Thanks for everything",
            body: "Hello there",
            template: "plain",
            from: { type: "admin", id: 394051 },
            to: { type: "user", id: "536e564f316c83104c000020" },
        };
        const rawResponseBody = {
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        };
        server
            .mockEndpoint()
            .post("/messages")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.messages.create({
            message_type: "email",
            subject: "Thanks for everything",
            body: "Hello there",
            template: "plain",
            from: {
                type: "admin",
                id: 394051,
            },
            to: {
                type: "user",
                id: "536e564f316c83104c000020",
            },
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        });
    });

    test("create (8)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            message_type: "email",
            subject: "Thanks for everything",
            body: "Hello there",
            template: "plain",
            from: { type: "admin", id: 394051 },
            to: { type: "user", id: "536e564f316c83104c000020" },
        };
        const rawResponseBody = {
            type: "user_message",
            id: "403918397",
            created_at: 1734537783,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "614",
        };
        server
            .mockEndpoint()
            .post("/messages")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.messages.create({
            message_type: "email",
            subject: "Thanks for everything",
            body: "Hello there",
            template: "plain",
            from: {
                type: "admin",
                id: 394051,
            },
            to: {
                type: "user",
                id: "536e564f316c83104c000020",
            },
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918397",
            created_at: 1734537783,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "614",
        });
    });

    test("create (9)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            message_type: "email",
            subject: "Thanks for everything",
            body: "Hello there",
            template: "plain",
            from: { type: "admin", id: 394051 },
            to: { type: "user", id: "536e564f316c83104c000020" },
        };
        const rawResponseBody = {
            type: "admin_message",
            id: "19",
            created_at: 1734537786,
            subject: "heyy",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "64619700005570",
        };
        server
            .mockEndpoint()
            .post("/messages")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.messages.create({
            message_type: "email",
            subject: "Thanks for everything",
            body: "Hello there",
            template: "plain",
            from: {
                type: "admin",
                id: 394051,
            },
            to: {
                type: "user",
                id: "536e564f316c83104c000020",
            },
        });
        expect(response).toEqual({
            type: "admin_message",
            id: "19",
            created_at: 1734537786,
            subject: "heyy",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "64619700005570",
        });
    });

    test("create (10)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = { key: "value" };
        server.mockEndpoint().post("/messages").respondWith().statusCode(400).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.messages.create(undefined);
        }).rejects.toThrow(Intercom.BadRequestError);
    });

    test("create (11)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = { type: "error.list", errors: [{ code: "code" }, { code: "code" }] };
        server.mockEndpoint().post("/messages").respondWith().statusCode(401).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.messages.create(undefined);
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    test("create (12)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = { type: "error.list", errors: [{ code: "code" }, { code: "code" }] };
        server.mockEndpoint().post("/messages").respondWith().statusCode(403).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.messages.create(undefined);
        }).rejects.toThrow(Intercom.ForbiddenError);
    });

    test("create (13)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = { key: "value" };
        server.mockEndpoint().post("/messages").respondWith().statusCode(422).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.messages.create(undefined);
        }).rejects.toThrow(Intercom.UnprocessableEntityError);
    });
});
