scalebox-sdk 0.1.13__py3-none-any.whl → 0.1.14__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3416 +0,0 @@
1
- #!/usr/bin/env python3
2
- """
3
- Comprehensive validation test for code_interpreter sync module.
4
-
5
- This test suite demonstrates and validates all key functionality of the CodeInterpreter:
6
- - Basic code execution (Python, shell commands)
7
- - Callback handling (stdout, stderr, result, error)
8
- - Context management (create, persist, destroy)
9
- - Error handling and edge cases
10
- - Performance testing
11
- - Different data types and formats
12
- """
13
-
14
- import datetime
15
- import json
16
- import logging
17
- import os
18
- import tempfile
19
- import time
20
- from io import StringIO
21
- from typing import Any, Dict, List, Optional
22
-
23
- from e2b_code_interpreter import (
24
- Context,
25
- Execution,
26
- ExecutionError,
27
- Logs,
28
- OutputMessage,
29
- Result,
30
- Sandbox,
31
- )
32
-
33
- # from scalebox.code_interpreter import Sandbox, Context, Execution, ExecutionError, Result, OutputMessage, Logs
34
-
35
- # 配置日志
36
- logging.basicConfig(
37
- level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
38
- )
39
- logger = logging.getLogger(__name__)
40
-
41
-
42
- class CodeInterpreterValidator:
43
- """Comprehensive CodeInterpreter validation test suite."""
44
-
45
- def __init__(self):
46
- self.sandbox: Optional[Sandbox] = None
47
- self.test_results = []
48
- self.failed_tests = []
49
- self.contexts: Dict[str, Context] = {}
50
-
51
- def log_test_result(
52
- self, test_name: str, success: bool, message: str = "", duration: float = 0
53
- ):
54
- """记录测试结果"""
55
- status = "✅ PASS" if success else "❌ FAIL"
56
- result = {
57
- "test": test_name,
58
- "success": success,
59
- "message": message,
60
- "duration": duration,
61
- }
62
- self.test_results.append(result)
63
-
64
- if not success:
65
- self.failed_tests.append(test_name)
66
-
67
- logger.info(f"{status} {test_name} ({duration:.3f}s) {message}")
68
-
69
- def run_test(self, test_func, test_name: str):
70
- """运行单个测试并记录结果"""
71
- start_time = time.time()
72
- try:
73
- test_func()
74
- duration = time.time() - start_time
75
- self.log_test_result(test_name, True, duration=duration)
76
- except Exception as e:
77
- duration = time.time() - start_time
78
- self.log_test_result(test_name, False, str(e), duration=duration)
79
-
80
- # ======================== 基础代码解释器操作测试 ========================
81
-
82
- def test_code_interpreter_creation(self):
83
- """测试代码解释器创建"""
84
- # self.sandbox = Sandbox(
85
- # template="code-interpreter",
86
- # timeout=600,
87
- # debug=True,
88
- # metadata={"test": "code_interpreter_validation"},
89
- # envs={"CI_TEST": "sync_test"}
90
- # )
91
- self.sandbox = Sandbox.create()
92
-
93
- # time.sleep(5)
94
- assert self.sandbox is not None
95
- assert self.sandbox.sandbox_id is not None
96
- logger.info(
97
- f"Created CodeInterpreter sandbox with ID: {self.sandbox.sandbox_id}"
98
- )
99
-
100
- def test_basic_python_execution(self):
101
- """测试基础Python代码执行"""
102
- assert self.sandbox is not None
103
-
104
- code = """
105
- print("Hello, CodeInterpreter!")
106
- x = 1 + 2
107
- y = x * 3
108
- print(f"计算结果: x={x}, y={y}")
109
- result = {"x": x, "y": y}
110
- print(result)
111
- """
112
-
113
- execution = self.sandbox.run_code(code, language="python")
114
- print(execution.to_json())
115
- assert isinstance(execution, Execution)
116
- assert execution.error is None
117
- assert len(execution.logs.stdout) > 0
118
- assert "Hello, CodeInterpreter!" in execution.logs.stdout[0]
119
- logger.info(f"Python execution stdout: {execution.logs.stdout}")
120
-
121
- def test_math_calculations(self):
122
- """测试数学计算"""
123
- assert self.sandbox is not None
124
-
125
- code = """
126
- import math
127
- import numpy as np
128
-
129
- # 基础数学运算
130
- circle_radius = 5
131
- area = math.pi * circle_radius ** 2
132
- circumference = 2 * math.pi * circle_radius
133
-
134
- print(f"圆的半径: {circle_radius}")
135
- print(f"圆的面积: {area:.2f}")
136
- print(f"圆的周长: {circumference:.2f}")
137
-
138
- # 使用numpy进行计算
139
- arr = np.array([1, 2, 3, 4, 5])
140
- mean_val = np.mean(arr)
141
- std_val = np.std(arr)
142
-
143
- print(f"数组: {arr}")
144
- print(f"平均值: {mean_val}")
145
- print(f"标准差: {std_val:.3f}")
146
-
147
- # 返回结果
148
- {
149
- "circle": {"radius": circle_radius, "area": area, "circumference": circumference},
150
- "array_stats": {"mean": mean_val, "std": std_val}
151
- }
152
- """
153
-
154
- execution = self.sandbox.run_code(code, language="python")
155
- print(execution.to_json())
156
- assert execution.error is None
157
- assert any("圆的面积" in line for line in execution.logs.stdout)
158
- logger.info("Math calculations completed successfully")
159
-
160
- def test_data_processing(self):
161
- """测试数据处理"""
162
- assert self.sandbox is not None
163
-
164
- code = """
165
- import pandas as pd
166
- import json
167
-
168
- # 创建示例数据
169
- data = {
170
- 'name': ['Alice', 'Bob', 'Charlie', 'Diana'],
171
- 'age': [25, 30, 35, 28],
172
- 'city': ['New York', 'London', 'Tokyo', 'Paris'],
173
- 'salary': [50000, 75000, 80000, 65000]
174
- }
175
-
176
- df = pd.DataFrame(data)
177
- print("原始数据:")
178
- print(df)
179
-
180
- # 数据处理
181
- avg_age = df['age'].mean()
182
- avg_salary = df['salary'].mean()
183
- city_counts = df['city'].value_counts()
184
-
185
- print(f"\\n平均年龄: {avg_age}")
186
- print(f"平均工资: {avg_salary}")
187
- print(f"城市分布: {city_counts.to_dict()}")
188
-
189
- # 筛选数据
190
- high_earners = df[df['salary'] > 60000]
191
- print(f"\\n高收入人员:")
192
- print(high_earners)
193
-
194
- result = {
195
- "total_people": len(df),
196
- "avg_age": avg_age,
197
- "avg_salary": avg_salary,
198
- "high_earners": len(high_earners),
199
- "cities": city_counts.to_dict()
200
- }
201
- print(f"\\n处理结果: {json.dumps(result, indent=2)}")
202
- """
203
-
204
- execution = self.sandbox.run_code(code, language="python")
205
- print(execution.to_json())
206
- assert execution.error is None
207
- assert any("原始数据" in line for line in execution.logs.stdout)
208
- assert any("平均年龄" in line for line in execution.logs.stdout)
209
-
210
- def test_visualization_code(self):
211
- """测试数据可视化代码"""
212
- assert self.sandbox is not None
213
-
214
- code = """
215
- import matplotlib.pyplot as plt
216
- import numpy as np
217
- import base64
218
- import io
219
-
220
- # 创建数据
221
- x = np.linspace(0, 10, 100)
222
- y1 = np.sin(x)
223
- y2 = np.cos(x)
224
-
225
- # 创建图表
226
- fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))
227
-
228
- # 上子图:正弦和余弦
229
- ax1.plot(x, y1, 'b-', label='sin(x)', linewidth=2)
230
- ax1.plot(x, y2, 'r--', label='cos(x)', linewidth=2)
231
- ax1.set_title('三角函数')
232
- ax1.set_xlabel('x')
233
- ax1.set_ylabel('y')
234
- ax1.legend()
235
- ax1.grid(True)
236
-
237
- # 下子图:散点图
238
- np.random.seed(42)
239
- x_scatter = np.random.randn(100)
240
- y_scatter = np.random.randn(100)
241
- ax2.scatter(x_scatter, y_scatter, alpha=0.6, c='green')
242
- ax2.set_title('随机散点图')
243
- ax2.set_xlabel('X')
244
- ax2.set_ylabel('Y')
245
- ax2.grid(True)
246
-
247
- plt.tight_layout()
248
-
249
- # 保存图表为base64编码的字符串
250
- buffer = io.BytesIO()
251
- plt.savefig(buffer, format='png', dpi=100, bbox_inches='tight')
252
- buffer.seek(0)
253
- image_base64 = base64.b64encode(buffer.getvalue()).decode()
254
- buffer.close()
255
- plt.close()
256
-
257
- print(f"图表已生成,大小: {len(image_base64)} 字符")
258
- print("图表包含正弦、余弦函数和随机散点图")
259
-
260
- # 返回结果信息
261
- {"image_size": len(image_base64), "charts": ["sin/cos functions", "random scatter"]}
262
- """
263
-
264
- execution = self.sandbox.run_code(code, language="python")
265
- print(execution.to_json())
266
- assert execution.error is None
267
- assert any("图表已生成" in line for line in execution.logs.stdout)
268
-
269
- # ======================== 回调函数测试 ========================
270
-
271
- def test_callback_handling(self):
272
- """测试回调函数处理"""
273
- assert self.sandbox is not None
274
-
275
- stdout_messages = []
276
- stderr_messages = []
277
- results = []
278
- errors = []
279
-
280
- def stdout_callback(msg: OutputMessage):
281
- stdout_messages.append(msg.content)
282
- logger.info(f"STDOUT: {msg.content}")
283
-
284
- def stderr_callback(msg: OutputMessage):
285
- stderr_messages.append(msg.content)
286
- logger.info(f"STDERR: {msg.content}")
287
-
288
- def result_callback(result: Result):
289
- results.append(result)
290
- logger.info(f"RESULT: {result}")
291
-
292
- def error_callback(error: ExecutionError):
293
- errors.append(error)
294
- logger.info(f"ERROR: {error.name} - {error.value}")
295
-
296
- code = """
297
- import sys
298
-
299
- print("这是标准输出消息")
300
- print("另一条标准输出", file=sys.stdout)
301
- print("这是标准错误消息", file=sys.stderr)
302
-
303
- result_data = {"status": "completed", "value": 42}
304
- print(f"最终结果: {result_data}")
305
-
306
- result_data # 返回结果
307
- """
308
-
309
- execution = self.sandbox.run_code(
310
- code,
311
- language="python",
312
- on_stdout=stdout_callback,
313
- on_stderr=stderr_callback,
314
- on_result=result_callback,
315
- on_error=error_callback,
316
- )
317
- print(execution.to_json())
318
- assert execution.error is None
319
- # 注意:回调可能在执行完成后才触发
320
- logger.info(
321
- f"Callback test completed. stdout: {len(stdout_messages)}, stderr: {len(stderr_messages)}"
322
- )
323
-
324
- def test_error_handling(self):
325
- """测试错误处理"""
326
- assert self.sandbox is not None
327
-
328
- error_messages = []
329
-
330
- def error_callback(error: ExecutionError):
331
- error_messages.append(error)
332
- logger.info(f"捕获错误: {error.name} - {error.value}")
333
-
334
- # 测试语法错误
335
- code_syntax_error = """
336
- print("开始执行")
337
- invalid syntax here # 这里有语法错误
338
- print("这行不会执行")
339
- """
340
-
341
- execution = self.sandbox.run_code(
342
- code_syntax_error, language="python", on_error=error_callback
343
- )
344
- assert execution.error is not None
345
- assert execution.error.name in ["SyntaxError", "ParseError"]
346
- logger.info(f"正确捕获语法错误: {execution.error.name}")
347
-
348
- # 测试运行时错误
349
- code_runtime_error = """
350
- print("开始执行")
351
- x = 10
352
- y = 0
353
- result = x / y # 除零错误
354
- print(f"结果: {result}")
355
- """
356
-
357
- execution2 = self.sandbox.run_code(
358
- code_runtime_error, language="python", on_error=error_callback
359
- )
360
- print(execution.to_json())
361
- assert execution2.error is not None
362
- assert "ZeroDivisionError" in execution2.error.name
363
- logger.info(f"正确捕获运行时错误: {execution2.error.name}")
364
-
365
- # ======================== 上下文管理测试 ========================
366
-
367
- def test_context_creation(self):
368
- """测试上下文创建"""
369
- assert self.sandbox is not None
370
-
371
- # 创建Python上下文
372
- python_context = self.sandbox.create_code_context(language="python", cwd="/tmp")
373
- assert isinstance(python_context, Context)
374
- assert python_context.id is not None
375
- assert python_context.language == "python"
376
- self.contexts["python"] = python_context
377
- logger.info(f"Created Python context: {python_context.id}")
378
-
379
- # 测试完成后立即清理context
380
- try:
381
- self.sandbox.destroy_context(python_context)
382
- logger.info(f"Successfully destroyed context: {python_context.id}")
383
- # 从contexts字典中移除
384
- if "python" in self.contexts:
385
- del self.contexts["python"]
386
- except Exception as e:
387
- logger.warning(f"Failed to destroy context {python_context.id}: {e}")
388
-
389
- def test_context_persistence(self):
390
- """测试上下文状态持久性"""
391
- assert self.sandbox is not None
392
-
393
- # 创建新的上下文用于持久性测试
394
- context = self.sandbox.create_code_context(language="python", cwd="/tmp")
395
- self.contexts["persistence_test"] = context
396
-
397
- # 在上下文中定义变量
398
- code1 = """
399
- test_var = "Hello from context"
400
- numbers = [1, 2, 3, 4, 5]
401
- counter = 0
402
- print(f"定义了变量: test_var={test_var}, numbers={numbers}")
403
- """
404
-
405
- execution1 = self.sandbox.run_code(code1, context=context)
406
- print(execution1.to_json())
407
- assert execution1.error is None
408
-
409
- # 在同一上下文中使用之前定义的变量
410
- code2 = """
411
- print(f"从上下文读取: test_var={test_var}")
412
- counter += 10
413
- numbers.append(6)
414
- print(f"修改后: counter={counter}, numbers={numbers}")
415
- """
416
-
417
- execution2 = self.sandbox.run_code(code2, context=context)
418
- print(execution2.to_json())
419
- assert execution2.error is None
420
- assert any("从上下文读取" in line for line in execution2.logs.stdout)
421
- logger.info("Context persistence test passed")
422
-
423
- # 测试完成后立即清理context
424
- try:
425
- self.sandbox.destroy_context(context)
426
- logger.info(f"Successfully destroyed persistence context: {context.id}")
427
- # 从contexts字典中移除
428
- if "persistence_test" in self.contexts:
429
- del self.contexts["persistence_test"]
430
- except Exception as e:
431
- logger.warning(f"Failed to destroy persistence context {context.id}: {e}")
432
-
433
- def test_multiple_contexts(self):
434
- """测试多个上下文"""
435
- assert self.sandbox is not None
436
-
437
- # 创建两个独立的上下文
438
- context1 = self.sandbox.create_code_context(language="python", cwd="/tmp")
439
- context2 = self.sandbox.create_code_context(language="python", cwd="/home")
440
- self.contexts["multi_context1"] = context1
441
- self.contexts["multi_context2"] = context2
442
-
443
- # 在第一个上下文中设置变量
444
- code1 = """
445
- context_name = "context_1"
446
- shared_data = {"source": "context_1", "value": 100}
447
- print(f"在 {context_name} 中设置数据")
448
- """
449
-
450
- execution1 = self.sandbox.run_code(code1, context=context1)
451
- print(execution1.to_json())
452
- assert execution1.error is None
453
-
454
- # 在第二个上下文中设置不同的变量
455
- code2 = """
456
- context_name = "context_2"
457
- shared_data = {"source": "context_2", "value": 200}
458
- print(f"在 {context_name} 中设置数据")
459
- """
460
-
461
- execution2 = self.sandbox.run_code(code2, context=context2)
462
- print(execution2.to_json())
463
- assert execution2.error is None
464
-
465
- # 验证两个上下文的独立性
466
- verify_code = """
467
- print(f"当前上下文: {context_name}")
468
- print(f"数据: {shared_data}")
469
- """
470
-
471
- result1 = self.sandbox.run_code(verify_code, context=context1)
472
- print(result1.to_json())
473
- result2 = self.sandbox.run_code(verify_code, context=context2)
474
- print(result2.to_json())
475
- assert result1.error is None and result2.error is None
476
- logger.info("Multiple contexts test passed")
477
-
478
- # 测试完成后立即清理所有contexts
479
- contexts_to_destroy = [context1, context2]
480
- for context in contexts_to_destroy:
481
- try:
482
- self.sandbox.destroy_context(context)
483
- logger.info(f"Successfully destroyed multi-context: {context.id}")
484
- except Exception as e:
485
- logger.warning(f"Failed to destroy multi-context {context.id}: {e}")
486
-
487
- # 从contexts字典中移除
488
- if "multi_context1" in self.contexts:
489
- del self.contexts["multi_context1"]
490
- if "multi_context2" in self.contexts:
491
- del self.contexts["multi_context2"]
492
-
493
- # ======================== 数据类型和格式测试 ========================
494
-
495
- def test_different_data_types(self):
496
- """测试不同数据类型"""
497
- assert self.sandbox is not None
498
-
499
- code = """
500
- import json
501
- import datetime
502
- from decimal import Decimal
503
-
504
- # 测试各种数据类型
505
- test_data = {
506
- "string": "Hello, 世界!",
507
- "integer": 42,
508
- "float": 3.14159,
509
- "boolean": True,
510
- "none_value": None,
511
- "list": [1, 2, 3, "four", 5.0],
512
- "dict": {"nested": "value", "number": 123},
513
- "tuple": (1, 2, 3),
514
- "decimal": str(Decimal('123.456')),
515
- "datetime": datetime.datetime.now().isoformat()
516
- }
517
-
518
- print("数据类型测试:")
519
- for key, value in test_data.items():
520
- print(f" {key}: {value} ({type(value).__name__})")
521
-
522
- # JSON序列化测试
523
- json_str = json.dumps(test_data, ensure_ascii=False, indent=2)
524
- print(f"\\nJSON序列化长度: {len(json_str)}")
525
-
526
- # 返回测试数据
527
- test_data
528
- """
529
-
530
- execution = self.sandbox.run_code(code, language="python")
531
- print(execution.to_json())
532
- assert execution.error is None
533
- assert any("数据类型测试" in line for line in execution.logs.stdout)
534
-
535
- def test_file_operations_simulation(self):
536
- """测试文件操作(模拟)"""
537
- assert self.sandbox is not None
538
-
539
- code = """
540
- import tempfile
541
- import os
542
- import json
543
-
544
- # 创建临时文件并写入数据
545
- test_data = {
546
- "name": "CodeInterpreter Test",
547
- "timestamp": "2024-01-15 10:30:00",
548
- "data": [1, 2, 3, 4, 5],
549
- "status": "success"
550
- }
551
-
552
- # 写入文件
553
- temp_file = "/tmp/ci_test_file.json"
554
- with open(temp_file, 'w', encoding='utf-8') as f:
555
- json.dump(test_data, f, ensure_ascii=False, indent=2)
556
-
557
- print(f"数据已写入文件: {temp_file}")
558
-
559
- # 读取文件
560
- with open(temp_file, 'r', encoding='utf-8') as f:
561
- loaded_data = json.load(f)
562
-
563
- print("从文件读取的数据:")
564
- print(json.dumps(loaded_data, ensure_ascii=False, indent=2))
565
-
566
- # 验证数据一致性
567
- data_match = loaded_data == test_data
568
- print(f"\\n数据一致性检查: {data_match}")
569
-
570
- # 文件大小
571
- file_size = os.path.getsize(temp_file)
572
- print(f"文件大小: {file_size} 字节")
573
-
574
- # 清理
575
- os.remove(temp_file)
576
- print("临时文件已清理")
577
-
578
- {"file_size": file_size, "data_match": data_match}
579
- """
580
-
581
- execution = self.sandbox.run_code(code, language="python")
582
- print(execution.to_json())
583
- assert execution.error is None
584
- assert any("数据已写入文件" in line for line in execution.logs.stdout)
585
-
586
- # ======================== 性能测试 ========================
587
-
588
- def test_performance_simple_calculations(self):
589
- """测试简单计算性能"""
590
- assert self.sandbox is not None
591
-
592
- code = """
593
- import time
594
- import random
595
-
596
- print("开始性能测试...")
597
- start_time = time.time()
598
-
599
- # 执行大量简单计算
600
- total = 0
601
- for i in range(10000):
602
- total += i * 2 + random.randint(1, 10)
603
-
604
- mid_time = time.time()
605
- calculation_time = mid_time - start_time
606
-
607
- # 字符串操作
608
- text_data = []
609
- for i in range(1000):
610
- text_data.append(f"Item {i}: {random.choice(['A', 'B', 'C', 'D'])}")
611
-
612
- combined_text = " | ".join(text_data)
613
-
614
- end_time = time.time()
615
- string_time = end_time - mid_time
616
- total_time = end_time - start_time
617
-
618
- print(f"计算结果: {total}")
619
- print(f"字符串长度: {len(combined_text)}")
620
- print(f"计算时间: {calculation_time:.3f}s")
621
- print(f"字符串操作时间: {string_time:.3f}s")
622
- print(f"总时间: {total_time:.3f}s")
623
-
624
- {"total": total, "calculation_time": calculation_time, "string_time": string_time, "total_time": total_time}
625
- """
626
-
627
- start_test_time = time.time()
628
- execution = self.sandbox.run_code(code, language="python")
629
- print(execution.to_json())
630
- test_duration = time.time() - start_test_time
631
-
632
- assert execution.error is None
633
- assert any("开始性能测试" in line for line in execution.logs.stdout)
634
- logger.info(f"Performance test completed in {test_duration:.3f}s")
635
-
636
- # 性能断言
637
- assert test_duration < 30 # 整个测试应在30秒内完成
638
-
639
- def test_performance_concurrent_simulation(self):
640
- """测试并发模拟(使用线程)"""
641
- assert self.sandbox is not None
642
-
643
- code = """
644
- import threading
645
- import time
646
- import queue
647
-
648
- results_queue = queue.Queue()
649
-
650
- def worker_task(worker_id, iterations):
651
- '''模拟工作任务'''
652
- start_time = time.time()
653
- result = 0
654
-
655
- for i in range(iterations):
656
- result += i * worker_id
657
- # 模拟一些工作
658
- if i % 100 == 0:
659
- time.sleep(0.001)
660
-
661
- duration = time.time() - start_time
662
- results_queue.put({
663
- 'worker_id': worker_id,
664
- 'result': result,
665
- 'duration': duration
666
- })
667
- return result
668
-
669
- print("开始并发模拟测试...")
670
- start_time = time.time()
671
-
672
- # 创建多个线程
673
- threads = []
674
- num_workers = 5
675
- iterations_per_worker = 1000
676
-
677
- for i in range(num_workers):
678
- thread = threading.Thread(target=worker_task, args=(i, iterations_per_worker))
679
- threads.append(thread)
680
- thread.start()
681
-
682
- # 等待所有线程完成
683
- for thread in threads:
684
- thread.join()
685
-
686
- end_time = time.time()
687
- total_time = end_time - start_time
688
-
689
- # 收集结果
690
- results = []
691
- while not results_queue.empty():
692
- results.append(results_queue.get())
693
-
694
- print(f"\\n并发测试完成:")
695
- print(f"工作者数量: {num_workers}")
696
- print(f"每个工作者迭代: {iterations_per_worker}")
697
- print(f"总执行时间: {total_time:.3f}s")
698
-
699
- for result in results:
700
- print(f"工作者 {result['worker_id']}: 结果={result['result']}, 时间={result['duration']:.3f}s")
701
-
702
- {
703
- "num_workers": num_workers,
704
- "total_time": total_time,
705
- "avg_worker_time": sum(r['duration'] for r in results) / len(results),
706
- "results": results
707
- }
708
- """
709
-
710
- execution = self.sandbox.run_code(code, language="python")
711
- print(execution.to_json())
712
- assert execution.error is None
713
- assert any("并发测试完成" in line for line in execution.logs.stdout)
714
-
715
- # ======================== 结果格式测试 ========================
716
-
717
- def test_text_result(self):
718
- """测试文本格式结果"""
719
- assert self.sandbox is not None
720
-
721
- code = """
722
- # 生成纯文本结果
723
- text_content = '''
724
- 这是一个多行文本结果示例
725
- 包含各种信息:
726
- - 项目名称: CodeInterpreter
727
- - 版本: 1.0.0
728
- - 状态: 运行中
729
-
730
- 详细描述:
731
- 本系统能够执行Python代码并返回各种格式的结果,
732
- 支持文本、HTML、图像等多种输出格式。
733
- '''
734
-
735
- print("生成文本格式结果")
736
- text_content
737
- """
738
-
739
- execution = self.sandbox.run_code(code, language="python")
740
- print(execution.to_json())
741
- assert execution.error is None
742
- assert len(execution.results) > 0
743
-
744
- # 检查是否有文本结果
745
- for result in execution.results:
746
- if hasattr(result, "text") and result.text:
747
- logger.info(f"文本结果长度: {len(result.text)}")
748
- assert "CodeInterpreter" in result.text
749
-
750
- def test_html_result(self):
751
- """测试HTML格式结果"""
752
- assert self.sandbox is not None
753
-
754
- code = """
755
- # 生成HTML格式结果
756
- html_content = '''<!DOCTYPE html>
757
- <html>
758
- <head>
759
- <title>CodeInterpreter 测试报告</title>
760
- <style>
761
- body { font-family: Arial, sans-serif; margin: 20px; }
762
- .header { background-color: #4CAF50; color: white; padding: 15px; }
763
- .content { padding: 20px; }
764
- .status { color: #2196F3; font-weight: bold; }
765
- table { border-collapse: collapse; width: 100%; }
766
- th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
767
- th { background-color: #f2f2f2; }
768
- </style>
769
- </head>
770
- <body>
771
- <div class="header">
772
- <h1>🚀 CodeInterpreter 执行报告</h1>
773
- </div>
774
- <div class="content">
775
- <h2>执行概要</h2>
776
- <p class="status">状态: ✅ 成功</p>
777
- <p>执行时间: 2024-09-17 10:30:00</p>
778
-
779
- <h2>执行结果</h2>
780
- <table>
781
- <tr><th>指标</th><th>值</th><th>状态</th></tr>
782
- <tr><td>代码行数</td><td>25</td><td>✅</td></tr>
783
- <tr><td>执行时间</td><td>0.123s</td><td>✅</td></tr>
784
- <tr><td>内存使用</td><td>15.6MB</td><td>✅</td></tr>
785
- </table>
786
-
787
- <h2>详细信息</h2>
788
- <ul>
789
- <li>支持多种结果格式</li>
790
- <li>HTML渲染正常</li>
791
- <li>样式加载成功</li>
792
- </ul>
793
- </div>
794
- </body>
795
- </html>'''
796
-
797
- print("生成HTML格式结果")
798
- # 模拟返回HTML结果
799
- from IPython.display import HTML
800
- HTML(html_content)
801
- """
802
-
803
- execution = self.sandbox.run_code(code, language="python")
804
- print(execution.to_json())
805
- assert execution.error is None
806
- logger.info("HTML格式结果测试完成")
807
-
808
- def test_markdown_result(self):
809
- """测试Markdown格式结果"""
810
- assert self.sandbox is not None
811
-
812
- code = """
813
- # 生成Markdown格式结果
814
- markdown_content = '''# 📊 CodeInterpreter 测试报告
815
-
816
- ## 🎯 测试概述
817
-
818
- 这是一个**CodeInterpreter**的测试报告,展示了系统的各项功能。
819
-
820
- ### ✅ 测试结果
821
-
822
- | 测试项目 | 状态 | 执行时间 | 备注 |
823
- |---------|------|----------|------|
824
- | 基础执行 | ✅ 通过 | 0.123s | 正常 |
825
- | 数据处理 | ✅ 通过 | 0.456s | 正常 |
826
- | 图表生成 | ✅ 通过 | 0.789s | 正常 |
827
-
828
- ### 📈 性能指标
829
-
830
- - **CPU使用率**: 15.6%
831
- - **内存使用**: 128MB
832
- - **执行效率**: 优秀
833
-
834
- ### 🔧 功能特性
835
-
836
- 1. **多格式支持**
837
- - 支持文本输出
838
- - 支持HTML渲染
839
- - 支持图像生成
840
-
841
- 2. **数据处理能力**
842
- - Pandas数据分析
843
- - NumPy数值计算
844
- - Matplotlib可视化
845
-
846
- 3. **错误处理**
847
- - 语法错误捕获
848
- - 运行时异常处理
849
- - 详细错误信息
850
-
851
- ### 💡 使用示例
852
-
853
- ```python
854
- from scalebox.code_interpreter import Sandbox
855
-
856
- # 创建沙箱
857
- sandbox = Sandbox()
858
-
859
- # 执行代码
860
- result = sandbox.run_code(\"\"\"
861
- import pandas as pd
862
- df = pd.DataFrame({'a': [1, 2, 3]})
863
- df.describe()
864
- \"\"\")
865
-
866
- print(result)
867
- ```
868
-
869
- ### 📝 总结
870
-
871
- CodeInterpreter系统运行稳定,功能完整,性能优秀!
872
-
873
- ---
874
-
875
- *报告生成时间: 2024-09-17 10:30:00*
876
- '''
877
-
878
- print("生成Markdown格式结果")
879
- from IPython.display import Markdown
880
- Markdown(markdown_content)
881
- """
882
-
883
- execution = self.sandbox.run_code(code, language="python")
884
- print(execution.to_json())
885
- assert execution.error is None
886
- logger.info("Markdown格式结果测试完成")
887
-
888
- def test_svg_result(self):
889
- """测试SVG格式结果"""
890
- assert self.sandbox is not None
891
-
892
- code = """
893
- # 生成SVG格式结果
894
- svg_content = '''<svg width="400" height="300" xmlns="http://www.w3.org/2000/svg">
895
- <!-- 背景 -->
896
- <rect width="100%" height="100%" fill="#f8f9fa"/>
897
-
898
- <!-- 标题 -->
899
- <text x="200" y="30" text-anchor="middle" font-size="20" font-weight="bold" fill="#333">
900
- CodeInterpreter SVG 报告
901
- </text>
902
-
903
- <!-- 进度条背景 -->
904
- <rect x="50" y="60" width="300" height="20" fill="#e9ecef" rx="10"/>
905
-
906
- <!-- 进度条 -->
907
- <rect x="50" y="60" width="240" height="20" fill="#28a745" rx="10">
908
- <animate attributeName="width" from="0" to="240" dur="2s" repeatCount="1"/>
909
- </rect>
910
-
911
- <!-- 进度文本 -->
912
- <text x="200" y="75" text-anchor="middle" font-size="12" fill="white" font-weight="bold">
913
- 80% 完成
914
- </text>
915
-
916
- <!-- 统计图表 -->
917
- <g transform="translate(50, 120)">
918
- <!-- 柱状图 -->
919
- <rect x="0" y="120" width="40" height="80" fill="#007bff"/>
920
- <rect x="60" y="100" width="40" height="100" fill="#28a745"/>
921
- <rect x="120" y="140" width="40" height="60" fill="#ffc107"/>
922
- <rect x="180" y="90" width="40" height="110" fill="#dc3545"/>
923
- <rect x="240" y="110" width="40" height="90" fill="#17a2b8"/>
924
-
925
- <!-- 标签 -->
926
- <text x="20" y="220" text-anchor="middle" font-size="10">测试A</text>
927
- <text x="80" y="220" text-anchor="middle" font-size="10">测试B</text>
928
- <text x="140" y="220" text-anchor="middle" font-size="10">测试C</text>
929
- <text x="200" y="220" text-anchor="middle" font-size="10">测试D</text>
930
- <text x="260" y="220" text-anchor="middle" font-size="10">测试E</text>
931
- </g>
932
-
933
- <!-- 说明文字 -->
934
- <text x="200" y="270" text-anchor="middle" font-size="14" fill="#666">
935
- 各测试模块执行情况统计
936
- </text>
937
- </svg>'''
938
-
939
- print("生成SVG格式结果")
940
- from IPython.display import SVG
941
- SVG(svg_content)
942
- """
943
-
944
- execution = self.sandbox.run_code(code, language="python")
945
- print(execution.to_json())
946
- assert execution.error is None
947
- logger.info("SVG格式结果测试完成")
948
-
949
- def test_image_results(self):
950
- """测试图像格式结果 (PNG/JPEG)"""
951
- assert self.sandbox is not None
952
-
953
- code = """
954
- import matplotlib.pyplot as plt
955
- import numpy as np
956
- import base64
957
- import io
958
-
959
- # 创建一个复杂的图表
960
- fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(12, 10))
961
- fig.suptitle('CodeInterpreter 测试结果图表集', fontsize=16, fontweight='bold')
962
-
963
- # 图表1: 正弦波
964
- x = np.linspace(0, 4*np.pi, 100)
965
- y1 = np.sin(x)
966
- y2 = np.cos(x)
967
- ax1.plot(x, y1, 'b-', label='sin(x)', linewidth=2)
968
- ax1.plot(x, y2, 'r--', label='cos(x)', linewidth=2)
969
- ax1.set_title('三角函数')
970
- ax1.legend()
971
- ax1.grid(True)
972
-
973
- # 图表2: 柱状图
974
- categories = ['测试A', '测试B', '测试C', '测试D', '测试E']
975
- values = [85, 92, 78, 96, 88]
976
- colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7']
977
- bars = ax2.bar(categories, values, color=colors)
978
- ax2.set_title('测试模块得分')
979
- ax2.set_ylabel('得分')
980
- for bar, value in zip(bars, values):
981
- height = bar.get_height()
982
- ax2.text(bar.get_x() + bar.get_width()/2., height + 1,
983
- f'{value}%', ha='center', va='bottom')
984
-
985
- # 图表3: 饼图
986
- labels = ['成功', '警告', '错误', '跳过']
987
- sizes = [75, 15, 5, 5]
988
- colors = ['#2ECC71', '#F39C12', '#E74C3C', '#95A5A6']
989
- wedges, texts, autotexts = ax3.pie(sizes, labels=labels, colors=colors,
990
- autopct='%1.1f%%', startangle=90)
991
- ax3.set_title('测试结果分布')
992
-
993
- # 图表4: 散点图
994
- np.random.seed(42)
995
- x_scatter = np.random.randn(100)
996
- y_scatter = 2 * x_scatter + np.random.randn(100)
997
- ax4.scatter(x_scatter, y_scatter, alpha=0.6, c=y_scatter, cmap='viridis')
998
- ax4.set_title('性能相关性分析')
999
- ax4.set_xlabel('输入复杂度')
1000
- ax4.set_ylabel('执行时间')
1001
-
1002
- plt.tight_layout()
1003
-
1004
- # 保存为PNG格式 (base64编码)
1005
- png_buffer = io.BytesIO()
1006
- plt.savefig(png_buffer, format='png', dpi=150, bbox_inches='tight')
1007
- png_buffer.seek(0)
1008
- png_base64 = base64.b64encode(png_buffer.getvalue()).decode()
1009
- png_buffer.close()
1010
-
1011
- # 保存为JPEG格式 (base64编码)
1012
- jpeg_buffer = io.BytesIO()
1013
- plt.savefig(jpeg_buffer, format='jpeg', dpi=150, bbox_inches='tight', pil_kwargs={'quality': 95})
1014
- jpeg_buffer.seek(0)
1015
- jpeg_base64 = base64.b64encode(jpeg_buffer.getvalue()).decode()
1016
- jpeg_buffer.close()
1017
-
1018
- plt.close()
1019
-
1020
- print(f"生成图像结果:")
1021
- print(f" PNG大小: {len(png_base64)} 字符")
1022
- print(f" JPEG大小: {len(jpeg_base64)} 字符")
1023
-
1024
- # 返回图像数据
1025
- {
1026
- "png_data": png_base64,
1027
- "jpeg_data": jpeg_base64,
1028
- "formats": ["png", "jpeg"],
1029
- "description": "CodeInterpreter测试结果图表集"
1030
- }
1031
- """
1032
-
1033
- execution = self.sandbox.run_code(code, language="python")
1034
- # for result in execution.results:
1035
- # print(result.__str__())
1036
- assert execution.error is None
1037
- assert any("生成图像结果" in line for line in execution.logs.stdout)
1038
- logger.info("图像格式结果测试完成")
1039
-
1040
- def test_latex_result(self):
1041
- """测试LaTeX格式结果"""
1042
- assert self.sandbox is not None
1043
-
1044
- code = """
1045
- # 生成LaTeX格式结果
1046
- latex_content = r'''
1047
- \\documentclass{article}
1048
- \\usepackage[utf8]{inputenc}
1049
- \\usepackage{amsmath}
1050
- \\usepackage{amsfonts}
1051
- \\usepackage{booktabs}
1052
- \\usepackage{geometry}
1053
- \\geometry{a4paper, margin=1in}
1054
-
1055
- \\title{CodeInterpreter 数学公式展示}
1056
- \\author{测试系统}
1057
- \\date{\\today}
1058
-
1059
- \\begin{document}
1060
-
1061
- \\maketitle
1062
-
1063
- \\section{基础数学公式}
1064
-
1065
- \\subsection{代数公式}
1066
-
1067
- 二次方程求根公式:
1068
- \\begin{equation}
1069
- x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}
1070
- \\end{equation}
1071
-
1072
- 欧拉恒等式 (数学中最美的公式):
1073
- \\begin{equation}
1074
- e^{i\\pi} + 1 = 0
1075
- \\end{equation}
1076
-
1077
- \\subsection{微积分}
1078
-
1079
- 导数定义:
1080
- \\begin{equation}
1081
- f'(x) = \\lim_{h \\to 0} \\frac{f(x+h) - f(x)}{h}
1082
- \\end{equation}
1083
-
1084
- 积分基本定理:
1085
- \\begin{equation}
1086
- \\int_a^b f(x) dx = F(b) - F(a)
1087
- \\end{equation}
1088
-
1089
- \\subsection{线性代数}
1090
-
1091
- 矩阵乘法:
1092
- \\begin{equation}
1093
- (AB)_{ij} = \\sum_{k=1}^{n} A_{ik}B_{kj}
1094
- \\end{equation}
1095
-
1096
- 特征值方程:
1097
- \\begin{equation}
1098
- Av = \\lambda v
1099
- \\end{equation}
1100
-
1101
- \\section{统计学公式}
1102
-
1103
- 正态分布概率密度函数:
1104
- \\begin{equation}
1105
- f(x) = \\frac{1}{\\sigma\\sqrt{2\\pi}} e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}}
1106
- \\end{equation}
1107
-
1108
- 贝叶斯定理:
1109
- \\begin{equation}
1110
- P(A|B) = \\frac{P(B|A)P(A)}{P(B)}
1111
- \\end{equation}
1112
-
1113
- \\section{测试数据表格}
1114
-
1115
- \\begin{table}[h!]
1116
- \\centering
1117
- \\begin{tabular}{@{}lccc@{}}
1118
- \\toprule
1119
- 测试模块 & 执行时间(s) & 内存使用(MB) & 状态 \\\\
1120
- \\midrule
1121
- 基础运算 & 0.123 & 15.6 & ✓ \\\\
1122
- 数据处理 & 0.456 & 32.1 & ✓ \\\\
1123
- 图表生成 & 0.789 & 48.7 & ✓ \\\\
1124
- 错误处理 & 0.234 & 12.3 & ✓ \\\\
1125
- \\bottomrule
1126
- \\end{tabular}
1127
- \\caption{CodeInterpreter性能测试结果}
1128
- \\label{tab:performance}
1129
- \\end{table}
1130
-
1131
- \\section{结论}
1132
-
1133
- CodeInterpreter系统能够成功处理各种数学公式和科学计算,
1134
- 支持LaTeX格式输出,适用于学术和科研应用。
1135
-
1136
- \\end{document}
1137
- '''
1138
-
1139
- print("生成LaTeX格式结果")
1140
- from IPython.display import Latex
1141
- Latex(latex_content)
1142
- """
1143
-
1144
- execution = self.sandbox.run_code(code, language="python")
1145
- print(execution.to_json())
1146
- assert execution.error is None
1147
- logger.info("LaTeX格式结果测试完成")
1148
-
1149
- def test_json_data_result(self):
1150
- """测试JSON数据格式结果"""
1151
- assert self.sandbox is not None
1152
-
1153
- code = """
1154
- import json
1155
- from datetime import datetime
1156
-
1157
- # 生成复杂的JSON数据结构
1158
- json_data = {
1159
- "report_info": {
1160
- "title": "CodeInterpreter 测试报告",
1161
- "version": "1.0.0",
1162
- "generated_at": datetime.now().isoformat(),
1163
- "generator": "CodeInterpreter测试系统"
1164
- },
1165
- "test_summary": {
1166
- "total_tests": 25,
1167
- "passed": 23,
1168
- "failed": 1,
1169
- "skipped": 1,
1170
- "success_rate": 92.0,
1171
- "execution_time": 45.67
1172
- },
1173
- "test_modules": [
1174
- {
1175
- "name": "基础执行",
1176
- "tests": 8,
1177
- "passed": 8,
1178
- "failed": 0,
1179
- "duration": 12.34,
1180
- "details": {
1181
- "memory_usage": "15.6MB",
1182
- "cpu_time": "0.123s",
1183
- "status": "success"
1184
- }
1185
- },
1186
- {
1187
- "name": "数据处理",
1188
- "tests": 6,
1189
- "passed": 5,
1190
- "failed": 1,
1191
- "duration": 18.92,
1192
- "details": {
1193
- "memory_usage": "32.1MB",
1194
- "cpu_time": "0.456s",
1195
- "status": "partial",
1196
- "errors": ["数据类型不匹配"]
1197
- }
1198
- },
1199
- {
1200
- "name": "图表生成",
1201
- "tests": 4,
1202
- "passed": 4,
1203
- "failed": 0,
1204
- "duration": 8.73,
1205
- "details": {
1206
- "memory_usage": "48.7MB",
1207
- "cpu_time": "0.789s",
1208
- "status": "success",
1209
- "formats": ["png", "svg", "pdf"]
1210
- }
1211
- }
1212
- ],
1213
- "performance_metrics": {
1214
- "avg_response_time": 0.234,
1215
- "max_memory_usage": 64.2,
1216
- "cpu_utilization": 15.6,
1217
- "disk_io": {
1218
- "read_bytes": 1048576,
1219
- "write_bytes": 524288
1220
- },
1221
- "network": {
1222
- "requests_sent": 12,
1223
- "requests_successful": 11,
1224
- "bytes_transferred": 2097152
1225
- }
1226
- },
1227
- "environment": {
1228
- "python_version": "3.9.2",
1229
- "platform": "Linux x86_64",
1230
- "packages": {
1231
- "numpy": "1.21.0",
1232
- "pandas": "1.3.0",
1233
- "matplotlib": "3.4.2"
1234
- }
1235
- },
1236
- "recommendations": [
1237
- "优化内存使用,减少峰值内存占用",
1238
- "增加更多错误处理测试用例",
1239
- "考虑添加异步执行支持"
1240
- ],
1241
- "metadata": {
1242
- "schema_version": "2.0",
1243
- "data_format": "json",
1244
- "compression": "none",
1245
- "checksum": "sha256:abc123def456"
1246
- }
1247
- }
1248
-
1249
- print("生成JSON数据格式结果")
1250
- print(f"JSON数据包含 {len(json_data)} 个顶级字段")
1251
- print(f"测试模块数量: {len(json_data['test_modules'])}")
1252
- print(f"性能指标项: {len(json_data['performance_metrics'])}")
1253
-
1254
- # 格式化输出JSON
1255
- formatted_json = json.dumps(json_data, indent=2, ensure_ascii=False)
1256
- print(f"\\n格式化JSON长度: {len(formatted_json)} 字符")
1257
-
1258
- # 返回JSON数据
1259
- json_data
1260
- """
1261
-
1262
- execution = self.sandbox.run_code(code, language="python")
1263
- print(execution.to_json())
1264
- assert execution.error is None
1265
- assert any("JSON数据格式" in line for line in execution.logs.stdout)
1266
- logger.info("JSON数据格式结果测试完成")
1267
-
1268
- def test_javascript_result(self):
1269
- """测试JavaScript格式结果"""
1270
- assert self.sandbox is not None
1271
-
1272
- code = """
1273
- # 生成JavaScript格式结果
1274
- javascript_code = '''
1275
- // CodeInterpreter 交互式结果展示脚本
1276
-
1277
- class CodeInterpreterResults {
1278
- constructor(containerId) {
1279
- this.container = document.getElementById(containerId);
1280
- this.data = null;
1281
- this.charts = [];
1282
- this.init();
1283
- }
1284
-
1285
- init() {
1286
- console.log('CodeInterpreter Results 初始化完成');
1287
- this.createLayout();
1288
- this.loadTestData();
1289
- }
1290
-
1291
- createLayout() {
1292
- this.container.innerHTML = `
1293
- <div class="ci-dashboard">
1294
- <header class="ci-header">
1295
- <h1>🚀 CodeInterpreter 执行结果</h1>
1296
- <div class="ci-status" id="status">正在加载...</div>
1297
- </header>
1298
-
1299
- <div class="ci-content">
1300
- <div class="ci-summary" id="summary"></div>
1301
- <div class="ci-charts" id="charts"></div>
1302
- <div class="ci-logs" id="logs"></div>
1303
- </div>
1304
- </div>
1305
- `;
1306
-
1307
- // 添加样式
1308
- const style = document.createElement('style');
1309
- style.textContent = `
1310
- .ci-dashboard {
1311
- font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
1312
- max-width: 1200px; margin: 0 auto; padding: 20px;
1313
- }
1314
- .ci-header {
1315
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
1316
- color: white; padding: 20px; border-radius: 10px; margin-bottom: 20px;
1317
- }
1318
- .ci-status {
1319
- background: rgba(255,255,255,0.2);
1320
- padding: 5px 15px; border-radius: 15px;
1321
- display: inline-block; font-size: 14px;
1322
- }
1323
- .ci-summary {
1324
- display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
1325
- gap: 20px; margin-bottom: 20px;
1326
- }
1327
- .ci-card {
1328
- background: white; border-radius: 8px; padding: 20px;
1329
- box-shadow: 0 2px 10px rgba(0,0,0,0.1);
1330
- }
1331
- `;
1332
- document.head.appendChild(style);
1333
- }
1334
-
1335
- loadTestData() {
1336
- // 模拟加载测试数据
1337
- this.data = {
1338
- execution: {
1339
- startTime: new Date().toISOString(),
1340
- duration: 1.234,
1341
- status: 'success',
1342
- memory: 15.6,
1343
- cpu: 12.3
1344
- },
1345
- tests: {
1346
- total: 25,
1347
- passed: 23,
1348
- failed: 1,
1349
- skipped: 1
1350
- },
1351
- modules: [
1352
- {name: '基础执行', score: 95, time: 0.123},
1353
- {name: '数据处理', score: 88, time: 0.456},
1354
- {name: '图表生成', score: 92, time: 0.789},
1355
- {name: '错误处理', score: 100, time: 0.234}
1356
- ]
1357
- };
1358
-
1359
- this.renderSummary();
1360
- this.renderCharts();
1361
- this.updateStatus('✅ 加载完成');
1362
-
1363
- // 添加交互性
1364
- this.addInteractivity();
1365
- }
1366
-
1367
- renderSummary() {
1368
- const summaryEl = document.getElementById('summary');
1369
- summaryEl.innerHTML = `
1370
- <div class="ci-card">
1371
- <h3>📊 执行概要</h3>
1372
- <p>执行时间: ${this.data.execution.duration}s</p>
1373
- <p>内存使用: ${this.data.execution.memory}MB</p>
1374
- <p>CPU时间: ${this.data.execution.cpu}%</p>
1375
- </div>
1376
- <div class="ci-card">
1377
- <h3>🎯 测试结果</h3>
1378
- <p>总测试数: ${this.data.tests.total}</p>
1379
- <p>通过: ${this.data.tests.passed}</p>
1380
- <p>失败: ${this.data.tests.failed}</p>
1381
- <p>跳过: ${this.data.tests.skipped}</p>
1382
- </div>
1383
- <div class="ci-card">
1384
- <h3>⚡ 性能指标</h3>
1385
- <p>成功率: ${Math.round(this.data.tests.passed/this.data.tests.total*100)}%</p>
1386
- <p>平均响应: ${this.data.execution.duration/this.data.tests.total}s</p>
1387
- <p>效率评级: A+</p>
1388
- </div>
1389
- `;
1390
- }
1391
-
1392
- renderCharts() {
1393
- const chartsEl = document.getElementById('charts');
1394
- chartsEl.innerHTML = '<div class="ci-card"><h3>📈 性能图表</h3><canvas id="performanceChart" width="400" height="200"></canvas></div>';
1395
-
1396
- // 简单的canvas图表
1397
- const canvas = document.getElementById('performanceChart');
1398
- const ctx = canvas.getContext('2d');
1399
-
1400
- // 绘制柱状图
1401
- const modules = this.data.modules;
1402
- const barWidth = 60;
1403
- const barSpacing = 20;
1404
-
1405
- modules.forEach((module, index) => {
1406
- const x = index * (barWidth + barSpacing) + 50;
1407
- const height = module.score * 1.5; // 缩放高度
1408
- const y = canvas.height - height - 30;
1409
-
1410
- // 绘制柱子
1411
- ctx.fillStyle = `hsl(${120 + index * 60}, 60%, 50%)`;
1412
- ctx.fillRect(x, y, barWidth, height);
1413
-
1414
- // 绘制标签
1415
- ctx.fillStyle = '#333';
1416
- ctx.font = '12px Arial';
1417
- ctx.textAlign = 'center';
1418
- ctx.fillText(module.name, x + barWidth/2, canvas.height - 10);
1419
- ctx.fillText(module.score + '%', x + barWidth/2, y - 5);
1420
- });
1421
- }
1422
-
1423
- addInteractivity() {
1424
- // 添加点击事件
1425
- document.querySelectorAll('.ci-card').forEach(card => {
1426
- card.addEventListener('click', () => {
1427
- card.style.transform = card.style.transform ? '' : 'scale(1.02)';
1428
- });
1429
- });
1430
-
1431
- // 定时更新状态
1432
- setInterval(() => {
1433
- this.updateMemoryUsage();
1434
- }, 2000);
1435
- }
1436
-
1437
- updateStatus(status) {
1438
- document.getElementById('status').textContent = status;
1439
- }
1440
-
1441
- updateMemoryUsage() {
1442
- // 模拟内存使用变化
1443
- const memory = (Math.random() * 10 + 15).toFixed(1);
1444
- const cards = document.querySelectorAll('.ci-card p');
1445
- if (cards[1]) {
1446
- cards[1].textContent = `内存使用: ${memory}MB`;
1447
- }
1448
- }
1449
-
1450
- exportResults() {
1451
- return {
1452
- timestamp: new Date().toISOString(),
1453
- data: this.data,
1454
- summary: 'CodeInterpreter执行完成',
1455
- format: 'javascript_interactive'
1456
- };
1457
- }
1458
- }
1459
-
1460
- // 自动初始化
1461
- document.addEventListener('DOMContentLoaded', function() {
1462
- if (document.getElementById('ci-results-container')) {
1463
- const ciResults = new CodeInterpreterResults('ci-results-container');
1464
-
1465
- // 暴露到全局
1466
- window.CodeInterpreterResults = ciResults;
1467
-
1468
- console.log('CodeInterpreter JavaScript 结果系统已启动');
1469
- }
1470
- });
1471
- '''
1472
-
1473
- print("生成JavaScript格式结果")
1474
- print(f"JavaScript代码长度: {len(javascript_code)} 字符")
1475
- print("包含完整的交互式结果展示系统")
1476
-
1477
- # 模拟返回JavaScript结果
1478
- {
1479
- "javascript_code": javascript_code,
1480
- "type": "interactive_dashboard",
1481
- "features": ["实时更新", "交互式图表", "响应式设计"],
1482
- "description": "CodeInterpreter交互式结果展示系统"
1483
- }
1484
- """
1485
-
1486
- execution = self.sandbox.run_code(code, language="python")
1487
- print(execution.to_json())
1488
- assert execution.error is None
1489
- assert any("JavaScript格式" in line for line in execution.logs.stdout)
1490
- logger.info("JavaScript格式结果测试完成")
1491
-
1492
- def test_chart_data_result(self):
1493
- """测试图表数据格式结果"""
1494
- assert self.sandbox is not None
1495
-
1496
- code = """
1497
- import json
1498
-
1499
- # 生成图表数据格式结果
1500
- chart_data = {
1501
- "chart_type": "multi_chart_dashboard",
1502
- "title": "CodeInterpreter 性能分析图表",
1503
- "description": "展示各项测试指标和性能数据",
1504
- "charts": [
1505
- {
1506
- "id": "performance_overview",
1507
- "type": "line",
1508
- "title": "性能趋势图",
1509
- "data": {
1510
- "labels": ["00:00", "00:15", "00:30", "00:45", "01:00"],
1511
- "datasets": [
1512
- {
1513
- "label": "CPU使用率 (%)",
1514
- "data": [12, 19, 15, 25, 18],
1515
- "borderColor": "rgb(75, 192, 192)",
1516
- "tension": 0.1
1517
- },
1518
- {
1519
- "label": "内存使用率 (%)",
1520
- "data": [8, 15, 12, 20, 16],
1521
- "borderColor": "rgb(255, 99, 132)",
1522
- "tension": 0.1
1523
- }
1524
- ]
1525
- },
1526
- "options": {
1527
- "responsive": True,
1528
- "scales": {
1529
- "y": {"beginAtZero": True, "max": 100}
1530
- }
1531
- }
1532
- },
1533
- {
1534
- "id": "test_results_pie",
1535
- "type": "pie",
1536
- "title": "测试结果分布",
1537
- "data": {
1538
- "labels": ["通过", "失败", "跳过", "警告"],
1539
- "datasets": [{
1540
- "data": [23, 1, 1, 2],
1541
- "backgroundColor": [
1542
- "#28a745", # 绿色 - 通过
1543
- "#dc3545", # 红色 - 失败
1544
- "#6c757d", # 灰色 - 跳过
1545
- "#ffc107" # 黄色 - 警告
1546
- ]
1547
- }]
1548
- },
1549
- "options": {
1550
- "responsive": True,
1551
- "plugins": {
1552
- "legend": {"position": "right"}
1553
- }
1554
- }
1555
- },
1556
- {
1557
- "id": "module_scores",
1558
- "type": "bar",
1559
- "title": "模块测试得分",
1560
- "data": {
1561
- "labels": ["基础执行", "数据处理", "图表生成", "错误处理", "性能测试"],
1562
- "datasets": [{
1563
- "label": "得分",
1564
- "data": [95, 88, 92, 100, 85],
1565
- "backgroundColor": [
1566
- "rgba(54, 162, 235, 0.8)",
1567
- "rgba(255, 99, 132, 0.8)",
1568
- "rgba(255, 205, 86, 0.8)",
1569
- "rgba(75, 192, 192, 0.8)",
1570
- "rgba(153, 102, 255, 0.8)"
1571
- ],
1572
- "borderColor": [
1573
- "rgba(54, 162, 235, 1)",
1574
- "rgba(255, 99, 132, 1)",
1575
- "rgba(255, 205, 86, 1)",
1576
- "rgba(75, 192, 192, 1)",
1577
- "rgba(153, 102, 255, 1)"
1578
- ],
1579
- "borderWidth": 1
1580
- }]
1581
- },
1582
- "options": {
1583
- "responsive": True,
1584
- "scales": {
1585
- "y": {"beginAtZero": True, "max": 100}
1586
- }
1587
- }
1588
- },
1589
- {
1590
- "id": "response_time_histogram",
1591
- "type": "histogram",
1592
- "title": "响应时间分布",
1593
- "data": {
1594
- "labels": ["0-0.1s", "0.1-0.2s", "0.2-0.5s", "0.5-1s", "1s+"],
1595
- "datasets": [{
1596
- "label": "请求数量",
1597
- "data": [45, 32, 18, 8, 2],
1598
- "backgroundColor": "rgba(75, 192, 192, 0.6)",
1599
- "borderColor": "rgba(75, 192, 192, 1)",
1600
- "borderWidth": 1
1601
- }]
1602
- },
1603
- "options": {
1604
- "responsive": True,
1605
- "scales": {
1606
- "y": {"beginAtZero": True}
1607
- }
1608
- }
1609
- }
1610
- ],
1611
- "summary": {
1612
- "total_charts": 4,
1613
- "data_points": 105,
1614
- "time_range": "1小时",
1615
- "update_frequency": "15秒"
1616
- },
1617
- "interactive_features": [
1618
- "点击图例显示/隐藏数据系列",
1619
- "悬停显示详细数值",
1620
- "图表缩放和平移",
1621
- "数据导出功能"
1622
- ],
1623
- "export_options": [
1624
- "PNG图片",
1625
- "SVG矢量图",
1626
- "PDF报告",
1627
- "CSV数据",
1628
- "JSON配置"
1629
- ],
1630
- "metadata": {
1631
- "created_at": "2024-09-17T10:30:00Z",
1632
- "version": "1.0",
1633
- "generator": "CodeInterpreter",
1634
- "format": "chart.js_compatible"
1635
- }
1636
- }
1637
-
1638
- print("生成图表数据格式结果")
1639
- print(f"包含 {len(chart_data['charts'])} 个图表")
1640
- print(f"总数据点: {chart_data['summary']['data_points']}")
1641
-
1642
- for i, chart in enumerate(chart_data['charts'], 1):
1643
- print(f" 图表{i}: {chart['title']} ({chart['type']})")
1644
-
1645
- print(f"\\n图表数据JSON长度: {len(json.dumps(chart_data))} 字符")
1646
-
1647
- # 返回图表数据
1648
- chart_data
1649
- """
1650
-
1651
- execution = self.sandbox.run_code(code, language="python")
1652
- print(execution.to_json())
1653
- assert execution.error is None
1654
- assert any("图表数据格式" in line for line in execution.logs.stdout)
1655
- logger.info("图表数据格式结果测试完成")
1656
-
1657
- def test_mixed_format_result(self):
1658
- """测试混合格式结果"""
1659
- assert self.sandbox is not None
1660
-
1661
- code = """
1662
- import json
1663
- import base64
1664
- import matplotlib.pyplot as plt
1665
- import numpy as np
1666
- import io
1667
-
1668
- # 生成混合格式的综合结果
1669
- print("生成混合格式综合结果...")
1670
-
1671
- # 1. 文本摘要
1672
- text_summary = '''
1673
- CodeInterpreter 综合测试报告
1674
- ================================
1675
-
1676
- 执行时间: 2024-09-17 10:30:00
1677
- 测试模块: 17个
1678
- 总体状态: ✅ 成功
1679
-
1680
- 主要发现:
1681
- - 所有核心功能运行正常
1682
- - 性能表现优秀
1683
- - 支持多种输出格式
1684
- - 错误处理机制完善
1685
- '''
1686
-
1687
- # 2. HTML报告
1688
- html_report = '''
1689
- <div class="mixed-result-report">
1690
- <h2>🚀 CodeInterpreter 测试完成</h2>
1691
- <div class="status-badge success">✅ 全部通过</div>
1692
- <ul>
1693
- <li>文本格式: ✅</li>
1694
- <li>HTML格式: ✅</li>
1695
- <li>图像格式: ✅</li>
1696
- <li>数据格式: ✅</li>
1697
- </ul>
1698
- </div>
1699
- '''
1700
-
1701
- # 3. 生成图表
1702
- fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
1703
-
1704
- # 测试结果饼图
1705
- labels = ['通过', '失败', '警告']
1706
- sizes = [92, 4, 4]
1707
- colors = ['#28a745', '#dc3545', '#ffc107']
1708
- ax1.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
1709
- ax1.set_title('测试结果分布')
1710
-
1711
- # 性能趋势图
1712
- x = range(5)
1713
- performance = [85, 88, 92, 95, 93]
1714
- ax2.plot(x, performance, marker='o', linewidth=2, markersize=6)
1715
- ax2.set_title('性能趋势')
1716
- ax2.set_xlabel('测试轮次')
1717
- ax2.set_ylabel('得分')
1718
- ax2.grid(True, alpha=0.3)
1719
-
1720
- plt.tight_layout()
1721
-
1722
- # 转换为base64
1723
- img_buffer = io.BytesIO()
1724
- plt.savefig(img_buffer, format='png', dpi=100, bbox_inches='tight')
1725
- img_buffer.seek(0)
1726
- img_base64 = base64.b64encode(img_buffer.getvalue()).decode()
1727
- img_buffer.close()
1728
- plt.close()
1729
-
1730
- # 4. JSON数据
1731
- json_data = {
1732
- "test_summary": {
1733
- "total_tests": 17,
1734
- "passed": 16,
1735
- "failed": 1,
1736
- "success_rate": 94.1
1737
- },
1738
- "performance_metrics": {
1739
- "execution_time": 45.67,
1740
- "memory_peak": 64.2,
1741
- "cpu_avg": 15.6
1742
- },
1743
- "module_results": [
1744
- {"name": "基础功能", "score": 95, "status": "pass"},
1745
- {"name": "数据处理", "score": 88, "status": "pass"},
1746
- {"name": "图表生成", "score": 92, "status": "pass"},
1747
- {"name": "错误处理", "score": 100, "status": "pass"}
1748
- ]
1749
- }
1750
-
1751
- # 5. LaTeX公式
1752
- latex_formula = r'''
1753
- 测试成功率计算公式:
1754
- $$\\text{Success Rate} = \\frac{\\text{Passed Tests}}{\\text{Total Tests}} \\times 100\\% = \\frac{16}{17} \\times 100\\% = 94.1\\%$$
1755
- '''
1756
-
1757
- # 6. Markdown格式
1758
- markdown_content = '''
1759
- ## 📊 CodeInterpreter 测试总结
1760
-
1761
- ### ✅ 主要成就
1762
- - **高成功率**: 94.1% 的测试通过
1763
- - **多格式支持**: 支持7种不同的输出格式
1764
- - **优秀性能**: 平均响应时间 < 0.5s
1765
-
1766
- ### 📈 性能指标
1767
- | 指标 | 数值 | 状态 |
1768
- |------|------|------|
1769
- | 执行时间 | 45.67s | ✅ |
1770
- | 内存峰值 | 64.2MB | ✅ |
1771
- | CPU使用率 | 15.6% | ✅ |
1772
-
1773
- ### 🎯 下一步计划
1774
- 1. 优化内存使用
1775
- 2. 增加更多测试用例
1776
- 3. 提升错误处理能力
1777
- '''
1778
-
1779
- # 7. 生成SVG图标
1780
- svg_icon = '''
1781
- <svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
1782
- <circle cx="50" cy="50" r="40" stroke="#28a745" stroke-width="4" fill="#d4edda"/>
1783
- <text x="50" y="55" font-family="Arial" font-size="24" text-anchor="middle" fill="#155724">✓</text>
1784
- </svg>
1785
- '''
1786
-
1787
- print(f"混合格式结果生成完成:")
1788
- print(f" 文本长度: {len(text_summary)} 字符")
1789
- print(f" HTML长度: {len(html_report)} 字符")
1790
- print(f" 图片大小: {len(img_base64)} 字符")
1791
- print(f" JSON字段: {len(json_data)} 个")
1792
- print(f" LaTeX公式: 包含成功率计算")
1793
- print(f" Markdown段落: 3个主要章节")
1794
- print(f" SVG图标: 成功状态指示")
1795
-
1796
- # 返回混合格式结果
1797
- {
1798
- "formats": ["text", "html", "png", "json", "latex", "markdown", "svg"],
1799
- "text": text_summary,
1800
- "html": html_report,
1801
- "png_data": img_base64,
1802
- "json_data": json_data,
1803
- "latex": latex_formula,
1804
- "markdown": markdown_content,
1805
- "svg": svg_icon,
1806
- "summary": {
1807
- "total_formats": 7,
1808
- "content_size": len(text_summary) + len(html_report) + len(img_base64) + len(markdown_content),
1809
- "description": "包含多种格式的综合测试结果"
1810
- }
1811
- }
1812
- """
1813
-
1814
- execution = self.sandbox.run_code(code, language="python")
1815
- print(execution.to_json())
1816
- assert execution.error is None
1817
- assert any("混合格式" in line for line in execution.logs.stdout)
1818
- logger.info("混合格式结果测试完成")
1819
-
1820
- # ======================== R语言测试 ========================
1821
-
1822
- def test_r_language_basic_execution(self):
1823
- """测试R语言基础执行"""
1824
- assert self.sandbox is not None
1825
-
1826
- code = """
1827
- # R语言基础执行测试
1828
- print("Hello from R Language!")
1829
-
1830
- # 基础数学运算
1831
- x <- 10
1832
- y <- 20
1833
- sum_result <- x + y
1834
- product_result <- x * y
1835
-
1836
- print(paste("Sum:", sum_result))
1837
- print(paste("Product:", product_result))
1838
-
1839
- # 向量操作
1840
- numbers <- c(1, 2, 3, 4, 5)
1841
- mean_value <- mean(numbers)
1842
- print(paste("Mean of numbers:", mean_value))
1843
-
1844
- # 数据框创建
1845
- df <- data.frame(
1846
- name = c("Alice", "Bob", "Charlie"),
1847
- age = c(25, 30, 35),
1848
- city = c("New York", "London", "Tokyo")
1849
- )
1850
-
1851
- print("Data frame created:")
1852
- print(df)
1853
-
1854
- # 返回结果
1855
- list(
1856
- sum = sum_result,
1857
- product = product_result,
1858
- mean = mean_value,
1859
- data_frame = df
1860
- )
1861
- """
1862
-
1863
- execution = self.sandbox.run_code(code, language="r")
1864
- print(execution.to_json())
1865
- assert execution.error is None
1866
- assert any("Hello from R Language!" in line for line in execution.logs.stdout)
1867
- assert any("Sum:" in line for line in execution.logs.stdout)
1868
- logger.info("R language basic execution test passed")
1869
-
1870
- def test_r_language_data_analysis(self):
1871
- """测试R语言数据分析"""
1872
- assert self.sandbox is not None
1873
-
1874
- code = """
1875
- # R语言数据分析测试
1876
- library(dplyr)
1877
-
1878
- # 创建示例数据集
1879
- set.seed(123)
1880
- data <- data.frame(
1881
- id = 1:100,
1882
- value = rnorm(100, mean = 50, sd = 15),
1883
- category = sample(c("A", "B", "C"), 100, replace = TRUE),
1884
- score = runif(100, 0, 100)
1885
- )
1886
-
1887
- print("Dataset created with 100 rows")
1888
- print(paste("Columns:", paste(names(data), collapse = ", ")))
1889
-
1890
- # 基础统计
1891
- summary_stats <- summary(data$value)
1892
- print("Summary statistics for value column:")
1893
- print(summary_stats)
1894
-
1895
- # 按类别分组统计
1896
- grouped_stats <- data %>%
1897
- group_by(category) %>%
1898
- summarise(
1899
- count = n(),
1900
- mean_value = mean(value),
1901
- mean_score = mean(score),
1902
- .groups = 'drop'
1903
- )
1904
-
1905
- print("Grouped statistics:")
1906
- print(grouped_stats)
1907
-
1908
- # 数据过滤
1909
- high_scores <- data %>%
1910
- filter(score > 80) %>%
1911
- arrange(desc(score))
1912
-
1913
- print(paste("High scores (>80):", nrow(high_scores), "rows"))
1914
-
1915
- # 返回分析结果
1916
- list(
1917
- total_rows = nrow(data),
1918
- summary = summary_stats,
1919
- grouped = grouped_stats,
1920
- high_scores_count = nrow(high_scores)
1921
- )
1922
- """
1923
-
1924
- execution = self.sandbox.run_code(code, language="r")
1925
- print(execution.to_json())
1926
- assert execution.error is None
1927
- assert any(
1928
- "Dataset created with 100 rows" in line for line in execution.logs.stdout
1929
- )
1930
- assert any("Summary statistics" in line for line in execution.logs.stdout)
1931
- logger.info("R language data analysis test passed")
1932
-
1933
- def test_r_language_visualization(self):
1934
- """测试R语言数据可视化"""
1935
- assert self.sandbox is not None
1936
-
1937
- code = """
1938
- # R语言数据可视化测试
1939
- library(ggplot2)
1940
-
1941
- # 创建示例数据
1942
- set.seed(456)
1943
- plot_data <- data.frame(
1944
- x = 1:50,
1945
- y = cumsum(rnorm(50)),
1946
- group = rep(c("Group1", "Group2"), each = 25)
1947
- )
1948
-
1949
- print("Creating visualizations...")
1950
-
1951
- # 基础散点图
1952
- p1 <- ggplot(plot_data, aes(x = x, y = y)) +
1953
- geom_point() +
1954
- geom_smooth(method = "lm") +
1955
- labs(title = "Scatter Plot with Trend Line",
1956
- x = "X Values", y = "Y Values") +
1957
- theme_minimal()
1958
-
1959
- print("Scatter plot created")
1960
-
1961
- # 分组箱线图
1962
- p2 <- ggplot(plot_data, aes(x = group, y = y, fill = group)) +
1963
- geom_boxplot() +
1964
- labs(title = "Box Plot by Group",
1965
- x = "Group", y = "Y Values") +
1966
- theme_minimal()
1967
-
1968
- print("Box plot created")
1969
-
1970
- # 直方图
1971
- p3 <- ggplot(plot_data, aes(x = y)) +
1972
- geom_histogram(bins = 20, fill = "skyblue", alpha = 0.7) +
1973
- labs(title = "Distribution of Y Values",
1974
- x = "Y Values", y = "Frequency") +
1975
- theme_minimal()
1976
-
1977
- print("Histogram created")
1978
-
1979
- # 保存图表信息
1980
- plot_info <- list(
1981
- scatter_plot = "Created scatter plot with trend line",
1982
- box_plot = "Created box plot by group",
1983
- histogram = "Created histogram of y values",
1984
- total_plots = 3
1985
- )
1986
-
1987
- print("All visualizations completed successfully")
1988
- plot_info
1989
- """
1990
-
1991
- execution = self.sandbox.run_code(code, language="r")
1992
- print(execution.to_json())
1993
- assert execution.error is None
1994
- assert any(
1995
- "Creating visualizations..." in line for line in execution.logs.stdout
1996
- )
1997
- assert any(
1998
- "All visualizations completed successfully" in line
1999
- for line in execution.logs.stdout
2000
- )
2001
- logger.info("R language visualization test passed")
2002
-
2003
- def test_r_language_statistics(self):
2004
- """测试R语言统计分析"""
2005
- assert self.sandbox is not None
2006
-
2007
- code = """
2008
- # R语言统计分析测试
2009
- library(stats)
2010
-
2011
- # 创建两个样本数据
2012
- set.seed(789)
2013
- sample1 <- rnorm(100, mean = 10, sd = 2)
2014
- sample2 <- rnorm(100, mean = 12, sd = 2.5)
2015
-
2016
- print("Created two sample datasets")
2017
-
2018
- # 描述性统计
2019
- desc_stats1 <- list(
2020
- mean = mean(sample1),
2021
- median = median(sample1),
2022
- sd = sd(sample1),
2023
- min = min(sample1),
2024
- max = max(sample1)
2025
- )
2026
-
2027
- desc_stats2 <- list(
2028
- mean = mean(sample2),
2029
- median = median(sample2),
2030
- sd = sd(sample2),
2031
- min = min(sample2),
2032
- max = max(sample2)
2033
- )
2034
-
2035
- print("Descriptive statistics calculated")
2036
-
2037
- # t检验
2038
- t_test_result <- t.test(sample1, sample2)
2039
- print("T-test performed")
2040
-
2041
- # 相关性分析
2042
- correlation <- cor(sample1, sample2)
2043
- print(paste("Correlation coefficient:", round(correlation, 4)))
2044
-
2045
- # 线性回归
2046
- lm_model <- lm(sample2 ~ sample1)
2047
- summary_lm <- summary(lm_model)
2048
- print("Linear regression model fitted")
2049
-
2050
- # 正态性检验
2051
- shapiro_test1 <- shapiro.test(sample1[1:50]) # 限制样本大小
2052
- shapiro_test2 <- shapiro.test(sample2[1:50])
2053
-
2054
- print("Normality tests performed")
2055
-
2056
- # 返回统计结果
2057
- list(
2058
- sample1_stats = desc_stats1,
2059
- sample2_stats = desc_stats2,
2060
- t_test_p_value = t_test_result$p.value,
2061
- correlation = correlation,
2062
- r_squared = summary_lm$r.squared,
2063
- normality_test1_p = shapiro_test1$p.value,
2064
- normality_test2_p = shapiro_test2$p.value
2065
- )
2066
- """
2067
-
2068
- execution = self.sandbox.run_code(code, language="r")
2069
- print(execution.to_json())
2070
- assert execution.error is None
2071
- assert any(
2072
- "Created two sample datasets" in line for line in execution.logs.stdout
2073
- )
2074
- assert any("T-test performed" in line for line in execution.logs.stdout)
2075
- logger.info("R language statistics test passed")
2076
-
2077
- def test_r_language_context_management(self):
2078
- """测试R语言上下文管理"""
2079
- assert self.sandbox is not None
2080
-
2081
- # 创建R语言上下文
2082
- r_context = self.sandbox.create_code_context(language="r", cwd="/tmp")
2083
- self.contexts["r_language"] = r_context
2084
-
2085
- # 在上下文中定义变量和函数
2086
- setup_code = """
2087
- # R语言上下文设置
2088
- print("Setting up R language context...")
2089
-
2090
- # 定义全局变量
2091
- global_var <- "Hello from R Context"
2092
- counter <- 0
2093
- data_cache <- list()
2094
-
2095
- # 定义函数
2096
- increment_counter <- function() {
2097
- counter <<- counter + 1
2098
- return(counter)
2099
- }
2100
-
2101
- add_to_cache <- function(key, value) {
2102
- data_cache[[key]] <<- value
2103
- return(length(data_cache))
2104
- }
2105
-
2106
- # 初始化一些数据
2107
- sample_data <- data.frame(
2108
- x = 1:10,
2109
- y = (1:10) ^ 2
2110
- )
2111
-
2112
- print(paste("Context setup complete. Counter:", counter))
2113
- print(paste("Cache size:", length(data_cache)))
2114
-
2115
- # 返回设置信息
2116
- list(
2117
- global_var = global_var,
2118
- counter = counter,
2119
- cache_size = length(data_cache),
2120
- data_rows = nrow(sample_data)
2121
- )
2122
- """
2123
-
2124
- execution1 = self.sandbox.run_code(setup_code, context=r_context)
2125
- print(execution1.to_json())
2126
- assert execution1.error is None
2127
- assert any(
2128
- "Setting up R language context..." in line
2129
- for line in execution1.logs.stdout
2130
- )
2131
-
2132
- # 在同一上下文中使用之前定义的变量和函数
2133
- use_code = """
2134
- # 使用R语言上下文中的变量和函数
2135
- print("Using R language context...")
2136
-
2137
- # 使用全局变量
2138
- print(paste("Global variable:", global_var))
2139
-
2140
- # 使用函数
2141
- new_counter <- increment_counter()
2142
- print(paste("Counter after increment:", new_counter))
2143
-
2144
- # 添加到缓存
2145
- cache_size <- add_to_cache("test_key", "test_value")
2146
- print(paste("Cache size after addition:", cache_size))
2147
-
2148
- # 使用数据
2149
- data_summary <- summary(sample_data)
2150
- print("Data summary:")
2151
- print(data_summary)
2152
-
2153
- # 修改数据
2154
- sample_data$z <- sample_data$x + sample_data$y
2155
- print(paste("Added new column. Total columns:", ncol(sample_data)))
2156
-
2157
- # 返回使用结果
2158
- list(
2159
- final_counter = new_counter,
2160
- final_cache_size = cache_size,
2161
- data_columns = ncol(sample_data),
2162
- context_active = TRUE
2163
- )
2164
- """
2165
-
2166
- execution2 = self.sandbox.run_code(use_code, context=r_context)
2167
- print(execution2.to_json())
2168
- assert execution2.error is None
2169
- assert any(
2170
- "Using R language context..." in line for line in execution2.logs.stdout
2171
- )
2172
- assert any(
2173
- "Counter after increment:" in line for line in execution2.logs.stdout
2174
- )
2175
- logger.info("R language context management test passed")
2176
-
2177
- # 测试完成后立即清理context
2178
- try:
2179
- self.sandbox.destroy_context(r_context)
2180
- logger.info(f"Successfully destroyed R context: {r_context.id}")
2181
- # 从contexts字典中移除
2182
- if "r_language" in self.contexts:
2183
- del self.contexts["r_language"]
2184
- except Exception as e:
2185
- logger.warning(f"Failed to destroy R context {r_context.id}: {e}")
2186
-
2187
- # ======================== Node.js/JavaScript 测试 ========================
2188
-
2189
- def test_nodejs_basic_execution(self):
2190
- """测试Node.js基础执行"""
2191
- assert self.sandbox is not None
2192
-
2193
- code = """
2194
- // Node.js 基础执行测试
2195
- console.log("Hello from Node.js Kernel!");
2196
-
2197
- // 基础运算与字符串模板
2198
- const a = 7;
2199
- const b = 5;
2200
- const sum = a + b;
2201
- const product = a * b;
2202
- console.log(`Sum: ${sum}`);
2203
- console.log(`Product: ${product}`);
2204
-
2205
- // 对象与数组
2206
- const users = [
2207
- { id: 1, name: "Alice", score: 88 },
2208
- { id: 2, name: "Bob", score: 92 },
2209
- { id: 3, name: "Charlie", score: 75 }
2210
- ];
2211
- const top = users.filter(u => u.score >= 90);
2212
- console.log(`Top users: ${top.map(u => u.name).join(', ')}`);
2213
-
2214
- // 返回综合数据
2215
- ({ sum, product, topCount: top.length })
2216
- """
2217
-
2218
- execution = self.sandbox.run_code(code, language="javascript")
2219
- print(execution.to_json())
2220
- assert execution.error is None
2221
- assert any(
2222
- "Hello from Node.js Kernel!" in line for line in execution.logs.stdout
2223
- )
2224
- assert any("Sum:" in line for line in execution.logs.stdout)
2225
- logger.info("Node.js basic execution test passed")
2226
-
2227
- def test_nodejs_async_promises(self):
2228
- """测试Node.js异步/Promise"""
2229
- assert self.sandbox is not None
2230
-
2231
- code = """
2232
- // Node.js 异步 Promise 测试
2233
- function delay(ms) {
2234
- return new Promise(resolve => setTimeout(resolve, ms));
2235
- }
2236
-
2237
- async function main() {
2238
- console.log("Starting async tasks...");
2239
- const start = Date.now();
2240
- await delay(50);
2241
- const data = await Promise.all([
2242
- (async () => { await delay(20); return { name: 'task1', value: 42 }; })(),
2243
- (async () => { await delay(30); return { name: 'task2', value: 58 }; })()
2244
- ]);
2245
- const total = data.reduce((acc, cur) => acc + cur.value, 0);
2246
- const duration = Date.now() - start;
2247
- console.log(`Async tasks done in ${duration} ms`);
2248
- return { total, duration, tasks: data.map(d => d.name) };
2249
- }
2250
-
2251
- main();
2252
- """
2253
-
2254
- execution = self.sandbox.run_code(code, language="javascript")
2255
- print(execution.to_json())
2256
- assert execution.error is None
2257
- assert any("Starting async tasks..." in line for line in execution.logs.stdout)
2258
- assert any("Async tasks done" in line for line in execution.logs.stdout)
2259
- logger.info("Node.js async/promises test passed")
2260
-
2261
- def test_nodejs_data_processing(self):
2262
- """测试Node.js数据处理"""
2263
- assert self.sandbox is not None
2264
-
2265
- code = """
2266
- // Node.js 数据处理示例(无需外部库)
2267
- const records = [];
2268
- for (let i = 0; i < 100; i++) {
2269
- records.push({ id: i + 1, value: Math.round(Math.random() * 100), group: ['A','B','C'][i % 3] });
2270
- }
2271
-
2272
- const summary = records.reduce((acc, r) => {
2273
- if (!acc[r.group]) acc[r.group] = { count: 0, sum: 0 };
2274
- acc[r.group].count++;
2275
- acc[r.group].sum += r.value;
2276
- return acc;
2277
- }, {});
2278
-
2279
- const grouped = Object.entries(summary).map(([group, s]) => ({ group, count: s.count, mean: s.sum / s.count }));
2280
- console.log("Grouped summary ready");
2281
- ({ total: records.length, groups: grouped })
2282
- """
2283
-
2284
- execution = self.sandbox.run_code(code, language="javascript")
2285
- print(execution.to_json())
2286
- assert execution.error is None
2287
- assert any("Grouped summary ready" in line for line in execution.logs.stdout)
2288
- logger.info("Node.js data processing test passed")
2289
-
2290
- def test_nodejs_chart_data(self):
2291
- """测试Node.js图表数据生成(Chart.js兼容结构)"""
2292
- assert self.sandbox is not None
2293
-
2294
- code = """
2295
- // 生成Chart.js兼容的数据对象
2296
- const labels = Array.from({length: 7}, (_, i) => `Day ${i+1}`);
2297
- const dataset = labels.map(() => Math.round(Math.random() * 100));
2298
-
2299
- const chart = {
2300
- type: 'line',
2301
- data: {
2302
- labels,
2303
- datasets: [{ label: 'Random Series', data: dataset, borderColor: '#2b8a3e' }]
2304
- },
2305
- options: {
2306
- responsive: true,
2307
- plugins: { legend: { display: true } }
2308
- }
2309
- };
2310
-
2311
- console.log("Chart data generated");
2312
- ({ chart })
2313
- """
2314
-
2315
- execution = self.sandbox.run_code(code, language="javascript")
2316
- print(execution.to_json())
2317
- assert execution.error is None
2318
- assert any("Chart data generated" in line for line in execution.logs.stdout)
2319
- # 验证结果中存在chart/data之一
2320
- assert len(execution.results) > 0
2321
- logger.info("Node.js chart data test passed")
2322
-
2323
- def test_nodejs_context_management(self):
2324
- """测试Node.js上下文管理"""
2325
- assert self.sandbox is not None
2326
-
2327
- # 创建Node.js上下文
2328
- js_context = self.sandbox.create_code_context(language="javascript", cwd="/tmp")
2329
- self.contexts["nodejs"] = js_context
2330
-
2331
- # 在上下文中定义变量与函数
2332
- setup = """
2333
- // Node.js 上下文初始化
2334
- console.log("Setting up Node.js context...");
2335
- globalThis.cache = { items: [] };
2336
- function addItem(x) { globalThis.cache.items.push(x); return globalThis.cache.items.length; }
2337
- function sum(a,b) { return a + b; }
2338
- console.log(`Init done with ${globalThis.cache.items.length} items`);
2339
- ({ size: globalThis.cache.items.length })
2340
- """
2341
-
2342
- exec1 = self.sandbox.run_code(setup, context=js_context)
2343
- print(exec1.to_json())
2344
- assert exec1.error is None
2345
- assert any(
2346
- "Setting up Node.js context..." in line for line in exec1.logs.stdout
2347
- )
2348
-
2349
- # 使用上下文中的函数与状态
2350
- use = """
2351
- console.log("Using Node.js context...");
2352
- const n1 = addItem({ id: 1, value: 10 });
2353
- const n2 = addItem({ id: 2, value: 15 });
2354
- const s = sum(3, 4);
2355
- console.log(`Items now: ${n2}, sum=${s}`);
2356
- ({ items: n2, sum: s })
2357
- """
2358
-
2359
- exec2 = self.sandbox.run_code(use, context=js_context)
2360
- print(exec2.to_json())
2361
- assert exec2.error is None
2362
- assert any("Using Node.js context..." in line for line in exec2.logs.stdout)
2363
-
2364
- # 清理上下文
2365
- try:
2366
- self.sandbox.destroy_context(js_context)
2367
- if "nodejs" in self.contexts:
2368
- del self.contexts["nodejs"]
2369
- logger.info("Destroyed Node.js context")
2370
- except Exception as e:
2371
- logger.warning(f"Failed to destroy Node.js context: {e}")
2372
-
2373
- # ======================== Bash 测试 ========================
2374
-
2375
- def test_bash_basic_execution(self):
2376
- """测试Bash基础执行"""
2377
- assert self.sandbox is not None
2378
-
2379
- code = """
2380
- # Bash 基础执行
2381
- echo "Hello from Bash Kernel!"
2382
- NAME="scalebox"
2383
- GREETING="Hello, ${NAME}!"
2384
- echo "${GREETING}"
2385
- echo "Current user: $(whoami)"
2386
- date
2387
- """
2388
-
2389
- execution = self.sandbox.run_code(code, language="bash")
2390
- print(execution.to_json())
2391
- assert execution.error is None
2392
- assert any("Hello from Bash Kernel!" in line for line in execution.logs.stdout)
2393
- assert any("Hello, scalebox!" in line for line in execution.logs.stdout)
2394
- logger.info("Bash basic execution test passed")
2395
-
2396
- def test_bash_file_operations(self):
2397
- """测试Bash文件操作"""
2398
- assert self.sandbox is not None
2399
-
2400
- code = """
2401
- set -e
2402
- echo "Creating files..."
2403
- WORKDIR="/tmp/bash_demo"
2404
- mkdir -p "$WORKDIR"
2405
- cd "$WORKDIR"
2406
- echo "alpha" > a.txt
2407
- echo "beta" > b.txt
2408
- cat a.txt b.txt > all.txt
2409
- echo "Files in $(pwd):"
2410
- ls -l
2411
- wc -l all.txt
2412
- echo "Done"
2413
- """
2414
-
2415
- execution = self.sandbox.run_code(code, language="bash")
2416
- print(execution.to_json())
2417
- assert execution.error is None
2418
- assert any("Creating files..." in line for line in execution.logs.stdout)
2419
- assert any("Done" in line for line in execution.logs.stdout)
2420
- logger.info("Bash file operations test passed")
2421
-
2422
- def test_bash_pipelines_and_grep(self):
2423
- """测试Bash管道与grep"""
2424
- assert self.sandbox is not None
2425
-
2426
- code = """
2427
- set -e
2428
- printf "%s\n" foo bar baz foo bar | grep -n "foo" | awk -F: '{print "line", $1, ":", $2}'
2429
- echo "PIPELINE_OK"
2430
- """
2431
-
2432
- execution = self.sandbox.run_code(code, language="bash")
2433
- print(execution.to_json())
2434
- assert execution.error is None
2435
- assert any("PIPELINE_OK" in line for line in execution.logs.stdout)
2436
- logger.info("Bash pipelines/grep test passed")
2437
-
2438
- def test_bash_env_and_exit_codes(self):
2439
- """测试Bash环境变量与退出码"""
2440
- assert self.sandbox is not None
2441
-
2442
- code = """
2443
- export APP_ENV=production
2444
- echo "ENV=$APP_ENV"
2445
- (exit 7)
2446
- echo $?
2447
- """
2448
-
2449
- execution = self.sandbox.run_code(code, language="bash")
2450
- print(execution.to_json())
2451
- assert execution.error is None
2452
- assert any("ENV=production" in line for line in execution.logs.stdout)
2453
- # 由于shell分行执行,上一条(exit 7)的退出码会在下一行$?中打印为7
2454
- assert any(line.strip() == "7" for line in execution.logs.stdout)
2455
- logger.info("Bash env and exit codes test passed")
2456
-
2457
- def test_bash_context_management(self):
2458
- """测试Bash上下文管理"""
2459
- assert self.sandbox is not None
2460
-
2461
- # 创建Bash上下文
2462
- bash_ctx = self.sandbox.create_code_context(language="bash", cwd="/tmp")
2463
- self.contexts["bash"] = bash_ctx
2464
-
2465
- setup = """
2466
- echo "Setting up Bash context..."
2467
- MYVAR=42
2468
- echo $MYVAR
2469
- """
2470
-
2471
- e1 = self.sandbox.run_code(setup, context=bash_ctx)
2472
- print(e1.to_json())
2473
- assert e1.error is None
2474
- assert any("Setting up Bash context..." in line for line in e1.logs.stdout)
2475
-
2476
- use = """
2477
- echo "Using Bash context..."
2478
- echo "MYVAR=$MYVAR"
2479
- MYVAR=$((MYVAR+8))
2480
- echo "MYVAR_AFTER=$MYVAR"
2481
- """
2482
-
2483
- e2 = self.sandbox.run_code(use, context=bash_ctx)
2484
- print(e2.to_json())
2485
- assert e2.error is None
2486
- assert any("Using Bash context..." in line for line in e2.logs.stdout)
2487
- assert any("MYVAR_AFTER=50" in line for line in e2.logs.stdout)
2488
-
2489
- # 清理上下文
2490
- try:
2491
- self.sandbox.destroy_context(bash_ctx)
2492
- if "bash" in self.contexts:
2493
- del self.contexts["bash"]
2494
- logger.info("Destroyed Bash context")
2495
- except Exception as e:
2496
- logger.warning(f"Failed to destroy Bash context: {e}")
2497
-
2498
- # ======================== IJAVA 测试 ========================
2499
-
2500
- def test_ijava_basic_execution(self):
2501
- """测试IJAVA基础执行"""
2502
- assert self.sandbox is not None
2503
-
2504
- code = """
2505
- // IJAVA 基础执行测试
2506
- System.out.println("Hello from IJAVA Kernel!");
2507
-
2508
- // 基础变量和运算
2509
- int a = 10;
2510
- int b = 20;
2511
- int sum = a + b;
2512
- int product = a * b;
2513
-
2514
- System.out.println("Sum: " + sum);
2515
- System.out.println("Product: " + product);
2516
-
2517
- // 字符串操作
2518
- String name = "ScaleBox";
2519
- String greeting = "Hello, " + name + "!";
2520
- System.out.println(greeting);
2521
-
2522
- // 数组操作
2523
- int[] numbers = {1, 2, 3, 4, 5};
2524
- int total = 0;
2525
- for (int num : numbers) {
2526
- total += num;
2527
- }
2528
- System.out.println("Array sum: " + total);
2529
-
2530
- // 正确输出变量
2531
- System.out.println(a);
2532
- System.out.println(b);
2533
- System.out.println(sum);
2534
- """
2535
-
2536
- execution = self.sandbox.run_code(code, language="java")
2537
- print(execution.to_json())
2538
- assert execution.error is None
2539
- assert any("Hello from IJAVA Kernel!" in line for line in execution.logs.stdout)
2540
- assert any("Sum: 30" in line for line in execution.logs.stdout)
2541
- assert any("Array sum: 15" in line for line in execution.logs.stdout)
2542
- logger.info("IJAVA basic execution test passed")
2543
-
2544
- def test_ijava_oop_features(self):
2545
- """测试IJAVA面向对象特性"""
2546
- assert self.sandbox is not None
2547
-
2548
- code = """
2549
- // IJAVA 面向对象特性测试
2550
- System.out.println("Testing IJAVA OOP features...");
2551
-
2552
- // 定义类
2553
- class Person {
2554
- private String name;
2555
- private int age;
2556
-
2557
- public Person(String name, int age) {
2558
- this.name = name;
2559
- this.age = age;
2560
- }
2561
-
2562
- public String getName() { return name; }
2563
- public int getAge() { return age; }
2564
-
2565
- public void introduce() {
2566
- System.out.println("Hi, I'm " + name + " and I'm " + age + " years old.");
2567
- }
2568
- }
2569
-
2570
- class Student extends Person {
2571
- private String major;
2572
-
2573
- public Student(String name, int age, String major) {
2574
- super(name, age);
2575
- this.major = major;
2576
- }
2577
-
2578
- @Override
2579
- public void introduce() {
2580
- super.introduce();
2581
- System.out.println("I'm studying " + major + ".");
2582
- }
2583
- }
2584
-
2585
- // 创建对象并测试
2586
- Person person = new Person("Alice", 25);
2587
- person.introduce();
2588
-
2589
- Student student = new Student("Bob", 22, "Computer Science");
2590
- student.introduce();
2591
-
2592
- // IJAVA 特色:直接输出对象信息
2593
- person.getName();
2594
- student.getAge();
2595
- student;
2596
-
2597
- System.out.println("IJAVA OOP test completed successfully!");
2598
- """
2599
-
2600
- execution = self.sandbox.run_code(code, language="java")
2601
- print(execution.to_json())
2602
- assert execution.error is None
2603
- assert any(
2604
- "Testing IJAVA OOP features..." in line for line in execution.logs.stdout
2605
- )
2606
- assert any("Hi, I'm Alice" in line for line in execution.logs.stdout)
2607
- assert any(
2608
- "I'm studying Computer Science" in line for line in execution.logs.stdout
2609
- )
2610
- logger.info("IJAVA OOP features test passed")
2611
-
2612
- def test_ijava_collections(self):
2613
- """测试IJAVA集合框架"""
2614
- assert self.sandbox is not None
2615
-
2616
- code = """
2617
- import java.util.*;
2618
-
2619
- System.out.println("Testing IJAVA Collections...");
2620
-
2621
- // ArrayList
2622
- List<String> fruits = new ArrayList<>();
2623
- fruits.add("Apple");
2624
- fruits.add("Banana");
2625
- fruits.add("Orange");
2626
- System.out.println("Fruits: " + fruits);
2627
-
2628
- // HashMap
2629
- Map<String, Integer> scores = new HashMap<>();
2630
- scores.put("Alice", 95);
2631
- scores.put("Bob", 87);
2632
- scores.put("Charlie", 92);
2633
- System.out.println("Scores: " + scores);
2634
-
2635
- // HashSet
2636
- Set<Integer> numbers = new HashSet<>();
2637
- numbers.add(1);
2638
- numbers.add(2);
2639
- numbers.add(3);
2640
- numbers.add(2); // 重复元素
2641
- System.out.println("Unique numbers: " + numbers);
2642
-
2643
- // 遍历集合
2644
- System.out.println("Iterating through fruits:");
2645
- for (String fruit : fruits) {
2646
- System.out.println("- " + fruit);
2647
- }
2648
-
2649
- // IJAVA 特色:直接输出集合内容
2650
- fruits;
2651
- scores;
2652
- numbers;
2653
-
2654
- // 集合操作
2655
- fruits.size();
2656
- scores.containsKey("Alice");
2657
- numbers.contains(2);
2658
-
2659
- System.out.println("IJAVA Collections test completed!");
2660
- """
2661
-
2662
- execution = self.sandbox.run_code(code, language="java")
2663
- print(execution.to_json())
2664
- assert execution.error is None
2665
- assert any(
2666
- "Testing IJAVA Collections..." in line for line in execution.logs.stdout
2667
- )
2668
- assert any(
2669
- "Fruits: [Apple, Banana, Orange]" in line for line in execution.logs.stdout
2670
- )
2671
- assert any(
2672
- "Unique numbers: [1, 2, 3]" in line for line in execution.logs.stdout
2673
- )
2674
- logger.info("IJAVA collections test passed")
2675
-
2676
- def test_ijava_file_io(self):
2677
- """测试IJAVA文件I/O"""
2678
- assert self.sandbox is not None
2679
-
2680
- code = """
2681
- import java.io.*;
2682
- import java.nio.file.*;
2683
- import java.util.*;
2684
-
2685
- System.out.println("Testing IJAVA File I/O...");
2686
-
2687
- try {
2688
- // 创建临时目录
2689
- Path tempDir = Files.createTempDirectory("ijava_demo");
2690
- System.out.println("Created temp directory: " + tempDir);
2691
-
2692
- // 用 List 拼装多行内容(避免跨行字面量)
2693
- List<String> lines = Arrays.asList(
2694
- "Hello from IJAVA File I/O!",
2695
- "This is a test file."
2696
- );
2697
- Path filePath = tempDir.resolve("test.txt");
2698
- Files.write(filePath, lines); // 直接写行列表
2699
- System.out.println("File written successfully");
2700
-
2701
- // 读取文件
2702
- List<String> readLines = Files.readAllLines(filePath);
2703
- System.out.println("File content:");
2704
- readLines.forEach(System.out::println);
2705
-
2706
- // 文件信息
2707
- long size = Files.size(filePath);
2708
- System.out.println("File size: " + size + " bytes");
2709
-
2710
- // 输出变量(改成打印语句)
2711
- System.out.println("filePath = " + filePath);
2712
- System.out.println("size = " + size);
2713
- System.out.println("exists = " + Files.exists(filePath));
2714
-
2715
- // 清理
2716
- Files.delete(filePath);
2717
- Files.delete(tempDir);
2718
- System.out.println("IJAVA File I/O test completed successfully!");
2719
-
2720
- } catch (IOException e) {
2721
- System.err.println("Error: " + e.getMessage());
2722
- }
2723
- """
2724
-
2725
- execution = self.sandbox.run_code(code, language="java")
2726
- print(execution.to_json())
2727
- assert execution.error is None
2728
- assert any(
2729
- "Testing IJAVA File I/O..." in line for line in execution.logs.stdout
2730
- )
2731
- assert any(
2732
- "File written successfully" in line for line in execution.logs.stdout
2733
- )
2734
- assert any(
2735
- "Hello from IJAVA File I/O!" in line for line in execution.logs.stdout
2736
- )
2737
- logger.info("IJAVA file I/O test passed")
2738
-
2739
- def test_ijava_context_management(self):
2740
- """测试IJAVA上下文管理"""
2741
- assert self.sandbox is not None
2742
-
2743
- # 创建IJAVA上下文
2744
- ijava_context = self.sandbox.create_code_context(language="java", cwd="/tmp")
2745
- self.contexts["ijava"] = ijava_context
2746
-
2747
- # 在上下文中定义类和变量
2748
- setup_code = """
2749
- System.out.println("Setting up IJAVA context...");
2750
-
2751
- // 定义全局变量
2752
- int counter = 0;
2753
- String message = "Hello from IJAVA Context!";
2754
-
2755
- System.out.println("Initial counter: " + counter);
2756
- System.out.println("Message: " + message);
2757
-
2758
- // 定义方法
2759
- void incrementCounter() {
2760
- counter++;
2761
- }
2762
-
2763
- int getCounter() {
2764
- return counter;
2765
- }
2766
-
2767
- // 定义类
2768
- class ContextDemo {
2769
- private static int staticCounter = 0;
2770
-
2771
- public static void incrementStaticCounter() {
2772
- staticCounter++;
2773
- }
2774
-
2775
- public static int getStaticCounter() {
2776
- return staticCounter;
2777
- }
2778
- }
2779
-
2780
- // 测试方法
2781
- incrementCounter();
2782
- System.out.println("Counter after increment: " + counter);
2783
-
2784
- // IJAVA 特色:直接输出变量值
2785
- counter;
2786
- message;
2787
- getCounter();
2788
- """
2789
-
2790
- execution1 = self.sandbox.run_code(setup_code, context=ijava_context)
2791
- print(execution1.to_json())
2792
- assert execution1.error is None
2793
- assert any(
2794
- "Setting up IJAVA context..." in line for line in execution1.logs.stdout
2795
- )
2796
- assert any("Initial counter: 0" in line for line in execution1.logs.stdout)
2797
-
2798
- # 在同一上下文中使用之前定义的变量和方法
2799
- use_code = """
2800
- System.out.println("Using IJAVA context...");
2801
-
2802
- // 使用之前定义的变量和方法
2803
- incrementCounter();
2804
- int currentCounter = getCounter();
2805
- System.out.println("Current counter: " + currentCounter);
2806
-
2807
- // 使用之前定义的类
2808
- ContextDemo.incrementStaticCounter();
2809
- int staticCounter = ContextDemo.getStaticCounter();
2810
- System.out.println("Static counter: " + staticCounter);
2811
-
2812
- // 创建新变量
2813
- String newMessage = "Modified context data";
2814
- System.out.println("New message: " + newMessage);
2815
-
2816
- // IJAVA 特色:直接输出所有变量
2817
- counter;
2818
- currentCounter;
2819
- staticCounter;
2820
- newMessage;
2821
- ContextDemo.getStaticCounter();
2822
-
2823
- System.out.println("IJAVA context usage completed!");
2824
- """
2825
-
2826
- execution2 = self.sandbox.run_code(use_code, context=ijava_context)
2827
- print(execution2.to_json())
2828
- assert execution2.error is None
2829
- assert any("Using IJAVA context..." in line for line in execution2.logs.stdout)
2830
- assert any("Current counter: 2" in line for line in execution2.logs.stdout)
2831
- assert any("Static counter: 1" in line for line in execution2.logs.stdout)
2832
- logger.info("IJAVA context management test passed")
2833
-
2834
- # 测试完成后立即清理context
2835
- try:
2836
- self.sandbox.destroy_context(ijava_context)
2837
- logger.info(f"Successfully destroyed IJAVA context: {ijava_context.id}")
2838
- # 从contexts字典中移除
2839
- if "ijava" in self.contexts:
2840
- del self.contexts["ijava"]
2841
- except Exception as e:
2842
- logger.warning(f"Failed to destroy IJAVA context {ijava_context.id}: {e}")
2843
-
2844
- # ======================== Deno 测试 ========================
2845
-
2846
- def test_deno_basic_execution(self):
2847
- """测试Deno基础执行"""
2848
- assert self.sandbox is not None
2849
-
2850
- code = """
2851
- // Deno 基础执行测试
2852
- console.log("Hello from Deno Kernel!");
2853
-
2854
- // 基础变量和运算
2855
- const a: number = 12;
2856
- const b: number = 18;
2857
- const sum: number = a + b;
2858
- const product: number = a * b;
2859
-
2860
- console.log(`Sum: ${sum}`);
2861
- console.log(`Product: ${product}`);
2862
-
2863
- // 字符串操作
2864
- const name: string = "DenoScaleBox";
2865
- const greeting: string = `Hello, ${name}!`;
2866
- console.log(greeting);
2867
-
2868
- // 数组操作
2869
- const numbers: number[] = [1, 2, 3, 4, 5];
2870
- const total: number = numbers.reduce((acc, num) => acc + num, 0);
2871
- console.log(`Array sum: ${total}`);
2872
-
2873
- // 对象操作
2874
- const person = {
2875
- name: "Alice",
2876
- age: 25,
2877
- city: "Tokyo"
2878
- };
2879
- console.log(`Person: ${person.name}, ${person.age} years old`);
2880
- """
2881
-
2882
- execution = self.sandbox.run_code(code, language="typescript")
2883
- print(execution.to_json())
2884
- assert execution.error is None
2885
- assert any("Hello from Deno Kernel!" in line for line in execution.logs.stdout)
2886
- assert any("Sum: 30" in line for line in execution.logs.stdout)
2887
- assert any("Array sum: 15" in line for line in execution.logs.stdout)
2888
- logger.info("Deno basic execution test passed")
2889
-
2890
- def test_deno_typescript_features(self):
2891
- """测试Deno TypeScript特性"""
2892
- assert self.sandbox is not None
2893
-
2894
- code = """
2895
- // Deno TypeScript 特性测试
2896
- interface User {
2897
- id: number;
2898
- name: string;
2899
- email: string;
2900
- isActive: boolean;
2901
- }
2902
-
2903
- class UserManager {
2904
- private users: User[] = [];
2905
-
2906
- constructor() {
2907
- console.log("UserManager initialized");
2908
- }
2909
-
2910
- addUser(user: User): void {
2911
- this.users.push(user);
2912
- console.log(`Added user: ${user.name}`);
2913
- }
2914
-
2915
- getUsers(): User[] {
2916
- return this.users;
2917
- }
2918
-
2919
- findUserById(id: number): User | undefined {
2920
- return this.users.find(user => user.id === id);
2921
- }
2922
- }
2923
-
2924
- // 使用泛型
2925
- function processItems<T>(items: T[], processor: (item: T) => void): void {
2926
- items.forEach(processor);
2927
- }
2928
-
2929
- // 枚举
2930
- enum Status {
2931
- PENDING = "pending",
2932
- APPROVED = "approved",
2933
- REJECTED = "rejected"
2934
- }
2935
-
2936
- console.log("Testing Deno TypeScript features...");
2937
-
2938
- const userManager = new UserManager();
2939
- userManager.addUser({
2940
- id: 1,
2941
- name: "John Doe",
2942
- email: "john@example.com",
2943
- isActive: true
2944
- });
2945
-
2946
- userManager.addUser({
2947
- id: 2,
2948
- name: "Jane Smith",
2949
- email: "jane@example.com",
2950
- isActive: false
2951
- });
2952
-
2953
- const users = userManager.getUsers();
2954
- console.log(`Total users: ${users.length}`);
2955
-
2956
- const foundUser = userManager.findUserById(1);
2957
- console.log(`Found user: ${foundUser?.name}`);
2958
-
2959
- // 使用泛型函数
2960
- const numbers = [1, 2, 3, 4, 5];
2961
- processItems(numbers, (num) => console.log(`Processing: ${num}`));
2962
-
2963
- console.log(`Status: ${Status.APPROVED}`);
2964
- console.log("TypeScript features test completed!");
2965
- """
2966
-
2967
- execution = self.sandbox.run_code(code, language="typescript")
2968
- print(execution.to_json())
2969
- assert execution.error is None
2970
- assert any(
2971
- "Testing Deno TypeScript features..." in line
2972
- for line in execution.logs.stdout
2973
- )
2974
- assert any("Added user: John Doe" in line for line in execution.logs.stdout)
2975
- assert any("Total users: 2" in line for line in execution.logs.stdout)
2976
- logger.info("Deno TypeScript features test passed")
2977
-
2978
- def test_deno_async_await(self):
2979
- """测试Deno异步/await"""
2980
- assert self.sandbox is not None
2981
-
2982
- code = """
2983
- // Deno 异步/await 测试
2984
- async function delay(ms: number): Promise<void> {
2985
- return new Promise(resolve => setTimeout(resolve, ms));
2986
- }
2987
-
2988
- async function fetchData(id: number): Promise<{ id: number; data: string }> {
2989
- await delay(50);
2990
- return { id, data: `Data for ID ${id}` };
2991
- }
2992
-
2993
- async function processBatch(ids: number[]): Promise<string[]> {
2994
- console.log("Starting batch processing...");
2995
- const start = Date.now();
2996
-
2997
- const results = await Promise.all(
2998
- ids.map(async (id) => {
2999
- const result = await fetchData(id);
3000
- console.log(`Processed: ${result.data}`);
3001
- return result.data;
3002
- })
3003
- );
3004
-
3005
- const duration = Date.now() - start;
3006
- console.log(`Batch processing completed in ${duration}ms`);
3007
- return results;
3008
- }
3009
-
3010
- async function main(): Promise<void> {
3011
- console.log("Testing Deno async/await...");
3012
- const ids = [1, 2, 3, 4, 5];
3013
- const results = await processBatch(ids);
3014
- console.log(`Total results: ${results.length}`);
3015
- console.log("Async/await test completed!");
3016
- }
3017
-
3018
- // 顶层 await,让内核等待完成
3019
- await main();
3020
- """
3021
-
3022
- execution = self.sandbox.run_code(code, language="typescript")
3023
- print(execution.to_json())
3024
- assert execution.error is None
3025
- assert any(
3026
- "Testing Deno async/await..." in line for line in execution.logs.stdout
3027
- )
3028
- assert any(
3029
- "Starting batch processing..." in line for line in execution.logs.stdout
3030
- )
3031
- assert any(
3032
- "Batch processing completed" in line for line in execution.logs.stdout
3033
- )
3034
- logger.info("Deno async/await test passed")
3035
-
3036
- def test_deno_file_operations(self):
3037
- """测试Deno文件操作"""
3038
- assert self.sandbox is not None
3039
-
3040
- code = """
3041
- // Deno 文件操作测试(原生 API,兼容 1.x+)
3042
- await (async () => {
3043
- console.log("Testing Deno file operations...");
3044
-
3045
- try {
3046
- const tempDir = "/tmp/deno_demo";
3047
- await Deno.mkdir(tempDir, { recursive: true });
3048
- console.log(`Created directory: ${tempDir}`);
3049
-
3050
- const filePath = `${tempDir}/test.txt`;
3051
- const content = "Hello from Deno File Operations!This is a test file.";
3052
- await Deno.writeTextFile(filePath, content);
3053
- console.log("File written successfully");
3054
-
3055
- const readContent = await Deno.readTextFile(filePath);
3056
- console.log("File content:");
3057
- console.log(readContent);
3058
-
3059
- const { size } = await Deno.stat(filePath);
3060
- console.log(`File size: ${size} bytes`);
3061
-
3062
- await Deno.remove(tempDir, { recursive: true });
3063
- console.log("File operations test completed successfully!");
3064
- } catch (error) {
3065
- console.error(`Error: ${error.message}`);
3066
- }
3067
- })();
3068
- """
3069
-
3070
- execution = self.sandbox.run_code(code, language="typescript")
3071
- print(execution.to_json())
3072
- assert execution.error is None
3073
- assert any(
3074
- "Testing Deno file operations..." in line for line in execution.logs.stdout
3075
- )
3076
- assert any(
3077
- "File written successfully" in line for line in execution.logs.stdout
3078
- )
3079
- assert any(
3080
- "Hello from Deno File Operations!" in line for line in execution.logs.stdout
3081
- )
3082
- logger.info("Deno file operations test passed")
3083
-
3084
- def test_deno_context_management(self):
3085
- """测试Deno上下文管理"""
3086
- assert self.sandbox is not None
3087
-
3088
- # 创建Deno上下文
3089
- deno_context = self.sandbox.create_code_context(
3090
- language="typescript", cwd="/tmp"
3091
- )
3092
- self.contexts["deno"] = deno_context
3093
-
3094
- # 在上下文中定义变量和函数
3095
- setup_code = """
3096
- // Deno 上下文设置
3097
- console.log("Setting up Deno context...");
3098
-
3099
- // 定义全局变量
3100
- let counter: number = 0;
3101
- const cache: Map<string, any> = new Map();
3102
-
3103
- // 定义函数
3104
- function incrementCounter(): number {
3105
- counter++;
3106
- return counter;
3107
- }
3108
-
3109
- function addToCache(key: string, value: any): number {
3110
- cache.set(key, value);
3111
- return cache.size;
3112
- }
3113
-
3114
- // 定义接口
3115
- interface ContextData {
3116
- id: number;
3117
- value: string;
3118
- }
3119
-
3120
- const contextData: ContextData = {
3121
- id: 1,
3122
- value: "Hello from Deno Context!"
3123
- };
3124
-
3125
- console.log(`Initial counter: ${counter}`);
3126
- console.log(`Cache size: ${cache.size}`);
3127
- console.log(`Context data: ${contextData.value}`);
3128
- """
3129
-
3130
- execution1 = self.sandbox.run_code(setup_code, context=deno_context)
3131
- print(execution1.to_json())
3132
- assert execution1.error is None
3133
- assert any(
3134
- "Setting up Deno context..." in line for line in execution1.logs.stdout
3135
- )
3136
- assert any("Initial counter: 0" in line for line in execution1.logs.stdout)
3137
-
3138
- # 在同一上下文中使用之前定义的变量和函数
3139
- use_code = """
3140
- // 使用 Deno 上下文中的变量和函数
3141
- console.log("Using Deno context...");
3142
-
3143
- // 使用全局变量
3144
- console.log(`Current counter: ${counter}`);
3145
- console.log(`Current cache size: ${cache.size}`);
3146
-
3147
- // 使用函数
3148
- const newCounter = incrementCounter();
3149
- console.log(`Counter after increment: ${newCounter}`);
3150
-
3151
- const newCacheSize = addToCache("test_key", "test_value");
3152
- console.log(`Cache size after addition: ${newCacheSize}`);
3153
-
3154
- // 使用上下文数据
3155
- console.log(`Context data ID: ${contextData.id}`);
3156
- console.log(`Context data value: ${contextData.value}`);
3157
-
3158
- // 修改上下文数据
3159
- contextData.value = "Modified context data";
3160
- console.log(`Modified context data: ${contextData.value}`);
3161
-
3162
- console.log("Context usage completed!");
3163
- """
3164
-
3165
- execution2 = self.sandbox.run_code(use_code, context=deno_context)
3166
- print(execution2.to_json())
3167
- assert execution2.error is None
3168
- assert any("Using Deno context..." in line for line in execution2.logs.stdout)
3169
- assert any(
3170
- "Counter after increment: 1" in line for line in execution2.logs.stdout
3171
- )
3172
- assert any(
3173
- "Cache size after addition: 1" in line for line in execution2.logs.stdout
3174
- )
3175
- logger.info("Deno context management test passed")
3176
-
3177
- # 测试完成后立即清理context
3178
- try:
3179
- self.sandbox.destroy_context(deno_context)
3180
- logger.info(f"Successfully destroyed Deno context: {deno_context.id}")
3181
- # 从contexts字典中移除
3182
- if "deno" in self.contexts:
3183
- del self.contexts["deno"]
3184
- except Exception as e:
3185
- logger.warning(f"Failed to destroy Deno context {deno_context.id}: {e}")
3186
-
3187
- # ======================== 高级功能测试 ========================
3188
-
3189
- def test_web_request_simulation(self):
3190
- """测试网络请求模拟"""
3191
- assert self.sandbox is not None
3192
-
3193
- code = """
3194
- import json
3195
- import time
3196
- from datetime import datetime
3197
-
3198
- def simulate_api_call(url, method="GET", data=None):
3199
- '''模拟API调用'''
3200
- # 模拟网络延迟
3201
- time.sleep(0.1)
3202
-
3203
- # 根据URL返回不同的模拟数据
3204
- if "users" in url:
3205
- return {
3206
- "status_code": 200,
3207
- "data": [
3208
- {"id": 1, "name": "Alice", "email": "alice@example.com"},
3209
- {"id": 2, "name": "Bob", "email": "bob@example.com"}
3210
- ],
3211
- "timestamp": datetime.now().isoformat()
3212
- }
3213
- elif "weather" in url:
3214
- return {
3215
- "status_code": 200,
3216
- "data": {
3217
- "location": "Beijing",
3218
- "temperature": 22,
3219
- "humidity": 65,
3220
- "description": "晴朗"
3221
- },
3222
- "timestamp": datetime.now().isoformat()
3223
- }
3224
- else:
3225
- return {
3226
- "status_code": 404,
3227
- "error": "Not Found",
3228
- "timestamp": datetime.now().isoformat()
3229
- }
3230
-
3231
- print("开始API调用模拟...")
3232
-
3233
- # 模拟多个API调用
3234
- apis = [
3235
- "https://api.example.com/users",
3236
- "https://api.example.com/weather",
3237
- "https://api.example.com/unknown"
3238
- ]
3239
-
3240
- results = []
3241
- for api_url in apis:
3242
- print(f"\\n调用 {api_url}")
3243
- response = simulate_api_call(api_url)
3244
- results.append({
3245
- "url": api_url,
3246
- "status": response["status_code"],
3247
- "response": response
3248
- })
3249
- print(f"状态码: {response['status_code']}")
3250
- if response["status_code"] == 200:
3251
- print(f"数据: {json.dumps(response['data'], ensure_ascii=False, indent=2)}")
3252
-
3253
- print(f"\\n完成 {len(results)} 个API调用")
3254
-
3255
- {
3256
- "total_calls": len(results),
3257
- "successful_calls": sum(1 for r in results if r["status"] == 200),
3258
- "results": results
3259
- }
3260
- """
3261
-
3262
- execution = self.sandbox.run_code(code, language="python")
3263
- print(execution.to_json())
3264
- assert execution.error is None
3265
- assert any("API调用模拟" in line for line in execution.logs.stdout)
3266
-
3267
- # ======================== 主测试执行器 ========================
3268
-
3269
- def run_all_tests(self):
3270
- """运行所有测试"""
3271
- logger.info("开始CodeInterpreter综合验证测试...")
3272
-
3273
- # 基础操作测试
3274
- self.run_test(self.test_code_interpreter_creation, "CodeInterpreter Creation")
3275
- self.run_test(self.test_basic_python_execution, "Basic Python Execution")
3276
- self.run_test(self.test_math_calculations, "Math Calculations")
3277
- self.run_test(self.test_data_processing, "Data Processing")
3278
- self.run_test(self.test_visualization_code, "Visualization Code")
3279
-
3280
- # 回调函数测试
3281
- self.run_test(self.test_callback_handling, "Callback Handling")
3282
- self.run_test(self.test_error_handling, "Error Handling")
3283
-
3284
- # 上下文管理测试
3285
- self.run_test(self.test_context_creation, "Context Creation")
3286
- self.run_test(self.test_context_persistence, "Context Persistence")
3287
- self.run_test(self.test_multiple_contexts, "Multiple Contexts")
3288
-
3289
- # 数据类型测试
3290
- self.run_test(self.test_different_data_types, "Different Data Types")
3291
- self.run_test(
3292
- self.test_file_operations_simulation, "File Operations Simulation"
3293
- )
3294
-
3295
- # 性能测试
3296
- self.run_test(
3297
- self.test_performance_simple_calculations, "Performance Simple Calculations"
3298
- )
3299
- self.run_test(
3300
- self.test_performance_concurrent_simulation,
3301
- "Performance Concurrent Simulation",
3302
- )
3303
-
3304
- # 结果格式测试
3305
- self.run_test(self.test_text_result, "Text Result Format")
3306
- self.run_test(self.test_html_result, "HTML Result Format")
3307
- self.run_test(self.test_markdown_result, "Markdown Result Format")
3308
- self.run_test(self.test_svg_result, "SVG Result Format")
3309
- self.run_test(self.test_image_results, "Image Result Formats (PNG/JPEG)")
3310
- self.run_test(self.test_latex_result, "LaTeX Result Format")
3311
- self.run_test(self.test_json_data_result, "JSON Data Result Format")
3312
- self.run_test(self.test_javascript_result, "JavaScript Result Format")
3313
- self.run_test(self.test_chart_data_result, "Chart Data Result Format")
3314
- self.run_test(self.test_mixed_format_result, "Mixed Format Result")
3315
-
3316
- # R语言测试
3317
- self.run_test(
3318
- self.test_r_language_basic_execution, "R Language Basic Execution"
3319
- )
3320
- self.run_test(self.test_r_language_data_analysis, "R Language Data Analysis")
3321
- self.run_test(self.test_r_language_visualization, "R Language Visualization")
3322
- self.run_test(self.test_r_language_statistics, "R Language Statistics")
3323
- self.run_test(
3324
- self.test_r_language_context_management, "R Language Context Management"
3325
- )
3326
-
3327
- # Node.js/JavaScript 测试
3328
- self.run_test(self.test_nodejs_basic_execution, "Node.js Basic Execution")
3329
- self.run_test(self.test_nodejs_async_promises, "Node.js Async Promises")
3330
- self.run_test(self.test_nodejs_data_processing, "Node.js Data Processing")
3331
- self.run_test(self.test_nodejs_chart_data, "Node.js Chart Data Generation")
3332
- self.run_test(self.test_nodejs_context_management, "Node.js Context Management")
3333
-
3334
- # Bash 测试
3335
- self.run_test(self.test_bash_basic_execution, "Bash Basic Execution")
3336
- self.run_test(self.test_bash_file_operations, "Bash File Operations")
3337
- self.run_test(self.test_bash_pipelines_and_grep, "Bash Pipelines and Grep")
3338
- # self.run_test(self.test_bash_env_and_exit_codes, "Bash Env and Exit Codes")
3339
- self.run_test(self.test_bash_context_management, "Bash Context Management")
3340
-
3341
- # IJAVA 测试
3342
- self.run_test(self.test_ijava_basic_execution, "IJAVA Basic Execution")
3343
- self.run_test(self.test_ijava_oop_features, "IJAVA OOP Features")
3344
- self.run_test(self.test_ijava_collections, "IJAVA Collections")
3345
- self.run_test(self.test_ijava_file_io, "IJAVA File I/O")
3346
- self.run_test(self.test_ijava_context_management, "IJAVA Context Management")
3347
-
3348
- # Deno 测试
3349
- self.run_test(self.test_deno_basic_execution, "Deno Basic Execution")
3350
- self.run_test(self.test_deno_typescript_features, "Deno TypeScript Features")
3351
- self.run_test(self.test_deno_async_await, "Deno Async/Await")
3352
- self.run_test(self.test_deno_file_operations, "Deno File Operations")
3353
- self.run_test(self.test_deno_context_management, "Deno Context Management")
3354
-
3355
- # 高级功能测试
3356
- self.run_test(self.test_web_request_simulation, "Web Request Simulation")
3357
-
3358
- def cleanup(self):
3359
- """清理资源"""
3360
- # 清理剩余的上下文
3361
- for name, context in self.contexts.items():
3362
- try:
3363
- self.sandbox.destroy_context(context)
3364
- logger.info(f"Successfully destroyed context {name}: {context.id}")
3365
- except Exception as e:
3366
- logger.warning(f"Error cleaning up context {name}: {e}")
3367
-
3368
- # 清空contexts字典
3369
- self.contexts.clear()
3370
-
3371
- # 清理沙箱
3372
- if self.sandbox:
3373
- try:
3374
- self.sandbox.kill()
3375
- logger.info("CodeInterpreter sandbox cleaned up successfully")
3376
- except Exception as e:
3377
- logger.error(f"Error cleaning up sandbox: {e}")
3378
-
3379
- def print_summary(self):
3380
- """打印测试摘要"""
3381
- total_tests = len(self.test_results)
3382
- passed_tests = sum(1 for r in self.test_results if r["success"])
3383
- failed_tests = total_tests - passed_tests
3384
-
3385
- total_duration = sum(r["duration"] for r in self.test_results)
3386
-
3387
- print("\n" + "=" * 60)
3388
- print("CodeInterpreter综合验证测试报告")
3389
- print("=" * 60)
3390
- print(f"总测试数: {total_tests}")
3391
- print(f"通过数: {passed_tests}")
3392
- print(f"失败数: {failed_tests}")
3393
- print(f"总耗时: {total_duration:.3f}秒")
3394
- print(f"成功率: {(passed_tests/total_tests*100):.1f}%")
3395
-
3396
- if self.failed_tests:
3397
- print(f"\n失败的测试:")
3398
- for test in self.failed_tests:
3399
- print(f" ❌ {test}")
3400
-
3401
- print("=" * 60)
3402
-
3403
-
3404
- def main():
3405
- """主函数"""
3406
- validator = CodeInterpreterValidator()
3407
-
3408
- try:
3409
- validator.run_all_tests()
3410
- finally:
3411
- validator.cleanup()
3412
- validator.print_summary()
3413
-
3414
-
3415
- if __name__ == "__main__":
3416
- main()