|
| 1 | +"""Unit tests for plugin name caching and validation in tools/utils.py.""" |
| 2 | +import json |
| 3 | +import unittest |
| 4 | +from unittest.mock import patch, mock_open |
| 5 | + |
| 6 | +from api.tools.utils import ( |
| 7 | + _tokenize_plugin_name, |
| 8 | + _load_plugin_names, |
| 9 | + is_valid_plugin, |
| 10 | + filter_retrieved_data, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class TestTokenizePluginName(unittest.TestCase): |
| 15 | + """Tests for the _tokenize_plugin_name helper.""" |
| 16 | + |
| 17 | + def test_converts_to_lowercase(self): |
| 18 | + """Plugin names should be case-insensitive.""" |
| 19 | + self.assertEqual(_tokenize_plugin_name("Git"), "git") |
| 20 | + |
| 21 | + def test_removes_hyphens(self): |
| 22 | + """Hyphens should be stripped for comparison.""" |
| 23 | + self.assertEqual( |
| 24 | + _tokenize_plugin_name("blue-ocean"), "blueocean" |
| 25 | + ) |
| 26 | + |
| 27 | + def test_removes_spaces(self): |
| 28 | + """Spaces should be stripped for comparison.""" |
| 29 | + self.assertEqual( |
| 30 | + _tokenize_plugin_name("Blue Ocean"), "blueocean" |
| 31 | + ) |
| 32 | + |
| 33 | + def test_removes_hyphens_and_spaces(self): |
| 34 | + """Both hyphens and spaces should be stripped.""" |
| 35 | + self.assertEqual( |
| 36 | + _tokenize_plugin_name("my-cool plugin"), "mycoolplugin" |
| 37 | + ) |
| 38 | + |
| 39 | + def test_empty_string(self): |
| 40 | + """Empty string should return empty string.""" |
| 41 | + self.assertEqual(_tokenize_plugin_name(""), "") |
| 42 | + |
| 43 | + |
| 44 | +class TestLoadPluginNames(unittest.TestCase): |
| 45 | + """Tests for the cached _load_plugin_names function.""" |
| 46 | + |
| 47 | + def setUp(self): |
| 48 | + """Clear lru_cache before each test to ensure isolation.""" |
| 49 | + _load_plugin_names.cache_clear() |
| 50 | + |
| 51 | + def tearDown(self): |
| 52 | + """Clear lru_cache after each test.""" |
| 53 | + _load_plugin_names.cache_clear() |
| 54 | + |
| 55 | + @patch( |
| 56 | + "builtins.open", |
| 57 | + mock_open(read_data=json.dumps(["git", "Blue-Ocean", "credentials"])), |
| 58 | + ) |
| 59 | + @patch("os.path.dirname", return_value="/fake/dir") |
| 60 | + def test_returns_frozenset(self, _mock_dir): |
| 61 | + """Result should be a frozenset for O(1) lookups.""" |
| 62 | + result = _load_plugin_names() |
| 63 | + self.assertIsInstance(result, frozenset) |
| 64 | + |
| 65 | + @patch( |
| 66 | + "builtins.open", |
| 67 | + mock_open(read_data=json.dumps(["git", "Blue-Ocean", "credentials"])), |
| 68 | + ) |
| 69 | + @patch("os.path.dirname", return_value="/fake/dir") |
| 70 | + def test_tokenizes_all_names(self, _mock_dir): |
| 71 | + """All names should be tokenized (lowercased, no hyphens/spaces).""" |
| 72 | + result = _load_plugin_names() |
| 73 | + self.assertIn("git", result) |
| 74 | + self.assertIn("blueocean", result) |
| 75 | + self.assertIn("credentials", result) |
| 76 | + |
| 77 | + @patch( |
| 78 | + "builtins.open", |
| 79 | + mock_open(read_data=json.dumps(["git", "Blue-Ocean", "credentials"])), |
| 80 | + ) |
| 81 | + @patch("os.path.dirname", return_value="/fake/dir") |
| 82 | + def test_caching_reads_file_once(self, _mock_dir): |
| 83 | + """Calling _load_plugin_names twice should only open the file once.""" |
| 84 | + with patch("builtins.open", mock_open( |
| 85 | + read_data=json.dumps(["git"]) |
| 86 | + )) as mocked_file: |
| 87 | + _load_plugin_names() |
| 88 | + _load_plugin_names() |
| 89 | + mocked_file.assert_called_once() |
| 90 | + |
| 91 | + |
| 92 | +class TestIsValidPlugin(unittest.TestCase): |
| 93 | + """Tests for the is_valid_plugin function.""" |
| 94 | + |
| 95 | + def setUp(self): |
| 96 | + _load_plugin_names.cache_clear() |
| 97 | + |
| 98 | + def tearDown(self): |
| 99 | + _load_plugin_names.cache_clear() |
| 100 | + |
| 101 | + @patch( |
| 102 | + "builtins.open", |
| 103 | + mock_open( |
| 104 | + read_data=json.dumps( |
| 105 | + ["git", "blue-ocean", "credentials", "github-branch-source"] |
| 106 | + ) |
| 107 | + ), |
| 108 | + ) |
| 109 | + @patch("os.path.dirname", return_value="/fake/dir") |
| 110 | + def test_exact_match(self, _mock_dir): |
| 111 | + """Exact plugin name should be valid.""" |
| 112 | + self.assertTrue(is_valid_plugin("git")) |
| 113 | + |
| 114 | + @patch( |
| 115 | + "builtins.open", |
| 116 | + mock_open( |
| 117 | + read_data=json.dumps( |
| 118 | + ["git", "blue-ocean", "credentials", "github-branch-source"] |
| 119 | + ) |
| 120 | + ), |
| 121 | + ) |
| 122 | + @patch("os.path.dirname", return_value="/fake/dir") |
| 123 | + def test_case_insensitive_match(self, _mock_dir): |
| 124 | + """Plugin names should match case-insensitively.""" |
| 125 | + self.assertTrue(is_valid_plugin("Git")) |
| 126 | + self.assertTrue(is_valid_plugin("GIT")) |
| 127 | + |
| 128 | + @patch( |
| 129 | + "builtins.open", |
| 130 | + mock_open( |
| 131 | + read_data=json.dumps( |
| 132 | + ["git", "blue-ocean", "credentials", "github-branch-source"] |
| 133 | + ) |
| 134 | + ), |
| 135 | + ) |
| 136 | + @patch("os.path.dirname", return_value="/fake/dir") |
| 137 | + def test_hyphen_insensitive_match(self, _mock_dir): |
| 138 | + """Hyphens should be ignored during matching.""" |
| 139 | + self.assertTrue(is_valid_plugin("blue ocean")) |
| 140 | + self.assertTrue(is_valid_plugin("blueocean")) |
| 141 | + self.assertTrue(is_valid_plugin("Blue-Ocean")) |
| 142 | + |
| 143 | + @patch( |
| 144 | + "builtins.open", |
| 145 | + mock_open( |
| 146 | + read_data=json.dumps( |
| 147 | + ["git", "blue-ocean", "credentials", "github-branch-source"] |
| 148 | + ) |
| 149 | + ), |
| 150 | + ) |
| 151 | + @patch("os.path.dirname", return_value="/fake/dir") |
| 152 | + def test_invalid_plugin_returns_false(self, _mock_dir): |
| 153 | + """Non-existent plugin name should return False.""" |
| 154 | + self.assertFalse(is_valid_plugin("nonexistent-plugin")) |
| 155 | + self.assertFalse(is_valid_plugin("")) |
| 156 | + |
| 157 | + |
| 158 | +class TestFilterRetrievedData(unittest.TestCase): |
| 159 | + """Tests for filter_retrieved_data using the shared tokenizer.""" |
| 160 | + |
| 161 | + def test_filters_matching_entries(self): |
| 162 | + """Only entries whose title matches the plugin name should remain.""" |
| 163 | + semantic_data = [ |
| 164 | + {"metadata": {"title": "blue-ocean"}, "chunk_text": "a"}, |
| 165 | + {"metadata": {"title": "credentials"}, "chunk_text": "b"}, |
| 166 | + ] |
| 167 | + keyword_data = [ |
| 168 | + {"metadata": {"title": "Blue Ocean"}, "chunk_text": "c"}, |
| 169 | + {"metadata": {"title": "git"}, "chunk_text": "d"}, |
| 170 | + ] |
| 171 | + sem, kw = filter_retrieved_data( |
| 172 | + semantic_data, keyword_data, "blue-ocean" |
| 173 | + ) |
| 174 | + self.assertEqual(len(sem), 1) |
| 175 | + self.assertEqual(sem[0]["chunk_text"], "a") |
| 176 | + self.assertEqual(len(kw), 1) |
| 177 | + self.assertEqual(kw[0]["chunk_text"], "c") |
| 178 | + |
| 179 | + def test_returns_empty_when_no_match(self): |
| 180 | + """No results should be returned when nothing matches.""" |
| 181 | + data = [{"metadata": {"title": "git"}, "chunk_text": "x"}] |
| 182 | + sem, kw = filter_retrieved_data(data, data, "nonexistent") |
| 183 | + self.assertEqual(len(sem), 0) |
| 184 | + self.assertEqual(len(kw), 0) |
| 185 | + |
| 186 | + def test_empty_input(self): |
| 187 | + """Empty input lists should return empty lists.""" |
| 188 | + sem, kw = filter_retrieved_data([], [], "git") |
| 189 | + self.assertEqual(sem, []) |
| 190 | + self.assertEqual(kw, []) |
| 191 | + |
| 192 | + |
| 193 | +if __name__ == "__main__": |
| 194 | + unittest.main() |
0 commit comments