#!/usr/bin/env bats # tests/test_ui.bats — BATS tests for fs5 ui subcommand (UIAutomator-based) # Tests use a pre-dumped fixture XML to avoid requiring a live device for most cases. SCRIPT_DIR="$(cd "$(dirname "${BATS_TEST_FILENAME}")/.." && pwd)" FS5="${SCRIPT_DIR}/fs5" # ── Fixture XML (KDE Connect screen, 15 elements) ──────────────────────────── FIXTURE_XML="${BATS_TMPDIR}/fs5_ui_fixture.xml" setup_file() { # Create a minimal UIAutomator XML fixture cat > "${FIXTURE_XML}" <<'XML' XML } # ── ui --help ───────────────────────────────────────────────────────────────── @test "ui --help shows subcommands" { run "${FS5}" ui --help [ "${status}" -eq 0 ] [[ "${output}" == *"dump"* ]] [[ "${output}" == *"elements"* ]] [[ "${output}" == *"find"* ]] [[ "${output}" == *"tap"* ]] } @test "ui unknown subcommand exits 1" { run "${FS5}" ui bogus [ "${status}" -eq 1 ] [[ "${output}" == *"Unknown ui subcommand"* ]] } # ── ui dump ─────────────────────────────────────────────────────────────────── @test "ui dump --help exits 0" { run "${FS5}" ui dump --help [ "${status}" -eq 0 ] [[ "${output}" == *"UIAutomator"* ]] } # ── ui elements ─────────────────────────────────────────────────────────────── @test "ui elements with fixture shows element count" { run "${FS5}" ui elements "${FIXTURE_XML}" [ "${status}" -eq 0 ] [[ "${output}" == *"Found"*"elements"* ]] } @test "ui elements with fixture shows clickable marker" { run "${FS5}" ui elements "${FIXTURE_XML}" [ "${status}" -eq 0 ] [[ "${output}" == *"[✓]"* ]] } @test "ui elements with fixture shows More options" { run "${FS5}" ui elements "${FIXTURE_XML}" [ "${status}" -eq 0 ] [[ "${output}" == *"More options"* ]] } @test "ui elements --json returns JSON array" { run "${FS5}" ui elements "${FIXTURE_XML}" --json [ "${status}" -eq 0 ] # Output starts with JSON array [[ "${output}" == "["* ]] } @test "ui elements --json contains center_x field" { run "${FS5}" ui elements "${FIXTURE_XML}" --json [ "${status}" -eq 0 ] [[ "${output}" == *"center_x"* ]] } @test "ui elements --json contains clickable field" { run "${FS5}" ui elements "${FIXTURE_XML}" --json [ "${status}" -eq 0 ] [[ "${output}" == *"clickable"* ]] } @test "ui elements --help exits 0" { run "${FS5}" ui elements --help [ "${status}" -eq 0 ] } # ── ui find ─────────────────────────────────────────────────────────────────── @test "ui find by content-desc returns coordinates" { run "${FS5}" ui find "More options" "${FIXTURE_XML}" [ "${status}" -eq 0 ] # Output should be "x y" format [[ "${output}" =~ ^[0-9]+\ [0-9]+$ ]] } @test "ui find by text returns coordinates" { run "${FS5}" ui find "Send files" "${FIXTURE_XML}" [ "${status}" -eq 0 ] [[ "${output}" =~ ^[0-9]+\ [0-9]+$ ]] } @test "ui find case-insensitive match" { run "${FS5}" ui find "send files" "${FIXTURE_XML}" [ "${status}" -eq 0 ] [[ "${output}" =~ ^[0-9]+\ [0-9]+$ ]] } @test "ui find nonexistent element exits 1" { run "${FS5}" ui find "nonexistent_xyz_abc" "${FIXTURE_XML}" [ "${status}" -eq 1 ] [[ "${output}" == *"Element not found"* ]] } @test "ui find --json returns JSON with x and y" { run "${FS5}" ui find "More options" --json "${FIXTURE_XML}" [ "${status}" -eq 0 ] [[ "${output}" == *'"x"'* ]] [[ "${output}" == *'"y"'* ]] } @test "ui find --json contains text field" { run "${FS5}" ui find "More options" --json "${FIXTURE_XML}" [ "${status}" -eq 0 ] [[ "${output}" == *'"text"'* ]] [[ "${output}" == *"More options"* ]] } @test "ui find missing argument exits 1" { run "${FS5}" ui find [ "${status}" -eq 1 ] [[ "${output}" == *"Usage"* ]] } @test "ui find --help exits 0" { run "${FS5}" ui find --help [ "${status}" -eq 0 ] } # ── ui tap ──────────────────────────────────────────────────────────────────── @test "ui tap --help exits 0" { # --help is intercepted by global parse_global_flags (same as 'key --help') run "${FS5}" ui tap --help [ "${status}" -eq 0 ] [[ "${output}" == *"ui tap"* ]] } @test "ui tap missing argument exits 1" { run "${FS5}" ui tap [ "${status}" -eq 1 ] [[ "${output}" == *"Usage"* ]] } @test "ui tap nonexistent element exits 1" { run "${FS5}" ui tap "nonexistent_xyz_abc" "${FIXTURE_XML}" [ "${status}" -eq 1 ] [[ "${output}" == *"Element not found"* ]] }