# tests/test_frontend_policy.py
from __future__ import annotations

from pathlib import Path

from oceansguard.frontend_policy import (
    frontend_scripts,
    has_frontend,
    node_satisfies_for_vite,
)


def _write(p: Path, s: str) -> None:
    p.parent.mkdir(parents=True, exist_ok=True)
    p.write_text(s, encoding="utf-8")


def test_node_satisfies_for_vite_rules():
    assert node_satisfies_for_vite("v22.12.0") is True
    assert node_satisfies_for_vite("v22.11.0") is False
    assert node_satisfies_for_vite("v20.19.0") is True
    assert node_satisfies_for_vite("v20.18.0") is False
    assert node_satisfies_for_vite("v23.0.0") is True


def test_frontend_scripts_detects_build(tmp_path: Path):
    repo = tmp_path / "repo"
    repo.mkdir()
    _write(
        repo / "frontend" / "package.json",
        """{
  "name":"frontend",
  "private": true,
  "scripts": { "build":"tsc -b && vite build", "lint":"eslint ." }
}
""",
    )
    assert has_frontend(repo) is True
    scripts = frontend_scripts(repo)
    assert "build" in scripts
    assert "lint" in scripts
