# 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 tests.utils import assert_matches_type
from nimbleway_webit import Nimbleway, AsyncNimbleway
from nimbleway_webit.types.v2 import (
    CrawlGetResponse,
    CrawlListResponse,
    CrawlCrawlResponse,
    CrawlCancelResponse,
)
from nimbleway_webit.pagination import SyncCrawlPagination, AsyncCrawlPagination

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


class TestCrawl:
    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_list(self, client: Nimbleway) -> None:
        crawl = client.v2.crawl.list()
        assert_matches_type(SyncCrawlPagination[CrawlListResponse], crawl, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_method_list_with_all_params(self, client: Nimbleway) -> None:
        crawl = client.v2.crawl.list(
            cursor="cursor",
            limit=10,
            status="queued",
        )
        assert_matches_type(SyncCrawlPagination[CrawlListResponse], crawl, path=["response"])

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

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

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

            crawl = response.parse()
            assert_matches_type(SyncCrawlPagination[CrawlListResponse], crawl, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_method_cancel(self, client: Nimbleway) -> None:
        crawl = client.v2.crawl.cancel(
            "123e4567-e89b-12d3-a456-426614174000",
        )
        assert_matches_type(CrawlCancelResponse, crawl, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_raw_response_cancel(self, client: Nimbleway) -> None:
        response = client.v2.crawl.with_raw_response.cancel(
            "123e4567-e89b-12d3-a456-426614174000",
        )

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

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_streaming_response_cancel(self, client: Nimbleway) -> None:
        with client.v2.crawl.with_streaming_response.cancel(
            "123e4567-e89b-12d3-a456-426614174000",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            crawl = response.parse()
            assert_matches_type(CrawlCancelResponse, crawl, path=["response"])

        assert cast(Any, response.is_closed) is True

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

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_method_crawl(self, client: Nimbleway) -> None:
        crawl = client.v2.crawl.crawl(
            url="url",
        )
        assert_matches_type(CrawlCrawlResponse, crawl, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_method_crawl_with_all_params(self, client: Nimbleway) -> None:
        crawl = client.v2.crawl.crawl(
            url="url",
            allow_external_links=False,
            allow_subdomains=False,
            callback={
                "url": "https://example.com",
                "events": ["started"],
                "headers": {"foo": "string"},
                "metadata": {"foo": "bar"},
            },
            crawl_entire_domain=False,
            exclude_paths=["/exclude-this-path", "/and-this-path"],
            extract_options={
                "browser": "chrome",
                "browser_actions": [
                    {"goto": "https://example.com/login"},
                    {"wait_for_element": "#login-form"},
                    {
                        "fill": {
                            "selector": "#username",
                            "value": "user@example.com",
                            "click_on_element": True,
                            "delay": 1000,
                            "mode": "type",
                            "mouse_movement_strategy": "linear",
                            "required": "true",
                            "scroll": True,
                            "skip": "true",
                            "timeout": 0,
                            "typing_interval": 1000,
                            "typing_strategy": "simple",
                            "visible": True,
                        }
                    },
                    {
                        "fill": {
                            "selector": "#password",
                            "value": "password123",
                            "click_on_element": True,
                            "delay": 1000,
                            "mode": "type",
                            "mouse_movement_strategy": "linear",
                            "required": "true",
                            "scroll": True,
                            "skip": "true",
                            "timeout": 0,
                            "typing_interval": 1000,
                            "typing_strategy": "simple",
                            "visible": True,
                        }
                    },
                    {"click": "#submit"},
                    {
                        "screenshot": {
                            "format": "png",
                            "full_page": True,
                            "quality": 0,
                            "required": "true",
                            "skip": "true",
                        }
                    },
                ],
                "city": "Los Angeles",
                "consent_header": True,
                "cookies": [
                    {
                        "creation": "creation",
                        "domain": "domain",
                        "expires": "expires",
                        "extensions": ["string"],
                        "host_only": True,
                        "http_only": True,
                        "last_accessed": "lastAccessed",
                        "max_age": "Infinity",
                        "name": "name",
                        "path": "path",
                        "path_is_default": True,
                        "same_site": "strict",
                        "secure": True,
                        "value": "value",
                    }
                ],
                "country": "US",
                "device": "desktop",
                "driver": "vx8",
                "expected_status_codes": [200, 201],
                "formats": ["html"],
                "headers": {
                    "User-Agent": "CustomBot/1.0",
                    "Accept-Language": "en-US",
                },
                "http2": True,
                "is_xhr": True,
                "locale": "en-US",
                "method": "GET",
                "network_capture": [
                    {
                        "method": "GET",
                        "resource_type": "document",
                        "status_code": 100,
                        "url": {
                            "value": "value",
                            "type": "exact",
                        },
                        "validation": True,
                        "wait_for_requests_count": 0,
                        "wait_for_requests_count_timeout": 1,
                    }
                ],
                "os": "windows",
                "parse": True,
                "parser": {"myParser": "bar"},
                "referrer_type": "random",
                "render": True,
                "request_timeout": 30000,
                "session": {
                    "id": "id",
                    "prefetch_userbrowser": True,
                    "retry": True,
                    "timeout": 1,
                },
                "skill": "dynamic-content",
                "state": "CA",
                "tag": "campaign-2024-q1",
                "url": "url",
            },
            ignore_query_parameters=False,
            include_paths=["/include-this-path", "/and-this-path"],
            limit=100,
            max_discovery_depth=3,
            name="The best crawl ever",
            sitemap="include",
        )
        assert_matches_type(CrawlCrawlResponse, crawl, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_raw_response_crawl(self, client: Nimbleway) -> None:
        response = client.v2.crawl.with_raw_response.crawl(
            url="url",
        )

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

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

            crawl = response.parse()
            assert_matches_type(CrawlCrawlResponse, crawl, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_method_get(self, client: Nimbleway) -> None:
        crawl = client.v2.crawl.get(
            "123e4567-e89b-12d3-a456-426614174000",
        )
        assert_matches_type(CrawlGetResponse, crawl, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_raw_response_get(self, client: Nimbleway) -> None:
        response = client.v2.crawl.with_raw_response.get(
            "123e4567-e89b-12d3-a456-426614174000",
        )

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

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    def test_streaming_response_get(self, client: Nimbleway) -> None:
        with client.v2.crawl.with_streaming_response.get(
            "123e4567-e89b-12d3-a456-426614174000",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            crawl = response.parse()
            assert_matches_type(CrawlGetResponse, crawl, path=["response"])

        assert cast(Any, response.is_closed) is True

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


class TestAsyncCrawl:
    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_list(self, async_client: AsyncNimbleway) -> None:
        crawl = await async_client.v2.crawl.list()
        assert_matches_type(AsyncCrawlPagination[CrawlListResponse], crawl, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_method_list_with_all_params(self, async_client: AsyncNimbleway) -> None:
        crawl = await async_client.v2.crawl.list(
            cursor="cursor",
            limit=10,
            status="queued",
        )
        assert_matches_type(AsyncCrawlPagination[CrawlListResponse], crawl, path=["response"])

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

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

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

            crawl = await response.parse()
            assert_matches_type(AsyncCrawlPagination[CrawlListResponse], crawl, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_method_cancel(self, async_client: AsyncNimbleway) -> None:
        crawl = await async_client.v2.crawl.cancel(
            "123e4567-e89b-12d3-a456-426614174000",
        )
        assert_matches_type(CrawlCancelResponse, crawl, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_raw_response_cancel(self, async_client: AsyncNimbleway) -> None:
        response = await async_client.v2.crawl.with_raw_response.cancel(
            "123e4567-e89b-12d3-a456-426614174000",
        )

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

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_streaming_response_cancel(self, async_client: AsyncNimbleway) -> None:
        async with async_client.v2.crawl.with_streaming_response.cancel(
            "123e4567-e89b-12d3-a456-426614174000",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            crawl = await response.parse()
            assert_matches_type(CrawlCancelResponse, crawl, 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_cancel(self, async_client: AsyncNimbleway) -> None:
        with pytest.raises(ValueError, match=r"Expected a non-empty value for `id` but received ''"):
            await async_client.v2.crawl.with_raw_response.cancel(
                "",
            )

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_method_crawl(self, async_client: AsyncNimbleway) -> None:
        crawl = await async_client.v2.crawl.crawl(
            url="url",
        )
        assert_matches_type(CrawlCrawlResponse, crawl, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_method_crawl_with_all_params(self, async_client: AsyncNimbleway) -> None:
        crawl = await async_client.v2.crawl.crawl(
            url="url",
            allow_external_links=False,
            allow_subdomains=False,
            callback={
                "url": "https://example.com",
                "events": ["started"],
                "headers": {"foo": "string"},
                "metadata": {"foo": "bar"},
            },
            crawl_entire_domain=False,
            exclude_paths=["/exclude-this-path", "/and-this-path"],
            extract_options={
                "browser": "chrome",
                "browser_actions": [
                    {"goto": "https://example.com/login"},
                    {"wait_for_element": "#login-form"},
                    {
                        "fill": {
                            "selector": "#username",
                            "value": "user@example.com",
                            "click_on_element": True,
                            "delay": 1000,
                            "mode": "type",
                            "mouse_movement_strategy": "linear",
                            "required": "true",
                            "scroll": True,
                            "skip": "true",
                            "timeout": 0,
                            "typing_interval": 1000,
                            "typing_strategy": "simple",
                            "visible": True,
                        }
                    },
                    {
                        "fill": {
                            "selector": "#password",
                            "value": "password123",
                            "click_on_element": True,
                            "delay": 1000,
                            "mode": "type",
                            "mouse_movement_strategy": "linear",
                            "required": "true",
                            "scroll": True,
                            "skip": "true",
                            "timeout": 0,
                            "typing_interval": 1000,
                            "typing_strategy": "simple",
                            "visible": True,
                        }
                    },
                    {"click": "#submit"},
                    {
                        "screenshot": {
                            "format": "png",
                            "full_page": True,
                            "quality": 0,
                            "required": "true",
                            "skip": "true",
                        }
                    },
                ],
                "city": "Los Angeles",
                "consent_header": True,
                "cookies": [
                    {
                        "creation": "creation",
                        "domain": "domain",
                        "expires": "expires",
                        "extensions": ["string"],
                        "host_only": True,
                        "http_only": True,
                        "last_accessed": "lastAccessed",
                        "max_age": "Infinity",
                        "name": "name",
                        "path": "path",
                        "path_is_default": True,
                        "same_site": "strict",
                        "secure": True,
                        "value": "value",
                    }
                ],
                "country": "US",
                "device": "desktop",
                "driver": "vx8",
                "expected_status_codes": [200, 201],
                "formats": ["html"],
                "headers": {
                    "User-Agent": "CustomBot/1.0",
                    "Accept-Language": "en-US",
                },
                "http2": True,
                "is_xhr": True,
                "locale": "en-US",
                "method": "GET",
                "network_capture": [
                    {
                        "method": "GET",
                        "resource_type": "document",
                        "status_code": 100,
                        "url": {
                            "value": "value",
                            "type": "exact",
                        },
                        "validation": True,
                        "wait_for_requests_count": 0,
                        "wait_for_requests_count_timeout": 1,
                    }
                ],
                "os": "windows",
                "parse": True,
                "parser": {"myParser": "bar"},
                "referrer_type": "random",
                "render": True,
                "request_timeout": 30000,
                "session": {
                    "id": "id",
                    "prefetch_userbrowser": True,
                    "retry": True,
                    "timeout": 1,
                },
                "skill": "dynamic-content",
                "state": "CA",
                "tag": "campaign-2024-q1",
                "url": "url",
            },
            ignore_query_parameters=False,
            include_paths=["/include-this-path", "/and-this-path"],
            limit=100,
            max_discovery_depth=3,
            name="The best crawl ever",
            sitemap="include",
        )
        assert_matches_type(CrawlCrawlResponse, crawl, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_raw_response_crawl(self, async_client: AsyncNimbleway) -> None:
        response = await async_client.v2.crawl.with_raw_response.crawl(
            url="url",
        )

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

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

            crawl = await response.parse()
            assert_matches_type(CrawlCrawlResponse, crawl, path=["response"])

        assert cast(Any, response.is_closed) is True

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_method_get(self, async_client: AsyncNimbleway) -> None:
        crawl = await async_client.v2.crawl.get(
            "123e4567-e89b-12d3-a456-426614174000",
        )
        assert_matches_type(CrawlGetResponse, crawl, path=["response"])

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_raw_response_get(self, async_client: AsyncNimbleway) -> None:
        response = await async_client.v2.crawl.with_raw_response.get(
            "123e4567-e89b-12d3-a456-426614174000",
        )

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

    @pytest.mark.skip(reason="Mock server tests are disabled")
    @parametrize
    async def test_streaming_response_get(self, async_client: AsyncNimbleway) -> None:
        async with async_client.v2.crawl.with_streaming_response.get(
            "123e4567-e89b-12d3-a456-426614174000",
        ) as response:
            assert not response.is_closed
            assert response.http_request.headers.get("X-Stainless-Lang") == "python"

            crawl = await response.parse()
            assert_matches_type(CrawlGetResponse, crawl, 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_get(self, async_client: AsyncNimbleway) -> None:
        with pytest.raises(ValueError, match=r"Expected a non-empty value for `id` but received ''"):
            await async_client.v2.crawl.with_raw_response.get(
                "",
            )
