Files
customerTicket/tests/e2e/test_chat_coach.py
T
2026-09-16 10:40:21 +03:30

109 lines
4.8 KiB
Python

import pytest
from django.urls import reverse
from apps.accounts.models import User
from apps.customers.models import CustomerProfile
from apps.messaging.models import Message
from apps.staff.models import StaffProfile
from apps.tickets.models import TicketCategory
from apps.tickets.services import create_ticket
from tests.e2e.pages.auth import AuthenticationPage
from tests.e2e.pages.tickets import TicketConversationPage
pytestmark = [pytest.mark.e2e, pytest.mark.django_db(transaction=True)]
PASSWORD = "E2e-safe-password-2026"
def test_customer_sees_chat_guidance_after_two_rapid_messages(live_server, browser_page):
category = TicketCategory.objects.create(title="فروش E2E", slug="e2e-customer-chat")
customer = User.objects.create_user(
username="e2e-customer-chat",
mobile="09120009001",
password=PASSWORD,
account_type=User.AccountType.CUSTOMER,
)
CustomerProfile.objects.create(user=customer, first_name="مشتری", last_name="آزمایشی")
ticket = create_ticket(customer=customer, category=category, subject="تست هشدار مشتری", body="سلام")
with browser_page() as page:
AuthenticationPage(page, live_server.url).login_customer(customer, PASSWORD)
conversation = TicketConversationPage(page, live_server.url)
conversation.goto(reverse("tickets:detail", args=[ticket.public_id]))
conversation.expect_guidance_hidden()
conversation.send_reply("قیمت چنده؟")
conversation.expect_guidance_visible()
conversation.dismiss_guidance()
conversation.send_reply("موجود دارید؟")
conversation.expect_guidance_visible()
conversation.dismiss_guidance()
def test_staff_sees_chat_guidance_after_two_rapid_replies(live_server, browser_page):
category = TicketCategory.objects.create(title="پشتیبانی E2E", slug="e2e-staff-chat")
customer = User.objects.create_user(
username="e2e-staff-customer",
mobile="09120009002",
password=PASSWORD,
account_type=User.AccountType.CUSTOMER,
)
staff = User.objects.create_user(
username="e2e-staff-chat",
mobile="09120009003",
password=PASSWORD,
account_type=User.AccountType.STAFF,
)
StaffProfile.objects.create(user=staff, display_name="کارشناس E2E")
ticket = create_ticket(
customer=customer,
category=category,
subject="تست هشدار کارشناس",
selected_expert=staff,
body="درخواست مشتری",
)
ticket.owner = staff
ticket.selected_expert = None
ticket.save(update_fields=["owner", "selected_expert", "updated_at"])
with browser_page() as page:
AuthenticationPage(page, live_server.url).login_staff(staff, PASSWORD)
conversation = TicketConversationPage(page, live_server.url)
conversation.goto(reverse("staff_tickets:detail", args=[ticket.public_id]))
conversation.send_reply("پاسخ اول کارشناس")
conversation.expect_guidance_hidden()
conversation.send_reply("پاسخ دوم کارشناس")
conversation.expect_guidance_visible()
conversation.dismiss_guidance()
conversation.send_reply("پاسخ سوم کارشناس")
conversation.expect_guidance_visible()
conversation.dismiss_guidance()
def test_customer_mobile_composer_never_covers_latest_message(live_server, browser_page):
category = TicketCategory.objects.create(title="مکاتبات موبایل", slug="e2e-customer-mobile-composer")
customer = User.objects.create_user(
username="e2e-mobile-customer",
mobile="09120009021",
password=PASSWORD,
account_type=User.AccountType.CUSTOMER,
)
CustomerProfile.objects.create(user=customer, first_name="مشتری", last_name="موبایل")
staff = User.objects.create_user(
username="e2e-mobile-staff",
mobile="09120009022",
account_type=User.AccountType.STAFF,
)
StaffProfile.objects.create(user=staff, display_name="کارشناس موبایل")
ticket = create_ticket(customer=customer, category=category, subject="کنترل composer مشتری", body="پیام آغازین")
for index in range(24):
Message.objects.create(ticket=ticket, author=staff, body=f"پاسخ طولانی کارشناس شماره {index + 1}")
with browser_page() as page:
page.set_viewport_size({"width": 390, "height": 844})
AuthenticationPage(page, live_server.url).login_customer(customer, PASSWORD)
conversation = TicketConversationPage(page, live_server.url)
conversation.goto(reverse("tickets:detail", args=[ticket.public_id]))
conversation.expect_latest_message_above_composer()
conversation.open_composer()
conversation.expect_latest_message_above_composer()