"""apps.products 单元测试：页面树结构与列表上下文。"""
import pytest

from apps.products.models import ProductIndexPage, ProductPage

pytestmark = pytest.mark.django_db


@pytest.fixture
def product_index(home_page):
    index = ProductIndexPage(title="产品", slug="products", intro="我们的产品")
    home_page.add_child(instance=index)
    return index


def test_product_page_can_be_created_under_index(product_index):
    product = ProductPage(title="产品 A", slug="product-a", summary="简介 A")
    product_index.add_child(instance=product)

    assert ProductPage.objects.live().descendant_of(product_index).count() == 1


def test_product_index_context_lists_live_products_only(product_index):
    live_product = ProductPage(title="产品 A", slug="product-a")
    product_index.add_child(instance=live_product)

    draft_product = ProductPage(title="产品 B", slug="product-b", live=False)
    product_index.add_child(instance=draft_product)

    context = product_index.get_context(request=None)
    titles = [p.title for p in context["products"]]

    assert "产品 A" in titles
    assert "产品 B" not in titles
