"""Template plan visibility for get-by-ref (aligned with list_for_plan)."""
from unittest.mock import AsyncMock, MagicMock
from uuid import uuid4

import pytest

from src.domain.templates.service import TemplateService


@pytest.mark.asyncio
async def test_get_by_ref_for_plan_none_when_inactive():
    repo = AsyncMock()
    t = MagicMock()
    t.is_active = False
    repo.get_by_ref = AsyncMock(return_value=t)
    svc = TemplateService(repo)
    assert await svc.get_by_ref_for_plan("tpl1", "free") is None
    repo.plan_access_allows.assert_not_called()


@pytest.mark.asyncio
async def test_get_by_ref_for_plan_none_when_plan_denied():
    repo = AsyncMock()
    t = MagicMock()
    t.id = uuid4()
    t.is_active = True
    repo.get_by_ref = AsyncMock(return_value=t)
    repo.plan_access_allows = AsyncMock(return_value=False)
    svc = TemplateService(repo)
    assert await svc.get_by_ref_for_plan("tpl1", "free") is None
    repo.plan_access_allows.assert_awaited_once_with(t.id, "free")
