-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_advanced_features.py
More file actions
303 lines (233 loc) · 10.6 KB
/
test_advanced_features.py
File metadata and controls
303 lines (233 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# tests/test_advanced_features.py
from pathlib import Path
import shutil
import unittest
import tempfile
import json
from shellrosetta.parser import CommandParser, ASTNode, NodeType
from shellrosetta.plugins import CommandPlugin, PluginManager
from shellrosetta.ml_engine import MLEngine, CommandPattern
from shellrosetta.core import lnx2ps, ps2lnx
class TestCommandParser(unittest.TestCase):
"""Test the advanced command parser"""
def setUp(self):
self.parser = CommandParser()
def test_parse_simple_command(self):
"""Test parsing a simple command"""
ast = self.parser.parse("ls -la")
self.assertEqual(ast.node_type, NodeType.COMMAND)
self.assertEqual(ast.value, "ls")
self.assertEqual(len(ast.children), 1)
self.assertEqual(ast.children[0].node_type, NodeType.FLAG)
self.assertEqual(ast.children[0].value, "-la")
def test_parse_piped_commands(self):
"""Test parsing piped commands"""
ast = self.parser.parse("ls -la | grep error")
self.assertEqual(ast.node_type, NodeType.PIPE)
self.assertEqual(len(ast.children), 2)
# First command
self.assertEqual(ast.children[0].node_type, NodeType.COMMAND)
self.assertEqual(ast.children[0].value, "ls")
# Second command
self.assertEqual(ast.children[1].node_type, NodeType.COMMAND)
self.assertEqual(ast.children[1].value, "grep")
def test_extract_flags(self):
"""Test flag extraction"""
ast = self.parser.parse("ls -la -R")
flags = self.parser.extract_flags(ast)
self.assertIn("-la", flags)
self.assertIn("-R", flags)
def test_extract_arguments(self):
"""Test argument extraction"""
ast = self.parser.parse("cp file1.txt file2.txt")
args = self.parser.extract_arguments(ast)
self.assertIn("file1.txt", args)
self.assertIn("file2.txt", args)
def test_get_command_name(self):
"""Test command name extraction"""
ast = self.parser.parse("ls -la")
cmd_name = self.parser.get_command_name(ast)
self.assertEqual(cmd_name, "ls")
def test_empty_command(self):
"""Test parsing empty command"""
ast = self.parser.parse("")
self.assertEqual(ast.node_type, NodeType.COMMAND)
self.assertEqual(ast.value, "")
class TestPluginSystem(unittest.TestCase):
"""Test the plugin system"""
def setUp(self):
self.plugin_manager = PluginManager()
def test_plugin_creation(self):
"""Test creating a custom plugin"""
class TestPlugin(CommandPlugin):
def get_name(self):
return "test"
def get_version(self):
return "1.0.0"
def get_supported_commands(self):
return ["test_cmd"]
def translate(self, command, direction):
if "test_cmd" in command:
return "translated_command"
return None
plugin = TestPlugin()
self.assertEqual(plugin.get_name(), "test")
self.assertEqual(plugin.get_version(), "1.0.0")
self.assertEqual(plugin.get_supported_commands(), ["test_cmd"])
result = plugin.translate("test_cmd arg1", "lnx2ps")
self.assertEqual(result, "translated_command")
def test_plugin_manager_list_plugins(self):
"""Test listing plugins"""
plugins = self.plugin_manager.list_plugins()
# Should have at least the built-in plugins
self.assertIsInstance(plugins, list)
self.assertGreater(len(plugins), 0)
def test_plugin_translation(self):
"""Test plugin-based translation"""
# Test with a command that should be handled by a plugin
result = self.plugin_manager.translate_with_plugins("docker ps", "lnx2ps")
# Should return the same command (Docker commands are cross-platform)
# Note: This might return None if no plugin handles it exactly
self.assertIsNotNone(result)
if result:
self.assertEqual(result, "docker ps")
class TestMLEngine(unittest.TestCase):
"""Test the machine learning engine"""
def setUp(self):
# Use a temporary directory for testing
self.temp_dir = tempfile.mkdtemp()
self.ml_engine = MLEngine()
self.ml_engine.data_dir = Path(self.temp_dir)
self.ml_engine.patterns_file = self.ml_engine.data_dir / "patterns.json"
self.ml_engine.context_file = self.ml_engine.data_dir / "context.json"
self.ml_engine.suggestions_file = self.ml_engine.data_dir / "suggestions.json"
def tearDown(self):
shutil.rmtree(self.temp_dir)
def test_learn_pattern(self):
"""Test learning a new pattern"""
# Clear existing patterns for this test
self.ml_engine.patterns.clear()
self.ml_engine.learn_pattern(
"ls -la", "Get-ChildItem -Force | Format-List", "lnx2ps", success=True
)
# Check if pattern was learned
key = "lnx2ps:ls -la"
self.assertIn(key, self.ml_engine.patterns)
pattern = self.ml_engine.patterns[key]
self.assertEqual(pattern.command, "ls -la")
self.assertEqual(pattern.translation, "Get-ChildItem -Force | Format-List")
self.assertEqual(pattern.success_count, 1)
self.assertEqual(pattern.failure_count, 0)
def test_get_best_translation(self):
"""Test getting the best learned translation"""
# Learn a pattern
self.ml_engine.learn_pattern(
"ls -la", "Get-ChildItem -Force | Format-List", "lnx2ps", success=True
)
# Get the translation
result = self.ml_engine.get_best_translation("ls -la", "lnx2ps")
self.assertEqual(result, "Get-ChildItem -Force | Format-List")
def test_get_suggestions(self):
"""Test getting suggestions for partial commands"""
# Learn some patterns
self.ml_engine.learn_pattern(
"ls -la", "Get-ChildItem -Force | Format-List", "lnx2ps", success=True
)
self.ml_engine.learn_pattern(
"ls -lh", "Get-ChildItem | Format-List", "lnx2ps", success=True
)
# Get suggestions
suggestions = self.ml_engine.get_suggestions("ls -l", "lnx2ps", limit=5)
self.assertIsInstance(suggestions, list)
self.assertGreater(len(suggestions), 0)
def test_analyze_patterns(self):
"""Test pattern analysis"""
# Clear existing patterns for this test
self.ml_engine.patterns.clear()
# Learn some patterns
self.ml_engine.learn_pattern(
"ls -la", "Get-ChildItem -Force | Format-List", "lnx2ps", success=True
)
self.ml_engine.learn_pattern(
"ls -lh", "Get-ChildItem | Format-List", "lnx2ps", success=True
)
# Analyze patterns
analysis = self.ml_engine.analyze_patterns()
self.assertIn("total_patterns", analysis)
self.assertIn("success_rate", analysis)
self.assertIn("command_types", analysis)
self.assertEqual(analysis["total_patterns"], 2)
self.assertEqual(analysis["success_rate"], 1.0)
def test_command_classification(self):
"""Test command type classification"""
self.assertEqual(self.ml_engine._classify_command("ls -la"), "file_listing")
self.assertEqual(self.ml_engine._classify_command("grep error"), "search")
self.assertEqual(
self.ml_engine._classify_command("cp file1 file2"), "file_operation"
)
self.assertEqual(self.ml_engine._classify_command("docker ps"), "container")
self.assertEqual(
self.ml_engine._classify_command("git commit"), "version_control"
)
self.assertEqual(self.ml_engine._classify_command("unknown command"), "general")
class TestEnhancedCore(unittest.TestCase):
"""Test the enhanced core functions with ML and plugins"""
def test_lnx2ps_with_ml(self):
"""Test Linux to PowerShell translation with ML enabled"""
# This should work with the enhanced core
result = lnx2ps("ls -la", use_ml=True, use_plugins=True)
self.assertIn("Get-ChildItem", result)
def test_ps2lnx_with_ml(self):
"""Test PowerShell to Linux translation with ML enabled"""
# This should work with the enhanced core
result = ps2lnx("Get-ChildItem -Force", use_ml=True, use_plugins=True)
self.assertIn("ls", result)
def test_lnx2ps_without_ml(self):
"""Test Linux to PowerShell translation with ML disabled"""
result = lnx2ps("ls -la", use_ml=False, use_plugins=False)
self.assertIn("Get-ChildItem", result)
def test_ps2lnx_without_ml(self):
"""Test PowerShell to Linux translation with ML disabled"""
result = ps2lnx("Get-ChildItem -Force", use_ml=False, use_plugins=False)
self.assertIn("ls", result)
class TestCommandPattern(unittest.TestCase):
"""Test the CommandPattern class"""
def test_pattern_creation(self):
"""Test creating a command pattern"""
pattern = CommandPattern(
"ls -la", "Get-ChildItem -Force | Format-List", "lnx2ps"
)
self.assertEqual(pattern.command, "ls -la")
self.assertEqual(pattern.translation, "Get-ChildItem -Force | Format-List")
self.assertEqual(pattern.direction, "lnx2ps")
self.assertEqual(pattern.success_count, 0)
self.assertEqual(pattern.failure_count, 0)
def test_success_rate_calculation(self):
"""Test success rate calculation"""
pattern = CommandPattern("test", "translation", "lnx2ps")
# Initially 0
self.assertEqual(pattern.get_success_rate(), 0.0)
# After success
pattern.record_success()
self.assertEqual(pattern.get_success_rate(), 1.0)
# After failure
pattern.record_failure()
self.assertEqual(pattern.get_success_rate(), 0.5)
def test_pattern_serialization(self):
"""Test pattern serialization and deserialization"""
pattern = CommandPattern(
"ls -la", "Get-ChildItem -Force | Format-List", "lnx2ps"
)
pattern.record_success()
pattern.record_failure()
# Serialize
data = pattern.to_dict()
# Deserialize
new_pattern = CommandPattern.from_dict(data)
self.assertEqual(new_pattern.command, pattern.command)
self.assertEqual(new_pattern.translation, pattern.translation)
self.assertEqual(new_pattern.direction, pattern.direction)
self.assertEqual(new_pattern.success_count, pattern.success_count)
self.assertEqual(new_pattern.failure_count, pattern.failure_count)
if __name__ == "__main__":
unittest.main()