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


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

from workos.common.models import FeatureFlag, Flag, PaginationOrder
from workos._pagination import AsyncPage, SyncPage
from workos._errors import (
    AuthenticationError,
    BadRequestError,
    NotFoundError,
    RateLimitExceededError,
    ServerError,
    UnprocessableEntityError,
)


class TestFeatureFlags:
    def test_list_feature_flags(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("list_flag.json"),
        )
        page = workos.feature_flags.list_feature_flags()
        assert isinstance(page, SyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], Flag)

    def test_list_feature_flags_empty_page(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = workos.feature_flags.list_feature_flags()
        assert isinstance(page, SyncPage)
        assert page.data == []

    def test_list_feature_flags_encodes_query_params(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        workos.feature_flags.list_feature_flags(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
        )
        request = httpx_mock.get_request()
        assert request.url.params["limit"] == "10"
        assert request.url.params["before"] == "cursor before"
        assert request.url.params["after"] == "cursor/after"
        assert request.url.params["order"] == "value_order"

    def test_get_feature_flag(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("flag.json"),
        )
        result = workos.feature_flags.get_feature_flag("test_slug")
        assert isinstance(result, Flag)
        assert result.object == "feature_flag"
        assert result.id == "flag_01EHZNVPK3SFK441A1RGBFSHRT"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/feature-flags/test_slug")

    def test_disable_feature_flag(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("feature_flag.json"),
        )
        result = workos.feature_flags.disable_feature_flag("test_slug")
        assert isinstance(result, FeatureFlag)
        assert result.object == "feature_flag"
        assert result.id == "flag_01EHZNVPK3SFK441A1RGBFSHRT"
        request = httpx_mock.get_request()
        assert request.method == "PUT"
        assert request.url.path.endswith("/feature-flags/test_slug/disable")

    def test_enable_feature_flag(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("feature_flag.json"),
        )
        result = workos.feature_flags.enable_feature_flag("test_slug")
        assert isinstance(result, FeatureFlag)
        assert result.object == "feature_flag"
        assert result.id == "flag_01EHZNVPK3SFK441A1RGBFSHRT"
        request = httpx_mock.get_request()
        assert request.method == "PUT"
        assert request.url.path.endswith("/feature-flags/test_slug/enable")

    def test_add_flag_target(self, workos, httpx_mock):
        httpx_mock.add_response(json={})
        workos.feature_flags.add_flag_target("test_resourceId", "test_slug")
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith(
            "/feature-flags/test_slug/targets/test_resourceId"
        )

    def test_remove_flag_target(self, workos, httpx_mock):
        httpx_mock.add_response(status_code=204)
        result = workos.feature_flags.remove_flag_target("test_resourceId", "test_slug")
        assert result is None
        request = httpx_mock.get_request()
        assert request.method == "DELETE"
        assert request.url.path.endswith(
            "/feature-flags/test_slug/targets/test_resourceId"
        )

    def test_list_organization_feature_flags(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("list_flag.json"),
        )
        page = workos.feature_flags.list_organization_feature_flags(
            "test_organizationId"
        )
        assert isinstance(page, SyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], Flag)

    def test_list_organization_feature_flags_empty_page(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = workos.feature_flags.list_organization_feature_flags(
            "test_organizationId"
        )
        assert isinstance(page, SyncPage)
        assert page.data == []

    def test_list_organization_feature_flags_encodes_query_params(
        self, workos, httpx_mock
    ):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        workos.feature_flags.list_organization_feature_flags(
            "test_organizationId",
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
        )
        request = httpx_mock.get_request()
        assert request.url.params["limit"] == "10"
        assert request.url.params["before"] == "cursor before"
        assert request.url.params["after"] == "cursor/after"
        assert request.url.params["order"] == "value_order"

    def test_list_user_feature_flags(self, workos, httpx_mock):
        httpx_mock.add_response(
            json=load_fixture("list_flag.json"),
        )
        page = workos.feature_flags.list_user_feature_flags("test_userId")
        assert isinstance(page, SyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], Flag)

    def test_list_user_feature_flags_empty_page(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = workos.feature_flags.list_user_feature_flags("test_userId")
        assert isinstance(page, SyncPage)
        assert page.data == []

    def test_list_user_feature_flags_encodes_query_params(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        workos.feature_flags.list_user_feature_flags(
            "test_userId",
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
        )
        request = httpx_mock.get_request()
        assert request.url.params["limit"] == "10"
        assert request.url.params["before"] == "cursor before"
        assert request.url.params["after"] == "cursor/after"
        assert request.url.params["order"] == "value_order"

    def test_list_feature_flags_with_request_options(self, workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        workos.feature_flags.list_feature_flags(
            request_options={"extra_headers": {"X-Custom": "value"}}
        )
        request = httpx_mock.get_request()
        assert request.headers["X-Custom"] == "value"

    def test_list_feature_flags_unauthorized(self, workos, httpx_mock):
        httpx_mock.add_response(
            status_code=401,
            json={"message": "Unauthorized"},
        )
        with pytest.raises(AuthenticationError):
            workos.feature_flags.list_feature_flags()

    def test_list_feature_flags_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.feature_flags.list_feature_flags()
        finally:
            workos.close()

    def test_list_feature_flags_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.feature_flags.list_feature_flags()
        finally:
            workos.close()

    def test_list_feature_flags_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.feature_flags.list_feature_flags()
        finally:
            workos.close()

    def test_list_feature_flags_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.feature_flags.list_feature_flags()
        finally:
            workos.close()

    def test_list_feature_flags_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.feature_flags.list_feature_flags()
        finally:
            workos.close()


class TestAsyncFeatureFlags:
    @pytest.mark.asyncio
    async def test_list_feature_flags(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("list_flag.json"))
        page = await async_workos.feature_flags.list_feature_flags()
        assert isinstance(page, AsyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], Flag)

    @pytest.mark.asyncio
    async def test_list_feature_flags_empty_page(self, async_workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = await async_workos.feature_flags.list_feature_flags()
        assert isinstance(page, AsyncPage)
        assert page.data == []

    @pytest.mark.asyncio
    async def test_list_feature_flags_encodes_query_params(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        await async_workos.feature_flags.list_feature_flags(
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
        )
        request = httpx_mock.get_request()
        assert request.url.params["limit"] == "10"
        assert request.url.params["before"] == "cursor before"
        assert request.url.params["after"] == "cursor/after"
        assert request.url.params["order"] == "value_order"

    @pytest.mark.asyncio
    async def test_get_feature_flag(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("flag.json"))
        result = await async_workos.feature_flags.get_feature_flag("test_slug")
        assert isinstance(result, Flag)
        assert result.object == "feature_flag"
        assert result.id == "flag_01EHZNVPK3SFK441A1RGBFSHRT"
        request = httpx_mock.get_request()
        assert request.method == "GET"
        assert request.url.path.endswith("/feature-flags/test_slug")

    @pytest.mark.asyncio
    async def test_disable_feature_flag(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("feature_flag.json"))
        result = await async_workos.feature_flags.disable_feature_flag("test_slug")
        assert isinstance(result, FeatureFlag)
        assert result.object == "feature_flag"
        assert result.id == "flag_01EHZNVPK3SFK441A1RGBFSHRT"
        request = httpx_mock.get_request()
        assert request.method == "PUT"
        assert request.url.path.endswith("/feature-flags/test_slug/disable")

    @pytest.mark.asyncio
    async def test_enable_feature_flag(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("feature_flag.json"))
        result = await async_workos.feature_flags.enable_feature_flag("test_slug")
        assert isinstance(result, FeatureFlag)
        assert result.object == "feature_flag"
        assert result.id == "flag_01EHZNVPK3SFK441A1RGBFSHRT"
        request = httpx_mock.get_request()
        assert request.method == "PUT"
        assert request.url.path.endswith("/feature-flags/test_slug/enable")

    @pytest.mark.asyncio
    async def test_add_flag_target(self, async_workos, httpx_mock):
        httpx_mock.add_response(json={})
        await async_workos.feature_flags.add_flag_target("test_resourceId", "test_slug")
        request = httpx_mock.get_request()
        assert request.method == "POST"
        assert request.url.path.endswith(
            "/feature-flags/test_slug/targets/test_resourceId"
        )

    @pytest.mark.asyncio
    async def test_remove_flag_target(self, async_workos, httpx_mock):
        httpx_mock.add_response(status_code=204)
        result = await async_workos.feature_flags.remove_flag_target(
            "test_resourceId", "test_slug"
        )
        assert result is None
        request = httpx_mock.get_request()
        assert request.method == "DELETE"
        assert request.url.path.endswith(
            "/feature-flags/test_slug/targets/test_resourceId"
        )

    @pytest.mark.asyncio
    async def test_list_organization_feature_flags(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("list_flag.json"))
        page = await async_workos.feature_flags.list_organization_feature_flags(
            "test_organizationId"
        )
        assert isinstance(page, AsyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], Flag)

    @pytest.mark.asyncio
    async def test_list_organization_feature_flags_empty_page(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = await async_workos.feature_flags.list_organization_feature_flags(
            "test_organizationId"
        )
        assert isinstance(page, AsyncPage)
        assert page.data == []

    @pytest.mark.asyncio
    async def test_list_organization_feature_flags_encodes_query_params(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        await async_workos.feature_flags.list_organization_feature_flags(
            "test_organizationId",
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
        )
        request = httpx_mock.get_request()
        assert request.url.params["limit"] == "10"
        assert request.url.params["before"] == "cursor before"
        assert request.url.params["after"] == "cursor/after"
        assert request.url.params["order"] == "value_order"

    @pytest.mark.asyncio
    async def test_list_user_feature_flags(self, async_workos, httpx_mock):
        httpx_mock.add_response(json=load_fixture("list_flag.json"))
        page = await async_workos.feature_flags.list_user_feature_flags("test_userId")
        assert isinstance(page, AsyncPage)
        assert len(page.data) == 1
        assert isinstance(page.data[0], Flag)

    @pytest.mark.asyncio
    async def test_list_user_feature_flags_empty_page(self, async_workos, httpx_mock):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        page = await async_workos.feature_flags.list_user_feature_flags("test_userId")
        assert isinstance(page, AsyncPage)
        assert page.data == []

    @pytest.mark.asyncio
    async def test_list_user_feature_flags_encodes_query_params(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        await async_workos.feature_flags.list_user_feature_flags(
            "test_userId",
            limit=10,
            before="cursor before",
            after="cursor/after",
            order=PaginationOrder("value_order"),
        )
        request = httpx_mock.get_request()
        assert request.url.params["limit"] == "10"
        assert request.url.params["before"] == "cursor before"
        assert request.url.params["after"] == "cursor/after"
        assert request.url.params["order"] == "value_order"

    @pytest.mark.asyncio
    async def test_list_feature_flags_with_request_options(
        self, async_workos, httpx_mock
    ):
        httpx_mock.add_response(json={"data": [], "list_metadata": {}})
        await async_workos.feature_flags.list_feature_flags(
            request_options={"extra_headers": {"X-Custom": "value"}}
        )
        request = httpx_mock.get_request()
        assert request.headers["X-Custom"] == "value"

    @pytest.mark.asyncio
    async def test_list_feature_flags_unauthorized(self, async_workos, httpx_mock):
        httpx_mock.add_response(status_code=401, json={"message": "Unauthorized"})
        with pytest.raises(AuthenticationError):
            await async_workos.feature_flags.list_feature_flags()

    @pytest.mark.asyncio
    async def test_list_feature_flags_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.feature_flags.list_feature_flags()
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_list_feature_flags_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.feature_flags.list_feature_flags()
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_list_feature_flags_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.feature_flags.list_feature_flags()
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_list_feature_flags_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.feature_flags.list_feature_flags()
        finally:
            await workos.close()

    @pytest.mark.asyncio
    async def test_list_feature_flags_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.feature_flags.list_feature_flags()
        finally:
            await workos.close()
