"""Tests for push/sync error message localization."""

from server.utils.sync_error_message import translate_sync_error_message


class TestTranslateSyncErrorMessage:
    def test_none_and_empty(self):
        assert translate_sync_error_message(None) is None
        assert translate_sync_error_message("") == ""
        assert translate_sync_error_message("   ") == "   "

    def test_already_chinese_unchanged(self):
        msg = "推送已取消"
        assert translate_sync_error_message(msg) == msg

    def test_permission_denied(self):
        assert "权限被拒绝" in translate_sync_error_message("Permission denied")

    def test_no_such_file(self):
        assert "文件或目录不存在" in translate_sync_error_message(
            "rsync: No such file or directory (2)"
        )

    def test_connection_refused(self):
        assert "连接被拒绝" in translate_sync_error_message(
            "ssh: connect to host 1.2.3.4 port 22: Connection refused"
        )

    def test_rsync_connection_closed(self):
        assert "连接意外关闭" in translate_sync_error_message(
            "rsync: connection unexpectedly closed (0 bytes received so far) [sender]"
        )

    def test_exit_code(self):
        out = translate_sync_error_message("Command failed (exit 23)")
        assert "退出码" in out

    def test_server_not_found(self):
        assert translate_sync_error_message("Server not found") == "服务器不存在"

    def test_host_key_not_trusted(self):
        msg = translate_sync_error_message(
            "Host key is not trusted for host 66.154.115.131"
        )
        assert "主机密钥未信任" in msg

    def test_permission_denied_for_user(self):
        msg = translate_sync_error_message(
            "Permission denied for user root on host 66.154.115.131"
        )
        assert "认证失败" in msg
