"""Least-privileged, strict-host-key remote execution probe."""

from __future__ import annotations

import os

import pytest

from obliteratus.remote import RemoteConfig, RemoteRunner


pytestmark = pytest.mark.remote


def _required(name: str) -> str:
    value = os.environ.get(name)
    if not value:
        pytest.skip(f"set {name} and enable the remote conditional gate")
    return value


def test_remote_provider_connection_and_minimal_command():
    config = RemoteConfig(
        host=_required("OBLITERATUS_REMOTE_HOST"),
        user=_required("OBLITERATUS_REMOTE_USER"),
        port=int(os.environ.get("OBLITERATUS_REMOTE_PORT") or "22"),
        ssh_key=_required("OBLITERATUS_REMOTE_KEY"),
        known_hosts_file=_required("OBLITERATUS_REMOTE_KNOWN_HOSTS"),
    )
    assert config.user != "root", "the conditional provider must be least-privileged"
    runner = RemoteRunner(config, on_log=lambda _message: None)
    assert "StrictHostKeyChecking=yes" in runner._ssh_base_cmd()
    assert runner.check_connection()
    result = runner.run_ssh("python3 -c 'print(6 * 7)'", timeout=30)
    assert result.returncode == 0
    assert result.stdout.strip() == "42"
