"""Unit tests for ~ / $HOME expansion in container paths."""

import pytest

from src.services.config.config_profile_resolver import expand_container_path
from src.services.build.manifest_compiler import get_manifest_home_dir


class TestExpandContainerPath:
    """Tests for expand_container_path helper."""

    def test_tilde_slash_expands(self) -> None:
        """~/foo should expand to home_dir/foo."""
        assert (
            expand_container_path("~/workspace", "/home/user") == "/home/user/workspace"
        )

    def test_tilde_alone_expands(self) -> None:
        """~ should expand to home_dir."""
        assert expand_container_path("~", "/home/user") == "/home/user"

    def test_dollar_home_slash_expands(self) -> None:
        """$HOME/foo should expand to home_dir/foo."""
        assert (
            expand_container_path("$HOME/workspace", "/home/user")
            == "/home/user/workspace"
        )

    def test_dollar_home_alone_expands(self) -> None:
        """$HOME should expand to home_dir."""
        assert expand_container_path("$HOME", "/home/user") == "/home/user"

    def test_absolute_path_unchanged(self) -> None:
        """Absolute paths should not be modified."""
        assert expand_container_path("/app/workspace", "/home/user") == "/app/workspace"

    def test_relative_path_unchanged(self) -> None:
        """Relative paths should not be modified."""
        assert expand_container_path("workspace", "/home/user") == "workspace"

    def test_tilde_in_middle_unchanged(self) -> None:
        """~ in the middle of a path should not expand."""
        assert expand_container_path("/app/~user", "/home/user") == "/app/~user"

    def test_dollar_home_in_middle_unchanged(self) -> None:
        """$HOME in the middle of a path should not expand."""
        assert expand_container_path("/app/$HOMEuser", "/home/user") == "/app/$HOMEuser"

    def test_root_home(self) -> None:
        """Expansion works with /root as home."""
        assert expand_container_path("~/config", "/root") == "/root/config"


class TestGetManifestHomeDir:
    """Tests for get_manifest_home_dir helper."""

    def test_with_user_block(self) -> None:
        """Manifest with user block returns /home/{name}."""
        manifest = {"user": {"name": "developer", "uid": 1000, "gid": 1000}}
        assert get_manifest_home_dir(manifest) == "/home/developer"

    def test_without_user_block(self) -> None:
        """Manifest without user block returns /root."""
        manifest = {"base_image": "ubuntu:24.04"}
        assert get_manifest_home_dir(manifest) == "/root"

    def test_with_empty_user_name(self) -> None:
        """Manifest with empty user name returns /root."""
        manifest = {"user": {"name": "", "uid": 1000, "gid": 1000}}
        assert get_manifest_home_dir(manifest) == "/root"

    def test_with_none_user_name(self) -> None:
        """Manifest with None user name returns /root."""
        manifest = {"user": {"name": None, "uid": 1000, "gid": 1000}}
        assert get_manifest_home_dir(manifest) == "/root"

