# Copyright Visual Collaboration Technologies Inc. All Rights Reserved.
# See LICENSE for details.
"""Tests for vcti.plugincatalog.descriptor module."""

from vcti.plugincatalog.descriptor import Descriptor


class TestDescriptor:
    def test_basic_creation(self):
        desc = Descriptor(id="test", name="Test Plugin", instance=object())
        assert desc.id == "test"
        assert desc.name == "Test Plugin"

    def test_optional_fields_default(self):
        desc = Descriptor(id="test", name="Test", instance=object())
        assert desc.description is None
        assert desc.attributes == {}

    def test_attributes_stored(self):
        attrs = {"category": "test", "version": "1.0"}
        desc = Descriptor(id="test", name="Test", instance=object(), attributes=attrs)
        assert desc.attributes["category"] == "test"
        assert desc.attributes["version"] == "1.0"

    def test_description_stored(self):
        desc = Descriptor(id="test", name="Test", instance=object(), description="A test plugin")
        assert desc.description == "A test plugin"

    def test_instance_accessible(self):
        class Plugin:
            value = 42

        desc = Descriptor(id="p", name="P", instance=Plugin())
        assert desc.instance.value == 42

    def test_repr(self):
        desc = Descriptor(id="my-id", name="My Name", instance="hello")
        r = repr(desc)
        assert "my-id" in r
        assert "My Name" in r
        assert "str" in r

    def test_generic_type(self):
        """Descriptor preserves the instance type."""

        class MyPlugin:
            def run(self) -> str:
                return "running"

        desc = Descriptor[MyPlugin](id="mp", name="MP", instance=MyPlugin())
        assert desc.instance.run() == "running"

    def test_empty_string_id(self):
        """Empty string is a valid ID (registry enforces uniqueness, not descriptor)."""
        desc = Descriptor(id="", name="Empty", instance=object())
        assert desc.id == ""

    def test_whitespace_id(self):
        desc = Descriptor(id="  ", name="Spaces", instance=object())
        assert desc.id == "  "

    def test_repr_with_none_description(self):
        desc = Descriptor(id="x", name="X", instance=42)
        r = repr(desc)
        assert "x" in r
        assert "int" in r

    def test_attributes_default_is_independent(self):
        """Each descriptor gets its own empty dict, not a shared default."""
        d1 = Descriptor(id="a", name="A", instance=object())
        d2 = Descriptor(id="b", name="B", instance=object())
        d1.attributes["key"] = "value"
        assert "key" not in d2.attributes
