import subprocess
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
CLASSIFIER = ROOT / "post-start-smoke-process-classifier.awk"


def classify(sample: str) -> str:
    result = subprocess.run(
        ["awk", "-f", str(CLASSIFIER)],
        input=sample,
        text=True,
        capture_output=True,
        check=True,
    )
    return result.stdout


def test_ignores_gitea_git_log_with_playwright_path() -> None:
    sample = """\
PID PPID PGID STAT %CPU %MEM COMMAND COMMAND
3686370 1249344 3686370 R 48.0 0.0 git /usr/bin/git log -- .playwright-mcp docs
1249344 1249342 1249344 Ssl 60.6 1.1 gitea /usr/local/bin/gitea web
"""
    assert classify(sample) == ""


def test_detects_real_chromium_process() -> None:
    sample = """\
PID PPID PGID STAT %CPU %MEM COMMAND COMMAND
123 1 123 Sl 95.0 1.0 chromium /usr/bin/chromium --headless
"""
    output = classify(sample)
    assert "chromium" in output


def test_detects_stockplatform_smoke_process() -> None:
    sample = """\
PID PPID PGID STAT %CPU %MEM COMMAND COMMAND
456 1 456 Sl 88.0 0.8 node node scripts/ops/stockplatform-review-bulk-ux-smoke.mjs
"""
    output = classify(sample)
    assert "stockplatform-review-bulk-ux-smoke" in output
