File size: 4,336 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
#!/usr/bin/env python3
"""
Test script for safety confirmation workflow
"""

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

from gradio_llm_interface import GradioLlmInterface
import json

def test_safety_workflow():
    """Test the safety confirmation workflow"""
    print("Testing Safety Confirmation Workflow...")
    print("=" * 50)
    
    # Create mock task data
    mock_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"]
                }
            },
            {
                "task": "transport_soil_to_pit",
                "instruction_function": {
                    "name": "transport_soil_to_pit", 
                    "robot_ids": ["robot_dump_truck_01"],
                    "dependencies": ["excavate_soil_from_pile"],
                    "object_keywords": ["pit"]
                }
            }
        ]
    }
    
    # Initialize interface
    interface = GradioLlmInterface()
    
    # Create mock state with pending task plan
    mock_state = {
        'pending_task_plan': mock_task_data,
        'history': []
    }
    
    print("βœ“ Mock state created with pending task plan")
    print(f"  Tasks: {len(mock_task_data['tasks'])}")
    
    # Test validation and deployment
    try:
        result = interface.validate_and_deploy_task_plan(mock_state)
        
        if result:
            confirmation_msg, approved_image_path, button_update, updated_state = result
            
            print("\nπŸ“‹ Validation Results:")
            print(f"  Confirmation Message: {confirmation_msg[:100]}...")
            print(f"  Image Generated: {'βœ“' if approved_image_path else 'βœ—'}")
            print(f"  Button Hidden: {'βœ“' if not button_update.get('visible', True) else 'βœ—'}")
            print(f"  State Updated: {'βœ“' if updated_state.get('pending_task_plan') is None else 'βœ—'}")
            
            return True
        else:
            print("βœ— No result returned from validation function")
            return False
            
    except Exception as e:
        print(f"βœ— Error during validation: {e}")
        return False

def test_empty_state():
    """Test with empty state (no pending task plan)"""
    print("\nTesting Empty State Handling...")
    print("=" * 30)
    
    interface = GradioLlmInterface()
    empty_state = {'history': []}
    
    try:
        result = interface.validate_and_deploy_task_plan(empty_state)
        
        if result:
            warning_msg, image_path, button_update, state = result
            
            if "No Task Plan to Deploy" in warning_msg:
                print("βœ“ Empty state handled correctly")
                return True
            else:
                print("βœ— Unexpected warning message")
                return False
        else:
            print("βœ— No result returned for empty state")
            return False
            
    except Exception as e:
        print(f"βœ— Error handling empty state: {e}")
        return False

def main():
    """Run safety workflow tests"""
    print("πŸ”’ Safety Confirmation Workflow Tests")
    print("=" * 50)
    
    tests = [
        test_safety_workflow,
        test_empty_state
    ]
    
    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"Safety Tests passed: {passed}/{total}")
    
    if passed == total:
        print("πŸŽ‰ All safety workflow tests passed!")
        print("\nπŸ” Safety Features:")
        print("  βœ“ Task plan approval workflow")
        print("  βœ“ Visual confirmation before deployment") 
        print("  βœ“ Error handling and validation")
        print("  βœ“ State management for pending plans")
        return True
    else:
        print("❌ Some safety tests failed!")
        return False

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