# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

import os
from typing import Any, cast

import pytest

from telnyx import Telnyx, AsyncTelnyx
from tests.utils import assert_matches_type
from telnyx.types import Fax, FaxCreateResponse, FaxRetrieveResponse
from telnyx._utils import parse_datetime
from telnyx.pagination import SyncDefaultFlatPagination, AsyncDefaultFlatPagination

base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")


class TestFaxes:
    parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_method_create(self, client: Telnyx) -> None:
        fax = client.faxes.create(
            connection_id="234423",
            from_="+13125790015",
            to="+13127367276",
        )
        assert_matches_type(FaxCreateResponse, fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_method_create_with_all_params(self, client: Telnyx) -> None:
        fax = client.faxes.create(
            connection_id="234423",
            from_="+13125790015",
            to="+13127367276",
            black_threshold=1,
            client_state="aGF2ZSBhIG5pY2UgZGF5ID1d",
            from_display_name="Company Name",
            media_name="my_media_uploaded_to_media_storage_api",
            media_url="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
            monochrome=True,
            preview_format="pdf",
            quality="high",
            store_media=True,
            store_preview=True,
            t38_enabled=True,
            webhook_url="https://www.example.com/server-b/",
        )
        assert_matches_type(FaxCreateResponse, fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_raw_response_create(self, client: Telnyx) -> None:
        response = client.faxes.with_raw_response.create(
            connection_id="234423",
            from_="+13125790015",
            to="+13127367276",
        )

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        fax = response.parse()
        assert_matches_type(FaxCreateResponse, fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_streaming_response_create(self, client: Telnyx) -> None:
        with client.faxes.with_streaming_response.create(
            connection_id="234423",
            from_="+13125790015",
            to="+13127367276",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            fax = response.parse()
            assert_matches_type(FaxCreateResponse, fax, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_method_retrieve(self, client: Telnyx) -> None:
        fax = client.faxes.retrieve(
            "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
        )
        assert_matches_type(FaxRetrieveResponse, fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_raw_response_retrieve(self, client: Telnyx) -> None:
        response = client.faxes.with_raw_response.retrieve(
            "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
        )

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        fax = response.parse()
        assert_matches_type(FaxRetrieveResponse, fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_streaming_response_retrieve(self, client: Telnyx) -> None:
        with client.faxes.with_streaming_response.retrieve(
            "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            fax = response.parse()
            assert_matches_type(FaxRetrieveResponse, fax, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_path_params_retrieve(self, client: Telnyx) -> None:
        with pytest.raises(ValueError, match=r"Expected a non-empty value for `id` but received ''"):
            client.faxes.with_raw_response.retrieve(
                "",
            )

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_method_list(self, client: Telnyx) -> None:
        fax = client.faxes.list()
        assert_matches_type(SyncDefaultFlatPagination[Fax], fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_method_list_with_all_params(self, client: Telnyx) -> None:
        fax = client.faxes.list(
            filter={
                "created_at": {
                    "gt": parse_datetime("2020-02-02T22:25:27.521992Z"),
                    "gte": parse_datetime("2020-02-02T22:25:27.521992Z"),
                    "lt": parse_datetime("2020-02-02T22:25:27.521992Z"),
                    "lte": parse_datetime("2020-02-02T22:25:27.521992Z"),
                },
                "direction": {"eq": "inbound"},
                "from": {"eq": "+13127367276"},
                "to": {"eq": "+13127367276"},
            },
            page_number=0,
            page_size=0,
        )
        assert_matches_type(SyncDefaultFlatPagination[Fax], fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_raw_response_list(self, client: Telnyx) -> None:
        response = client.faxes.with_raw_response.list()

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        fax = response.parse()
        assert_matches_type(SyncDefaultFlatPagination[Fax], fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_streaming_response_list(self, client: Telnyx) -> None:
        with client.faxes.with_streaming_response.list() as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            fax = response.parse()
            assert_matches_type(SyncDefaultFlatPagination[Fax], fax, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_method_delete(self, client: Telnyx) -> None:
        fax = client.faxes.delete(
            "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
        )
        assert fax is None

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_raw_response_delete(self, client: Telnyx) -> None:
        response = client.faxes.with_raw_response.delete(
            "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
        )

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        fax = response.parse()
        assert fax is None

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_streaming_response_delete(self, client: Telnyx) -> None:
        with client.faxes.with_streaming_response.delete(
            "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            fax = response.parse()
            assert fax is None

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_path_params_delete(self, client: Telnyx) -> None:
        with pytest.raises(ValueError, match=r"Expected a non-empty value for `id` but received ''"):
            client.faxes.with_raw_response.delete(
                "",
            )


class TestAsyncFaxes:
    parametrize = pytest.mark.parametrize(
        "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
    )

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_method_create(self, async_client: AsyncTelnyx) -> None:
        fax = await async_client.faxes.create(
            connection_id="234423",
            from_="+13125790015",
            to="+13127367276",
        )
        assert_matches_type(FaxCreateResponse, fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_method_create_with_all_params(self, async_client: AsyncTelnyx) -> None:
        fax = await async_client.faxes.create(
            connection_id="234423",
            from_="+13125790015",
            to="+13127367276",
            black_threshold=1,
            client_state="aGF2ZSBhIG5pY2UgZGF5ID1d",
            from_display_name="Company Name",
            media_name="my_media_uploaded_to_media_storage_api",
            media_url="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
            monochrome=True,
            preview_format="pdf",
            quality="high",
            store_media=True,
            store_preview=True,
            t38_enabled=True,
            webhook_url="https://www.example.com/server-b/",
        )
        assert_matches_type(FaxCreateResponse, fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_raw_response_create(self, async_client: AsyncTelnyx) -> None:
        response = await async_client.faxes.with_raw_response.create(
            connection_id="234423",
            from_="+13125790015",
            to="+13127367276",
        )

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        fax = await response.parse()
        assert_matches_type(FaxCreateResponse, fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_streaming_response_create(self, async_client: AsyncTelnyx) -> None:
        async with async_client.faxes.with_streaming_response.create(
            connection_id="234423",
            from_="+13125790015",
            to="+13127367276",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            fax = await response.parse()
            assert_matches_type(FaxCreateResponse, fax, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_method_retrieve(self, async_client: AsyncTelnyx) -> None:
        fax = await async_client.faxes.retrieve(
            "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
        )
        assert_matches_type(FaxRetrieveResponse, fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_raw_response_retrieve(self, async_client: AsyncTelnyx) -> None:
        response = await async_client.faxes.with_raw_response.retrieve(
            "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
        )

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        fax = await response.parse()
        assert_matches_type(FaxRetrieveResponse, fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_streaming_response_retrieve(self, async_client: AsyncTelnyx) -> None:
        async with async_client.faxes.with_streaming_response.retrieve(
            "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            fax = await response.parse()
            assert_matches_type(FaxRetrieveResponse, fax, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_path_params_retrieve(self, async_client: AsyncTelnyx) -> None:
        with pytest.raises(ValueError, match=r"Expected a non-empty value for `id` but received ''"):
            await async_client.faxes.with_raw_response.retrieve(
                "",
            )

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_method_list(self, async_client: AsyncTelnyx) -> None:
        fax = await async_client.faxes.list()
        assert_matches_type(AsyncDefaultFlatPagination[Fax], fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_method_list_with_all_params(self, async_client: AsyncTelnyx) -> None:
        fax = await async_client.faxes.list(
            filter={
                "created_at": {
                    "gt": parse_datetime("2020-02-02T22:25:27.521992Z"),
                    "gte": parse_datetime("2020-02-02T22:25:27.521992Z"),
                    "lt": parse_datetime("2020-02-02T22:25:27.521992Z"),
                    "lte": parse_datetime("2020-02-02T22:25:27.521992Z"),
                },
                "direction": {"eq": "inbound"},
                "from": {"eq": "+13127367276"},
                "to": {"eq": "+13127367276"},
            },
            page_number=0,
            page_size=0,
        )
        assert_matches_type(AsyncDefaultFlatPagination[Fax], fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_raw_response_list(self, async_client: AsyncTelnyx) -> None:
        response = await async_client.faxes.with_raw_response.list()

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        fax = await response.parse()
        assert_matches_type(AsyncDefaultFlatPagination[Fax], fax, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_streaming_response_list(self, async_client: AsyncTelnyx) -> None:
        async with async_client.faxes.with_streaming_response.list() as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            fax = await response.parse()
            assert_matches_type(AsyncDefaultFlatPagination[Fax], fax, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_method_delete(self, async_client: AsyncTelnyx) -> None:
        fax = await async_client.faxes.delete(
            "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
        )
        assert fax is None

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_raw_response_delete(self, async_client: AsyncTelnyx) -> None:
        response = await async_client.faxes.with_raw_response.delete(
            "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
        )

        assert response.is_closed is True
        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
        fax = await response.parse()
        assert fax is None

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_streaming_response_delete(self, async_client: AsyncTelnyx) -> None:
        async with async_client.faxes.with_streaming_response.delete(
            "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            fax = await response.parse()
            assert fax is None

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_path_params_delete(self, async_client: AsyncTelnyx) -> None:
        with pytest.raises(ValueError, match=r"Expected a non-empty value for `id` but received ''"):
            await async_client.faxes.with_raw_response.delete(
                "",
            )
