"""apps.solutions 单元测试：页面树结构与列表上下文（与 apps.products/apps.cases 同构）。"""
import pytest

from apps.solutions.models import SolutionIndexPage, SolutionPage

pytestmark = pytest.mark.django_db


@pytest.fixture
def solution_index(home_page):
    index = SolutionIndexPage(title="解决方案", slug="solutions", intro="行业解决方案")
    home_page.add_child(instance=index)
    return index


def test_solution_page_can_be_created_under_index(solution_index):
    solution = SolutionPage(
        title="制造业解决方案",
        slug="manufacturing",
        industry="制造业",
        summary="面向制造业的数字化转型方案",
    )
    solution_index.add_child(instance=solution)

    assert SolutionPage.objects.live().descendant_of(solution_index).count() == 1


def test_solution_index_context_excludes_draft_solutions(solution_index):
    live_solution = SolutionPage(title="方案 A", slug="solution-a")
    solution_index.add_child(instance=live_solution)

    draft_solution = SolutionPage(title="方案 B", slug="solution-b", live=False)
    solution_index.add_child(instance=draft_solution)

    context = solution_index.get_context(request=None)
    titles = [s.title for s in context["solutions"]]

    assert "方案 A" in titles
    assert "方案 B" not in titles
