2026-06-21 03:18:00 +08:00
|
|
|
"""Shared test fixtures for BadNote tests."""
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import shutil
|
|
|
|
|
import sys
|
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
import pytest_asyncio
|
|
|
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
|
|
|
|
|
|
# Ensure the server package is importable
|
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
|
|
|
|
|
|
|
|
os.environ["BADNOTE_STORAGE_PATH"] = "/tmp/badnote_test_storage"
|
|
|
|
|
os.environ["BADNOTE_QUEUE_PATH"] = "/tmp/badnote_test_queue"
|
2026-08-05 19:04:48 +08:00
|
|
|
os.environ["BADNOTE_VAULT_PATH"] = "/tmp/badnote_test_vaults"
|
2026-06-21 03:18:00 +08:00
|
|
|
os.environ["BADNOTE_JWT_SECRET"] = "test-secret-key-for-testing-only"
|
|
|
|
|
|
|
|
|
|
from badnote_server.config import settings # noqa: E402
|
|
|
|
|
from badnote_server.main import app # noqa: E402
|
|
|
|
|
from badnote_server.database import get_db, close_db, init_db # noqa: E402
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture(autouse=True)
|
|
|
|
|
async def setup_db(tmp_path):
|
|
|
|
|
"""Fresh DB and clean queue for each test."""
|
|
|
|
|
db_path = str(tmp_path / "test.db")
|
|
|
|
|
settings.db_path = db_path
|
2026-08-05 19:04:48 +08:00
|
|
|
settings.vault_path = str(tmp_path / "vaults")
|
2026-06-21 03:18:00 +08:00
|
|
|
# Clean the queue directory before each test
|
|
|
|
|
queue_path = settings.queue_path
|
|
|
|
|
if os.path.exists(queue_path):
|
|
|
|
|
shutil.rmtree(queue_path)
|
2026-08-05 19:04:48 +08:00
|
|
|
if os.path.exists(settings.vault_path):
|
|
|
|
|
shutil.rmtree(settings.vault_path)
|
2026-06-21 03:18:00 +08:00
|
|
|
await init_db()
|
|
|
|
|
yield
|
|
|
|
|
await close_db()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
|
|
|
async def client():
|
|
|
|
|
"""Async test client for the FastAPI app."""
|
|
|
|
|
transport = ASGITransport(app=app)
|
|
|
|
|
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
|
|
|
|
yield ac
|