Skip to content

πŸ’« Toolkit to help you get started with SDD or any other process!

17.0 KB
Raw Blame
"""Unit tests for project catalog-config id derivation and url canonicalization."""
from __future__ import annotations

from pathlib import Path

import pytest

from specify_cli.bundler import BundlerError
from specify_cli.bundles import catalog_config as cc


def test_derive_id_incorporates_path_stem_for_same_host():
    # Two catalogs on the same host must not collide on the derived id.
    a = cc._derive_id("https://example.com/team-a.json")
    b = cc._derive_id("https://example.com/team-b.json")
    assert a == "example-com-team-a"
    assert b == "example-com-team-b"
    assert a != b


def test_derive_id_distinguishes_tlds():
    # Different TLDs sharing a second-level label must not collide.
    com = cc._derive_id("https://example.com/team-a.json")
    net = cc._derive_id("https://example.net/team-a.json")
    assert com == "example-com-team-a"
    assert net == "example-net-team-a"
    assert com != net


def test_derive_id_falls_back_to_host_when_no_path():
    assert cc._derive_id("https://example.com/") == "example-com"


def test_derive_id_for_local_path_uses_stem():
    assert cc._derive_id("./catalogs/my-catalog.json") == "my-catalog"


def test_canonicalize_makes_relative_local_path_absolute(tmp_path: Path, monkeypatch):
    monkeypatch.chdir(tmp_path)
    (tmp_path / "local.json").write_text("{}", encoding="utf-8")

    result = cc._canonicalize_url("local.json")

    assert Path(result).is_absolute()
    assert Path(result) == (tmp_path / "local.json").resolve()


def test_canonicalize_leaves_remote_urls_untouched():
    for url in (
        "https://example.com/c.json",
        "http://localhost:8080/c.json",
        "file:///tmp/c.json",
        "builtin://default",
    ):
        assert cc._canonicalize_url(url) == url


def test_add_source_persists_absolute_local_path(tmp_path: Path, monkeypatch):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    catalog = project / "sub" / "cat.json"
    catalog.parent.mkdir()
    catalog.write_text("{}", encoding="utf-8")

    monkeypatch.chdir(project)
    source, status = cc.add_source(
        project, "sub/cat.json", policy="install-allowed", priority=50
    )

    assert status == "added"
    assert Path(source.url).is_absolute()
    assert Path(source.url) == catalog.resolve()


def test_add_source_normalizes_existing_entry_for_idempotency(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    cc._write(
        project,
        [
            {
                "id": " example ",
                "url": " https://example.com/catalog.json ",
                "priority": "50",
                "install_policy": "install-allowed",
                "metadata": "preserved",
            }
        ],
    )
    original = cc._config_path(project).read_bytes()

    source, status = cc.add_source(
        project,
        "https://example.com/catalog.json",
        policy="install-allowed",
        priority=50,
        source_id="example",
    )

    assert status == "unchanged"
    assert source.id == "example"
    assert cc._config_path(project).read_bytes() == original


def test_add_source_rejects_partial_identity_matches(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    cc.add_source(
        project,
        "https://example.com/catalog.json",
        policy="install-allowed",
        priority=50,
        source_id="example",
    )

    with pytest.raises(BundlerError, match="already exists"):
        cc.add_source(
            project,
            "https://example.com/other.json",
            policy="install-allowed",
            priority=50,
            source_id="example",
        )
    with pytest.raises(BundlerError, match="already exists"):
        cc.add_source(
            project,
            "https://example.com/catalog.json",
            policy="install-allowed",
            priority=50,
            source_id="different",
        )


def test_add_source_uses_existing_id_when_id_is_omitted(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    first, _ = cc.add_source(
        project,
        "https://example.com/catalog.json",
        policy="install-allowed",
        priority=50,
        source_id="custom",
    )

    second, status = cc.add_source(
        project,
        "https://example.com/catalog.json",
        policy="install-allowed",
        priority=50,
    )

    assert status == "unchanged"
    assert second == first


def test_add_source_prefers_url_match_over_derived_id_collision(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    cc._write(
        project,
        [
            {
                "id": "example-com-target",
                "url": "https://other.example/catalog.json",
                "priority": 50,
                "install_policy": "install-allowed",
            },
            {
                "id": "custom",
                "url": "https://example.com/target.json",
                "priority": 50,
                "install_policy": "install-allowed",
            },
        ],
    )
    original = cc._config_path(project).read_bytes()

    source, status = cc.add_source(
        project,
        "https://example.com/target.json",
        policy="install-allowed",
        priority=50,
    )

    assert status == "unchanged"
    assert source.id == "custom"
    assert cc._config_path(project).read_bytes() == original


def test_remove_source_accepts_relative_local_path(tmp_path: Path, monkeypatch):
    """add_source stores a local path as an absolute url, so remove_source must
    accept the same relative path the caller added; otherwise `remove ./cat.json`
    cannot undo `add ./cat.json`."""
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    catalog = project / "sub" / "cat.json"
    catalog.parent.mkdir()
    catalog.write_text("{}", encoding="utf-8")
    monkeypatch.chdir(project)

    cc.add_source(project, "sub/cat.json", policy="install-allowed", priority=50)
    # Removing with the same relative path must succeed (stored absolute).
    removed = cc.remove_source(project, "sub/cat.json")
    assert removed == "sub/cat.json"
    # And it is actually gone now.
    with pytest.raises(BundlerError, match="No project-scoped catalog source"):
        cc.remove_source(project, "sub/cat.json")


def test_remove_deletes_a_project_source_overriding_a_builtin_id(tmp_path: Path):
    """The documented override must be undoable.

    `remove_source` refused any built-in id before looking at what the project
    config actually held β€” yet its own error tells the user to "add a same-id
    source to override it instead". Once they did, the same guard refused to
    delete that override, leaving no CLI path back short of hand-editing the
    config.
    """
    builtin_id = sorted(cc._BUILTIN_IDS)[0]
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)

    cc.add_source(
        project,
        "https://example.test/override.json",
        source_id=builtin_id,
        policy="install-allowed",
        priority=1,
    )
    assert [c["id"] for c in cc._read(project)] == [builtin_id]

    assert cc.remove_source(project, builtin_id) == builtin_id
    assert cc._read(project) == []


def test_remove_builtin_without_an_override_is_still_refused(tmp_path: Path):
    """Deleting the built-in default itself stays refused."""
    builtin_id = sorted(cc._BUILTIN_IDS)[0]
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)

    with pytest.raises(BundlerError, match="built-in default source"):
        cc.remove_source(project, builtin_id)


def test_remove_by_id_does_not_also_delete_canonical_url_match(tmp_path: Path, monkeypatch):
    """`remove <id>` must remove only the exact-id source, not also a different
    source whose url happens to equal the id's canonicalized path. (_canonicalize_url
    treats a bare id as a local path, so the canonical match is only a fallback when
    there is no exact id/url match.)"""
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    monkeypatch.chdir(project)
    # Source A: id "local", a remote url.
    cc.add_source(
        project, "https://example.com/a.json", source_id="local",
        policy="install-allowed", priority=10,
    )
    # Source B: a local path that canonicalizes to <cwd>/local, with a distinct id.
    cc.add_source(project, "local", source_id="bsource", policy="install-allowed", priority=20)

    removed = cc.remove_source(project, "local")
    assert removed == "local"
    ids = {c["id"] for c in cc._read(project)}
    assert "local" not in ids   # the exact-id source was removed
    assert "bsource" in ids     # the canonical-url source survives (not collateral)


def test_add_source_refuses_symlinked_specify_escape(tmp_path: Path):
    project = tmp_path / "proj"
    project.mkdir()
    outside = tmp_path / "outside"
    outside.mkdir()
    (project / ".specify").symlink_to(outside, target_is_directory=True)

    with pytest.raises(BundlerError, match="escapes the allowed root"):
        cc.add_source(project, "https://example.com/c.json", policy="install-allowed", priority=50)


def test_read_rejects_non_list_catalogs(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    cc._config_path(project).write_text(
        "schema_version: '1.0'\ncatalogs: not-a-list\n", encoding="utf-8"
    )

    with pytest.raises(BundlerError, match="'catalogs' must be a list"):
        cc._read(project)


def test_read_rejects_non_mapping_catalog_entry(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    cc._config_path(project).write_text(
        "schema_version: '1.0'\ncatalogs:\n  - just-a-string\n", encoding="utf-8"
    )

    with pytest.raises(BundlerError, match="each catalog entry must be a mapping"):
        cc._read(project)


def test_read_rejects_non_mapping_top_level(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    cc._config_path(project).write_text("- a\n- b\n", encoding="utf-8")

    with pytest.raises(BundlerError, match="expected a mapping at the top level"):
        cc._read(project)


@pytest.mark.parametrize("body", ["[]\n", "false\n", "0\n", "''\n", "null\n", "~\n"])
def test_read_rejects_falsy_non_mapping_top_level(tmp_path: Path, body: str):
    # A FALSY non-mapping top level ([], false, 0, '') OR an explicit null
    # (null/~) must raise like a truthy one. safe_load coerces these to
    # None/{}, so load_yaml distinguishes them from a truly empty document β€”
    # staying consistent with models/catalog._merge_config.
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    cc._config_path(project).write_text(body, encoding="utf-8")

    with pytest.raises(BundlerError, match="expected a mapping at the top level"):
        cc._read(project)


def test_read_rejects_unknown_schema_version(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    cc._config_path(project).write_text(
        "schema_version: '2.0'\ncatalogs: []\n", encoding="utf-8"
    )

    with pytest.raises(BundlerError, match="Unsupported catalog config schema version"):
        cc._read(project)


def test_read_accepts_forward_compatible_minor_schema(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    cc._config_path(project).write_text(
        "schema_version: '1.5'\ncatalogs: []\n", encoding="utf-8"
    )
    assert cc._read(project) == []


def test_read_tolerates_missing_schema_version(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    cc._config_path(project).write_text("catalogs: []\n", encoding="utf-8")
    assert cc._read(project) == []


def test_read_returns_empty_for_missing_or_empty_config(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    assert cc._read(project) == []

    cc._config_path(project).write_text("schema_version: '1.0'\n", encoding="utf-8")
    assert cc._read(project) == []


def test_slug_lowercases_for_deterministic_ids():
    # Mixed-case local filenames must derive the same id regardless of case so
    # the case-sensitive duplicate check cannot admit logical duplicates.
    assert cc._slug("Team-A") == "team-a"
    assert cc._derive_id("./catalogs/Team-A.json") == "team-a"
    assert cc._derive_id("https://Example.com/Team-A.json") == "example-com-team-a"


def test_derive_id_handles_ipv6_literal():
    # An IPv6 host must not be truncated at the first colon.
    derived = cc._derive_id("https://[2001:db8::1]/catalog.json")
    assert derived == "2001-db8--1-catalog"


def test_derive_id_ignores_credentials_and_port():
    assert cc._derive_id("https://user:[email protected]:8443/c.json") == "example-com-c"


def test_add_source_rejects_unsupported_scheme(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    with pytest.raises(BundlerError, match="Unsupported catalog url scheme"):
        cc.add_source(project, "ssh://host/catalog.json", policy="install-allowed", priority=50)


def test_add_source_allows_local_path_with_colon(tmp_path: Path, monkeypatch):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    monkeypatch.chdir(project)
    # A relative path containing ':' but no '://' is still a local path.
    source, status = cc.add_source(
        project, "weird:name.json", policy="install-allowed", priority=50
    )
    assert status == "added"
    assert source.url.endswith("weird:name.json") or "weird" in source.url


def test_add_source_rejects_plain_http_for_non_localhost(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    with pytest.raises(BundlerError, match="HTTPS"):
        cc.add_source(project, "http://example.com/catalog.json", policy="install-allowed", priority=50)


def test_add_source_allows_http_for_localhost(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    source, status = cc.add_source(
        project,
        "http://localhost:8080/c.json",
        policy="install-allowed",
        priority=50,
    )
    assert status == "added"
    assert source.url == "http://localhost:8080/c.json"


def test_add_source_rejects_host_less_remote_urls(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    for url in ("https://:8080", "https://user@"):
        with pytest.raises(BundlerError, match="host"):
            cc.add_source(project, url, policy="install-allowed", priority=50)


def test_add_source_wraps_invalid_ipv6_as_bundler_error(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    with pytest.raises(BundlerError, match="Invalid catalog url"):
        cc.add_source(project, "https://[::1/c.json", policy="install-allowed", priority=50)


def test_add_source_wraps_bracketed_non_ip_host_as_bundler_error(tmp_path: Path):
    # A bracketed-but-invalid IPv6 authority (e.g. "https://[not-an-ip]/c.json")
    # parses cleanly under urlparse() on Python < 3.14 and only raises ValueError
    # lazily on the first .hostname access; the raise moved eager into urlparse()
    # in 3.14. add_source must surface its own BundlerError on every supported
    # version, never leak a raw ValueError past the CLI's `except BundlerError`.
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    with pytest.raises(BundlerError, match="Invalid catalog url"):
        cc.add_source(project, "https://[not-an-ip]/c.json", policy="install-allowed", priority=50)


def test_add_source_wraps_lazy_hostname_valueerror(tmp_path: Path, monkeypatch):
    # Simulate the Python < 3.14 shape explicitly (independent of the running
    # interpreter): urlparse() succeeds but .hostname raises ValueError lazily.
    # This is the exact path the fix guards; it fails with a raw ValueError if
    # .hostname is read outside the try/except.
    from urllib.parse import urlparse as _real_urlparse

    class _LazyHostnameRaiser:
        def __init__(self, parsed):
            self._parsed = parsed

        @property
        def hostname(self):
            raise ValueError("simulated lazy IPv6 hostname failure")

        def __getattr__(self, name):
            return getattr(self._parsed, name)

    def _fake_urlparse(url, *args, **kwargs):
        return _LazyHostnameRaiser(_real_urlparse(url, *args, **kwargs))

    monkeypatch.setattr(cc, "urlparse", _fake_urlparse)

    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    with pytest.raises(BundlerError, match="Invalid catalog url"):
        cc.add_source(project, "https://example.com/c.json", policy="install-allowed", priority=50)


def test_remove_source_does_not_crash_on_invalid_ipv6(tmp_path: Path):
    project = tmp_path / "proj"
    (project / ".specify").mkdir(parents=True)
    with pytest.raises(BundlerError, match="No project-scoped catalog source"):
        cc.remove_source(project, "https://[::1/c.json")