File size: 7,053 Bytes
92ef79b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python3
"""
Test script for the enhanced editing workflow
Tests: Edit Task Plan β†’ Update DAG Visualization β†’ Validate & Deploy
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from gradio_llm_interface import GradioLlmInterface
import json

def test_task_plan_editor():
    """Test task plan editor functionality"""
    print("Testing Task Plan Editor...")
    print("=" * 40)
    
    # Create test task data
    test_task_data = {
        "tasks": [
            {
                "task": "excavate_soil_from_pile",
                "instruction_function": {
                    "name": "excavate_soil_from_pile",
                    "robot_ids": ["robot_excavator_01"],
                    "dependencies": [],
                    "object_keywords": ["soil_pile"]
                }
            }
        ]
    }
    
    interface = GradioLlmInterface()
    
    # Test with existing task plan
    state_with_plan = {'pending_task_plan': test_task_data}
    result = interface.show_task_plan_editor(state_with_plan)
    
    if result and len(result) == 4:
        editor_update, dag_btn_update, validate_btn_update, status_msg = result
        
        print("βœ“ Editor opened with existing task plan")
        print(f"  Status: {status_msg[:50]}...")
        print(f"  Editor visible: {'βœ“' if editor_update.get('visible') else 'βœ—'}")
        print(f"  JSON populated: {'βœ“' if editor_update.get('value') else 'βœ—'}")
        
        return True
    else:
        print("βœ— Failed to open task plan editor")
        return False

def test_dag_update_from_editor():
    """Test DAG update from edited JSON"""
    print("\nTesting DAG Update from Editor...")
    print("=" * 40)
    
    # Sample edited JSON
    edited_json = """{
  "tasks": [
    {
      "task": "move_to_position_1",
      "instruction_function": {
        "name": "move_to_position",
        "robot_ids": ["robot_dump_truck_01"],
        "dependencies": [],
        "object_keywords": ["loading_zone"]
      }
    },
    {
      "task": "load_material_1",
      "instruction_function": {
        "name": "load_material",
        "robot_ids": ["robot_dump_truck_01"],
        "dependencies": ["move_to_position_1"],
        "object_keywords": ["soil", "material"]
      }
    }
  ]
}"""
    
    interface = GradioLlmInterface()
    state = {}
    
    try:
        result = interface.update_dag_from_editor(edited_json, state)
        
        if result and len(result) == 6:
            dag_image, validate_btn, editor_vis, dag_btn_vis, status_msg, updated_state = result
            
            print("βœ“ DAG updated from edited JSON")
            print(f"  Image generated: {'βœ“' if dag_image else 'βœ—'}")
            print(f"  Validate button shown: {'βœ“' if validate_btn.get('visible') else 'βœ—'}")
            print(f"  Task plan stored: {'βœ“' if updated_state.get('pending_task_plan') else 'βœ—'}")
            
            return True
        else:
            print("βœ— Failed to update DAG from editor")
            return False
            
    except Exception as e:
        print(f"βœ— Error updating DAG: {e}")
        return False

def test_invalid_json_handling():
    """Test handling of invalid JSON"""
    print("\nTesting Invalid JSON Handling...")
    print("=" * 40)
    
    invalid_json = """{
  "tasks": [
    {
      "task": "invalid_task"
      "missing_comma": true
    }
  ]
}"""
    
    interface = GradioLlmInterface()
    state = {}
    
    try:
        result = interface.update_dag_from_editor(invalid_json, state)
        
        if result and len(result) == 6:
            dag_image, validate_btn, editor_vis, dag_btn_vis, status_msg, updated_state = result
            
            if "JSON Parsing Error" in status_msg:
                print("βœ“ Invalid JSON handled correctly")
                print(f"  Error message displayed: {'βœ“' if 'JSON Parsing Error' in status_msg else 'βœ—'}")
                print(f"  Editor kept visible: {'βœ“' if editor_vis.get('visible') else 'βœ—'}")
                return True
            else:
                print("βœ— Invalid JSON not handled correctly")
                return False
        else:
            print("βœ— Unexpected result from invalid JSON")
            return False
            
    except Exception as e:
        print(f"βœ— Unexpected exception: {e}")
        return False

def test_full_workflow():
    """Test the complete editing workflow"""
    print("\nTesting Complete Editing Workflow...")
    print("=" * 40)
    
    interface = GradioLlmInterface()
    
    # Step 1: Open editor
    state = {}
    print("Step 1: Opening task plan editor...")
    editor_result = interface.show_task_plan_editor(state)
    
    if not editor_result:
        print("βœ— Failed at step 1")
        return False
    
    # Step 2: Update DAG with valid JSON
    valid_json = """{
  "tasks": [
    {
      "task": "complete_workflow_test",
      "instruction_function": {
        "name": "test_function",
        "robot_ids": ["robot_excavator_01"],
        "dependencies": [],
        "object_keywords": ["test_object"]
      }
    }
  ]
}"""
    
    print("Step 2: Updating DAG from editor...")
    update_result = interface.update_dag_from_editor(valid_json, state)
    
    if not update_result or len(update_result) != 6:
        print("βœ— Failed at step 2")
        return False
    
    # Step 3: Validate and deploy
    print("Step 3: Validating and deploying...")
    deploy_result = interface.validate_and_deploy_task_plan(state)
    
    if deploy_result:
        print("βœ“ Complete workflow test passed")
        return True
    else:
        print("βœ— Failed at step 3")
        return False

def main():
    """Run all editing workflow tests"""
    print("πŸ› οΈ Enhanced Editing Workflow Tests")
    print("=" * 50)
    
    tests = [
        test_task_plan_editor,
        test_dag_update_from_editor,
        test_invalid_json_handling,
        test_full_workflow
    ]
    
    passed = 0
    total = len(tests)
    
    for test in tests:
        try:
            if test():
                passed += 1
        except Exception as e:
            print(f"βœ— Test failed with exception: {e}")
    
    print("\n" + "=" * 50)
    print(f"Editing Workflow Tests passed: {passed}/{total}")
    
    if passed == total:
        print("πŸŽ‰ All editing workflow tests passed!")
        print("\nπŸ”§ Enhanced Workflow Features:")
        print("  βœ“ Manual JSON editing capability")
        print("  βœ“ Real-time DAG visualization updates")
        print("  βœ“ JSON validation and error handling")
        print("  βœ“ Three-step safety workflow:")
        print("    1. πŸ“ Edit Task Plan")
        print("    2. πŸ”„ Update DAG Visualization") 
        print("    3. πŸ”’ Validate & Deploy Task Plan")
        return True
    else:
        print("❌ Some editing workflow tests failed!")
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)