#!/bin/bash

# 工作手机Agent测试脚本

echo "=== 工作手机Agent测试 ==="
echo ""

# 检查设备
echo "1. 检查设备连接..."
DEVICE=$(adb devices | grep "device$" | head -1 | awk '{print $1}')
if [ -z "$DEVICE" ]; then
    echo "❌ 未找到设备，请先启动模拟器"
    exit 1
fi
echo "✅ 设备已连接: $DEVICE"
echo ""

# 检查APP是否安装
echo "2. 检查APP安装..."
if adb shell pm list packages | grep -q "com.workphone.agent"; then
    echo "✅ APP已安装"
else
    echo "❌ APP未安装，正在安装..."
    cd "$(dirname "$0")/../android-app"
    adb install -r app/build/outputs/apk/debug/app-debug.apk
    if [ $? -eq 0 ]; then
        echo "✅ 安装成功"
    else
        echo "❌ 安装失败"
        exit 1
    fi
fi
echo ""

# 启动APP
echo "3. 启动APP..."
adb shell am start -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -n com.workphone.agent/.MainActivity
sleep 2
echo "✅ APP已启动"
echo ""

# 测试功能
echo "4. 功能测试..."
echo ""

echo "4.1 测试打开应用..."
adb shell input text "打开设置" && sleep 1 && adb shell input keyevent KEYCODE_ENTER
sleep 2
echo "✅ 测试完成"
echo ""

echo "4.2 测试返回..."
adb shell input keyevent KEYCODE_BACK
sleep 1
echo "✅ 测试完成"
echo ""

echo "4.3 测试截图..."
adb shell screencap -p /sdcard/test_screenshot.png
if [ $? -eq 0 ]; then
    echo "✅ 截图成功: /sdcard/test_screenshot.png"
    adb pull /sdcard/test_screenshot.png /tmp/test_screenshot.png 2>/dev/null
    if [ -f /tmp/test_screenshot.png ]; then
        echo "✅ 截图已保存到: /tmp/test_screenshot.png"
    fi
else
    echo "❌ 截图失败"
fi
echo ""

echo "4.4 检查日志..."
echo "查看最近日志："
adb logcat -d | grep -i "workphone\|agent" | tail -10
echo ""

echo "=== 测试完成 ==="
echo ""
echo "📱 APP状态："
adb shell "dumpsys window windows | grep -E 'mCurrentFocus'"
echo ""
echo "💡 提示："
echo "  - 在模拟器中手动测试语音功能"
echo "  - 检查设置中的服务器配置"
echo "  - 查看日志: adb logcat | grep WorkPhoneAgent"
