"""Content verification tests for bundled marketplace .md files.

These tests verify that specific strings are present in the bundled
marketplace files. They will FAIL before the .md edits are made (Step 1 Red).
"""

from __future__ import annotations

from pathlib import Path

import pytest

# Path to bundled marketplace files
_AGENTS_DIR = Path(__file__).parent.parent / "src" / "ievo" / "marketplace" / "agents"
_TEMPLATES_DIR = Path(__file__).parent.parent / "src" / "ievo" / "marketplace" / "templates"

# All 13 bundled agent filenames
_ALL_AGENTS = [
    "acceptance.md",
    "architect.md",
    "code-reviewer.md",
    "coder.md",
    "debugger.md",
    "defrag.md",
    "docs.md",
    "evolution.md",
    "hr.md",
    "qa.md",
    "researcher.md",
    "spec-writer.md",
    "tech-lead.md",
]


def test_evolution_agent_has_kernel_target_classification() -> None:
    """evolution.md contains .ievo/evolution/KERNEL.md as a log target path."""
    content = (_AGENTS_DIR / "evolution.md").read_text()
    assert ".ievo/evolution/KERNEL.md" in content, (
        "evolution.md must reference .ievo/evolution/KERNEL.md as a kernel-level log target"
    )


def test_evolution_agent_curator_issue_has_target_field() -> None:
    """evolution.md curator issue body contains **Target:** field (not **Target agent:**)."""
    content = (_AGENTS_DIR / "evolution.md").read_text()
    assert "**Target:**" in content, (
        "evolution.md curator issue must use **Target:** field (replacing **Target agent:**)"
    )
    # The old form should be gone
    assert "**Target agent:**" not in content, (
        "evolution.md must not use **Target agent:** — replace with **Target:**"
    )


def test_ievo_md_template_has_kernel_overlay_section() -> None:
    """iEVO.md template contains 'Kernel overlay' subsection and .ievo/evolution/KERNEL.md."""
    content = (_TEMPLATES_DIR / "iEVO.md").read_text()
    assert "Kernel overlay" in content, (
        "IEVO.md template must have a 'Kernel overlay' subsection in Evolution Overlay section"
    )
    assert ".ievo/evolution/KERNEL.md" in content, (
        "IEVO.md template must reference .ievo/evolution/KERNEL.md in kernel overlay section"
    )


@pytest.mark.parametrize("agent_file", _ALL_AGENTS)
def test_all_agents_context_loading_includes_kernel_overlay(agent_file: str) -> None:
    """Each agent .md file contains .ievo/evolution/KERNEL.md in its Context Loading area."""
    content = (_AGENTS_DIR / agent_file).read_text()
    assert ".ievo/evolution/KERNEL.md" in content, (
        f"{agent_file} must reference .ievo/evolution/KERNEL.md in Context Loading"
    )


def test_evolution_agent_step4_includes_kernel_target() -> None:
    """evolution.md Step 4 mentions writing to .ievo/evolution/KERNEL.md for kernel findings."""
    content = (_AGENTS_DIR / "evolution.md").read_text()
    # Step 4 must reference kernel-level target path
    assert ".ievo/evolution/KERNEL.md" in content, (
        "evolution.md Step 4 must mention .ievo/evolution/KERNEL.md as a write target"
    )
    # More specifically, the kernel vs agent distinction should be present
    assert "kernel" in content.lower() or "Kernel" in content, (
        "evolution.md must distinguish between kernel-level and agent-level findings"
    )
