1
+ """Test prefix and separator handling."""
2
+
3
+ import pytest
4
+ import os
5
+ from magg .settings import MaggConfig , ServerConfig
6
+ from magg .server .server import MaggServer
7
+ from fastmcp import Client
8
+
9
+
10
+ class TestPrefixHandling :
11
+ """Test custom prefix and separator handling."""
12
+
13
+ def test_prefix_separator_on_magg_config (self ):
14
+ """Test that prefix_sep is a configurable field on MaggConfig."""
15
+ config = MaggConfig ()
16
+ assert hasattr (config , 'prefix_sep' )
17
+ assert config .prefix_sep == "_" # Default value
18
+
19
+ # Test it can be configured
20
+ config2 = MaggConfig (prefix_sep = "-" )
21
+ assert config2 .prefix_sep == "-"
22
+
23
+ def test_server_config_uses_separator (self ):
24
+ """Test that ServerConfig validation uses the separator."""
25
+ # Valid prefix
26
+ server = ServerConfig (name = "test" , source = "test" , prefix = "myprefix" )
27
+ assert server .prefix == "myprefix"
28
+
29
+ # Invalid prefix with underscore
30
+ with pytest .raises (ValueError , match = "cannot contain underscores" ):
31
+ ServerConfig (name = "test" , source = "test" , prefix = "my_prefix" )
32
+
33
+ @pytest .mark .asyncio
34
+ async def test_custom_self_prefix (self , tmp_path , monkeypatch ):
35
+ """Test that custom self_prefix is used correctly."""
36
+ # Set custom prefix via environment
37
+ monkeypatch .setenv ("MAGG_SELF_PREFIX" , "myapp" )
38
+
39
+ config_path = tmp_path / "config.json"
40
+ server = MaggServer (config_path , enable_config_reload = False )
41
+
42
+ # Check configuration
43
+ assert server .self_prefix == "myapp"
44
+ assert server .self_prefix_ == "myapp_"
45
+
46
+ await server .setup ()
47
+
48
+ # Verify tools have the correct prefix
49
+ async with Client (server .mcp ) as client :
50
+ tools = await client .list_tools ()
51
+ tool_names = [tool .name for tool in tools ]
52
+
53
+ # All Magg tools should have myapp_ prefix
54
+ myapp_tools = [t for t in tool_names if t .startswith ("myapp_" )]
55
+ assert len (myapp_tools ) > 0
56
+ assert "myapp_list_servers" in tool_names
57
+ assert "myapp_add_server" in tool_names
58
+ assert "myapp_status" in tool_names
59
+
60
+ # Should not have any magg_ tools
61
+ magg_tools = [t for t in tool_names if t .startswith ("magg_" )]
62
+ assert len (magg_tools ) == 0
63
+
64
+ def test_prefix_separator_consistency (self ):
65
+ """Test that prefix separator is used consistently."""
66
+ # Create a server with default Magg config
67
+ config = MaggConfig ()
68
+ assert config .self_prefix == "magg"
69
+ assert config .prefix_sep == "_"
70
+
71
+ # Server config validation should use the separator
72
+ server1 = ServerConfig (name = "test1" , source = "test" , prefix = "myprefix" )
73
+ assert server1 .prefix == "myprefix"
74
+
75
+ # Empty prefix is allowed
76
+ server2 = ServerConfig (name = "test2" , source = "test" , prefix = "" )
77
+ assert server2 .prefix == ""
78
+
79
+ # None prefix is allowed
80
+ server3 = ServerConfig (name = "test3" , source = "test" , prefix = None )
81
+ assert server3 .prefix is None
0 commit comments