# TS-01 — Returning user re-pays an online bestie (lama happy path)
# (requirement/phase4-customer-flow.md → Test Scenarios → TS-01).
#
# §4 branch covered: Choice → "bestie yang udah kenal" → CheckOnline(yes) →
#   PickMethod → PickDuration → PayMethod → Bayar → WaitPay → paid →
#   notif-gate → PairRoute(lama) → Targeted → accept → S10 chat.
#
# Pre-reqs (HARD — flow assumes these):
#   - Backend reachable (BACKEND_INTERNAL_URL).
#   - NODE_ENV != 'production' (so /internal/_test/* routes are registered).
#   - At least ONE mitra is currently online in dev DB. The dev seed
#     (backend/src/db/seed.js) does NOT auto-create mitras — ops must sign
#     in a test mitra via mitra_app + heartbeat (or insert
#     mitra_online_status manually). `seed_history_session.js` picks the
#     most-recently-online mitra; that same mitra must still be online when
#     the flow reaches the post-payment targeted-wait so
#     `mitra_accept_latest_internal.js` can drive accept.
#
# Run:
#   maestro test client_app/.maestro/flows/ts-01_returning_lama_online.yaml
appId: com.mybestie
env:
  TEST_PHONE: "+6281234567890"
  BACKEND_INTERNAL_URL: http://localhost:3001
---
# --- 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

# --- Seed a completed chat_sessions row so the bestie history list isn't
# empty. The seed picks the most-recently-online mitra; that mitra remains
# online (this is the "lama" branch — TS-01 needs them online). ---
- runScript:
    file: ../scripts/seed_history_session.js
    env:
      TEST_PHONE: ${TEST_PHONE}
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}

# bestieHistoryProvider was already evaluated empty on the first home
# render. Pull-to-refresh to re-fetch /chat/history so the seeded row shows
# up and the CTA opens BestieChoiceSheet (instead of jumping straight to
# /payment/entry, which is the no-history branch).
- swipe:
    start: "50%, 30%"
    end: "50%, 80%"
- extendedWaitUntil:
    visible:
      text: "(?s).*curhatan sebelumnya.*"
    timeout: 10000

# --- Step 1: tap CTA → Bestie Choice Sheet ---
# After seeding, the CTA label flips from "aku mau curhat" (SHome1st) to
# "curhat sama bestie baru" (SHomeReturning).
- tapOn:
    text: "(?s).*curhat sama bestie baru.*"
    retryTapIfNoChange: true

# Sheet title "mau curhat sama siapa?" — `?` is regex meta; wrap in (?s).*…*
# and use `.` for the trailing literal `?`.
- extendedWaitUntil:
    visible:
      text: "(?s).*mau curhat sama siapa.*"
    timeout: 5000

# --- Step 2: pick "bestie yang udah kenal" → history list ---
- tapOn: "(?s).*bestie yang udah kenal.*"
- extendedWaitUntil:
    visible:
      text: "(?s).*bestie kamu sebelumnya.*"
    timeout: 5000

# --- Step 3: tap the seeded mitra row → /payment/entry → method-pick ---
# Row label is "bestie <name>" + ONLINE pill (merged blob).
- tapOn: "(?s).*bestie ${output.MITRA_NAME_RE}.*"

# /payment/entry is a routing shim; for a returning customer with prior
# completed sessions the discount is NOT eligible, so it forwards to
# /payment/method-pick (`pilih cara curhat`).
- extendedWaitUntil:
    visible:
      text: "(?s).*pilih cara curhat.*"
    timeout: 10000

# --- Step 4: pick chat mode → /payment/duration-pick ---
- tapOn:
    text: "(?s).*tulis dan baca dengan tenang.*"
- extendedWaitUntil:
    visible:
      text: "(?s).*pilih durasi.*"
    timeout: 10000

# --- Step 5: pick cheapest tier → CTA shows "bayar Rp X" ---
# Tiers render via tier.label / formatRupiah; "5 Menit" is current label.
- tapOn: "(?s).*5 Menit.*"
- tapOn: "(?s).*bayar Rp.*"

# --- Step 6: cara-bayar screen → QRIS preselected → tap bayar ---
- extendedWaitUntil:
    visible:
      text: "(?s).*cara bayar.*"
    timeout: 10000
- tapOn:
    text: "(?s).*bayar Rp.*"
    retryTapIfNoChange: true

# --- Step 7: waiting-payment screen → 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 8: notif-gate auto-advances (or "nanti aja" if shown) →
# /chat/searching kicks off targeted-pair → routes to
# /chat/waiting-targeted/<mitraId> ---
# We accept either landing on notif-gate (if perm not yet decided) or
# directly on targeted-waiting (if perm was already granted).
- runFlow:
    when:
      visible:
        text: "(?s).*biar nggak ketinggalan.*"
    commands:
      - tapOn:
          text: "(?s).*nanti aja.*"
- extendedWaitUntil:
    visible:
      text: "(?s).*MENUNGGU JAWABAN.*"
    timeout: 20000
- assertVisible: "(?s).*lagi nungguin.*"

# --- Step 9: mitra-side accept via dev-only internal route ---
- runScript:
    file: ../scripts/mitra_accept_latest_internal.js
    env:
      MITRA_ID: ${output.MITRA_ID}
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}

# --- Step 10: chat screen renders ---
# Header shows mitra name + "online" status pill. The presence of either
# the mitra name (post-accept) or the "online" indicator confirms we're on
# the chat screen.
- extendedWaitUntil:
    visible:
      text: "(?s).*online.*"
    timeout: 20000
- assertVisible: "(?s).*${output.MITRA_NAME_RE}.*"
