rwadurian/tools/e2e_wallet_test.ps1

169 lines
5.1 KiB
PowerShell

# =============================================================================
# E2E Test: Create Account and Wait for Wallet Ready (PowerShell)
# =============================================================================
#
# This script simulates the mobile app flow:
# 1. Call POST /user/auto-create to create a new account
# 2. Poll GET /user/wallet until wallet is ready (has mnemonic and 3 addresses)
#
# Usage: .\e2e_wallet_test.ps1 [-BaseUrl "http://localhost:3000/api/v1"]
# =============================================================================
param(
[string]$BaseUrl = "http://localhost:3000/api/v1"
)
$ErrorActionPreference = "Stop"
# Configuration
$DeviceId = "test-device-$(Get-Date -Format 'yyyyMMddHHmmss')"
$MaxRetries = 60
$RetryInterval = 5
Write-Host "=============================================="
Write-Host "E2E Wallet Creation Test"
Write-Host "=============================================="
Write-Host "Base URL: $BaseUrl"
Write-Host "Device ID: $DeviceId"
Write-Host "=============================================="
Write-Host ""
# Step 1: Create Account
Write-Host "[Step 1] Creating account..."
$CreateBody = @{
deviceId = $DeviceId
deviceName = @{
brand = "Test"
model = "E2E-Device"
platform = "android"
osVersion = "14"
}
} | ConvertTo-Json -Depth 3
try {
$CreateResponse = Invoke-RestMethod -Uri "$BaseUrl/user/auto-create" `
-Method Post `
-ContentType "application/json" `
-Body $CreateBody
} catch {
Write-Host "❌ Failed to create account!" -ForegroundColor Red
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
if (-not $CreateResponse.success) {
Write-Host "❌ Failed to create account!" -ForegroundColor Red
Write-Host "Error: $($CreateResponse.message)"
exit 1
}
$AccessToken = $CreateResponse.data.accessToken
$UserSerialNum = $CreateResponse.data.userSerialNum
$Username = $CreateResponse.data.username
$ReferralCode = $CreateResponse.data.referralCode
Write-Host "✅ Account created successfully!" -ForegroundColor Green
Write-Host " User Serial Number: $UserSerialNum"
Write-Host " Username: $Username"
Write-Host " Referral Code: $ReferralCode"
Write-Host ""
# Step 2: Poll Wallet Status
Write-Host "[Step 2] Polling wallet status..."
Write-Host " Max retries: $MaxRetries"
Write-Host " Retry interval: ${RetryInterval}s"
Write-Host ""
$Headers = @{
"Authorization" = "Bearer $AccessToken"
"Content-Type" = "application/json"
}
$WalletReady = $false
$WalletResponse = $null
for ($i = 1; $i -le $MaxRetries; $i++) {
try {
$WalletResponse = Invoke-RestMethod -Uri "$BaseUrl/user/wallet" `
-Method Get `
-Headers $Headers
} catch {
Write-Host "[Attempt $i/$MaxRetries] Error: $($_.Exception.Message)" -ForegroundColor Yellow
Start-Sleep -Seconds $RetryInterval
continue
}
$WalletStatus = $WalletResponse.data.status
Write-Host "[Attempt $i/$MaxRetries] Wallet status: $WalletStatus"
if ($WalletStatus -eq "ready") {
$WalletReady = $true
break
} elseif ($WalletStatus -eq "failed") {
Write-Host "❌ Wallet generation failed!" -ForegroundColor Red
Write-Host "Error: $($WalletResponse.data.error)"
exit 1
}
# Still generating, wait and retry
Start-Sleep -Seconds $RetryInterval
}
if (-not $WalletReady) {
Write-Host "❌ Timeout waiting for wallet to be ready!" -ForegroundColor Red
exit 1
}
Write-Host ""
Write-Host "=============================================="
Write-Host "✅ WALLET READY!" -ForegroundColor Green
Write-Host "=============================================="
Write-Host ""
# Extract wallet data
$Mnemonic = $WalletResponse.data.mnemonic
$KavaAddress = $WalletResponse.data.walletAddresses.kava
$DstAddress = $WalletResponse.data.walletAddresses.dst
$BscAddress = $WalletResponse.data.walletAddresses.bsc
Write-Host "📝 Mnemonic (12 words):" -ForegroundColor Cyan
Write-Host " $Mnemonic"
Write-Host ""
Write-Host "💰 Wallet Addresses:" -ForegroundColor Cyan
Write-Host " KAVA: $KavaAddress"
Write-Host " DST: $DstAddress"
Write-Host " BSC: $BscAddress"
Write-Host ""
Write-Host "=============================================="
Write-Host "Test completed successfully!" -ForegroundColor Green
Write-Host "=============================================="
# Validate results
if ([string]::IsNullOrEmpty($Mnemonic)) {
Write-Host "⚠️ Warning: Mnemonic is empty" -ForegroundColor Yellow
} else {
$WordCount = ($Mnemonic -split '\s+').Count
if ($WordCount -ne 12) {
Write-Host "⚠️ Warning: Mnemonic word count is $WordCount (expected 12)" -ForegroundColor Yellow
}
}
if ([string]::IsNullOrEmpty($KavaAddress) -or [string]::IsNullOrEmpty($DstAddress) -or [string]::IsNullOrEmpty($BscAddress)) {
Write-Host "⚠️ Warning: Some wallet addresses are missing" -ForegroundColor Yellow
}
# Return summary object
return @{
UserSerialNum = $UserSerialNum
Username = $Username
ReferralCode = $ReferralCode
Mnemonic = $Mnemonic
WalletAddresses = @{
Kava = $KavaAddress
Dst = $DstAddress
Bsc = $BscAddress
}
}