# ts-customer-05-02 — Symmetric to 05-01: customer sends a message and the
# mitra is in background → backend `sendMessage` falls back to FCM.
#
# Spec ref: requirement/flow_customer.mermaid.md §5 (Chat Room) +
# the user-stated path:
#   1. WebSocket when app is foreground / WS connected.
#   2. FCM when app is background / WS disconnected.
#
# Fix under test: mitra_app/lib/main.dart `_AppState.didChangeAppLifecycleState`
# closes the chat WS on paused/detached (and `mitra_chat_notifier.dart`
# exposes `connectedSessionId` + clears it on disconnect). Without this,
# Android keeps the TCP socket alive after the Dart isolate pauses, so
# the backend believes the mitra is online and never fires FCM — the
# mitra misses customer messages while the app is in background.
#
# Approach (Maestro can only drive one device per flow; here we drive the
# customer, and the mitra side is verified server-side):
#   1. Drive customer to active chat (same harness as ts-customer-04-04 /
#      ts-customer-05-01). The acceptor is whichever mitra the
#      seed_history_session.js helper picks (most-recently-online → on
#      our setup that is the signed-in mitra on emulator-5556).
#   2. Assert backend says mitra_connected=false. This is true when:
#        (a) the mitra app is force-stopped / not running, OR
#        (b) the mitra app is on the chat screen AND backgrounded — the
#            lifecycle observer should have closed the WS.
#      Either condition fulfils the precondition for FCM fallback.
#   3. Send a message AS customer via /internal/_test/send-chat-message-as-customer
#      and assert delivered_via="fcm".
#
# Pre-reqs (HARD):
#   - Backend reachable; NODE_ENV != 'production'.
#   - emulator-5556 mitra app: either force-stopped before the test, or
#     signed in with TestMitra-1501 (the seed_history_session pick) and
#     currently backgrounded. If the mitra is foreground in the chat
#     screen, mitra_connected=true and this test asserts will fail —
#     which is the correct failure mode (proves WS-over-FCM precedence).
#   - The currently-signed-in mitra must have a `fcm_token` row in
#     `mitras.fcm_token`; otherwise the FCM dispatch succeeds at the
#     backend code path but never reaches a device.
appId: com.halobestie.client.client_app
env:
  TEST_PHONE: "+6281234567890"
  BACKEND_INTERNAL_URL: http://localhost:3001
---
# ── Setup: drive customer through onboarding → pair via blast ───────────
- runScript:
    file: ../scripts/reset_phone.js
    env:
      TEST_PHONE: ${TEST_PHONE}
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}
- launchApp:
    clearState: true
- runFlow: ../subflows/onboarding_returning_user.yaml

- runScript:
    file: ../scripts/seed_history_session.js
    env:
      TEST_PHONE: ${TEST_PHONE}
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}
- swipe:
    start: "50%, 30%"
    end: "50%, 80%"
- extendedWaitUntil:
    visible:
      text: "(?s).*curhatan sebelumnya.*"
    timeout: 10000

- tapOn:
    text: "(?s).*curhat sama bestie baru.*"
    retryTapIfNoChange: true
- extendedWaitUntil:
    visible:
      text: "(?s).*mau curhat sama siapa.*"
    timeout: 5000
- tapOn: "(?s).*cari bestie baru yang siap dengerin.*"

- extendedWaitUntil:
    visible:
      text: "(?s).*pilih cara curhat.*"
    timeout: 10000
- tapOn:
    text: "(?s).*tulis dan baca dengan tenang.*"
- extendedWaitUntil:
    visible:
      text: "(?s).*pilih durasi.*"
    timeout: 10000
- tapOn: "(?s).*5 menit.*"
- tapOn: "(?s).*bayar Rp.*"
- extendedWaitUntil:
    visible:
      text: "(?s).*cara bayar.*"
    timeout: 10000
- tapOn:
    text: "(?s).*bayar Rp.*"
    retryTapIfNoChange: true
- extendedWaitUntil:
    visible:
      text: "scan QRIS untuk bayar"
    timeout: 10000
- runScript:
    file: ../scripts/mark_latest_payment_paid.js
    env:
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}
- runFlow:
    when:
      visible:
        text: "(?s).*biar nggak ketinggalan.*"
    commands:
      - tapOn:
          text: "(?s).*nanti aja.*"
- extendedWaitUntil:
    visible:
      text: "(?s).*lagi nyari bestie.*"
    timeout: 20000
- runScript:
    file: ../scripts/mitra_accept_latest_internal.js
    env:
      MITRA_ID: ${output.MITRA_ID}
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}
- extendedWaitUntil:
    visible:
      text: "(?s).*online.*"
    timeout: 20000

# ── Assertion 1: backend says mitra is NOT connected ────────────────────
# Precondition guard: if the mitra app is foreground, this assert fails
# loud — which is what we want (it proves WS would win over FCM).
- runScript:
    file: ../scripts/assert_ws_state.js
    env:
      SESSION_ID: ${output.ACCEPTED_SESSION_ID}
      EXPECTED_CUSTOMER_CONNECTED: "true"
      EXPECTED_MITRA_CONNECTED: "false"
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}

# ── Assertion 2: a customer-sent message is delivered via FCM ───────────
- runScript:
    file: ../scripts/assert_delivered_via.js
    env:
      SESSION_ID: ${output.ACCEPTED_SESSION_ID}
      CONTENT: "halo bestie, ini pesan saat kamu di background"
      EXPECTED_DELIVERED_VIA: "fcm"
      SENDER: "customer"
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}
