import pytest
from sqlalchemy import text
from sqlalchemy.ext.asyncio import create_async_engine

import app.models  # noqa: F401
from app.database import Base


@pytest.mark.anyio
async def test_brain_tables_are_registered_in_metadata(tmp_path):
    db_path = tmp_path / 'test_brain_models.db'
    engine = create_async_engine(f"sqlite+aiosqlite:///{db_path}", future=True)

    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)

        result = await conn.execute(text("SELECT name FROM sqlite_master WHERE type='table'"))
        table_names = {row[0] for row in result.fetchall()}

    await engine.dispose()

    assert 'brain_events' in table_names
    assert 'brain_candidates' in table_names
    assert 'brain_memories' in table_names
    assert 'brain_tags' in table_names
    assert 'brain_event_tags' in table_names
    assert 'brain_memory_tags' in table_names
    assert 'brain_memory_sources' in table_names
