DART-LLM_Task_Decomposer / test_safety_workflow.py
YongdongWang's picture
Upload folder using huggingface_hub
92ef79b verified
#!/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)