Skip to content

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

16.7 KB
Raw Blame
"""Unit tests for installed-bundle records and collateral-protection logic."""
from __future__ import annotations

import json
from pathlib import Path

import pytest

from specify_cli.bundler import BundlerError
from specify_cli.bundles.manifest import BundleRef, ComponentRef
from specify_cli.bundles.records import (
    InstalledBundleRecord,
    components_still_needed,
    load_records,
    records_path,
    remove_record,
    save_records,
    upsert_record,
)


def _record(bundle_id: str, comps) -> InstalledBundleRecord:
    return InstalledBundleRecord.create(
        bundle_id=bundle_id,
        version="1.0.0",
        components=[ComponentRef(kind=k, id=i) for k, i in comps],
    )


def test_save_and_load_roundtrip(tmp_path: Path):
    (tmp_path / ".specify").mkdir()
    rec = _record("a", [("presets", "p1"), ("steps", "s1")])
    save_records(tmp_path, [rec])
    loaded = load_records(tmp_path)
    assert len(loaded) == 1
    assert loaded[0].bundle_id == "a"
    assert {(c.kind, c.id) for c in loaded[0].contributed_components} == {
        ("presets", "p1"),
        ("steps", "s1"),
    }
    assert loaded[0].required_components == loaded[0].contributed_components


def test_roundtrip_preserves_bundle_references(tmp_path: Path):
    (tmp_path / ".specify").mkdir()
    references = [
        BundleRef("bugfix", "1.0.0", "default"),
        BundleRef("qa-pack", "2.0.0"),
    ]
    record = InstalledBundleRecord.create(
        bundle_id="team",
        version="1.0.0",
        components=[],
        bundle_references=references,
    )

    save_records(tmp_path, [record])
    restored = load_records(tmp_path)[0]

    assert restored.bundle_references == tuple(references)
    assert restored.to_dict()["bundle_references"] == [
        {"id": "bugfix", "version": "1.0.0", "source": "default"},
        {"id": "qa-pack", "version": "2.0.0", "source": None},
    ]


def test_empty_bundle_references_are_omitted_and_flat_records_stay_identical():
    record = _record("flat", [("extensions", "ext-a")])

    assert record.bundle_references == ()
    assert "bundle_references" not in record.to_dict()
    assert record.to_dict() == {
        "bundle_id": "flat",
        "version": "1.0.0",
        "installed_at": record.installed_at,
        "contributed_components": [{"kind": "extensions", "id": "ext-a"}],
        "required_components": [{"kind": "extensions", "id": "ext-a"}],
    }


@pytest.mark.parametrize("value", [None, []])
def test_from_dict_accepts_empty_bundle_references(value):
    data = {
        "bundle_id": "team",
        "version": "1.0.0",
        "contributed_components": [],
        "bundle_references": value,
    }
    assert InstalledBundleRecord.from_dict(data).bundle_references == ()
    data.pop("bundle_references")
    assert InstalledBundleRecord.from_dict(data).bundle_references == ()


@pytest.mark.parametrize(
    "value",
    [
        0,
        False,
        "not-a-list",
        {},
        [{"id": "bugfix"}],
        [{"version": "1.0.0"}],
        [{"id": "bugfix", "version": ""}],
        [{"id": "bugfix", "version": 1}],
        [{"id": 1, "version": "1.0.0"}],
        [{"id": "   ", "version": "1.0.0"}],
        [{"id": "bugfix", "version": "   "}],
        [{"id": "bugfix", "version": "1.0.0", "source": 1}],
        ["not-a-mapping"],
    ],
)
def test_from_dict_rejects_corrupt_bundle_references(value):
    data = {
        "bundle_id": "team",
        "version": "1.0.0",
        "contributed_components": [],
        "bundle_references": value,
    }
    with pytest.raises(BundlerError, match="Corrupt record: 'bundle_references'"):
        InstalledBundleRecord.from_dict(data)


def test_empty_reference_source_normalizes_to_none():
    data = {
        "bundle_id": "team",
        "version": "1.0.0",
        "contributed_components": [],
        "bundle_references": [{"id": "child", "version": "1.0.0", "source": ""}],
    }
    assert InstalledBundleRecord.from_dict(data).bundle_references[0].source is None


def test_roundtrip_retains_unowned_required_pin(tmp_path: Path):
    (tmp_path / ".specify").mkdir()
    required = ComponentRef(
        kind="extensions", id="independent", version="1.0.0", source="trusted"
    )
    record = InstalledBundleRecord.create(
        bundle_id="a", version="1.0.0", components=[], required_components=[required]
    )

    save_records(tmp_path, [record])
    restored = load_records(tmp_path)[0]

    assert restored.required_components == (required,)
    assert restored.contributed_components == ()
    serialized = json.loads(records_path(tmp_path).read_text())
    assert serialized["bundles"][0]["required_components"] == [{
        "kind": "extensions",
        "id": "independent",
        "version": "1.0.0",
        "source": "trusted",
    }]


def test_legacy_record_uses_contributions_as_known_requirements():
    record = InstalledBundleRecord.create(
        bundle_id="legacy",
        version="1.0.0",
        components=[ComponentRef(kind="extensions", id="ext-a", version="1.0.0")],
    )
    data = record.to_dict()
    data.pop("required_components", None)

    restored = InstalledBundleRecord.from_dict(data)

    assert restored.required_components == record.contributed_components


@pytest.mark.parametrize("bad", [None, 0, False, "", {}])
def test_from_dict_rejects_invalid_required_components(bad):
    data = {
        "bundle_id": "a",
        "version": "1.0.0",
        "contributed_components": [],
        "required_components": bad,
    }
    with pytest.raises(BundlerError, match="'required_components' must be a list"):
        InstalledBundleRecord.from_dict(data)


@pytest.mark.parametrize(
    ("component", "error"),
    [
        ({"kind": "bogus", "id": "ext-a"}, "kind' must be one of"),
        ({"kind": "extensions", "id": ""}, "required component is missing its 'id'"),
    ],
)
def test_from_dict_rejects_corrupt_required_component(component, error):
    data = {
        "bundle_id": "a",
        "version": "1.0.0",
        "contributed_components": [],
        "required_components": [component],
    }
    with pytest.raises(BundlerError, match=error):
        InstalledBundleRecord.from_dict(data)


@pytest.mark.parametrize(
    "required", [[], [{"kind": "extensions", "id": "ext-a", "version": "2.0.0"}]]
)
def test_from_dict_rejects_requirements_missing_contributed_pin(required):
    data = {
        "bundle_id": "a",
        "version": "1.0.0",
        "contributed_components": [
            {"kind": "extensions", "id": "ext-a", "version": "1.0.0"}
        ],
        "required_components": required,
    }
    with pytest.raises(BundlerError, match="must include all"):
        InstalledBundleRecord.from_dict(data)


def test_record_preserves_requested_and_installed_versions(tmp_path: Path):
    rec = InstalledBundleRecord.create(
        bundle_id="demo", version="1.0.0",
        components=[ComponentRef(kind="extensions", id="bug", version="1.0.0")],
        installed_versions={"extensions:bug": "1.0.1"},
    )
    save_records(tmp_path, [rec])

    loaded = load_records(tmp_path)[0]
    assert loaded.contributed_components[0].version == "1.0.0"
    assert loaded.installed_versions == {"extensions:bug": "1.0.1"}
    assert loaded.to_dict()["installed_versions"] == {"extensions:bug": "1.0.1"}


def test_legacy_record_has_no_installed_versions():
    rec = InstalledBundleRecord.from_dict(
        {"bundle_id": "demo", "version": "1.0.0", "contributed_components": []},
    )
    assert rec.installed_versions == {}
    assert "installed_versions" not in rec.to_dict()


@pytest.mark.parametrize("versions", [
    None, [], "1.0.1", False,
    {"extensions:bug": ""}, {"extensions:bug": "   "},
    {"extensions:bug": 1}, {"extensions:other": "1.0.1"},
])
def test_record_rejects_invalid_installed_versions(versions):
    with pytest.raises(BundlerError, match="installed_versions"):
        InstalledBundleRecord.from_dict({
            "bundle_id": "demo", "version": "1.0.0",
            "contributed_components": [{"kind": "extensions", "id": "bug"}],
            "installed_versions": versions,
        })


def test_load_missing_file_returns_empty(tmp_path: Path):
    (tmp_path / ".specify").mkdir()
    assert load_records(tmp_path) == []


@pytest.mark.parametrize("bad", [0, False, "", {}])
def test_load_records_rejects_falsy_non_list_bundles(tmp_path: Path, bad):
    # `data.get("bundles") or []` coerced a FALSY non-list (0, '', False, {})
    # to [] before the isinstance guard, silently treating a corrupt records
    # file as "no bundles". Only an absent/None value means empty.
    (tmp_path / ".specify").mkdir()
    records_path(tmp_path).write_text(
        json.dumps({"schema_version": "1.0", "bundles": bad}), encoding="utf-8"
    )
    with pytest.raises(BundlerError, match="'bundles' must be a list"):
        load_records(tmp_path)


@pytest.mark.parametrize("bad", [0, False, "", {}])
def test_from_dict_rejects_falsy_non_list_contributed_components(bad):
    # Same falsy-coercion hole for a record's 'contributed_components'.
    data = {"bundle_id": "a", "version": "1.0.0", "contributed_components": bad}
    with pytest.raises(BundlerError, match="'contributed_components' must be a list"):
        InstalledBundleRecord.from_dict(data)


def test_corrupt_priority_raises_actionable_error(tmp_path: Path):
    (tmp_path / ".specify").mkdir()
    rec = _record("a", [("presets", "p1")])
    save_records(tmp_path, [rec])
    path = records_path(tmp_path)
    data = json.loads(path.read_text(encoding="utf-8"))
    data["bundles"][0]["contributed_components"][0]["priority"] = "high"
    path.write_text(json.dumps(data), encoding="utf-8")
    with pytest.raises(BundlerError, match="priority must be an integer"):
        load_records(tmp_path)


def test_upsert_replaces_same_id():
    rec1 = _record("a", [("presets", "p1")])
    rec2 = _record("a", [("presets", "p2")])
    result = upsert_record([rec1], rec2)
    assert len(result) == 1
    assert result[0].contributed_components[0].id == "p2"


def test_remove_record_drops_target():
    recs = [_record("a", [("presets", "p1")]), _record("b", [("steps", "s1")])]
    result = remove_record(recs, "a")
    assert [r.bundle_id for r in result] == ["b"]


def test_components_still_needed_excludes_target():
    recs = [
        _record("a", [("presets", "shared"), ("steps", "only-a")]),
        _record("b", [("presets", "shared")]),
    ]
    needed = components_still_needed(recs, exclude_bundle_id="a")
    assert ("presets", "shared") in needed
    assert ("steps", "only-a") not in needed


def test_save_records_refuses_symlinked_specify_escape(tmp_path: Path):
    # Defense-in-depth: a symlinked .specify pointing outside the project must
    # not let records be written outside project_root.
    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"):
        save_records(project, [_record("a", [("presets", "p1")])])


def test_load_records_rejects_non_list_bundles(tmp_path: Path):
    (tmp_path / ".specify").mkdir()
    path = records_path(tmp_path)
    path.write_text(json.dumps({"schema_version": "1.0", "bundles": "oops"}), encoding="utf-8")
    with pytest.raises(BundlerError, match="'bundles' must be a list"):
        load_records(tmp_path)


def test_load_records_rejects_non_list_contributed_components(tmp_path: Path):
    (tmp_path / ".specify").mkdir()
    path = records_path(tmp_path)
    payload = {
        "schema_version": "1.0",
        "bundles": [
            {"bundle_id": "a", "version": "1.0.0", "contributed_components": "oops"}
        ],
    }
    path.write_text(json.dumps(payload), encoding="utf-8")
    with pytest.raises(BundlerError, match="'contributed_components' must be a list"):
        load_records(tmp_path)


def test_load_records_rejects_unknown_component_kind(tmp_path: Path):
    (tmp_path / ".specify").mkdir()
    path = records_path(tmp_path)
    payload = {
        "schema_version": "1.0",
        "bundles": [
            {
                "bundle_id": "a",
                "version": "1.0.0",
                "contributed_components": [{"kind": "bogus", "id": "x"}],
            }
        ],
    }
    path.write_text(json.dumps(payload), encoding="utf-8")
    with pytest.raises(BundlerError, match="must be one of"):
        load_records(tmp_path)


def test_load_records_rejects_component_missing_id(tmp_path: Path):
    (tmp_path / ".specify").mkdir()
    path = records_path(tmp_path)
    payload = {
        "schema_version": "1.0",
        "bundles": [
            {
                "bundle_id": "a",
                "version": "1.0.0",
                "contributed_components": [{"kind": "presets", "id": ""}],
            }
        ],
    }
    path.write_text(json.dumps(payload), encoding="utf-8")
    with pytest.raises(BundlerError, match="missing its 'id'"):
        load_records(tmp_path)


def test_load_records_rejects_missing_schema_version(tmp_path: Path):
    (tmp_path / ".specify").mkdir()
    records_path(tmp_path).write_text(json.dumps({"bundles": []}), encoding="utf-8")
    with pytest.raises(BundlerError, match="missing 'schema_version'"):
        load_records(tmp_path)


def test_load_records_rejects_unknown_schema_version(tmp_path: Path):
    (tmp_path / ".specify").mkdir()
    payload = {"schema_version": "2.0", "bundles": []}
    records_path(tmp_path).write_text(json.dumps(payload), encoding="utf-8")
    with pytest.raises(BundlerError, match="Unsupported records schema version"):
        load_records(tmp_path)


def test_load_records_rejects_record_missing_bundle_id(tmp_path: Path):
    (tmp_path / ".specify").mkdir()
    payload = {"schema_version": "1.0", "bundles": [{"version": "1.0.0"}]}
    records_path(tmp_path).write_text(json.dumps(payload), encoding="utf-8")
    with pytest.raises(BundlerError, match="missing its 'bundle_id'"):
        load_records(tmp_path)


def test_load_records_rejects_record_missing_version(tmp_path: Path):
    (tmp_path / ".specify").mkdir()
    payload = {"schema_version": "1.0", "bundles": [{"bundle_id": "a"}]}
    records_path(tmp_path).write_text(json.dumps(payload), encoding="utf-8")
    with pytest.raises(BundlerError, match="missing its 'version'"):
        load_records(tmp_path)


def test_load_records_accepts_forward_compatible_minor_schema(tmp_path: Path):
    (tmp_path / ".specify").mkdir()
    payload = {"schema_version": "1.5", "bundles": []}
    records_path(tmp_path).write_text(json.dumps(payload), encoding="utf-8")
    assert load_records(tmp_path) == []


@pytest.mark.parametrize(
    "field,message",
    [
        ("bundle_id", "missing its 'bundle_id'"),
        ("version", "missing its 'version'"),
    ],
)
def test_load_records_rejects_explicit_null_record_field(
    tmp_path: Path, field: str, message: str
):
    """An explicit JSON ``null`` is how a corrupt record spells an empty field.

    ``str(data.get(field, ""))`` defaults only a *missing* key, so a
    present-but-null value became the literal text ``"None"`` β€” non-empty, so
    it sailed past the required-field checks and the record was accepted as a
    bundle actually named ``"None"``. Mirrors ``manifest._text``.
    """
    (tmp_path / ".specify").mkdir()
    record = {"bundle_id": "a", "version": "1.0.0", "contributed_components": []}
    record[field] = None
    payload = {"schema_version": "1.0", "bundles": [record]}
    records_path(tmp_path).write_text(json.dumps(payload), encoding="utf-8")

    with pytest.raises(BundlerError, match=message):
        load_records(tmp_path)


def test_load_records_rejects_explicit_null_component_id(tmp_path: Path):
    """A null component id became ``"None"`` and entered the refcount.

    ``components_still_needed`` would then report a phantom
    ``('presets', 'None')`` as protected.
    """
    (tmp_path / ".specify").mkdir()
    payload = {
        "schema_version": "1.0",
        "bundles": [
            {
                "bundle_id": "a",
                "version": "1.0.0",
                "contributed_components": [{"kind": "presets", "id": None}],
            }
        ],
    }
    records_path(tmp_path).write_text(json.dumps(payload), encoding="utf-8")

    with pytest.raises(BundlerError, match="missing its 'id'"):
        load_records(tmp_path)


def test_load_records_accepts_explicit_null_installed_at(tmp_path: Path):
    """``installed_at`` is optional, so a null must become "" β€” not "None"."""
    (tmp_path / ".specify").mkdir()
    payload = {
        "schema_version": "1.0",
        "bundles": [
            {
                "bundle_id": "a",
                "version": "1.0.0",
                "installed_at": None,
                "contributed_components": [],
            }
        ],
    }
    records_path(tmp_path).write_text(json.dumps(payload), encoding="utf-8")

    records = load_records(tmp_path)
    assert records[0].installed_at == ""