|
| 1 | +"""Unit tests for tool_formatter utilities.""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from utils.tool_formatter import translate_vector_store_ids_to_user_facing |
| 6 | + |
| 7 | + |
| 8 | +class TestTranslateVectorStoreIdsToUserFacing: |
| 9 | + """Tests for translate_vector_store_ids_to_user_facing.""" |
| 10 | + |
| 11 | + def test_empty_mapping_returns_new_list_same_tool_objects(self) -> None: |
| 12 | + """When mapping is empty, return a new list with the same tool dicts.""" |
| 13 | + tools: list[dict[str, Any]] = [ |
| 14 | + {"type": "file_search", "vector_store_ids": ["vs-1"]}, |
| 15 | + ] |
| 16 | + result = translate_vector_store_ids_to_user_facing(tools, {}) |
| 17 | + assert result is not tools |
| 18 | + assert result == tools |
| 19 | + assert result[0] is tools[0] |
| 20 | + |
| 21 | + def test_file_search_vector_store_ids_mapped(self) -> None: |
| 22 | + """file_search tools get vector_store_ids rewritten via mapping.""" |
| 23 | + tools: list[dict[str, Any]] = [ |
| 24 | + { |
| 25 | + "type": "file_search", |
| 26 | + "vector_store_ids": ["llama-vs", "other-vs"], |
| 27 | + }, |
| 28 | + ] |
| 29 | + mapping = {"llama-vs": "user-rag-a", "other-vs": "user-rag-b"} |
| 30 | + result = translate_vector_store_ids_to_user_facing(tools, mapping) |
| 31 | + assert result[0]["vector_store_ids"] == ["user-rag-a", "user-rag-b"] |
| 32 | + assert result[0]["type"] == "file_search" |
| 33 | + |
| 34 | + def test_unmapped_id_passthrough(self) -> None: |
| 35 | + """IDs absent from mapping are left unchanged.""" |
| 36 | + tools: list[dict[str, Any]] = [ |
| 37 | + {"type": "file_search", "vector_store_ids": ["known", "unknown-id"]}, |
| 38 | + ] |
| 39 | + result = translate_vector_store_ids_to_user_facing( |
| 40 | + tools, {"known": "user-facing"} |
| 41 | + ) |
| 42 | + assert result[0]["vector_store_ids"] == ["user-facing", "unknown-id"] |
| 43 | + |
| 44 | + def test_non_file_search_tool_unchanged_identity(self) -> None: |
| 45 | + """Non-file_search entries are appended as the same dict instance.""" |
| 46 | + mcp_tool: dict[str, Any] = {"type": "mcp", "server_url": "http://x"} |
| 47 | + tools: list[dict[str, Any]] = [mcp_tool] |
| 48 | + result = translate_vector_store_ids_to_user_facing(tools, {"any": "mapping"}) |
| 49 | + assert len(result) == 1 |
| 50 | + assert result[0] is mcp_tool |
| 51 | + |
| 52 | + def test_file_search_new_dict_instance(self) -> None: |
| 53 | + """file_search entries are copied so original tool dict is not mutated.""" |
| 54 | + original: dict[str, Any] = { |
| 55 | + "type": "file_search", |
| 56 | + "vector_store_ids": ["vs-1"], |
| 57 | + } |
| 58 | + tools: list[dict[str, Any]] = [original] |
| 59 | + result = translate_vector_store_ids_to_user_facing(tools, {"vs-1": "u-1"}) |
| 60 | + assert result[0] is not original |
| 61 | + assert original["vector_store_ids"] == ["vs-1"] |
| 62 | + |
| 63 | + def test_mixed_tools_order_preserved(self) -> None: |
| 64 | + """Order and handling per type are stable across a mixed tool list.""" |
| 65 | + tools: list[dict[str, Any]] = [ |
| 66 | + {"type": "file_search", "vector_store_ids": ["a"]}, |
| 67 | + {"type": "function", "name": "fn"}, |
| 68 | + {"type": "file_search", "vector_store_ids": ["b", "c"]}, |
| 69 | + ] |
| 70 | + result = translate_vector_store_ids_to_user_facing( |
| 71 | + tools, {"a": "A", "b": "B", "c": "C"} |
| 72 | + ) |
| 73 | + assert [t["type"] for t in result] == ["file_search", "function", "file_search"] |
| 74 | + assert result[0]["vector_store_ids"] == ["A"] |
| 75 | + assert result[1] is tools[1] |
| 76 | + assert result[2]["vector_store_ids"] == ["B", "C"] |
0 commit comments