// 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("InternalArticlesClient", () => {
    test("listInternalArticles (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = {
            type: "list",
            pages: {
                type: "pages",
                page: 1,
                next: { per_page: 2, starting_after: "your-cursor-from-response" },
                per_page: 25,
                total_pages: 1,
            },
            total_count: 1,
            data: [
                {
                    type: "internal_article",
                    id: "39",
                    title: "Thanks for everything",
                    body: "Body of the Article",
                    owner_id: 991266252,
                    author_id: 991266252,
                    created_at: 1672928359,
                    updated_at: 1672928610,
                    locale: "en",
                },
            ],
        };
        server.mockEndpoint().get("/internal_articles").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.internalArticles.listInternalArticles();
        expect(response).toEqual({
            type: "list",
            pages: {
                type: "pages",
                page: 1,
                next: {
                    per_page: 2,
                    starting_after: "your-cursor-from-response",
                },
                per_page: 25,
                total_pages: 1,
            },
            total_count: 1,
            data: [
                {
                    type: "internal_article",
                    id: "39",
                    title: "Thanks for everything",
                    body: "Body of the Article",
                    owner_id: 991266252,
                    author_id: 991266252,
                    created_at: 1672928359,
                    updated_at: 1672928610,
                    locale: "en",
                },
            ],
        });
    });

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

        await expect(async () => {
            return await client.internalArticles.listInternalArticles();
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    test("createInternalArticle (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            title: "Thanks for everything",
            body: "Body of the Article",
            author_id: 991266252,
            owner_id: 991266252,
        };
        const rawResponseBody = {
            type: "internal_article",
            id: "42",
            title: "Thanks for everything",
            body: "Body of the Article",
            owner_id: 991266252,
            author_id: 991266252,
            created_at: 1672928359,
            updated_at: 1672928610,
            locale: "en",
        };
        server
            .mockEndpoint()
            .post("/internal_articles")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.internalArticles.createInternalArticle({
            title: "Thanks for everything",
            body: "Body of the Article",
            author_id: 991266252,
            owner_id: 991266252,
        });
        expect(response).toEqual({
            type: "internal_article",
            id: "42",
            title: "Thanks for everything",
            body: "Body of the Article",
            owner_id: 991266252,
            author_id: 991266252,
            created_at: 1672928359,
            updated_at: 1672928610,
            locale: "en",
        });
    });

    test("createInternalArticle (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            title: "Thanks for everything",
            body: "Body of the Internal Article",
            author_id: 1295,
            owner_id: 1295,
        };
        const rawResponseBody = {
            type: "internal_article",
            id: "42",
            title: "Thanks for everything",
            body: "Body of the Article",
            owner_id: 991266252,
            author_id: 991266252,
            created_at: 1672928359,
            updated_at: 1672928610,
            locale: "en",
        };
        server
            .mockEndpoint()
            .post("/internal_articles")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.internalArticles.createInternalArticle({
            title: "Thanks for everything",
            body: "Body of the Internal Article",
            author_id: 1295,
            owner_id: 1295,
        });
        expect(response).toEqual({
            type: "internal_article",
            id: "42",
            title: "Thanks for everything",
            body: "Body of the Article",
            owner_id: 991266252,
            author_id: 991266252,
            created_at: 1672928359,
            updated_at: 1672928610,
            locale: "en",
        });
    });

    test("createInternalArticle (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()
            .post("/internal_articles")
            .respondWith()
            .statusCode(400)
            .jsonBody(rawResponseBody)
            .build();

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

    test("createInternalArticle (4)", 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("/internal_articles")
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

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

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

        const rawResponseBody = {
            type: "internal_article",
            id: "45",
            title: "title",
            body: "Body of the Article",
            owner_id: 991266252,
            author_id: 991266252,
            created_at: 1672928359,
            updated_at: 1672928610,
            locale: "en",
        };
        server
            .mockEndpoint()
            .get("/internal_articles/1")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.internalArticles.retrieveInternalArticle({
            internal_article_id: 1,
        });
        expect(response).toEqual({
            type: "internal_article",
            id: "45",
            title: "title",
            body: "Body of the Article",
            owner_id: 991266252,
            author_id: 991266252,
            created_at: 1672928359,
            updated_at: 1672928610,
            locale: "en",
        });
    });

    test("retrieveInternalArticle (2)", 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()
            .get("/internal_articles/1")
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.internalArticles.retrieveInternalArticle({
                internal_article_id: 1,
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    test("retrieveInternalArticle (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()
            .get("/internal_articles/1")
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.internalArticles.retrieveInternalArticle({
                internal_article_id: 1,
            });
        }).rejects.toThrow(Intercom.NotFoundError);
    });

    test("updateInternalArticle (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { title: "Christmas is here!", body: "<p>New gifts in store for the jolly season</p>" };
        const rawResponseBody = {
            type: "internal_article",
            id: "48",
            title: "title",
            body: "Body of the Article",
            owner_id: 991266252,
            author_id: 991266252,
            created_at: 1672928359,
            updated_at: 1672928610,
            locale: "en",
        };
        server
            .mockEndpoint()
            .put("/internal_articles/1")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.internalArticles.updateInternalArticle({
            internal_article_id: 1,
            title: "Christmas is here!",
            body: "<p>New gifts in store for the jolly season</p>",
        });
        expect(response).toEqual({
            type: "internal_article",
            id: "48",
            title: "title",
            body: "Body of the Article",
            owner_id: 991266252,
            author_id: 991266252,
            created_at: 1672928359,
            updated_at: 1672928610,
            locale: "en",
        });
    });

    test("updateInternalArticle (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { title: "Christmas is here!", body: "<p>New gifts in store for the jolly season</p>" };
        const rawResponseBody = {
            type: "internal_article",
            id: "48",
            title: "title",
            body: "Body of the Article",
            owner_id: 991266252,
            author_id: 991266252,
            created_at: 1672928359,
            updated_at: 1672928610,
            locale: "en",
        };
        server
            .mockEndpoint()
            .put("/internal_articles/1")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.internalArticles.updateInternalArticle({
            internal_article_id: 1,
            title: "Christmas is here!",
            body: "<p>New gifts in store for the jolly season</p>",
        });
        expect(response).toEqual({
            type: "internal_article",
            id: "48",
            title: "title",
            body: "Body of the Article",
            owner_id: 991266252,
            author_id: 991266252,
            created_at: 1672928359,
            updated_at: 1672928610,
            locale: "en",
        });
    });

    test("updateInternalArticle (3)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {};
        const rawResponseBody = { type: "error.list", errors: [{ code: "code" }, { code: "code" }] };
        server
            .mockEndpoint()
            .put("/internal_articles/1")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.internalArticles.updateInternalArticle({
                internal_article_id: 1,
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    test("updateInternalArticle (4)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {};
        const rawResponseBody = { key: "value" };
        server
            .mockEndpoint()
            .put("/internal_articles/1")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.internalArticles.updateInternalArticle({
                internal_article_id: 1,
            });
        }).rejects.toThrow(Intercom.NotFoundError);
    });

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

        const rawResponseBody = { id: "51", object: "internal_article", deleted: true };
        server
            .mockEndpoint()
            .delete("/internal_articles/1")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.internalArticles.deleteInternalArticle({
            internal_article_id: 1,
        });
        expect(response).toEqual({
            id: "51",
            object: "internal_article",
            deleted: true,
        });
    });

    test("deleteInternalArticle (2)", 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()
            .delete("/internal_articles/1")
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.internalArticles.deleteInternalArticle({
                internal_article_id: 1,
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    test("deleteInternalArticle (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("/internal_articles/1")
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.internalArticles.deleteInternalArticle({
                internal_article_id: 1,
            });
        }).rejects.toThrow(Intercom.NotFoundError);
    });

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

        const rawResponseBody = {
            type: "list",
            total_count: 1,
            data: {
                internal_articles: [
                    {
                        id: "55",
                        body: "Body of the Article",
                        owner_id: 991266252,
                        author_id: 991266252,
                        created_at: 1672928359,
                        updated_at: 1672928610,
                        locale: "en",
                    },
                ],
            },
            pages: {
                type: "pages",
                page: 1,
                next: { per_page: 2, starting_after: "your-cursor-from-response" },
                per_page: 10,
                total_pages: 1,
            },
        };
        server
            .mockEndpoint()
            .get("/internal_articles/search")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.internalArticles.searchInternalArticles({
            folder_id: "folder_id",
        });
        expect(response).toEqual({
            type: "list",
            total_count: 1,
            data: {
                internal_articles: [
                    {
                        id: "55",
                        body: "Body of the Article",
                        owner_id: 991266252,
                        author_id: 991266252,
                        created_at: 1672928359,
                        updated_at: 1672928610,
                        locale: "en",
                    },
                ],
            },
            pages: {
                type: "pages",
                page: 1,
                next: {
                    per_page: 2,
                    starting_after: "your-cursor-from-response",
                },
                per_page: 10,
                total_pages: 1,
            },
        });
    });

    test("searchInternalArticles (2)", 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()
            .get("/internal_articles/search")
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.internalArticles.searchInternalArticles();
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });
});
