// 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("SubscriptionTypesClient", () => {
    test("attachSubscriptionTypeToContact (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { id: "37846", consent_type: "opt_in" };
        const rawResponseBody = {
            type: "subscription",
            id: "106",
            state: "live",
            default_translation: { name: "Newsletters", description: "Lorem ipsum dolor sit amet", locale: "en" },
            translations: [{ name: "Newsletters", description: "Lorem ipsum dolor sit amet", locale: "en" }],
            consent_type: "opt_in",
            content_types: ["sms_message"],
        };
        server
            .mockEndpoint()
            .post("/contacts/63a07ddf05a32042dffac965/subscriptions")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.unstable.subscriptionTypes.attachSubscriptionTypeToContact({
            contact_id: "63a07ddf05a32042dffac965",
            id: "37846",
            consent_type: "opt_in",
        });
        expect(response).toEqual({
            type: "subscription",
            id: "106",
            state: "live",
            default_translation: {
                name: "Newsletters",
                description: "Lorem ipsum dolor sit amet",
                locale: "en",
            },
            translations: [
                {
                    name: "Newsletters",
                    description: "Lorem ipsum dolor sit amet",
                    locale: "en",
                },
            ],
            consent_type: "opt_in",
            content_types: ["sms_message"],
        });
    });

    test("attachSubscriptionTypeToContact (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { id: "37846", consent_type: "opt_in" };
        const rawResponseBody = {
            type: "subscription",
            id: "106",
            state: "live",
            default_translation: { name: "Newsletters", description: "Lorem ipsum dolor sit amet", locale: "en" },
            translations: [{ name: "Newsletters", description: "Lorem ipsum dolor sit amet", locale: "en" }],
            consent_type: "opt_in",
            content_types: ["sms_message"],
        };
        server
            .mockEndpoint()
            .post("/contacts/63a07ddf05a32042dffac965/subscriptions")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.unstable.subscriptionTypes.attachSubscriptionTypeToContact({
            contact_id: "63a07ddf05a32042dffac965",
            id: "37846",
            consent_type: "opt_in",
        });
        expect(response).toEqual({
            type: "subscription",
            id: "106",
            state: "live",
            default_translation: {
                name: "Newsletters",
                description: "Lorem ipsum dolor sit amet",
                locale: "en",
            },
            translations: [
                {
                    name: "Newsletters",
                    description: "Lorem ipsum dolor sit amet",
                    locale: "en",
                },
            ],
            consent_type: "opt_in",
            content_types: ["sms_message"],
        });
    });

    test("attachSubscriptionTypeToContact (3)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { id: "invalid_id", consent_type: "opt_in" };
        const rawResponseBody = {
            type: "subscription",
            id: "106",
            state: "live",
            default_translation: { name: "Newsletters", description: "Lorem ipsum dolor sit amet", locale: "en" },
            translations: [{ name: "Newsletters", description: "Lorem ipsum dolor sit amet", locale: "en" }],
            consent_type: "opt_in",
            content_types: ["sms_message"],
        };
        server
            .mockEndpoint()
            .post("/contacts/63a07ddf05a32042dffac965/subscriptions")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.unstable.subscriptionTypes.attachSubscriptionTypeToContact({
            contact_id: "63a07ddf05a32042dffac965",
            id: "invalid_id",
            consent_type: "opt_in",
        });
        expect(response).toEqual({
            type: "subscription",
            id: "106",
            state: "live",
            default_translation: {
                name: "Newsletters",
                description: "Lorem ipsum dolor sit amet",
                locale: "en",
            },
            translations: [
                {
                    name: "Newsletters",
                    description: "Lorem ipsum dolor sit amet",
                    locale: "en",
                },
            ],
            consent_type: "opt_in",
            content_types: ["sms_message"],
        });
    });

    test("attachSubscriptionTypeToContact (4)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { id: "id", consent_type: "consent_type" };
        const rawResponseBody = { type: "type", errors: [{ code: "code" }, { code: "code" }] };
        server
            .mockEndpoint()
            .post("/contacts/contact_id/subscriptions")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.unstable.subscriptionTypes.attachSubscriptionTypeToContact({
                contact_id: "contact_id",
                id: "id",
                consent_type: "consent_type",
            });
        }).rejects.toThrow(Intercom.unstable.UnauthorizedError);
    });

    test("attachSubscriptionTypeToContact (5)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { id: "id", consent_type: "consent_type" };
        const rawResponseBody = { key: "value" };
        server
            .mockEndpoint()
            .post("/contacts/contact_id/subscriptions")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.unstable.subscriptionTypes.attachSubscriptionTypeToContact({
                contact_id: "contact_id",
                id: "id",
                consent_type: "consent_type",
            });
        }).rejects.toThrow(Intercom.unstable.NotFoundError);
    });

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

        const rawResponseBody = {
            type: "subscription",
            id: "122",
            state: "live",
            default_translation: { name: "Newsletters", description: "Lorem ipsum dolor sit amet", locale: "en" },
            translations: [{ name: "Newsletters", description: "Lorem ipsum dolor sit amet", locale: "en" }],
            consent_type: "opt_in",
            content_types: ["sms_message"],
        };
        server
            .mockEndpoint()
            .delete("/contacts/63a07ddf05a32042dffac965/subscriptions/37846")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.unstable.subscriptionTypes.detachSubscriptionTypeToContact({
            contact_id: "63a07ddf05a32042dffac965",
            id: "37846",
        });
        expect(response).toEqual({
            type: "subscription",
            id: "122",
            state: "live",
            default_translation: {
                name: "Newsletters",
                description: "Lorem ipsum dolor sit amet",
                locale: "en",
            },
            translations: [
                {
                    name: "Newsletters",
                    description: "Lorem ipsum dolor sit amet",
                    locale: "en",
                },
            ],
            consent_type: "opt_in",
            content_types: ["sms_message"],
        });
    });

    test("detachSubscriptionTypeToContact (2)", 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()
            .delete("/contacts/contact_id/subscriptions/id")
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.unstable.subscriptionTypes.detachSubscriptionTypeToContact({
                contact_id: "contact_id",
                id: "id",
            });
        }).rejects.toThrow(Intercom.unstable.UnauthorizedError);
    });

    test("detachSubscriptionTypeToContact (3)", 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()
            .delete("/contacts/contact_id/subscriptions/id")
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.unstable.subscriptionTypes.detachSubscriptionTypeToContact({
                contact_id: "contact_id",
                id: "id",
            });
        }).rejects.toThrow(Intercom.unstable.NotFoundError);
    });

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

        const rawResponseBody = {
            type: "list",
            data: [
                {
                    type: "subscription",
                    id: "135",
                    state: "live",
                    default_translation: {
                        name: "Newsletters",
                        description: "Lorem ipsum dolor sit amet",
                        locale: "en",
                    },
                    translations: [{ name: "Newsletters", description: "Lorem ipsum dolor sit amet", locale: "en" }],
                    consent_type: "opt_out",
                    content_types: ["email"],
                },
            ],
        };
        server
            .mockEndpoint()
            .get("/subscription_types")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.unstable.subscriptionTypes.listSubscriptionTypes();
        expect(response).toEqual({
            type: "list",
            data: [
                {
                    type: "subscription",
                    id: "135",
                    state: "live",
                    default_translation: {
                        name: "Newsletters",
                        description: "Lorem ipsum dolor sit amet",
                        locale: "en",
                    },
                    translations: [
                        {
                            name: "Newsletters",
                            description: "Lorem ipsum dolor sit amet",
                            locale: "en",
                        },
                    ],
                    consent_type: "opt_out",
                    content_types: ["email"],
                },
            ],
        });
    });

    test("listSubscriptionTypes (2)", 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("/subscription_types")
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.unstable.subscriptionTypes.listSubscriptionTypes();
        }).rejects.toThrow(Intercom.unstable.UnauthorizedError);
    });
});
