-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-app.ps1
More file actions
233 lines (201 loc) · 7.4 KB
/
Copy pathtest-app.ps1
File metadata and controls
233 lines (201 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# PowerShell script to test the Email-LLM Integration application
# Colors for output
$GREEN = "`e[0;32m"
$BLUE = "`e[0;34m"
$YELLOW = "`e[1;33m"
$RED = "`e[0;31m"
$NC = "`e[0m" # No Color
# Function to display test results
function Show-TestResult {
param (
[string]$testName,
[bool]$success,
[string]$message = ""
)
if ($success) {
Write-Host "${GREEN}[PASS]${NC} $testName"
} else {
Write-Host "${RED}[FAIL]${NC} $testName $message"
}
}
# Function to make HTTP requests
function Invoke-ApiRequest {
param (
[string]$method,
[string]$endpoint,
[object]$body = $null,
[string]$contentType = "application/json"
)
# Use the base URL provided or default to the standard port
if (-not [string]::IsNullOrEmpty($baseUrl)) {
# Use the provided base URL
} else {
$baseUrl = "http://localhost:8080" # Default API port mapped to container port
}
$url = "$baseUrl$endpoint"
try {
$headers = @{
"Content-Type" = $contentType
"Accept" = "application/json"
}
$params = @{
Method = $method
Uri = $url
Headers = $headers
UseBasicParsing = $true
}
if ($null -ne $body) {
if ($body -is [string]) {
$params.Body = $body
} else {
$params.Body = $body | ConvertTo-Json -Depth 10
}
}
$response = Invoke-WebRequest @params
if ($response.StatusCode -ge 200 -and $response.StatusCode -lt 300) {
return @{
Success = $true
StatusCode = $response.StatusCode
Content = $response.Content
Response = $response
}
} else {
return @{
Success = $false
StatusCode = $response.StatusCode
Content = $response.Content
Response = $response
Error = "Request failed with status code $($response.StatusCode)"
}
}
} catch {
return @{
Success = $false
Error = $_.Exception.Message
Exception = $_
}
}
}
# Function to send a test email
function Send-TestEmail {
param (
[string]$to = "test@example.com",
[string]$subject = "Test Email",
[string]$body = "This is a test email."
)
try {
$smtpServer = "localhost"
$smtpPort = 1026 # MailHog SMTP port
$smtpClient = New-Object System.Net.Mail.SmtpClient($smtpServer, $smtpPort)
$message = New-Object System.Net.Mail.MailMessage
$message.From = "sender@example.com"
$message.To.Add($to)
$message.Subject = $subject
$message.Body = $body
$message.IsBodyHtml = $false
$smtpClient.Send($message)
return @{
Success = $true
Message = "Email sent successfully"
}
} catch {
return @{
Success = $false
Error = $_.Exception.Message
}
}
}
# Function to check if Docker containers are running
function Test-ContainersRunning {
$requiredContainers = @("ollama", "mailserver", "camel-groovy-email-llm", "adminer")
$allRunning = $true
$notRunning = @()
foreach ($container in $requiredContainers) {
$containerStatus = docker ps --filter "name=$container" --format "{{.Status}}"
if (-not $containerStatus) {
$allRunning = $false
$notRunning += $container
}
}
if ($allRunning) {
return @{
Success = $true
Message = "All required containers are running"
}
} else {
return @{
Success = $false
Error = "The following containers are not running: $($notRunning -join ', ')"
}
}
}
# Main test function
function Run-Tests {
$testResults = @()
$totalTests = 0
$passedTests = 0
Write-Host "${BLUE}=== Starting Email-LLM Integration Tests ===${NC}"
Write-Host "Date: $(Get-Date)"
Write-Host ""
# Test 1: Check if containers are running
$totalTests++
$containerCheck = Test-ContainersRunning
Show-TestResult -testName "Container Status Check" -success $containerCheck.Success -message $containerCheck.Error
if ($containerCheck.Success) { $passedTests++ }
# Check container logs for errors
Write-Host "${BLUE}[INFO]${NC} Checking container logs for errors..."
$camelLogs = docker logs camel-groovy-email-llm 2>&1
if ($camelLogs -match "Error" -or $camelLogs -match "Exception") {
Write-Host "${YELLOW}[WARNING]${NC} Found errors in camel-groovy-email-llm logs:"
$errorLines = $camelLogs -split "`n" | Where-Object { $_ -match "Error" -or $_ -match "Exception" }
foreach ($line in $errorLines) {
Write-Host " $line"
}
Write-Host ""
}
# Test 2: Health check API
$totalTests++
$healthCheck = Invoke-ApiRequest -method "GET" -endpoint "/api/health"
Show-TestResult -testName "API Health Check" -success $healthCheck.Success -message $healthCheck.Error
if ($healthCheck.Success) { $passedTests++ }
# Test 3: API Documentation endpoint
$totalTests++
$apiDocCheck = Invoke-ApiRequest -method "GET" -endpoint "/api/api-doc"
Show-TestResult -testName "API Documentation Check" -success $apiDocCheck.Success -message $apiDocCheck.Error
if ($apiDocCheck.Success) { $passedTests++ }
# Test 4: LLM Direct Analysis API
$totalTests++
$llmAnalysisBody = @{
text = "Please schedule a meeting for tomorrow at 2 PM to discuss the project status."
context = "Project planning"
model = "mistral"
}
$llmAnalysis = Invoke-ApiRequest -method "POST" -endpoint "/api/llm/direct-analyze" -body $llmAnalysisBody
Show-TestResult -testName "LLM Analysis API" -success $llmAnalysis.Success -message $llmAnalysis.Error
if ($llmAnalysis.Success) { $passedTests++ }
# Test 5: Send test email
$totalTests++
$emailTest = Send-TestEmail -subject "Test Email from PowerShell" -body "This is an automated test email sent from the PowerShell test script."
Show-TestResult -testName "Send Test Email" -success $emailTest.Success -message $emailTest.Error
if ($emailTest.Success) { $passedTests++ }
# Test 6: Check MailHog API for received emails
$totalTests++
Start-Sleep -Seconds 2 # Wait for email processing
$mailhogCheck = Invoke-ApiRequest -method "GET" -endpoint "/api/v2/messages" -baseUrl "http://localhost:8026"
$mailhogSuccess = $mailhogCheck.Success -and ($mailhogCheck.Content -match "Test Email from PowerShell")
Show-TestResult -testName "MailHog Email Check" -success $mailhogSuccess -message "Failed to find test email in MailHog"
if ($mailhogSuccess) { $passedTests++ }
# Summary
Write-Host ""
Write-Host "${BLUE}=== Test Summary ===${NC}"
Write-Host "Total Tests: $totalTests"
Write-Host "Passed: ${GREEN}$passedTests${NC}"
Write-Host "Failed: ${RED}$($totalTests - $passedTests)${NC}"
if ($passedTests -eq $totalTests) {
Write-Host "${GREEN}All tests passed!${NC}"
} else {
Write-Host "${YELLOW}Some tests failed. Check the logs for details.${NC}"
}
}
# Run the tests
Run-Tests