feat: skip optional trip fields and accept natural date expressions
Browse files- Replace missing-fields error with sensible defaults (4 seats, price 0)
so drivers can create trips without specifying seats/price
- Update departure_date schema description to instruct AI to convert
natural date expressions (اليوم/بكرة/غدا/يوم الخميس) to YYYY-MM-DD
- Add driver system prompt rule to never ask drivers to rewrite dates
- Add driver message card support (emoji-rich messages used as-is)
- Add tests for new behaviors
- app/ai/tool_schemas.py +5 -1
- app/database/supabase.py +4 -0
- app/services/trip_indexing.py +2 -0
- app/tools/handlers.py +17 -17
- app/utils/emoji.py +22 -0
- app/whatsapp/trip_selection.py +3 -0
- prompts/system_driver.md +3 -0
- supabase/migrations/202607140001_driver_message_card.sql +135 -0
- tests/test_emoji.py +33 -0
- tests/test_tools.py +129 -0
- tests/test_trip_selection.py +58 -0
app/ai/tool_schemas.py
CHANGED
|
@@ -214,7 +214,11 @@ _ADD_TRIP_BY_DRIVER = {
|
|
| 214 |
},
|
| 215 |
"departure_date": {
|
| 216 |
"type": "string",
|
| 217 |
-
"description":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
},
|
| 219 |
"departure_time": {
|
| 220 |
"type": "string",
|
|
|
|
| 214 |
},
|
| 215 |
"departure_date": {
|
| 216 |
"type": "string",
|
| 217 |
+
"description": (
|
| 218 |
+
"Trip date as YYYY-MM-DD in Asia/Aden. "
|
| 219 |
+
"Convert natural expressions (اليوم/بكرة/غدا/يوم الخميس etc.) "
|
| 220 |
+
"to YYYY-MM-DD using the current date from system context."
|
| 221 |
+
),
|
| 222 |
},
|
| 223 |
"departure_time": {
|
| 224 |
"type": "string",
|
app/database/supabase.py
CHANGED
|
@@ -649,6 +649,8 @@ class SupabaseRepository:
|
|
| 649 |
available_seats: int,
|
| 650 |
total_seats: int,
|
| 651 |
price: float,
|
|
|
|
|
|
|
| 652 |
) -> dict[str, Any]:
|
| 653 |
payload = {
|
| 654 |
"driver_id": driver_id,
|
|
@@ -661,6 +663,8 @@ class SupabaseRepository:
|
|
| 661 |
"total_seats": total_seats,
|
| 662 |
"price": price,
|
| 663 |
"status": "active",
|
|
|
|
|
|
|
| 664 |
}
|
| 665 |
response = await self.client.table("driver_trips").insert(payload).execute()
|
| 666 |
data = _response_data(response)
|
|
|
|
| 649 |
available_seats: int,
|
| 650 |
total_seats: int,
|
| 651 |
price: float,
|
| 652 |
+
driver_message: str | None = None,
|
| 653 |
+
use_driver_message: bool = False,
|
| 654 |
) -> dict[str, Any]:
|
| 655 |
payload = {
|
| 656 |
"driver_id": driver_id,
|
|
|
|
| 663 |
"total_seats": total_seats,
|
| 664 |
"price": price,
|
| 665 |
"status": "active",
|
| 666 |
+
"driver_message": driver_message,
|
| 667 |
+
"use_driver_message": use_driver_message,
|
| 668 |
}
|
| 669 |
response = await self.client.table("driver_trips").insert(payload).execute()
|
| 670 |
data = _response_data(response)
|
app/services/trip_indexing.py
CHANGED
|
@@ -18,6 +18,8 @@ def build_trip_embedding_record(trip: dict[str, Any], embedding_model: str) -> d
|
|
| 18 |
f"Vehicle: {car.get('car_type')}. Driver: {driver.get('name')}. "
|
| 19 |
f"Price: {trip.get('price')}. Status: {trip.get('status')}."
|
| 20 |
)
|
|
|
|
|
|
|
| 21 |
return {
|
| 22 |
"trip_id": trip_id,
|
| 23 |
"chunk_text": chunk_text,
|
|
|
|
| 18 |
f"Vehicle: {car.get('car_type')}. Driver: {driver.get('name')}. "
|
| 19 |
f"Price: {trip.get('price')}. Status: {trip.get('status')}."
|
| 20 |
)
|
| 21 |
+
if trip.get("use_driver_message") and trip.get("driver_message"):
|
| 22 |
+
chunk_text += f" Driver message: {trip['driver_message']}"
|
| 23 |
return {
|
| 24 |
"trip_id": trip_id,
|
| 25 |
"chunk_text": chunk_text,
|
app/tools/handlers.py
CHANGED
|
@@ -22,6 +22,7 @@ from app.utils.departure import (
|
|
| 22 |
trip_departure_date,
|
| 23 |
trip_satisfies_departure_request,
|
| 24 |
)
|
|
|
|
| 25 |
from app.whatsapp.client import WhatsAppClient, WhatsAppClientError
|
| 26 |
from app.whatsapp.trip_selection import format_trip_card
|
| 27 |
|
|
@@ -545,23 +546,12 @@ class FalsaToolHandlers:
|
|
| 545 |
if price is None and latest_trip is not None:
|
| 546 |
price = _optional_price(latest_trip.get("price"))
|
| 547 |
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
]
|
| 555 |
-
if value is None
|
| 556 |
-
]
|
| 557 |
-
if matched_car is None and not vehicle_type:
|
| 558 |
-
missing.insert(0, "vehicle_type")
|
| 559 |
-
if missing:
|
| 560 |
-
return ToolResult(
|
| 561 |
-
ok=False,
|
| 562 |
-
data={"missing_fields": missing},
|
| 563 |
-
error=f"Missing required trip fields: {', '.join(missing)}",
|
| 564 |
-
)
|
| 565 |
|
| 566 |
if not matched_car:
|
| 567 |
if vehicle_type:
|
|
@@ -592,6 +582,14 @@ class FalsaToolHandlers:
|
|
| 592 |
if price is None or price < 0:
|
| 593 |
return ToolResult(ok=False, data={}, error="price must be zero or greater")
|
| 594 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 595 |
trip = await self.repository.create_driver_trip(
|
| 596 |
driver_id=str(driver["id"]),
|
| 597 |
car_id=car_id,
|
|
@@ -602,6 +600,8 @@ class FalsaToolHandlers:
|
|
| 602 |
available_seats=available_seats,
|
| 603 |
total_seats=total_seats,
|
| 604 |
price=price,
|
|
|
|
|
|
|
| 605 |
)
|
| 606 |
|
| 607 |
await index_trip(
|
|
|
|
| 22 |
trip_departure_date,
|
| 23 |
trip_satisfies_departure_request,
|
| 24 |
)
|
| 25 |
+
from app.utils.emoji import count_emojis
|
| 26 |
from app.whatsapp.client import WhatsAppClient, WhatsAppClientError
|
| 27 |
from app.whatsapp.trip_selection import format_trip_card
|
| 28 |
|
|
|
|
| 546 |
if price is None and latest_trip is not None:
|
| 547 |
price = _optional_price(latest_trip.get("price"))
|
| 548 |
|
| 549 |
+
if total_seats is None:
|
| 550 |
+
total_seats = 4
|
| 551 |
+
if available_seats is None:
|
| 552 |
+
available_seats = total_seats
|
| 553 |
+
if price is None:
|
| 554 |
+
price = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 555 |
|
| 556 |
if not matched_car:
|
| 557 |
if vehicle_type:
|
|
|
|
| 582 |
if price is None or price < 0:
|
| 583 |
return ToolResult(ok=False, data={}, error="price must be zero or greater")
|
| 584 |
|
| 585 |
+
driver_message: str | None = None
|
| 586 |
+
use_driver_message = False
|
| 587 |
+
if self.current_message:
|
| 588 |
+
raw_msg = self.current_message.get("message") or ""
|
| 589 |
+
if count_emojis(raw_msg) > 2:
|
| 590 |
+
driver_message = raw_msg
|
| 591 |
+
use_driver_message = True
|
| 592 |
+
|
| 593 |
trip = await self.repository.create_driver_trip(
|
| 594 |
driver_id=str(driver["id"]),
|
| 595 |
car_id=car_id,
|
|
|
|
| 600 |
available_seats=available_seats,
|
| 601 |
total_seats=total_seats,
|
| 602 |
price=price,
|
| 603 |
+
driver_message=driver_message,
|
| 604 |
+
use_driver_message=use_driver_message,
|
| 605 |
)
|
| 606 |
|
| 607 |
await index_trip(
|
app/utils/emoji.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
_EMOJI_RE = re.compile(
|
| 4 |
+
"["
|
| 5 |
+
"\U0001F600-\U0001F64F"
|
| 6 |
+
"\U0001F300-\U0001F5FF"
|
| 7 |
+
"\U0001F680-\U0001F6FF"
|
| 8 |
+
"\U0001F1E0-\U0001F1FF"
|
| 9 |
+
"\U00002702-\U000027B0"
|
| 10 |
+
"\U0000FE00-\U0000FE0F"
|
| 11 |
+
"\U0001F900-\U0001F9FF"
|
| 12 |
+
"\U0001FA00-\U0001FA6F"
|
| 13 |
+
"\U0001FA70-\U0001FAFF"
|
| 14 |
+
"\U00002600-\U000026FF"
|
| 15 |
+
"\U0000200D"
|
| 16 |
+
"]",
|
| 17 |
+
flags=re.UNICODE,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def count_emojis(text: str) -> int:
|
| 22 |
+
return len(_EMOJI_RE.findall(text or ""))
|
app/whatsapp/trip_selection.py
CHANGED
|
@@ -9,6 +9,9 @@ _BUCKET_LABELS = {
|
|
| 9 |
}
|
| 10 |
|
| 11 |
def format_trip_card(trip: dict[str, Any]) -> str:
|
|
|
|
|
|
|
|
|
|
| 12 |
departure = str(trip.get("departure") or "")
|
| 13 |
destination = str(trip.get("destination") or "")
|
| 14 |
parsed_date = trip_departure_date(trip)
|
|
|
|
| 9 |
}
|
| 10 |
|
| 11 |
def format_trip_card(trip: dict[str, Any]) -> str:
|
| 12 |
+
if trip.get("use_driver_message") and trip.get("driver_message"):
|
| 13 |
+
return trip["driver_message"]
|
| 14 |
+
|
| 15 |
departure = str(trip.get("departure") or "")
|
| 16 |
destination = str(trip.get("destination") or "")
|
| 17 |
parsed_date = trip_departure_date(trip)
|
prompts/system_driver.md
CHANGED
|
@@ -2,6 +2,9 @@
|
|
| 2 |
- check_driver_trips for upcoming trips.
|
| 3 |
- add_driver_car to register vehicle (name required).
|
| 4 |
- add_trip_by_driver(route, departure_date, departure_time).
|
|
|
|
|
|
|
|
|
|
| 5 |
- For delete/modify: initiate_trip_action (sends numbered list). Driver replies with number.
|
| 6 |
- delete_trip_by_number(trip_number) / modify_trip_by_number(trip_number, field, value). No trip_id.
|
| 7 |
- To search/book as traveler: switch_to_passenger (name optional).
|
|
|
|
| 2 |
- check_driver_trips for upcoming trips.
|
| 3 |
- add_driver_car to register vehicle (name required).
|
| 4 |
- add_trip_by_driver(route, departure_date, departure_time).
|
| 5 |
+
- Accept natural date expressions from the driver (اليوم/بكرة/غدا/يوم الخميس/بعد يومين etc.)
|
| 6 |
+
and convert them to YYYY-MM-DD using the current date in context. Never ask the driver
|
| 7 |
+
to rewrite the date in a specific format.
|
| 8 |
- For delete/modify: initiate_trip_action (sends numbered list). Driver replies with number.
|
| 9 |
- delete_trip_by_number(trip_number) / modify_trip_by_number(trip_number, field, value). No trip_id.
|
| 10 |
- To search/book as traveler: switch_to_passenger (name optional).
|
supabase/migrations/202607140001_driver_message_card.sql
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-- Add driver message card support to driver_trips
|
| 2 |
+
|
| 3 |
+
ALTER TABLE driver_trips
|
| 4 |
+
ADD COLUMN driver_message text,
|
| 5 |
+
ADD COLUMN use_driver_message boolean NOT NULL DEFAULT false;
|
| 6 |
+
|
| 7 |
+
-- Update match_active_trips to include driver message fields
|
| 8 |
+
|
| 9 |
+
DROP FUNCTION IF EXISTS public.match_active_trips(
|
| 10 |
+
extensions.vector(1024), float, int, text, text, date, text, time, int, text
|
| 11 |
+
);
|
| 12 |
+
|
| 13 |
+
CREATE OR REPLACE FUNCTION public.match_active_trips(
|
| 14 |
+
query_embedding extensions.vector(1024),
|
| 15 |
+
match_threshold float DEFAULT 0.0,
|
| 16 |
+
match_count int DEFAULT 10,
|
| 17 |
+
filter_departure text DEFAULT NULL,
|
| 18 |
+
filter_destination text DEFAULT NULL,
|
| 19 |
+
filter_driver_name text DEFAULT NULL,
|
| 20 |
+
filter_departure_date date DEFAULT NULL,
|
| 21 |
+
filter_departure_time text DEFAULT NULL,
|
| 22 |
+
filter_requested_time time DEFAULT NULL,
|
| 23 |
+
filter_seats int DEFAULT 1,
|
| 24 |
+
filter_vehicle_type text DEFAULT NULL
|
| 25 |
+
)
|
| 26 |
+
RETURNS TABLE (
|
| 27 |
+
trip_id uuid,
|
| 28 |
+
departure text,
|
| 29 |
+
destination text,
|
| 30 |
+
departure_date date,
|
| 31 |
+
departure_time text,
|
| 32 |
+
available_seats integer,
|
| 33 |
+
total_seats integer,
|
| 34 |
+
price numeric,
|
| 35 |
+
status text,
|
| 36 |
+
driver_name text,
|
| 37 |
+
driver_phone_number text,
|
| 38 |
+
car_type text,
|
| 39 |
+
chunk_text text,
|
| 40 |
+
similarity float,
|
| 41 |
+
time_difference_minutes integer,
|
| 42 |
+
registered boolean,
|
| 43 |
+
driver_message text,
|
| 44 |
+
use_driver_message boolean
|
| 45 |
+
)
|
| 46 |
+
LANGUAGE sql STABLE
|
| 47 |
+
AS $$
|
| 48 |
+
WITH ranked AS (
|
| 49 |
+
SELECT
|
| 50 |
+
driver_trips.id AS trip_id,
|
| 51 |
+
driver_trips.departure,
|
| 52 |
+
driver_trips.destination,
|
| 53 |
+
driver_trips.departure_date,
|
| 54 |
+
driver_trips.departure_time,
|
| 55 |
+
driver_trips.available_seats,
|
| 56 |
+
driver_trips.total_seats,
|
| 57 |
+
driver_trips.price,
|
| 58 |
+
driver_trips.status,
|
| 59 |
+
customers.name AS driver_name,
|
| 60 |
+
customers."remoteJid" AS driver_phone_number,
|
| 61 |
+
driver_cars.car_type,
|
| 62 |
+
driver_trip_embeddings.chunk_text,
|
| 63 |
+
COALESCE(customers.registered, false) AS registered,
|
| 64 |
+
driver_trips.driver_message,
|
| 65 |
+
driver_trips.use_driver_message,
|
| 66 |
+
1 - (driver_trip_embeddings.embedding <=> query_embedding) AS similarity,
|
| 67 |
+
driver_trip_embeddings.embedding <=> query_embedding AS vector_distance,
|
| 68 |
+
CASE
|
| 69 |
+
WHEN filter_requested_time IS NULL THEN NULL
|
| 70 |
+
ELSE abs(
|
| 71 |
+
extract(epoch FROM (
|
| 72 |
+
public.departure_bucket_clock_time(driver_trips.departure_time)
|
| 73 |
+
- filter_requested_time
|
| 74 |
+
)) / 60
|
| 75 |
+
)::integer
|
| 76 |
+
END AS time_difference_minutes
|
| 77 |
+
FROM public.driver_trip_embeddings
|
| 78 |
+
JOIN public.driver_trips ON driver_trips.id = driver_trip_embeddings.trip_id
|
| 79 |
+
LEFT JOIN public.drivers ON drivers.id = driver_trips.driver_id
|
| 80 |
+
LEFT JOIN public.customers ON customers.id = drivers.customer_id
|
| 81 |
+
LEFT JOIN public.driver_cars ON driver_cars.id = driver_trips.car_id
|
| 82 |
+
WHERE driver_trips.status = 'active'
|
| 83 |
+
AND driver_trips.available_seats >= COALESCE(filter_seats, 1)
|
| 84 |
+
AND (filter_departure IS NULL OR driver_trips.departure ILIKE '%' || filter_departure || '%')
|
| 85 |
+
AND (filter_destination IS NULL OR driver_trips.destination ILIKE '%' || filter_destination || '%')
|
| 86 |
+
AND (filter_driver_name IS NULL OR customers.name ILIKE '%' || filter_driver_name || '%')
|
| 87 |
+
AND (filter_departure_date IS NULL OR driver_trips.departure_date = filter_departure_date)
|
| 88 |
+
AND (filter_departure_time IS NULL OR driver_trips.departure_time = filter_departure_time)
|
| 89 |
+
AND (filter_vehicle_type IS NULL OR driver_cars.car_type ILIKE '%' || filter_vehicle_type || '%')
|
| 90 |
+
AND (
|
| 91 |
+
driver_trips.departure_date > (NOW() AT TIME ZONE 'Asia/Aden')::date
|
| 92 |
+
OR (
|
| 93 |
+
driver_trips.departure_date = (NOW() AT TIME ZONE 'Asia/Aden')::date
|
| 94 |
+
AND (
|
| 95 |
+
(NOW() AT TIME ZONE 'Asia/Aden')::time < TIME '12:00'
|
| 96 |
+
OR (
|
| 97 |
+
(NOW() AT TIME ZONE 'Asia/Aden')::time < TIME '18:00'
|
| 98 |
+
AND driver_trips.departure_time IN ('noon', 'night')
|
| 99 |
+
)
|
| 100 |
+
OR (
|
| 101 |
+
(NOW() AT TIME ZONE 'Asia/Aden')::time >= TIME '18:00'
|
| 102 |
+
AND driver_trips.departure_time = 'night'
|
| 103 |
+
)
|
| 104 |
+
)
|
| 105 |
+
)
|
| 106 |
+
)
|
| 107 |
+
)
|
| 108 |
+
SELECT
|
| 109 |
+
ranked.trip_id,
|
| 110 |
+
ranked.departure,
|
| 111 |
+
ranked.destination,
|
| 112 |
+
ranked.departure_date,
|
| 113 |
+
ranked.departure_time,
|
| 114 |
+
ranked.available_seats,
|
| 115 |
+
ranked.total_seats,
|
| 116 |
+
ranked.price,
|
| 117 |
+
ranked.status,
|
| 118 |
+
ranked.driver_name,
|
| 119 |
+
ranked.driver_phone_number,
|
| 120 |
+
ranked.car_type,
|
| 121 |
+
ranked.chunk_text,
|
| 122 |
+
ranked.similarity,
|
| 123 |
+
ranked.time_difference_minutes,
|
| 124 |
+
ranked.registered,
|
| 125 |
+
ranked.driver_message,
|
| 126 |
+
ranked.use_driver_message
|
| 127 |
+
FROM ranked
|
| 128 |
+
WHERE ranked.similarity >= match_threshold
|
| 129 |
+
ORDER BY
|
| 130 |
+
ranked.registered DESC,
|
| 131 |
+
ranked.time_difference_minutes NULLS LAST,
|
| 132 |
+
ranked.departure_date,
|
| 133 |
+
ranked.vector_distance
|
| 134 |
+
LIMIT match_count;
|
| 135 |
+
$$;
|
tests/test_emoji.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from app.utils.emoji import count_emojis
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def test_count_emojis_no_emojis():
|
| 5 |
+
assert count_emojis("باص من صنعاء الى تعز") == 0
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_count_emojis_single_emoji():
|
| 9 |
+
assert count_emojis("باص 😊") == 1
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_count_emojis_two_emojis():
|
| 13 |
+
assert count_emojis("باص 😊😊") == 2
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_count_emojis_three_emojis():
|
| 17 |
+
assert count_emojis("باص 😊😊😊") == 3
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def test_count_emojis_mixed_emojis():
|
| 21 |
+
assert count_emojis("رحلة 🚌🚗") == 2
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test_count_emojis_empty_string():
|
| 25 |
+
assert count_emojis("") == 0
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def test_count_emojis_none():
|
| 29 |
+
assert count_emojis(None) == 0
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def test_count_emojis_only_emojis():
|
| 33 |
+
assert count_emojis("😊😊😊😊") == 4
|
tests/test_tools.py
CHANGED
|
@@ -562,6 +562,135 @@ async def test_add_trip_by_driver_indexes_new_trip():
|
|
| 562 |
assert len(repository.trip_embeddings) == 1
|
| 563 |
|
| 564 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 565 |
@pytest.mark.asyncio
|
| 566 |
async def test_switch_to_driver_requires_existing_driver_account():
|
| 567 |
repository = FakeRepository()
|
|
|
|
| 562 |
assert len(repository.trip_embeddings) == 1
|
| 563 |
|
| 564 |
|
| 565 |
+
@pytest.mark.asyncio
|
| 566 |
+
async def test_add_trip_by_driver_creates_trip_without_optional_fields():
|
| 567 |
+
repository = FakeRepository()
|
| 568 |
+
repository.drivers_by_remote_jid["967700000010"] = {
|
| 569 |
+
"id": "driver-1",
|
| 570 |
+
"name": "Ali",
|
| 571 |
+
"remoteJid": "967700000010",
|
| 572 |
+
}
|
| 573 |
+
repository.driver_cars_by_driver["driver-1"] = [
|
| 574 |
+
{"id": "car-1", "car_type": "SUV", "seat_count": 4},
|
| 575 |
+
]
|
| 576 |
+
handlers = make_handlers(repository=repository, sender_phone="967700000010")
|
| 577 |
+
|
| 578 |
+
result = await handlers.add_trip_by_driver(
|
| 579 |
+
{
|
| 580 |
+
"departure": "عدن",
|
| 581 |
+
"destination": "صنعاء",
|
| 582 |
+
"departure_date": "2026-06-10",
|
| 583 |
+
"departure_time": "noon",
|
| 584 |
+
}
|
| 585 |
+
)
|
| 586 |
+
|
| 587 |
+
assert result.ok is True
|
| 588 |
+
assert repository.created_trips[0]["car_id"] == "car-1"
|
| 589 |
+
assert repository.created_trips[0]["total_seats"] == 4
|
| 590 |
+
assert repository.created_trips[0]["available_seats"] == 4
|
| 591 |
+
assert repository.created_trips[0]["price"] == 0
|
| 592 |
+
|
| 593 |
+
|
| 594 |
+
@pytest.mark.asyncio
|
| 595 |
+
async def test_add_trip_by_driver_stores_driver_message_with_emojis():
|
| 596 |
+
repository = FakeRepository()
|
| 597 |
+
repository.drivers_by_remote_jid["967700000010"] = {
|
| 598 |
+
"id": "driver-1",
|
| 599 |
+
"name": "Ali",
|
| 600 |
+
"remoteJid": "967700000010",
|
| 601 |
+
}
|
| 602 |
+
repository.driver_cars_by_driver["driver-1"] = [
|
| 603 |
+
{"id": "car-1", "car_type": "SUV", "seat_count": 4},
|
| 604 |
+
]
|
| 605 |
+
current_message = {"message": "باص من عدن الى المكلا 😊😊😊"}
|
| 606 |
+
handlers = make_handlers(repository=repository, sender_phone="967700000010")
|
| 607 |
+
handlers.current_message = current_message
|
| 608 |
+
|
| 609 |
+
result = await handlers.add_trip_by_driver(
|
| 610 |
+
{
|
| 611 |
+
"departure": "عدن",
|
| 612 |
+
"destination": "المكلا",
|
| 613 |
+
"departure_date": "2026-06-10",
|
| 614 |
+
"departure_time": "morning",
|
| 615 |
+
"vehicle_type": "SUV",
|
| 616 |
+
"available_seats": 2,
|
| 617 |
+
"total_seats": 4,
|
| 618 |
+
"price": 50,
|
| 619 |
+
}
|
| 620 |
+
)
|
| 621 |
+
|
| 622 |
+
assert result.ok is True
|
| 623 |
+
created_trip = repository.created_trips[-1]
|
| 624 |
+
assert created_trip["use_driver_message"] is True
|
| 625 |
+
assert created_trip["driver_message"] == "باص من عدن الى المكلا 😊😊😊"
|
| 626 |
+
|
| 627 |
+
|
| 628 |
+
@pytest.mark.asyncio
|
| 629 |
+
async def test_add_trip_by_driver_no_driver_message_with_few_emojis():
|
| 630 |
+
repository = FakeRepository()
|
| 631 |
+
repository.drivers_by_remote_jid["967700000010"] = {
|
| 632 |
+
"id": "driver-1",
|
| 633 |
+
"name": "Ali",
|
| 634 |
+
"remoteJid": "967700000010",
|
| 635 |
+
}
|
| 636 |
+
repository.driver_cars_by_driver["driver-1"] = [
|
| 637 |
+
{"id": "car-1", "car_type": "SUV", "seat_count": 4},
|
| 638 |
+
]
|
| 639 |
+
current_message = {"message": "باص من عدن الى المكلا 😊"}
|
| 640 |
+
handlers = make_handlers(repository=repository, sender_phone="967700000010")
|
| 641 |
+
handlers.current_message = current_message
|
| 642 |
+
|
| 643 |
+
result = await handlers.add_trip_by_driver(
|
| 644 |
+
{
|
| 645 |
+
"departure": "عدن",
|
| 646 |
+
"destination": "المكلا",
|
| 647 |
+
"departure_date": "2026-06-10",
|
| 648 |
+
"departure_time": "morning",
|
| 649 |
+
"vehicle_type": "SUV",
|
| 650 |
+
"available_seats": 2,
|
| 651 |
+
"total_seats": 4,
|
| 652 |
+
"price": 50,
|
| 653 |
+
}
|
| 654 |
+
)
|
| 655 |
+
|
| 656 |
+
assert result.ok is True
|
| 657 |
+
created_trip = repository.created_trips[-1]
|
| 658 |
+
assert created_trip["use_driver_message"] is False
|
| 659 |
+
assert created_trip["driver_message"] is None
|
| 660 |
+
|
| 661 |
+
|
| 662 |
+
@pytest.mark.asyncio
|
| 663 |
+
async def test_add_trip_by_driver_no_current_message():
|
| 664 |
+
repository = FakeRepository()
|
| 665 |
+
repository.drivers_by_remote_jid["967700000010"] = {
|
| 666 |
+
"id": "driver-1",
|
| 667 |
+
"name": "Ali",
|
| 668 |
+
"remoteJid": "967700000010",
|
| 669 |
+
}
|
| 670 |
+
repository.driver_cars_by_driver["driver-1"] = [
|
| 671 |
+
{"id": "car-1", "car_type": "SUV", "seat_count": 4},
|
| 672 |
+
]
|
| 673 |
+
handlers = make_handlers(repository=repository, sender_phone="967700000010")
|
| 674 |
+
|
| 675 |
+
result = await handlers.add_trip_by_driver(
|
| 676 |
+
{
|
| 677 |
+
"departure": "عدن",
|
| 678 |
+
"destination": "المكلا",
|
| 679 |
+
"departure_date": "2026-06-10",
|
| 680 |
+
"departure_time": "morning",
|
| 681 |
+
"vehicle_type": "SUV",
|
| 682 |
+
"available_seats": 2,
|
| 683 |
+
"total_seats": 4,
|
| 684 |
+
"price": 50,
|
| 685 |
+
}
|
| 686 |
+
)
|
| 687 |
+
|
| 688 |
+
assert result.ok is True
|
| 689 |
+
created_trip = repository.created_trips[-1]
|
| 690 |
+
assert created_trip["use_driver_message"] is False
|
| 691 |
+
assert created_trip["driver_message"] is None
|
| 692 |
+
|
| 693 |
+
|
| 694 |
@pytest.mark.asyncio
|
| 695 |
async def test_switch_to_driver_requires_existing_driver_account():
|
| 696 |
repository = FakeRepository()
|
tests/test_trip_selection.py
CHANGED
|
@@ -25,3 +25,61 @@ def test_format_trip_card():
|
|
| 25 |
assert "باص" in card
|
| 26 |
assert "أحمد" in card
|
| 27 |
assert "2" in card
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
assert "باص" in card
|
| 26 |
assert "أحمد" in card
|
| 27 |
assert "2" in card
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def test_format_trip_card_driver_message():
|
| 31 |
+
trip = {
|
| 32 |
+
"id": "trip-1",
|
| 33 |
+
"departure": "صنعاء",
|
| 34 |
+
"destination": "تعز",
|
| 35 |
+
"departure_date": "2026-12-01",
|
| 36 |
+
"departure_time": "noon",
|
| 37 |
+
"available_seats": 3,
|
| 38 |
+
"total_seats": 4,
|
| 39 |
+
"price": 5000,
|
| 40 |
+
"driver_cars": [{"car_type": "باص"}],
|
| 41 |
+
"drivers": [{"name": "أحمد"}],
|
| 42 |
+
"use_driver_message": True,
|
| 43 |
+
"driver_message": "باص من صنعاء الى تعز 😊😊😊",
|
| 44 |
+
}
|
| 45 |
+
card = format_trip_card(trip)
|
| 46 |
+
assert card == "باص من صنعاء الى تعز 😊😊😊"
|
| 47 |
+
assert "صنعاء ← إلى" not in card
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def test_format_trip_card_driver_message_flag_false():
|
| 51 |
+
trip = {
|
| 52 |
+
"id": "trip-1",
|
| 53 |
+
"departure": "صنعاء",
|
| 54 |
+
"destination": "تعز",
|
| 55 |
+
"departure_date": "2026-12-01",
|
| 56 |
+
"departure_time": "noon",
|
| 57 |
+
"available_seats": 3,
|
| 58 |
+
"total_seats": 4,
|
| 59 |
+
"price": 5000,
|
| 60 |
+
"driver_cars": [{"car_type": "باص"}],
|
| 61 |
+
"drivers": [{"name": "أحمد"}],
|
| 62 |
+
"use_driver_message": False,
|
| 63 |
+
"driver_message": "باص من صنعاء الى تعز 😊😊😊",
|
| 64 |
+
}
|
| 65 |
+
card = format_trip_card(trip)
|
| 66 |
+
assert "صنعاء ← إلى" in card
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def test_format_trip_card_driver_message_empty():
|
| 70 |
+
trip = {
|
| 71 |
+
"id": "trip-1",
|
| 72 |
+
"departure": "صنعاء",
|
| 73 |
+
"destination": "تعز",
|
| 74 |
+
"departure_date": "2026-12-01",
|
| 75 |
+
"departure_time": "noon",
|
| 76 |
+
"available_seats": 3,
|
| 77 |
+
"total_seats": 4,
|
| 78 |
+
"price": 5000,
|
| 79 |
+
"driver_cars": [{"car_type": "باص"}],
|
| 80 |
+
"drivers": [{"name": "أحمد"}],
|
| 81 |
+
"use_driver_message": True,
|
| 82 |
+
"driver_message": "",
|
| 83 |
+
}
|
| 84 |
+
card = format_trip_card(trip)
|
| 85 |
+
assert "صنعاء ← إلى" in card
|