File size: 31,665 Bytes
102dd4f cf6d08c 102dd4f cf6d08c fcf2812 cf6d08c fcf2812 102dd4f fcf2812 0467de1 102dd4f 2688cc5 1cd53ba 102dd4f 1cd53ba 2688cc5 1cd53ba 102dd4f 2688cc5 102dd4f cf6d08c 102dd4f cf6d08c 102dd4f cf6d08c 102dd4f cf6d08c 102dd4f c925062 102dd4f c925062 102dd4f cc10684 b5a802c cc10684 102dd4f edb038d 102dd4f d4f6fe7 102dd4f edb038d 102dd4f edb038d 102dd4f 2688cc5 edb038d 2688cc5 edb038d 2688cc5 102dd4f edb038d 102dd4f edb038d 102dd4f edb038d 102dd4f edb038d 102dd4f edb038d 102dd4f d4f6fe7 102dd4f d4f6fe7 fcf2812 c925062 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f fcf2812 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f 96f3b64 64075d7 96f3b64 bb9b63f c925062 bb9b63f 96f3b64 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f d4f6fe7 102dd4f dc8cb49 fcf2812 dc8cb49 fcf2812 dc8cb49 fcf2812 dc8cb49 | 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 | import pytest
from app.tools.handlers import FalzhToolHandlers
from tests.conftest import FakeEmbeddings, FakeRepository, FakeWhatsApp
def make_handlers(
*,
repository: FakeRepository | None = None,
embeddings: FakeEmbeddings | None = None,
whatsapp: FakeWhatsApp | None = None,
customer: dict | None = None,
sender_phone: str = "967700000001",
) -> FalzhToolHandlers:
repo = repository or FakeRepository()
if customer is None:
cust = {"id": "cust-1", "remoteJid": sender_phone, "session_data": {}}
repo.customers_by_remote_jid[sender_phone] = cust
customer = cust
return FalzhToolHandlers(
repository=repo,
embeddings=embeddings or FakeEmbeddings(),
whatsapp=whatsapp or FakeWhatsApp(),
customer=customer,
remoteJid=sender_phone,
embedding_model="jina-embeddings-v5-text-small",
)
def trip(
*,
trip_id="trip-1",
departure="Aden",
destination="Mukalla",
departure_date="2026-12-01",
departure_time="morning",
available_seats=3,
status="active",
car_type="SUV",
driver_phone_number=None,
registered=True,
):
customers: dict = {"registered": registered}
if driver_phone_number is not None:
customers["phone_number"] = driver_phone_number
drivers = {"name": "Ali", "remoteJid": "967700000009", "customers": customers}
return {
"id": trip_id,
"departure": departure,
"destination": destination,
"departure_date": departure_date,
"departure_time": departure_time,
"available_seats": available_seats,
"total_seats": 4,
"price": "50.00",
"status": status,
"drivers": drivers,
"driver_cars": {"car_type": car_type},
}
@pytest.mark.asyncio
async def test_about_falzh_returns_retrieved_context():
repository = FakeRepository()
repository.info_search_results = [
{
"similarity": 0.91,
"chunk_text": "FALZH creates pending booking leads.",
"source": "prompts/falzh_info.md",
}
]
embeddings = FakeEmbeddings()
handlers = make_handlers(repository=repository, embeddings=embeddings)
result = await handlers.about_falzh({"query": "What is FALZH?", "language": "en"})
assert result.ok is True
assert result.data["answer_context"][0]["text"] == "FALZH creates pending booking leads."
assert embeddings.query_texts == ["What is FALZH?"]
@pytest.mark.asyncio
async def test_about_falzh_no_match_instructs_apology():
handlers = make_handlers()
result = await handlers.about_falzh({"query": "Do you support rewards?", "language": "en"})
assert result.ok is True
assert result.data["sources"] == []
assert "Do not guess" in result.data["reply_instruction"]
@pytest.mark.asyncio
async def test_search_trips_uses_supabase_vector_matches_and_validates_business_rules():
repository = FakeRepository()
repository.trip_vector_search_results = [
trip(trip_id="trip-1", available_seats=2),
trip(trip_id="trip-2", status="cancelled"),
trip(trip_id="trip-3", available_seats=0),
]
embeddings = FakeEmbeddings()
handlers = make_handlers(repository=repository, embeddings=embeddings)
result = await handlers.search_trips(
{"departure": "Aden", "destination": "Mukalla", "seats": 2, "vehicle_type": "SUV"}
)
assert result.ok is True
assert result.data["count"] == 1
assert result.data["matches"][0]["trip_id"] == "trip-1"
assert result.data["matches"][0]["departure_time_type"] == "morning"
assert embeddings.query_texts == ["Aden Mukalla 2 seats SUV"]
assert repository.vector_trip_search_calls[0]["departure"] == "Aden"
assert repository.vector_trip_search_calls[0]["destination"] == "Mukalla"
@pytest.mark.asyncio
async def test_search_trips_filters_requested_datetime_to_departure_bucket():
repository = FakeRepository()
repository.trip_vector_search_results = [
trip(trip_id="trip-1", departure_date="2026-12-02", departure_time="morning"),
trip(trip_id="trip-2", departure_date="2026-12-02", departure_time="night"),
]
handlers = make_handlers(repository=repository)
result = await handlers.search_trips(
{
"departure": "Aden",
"destination": "Mukalla",
"travel_datetime": "2026-12-02T19:30:00+03:00",
}
)
assert result.ok is True
assert result.data["count"] == 1
assert result.data["matches"][0]["trip_id"] == "trip-2"
assert repository.vector_trip_search_calls[0]["departure_time"] == "night"
assert repository.vector_trip_search_calls[0]["requested_time"].hour == 19
@pytest.mark.asyncio
async def test_search_trips_reports_no_matches():
handlers = make_handlers()
result = await handlers.search_trips({"departure": "Aden", "destination": "Sana'a"})
assert result.ok is True
assert result.data["matches"] == []
assert "No active" in result.data["note"]
assert "Do not promise to notify" in result.data["reply_instruction"]
assert "already a general search" in result.data["reply_instruction"]
@pytest.mark.asyncio
async def test_search_trips_no_timed_match_instructs_no_narrow_retry():
handlers = make_handlers()
result = await handlers.search_trips(
{
"departure": "Aden",
"destination": "Sana'a",
"travel_date": "2026-12-01",
"travel_time": "morning",
}
)
assert result.ok is True
assert result.data["matches"] == []
assert "narrower/specific time" in result.data["reply_instruction"]
@pytest.mark.asyncio
async def test_search_trips_requires_at_least_departure_or_destination():
handlers = make_handlers()
result = await handlers.search_trips({"seats": 2})
assert result.ok is False
assert "departure" in (result.error or "")
assert "driver name" in (result.error or "")
@pytest.mark.asyncio
async def test_search_trips_with_departure_only():
repository = FakeRepository()
repository.trip_vector_search_results = [
trip(trip_id="trip-1", departure="Aden", destination="Mukalla"),
trip(trip_id="trip-2", departure="Aden", destination="Taiz"),
]
embeddings = FakeEmbeddings()
handlers = make_handlers(repository=repository, embeddings=embeddings)
result = await handlers.search_trips({"departure": "Aden"})
assert result.ok is True
assert result.data["count"] == 2
assert result.data["matches"][0]["trip_id"] == "trip-1"
assert result.data["matches"][1]["trip_id"] == "trip-2"
assert repository.vector_trip_search_calls[0]["departure"] == "Aden"
assert repository.vector_trip_search_calls[0]["destination"] is None
@pytest.mark.asyncio
async def test_search_trips_with_destination_only():
repository = FakeRepository()
repository.trip_vector_search_results = [
trip(trip_id="trip-1", departure="Aden", destination="Mukalla"),
trip(trip_id="trip-2", departure="Taiz", destination="Mukalla"),
]
embeddings = FakeEmbeddings()
handlers = make_handlers(repository=repository, embeddings=embeddings)
result = await handlers.search_trips({"destination": "Mukalla"})
assert result.ok is True
assert result.data["count"] == 2
assert repository.vector_trip_search_calls[0]["departure"] is None
assert repository.vector_trip_search_calls[0]["destination"] == "Mukalla"
@pytest.mark.asyncio
async def test_search_trips_defaults_seats_to_one():
repository = FakeRepository()
repository.trip_vector_search_results = [
trip(trip_id="trip-1", available_seats=1),
]
handlers = make_handlers(repository=repository)
result = await handlers.search_trips({"departure": "Aden", "destination": "Mukalla"})
assert result.ok is True
assert result.data["count"] == 1
assert repository.vector_trip_search_calls[0]["seats"] == 1
@pytest.mark.asyncio
async def test_search_trips_includes_alternate_alert_when_first_result_is_over_one_hour():
repository = FakeRepository()
repository.trip_vector_search_results = [
{
**trip(trip_id="trip-1", departure_time="morning"),
"time_difference_minutes": 90,
}
]
handlers = make_handlers(repository=repository)
result = await handlers.search_trips(
{
"departure": "Aden",
"destination": "Mukalla",
"travel_time": "morning",
"travel_time_exact": "04:30",
"vector_query_text": "Aden to Mukalla tomorrow morning at 04:30",
}
)
assert result.ok is True
assert result.data["count"] == 1
assert "more than 60 minutes" in result.data["alternate_alert"]
assert result.data["matches"][0]["time_difference_minutes"] == 90
@pytest.mark.asyncio
async def test_select_trip_notifies_driver():
repository = FakeRepository()
repository.trips_by_id["trip-1"] = trip()
whatsapp = FakeWhatsApp()
handlers = make_handlers(
repository=repository,
whatsapp=whatsapp,
customer={"id": "cust-1", "remoteJid": "967700000001", "name": "Mona"},
)
result = await handlers.select_trip(
{"trip_id": "trip-1", "requested_seats": 2, "notes": "Window seat"}
)
assert result.ok is True
assert result.data["driver_notification_status"] == "sent"
assert repository.trip_selections[0]["requested_seats"] == 2
assert whatsapp.sent[0][0] == "967700000009"
@pytest.mark.asyncio
async def test_select_trip_uses_driver_phone_number():
repository = FakeRepository()
repository.trips_by_id["trip-1"] = trip(driver_phone_number="967700000099")
whatsapp = FakeWhatsApp()
handlers = make_handlers(
repository=repository,
whatsapp=whatsapp,
customer={"id": "cust-1", "remoteJid": "967700000001", "name": "Mona"},
)
result = await handlers.select_trip(
{"trip_id": "trip-1", "requested_seats": 1}
)
assert result.ok is True
assert result.data["driver_notification_status"] == "sent"
assert whatsapp.sent[0][0] == "967700000099"
assert result.data["driver_phone"] == "967700000099"
@pytest.mark.asyncio
async def test_select_trip_keeps_pending_when_driver_notification_fails():
repository = FakeRepository()
repository.trips_by_id["trip-1"] = trip()
handlers = make_handlers(
repository=repository,
whatsapp=FakeWhatsApp(fail=True),
)
result = await handlers.select_trip({"trip_id": "trip-1", "requested_seats": 1})
assert result.ok is True
assert result.data["status"] == "pending"
assert result.data["driver_notification_status"] == "failed"
assert repository.notification_updates[0]["status"] == "failed"
@pytest.mark.asyncio
async def test_select_trip_rejects_insufficient_seats():
repository = FakeRepository()
repository.trips_by_id["trip-1"] = trip(available_seats=1)
handlers = make_handlers(repository=repository)
result = await handlers.select_trip({"trip_id": "trip-1", "requested_seats": 2})
assert result.ok is False
assert result.error == "Not enough available seats"
assert repository.trip_selections == []
@pytest.mark.asyncio
async def test_create_driver_account_uses_sender_phone():
repository = FakeRepository()
handlers = make_handlers(repository=repository, sender_phone="967700000010")
result = await handlers.create_driver_account({"name": "Ali Driver"})
assert result.ok is True
assert repository.created_drivers[0]["customers"]["remoteJid"] == "967700000010"
assert result.data["driver_id"] == "driver-1"
assert "own format/style" in result.data["reply_instruction"]
assert "phone numbers" in result.data["reply_instruction"]
@pytest.mark.asyncio
async def test_create_driver_account_rejects_duplicate():
repository = FakeRepository()
repository.drivers_by_remote_jid["967700000010"] = {
"id": "driver-1",
"name": "Ali",
"remoteJid": "967700000010",
}
handlers = make_handlers(repository=repository, sender_phone="967700000010")
result = await handlers.create_driver_account({"name": "Ali Driver"})
assert result.ok is False
assert "already exists" in (result.error or "")
assert repository.created_drivers == []
@pytest.mark.asyncio
async def test_add_trip_by_driver_requires_account():
handlers = make_handlers(sender_phone="967700000010")
result = await handlers.add_trip_by_driver(
{
"departure": "ุนุฏู",
"destination": "ุงูู
ููุง",
"departure_date": "2026-06-10",
"departure_time": "morning",
}
)
assert result.ok is False
assert result.data.get("action") == "create_driver_account"
assert "create_driver_account" in (result.error or "")
@pytest.mark.asyncio
async def test_add_driver_car_accepts_name_only():
repository = FakeRepository()
repository.drivers_by_remote_jid["967700000010"] = {
"id": "driver-1",
"name": "Ali",
"remoteJid": "967700000010",
}
handlers = make_handlers(repository=repository, sender_phone="967700000010")
result = await handlers.add_driver_car({"name": "ุณูุงุฑุฉ"})
assert result.ok is True
assert result.data["name"] == "ุณูุงุฑุฉ"
assert result.data["plate_number"] is None
assert result.data["seat_count"] is None
assert repository.driver_cars_by_driver["driver-1"][0]["car_type"] == "ุณูุงุฑุฉ"
@pytest.mark.asyncio
async def test_check_driver_info_returns_account_summary():
repository = FakeRepository()
repository.drivers_by_remote_jid["967700000010"] = {
"id": "driver-1",
"name": "Ali",
"remoteJid": "967700000010",
"status": "active",
}
repository.driver_cars_by_driver["driver-1"] = [
{"id": "car-1", "car_type": "SUV", "plate_number": "1234", "seat_count": 4}
]
repository.trips_by_id["trip-1"] = {
"id": "trip-1",
"driver_id": "driver-1",
"departure": "ุนุฏู",
"destination": "ุงูู
ููุง",
"departure_date": "2026-12-01",
"departure_time": "morning",
"available_seats": 2,
"total_seats": 4,
"price": "80.00",
"status": "active",
"driver_cars": {"car_type": "SUV"},
"drivers": {"name": "Ali"},
}
handlers = make_handlers(repository=repository, sender_phone="967700000010")
result = await handlers.check_driver_info({})
assert result.ok is True
assert result.data["driver_id"] == "driver-1"
assert result.data["vehicle_count"] == 1
assert result.data["active_trip_count"] == 1
assert result.data["vehicles"][0]["name"] == "SUV"
assert result.data["active_trips"][0]["trip_id"] == "trip-1"
@pytest.mark.asyncio
async def test_check_driver_trips_returns_upcoming_trips():
repository = FakeRepository()
repository.drivers_by_remote_jid["967700000010"] = {
"id": "driver-1",
"name": "Ali",
"remoteJid": "967700000010",
}
repository.trips_by_id["trip-1"] = {
"id": "trip-1",
"driver_id": "driver-1",
"departure": "ุนุฏู",
"destination": "ุงูู
ููุง",
"departure_date": "2026-12-01",
"departure_time": "morning",
"available_seats": 2,
"total_seats": 4,
"price": "80.00",
"status": "active",
"driver_cars": {"car_type": "SUV"},
"drivers": {"name": "Ali"},
}
handlers = make_handlers(repository=repository, sender_phone="967700000010")
result = await handlers.check_driver_trips({})
assert result.ok is True
assert result.data["count"] == 1
assert result.suppress_llm_reply is True
@pytest.mark.asyncio
async def test_check_driver_trips_requests_driver_registration_if_unregistered():
handlers = make_handlers(sender_phone="967700000010")
result = await handlers.check_driver_trips({})
assert result.ok is False
assert result.data["action"] == "create_driver_account"
assert "create_driver_account" in (result.error or "")
@pytest.mark.asyncio
async def test_check_driver_info_requests_driver_registration_if_unregistered():
handlers = make_handlers(sender_phone="967700000010")
result = await handlers.check_driver_info({})
assert result.ok is False
assert result.data["action"] == "create_driver_account"
assert "create_driver_account" in (result.error or "")
@pytest.mark.asyncio
async def test_add_trip_by_driver_uses_latest_trip_defaults():
repository = FakeRepository()
repository.drivers_by_remote_jid["967700000010"] = {
"id": "driver-1",
"name": "Ali",
"remoteJid": "967700000010",
}
repository.driver_cars_by_driver["driver-1"] = [
{"id": "car-1", "car_type": "SUV", "seat_count": 4},
]
repository.latest_trips_by_driver["driver-1"] = {
"car_id": "car-1",
"available_seats": 3,
"total_seats": 4,
"price": "75.00",
}
embeddings = FakeEmbeddings()
handlers = make_handlers(
repository=repository,
embeddings=embeddings,
sender_phone="967700000010",
)
result = await handlers.add_trip_by_driver(
{
"departure": "ุนุฏู",
"destination": "ุตูุนุงุก",
"departure_date": "2026-06-10",
"departure_time": "noon",
}
)
assert result.ok is True
assert result.data["trip_id"] == "trip-1"
assert repository.created_trips[0]["car_id"] == "car-1"
assert repository.created_trips[0]["available_seats"] == 3
assert repository.created_trips[0]["price"] == 75.0
assert repository.trip_embeddings[0]["trip_id"] == "trip-1"
assert embeddings.passage_texts == [["Trip trip-1: ุนุฏู to ุตูุนุงุก on 2026-06-10 during noon. Available seats: 3 of 4. Vehicle: SUV. Driver: Ali. Price: 75.0. Status: active."]]
@pytest.mark.asyncio
async def test_add_trip_by_driver_resolves_vehicle_type_by_name():
repository = FakeRepository()
repository.drivers_by_remote_jid["967700000010"] = {
"id": "driver-1",
"name": "Ali",
"remoteJid": "967700000010",
}
repository.driver_cars_by_driver["driver-1"] = [
{"id": "car-1", "car_type": "ุจุงุต", "plate_number": "1234", "seat_count": 14},
{"id": "car-2", "car_type": "ุณูุงุฑุฉ", "plate_number": "5678", "seat_count": 4},
]
handlers = make_handlers(repository=repository, sender_phone="967700000010")
result = await handlers.add_trip_by_driver(
{
"departure": "ุนุฏู",
"destination": "ุงูู
ููุง",
"departure_date": "2026-06-10",
"departure_time": "morning",
"vehicle_type": "ุจุงุต",
"available_seats": 10,
"total_seats": 14,
"price": 120,
}
)
assert result.ok is True
assert repository.created_trips[0]["car_id"] == "car-1"
@pytest.mark.asyncio
async def test_add_trip_by_driver_indexes_new_trip():
repository = FakeRepository()
repository.drivers_by_remote_jid["967700000010"] = {
"id": "driver-1",
"name": "Ali",
"remoteJid": "967700000010",
}
repository.driver_cars_by_driver["driver-1"] = [
{"id": "car-1", "car_type": "SUV", "seat_count": 4},
]
handlers = make_handlers(repository=repository, sender_phone="967700000010")
result = await handlers.add_trip_by_driver(
{
"departure": "ุนุฏู",
"destination": "ุงูู
ููุง",
"departure_date": "2026-06-10",
"departure_time": "morning",
"vehicle_type": "SUV",
"available_seats": 2,
"total_seats": 4,
"price": 50,
}
)
assert result.ok is True
assert result.data["indexed"] is True
assert len(repository.trip_embeddings) == 1
@pytest.mark.asyncio
async def test_add_trip_by_driver_creates_trip_without_optional_fields():
repository = FakeRepository()
repository.drivers_by_remote_jid["967700000010"] = {
"id": "driver-1",
"name": "Ali",
"remoteJid": "967700000010",
}
repository.driver_cars_by_driver["driver-1"] = [
{"id": "car-1", "car_type": "SUV", "seat_count": 4},
]
handlers = make_handlers(repository=repository, sender_phone="967700000010")
result = await handlers.add_trip_by_driver(
{
"departure": "ุนุฏู",
"destination": "ุตูุนุงุก",
"departure_date": "2026-06-10",
"departure_time": "noon",
}
)
assert result.ok is True
assert repository.created_trips[0]["car_id"] == "car-1"
assert repository.created_trips[0]["total_seats"] == 4
assert repository.created_trips[0]["available_seats"] is None
assert repository.created_trips[0]["price"] == 0
@pytest.mark.asyncio
async def test_add_trip_by_driver_stores_driver_message_with_emojis():
repository = FakeRepository()
repository.drivers_by_remote_jid["967700000010"] = {
"id": "driver-1",
"name": "Ali",
"remoteJid": "967700000010",
}
repository.driver_cars_by_driver["driver-1"] = [
{"id": "car-1", "car_type": "SUV", "seat_count": 4},
]
current_message = {"message": "ุจุงุต ู
ู ุนุฏู ุงูู ุงูู
ููุง ๐๐๐"}
handlers = make_handlers(repository=repository, sender_phone="967700000010")
handlers.current_message = current_message
result = await handlers.add_trip_by_driver(
{
"departure": "ุนุฏู",
"destination": "ุงูู
ููุง",
"departure_date": "2026-06-10",
"departure_time": "morning",
"vehicle_type": "SUV",
"available_seats": 2,
"total_seats": 4,
"price": 50,
}
)
assert result.ok is True
created_trip = repository.created_trips[-1]
assert created_trip["use_driver_message"] is True
assert created_trip["driver_message"] == "ุจุงุต ู
ู ุนุฏู ุงูู ุงูู
ููุง ๐๐๐"
@pytest.mark.asyncio
async def test_add_trip_by_driver_uses_llm_cleaned_message():
repository = FakeRepository()
repository.drivers_by_remote_jid["967700000010"] = {
"id": "driver-1",
"name": "Ali",
"remoteJid": "967700000010",
}
repository.driver_cars_by_driver["driver-1"] = [
{"id": "car-1", "car_type": "SUV", "seat_count": 4},
]
raw_msg = "๐ุจุต ููุฏ ู
ุฑูุญ ๐. ูุงุฒู ุงูุฌู
ุนุฉ ูง ุตุจุงุญุง ๐ุชุฑูู
ุงูู
ููุง๐ ุจู ุจูุฑ ๐770026665 ๐ุจู ุจูุฑ๐"
cleaned_msg = "๐ุจุต ููุฏ ู
ุฑูุญ ๐. ูุงุฒู ุงูุฌู
ุนุฉ ูง ุตุจุงุญุง ๐ุชุฑูู
ุงูู
ููุง๐ ๐"
current_message = {"message": raw_msg}
handlers = make_handlers(repository=repository, sender_phone="967700000010")
handlers.current_message = current_message
result = await handlers.add_trip_by_driver(
{
"departure": "ุชุฑูู
",
"destination": "ุงูู
ููุง",
"departure_date": "2026-06-10",
"departure_time": "morning",
"vehicle_type": "SUV",
"available_seats": 2,
"total_seats": 4,
"price": 50,
"driver_message": cleaned_msg,
}
)
assert result.ok is True
created_trip = repository.created_trips[-1]
assert created_trip["use_driver_message"] is True
assert created_trip["driver_message"] == cleaned_msg
@pytest.mark.asyncio
async def test_add_trip_by_driver_uses_cleaned_message_without_many_emojis():
repository = FakeRepository()
repository.drivers_by_remote_jid["967700000010"] = {
"id": "driver-1",
"name": "Ali",
"remoteJid": "967700000010",
}
repository.driver_cars_by_driver["driver-1"] = [
{"id": "car-1", "car_type": "SUV", "seat_count": 4},
]
raw_msg = "ุฑุญูุฉ ููู
ูุฉ ู
ู ุนุฏู ุฅูู ุงูู
ููุง ููุชูุงุตู 770026665"
cleaned_msg = "ุฑุญูุฉ ููู
ูุฉ ู
ู ุนุฏู ุฅูู ุงูู
ููุง"
handlers = make_handlers(repository=repository, sender_phone="967700000010")
handlers.current_message = {"message": raw_msg}
result = await handlers.add_trip_by_driver(
{
"departure": "ุนุฏู",
"destination": "ุงูู
ููุง",
"departure_date": "2026-06-10",
"departure_time": "morning",
"vehicle_type": "SUV",
"available_seats": 2,
"total_seats": 4,
"price": 50,
"driver_message": cleaned_msg,
}
)
assert result.ok is True
created_trip = repository.created_trips[-1]
assert created_trip["use_driver_message"] is True
assert created_trip["driver_message"] == cleaned_msg
@pytest.mark.asyncio
async def test_add_trip_by_driver_no_driver_message_with_few_emojis():
repository = FakeRepository()
repository.drivers_by_remote_jid["967700000010"] = {
"id": "driver-1",
"name": "Ali",
"remoteJid": "967700000010",
}
repository.driver_cars_by_driver["driver-1"] = [
{"id": "car-1", "car_type": "SUV", "seat_count": 4},
]
current_message = {"message": "ุจุงุต ู
ู ุนุฏู ุงูู ุงูู
ููุง ๐"}
handlers = make_handlers(repository=repository, sender_phone="967700000010")
handlers.current_message = current_message
result = await handlers.add_trip_by_driver(
{
"departure": "ุนุฏู",
"destination": "ุงูู
ููุง",
"departure_date": "2026-06-10",
"departure_time": "morning",
"vehicle_type": "SUV",
"available_seats": 2,
"total_seats": 4,
"price": 50,
}
)
assert result.ok is True
created_trip = repository.created_trips[-1]
assert created_trip["use_driver_message"] is False
assert created_trip["driver_message"] is None
@pytest.mark.asyncio
async def test_add_trip_by_driver_no_current_message():
repository = FakeRepository()
repository.drivers_by_remote_jid["967700000010"] = {
"id": "driver-1",
"name": "Ali",
"remoteJid": "967700000010",
}
repository.driver_cars_by_driver["driver-1"] = [
{"id": "car-1", "car_type": "SUV", "seat_count": 4},
]
handlers = make_handlers(repository=repository, sender_phone="967700000010")
result = await handlers.add_trip_by_driver(
{
"departure": "ุนุฏู",
"destination": "ุงูู
ููุง",
"departure_date": "2026-06-10",
"departure_time": "morning",
"vehicle_type": "SUV",
"available_seats": 2,
"total_seats": 4,
"price": 50,
}
)
assert result.ok is True
created_trip = repository.created_trips[-1]
assert created_trip["use_driver_message"] is False
assert created_trip["driver_message"] is None
@pytest.mark.asyncio
async def test_switch_to_driver_requires_existing_driver_account():
repository = FakeRepository()
customer = {"id": "cust-1", "remoteJid": "967700000001"}
handlers = make_handlers(repository=repository, customer=customer)
result = await handlers.switch_to_driver({})
assert result.ok is False
assert result.data["action"] == "create_driver_account"
@pytest.mark.asyncio
async def test_switch_to_driver_updates_customer_mode():
repository = FakeRepository()
customer = await repository.upsert_customer(remote_jid="967700000001")
repository.drivers_by_remote_jid["967700000001"] = {
"id": "driver-1",
"name": "Ali",
"remoteJid": "967700000001",
}
handlers = make_handlers(repository=repository, customer=customer)
result = await handlers.switch_to_driver({})
assert result.ok is True
assert result.data["user_mode"] == "driver"
assert customer["user_mode"] == "driver"
@pytest.mark.asyncio
async def test_switch_to_passenger_updates_mode_and_optional_name():
repository = FakeRepository()
customer = await repository.upsert_customer(remote_jid="967700000001")
handlers = make_handlers(repository=repository, customer=customer)
result = await handlers.switch_to_passenger({"name": "Sara"})
assert result.ok is True
assert result.data["user_mode"] == "passenger"
assert customer["user_mode"] == "passenger"
assert customer["name"] == "Sara"
@pytest.mark.asyncio
async def test_switch_to_passenger_without_name():
repository = FakeRepository()
customer = await repository.upsert_customer(remote_jid="967700000001", name="Existing")
handlers = make_handlers(repository=repository, customer=customer)
result = await handlers.switch_to_passenger({})
assert result.ok is True
assert customer["user_mode"] == "passenger"
assert customer["name"] == "Existing"
def _driver_setup(repository: FakeRepository, *, phone: str = "967700000010") -> None:
repository.drivers_by_remote_jid[phone] = {
"id": "driver-1",
"name": "Ali",
"phone_number": phone,
}
repository.trips_by_id["trip-1"] = {
"id": "trip-1",
"driver_id": "driver-1",
"departure": "ุนุฏู",
"destination": "ุงูู
ููุง",
"departure_date": "2026-12-01",
"departure_time": "morning",
"available_seats": 2,
"total_seats": 4,
"price": "80.00",
"status": "active",
"driver_cars": {"car_type": "SUV"},
"drivers": {"name": "Ali"},
}
@pytest.mark.asyncio
async def test_initiate_trip_action_sends_trip_cards():
repository = FakeRepository()
whatsapp = FakeWhatsApp()
_driver_setup(repository)
handlers = make_handlers(
repository=repository,
whatsapp=whatsapp,
sender_phone="967700000010",
)
result = await handlers.initiate_trip_action({"action_type": "DELETE"})
assert result.ok is True
assert result.data["count"] == 1
assert result.suppress_llm_reply is True
assert len(whatsapp.sent) == 2 # card + prompt
card_text = whatsapp.sent[0][1]
assert "ุนุฏู" in card_text
assert "ุงูู
ููุง" in card_text
@pytest.mark.asyncio
async def test_initiate_trip_action_returns_no_trips_message():
repository = FakeRepository()
_driver_setup(repository)
repository.trips_by_id.clear()
handlers = make_handlers(repository=repository, sender_phone="967700000010")
result = await handlers.initiate_trip_action({"action_type": "MODIFY"})
assert result.ok is True
assert result.data["count"] == 0
assert "No trips found" in result.data["message"]
@pytest.mark.asyncio
async def test_update_trip_field_uses_active_session_and_clears_it():
repository = FakeRepository()
customer = await repository.upsert_customer(phone_number="967700000010")
await repository.set_customer_session_field(
customer_id=customer["id"],
key="active_edit_trip_id",
value="trip-1",
)
_driver_setup(repository)
handlers = make_handlers(
repository=repository,
customer=customer,
sender_phone="967700000010",
)
result = await handlers.update_trip_field(
{"field": "pickup_time", "value": "15:00"},
)
assert result.ok is True
assert repository.trips_by_id["trip-1"]["departure_time"] == "noon"
assert await repository.get_customer_session(customer["id"]) == {}
assert len(repository.trip_embeddings) == 1
|