// 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("ItemsClient", () => {
    test("list (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: 10,
                total_pages: 1,
            },
            total_count: 2,
            data: [
                {
                    type: "news-item",
                    id: "141",
                    workspace_id: "t74hdn32",
                    title: "New feature: News Items",
                    body: "We are excited to announce the launch of News Items, a new content type in Intercom enabling you to announce product updates, company news, promotions, events and more with your customers.",
                    sender_id: 123,
                    state: "draft",
                    newsfeed_assignments: [{ newsfeed_id: 198313, published_at: 1674917488 }],
                    labels: ["Product Update"],
                    cover_image_url: "https://example.com/cover.jpg",
                    reactions: ["👍"],
                    deliver_silently: true,
                    created_at: 1610589632,
                    updated_at: 1610589632,
                },
                {
                    type: "news-item",
                    id: "141",
                    workspace_id: "t74hdn32",
                    title: "New feature: News Items",
                    body: "We are excited to announce the launch of News Items, a new content type in Intercom enabling you to announce product updates, company news, promotions, events and more with your customers.",
                    sender_id: 123,
                    state: "draft",
                    newsfeed_assignments: [{ newsfeed_id: 198313, published_at: 1674917488 }],
                    labels: ["Product Update"],
                    cover_image_url: "https://example.com/cover.jpg",
                    reactions: ["👍"],
                    deliver_silently: true,
                    created_at: 1610589632,
                    updated_at: 1610589632,
                },
            ],
        };
        server.mockEndpoint().get("/news/news_items").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.news.items.list();
        expect(response).toEqual({
            type: "list",
            pages: {
                type: "pages",
                page: 1,
                next: {
                    per_page: 2,
                    starting_after: "your-cursor-from-response",
                },
                per_page: 10,
                total_pages: 1,
            },
            total_count: 2,
            data: [
                {
                    type: "news-item",
                    id: "141",
                    workspace_id: "t74hdn32",
                    title: "New feature: News Items",
                    body: "We are excited to announce the launch of News Items, a new content type in Intercom enabling you to announce product updates, company news, promotions, events and more with your customers.",
                    sender_id: 123,
                    state: "draft",
                    newsfeed_assignments: [
                        {
                            newsfeed_id: 198313,
                            published_at: 1674917488,
                        },
                    ],
                    labels: ["Product Update"],
                    cover_image_url: "https://example.com/cover.jpg",
                    reactions: ["\uD83D\uDC4D"],
                    deliver_silently: true,
                    created_at: 1610589632,
                    updated_at: 1610589632,
                },
                {
                    type: "news-item",
                    id: "141",
                    workspace_id: "t74hdn32",
                    title: "New feature: News Items",
                    body: "We are excited to announce the launch of News Items, a new content type in Intercom enabling you to announce product updates, company news, promotions, events and more with your customers.",
                    sender_id: 123,
                    state: "draft",
                    newsfeed_assignments: [
                        {
                            newsfeed_id: 198313,
                            published_at: 1674917488,
                        },
                    ],
                    labels: ["Product Update"],
                    cover_image_url: "https://example.com/cover.jpg",
                    reactions: ["\uD83D\uDC4D"],
                    deliver_silently: true,
                    created_at: 1610589632,
                    updated_at: 1610589632,
                },
            ],
        });
    });

    test("list (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("/news/news_items").respondWith().statusCode(401).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.news.items.list();
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    test("create (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            title: "Halloween is here!",
            body: "<p>New costumes in store for this spooky season</p>",
            sender_id: 991267834,
            state: "live",
            deliver_silently: true,
            labels: ["Product", "Update", "New"],
            reactions: ["😆", "😅"],
            newsfeed_assignments: [{ newsfeed_id: 53, published_at: 1664638214 }],
        };
        const rawResponseBody = {
            type: "news-item",
            id: "33",
            workspace_id: "this_is_an_id534_that_should_be_at_least_",
            title: "Halloween is here!",
            body: "<p>New costumes in store for this spooky season</p>",
            sender_id: 991267834,
            state: "live",
            newsfeed_assignments: [{ newsfeed_id: 53, published_at: 1664638214 }],
            labels: ["New", "Product", "Update"],
            cover_image_url: "https://example.com/cover.jpg",
            reactions: ["😆", "😅"],
            deliver_silently: true,
            created_at: 1734537797,
            updated_at: 1734537797,
        };
        server
            .mockEndpoint()
            .post("/news/news_items")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.news.items.create({
            title: "Halloween is here!",
            body: "<p>New costumes in store for this spooky season</p>",
            sender_id: 991267834,
            state: "live",
            deliver_silently: true,
            labels: ["Product", "Update", "New"],
            reactions: ["\uD83D\uDE06", "\uD83D\uDE05"],
            newsfeed_assignments: [
                {
                    newsfeed_id: 53,
                    published_at: 1664638214,
                },
            ],
        });
        expect(response).toEqual({
            type: "news-item",
            id: "33",
            workspace_id: "this_is_an_id534_that_should_be_at_least_",
            title: "Halloween is here!",
            body: "<p>New costumes in store for this spooky season</p>",
            sender_id: 991267834,
            state: "live",
            newsfeed_assignments: [
                {
                    newsfeed_id: 53,
                    published_at: 1664638214,
                },
            ],
            labels: ["New", "Product", "Update"],
            cover_image_url: "https://example.com/cover.jpg",
            reactions: ["\uD83D\uDE06", "\uD83D\uDE05"],
            deliver_silently: true,
            created_at: 1734537797,
            updated_at: 1734537797,
        });
    });

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

        await expect(async () => {
            return await client.news.items.create({
                title: "title",
                sender_id: 1,
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

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

        const rawResponseBody = {
            type: "news-item",
            id: "34",
            workspace_id: "this_is_an_id538_that_should_be_at_least_",
            title: "We have news",
            body: "<p>Hello there,</p>",
            sender_id: 991267837,
            state: "live",
            newsfeed_assignments: [{ newsfeed_id: 55, published_at: 1734537800 }],
            labels: ["Product Update"],
            cover_image_url: "https://example.com/cover.jpg",
            reactions: ["👍", "👍", "👍", "👍"],
            deliver_silently: false,
            created_at: 1734537800,
            updated_at: 1734537800,
        };
        server.mockEndpoint().get("/news/news_items/1").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.news.items.find({
            news_item_id: 1,
        });
        expect(response).toEqual({
            type: "news-item",
            id: "34",
            workspace_id: "this_is_an_id538_that_should_be_at_least_",
            title: "We have news",
            body: "<p>Hello there,</p>",
            sender_id: 991267837,
            state: "live",
            newsfeed_assignments: [
                {
                    newsfeed_id: 55,
                    published_at: 1734537800,
                },
            ],
            labels: ["Product Update"],
            cover_image_url: "https://example.com/cover.jpg",
            reactions: ["\uD83D\uDC4D", "\uD83D\uDC4D", "\uD83D\uDC4D", "\uD83D\uDC4D"],
            deliver_silently: false,
            created_at: 1734537800,
            updated_at: 1734537800,
        });
    });

    test("find (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("/news/news_items/1").respondWith().statusCode(401).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.news.items.find({
                news_item_id: 1,
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

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

        await expect(async () => {
            return await client.news.items.find({
                news_item_id: 1,
            });
        }).rejects.toThrow(Intercom.NotFoundError);
    });

    test("update (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>",
            sender_id: 991267845,
            reactions: ["😝", "😂"],
        };
        const rawResponseBody = {
            type: "news-item",
            id: "37",
            workspace_id: "this_is_an_id544_that_should_be_at_least_",
            title: "Christmas is here!",
            body: "<p>New gifts in store for the jolly season</p>",
            sender_id: 991267845,
            state: "live",
            newsfeed_assignments: [{ newsfeed_id: 198313, published_at: 1674917488 }],
            labels: ["Product Update"],
            cover_image_url: "https://example.com/cover.jpg",
            reactions: ["😝", "😂"],
            deliver_silently: false,
            created_at: 1734537803,
            updated_at: 1734537804,
        };
        server
            .mockEndpoint()
            .put("/news/news_items/1")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.news.items.update({
            news_item_id: 1,
            body: {
                title: "Christmas is here!",
                body: "<p>New gifts in store for the jolly season</p>",
                sender_id: 991267845,
                reactions: ["\uD83D\uDE1D", "\uD83D\uDE02"],
            },
        });
        expect(response).toEqual({
            type: "news-item",
            id: "37",
            workspace_id: "this_is_an_id544_that_should_be_at_least_",
            title: "Christmas is here!",
            body: "<p>New gifts in store for the jolly season</p>",
            sender_id: 991267845,
            state: "live",
            newsfeed_assignments: [
                {
                    newsfeed_id: 198313,
                    published_at: 1674917488,
                },
            ],
            labels: ["Product Update"],
            cover_image_url: "https://example.com/cover.jpg",
            reactions: ["\uD83D\uDE1D", "\uD83D\uDE02"],
            deliver_silently: false,
            created_at: 1734537803,
            updated_at: 1734537804,
        });
    });

    test("update (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>",
            sender_id: 991267848,
            reactions: ["😝", "😂"],
        };
        const rawResponseBody = {
            type: "news-item",
            id: "37",
            workspace_id: "this_is_an_id544_that_should_be_at_least_",
            title: "Christmas is here!",
            body: "<p>New gifts in store for the jolly season</p>",
            sender_id: 991267845,
            state: "live",
            newsfeed_assignments: [{ newsfeed_id: 198313, published_at: 1674917488 }],
            labels: ["Product Update"],
            cover_image_url: "https://example.com/cover.jpg",
            reactions: ["😝", "😂"],
            deliver_silently: false,
            created_at: 1734537803,
            updated_at: 1734537804,
        };
        server
            .mockEndpoint()
            .put("/news/news_items/1")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.news.items.update({
            news_item_id: 1,
            body: {
                title: "Christmas is here!",
                body: "<p>New gifts in store for the jolly season</p>",
                sender_id: 991267848,
                reactions: ["\uD83D\uDE1D", "\uD83D\uDE02"],
            },
        });
        expect(response).toEqual({
            type: "news-item",
            id: "37",
            workspace_id: "this_is_an_id544_that_should_be_at_least_",
            title: "Christmas is here!",
            body: "<p>New gifts in store for the jolly season</p>",
            sender_id: 991267845,
            state: "live",
            newsfeed_assignments: [
                {
                    newsfeed_id: 198313,
                    published_at: 1674917488,
                },
            ],
            labels: ["Product Update"],
            cover_image_url: "https://example.com/cover.jpg",
            reactions: ["\uD83D\uDE1D", "\uD83D\uDE02"],
            deliver_silently: false,
            created_at: 1734537803,
            updated_at: 1734537804,
        });
    });

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

        await expect(async () => {
            return await client.news.items.update({
                news_item_id: 1,
                body: {
                    title: "title",
                    sender_id: 1,
                },
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

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

        await expect(async () => {
            return await client.news.items.update({
                news_item_id: 1,
                body: {
                    title: "title",
                    sender_id: 1,
                },
            });
        }).rejects.toThrow(Intercom.NotFoundError);
    });

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

        const rawResponseBody = { id: "40", object: "news-item", deleted: true };
        server
            .mockEndpoint()
            .delete("/news/news_items/1")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.news.items.delete({
            news_item_id: 1,
        });
        expect(response).toEqual({
            id: "40",
            object: "news-item",
            deleted: true,
        });
    });

    test("delete (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("/news/news_items/1")
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.news.items.delete({
                news_item_id: 1,
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

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

        await expect(async () => {
            return await client.news.items.delete({
                news_item_id: 1,
            });
        }).rejects.toThrow(Intercom.NotFoundError);
    });
});
