2026-07-04 03:10:33 +02:00
|
|
|
"""DB seed coverage — agent bootstrap."""
|
2026-05-05 05:50:01 +02:00
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-05-06 21:02:31 +02:00
|
|
|
from contextlib import asynccontextmanager
|
2026-06-14 13:43:46 +02:00
|
|
|
from typing import TYPE_CHECKING, Any
|
2026-05-06 21:02:31 +02:00
|
|
|
from unittest.mock import AsyncMock, patch
|
2026-05-05 05:50:01 +02:00
|
|
|
|
|
|
|
|
import pytest
|
2026-07-04 03:10:33 +02:00
|
|
|
from roboco.db.seed import bootstrap_database, create_agents
|
2026-05-05 05:50:01 +02:00
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2026-06-14 13:43:46 +02:00
|
|
|
from collections.abc import AsyncGenerator
|
|
|
|
|
|
2026-05-05 05:50:01 +02:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_create_agents_seeds_defaults(db_session: AsyncSession) -> None:
|
|
|
|
|
agent_ids = await create_agents(db_session)
|
|
|
|
|
assert len(agent_ids) > 0
|
|
|
|
|
# Agents include be-dev-1, be-qa, etc.
|
|
|
|
|
assert any("be-" in slug or "fe-" in slug for slug in agent_ids)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_create_agents_idempotent(db_session: AsyncSession) -> None:
|
|
|
|
|
first = await create_agents(db_session)
|
|
|
|
|
second = await create_agents(db_session)
|
|
|
|
|
for slug, aid in first.items():
|
|
|
|
|
assert second[slug] == aid
|
|
|
|
|
|
|
|
|
|
|
2026-05-06 21:02:31 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_bootstrap_database_invokes_full_pipeline() -> None:
|
2026-07-04 03:10:33 +02:00
|
|
|
"""bootstrap_database wires up init_db + agent seeding."""
|
2026-05-06 21:02:31 +02:00
|
|
|
fake_session = AsyncMock()
|
|
|
|
|
fake_session.commit = AsyncMock()
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager
|
2026-06-14 13:43:46 +02:00
|
|
|
async def _ctx() -> AsyncGenerator[Any]:
|
2026-05-06 21:02:31 +02:00
|
|
|
yield fake_session
|
|
|
|
|
|
|
|
|
|
with (
|
|
|
|
|
patch("roboco.db.seed.init_db", AsyncMock()) as mock_init,
|
|
|
|
|
patch("roboco.db.seed.get_db_context", _ctx),
|
|
|
|
|
patch(
|
|
|
|
|
"roboco.db.seed.create_agents",
|
|
|
|
|
AsyncMock(return_value={"be-dev-1": "id"}),
|
|
|
|
|
) as mock_ag,
|
|
|
|
|
):
|
|
|
|
|
await bootstrap_database()
|
|
|
|
|
mock_init.assert_awaited_once()
|
|
|
|
|
mock_ag.assert_awaited_once()
|
|
|
|
|
fake_session.commit.assert_awaited_once()
|