import os
import pytest
import asyncpg


@pytest.fixture
async def db_conn():
    """Async asyncpg connection wrapped in a transaction that rolls back after each test.

    Requires KB_TEST_DSN env var or a kb-postgres running on localhost:5433.
    Only used by tests marked @pytest.mark.integration.
    """
    dsn = os.environ.get("KB_TEST_DSN", "postgresql://kb:kb@localhost:5433/kb")
    conn = await asyncpg.connect(dsn)
    await conn.execute("BEGIN")
    yield conn
    await conn.execute("ROLLBACK")
    await conn.close()
