#!/bin/bash

# 测试 OAuth 授权流程
set -e

echo "🔍 Testing OAuth authorization flow..."

# 启动 aiw push 命令
echo "🚀 Starting aiw push..."
timeout 30s ./target/release/aiw push &
PUSH_PID=$!

# 等待 TUI 启动
sleep 5

# 发送确认按键（Enter）
echo "⏺️  Sending Enter key to confirm OAuth dialog..."
xdotool keydown Return
xdotool keyup Return
sleep 2

# 发送 Escape 键返回主界面
echo "⏺️  Sending Escape key to go back..."
xdotool keydown Escape
xdotool keyup Escape
sleep 2

# 等待进程结束
wait $PUSH_PID
EXIT_CODE=$?

echo ""
echo "✅ Test completed with exit code: $EXIT_CODE"

if [ $EXIT_CODE -eq 0 ]; then
    echo "🎉 OAuth flow test passed!"
else
    echo "❌ OAuth flow test failed with exit code: $EXIT_CODE"
    exit 1
fi