# This file is auto-generated by oagen. Do not edit.

import json

import pytest
from workos import WorkOSClient, AsyncWorkOSClient
from tests.generated_helpers import load_fixture

from workos.agents.models import (
    AgentAdminLinkClaimAttemptToExternalUserRequestUser,
    AgentCredentialValidation,
    AgentRegistration,
    ClaimViewResponse,
)
from workos.common.models import AgentAdminValidateCredentialRequestType
from workos._errors import (
    AuthenticationError,
    BadRequestError,
    NotFoundError,
    RateLimitExceededError,
    ServerError,
    UnprocessableEntityError,
)


class TestAgents:
    def test_update_attempts(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("claim_view_response.json"),
        )
        result = workos.agents.update_attempts(
            type="link_external_user",
            claim_attempt_token="test_claim_attempt_token",
            user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                load_fixture(
                    "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                )
            ),
        )
        assert isinstance(result, ClaimViewResponse)
        assert result.id == "agent_reg_01EHWNCE74X7JSDV0X3SZ3KJNY"
        assert result.status == "unverified"
        request = httpx_mock.get_request()
        assert request.method == "PATCH"
        assert request.url.path.endswith("/agents/claims/attempts")
        body = json.loads(request.content)
        assert body["type"] == "link_external_user"
        assert body["claim_attempt_token"] == "test_claim_attempt_token"
        assert "user" in body

    def test_create_validate(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("agent_credential_validation.json"),
        )
        result = workos.agents.create_validate(
            type=AgentAdminValidateCredentialRequestType("api_key"),
            credential="test_credential",
        )
        assert isinstance(result, AgentCredentialValidation)
        assert result.valid is True
        assert result.registration_id == "agent_reg_01EHWNCE74X7JSDV0X3SZ3KJNY"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/agents/credentials/validate")
        body = json.loads(request.content)
        assert body["type"] == AgentAdminValidateCredentialRequestType("api_key")
        assert body["credential"] == "test_credential"

    def test_get_registration(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("agent_registration.json"),
        )
        result = workos.agents.get_registration("test_id")
        assert isinstance(result, AgentRegistration)
        assert result.id == "agent_reg_01EHWNCE74X7JSDV0X3SZ3KJNY"
        assert result.organization_id == "org_01EHQMYV6MBK39QC5PZXHY59C3"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/agents/registrations/test_id")

    def test_update_attempts_with_request_options(self, workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("claim_view_response.json"))
        workos.agents.update_attempts(
            type="link_external_user",
            claim_attempt_token="test_claim_attempt_token",
            user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                load_fixture(
                    "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                )
            ),
            request_options={"extra_headers": {"X-Custom": "value"}},
        )
        request = httpx_mock.get_request()
        assert request.headers["X-Custom"] == "value"

    def test_update_attempts_unauthorized(self, workos, httpx_mock):
        httpx_mock.add_response(
            status_code=401,
            json={"message": "Unauthorized"},
        )
        with pytest.raises(AuthenticationError):
            workos.agents.update_attempts(
                type="link_external_user",
                claim_attempt_token="test_claim_attempt_token",
                user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                    load_fixture(
                        "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                    )
                ),
            )

    def test_update_attempts_not_found(self, httpx_mock):
        workos = WorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=404, json={"message": "Not found"})
            with pytest.raises(NotFoundError):
                workos.agents.update_attempts(
                    type="link_external_user",
                    claim_attempt_token="test_claim_attempt_token",
                    user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                        load_fixture(
                            "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                        )
                    ),
                )
        finally:
            workos.close()

    def test_update_attempts_rate_limited(self, httpx_mock):
        workos = WorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(
                status_code=429,
                headers={"Retry-After": "0"},
                json={"message": "Slow down"},
            )
            with pytest.raises(RateLimitExceededError):
                workos.agents.update_attempts(
                    type="link_external_user",
                    claim_attempt_token="test_claim_attempt_token",
                    user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                        load_fixture(
                            "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                        )
                    ),
                )
        finally:
            workos.close()

    def test_update_attempts_server_error(self, httpx_mock):
        workos = WorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=500, json={"message": "Server error"})
            with pytest.raises(ServerError):
                workos.agents.update_attempts(
                    type="link_external_user",
                    claim_attempt_token="test_claim_attempt_token",
                    user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                        load_fixture(
                            "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                        )
                    ),
                )
        finally:
            workos.close()

    def test_update_attempts_bad_request(self, httpx_mock):
        workos = WorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=400, json={"message": "Bad request"})
            with pytest.raises(BadRequestError):
                workos.agents.update_attempts(
                    type="link_external_user",
                    claim_attempt_token="test_claim_attempt_token",
                    user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                        load_fixture(
                            "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                        )
                    ),
                )
        finally:
            workos.close()

    def test_update_attempts_unprocessable(self, httpx_mock):
        workos = WorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=422, json={"message": "Unprocessable"})
            with pytest.raises(UnprocessableEntityError):
                workos.agents.update_attempts(
                    type="link_external_user",
                    claim_attempt_token="test_claim_attempt_token",
                    user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                        load_fixture(
                            "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                        )
                    ),
                )
        finally:
            workos.close()


class TestAsyncAgents:
    @pytest.mark.asyncio
    async def test_update_attempts(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("claim_view_response.json"))
        result = await async_workos.agents.update_attempts(
            type="link_external_user",
            claim_attempt_token="test_claim_attempt_token",
            user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                load_fixture(
                    "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                )
            ),
        )
        assert isinstance(result, ClaimViewResponse)
        assert result.id == "agent_reg_01EHWNCE74X7JSDV0X3SZ3KJNY"
        assert result.status == "unverified"
        request = httpx_mock.get_request()
        assert request.method == "PATCH"
        assert request.url.path.endswith("/agents/claims/attempts")

    @pytest.mark.asyncio
    async def test_create_validate(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("agent_credential_validation.json"))
        result = await async_workos.agents.create_validate(
            type=AgentAdminValidateCredentialRequestType("api_key"),
            credential="test_credential",
        )
        assert isinstance(result, AgentCredentialValidation)
        assert result.valid is True
        assert result.registration_id == "agent_reg_01EHWNCE74X7JSDV0X3SZ3KJNY"
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith("/agents/credentials/validate")

    @pytest.mark.asyncio
    async def test_get_registration(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("agent_registration.json"))
        result = await async_workos.agents.get_registration("test_id")
        assert isinstance(result, AgentRegistration)
        assert result.id == "agent_reg_01EHWNCE74X7JSDV0X3SZ3KJNY"
        assert result.organization_id == "org_01EHQMYV6MBK39QC5PZXHY59C3"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/agents/registrations/test_id")

    @pytest.mark.asyncio
    async def test_update_attempts_with_request_options(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("claim_view_response.json"))
        await async_workos.agents.update_attempts(
            type="link_external_user",
            claim_attempt_token="test_claim_attempt_token",
            user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                load_fixture(
                    "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                )
            ),
            request_options={"extra_headers": {"X-Custom": "value"}},
        )
        request = httpx_mock.get_request()
        assert request.headers["X-Custom"] == "value"

    @pytest.mark.asyncio
    async def test_update_attempts_unauthorized(self, async_workos, httpx_mock):
        httpx_mock.add_response(status_code=401, json={"message": "Unauthorized"})
        with pytest.raises(AuthenticationError):
            await async_workos.agents.update_attempts(
                type="link_external_user",
                claim_attempt_token="test_claim_attempt_token",
                user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                    load_fixture(
                        "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                    )
                ),
            )

    @pytest.mark.asyncio
    async def test_update_attempts_not_found(self, httpx_mock):
        workos = AsyncWorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=404, json={"message": "Not found"})
            with pytest.raises(NotFoundError):
                await workos.agents.update_attempts(
                    type="link_external_user",
                    claim_attempt_token="test_claim_attempt_token",
                    user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                        load_fixture(
                            "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                        )
                    ),
                )
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_update_attempts_rate_limited(self, httpx_mock):
        workos = AsyncWorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(
                status_code=429,
                headers={"Retry-After": "0"},
                json={"message": "Slow down"},
            )
            with pytest.raises(RateLimitExceededError):
                await workos.agents.update_attempts(
                    type="link_external_user",
                    claim_attempt_token="test_claim_attempt_token",
                    user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                        load_fixture(
                            "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                        )
                    ),
                )
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_update_attempts_server_error(self, httpx_mock):
        workos = AsyncWorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=500, json={"message": "Server error"})
            with pytest.raises(ServerError):
                await workos.agents.update_attempts(
                    type="link_external_user",
                    claim_attempt_token="test_claim_attempt_token",
                    user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                        load_fixture(
                            "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                        )
                    ),
                )
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_update_attempts_bad_request(self, httpx_mock):
        workos = AsyncWorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=400, json={"message": "Bad request"})
            with pytest.raises(BadRequestError):
                await workos.agents.update_attempts(
                    type="link_external_user",
                    claim_attempt_token="test_claim_attempt_token",
                    user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                        load_fixture(
                            "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                        )
                    ),
                )
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_update_attempts_unprocessable(self, httpx_mock):
        workos = AsyncWorkOSClient(
            api_key="sk_test_123", client_id="client_test", max_retries=0
        )
        try:
            httpx_mock.add_response(status_code=422, json={"message": "Unprocessable"})
            with pytest.raises(UnprocessableEntityError):
                await workos.agents.update_attempts(
                    type="link_external_user",
                    claim_attempt_token="test_claim_attempt_token",
                    user=AgentAdminLinkClaimAttemptToExternalUserRequestUser.from_dict(
                        load_fixture(
                            "agent_admin_link_claim_attempt_to_external_user_request_user.json"
                        )
                    ),
                )
        finally:
            await workos.close()
