import pytest

from server.api.remote_path_validation import (
    RemotePathError,
    assert_clipboard_transfer_safe,
    normalize_remote_abs_path,
)


def test_normalize_ok():
    assert normalize_remote_abs_path("/www/wwwroot/foo/") == "/www/wwwroot/foo"


def test_normalize_rejects_relative():
    with pytest.raises(RemotePathError):
        normalize_remote_abs_path("relative/path")


def test_normalize_rejects_dotdot():
    with pytest.raises(RemotePathError):
        normalize_remote_abs_path("/www/../etc/passwd")


def test_transfer_blocks_dest_inside_source():
    with pytest.raises(RemotePathError):
        assert_clipboard_transfer_safe(
            ["/www/wwwroot/project"],
            "/www/wwwroot/project/src",
        )


def test_transfer_allows_sibling_dirs():
    sources, dest = assert_clipboard_transfer_safe(
        ["/www/wwwroot/a/file.txt"],
        "/www/wwwroot/b",
    )
    assert sources == ["/www/wwwroot/a/file.txt"]
    assert dest == "/www/wwwroot/b"


def test_transfer_allows_parent_directory():
    """跨目录：粘贴到父级目录（从子目录剪切/复制到上级）"""
    sources, dest = assert_clipboard_transfer_safe(
        ["/www/wwwroot/project/src/main.py"],
        "/www/wwwroot/project",
    )
    assert dest == "/www/wwwroot/project"
    assert sources[0].endswith("main.py")
