TestPythonRuntimeToolInjection 测试 Python 运行时工具注入
(t *testing.T)
| 183 | |
| 184 | // TestPythonRuntimeToolInjection 测试 Python 运行时工具注入 |
| 185 | func TestPythonRuntimeToolInjection(t *testing.T) { |
| 186 | runtime := NewPythonRuntime(nil) |
| 187 | |
| 188 | // 测试无工具的简单模式 |
| 189 | t.Run("NoToolsMode", func(t *testing.T) { |
| 190 | code := "print('Hello World')" |
| 191 | result, err := runtime.Execute(context.Background(), code, map[string]any{}) |
| 192 | if err != nil { |
| 193 | t.Fatalf("Failed to execute: %v", err) |
| 194 | } |
| 195 | if !result.Success { |
| 196 | t.Error("Expected success") |
| 197 | } |
| 198 | }) |
| 199 | |
| 200 | // 测试带工具的 PTC 模式 |
| 201 | t.Run("PTCMode", func(t *testing.T) { |
| 202 | runtime.SetTools([]string{"Read", "Write"}) |
| 203 | runtime.SetBridgeURL("http://localhost:8080") |
| 204 | |
| 205 | // 验证生成的代码包含工具注入 |
| 206 | wrappedCode := runtime.wrapCode("print('test')", map[string]any{}) |
| 207 | |
| 208 | // 检查是否包含关键组件 |
| 209 | if !contains(wrappedCode, "_AsterBridge") { |
| 210 | t.Error("Expected _AsterBridge class in wrapped code") |
| 211 | } |
| 212 | if !contains(wrappedCode, "async def _user_main()") { |
| 213 | t.Error("Expected async main wrapper") |
| 214 | } |
| 215 | if !contains(wrappedCode, "Read") { |
| 216 | t.Error("Expected Read tool in injected tools") |
| 217 | } |
| 218 | }) |
| 219 | } |
| 220 | |
| 221 | // contains 检查字符串是否包含子串 |
| 222 | func contains(s, substr string) bool { |
nothing calls this directly
no test coverage detected