# -*- coding: utf-8 -*-
"""
Property 2: Python 子项目配置完整性

对于任意 uv workspace 声明的 Python 子项目成员，该子项目目录下应存在
独立的 pyproject.toml 文件，且文件中包含 [project] 段落。

Validates: Requirements 3.2
"""
import os
import re

from hypothesis import given, settings
from hypothesis.strategies import sampled_from

# uv workspace 声明的 Python 子项目成员
WORKSPACE_MEMBERS = [
    "apps/etl/connectors/feiqiu",
    "apps/backend",
    "packages/shared",
]

MONOREPO_ROOT = r"C:\Project\NeoZQYY"


@settings(max_examples=100)
@given(member=sampled_from(WORKSPACE_MEMBERS))
def test_pyproject_toml_exists(member: str) -> None:
    """子项目目录下应存在 pyproject.toml 文件。"""
    path = os.path.join(MONOREPO_ROOT, member, "pyproject.toml")
    assert os.path.isfile(path), f"{member}/pyproject.toml 不存在"


@settings(max_examples=100)
@given(member=sampled_from(WORKSPACE_MEMBERS))
def test_pyproject_contains_project_section(member: str) -> None:
    """pyproject.toml 应包含 [project] 段落。"""
    path = os.path.join(MONOREPO_ROOT, member, "pyproject.toml")
    content = open(path, encoding="utf-8").read()
    assert re.search(r"^\[project\]", content, re.MULTILINE), (
        f"{member}/pyproject.toml 缺少 [project] 段落"
    )


@settings(max_examples=100)
@given(member=sampled_from(WORKSPACE_MEMBERS))
def test_pyproject_contains_name(member: str) -> None:
    """pyproject.toml 的 [project] 段落应包含 name 字段。"""
    path = os.path.join(MONOREPO_ROOT, member, "pyproject.toml")
    content = open(path, encoding="utf-8").read()
    assert re.search(r'^name\s*=\s*".+"', content, re.MULTILINE), (
        f"{member}/pyproject.toml 缺少 name 字段"
    )


@settings(max_examples=100)
@given(member=sampled_from(WORKSPACE_MEMBERS))
def test_pyproject_contains_version(member: str) -> None:
    """pyproject.toml 的 [project] 段落应包含 version 字段。"""
    path = os.path.join(MONOREPO_ROOT, member, "pyproject.toml")
    content = open(path, encoding="utf-8").read()
    assert re.search(r'^version\s*=\s*".+"', content, re.MULTILINE), (
        f"{member}/pyproject.toml 缺少 version 字段"
    )
