# dm/send-message.yaml
#
# Journey: Login → Chat tab → open first DM conversation → type a message →
#          send → assert message appears in bubble list.
#
# What this covers:
#   - DM screen loads without crash
#   - Text input is accessible and accepts input
#   - Send button (or Return key) dispatches the message
#   - Optimistic message bubble appears immediately after send
#   - (Realtime delivery to a second device is NOT tested here — single device)
#
# Pre-requisite:
#   - Test-user has at least one existing DM conversation on staging.
#   - The DM partner must exist (any user — staging DB seeded).
#   - If no existing DM: create one manually first via the app, then run flow.
#
# IMPORTANT: This flow requires the test user to have a pre-existing DM.
#   If the chat list is empty, the flow will correctly fail at "Direktnachrichten"
#   being visible but no conversation entry to tap — making the setup gap obvious.
#
# Selector notes:
#   - DM list items: no testID. Matched by peer nickname text.
#     If E2E_TEST_PEER_NICKNAME is set, we tap that. Otherwise: first item below
#     "Direktnachrichten" label. THIS IS FRAGILE.
#   - DM screen text input: no testID. chat.placeholder = "Nachricht schreiben…"
#   - Send button: no testID. Using Return key (onSubmitEditing) as fallback.
#
# Env-Vars: E2E_TEST_USER, E2E_TEST_PASSWORD
# Optional: E2E_TEST_PEER_NICKNAME (if set, tapped directly; else first DM item)

appId: org.rebreak.app
---
- launchApp:
    clearState: true

- waitForAnimationToEnd:
    timeout: 5000

# --- Auth ---
- assertVisible:
    text: "E-Mail"
- tapOn:
    text: "E-Mail"
- inputText: ${E2E_TEST_USER}@rebreak.internal
- tapOn:
    text: "Passwort"
- inputText: ${E2E_TEST_PASSWORD}
- tapOn:
    text: "Anmelden"
- waitForAnimationToEnd:
    timeout: 10000

- assertVisible:
    text: "ReBreak"

# --- Navigate to Chat tab ---
- tapOn:
    text: "Chat"
- waitForAnimationToEnd:
    timeout: 3000

- assertVisible:
    text: "Direktnachrichten"

# --- Open DM conversation ---
# Tap into an existing DM. We need a peer to tap on.
# If E2E_TEST_PEER_NICKNAME env var is set, use it as the tap target.
# This is the cleanest path — set this var to the nickname of the staging peer.
- tapOn:
    text: ${E2E_TEST_PEER_NICKNAME}
- waitForAnimationToEnd:
    timeout: 4000

# DM screen loaded: partner name shown in header (AppHeader title).
# We assert the message input placeholder is visible.
# chat.placeholder = "Nachricht schreiben…" (de.json line 1072)
- assertVisible:
    text: "Nachricht schreiben…"

# --- Compose and send a message ---
- tapOn:
    text: "Nachricht schreiben…"
- waitForAnimationToEnd:
    timeout: 1000

- inputText: "[E2E] Maestro-Testnachricht"

# Submit via Return key. dm.tsx TextInput has onSubmitEditing={handleSend}.
# This avoids needing testID on the send Pressable (currently missing).
- pressKey: Return
- waitForAnimationToEnd:
    timeout: 5000

# After send: the optimistic message bubble appears immediately with the sent text.
# The bubble shows the message content. Assert the text is visible in the list.
- assertVisible:
    text: "[E2E] Maestro-Testnachricht"

# Input bar resets to empty — placeholder is visible again.
- assertVisible:
    text: "Nachricht schreiben…"
