from __future__ import annotations

from unittest.mock import MagicMock, patch

from media_library_viewer_api.clients.ssh import RemoteSSHClient


def test_connect_uses_existing_known_hosts_without_reprobing(tmp_path):
    known_hosts_path = tmp_path / "known_hosts"
    known_hosts_path.write_text(
        "example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID8tdNkXNS1EWbkjb2XxFHQXRrpYzl0MXaVLRmVrbmDZ\n"
    )

    ssh_client = MagicMock()
    ssh_client.connect.return_value = None

    with (
        patch("media_library_viewer_api.clients.ssh.paramiko.SSHClient", return_value=ssh_client),
        patch("media_library_viewer_api.clients.ssh.paramiko.RejectPolicy", return_value=object()),
    ):
        client = RemoteSSHClient(
            host="example.com",
            username="alex",
            known_hosts_path=str(known_hosts_path),
        )

        client.connect()

    ssh_client.load_system_host_keys.assert_called_once_with()
    ssh_client.load_host_keys.assert_called_once_with(str(known_hosts_path))
    ssh_client.set_missing_host_key_policy.assert_called_once()
    ssh_client.connect.assert_called_once()
