@@ -71,6 +71,48 @@ def test_disable_warn_on_duplicate_resources(temp_file: Path, caplog: pytest.Log
7171 assert "Resource already exists" not in caplog .text
7272
7373
74+ def test_add_duplicate_template ():
75+ """Test adding the same template twice keeps the first one."""
76+
77+ def first (item_id : str ) -> str :
78+ return f"first:{ item_id } "
79+
80+ def second (item_id : str ) -> str :
81+ return f"second:{ item_id } "
82+
83+ manager = ResourceManager ()
84+ first_template = manager .add_template (first , uri_template = "items://{item_id}" )
85+ second_template = manager .add_template (second , uri_template = "items://{item_id}" )
86+
87+ assert first_template is second_template
88+ assert first_template .fn ("a" ) == "first:a"
89+ assert manager .list_templates () == [first_template ]
90+
91+
92+ def test_warn_on_duplicate_templates (caplog : pytest .LogCaptureFixture ):
93+ """Test warning on duplicate templates."""
94+
95+ def handler (item_id : str ) -> str :
96+ return item_id
97+
98+ manager = ResourceManager ()
99+ manager .add_template (handler , uri_template = "items://{item_id}" )
100+ manager .add_template (handler , uri_template = "items://{item_id}" )
101+ assert "Resource template already exists" in caplog .text
102+
103+
104+ def test_disable_warn_on_duplicate_templates (caplog : pytest .LogCaptureFixture ):
105+ """Test disabling warning on duplicate templates."""
106+
107+ def handler (item_id : str ) -> str :
108+ return item_id
109+
110+ manager = ResourceManager (warn_on_duplicate_resources = False )
111+ manager .add_template (handler , uri_template = "items://{item_id}" )
112+ manager .add_template (handler , uri_template = "items://{item_id}" )
113+ assert "Resource template already exists" not in caplog .text
114+
115+
74116@pytest .mark .anyio
75117async def test_get_resource (temp_file : Path ):
76118 """Test getting a resource by URI."""
0 commit comments