"""Unit tests for async document operations."""

import httpx
import pytest
import respx

from pinecone._internal.http import AsyncHTTPClient, HTTPConfig
from pinecone.async_index.documents import AsyncDocuments


@pytest.mark.asyncio()
@respx.mock
async def test_async_upsert_success():
    """Test successful async document upsert."""
    respx.post("https://test.io/namespaces/my-ns/documents/upsert").mock(
        return_value=httpx.Response(200, json={"upserted_count": 2})
    )

    client = AsyncHTTPClient(api_key="test", config=HTTPConfig(http2=False))
    documents = AsyncDocuments(http=client, base_url="https://test.io")

    response = await documents.upsert(
        namespace="my-ns",
        documents=[
            {"_id": "doc1", "title": "Hello"},
            {"_id": "doc2", "title": "World"},
        ],
    )

    assert response.upserted_count == 2


@pytest.mark.asyncio()
@respx.mock
async def test_async_search_success():
    """Test successful async document search."""
    respx.post("https://test.io/namespaces/my-ns/documents/search").mock(
        return_value=httpx.Response(
            200,
            json={
                "documents": [{"_id": "doc1", "score": 0.95}],
                "usage": {"read_units": 5},
            },
        )
    )

    client = AsyncHTTPClient(api_key="test", config=HTTPConfig(http2=False))
    documents = AsyncDocuments(http=client, base_url="https://test.io")

    response = await documents.search(
        namespace="my-ns",
        top_k=10,
        score_by=[{"type": "text", "field": "title", "text_query": "hello"}],
    )

    assert len(response.documents) == 1
    assert response.documents[0].id == "doc1"
