import pytest
from brox.scan.normalize import is_scope_wider, normalize_scope


class TestNormalizeScope:
    def test_removes_leading_dot_slash(self):
        assert normalize_scope("./foo") == "foo"
        assert normalize_scope("./data/**") == "data/**"

    def test_adds_double_star_to_directories(self):
        assert normalize_scope("data") == "data/**"
        assert normalize_scope("/etc") == "/etc/**"

    def test_preserves_existing_wildcards(self):
        assert normalize_scope("data/**") == "data/**"
        assert normalize_scope("data/*") == "data/*"

    def test_root_wildcard_unchanged(self):
        assert normalize_scope("/**") == "/**"

    def test_file_patterns_unchanged(self):
        assert normalize_scope("**/*.pem") == "**/*.pem"
        assert normalize_scope("data/*.json") == "data/*.json"


class TestIsScopeWider:
    def test_parent_is_wider_than_child(self):
        assert is_scope_wider("/a/b/**", "/a/**") is True

    def test_child_is_not_wider_than_parent(self):
        assert is_scope_wider("/a/**", "/a/b/**") is False

    def test_same_scope_not_wider(self):
        assert is_scope_wider("/a/**", "/a/**") is False
        assert is_scope_wider("./foo", "./foo") is False

    def test_root_wildcard_is_widest(self):
        assert is_scope_wider("/etc/**", "/**") is True
        assert is_scope_wider("/a/b/c/**", "/**") is True
        assert is_scope_wider("anything/**", "/**") is True

    def test_root_wildcard_to_root_wildcard_not_wider(self):
        assert is_scope_wider("/**", "/**") is False

    def test_handles_normalized_paths(self):
        assert is_scope_wider("./data/**", "data/**") is False
        assert is_scope_wider("data/**", "./data/**") is False

    def test_normalized_parent_child(self):
        assert is_scope_wider("data/**", "data/sub/**") is False
        assert is_scope_wider("data/sub/**", "data/**") is True

    def test_etc_is_wider_than_etc_subdir(self):
        assert is_scope_wider("/etc/passwd/**", "/etc/**") is True

    def test_root_is_wider_than_etc(self):
        assert is_scope_wider("/etc/**", "/**") is True

    def test_ssh_dir_examples(self):
        assert is_scope_wider("~/.ssh/id_rsa/**", "~/.ssh/**") is True
        assert is_scope_wider("~/.ssh/**", "~/**") is True

    def test_unrelated_paths_not_wider(self):
        assert is_scope_wider("/etc/**", "/var/**") is False
        assert is_scope_wider("/data/**", "/logs/**") is False

    def test_single_star_vs_double_star(self):
        assert is_scope_wider("/data/*", "/data/**") is False

    def test_depth_comparison(self):
        assert is_scope_wider("/a/**", "/a/b/**") is False
        assert is_scope_wider("/a/**", "/a/b/c/**") is False
        assert is_scope_wider("/a/b/**", "/a/**") is True
        assert is_scope_wider("/a/b/c/**", "/a/**") is True

    def test_windows_paths_normalized(self):
        pass

    def test_glob_patterns(self):
        assert is_scope_wider("/etc/**/*.pem", "/etc/**") is False

    def test_real_world_scenario_data_to_root(self):
        assert is_scope_wider("./data/**", "/**") is True
        assert is_scope_wider("data/**", "/**") is True

    def test_real_world_scenario_data_to_etc(self):
        assert is_scope_wider("./data/**", "/etc/**") is False

    def test_real_world_scenario_read_to_etc(self):
        assert is_scope_wider("data/**", "/etc/**") is False

    def test_multi_level_hierarchy(self):
        assert is_scope_wider("/a/b/c/d/**", "/a/b/c/**") is False
        assert is_scope_wider("/a/b/c/**", "/a/b/**") is False
        assert is_scope_wider("/a/b/**", "/a/**") is False
        assert is_scope_wider("/a/**", "/**") is False

        assert is_scope_wider("/a/b/c/**", "/a/b/c/d/**") is True
        assert is_scope_wider("/a/b/**", "/a/b/c/**") is True
        assert is_scope_wider("/a/**", "/a/b/**") is True
        assert is_scope_wider("/**", "/a/**") is True


class TestScopeWideningIntegration:
    def test_capability_diff_scenario(self):
        base_scope = "./data/**"

        head_scope_1 = "/**"
        assert is_scope_wider(
            normalize_scope(base_scope), normalize_scope(head_scope_1)
        ) is True

        head_scope_2 = "./data/logs/**"
        assert is_scope_wider(
            normalize_scope(base_scope), normalize_scope(head_scope_2)
        ) is False

        head_scope_3 = "./data/**"
        assert is_scope_wider(
            normalize_scope(base_scope), normalize_scope(head_scope_3)
        ) is False


if __name__ == "__main__":
    pytest.main([__file__, "-v"])
