from __future__ import annotations

from unittest.mock import Mock, patch

from sqlalchemy.orm import Session

from supply_infra.db.repositories.demand_grade_plan_repo import (
    DemandGradePlanRepository,
)


def test_materialize_group_items_reuses_repository_session(monkeypatch) -> None:
    session = Mock(spec=Session)
    repository = DemandGradePlanRepository(session)
    monkeypatch.setattr(repository, "group_item_count", lambda _group_id: 0)

    with patch(
        "supply_infra.scheduler.plan_group_batch.resolve_demands_for_category_ids",
        return_value=[],
    ) as resolve:
        created = repository.materialize_group_items(
            17,
            "20260727",
            [316, 317],
        )

    assert created == 0
    resolve.assert_called_once_with(
        "20260727",
        [316, 317],
        session=session,
    )
