"""validate_workspace.py discovery tests."""

import os
import sys
from pathlib import Path

import validate_workspace as vw


def test_env_commands_override_project_discovery(monkeypatch, tmp_path):
    monkeypatch.setenv("HARNESS_VALIDATION_COMMANDS", "echo one\n\n echo two ")

    assert vw.discover_commands(tmp_path) == ["echo one", "echo two"]


def test_npm_commands_take_precedence_over_python_defaults(monkeypatch, tmp_path):
    monkeypatch.delenv("HARNESS_VALIDATION_COMMANDS", raising=False)
    (tmp_path / "package.json").write_text(
        '{"scripts": {"lint": "eslint .", "build": "vite build", "test": "vitest"}}',
        encoding="utf-8",
    )
    scripts = tmp_path / "scripts"
    scripts.mkdir()
    (scripts / "validate_workspace.py").write_text("print('ok')", encoding="utf-8")

    assert vw.discover_commands(tmp_path) == [
        "npm run lint",
        "npm run build",
        "npm run test",
    ]


def test_python_defaults_use_repo_local_conda_venv(monkeypatch, tmp_path):
    monkeypatch.delenv("HARNESS_VALIDATION_COMMANDS", raising=False)
    (tmp_path / "venv").mkdir()
    python = tmp_path / "venv" / "python.exe"
    python.write_text("", encoding="utf-8")
    scripts = tmp_path / "scripts"
    scripts.mkdir()
    (scripts / "execute.py").write_text("print('execute')", encoding="utf-8")
    (scripts / "validate_workspace.py").write_text("print('validate')", encoding="utf-8")
    (scripts / "test_execute.py").write_text("def test_ok(): assert True", encoding="utf-8")

    commands = vw.discover_commands(tmp_path)

    assert commands == [
        f'"{python}" -m py_compile scripts\\execute.py scripts\\validate_workspace.py',
        f'"{python}" -m pytest scripts\\test_execute.py',
    ]


def test_python_defaults_fall_back_to_current_interpreter(monkeypatch, tmp_path):
    monkeypatch.delenv("HARNESS_VALIDATION_COMMANDS", raising=False)
    scripts = tmp_path / "scripts"
    scripts.mkdir()
    (scripts / "validate_workspace.py").write_text("print('validate')", encoding="utf-8")

    commands = vw.discover_commands(tmp_path)

    assert commands == [
        f'"{Path(sys.executable)}" -m py_compile scripts\\validate_workspace.py',
    ]


def test_python_defaults_include_tests_directory(monkeypatch, tmp_path):
    monkeypatch.delenv("HARNESS_VALIDATION_COMMANDS", raising=False)
    scripts = tmp_path / "scripts"
    scripts.mkdir()
    (scripts / "validate_workspace.py").write_text("print('validate')", encoding="utf-8")
    tests = tmp_path / "tests"
    tests.mkdir()
    (tests / "test_placeholder.py").write_text("def test_ok(): assert True", encoding="utf-8")

    commands = vw.discover_commands(tmp_path)

    assert commands == [
        f'"{Path(sys.executable)}" -m py_compile scripts\\validate_workspace.py',
        f'"{Path(sys.executable)}" -m pytest tests',
    ]


def test_no_commands_without_known_project_files(monkeypatch, tmp_path):
    monkeypatch.delenv("HARNESS_VALIDATION_COMMANDS", raising=False)

    assert vw.discover_commands(tmp_path) == []


def test_run_command_executes_from_root(tmp_path):
    result = vw.run_command(f'"{Path(sys.executable)}" -c "import os; print(os.getcwd())"', tmp_path)

    assert result.returncode == 0
    assert os.fspath(tmp_path) in result.stdout.strip()
