-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-code-execution.js
More file actions
143 lines (118 loc) · 3.58 KB
/
Copy pathtest-code-execution.js
File metadata and controls
143 lines (118 loc) · 3.58 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
#!/usr/bin/env node
/**
* Test code execution MCP handler
*/
import { CodeExecutionHandler } from '../dist/integrations/mcp/handlers/code-execution-handlers.js';
async function testPythonExecution() {
console.log('🐍 Testing Python Code Execution\n');
const handler = new CodeExecutionHandler();
// Test 1: Simple Python code
console.log('Test 1: Simple calculation');
const result1 = await handler.executeCode({
language: 'python',
code: `
import math
def calculate_circle_area(radius):
return math.pi * radius ** 2
# Test the function
radius = 5
area = calculate_circle_area(radius)
print(f"Circle with radius {radius} has area: {area:.2f}")
# Generate some data
data = [i**2 for i in range(10)]
print(f"Squares: {data}")
`,
});
console.log('Success:', result1.success);
console.log('Output:', result1.stdout);
if (result1.stderr) console.log('Errors:', result1.stderr);
console.log('---\n');
// Test 2: Code with warnings
console.log('Test 2: Code with security warnings');
const code2 = `
import os
import subprocess
# This should trigger warnings
print("Current directory:", os.getcwd())
`;
const validation = handler.validateCode(code2);
console.log('Validation result:', validation);
if (!validation.safe) {
console.log('Executing anyway with force flag...');
const result2 = await handler.executeCode({
language: 'python',
code: code2,
});
console.log('Output:', result2.stdout);
}
console.log('---\n');
// Test 3: JavaScript execution
console.log('Test 3: JavaScript code');
const result3 = await handler.executeCode({
language: 'javascript',
code: `
// Calculate fibonacci
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
for (let i = 0; i < 10; i++) {
console.log(\`fibonacci(\${i}) = \${fibonacci(i)}\`);
}
// Test async operation
setTimeout(() => {
console.log('Async operation completed');
}, 100);
// Wait a bit for async
new Promise(resolve => setTimeout(resolve, 200)).then(() => {
console.log('Promise resolved');
});
`,
});
console.log('Success:', result3.success);
console.log('Output:', result3.stdout);
console.log('---\n');
// Test 4: Long-running code with timeout
console.log('Test 4: Timeout test');
const result4 = await handler.executeCode({
language: 'python',
code: `
import time
print("Starting long operation...")
time.sleep(5) # This should timeout if timeout is < 5 seconds
print("This should not print")
`,
timeout: 2000, // 2 second timeout
});
console.log('Success:', result4.success);
console.log('Output:', result4.stdout);
console.log('Errors:', result4.stderr);
console.log('---\n');
// Test 5: Large output truncation
console.log('Test 5: Large output handling');
const result5 = await handler.executeCode({
language: 'python',
code: `
# Generate large output
for i in range(10000):
print(f"Line {i}: {'=' * 50}")
`,
});
console.log('Success:', result5.success);
console.log('Truncated:', result5.truncated);
if (result5.truncated) {
console.log('Output file:', result5.outputFile);
}
console.log('Output length:', result5.stdout.length);
console.log('---\n');
// Get sandbox status
const status = await handler.getSandboxStatus();
console.log('📊 Sandbox Status:', status);
// Clean sandbox
console.log('\n🧹 Cleaning sandbox...');
await handler.cleanSandbox();
const statusAfter = await handler.getSandboxStatus();
console.log('Sandbox after cleanup:', statusAfter);
}
// Run tests
testPythonExecution().catch(console.error);