fleetmind-dispatch-ai / test_complete_delivery.py
mashrur950's picture
Implement migrations for assignments management and testing framework
5b558e5
raw
history blame
6.01 kB
"""
Test script for complete_delivery tool
Verifies that delivery completion updates driver location correctly
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from chat.tools import (
handle_create_order,
handle_create_driver,
handle_create_assignment,
handle_complete_delivery,
handle_get_driver_details,
handle_get_assignment_details
)
print("=" * 70)
print("Testing Delivery Completion Workflow")
print("=" * 70)
# Step 1: Create test order
print("\n[1] Creating test order...")
order_result = handle_create_order({
"customer_name": "Completion Test Customer",
"customer_phone": "+8801712345678",
"delivery_address": "Ahsanullah University, Dhaka",
"delivery_lat": 23.7808,
"delivery_lng": 90.4130,
"priority": "standard",
"weight_kg": 3.0
})
if not order_result.get("success"):
print(f"FAILED: {order_result.get('error')}")
sys.exit(1)
order_id = order_result["order_id"]
print(f"SUCCESS: Order created: {order_id}")
# Step 2: Create test driver at different location
print("\n[2] Creating test driver at starting location...")
driver_result = handle_create_driver({
"name": "Completion Test Driver",
"phone": "+8801812345678",
"vehicle_type": "motorcycle",
"current_lat": 23.7549, # Different from delivery location
"current_lng": 90.3909
})
if not driver_result.get("success"):
print(f"FAILED: {driver_result.get('error')}")
sys.exit(1)
driver_id = driver_result["driver_id"]
driver_start_lat = 23.7549
driver_start_lng = 90.3909
print(f"SUCCESS: Driver created: {driver_id}")
print(f" Driver starting location: ({driver_start_lat}, {driver_start_lng})")
# Step 3: Create assignment
print("\n[3] Creating assignment...")
assignment_result = handle_create_assignment({
"order_id": order_id,
"driver_id": driver_id
})
if not assignment_result.get("success"):
print(f"FAILED: {assignment_result.get('error')}")
sys.exit(1)
assignment_id = assignment_result["assignment_id"]
print(f"SUCCESS: Assignment created: {assignment_id}")
# Step 4: Get driver details BEFORE completion
print("\n[4] Getting driver location BEFORE delivery completion...")
driver_before = handle_get_driver_details({"driver_id": driver_id})
if driver_before.get("success"):
driver_data = driver_before["driver"]
location = driver_data["location"]
print(f"Driver location BEFORE: ({location['latitude']}, {location['longitude']})")
print(f" Status: {driver_data['status']}")
else:
print(f"FAILED to get driver details")
# Step 5: Complete delivery
print("\n[5] Completing delivery...")
completion_result = handle_complete_delivery({
"assignment_id": assignment_id,
"confirm": True,
"actual_distance_meters": 4500,
"notes": "Delivered successfully to security desk"
})
if not completion_result.get("success"):
print(f"FAILED: {completion_result.get('error')}")
sys.exit(1)
print(f"SUCCESS: Delivery completed!")
print(f" Assignment ID: {completion_result['assignment_id']}")
print(f" Order ID: {completion_result['order_id']}")
print(f" Customer: {completion_result['customer_name']}")
print(f" Driver: {completion_result['driver_name']}")
print(f" Completed at: {completion_result['completed_at']}")
# Check driver location update
driver_updated = completion_result.get("driver_updated", {})
print(f"\nDriver location UPDATE:")
print(f" New location: {driver_updated.get('new_location', 'N/A')}")
print(f" Updated at: {driver_updated.get('location_updated_at', 'N/A')}")
# Cascading actions
cascading = completion_result.get("cascading_actions", [])
if cascading:
print(f"\nCascading actions:")
for action in cascading:
print(f" - {action}")
# Step 6: Get driver details AFTER completion to verify location changed
print("\n[6] Verifying driver location AFTER delivery completion...")
driver_after = handle_get_driver_details({"driver_id": driver_id})
if driver_after.get("success"):
driver_data = driver_after["driver"]
location = driver_data["location"]
after_lat = location['latitude']
after_lng = location['longitude']
print(f"Driver location AFTER: ({after_lat}, {after_lng})")
print(f" Status: {driver_data['status']}")
# Verify location changed
delivery_lat = 23.7808
delivery_lng = 90.4130
if abs(after_lat - delivery_lat) < 0.0001 and abs(after_lng - delivery_lng) < 0.0001:
print(f"\nSUCCESS: Driver location updated to delivery address!")
print(f" Expected: ({delivery_lat}, {delivery_lng})")
print(f" Got: ({after_lat}, {after_lng})")
else:
print(f"\nFAILED: Driver location NOT updated correctly")
print(f" Expected: ({delivery_lat}, {delivery_lng})")
print(f" Got: ({after_lat}, {after_lng})")
else:
print(f"FAILED to get driver details after completion")
# Step 7: Verify assignment status
print("\n[7] Verifying assignment status...")
assignment_details = handle_get_assignment_details({"assignment_id": assignment_id})
if assignment_details.get("success"):
assignment = assignment_details.get("assignment", {})
print(f"Assignment status: {assignment.get('status')}")
print(f"Actual arrival: {assignment.get('actual_arrival')}")
order = assignment.get("order", {})
print(f"Order status: {order.get('status')}")
if assignment.get('status') == 'completed' and order.get('status') == 'delivered':
print(f"\nSUCCESS: Assignment and order statuses updated correctly!")
else:
print(f"\nFAILED: Statuses not updated correctly")
else:
print(f"FAILED to get assignment details")
print("\n" + "=" * 70)
print("Delivery Completion Test Complete!")
print("=" * 70)
# Cleanup
print("\nCleaning up test data...")
from chat.tools import handle_delete_order, handle_delete_driver
handle_delete_order({"order_id": order_id, "confirm": True})
handle_delete_driver({"driver_id": driver_id, "confirm": True})
print("Cleanup complete!")