OnyxMunk/AudioForge
0
1# Test Music Generation API
2# Tests the generation endpoint until it works
3
4$Colors = @{
5 Red = "Red"
6 Green = "Green"
7 Yellow = "Yellow"
8 Blue = "Cyan"
9 Cyan = "Cyan"
10}
11
12function Write-Info { Write-Host "[INFO] $args" -ForegroundColor $Colors.Blue }
13function Write-Success { Write-Host "[SUCCESS] $args" -ForegroundColor $Colors.Green }
14function Write-Warning { Write-Host "[WARNING] $args" -ForegroundColor $Colors.Yellow }
15function Write-Error { Write-Host "[ERROR] $args" -ForegroundColor $Colors.Red }
16
17Write-Host "`n╔═══════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
18Write-Host "║ Music Generation API Tester ║" -ForegroundColor Cyan
19Write-Host "╚═══════════════════════════════════════════════════════════╝`n" -ForegroundColor Cyan
20
21$baseUrl = "http://localhost:8000"
22$maxAttempts = 5
23$attempt = 0
24
25# Test 1: Health endpoint
26Write-Info "Test 1: Checking backend health..."
27try {
28 $response = Invoke-WebRequest -Uri "$baseUrl/health" -UseBasicParsing -TimeoutSec 5
29 if ($response.StatusCode -eq 200) {
30 Write-Success "Backend is healthy (Status: $($response.StatusCode))"
31 Write-Host " Response: $($response.Content)" -ForegroundColor Gray
32 } else {
33 Write-Error "Unexpected status: $($response.StatusCode)"
34 exit 1
35 }
36} catch {
37 Write-Error "Backend not responding: $($_.Exception.Message)"
38 Write-Warning "Make sure backend is running on port 8000"
39 exit 1
40}
41
42# Test 2: List generations (GET)
43Write-Info "`nTest 2: Testing GET /api/v1/generations..."
44try {
45 $response = Invoke-WebRequest -Uri "$baseUrl/api/v1/generations" -UseBasicParsing -TimeoutSec 5
46 if ($response.StatusCode -eq 200) {
47 Write-Success "GET generations works (Status: $($response.StatusCode))"
48 $data = $response.Content | ConvertFrom-Json
49 Write-Host " Found $($data.total) generations" -ForegroundColor Gray
50 } else {
51 Write-Error "Unexpected status: $($response.StatusCode)"
52 }
53} catch {
54 $statusCode = $_.Exception.Response.StatusCode.value__
55 Write-Error "GET failed with status $statusCode : $($_.Exception.Message)"
56 if ($statusCode -eq 401) {
57 Write-Warning "401 Unauthorized - Check CORS and authentication middleware"
58 }
59}
60
61# Test 3: Create generation (POST)
62Write-Info "`nTest 3: Testing POST /api/v1/generations..."
63$testPrompt = "A calm acoustic guitar melody"
64$body = @{
65 prompt = $testPrompt
66 duration = 10
67} | ConvertTo-Json
68
69while ($attempt -lt $maxAttempts) {
70 $attempt++
71 Write-Host "`nAttempt $attempt/$maxAttempts..." -ForegroundColor Yellow
72
73 try {
74 $response = Invoke-WebRequest -Uri "$baseUrl/api/v1/generations" `
75 -Method POST `
76 -Body $body `
77 -ContentType "application/json" `
78 -UseBasicParsing `
79 -TimeoutSec 10
80
81 if ($response.StatusCode -eq 202) {
82 Write-Success "✅ Generation created successfully!"
83 $data = $response.Content | ConvertFrom-Json
84 Write-Host " Generation ID: $($data.id)" -ForegroundColor Gray
85 Write-Host " Status: $($data.status)" -ForegroundColor Gray
86 Write-Host "`n🎉 Music generation feature is working!`n" -ForegroundColor Green
87 exit 0
88 } else {
89 Write-Warning "Unexpected status: $($response.StatusCode)"
90 Write-Host " Response: $($response.Content)" -ForegroundColor Gray
91 }
92 } catch {
93 $statusCode = if ($_.Exception.Response) { $_.Exception.Response.StatusCode.value__ } else { "Unknown" }
94 $errorMsg = $_.Exception.Message
95
96 Write-Error "POST failed: Status $statusCode - $errorMsg"
97
98 if ($statusCode -eq 401) {
99 Write-Warning "401 Unauthorized detected!"
100 Write-Host " Checking for authentication middleware..." -ForegroundColor Yellow
101 # Check if there's auth middleware we need to disable
102 } elseif ($statusCode -eq 500) {
103 Write-Warning "500 Internal Server Error - Check backend logs"
104 Write-Host " This might be a database connection issue" -ForegroundColor Yellow
105 } elseif ($statusCode -eq 0 -or $statusCode -eq "Unknown") {
106 Write-Warning "Connection error - Backend might not be running"
107 }
108
109 if ($attempt -lt $maxAttempts) {
110 Write-Host " Waiting 3 seconds before retry..." -ForegroundColor Gray
111 Start-Sleep -Seconds 3
112 }
113 }
114}
115
116Write-Error "`n❌ Failed to create generation after $maxAttempts attempts"
117Write-Host "`nTroubleshooting steps:" -ForegroundColor Yellow
118Write-Host "1. Check backend logs for errors" -ForegroundColor White
119Write-Host "2. Verify Docker containers are running: docker ps" -ForegroundColor White
120Write-Host "3. Check database connection: python backend/test_db_connection.py" -ForegroundColor White
121Write-Host "4. Verify CORS settings in backend/app/main.py" -ForegroundColor White
122exit 1
123 