# TS-02 — Returning user picks offline bestie → "cari bestie lain" → blast
# (requirement/phase4-customer-flow.md → Test Scenarios → TS-02).
#
# §4 branch covered: Choice → "bestie yang udah kenal" → CheckOnline(no) →
#   OfflinePopup(pre-pay) → "cari bestie lain" → PickMethod → … → paid →
#   PairRoute(baru) → BlastFlow → S10 chat.
#
# Pre-reqs (HARD):
#   - Backend reachable; NODE_ENV != 'production'.
#   - >= 2 mitras online in dev DB at flow start. The flow:
#       1. `seed_history_session.js` picks the most-recently-online mitra
#          (call this M1) and creates a history row.
#       2. `force_mitra_offline.js` forces M1 offline so the history list
#          renders dim.
#       3. After the user takes the blast branch, a SECOND online mitra
#          (M2) is required to accept the blast via
#          `mitra_accept_latest_internal.js`.
#     If only one mitra exists, the blast will have no acceptor and step
#     10's chat-screen assertion will time out.
#   - Dev seed (backend/src/db/seed.js) does NOT auto-create mitras — ops
#     must sign in >= 2 test mitras via mitra_app + heartbeat.
#
# Run:
#   maestro test client_app/.maestro/flows/ts-02_returning_lama_offline_blast.yaml
appId: com.mybestie
env:
  TEST_PHONE: "+6281234567890"
  BACKEND_INTERNAL_URL: http://localhost:3001
  # Second online mitra that will accept the blast. Maestro flows currently
  # don't have a "pick any online mitra" helper, so we rely on
  # mitra-accept-latest taking the FIRST mitra that received the blast.
  # See "Open question" in the task spec — this is the "TS-02 needs two
  # online mitras" caveat.
  TEST_MITRA_ID_ACCEPTOR: "${TEST_MITRA_ID_ACCEPTOR}"
---
# --- Cold-start reset + onboarding prelude ---
- 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

# --- Reset every mitra online first (so seed has someone to pick after
# earlier offline-forcing tests left state dirty). ---
- runScript:
    file: ../scripts/reset_all_mitras_online.js
    env:
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}

# --- Seed history with M1, ensure a different M2 is online (the eventual
# blast acceptor), then force M1 offline. ---
- runScript:
    file: ../scripts/seed_history_session.js
    env:
      TEST_PHONE: ${TEST_PHONE}
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}
- runScript:
    file: ../scripts/force_other_mitra_online.js
    env:
      MITRA_ID: ${output.MITRA_ID}
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}
- runScript:
    file: ../scripts/force_mitra_offline.js
    env:
      MITRA_ID: ${output.MITRA_ID}
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}

# Refresh home to pick up the seeded history row + offline status.
- swipe:
    start: "50%, 30%"
    end: "50%, 80%"
- extendedWaitUntil:
    visible:
      text: "(?s).*curhatan sebelumnya.*"
    timeout: 10000

# --- Steps 1-2: tap CTA → choice sheet → "bestie yang udah kenal" ---
- tapOn:
    text: "(?s).*curhat sama bestie baru.*"
    retryTapIfNoChange: true
- extendedWaitUntil:
    visible:
      text: "(?s).*mau curhat sama siapa.*"
    timeout: 5000
- tapOn: "(?s).*bestie yang udah kenal.*"
- extendedWaitUntil:
    visible:
      text: "(?s).*bestie kamu sebelumnya.*"
    timeout: 5000

# --- Step 3: tap the DIMMED M1 row → BestieOfflinePopup (prePayReturning)
# Popup title: "<mitraName> lagi nggak online" (verified against
# bestie_unavailable_dialog.dart). CTA labels: "cari bestie lain" + "tanya
# admin" + "kembali ke home" (ghost). ---
- tapOn: "(?s).*bestie ${output.MITRA_NAME_RE}.*"
- extendedWaitUntil:
    visible:
      text: "(?s).*${output.MITRA_NAME_RE}.*lagi nggak online.*"
    timeout: 10000
- assertVisible: "(?s).*cari bestie lain.*"
- assertVisible: "(?s).*tanya admin.*"

# --- Step 4: tap "cari bestie lain" → draft.reset() → /payment/entry ---
- tapOn: "(?s).*cari bestie lain.*"

# /payment/entry forwards to /payment/method-pick for an ineligible
# customer (prior completed session → no first-session discount).
- extendedWaitUntil:
    visible:
      text: "(?s).*pilih cara curhat.*"
    timeout: 10000

# --- Step 5: pick chat → duration → tier → cara bayar → bayar ---
- 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

# --- Step 6: waiting-payment → force-confirm ---
- extendedWaitUntil:
    visible:
      text: "scan QRIS untuk bayar"
    timeout: 10000
- runScript:
    file: ../scripts/mark_latest_payment_paid.js
    env:
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}

# --- Step 7: notif-gate → /chat/searching (BLAST, not targeted-wait) ---
# Because the popup's "cari bestie lain" CTA reset the draft, targetedMitraId
# is null → searching_screen.dart fires startSearch() (blast), NOT
# startTargetedSearch().
- runFlow:
    when:
      visible:
        text: "(?s).*biar nggak ketinggalan.*"
    commands:
      - tapOn:
          text: "(?s).*nanti aja.*"
- extendedWaitUntil:
    visible:
      text: "(?s).*lagi nyari bestie.*"
    timeout: 20000

# --- Step 8: any other online mitra accepts the blast ---
# accept-latest-pending picks whichever mitra received the latest blast
# notification — we don't care which one as long as it's not the seeded
# (force-offline) one. Pre-req: ≥1 OTHER online mitra in the dev DB.
- runScript:
    file: ../scripts/accept_latest_pending.js
    env:
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}

# --- Step 9: chat screen renders ---
- extendedWaitUntil:
    visible:
      text: "(?s).*online.*"
    timeout: 20000
