|
| 1 | +"""Unit tests for models.utils (mirrors src/models/utils.py).""" |
| 2 | + |
| 3 | +from llama_stack_api.openai_responses import ( |
| 4 | + OpenAIResponseInputToolFileSearch as InputToolFileSearch, |
| 5 | +) |
| 6 | +from llama_stack_api.openai_responses import ( |
| 7 | + OpenAIResponseInputToolMCP as InputToolMCP, |
| 8 | +) |
| 9 | + |
| 10 | +from models.utils import add_mcp_authorizations |
| 11 | + |
| 12 | + |
| 13 | +class TestAddMcpAuthorizations: |
| 14 | + """Tests for add_mcp_authorizations with realistic MCP tool rows. |
| 15 | +
|
| 16 | + Assumes server_label is present on MCP dicts and unique across configured |
| 17 | + servers; see InputToolMCP in llama-stack-api. |
| 18 | + """ |
| 19 | + |
| 20 | + def test_merges_authorization_by_server_label(self) -> None: |
| 21 | + """MCP model_dump omits authorization; the helper restores it by server_label.""" |
| 22 | + live = InputToolMCP( |
| 23 | + server_label="alpha", |
| 24 | + server_url="http://alpha", |
| 25 | + require_approval="never", |
| 26 | + authorization="secret-token", |
| 27 | + ) |
| 28 | + dumped = [live.model_dump()] |
| 29 | + assert "authorization" not in dumped[0] |
| 30 | + |
| 31 | + out = add_mcp_authorizations(dumped, [live]) |
| 32 | + assert len(out) == 1 |
| 33 | + assert out[0]["authorization"] == "secret-token" |
| 34 | + assert out[0]["server_label"] == "alpha" |
| 35 | + |
| 36 | + def test_two_mcp_servers_distinct_tokens(self) -> None: |
| 37 | + """Each server_label receives its own authorization.""" |
| 38 | + a = InputToolMCP( |
| 39 | + server_label="srv-a", |
| 40 | + server_url="http://a", |
| 41 | + require_approval="never", |
| 42 | + authorization="token-a", |
| 43 | + ) |
| 44 | + b = InputToolMCP( |
| 45 | + server_label="srv-b", |
| 46 | + server_url="http://b", |
| 47 | + require_approval="never", |
| 48 | + authorization="token-b", |
| 49 | + ) |
| 50 | + dumped = [a.model_dump(), b.model_dump()] |
| 51 | + assert "authorization" not in dumped[0] |
| 52 | + assert "authorization" not in dumped[1] |
| 53 | + |
| 54 | + out = add_mcp_authorizations(dumped, [a, b]) |
| 55 | + assert out[0]["authorization"] == "token-a" |
| 56 | + assert out[1]["authorization"] == "token-b" |
| 57 | + |
| 58 | + def test_file_search_row_unchanged_no_authorization_merge(self) -> None: |
| 59 | + """Non-MCP rows are copied; MCP row still gets auth from live list.""" |
| 60 | + mcp = InputToolMCP( |
| 61 | + server_label="m", |
| 62 | + server_url="http://m", |
| 63 | + require_approval="never", |
| 64 | + authorization="mcp-secret", |
| 65 | + ) |
| 66 | + fs = InputToolFileSearch(type="file_search", vector_store_ids=["vs-1"]) |
| 67 | + dumped = [fs.model_dump(), mcp.model_dump()] |
| 68 | + assert "authorization" not in dumped[1] |
| 69 | + |
| 70 | + out = add_mcp_authorizations(dumped, [fs, mcp]) |
| 71 | + assert out[0]["type"] == "file_search" |
| 72 | + assert "authorization" not in out[0] |
| 73 | + assert out[1]["authorization"] == "mcp-secret" |
| 74 | + |
| 75 | + def test_subset_dumped_rows_still_match_live_by_label(self) -> None: |
| 76 | + """When only some MCP tools appear in dumped_tools, labels still align.""" |
| 77 | + first = InputToolMCP( |
| 78 | + server_label="one", |
| 79 | + server_url="http://one", |
| 80 | + require_approval="never", |
| 81 | + authorization="tok-one", |
| 82 | + ) |
| 83 | + second = InputToolMCP( |
| 84 | + server_label="two", |
| 85 | + server_url="http://two", |
| 86 | + require_approval="never", |
| 87 | + authorization="tok-two", |
| 88 | + ) |
| 89 | + dumped = [second.model_dump()] |
| 90 | + assert "authorization" not in dumped[0] |
| 91 | + |
| 92 | + out = add_mcp_authorizations(dumped, [first, second]) |
| 93 | + assert len(out) == 1 |
| 94 | + assert out[0]["authorization"] == "tok-two" |
| 95 | + |
| 96 | + def test_does_not_mutate_input_list_or_dicts(self) -> None: |
| 97 | + """Output is new containers; inputs stay as provided.""" |
| 98 | + live = InputToolMCP( |
| 99 | + server_label="s", |
| 100 | + server_url="http://s", |
| 101 | + require_approval="never", |
| 102 | + authorization="t", |
| 103 | + ) |
| 104 | + dumped = [live.model_dump()] |
| 105 | + row = dumped[0] |
| 106 | + assert "authorization" not in row |
| 107 | + |
| 108 | + out = add_mcp_authorizations(dumped, [live]) |
| 109 | + assert out is not dumped |
| 110 | + assert out[0] is not row |
| 111 | + assert "authorization" not in row |
0 commit comments