|
3 | 3 | Test plugin functionality |
4 | 4 | """ |
5 | 5 |
|
6 | | -import pytest |
7 | 6 | import sys |
8 | 7 | import os |
| 8 | +import importlib |
| 9 | + |
| 10 | +# Try to import pytest, but don't fail if it's not available |
| 11 | +try: |
| 12 | + import pytest |
| 13 | +except ImportError: |
| 14 | + pytest = None |
| 15 | + |
9 | 16 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
10 | 17 |
|
11 | | -from optillm.plugins import load_plugin, is_plugin_approach |
12 | | -from optillm.plugins.memory_plugin import should_enable_memory |
| 18 | +from optillm import plugin_approaches, load_plugins |
13 | 19 |
|
14 | 20 |
|
15 | | -def test_plugin_loading(): |
16 | | - """Test loading plugins""" |
17 | | - # Test loading a known plugin |
18 | | - plugin = load_plugin("memory") |
19 | | - assert plugin is not None |
20 | | - assert hasattr(plugin, 'run') |
| 21 | +def test_plugin_module_imports(): |
| 22 | + """Test that plugin modules can be imported""" |
| 23 | + plugin_modules = [ |
| 24 | + 'optillm.plugins.memory_plugin', |
| 25 | + 'optillm.plugins.readurls_plugin', |
| 26 | + 'optillm.plugins.privacy_plugin', |
| 27 | + 'optillm.plugins.genselect_plugin', |
| 28 | + 'optillm.plugins.majority_voting_plugin' |
| 29 | + ] |
21 | 30 |
|
22 | | - # Test loading non-existent plugin returns None |
23 | | - plugin = load_plugin("nonexistent") |
24 | | - assert plugin is None |
| 31 | + for module_name in plugin_modules: |
| 32 | + try: |
| 33 | + module = importlib.import_module(module_name) |
| 34 | + assert hasattr(module, 'run'), f"{module_name} missing 'run' function" |
| 35 | + assert hasattr(module, 'SLUG'), f"{module_name} missing 'SLUG' attribute" |
| 36 | + except ImportError as e: |
| 37 | + if pytest: |
| 38 | + pytest.fail(f"Failed to import {module_name}: {e}") |
| 39 | + else: |
| 40 | + raise AssertionError(f"Failed to import {module_name}: {e}") |
25 | 41 |
|
26 | 42 |
|
27 | | -def test_is_plugin_approach(): |
28 | | - """Test plugin approach detection""" |
29 | | - # Known plugins |
30 | | - assert is_plugin_approach("memory") == True |
31 | | - assert is_plugin_approach("readurls") == True |
32 | | - assert is_plugin_approach("privacy") == True |
| 43 | +def test_plugin_approach_detection(): |
| 44 | + """Test plugin approach detection after loading""" |
| 45 | + # Load plugins first |
| 46 | + load_plugins() |
33 | 47 |
|
34 | | - # Non-plugins |
35 | | - assert is_plugin_approach("mcts") == False |
36 | | - assert is_plugin_approach("bon") == False |
37 | | - assert is_plugin_approach("nonexistent") == False |
| 48 | + # Check if known plugins are loaded |
| 49 | + expected_plugins = ["memory", "readurls", "privacy"] |
| 50 | + for plugin_name in expected_plugins: |
| 51 | + assert plugin_name in plugin_approaches, f"Plugin {plugin_name} not loaded" |
38 | 52 |
|
39 | 53 |
|
40 | | -def test_memory_plugin_detection(): |
41 | | - """Test memory plugin auto-detection""" |
42 | | - # Test with context length exceeding threshold |
43 | | - long_context = "x" * 500000 # 500k chars |
44 | | - assert should_enable_memory(long_context) == True |
45 | | - |
46 | | - # Test with short context |
47 | | - short_context = "Hello world" |
48 | | - assert should_enable_memory(short_context) == False |
49 | | - |
50 | | - # Test with explicit false in config |
51 | | - assert should_enable_memory(long_context, {"memory": False}) == False |
52 | | - |
53 | | - # Test with explicit true in config |
54 | | - assert should_enable_memory(short_context, {"memory": True}) == True |
| 54 | +def test_memory_plugin_structure(): |
| 55 | + """Test memory plugin has required structure""" |
| 56 | + import optillm.plugins.memory_plugin as plugin |
| 57 | + assert hasattr(plugin, 'run') |
| 58 | + assert hasattr(plugin, 'SLUG') |
| 59 | + assert plugin.SLUG == "memory" |
| 60 | + assert hasattr(plugin, 'Memory') # Check for Memory class |
55 | 61 |
|
56 | 62 |
|
57 | 63 | def test_genselect_plugin(): |
58 | | - """Test genselect plugin exists""" |
59 | | - plugin = load_plugin("genselect") |
60 | | - assert plugin is not None |
| 64 | + """Test genselect plugin module""" |
| 65 | + import optillm.plugins.genselect_plugin as plugin |
61 | 66 | assert hasattr(plugin, 'run') |
| 67 | + assert hasattr(plugin, 'SLUG') |
62 | 68 | assert hasattr(plugin, 'DEFAULT_NUM_CANDIDATES') |
| 69 | + assert plugin.SLUG == "genselect" |
63 | 70 |
|
64 | 71 |
|
65 | 72 | def test_majority_voting_plugin(): |
66 | | - """Test majority voting plugin""" |
67 | | - plugin = load_plugin("majority_voting") |
68 | | - assert plugin is not None |
| 73 | + """Test majority voting plugin module""" |
| 74 | + import optillm.plugins.majority_voting_plugin as plugin |
69 | 75 | assert hasattr(plugin, 'run') |
| 76 | + assert hasattr(plugin, 'SLUG') |
70 | 77 | assert hasattr(plugin, 'extract_answer') |
71 | 78 | assert hasattr(plugin, 'normalize_answer') |
| 79 | + assert plugin.SLUG == "majority_voting" |
72 | 80 |
|
73 | 81 |
|
74 | 82 | if __name__ == "__main__": |
75 | 83 | print("Running plugin tests...") |
76 | 84 |
|
77 | 85 | try: |
78 | | - test_plugin_loading() |
79 | | - print("✅ Plugin loading test passed") |
| 86 | + test_plugin_module_imports() |
| 87 | + print("✅ Plugin module imports test passed") |
80 | 88 | except Exception as e: |
81 | | - print(f"❌ Plugin loading test failed: {e}") |
| 89 | + print(f"❌ Plugin module imports test failed: {e}") |
82 | 90 |
|
83 | 91 | try: |
84 | | - test_is_plugin_approach() |
| 92 | + test_plugin_approach_detection() |
85 | 93 | print("✅ Plugin approach detection test passed") |
86 | 94 | except Exception as e: |
87 | 95 | print(f"❌ Plugin approach detection test failed: {e}") |
88 | 96 |
|
89 | 97 | try: |
90 | | - test_memory_plugin_detection() |
91 | | - print("✅ Memory plugin detection test passed") |
| 98 | + test_memory_plugin_structure() |
| 99 | + print("✅ Memory plugin structure test passed") |
92 | 100 | except Exception as e: |
93 | | - print(f"❌ Memory plugin detection test failed: {e}") |
| 101 | + print(f"❌ Memory plugin structure test failed: {e}") |
94 | 102 |
|
95 | 103 | try: |
96 | 104 | test_genselect_plugin() |
|
0 commit comments