From f9e67bfc3d49618789537289d39d5c4359331746 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 10 Jul 2025 21:36:40 +0800 Subject: [PATCH] fix: template --- src-tauri/src/python_executor.rs | 12 +++++++--- src-tauri/src/test_template.rs | 38 ++++++++++++++++++++++++++++++++ test_template_loading.py | 30 +++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 src-tauri/src/test_template.rs create mode 100644 test_template_loading.py diff --git a/src-tauri/src/python_executor.rs b/src-tauri/src/python_executor.rs index b015d91..1c7bf33 100644 --- a/src-tauri/src/python_executor.rs +++ b/src-tauri/src/python_executor.rs @@ -145,9 +145,15 @@ pub async fn execute_python_command( } else if json_value.get("result").is_some() || json_value.get("error").is_some() { // This is a final result or error response if let Ok(mut result) = final_result_clone.lock() { - *result = Some(json_str.to_string()); - } - println!("JSON-RPC result found: {}", json_str); + // Extract the result or error content from JSON-RPC + if let Some(result_content) = json_value.get("result") { + *result = Some(result_content.to_string()); + println!("JSON-RPC result extracted: {}", result_content); + } else if let Some(error_content) = json_value.get("error") { + *result = Some(format!("{{\"status\":false,\"error\":{}}}", error_content)); + println!("JSON-RPC error extracted: {}", error_content); + } + }; } } } else if line.trim().starts_with('{') && line.trim().ends_with('}') { diff --git a/src-tauri/src/test_template.rs b/src-tauri/src/test_template.rs new file mode 100644 index 0000000..e8f374d --- /dev/null +++ b/src-tauri/src/test_template.rs @@ -0,0 +1,38 @@ +use crate::python_executor::execute_python_command; + +#[tokio::test] +async fn test_get_templates() { + let app = tauri::test::mock_app(); + let args = vec![ + "-m".to_string(), + "python_core.services.template_manager".to_string(), + "--action".to_string(), + "get_templates".to_string(), + ]; + + match execute_python_command(app, &args, None).await { + Ok(result) => { + println!("Result: {}", result); + // Parse the result to verify it's valid JSON + match serde_json::from_str::(&result) { + Ok(json) => { + println!("Parsed JSON successfully"); + if let Some(status) = json.get("status") { + println!("Status: {}", status); + } + if let Some(templates) = json.get("templates") { + if let Some(array) = templates.as_array() { + println!("Found {} templates", array.len()); + } + } + } + Err(e) => { + println!("Failed to parse JSON: {}", e); + } + } + } + Err(e) => { + println!("Error: {}", e); + } + } +} diff --git a/test_template_loading.py b/test_template_loading.py new file mode 100644 index 0000000..79a56e7 --- /dev/null +++ b/test_template_loading.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +""" +Test script to verify template loading functionality +""" + +import sys +import json +from python_core.services.template_manager import TemplateManager +from python_core.utils.jsonrpc import create_response_handler + +def test_get_templates(): + """Test getting templates""" + rpc = create_response_handler("test") + + try: + manager = TemplateManager() + templates = manager.get_templates() + + result = { + "status": True, + "templates": [template.__dict__ for template in templates] + } + + rpc.success(result) + + except Exception as e: + rpc.error(-32603, f"Failed to get templates: {str(e)}") + +if __name__ == "__main__": + test_get_templates()