"""Benchmarks for CI workflow YAML parsing and key lookup."""

from __future__ import annotations

from pathlib import Path

import yaml

CI_WORKFLOW_PATH = Path(".forgejo/workflows/ci.yml")


class CIYamlParseSuite:
    """Measure CI YAML parsing performance."""

    def setup(self) -> None:
        """Read the raw YAML text once for reuse."""
        if CI_WORKFLOW_PATH.exists():
            self.yaml_text = CI_WORKFLOW_PATH.read_text()
        else:
            self.yaml_text = ""

    def time_parse_ci_yaml(self) -> None:
        """Benchmark parsing the CI workflow YAML."""
        if self.yaml_text:
            yaml.safe_load(self.yaml_text)

    def time_extract_job_names(self) -> None:
        """Benchmark extracting job names from parsed YAML."""
        if self.yaml_text:
            data = yaml.safe_load(self.yaml_text)
            if data and "jobs" in data:
                list(data["jobs"].keys())

    def time_extract_nox_sessions(self) -> None:
        """Benchmark extracting nox session references from all steps."""
        if self.yaml_text:
            data = yaml.safe_load(self.yaml_text)
            if data and "jobs" in data:
                sessions: list[str] = []
                for job_data in data["jobs"].values():
                    for step in job_data.get("steps", []):
                        run_cmd = step.get("run", "")
                        if "nox -s" in run_cmd:
                            sessions.append(run_cmd.strip())
