// 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("createMessage (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            from: { type: "user", id: "6762f2341bb69f9f2193bc17" },
            body: "heyy",
            referer: "https://twitter.com/bob",
        };
        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.unstable.messages.createMessage({
            from: {
                type: "user",
                id: "6762f2341bb69f9f2193bc17",
            },
            body: "heyy",
            referer: "https://twitter.com/bob",
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        });
    });

    test("createMessage (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            from: { type: "lead", id: "6762f2371bb69f9f2193bc18" },
            body: "heyy",
            referer: "https://twitter.com/bob",
        };
        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.unstable.messages.createMessage({
            from: {
                type: "lead",
                id: "6762f2371bb69f9f2193bc18",
            },
            body: "heyy",
            referer: "https://twitter.com/bob",
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918397",
            created_at: 1734537783,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "614",
        });
    });

    test("createMessage (3)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            from: { type: "admin", id: "991267816" },
            to: [
                { type: "user", id: "6762f2391bb69f9f2193bc19" },
                { type: "lead", id: "6762f23c1bb69f9f2193bc1b" },
                { type: "user", id: "6762f23d1bb69f9f2193bc1c" },
            ],
            cc: [
                { type: "user", id: "6762f23e1bb69f9f2193bc1d" },
                { type: "user", id: "6762f23f1bb69f9f2193bc1e" },
            ],
            bcc: [{ type: "user", id: "6762f23e1bb69f9f2193bc2f" }],
            message_type: "conversation",
            body: "heyy",
        };
        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.unstable.messages.createMessage({
            from: {
                type: "admin",
                id: "991267816",
            },
            to: [
                {
                    type: "user",
                    id: "6762f2391bb69f9f2193bc19",
                },
                {
                    type: "lead",
                    id: "6762f23c1bb69f9f2193bc1b",
                },
                {
                    type: "user",
                    id: "6762f23d1bb69f9f2193bc1c",
                },
            ],
            cc: [
                {
                    type: "user",
                    id: "6762f23e1bb69f9f2193bc1d",
                },
                {
                    type: "user",
                    id: "6762f23f1bb69f9f2193bc1e",
                },
            ],
            bcc: [
                {
                    type: "user",
                    id: "6762f23e1bb69f9f2193bc2f",
                },
            ],
            message_type: "conversation",
            body: "heyy",
        });
        expect(response).toEqual({
            type: "admin_message",
            id: "19",
            created_at: 1734537786,
            subject: "heyy",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "64619700005570",
        });
    });

    test("createMessage (4)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            from: { type: "admin", id: "991267817" },
            to: { type: "user", id: "6762f23a1bb69f9f2193bc1a" },
            message_type: "sms",
            body: "heyy",
        };
        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.unstable.messages.createMessage({
            from: {
                type: "admin",
                id: "991267817",
            },
            to: {
                type: "user",
                id: "6762f23a1bb69f9f2193bc1a",
            },
            message_type: "sms",
            body: "heyy",
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        });
    });

    test("createMessage (5)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            from: { type: "admin", id: "991267817" },
            to: { phone: 5547999998888, name: "John Doe" },
            message_type: "whatsapp",
            components: [{ type: "BODY", parameters: [{ type: "text", text: "Username 123" }] }],
            template: "keep_live",
            locale: "en",
        };
        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.unstable.messages.createMessage({
            from: {
                type: "admin",
                id: "991267817",
            },
            to: {
                phone: 5547999998888,
                name: "John Doe",
            },
            message_type: "whatsapp",
            components: [
                {
                    type: "BODY",
                    parameters: [
                        {
                            type: "text",
                            text: "Username 123",
                        },
                    ],
                },
            ],
            template: "keep_live",
            locale: "en",
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        });
    });

    test("createMessage (6)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            from: { type: "admin", id: "991267818" },
            to: { type: "user", id: "6762f23b1bb69f9f2193bc1a" },
            message_type: "inapp",
            subject: "heyy",
        };
        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.unstable.messages.createMessage({
            from: {
                type: "admin",
                id: "991267818",
            },
            to: {
                type: "user",
                id: "6762f23b1bb69f9f2193bc1a",
            },
            message_type: "inapp",
            subject: "heyy",
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        });
    });

    test("createMessage (7)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            from: { type: "admin", id: "991267819" },
            to: { type: "user", user_id: "70" },
            message_type: "email",
            body: "hey there",
        };
        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.unstable.messages.createMessage({
            from: {
                type: "admin",
                id: "991267819",
            },
            to: {
                type: "user",
                user_id: "70",
            },
            message_type: "email",
            body: "hey there",
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        });
    });

    test("createMessage (8)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            from: { type: "admin", id: "991267820" },
            to: { type: "user", id: "6762f23d1bb69f9f2193bc1c" },
            message_type: "email",
            subject: "heyy",
        };
        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.unstable.messages.createMessage({
            from: {
                type: "admin",
                id: "991267820",
            },
            to: {
                type: "user",
                id: "6762f23d1bb69f9f2193bc1c",
            },
            message_type: "email",
            subject: "heyy",
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        });
    });

    test("createMessage (9)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            from: { type: "admin", id: "991267821" },
            to: { type: "user", id: "6762f23b1bb69f9f2193bc1d" },
            message_type: "sms",
            body: "heyy https://picsum.photos/200/300",
        };
        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.unstable.messages.createMessage({
            from: {
                type: "admin",
                id: "991267821",
            },
            to: {
                type: "user",
                id: "6762f23b1bb69f9f2193bc1d",
            },
            message_type: "sms",
            body: "heyy https://picsum.photos/200/300",
        });
        expect(response).toEqual({
            type: "user_message",
            id: "403918396",
            created_at: 1734537780,
            subject: "Greetings",
            body: "heyy",
            message_type: "inapp",
            conversation_id: "613",
        });
    });

    test("createMessage (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.unstable.messages.createMessage(undefined);
        }).rejects.toThrow(Intercom.unstable.BadRequestError);
    });

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

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

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

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

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

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

    test("createMessage (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.unstable.messages.createMessage(undefined);
        }).rejects.toThrow(Intercom.unstable.UnprocessableEntityError);
    });

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

        const rawResponseBody = {
            type: "list",
            ruleset_id: "ruleset_id",
            pages: { type: "pages", per_page: 50, total_pages: 3, next: { starting_after: "abc123" } },
            total_count: 125,
            events: [
                {
                    id: "event_1",
                    conversation_id: "conv_123",
                    status: "delivered",
                    type: "broadcast_outbound",
                    created_at: 1734537980,
                    updated_at: 1734538000,
                    whatsapp_message_id: "wamid_123",
                    template_name: "appointment_reminder",
                },
                {
                    id: "event_2",
                    conversation_id: "conv_456",
                    status: "sent",
                    type: "broadcast_outbound",
                    created_at: 1734537970,
                    updated_at: 1734538010,
                    whatsapp_message_id: "wamid_456",
                    template_name: "order_update",
                },
            ],
        };
        server.mockEndpoint().get("/messages/status").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.unstable.messages.getWhatsAppMessageStatus({
            ruleset_id: "ruleset_id",
            per_page: 1,
            starting_after: "starting_after",
        });
        expect(response).toEqual({
            type: "list",
            ruleset_id: "ruleset_id",
            pages: {
                type: "pages",
                per_page: 50,
                total_pages: 3,
                next: {
                    starting_after: "abc123",
                },
            },
            total_count: 125,
            events: [
                {
                    id: "event_1",
                    conversation_id: "conv_123",
                    status: "delivered",
                    type: "broadcast_outbound",
                    created_at: 1734537980,
                    updated_at: 1734538000,
                    whatsapp_message_id: "wamid_123",
                    template_name: "appointment_reminder",
                },
                {
                    id: "event_2",
                    conversation_id: "conv_456",
                    status: "sent",
                    type: "broadcast_outbound",
                    created_at: 1734537970,
                    updated_at: 1734538010,
                    whatsapp_message_id: "wamid_456",
                    template_name: "order_update",
                },
            ],
        });
    });

    test("getWhatsAppMessageStatus (2)", 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().get("/messages/status").respondWith().statusCode(400).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.unstable.messages.getWhatsAppMessageStatus({
                ruleset_id: "ruleset_id",
            });
        }).rejects.toThrow(Intercom.unstable.BadRequestError);
    });

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

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

        await expect(async () => {
            return await client.unstable.messages.getWhatsAppMessageStatus({
                ruleset_id: "ruleset_id",
            });
        }).rejects.toThrow(Intercom.unstable.UnauthorizedError);
    });

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

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

        await expect(async () => {
            return await client.unstable.messages.getWhatsAppMessageStatus({
                ruleset_id: "ruleset_id",
            });
        }).rejects.toThrow(Intercom.unstable.ForbiddenError);
    });

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

        const rawResponseBody = { type: "type", errors: [{ code: "code" }, { code: "code" }] };
        server.mockEndpoint().get("/messages/status").respondWith().statusCode(500).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.unstable.messages.getWhatsAppMessageStatus({
                ruleset_id: "ruleset_id",
            });
        }).rejects.toThrow(Intercom.unstable.InternalServerError);
    });
});
