|
| 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 | + is_valid_plugin, |
| 8 | + load_plugin_names, |
| 9 | + filter_retrieved_data, |
| 10 | +) |
| 11 | + |
| 12 | +SAMPLE_PLUGINS = json.dumps( |
| 13 | + ["git", "blue-ocean", "credentials", "github-branch-source"] |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class TestIsValidPlugin(unittest.TestCase): |
| 18 | + """Tests for the is_valid_plugin function.""" |
| 19 | + |
| 20 | + def setUp(self): |
| 21 | + load_plugin_names.cache_clear() |
| 22 | + |
| 23 | + def tearDown(self): |
| 24 | + load_plugin_names.cache_clear() |
| 25 | + |
| 26 | + @patch( |
| 27 | + "builtins.open", |
| 28 | + mock_open(read_data=SAMPLE_PLUGINS), |
| 29 | + ) |
| 30 | + @patch("os.path.dirname", return_value="/fake/dir") |
| 31 | + def test_exact_match(self, _mock_dir): |
| 32 | + """Exact plugin name should be valid.""" |
| 33 | + self.assertTrue(is_valid_plugin("git")) |
| 34 | + |
| 35 | + @patch( |
| 36 | + "builtins.open", |
| 37 | + mock_open(read_data=SAMPLE_PLUGINS), |
| 38 | + ) |
| 39 | + @patch("os.path.dirname", return_value="/fake/dir") |
| 40 | + def test_case_insensitive_match(self, _mock_dir): |
| 41 | + """Plugin names should match case-insensitively.""" |
| 42 | + self.assertTrue(is_valid_plugin("Git")) |
| 43 | + self.assertTrue(is_valid_plugin("GIT")) |
| 44 | + |
| 45 | + @patch( |
| 46 | + "builtins.open", |
| 47 | + mock_open(read_data=SAMPLE_PLUGINS), |
| 48 | + ) |
| 49 | + @patch("os.path.dirname", return_value="/fake/dir") |
| 50 | + def test_hyphen_insensitive_match(self, _mock_dir): |
| 51 | + """Hyphens should be ignored during matching.""" |
| 52 | + self.assertTrue(is_valid_plugin("blue ocean")) |
| 53 | + self.assertTrue(is_valid_plugin("blueocean")) |
| 54 | + self.assertTrue(is_valid_plugin("Blue-Ocean")) |
| 55 | + |
| 56 | + @patch( |
| 57 | + "builtins.open", |
| 58 | + mock_open(read_data=SAMPLE_PLUGINS), |
| 59 | + ) |
| 60 | + @patch("os.path.dirname", return_value="/fake/dir") |
| 61 | + def test_invalid_plugin_returns_false(self, _mock_dir): |
| 62 | + """Non-existent plugin name should return False.""" |
| 63 | + self.assertFalse(is_valid_plugin("nonexistent-plugin")) |
| 64 | + self.assertFalse(is_valid_plugin("")) |
| 65 | + |
| 66 | + |
| 67 | +class TestFilterRetrievedData(unittest.TestCase): |
| 68 | + """Tests for filter_retrieved_data using the shared tokenizer.""" |
| 69 | + |
| 70 | + def test_filters_matching_entries(self): |
| 71 | + """Only entries whose title matches the plugin name should remain.""" |
| 72 | + semantic_data = [ |
| 73 | + {"metadata": {"title": "blue-ocean"}, "chunk_text": "a"}, |
| 74 | + {"metadata": {"title": "credentials"}, "chunk_text": "b"}, |
| 75 | + ] |
| 76 | + keyword_data = [ |
| 77 | + {"metadata": {"title": "Blue Ocean"}, "chunk_text": "c"}, |
| 78 | + {"metadata": {"title": "git"}, "chunk_text": "d"}, |
| 79 | + ] |
| 80 | + sem, kw = filter_retrieved_data( |
| 81 | + semantic_data, keyword_data, "blue-ocean" |
| 82 | + ) |
| 83 | + self.assertEqual(len(sem), 1) |
| 84 | + self.assertEqual(sem[0]["chunk_text"], "a") |
| 85 | + self.assertEqual(len(kw), 1) |
| 86 | + self.assertEqual(kw[0]["chunk_text"], "c") |
| 87 | + |
| 88 | + def test_returns_empty_when_no_match(self): |
| 89 | + """No results should be returned when nothing matches.""" |
| 90 | + data = [{"metadata": {"title": "git"}, "chunk_text": "x"}] |
| 91 | + sem, kw = filter_retrieved_data(data, data, "nonexistent") |
| 92 | + self.assertEqual(len(sem), 0) |
| 93 | + self.assertEqual(len(kw), 0) |
| 94 | + |
| 95 | + def test_empty_input(self): |
| 96 | + """Empty input lists should return empty lists.""" |
| 97 | + sem, kw = filter_retrieved_data([], [], "git") |
| 98 | + self.assertEqual(sem, []) |
| 99 | + self.assertEqual(kw, []) |
| 100 | + |
| 101 | + |
| 102 | +class TestPluginNameCacheIntegration(unittest.TestCase): |
| 103 | + """Integration tests verifying lru_cache behaviour through the public API. |
| 104 | +
|
| 105 | + These tests confirm that plugin_names.json is read from disk exactly |
| 106 | + once, regardless of how many times public functions that depend on the |
| 107 | + cached data are called. |
| 108 | + """ |
| 109 | + |
| 110 | + def setUp(self): |
| 111 | + load_plugin_names.cache_clear() |
| 112 | + |
| 113 | + def tearDown(self): |
| 114 | + load_plugin_names.cache_clear() |
| 115 | + |
| 116 | + @patch("os.path.dirname", return_value="/fake/dir") |
| 117 | + def test_multiple_is_valid_plugin_calls_read_file_once(self, _mock_dir): |
| 118 | + """Repeated is_valid_plugin() calls should hit the cache, not disk.""" |
| 119 | + with patch( |
| 120 | + "builtins.open", mock_open(read_data=SAMPLE_PLUGINS) |
| 121 | + ) as mocked_file: |
| 122 | + is_valid_plugin("git") |
| 123 | + is_valid_plugin("blue-ocean") |
| 124 | + is_valid_plugin("nonexistent") |
| 125 | + mocked_file.assert_called_once() |
| 126 | + |
| 127 | + @patch("os.path.dirname", return_value="/fake/dir") |
| 128 | + def test_cache_shared_across_public_functions(self, _mock_dir): |
| 129 | + """is_valid_plugin and load_plugin_names should share the same cache.""" |
| 130 | + with patch( |
| 131 | + "builtins.open", mock_open(read_data=SAMPLE_PLUGINS) |
| 132 | + ) as mocked_file: |
| 133 | + # First access via is_valid_plugin populates the cache |
| 134 | + is_valid_plugin("git") |
| 135 | + # Direct call should reuse the cached result |
| 136 | + result = load_plugin_names() |
| 137 | + self.assertIsInstance(result, frozenset) |
| 138 | + mocked_file.assert_called_once() |
| 139 | + |
| 140 | + @patch("os.path.dirname", return_value="/fake/dir") |
| 141 | + def test_cache_clear_forces_reload(self, _mock_dir): |
| 142 | + """After cache_clear(), the next call should re-read the file.""" |
| 143 | + with patch( |
| 144 | + "builtins.open", mock_open(read_data=SAMPLE_PLUGINS) |
| 145 | + ) as mocked_file: |
| 146 | + is_valid_plugin("git") |
| 147 | + self.assertEqual(mocked_file.call_count, 1) |
| 148 | + |
| 149 | + load_plugin_names.cache_clear() |
| 150 | + |
| 151 | + is_valid_plugin("git") |
| 152 | + self.assertEqual(mocked_file.call_count, 2) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + unittest.main() |
0 commit comments