This repository was archived by the owner on Mar 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-conversion.ps1
More file actions
218 lines (181 loc) · 8.25 KB
/
Copy pathtest-conversion.ps1
File metadata and controls
218 lines (181 loc) · 8.25 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
#!/usr/bin/env pwsh
# NLdoc Conversion Test Suite
# Tests the conversion pipeline via API without editor complexity
param(
[string]$TestFile = "",
[string]$ApiBase = "https://api.nldoc.commonground.nu",
[switch]$SkipUpload
)
$ErrorActionPreference = "Stop"
function Write-TestHeader($msg) {
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host " $msg" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
}
function Write-TestResult($name, $success, $details = "") {
if ($success) {
Write-Host "[PASS] $name" -ForegroundColor Green
} else {
Write-Host "[FAIL] $name" -ForegroundColor Red
}
if ($details) {
Write-Host " $details" -ForegroundColor Gray
}
}
# Test 1: API Health Check
Write-TestHeader "Test 1: API Health Check"
try {
$health = Invoke-WebRequest -Uri "$ApiBase/health" -Method GET -TimeoutSec 10 -ErrorAction SilentlyContinue
Write-TestResult "API responds" $true "Status: $($health.StatusCode)"
} catch {
Write-TestResult "API responds" $false $_.Exception.Message
# Try to continue anyway
}
# Test 2: CORS Preflight
Write-TestHeader "Test 2: CORS Preflight"
try {
$cors = Invoke-WebRequest -Uri "$ApiBase/conversion" -Method OPTIONS -Headers @{
"Origin" = "https://editor.nldoc.commonground.nu"
"Access-Control-Request-Method" = "POST"
"Access-Control-Request-Headers" = "content-type"
} -TimeoutSec 10 -ErrorAction SilentlyContinue
$allowOrigin = $cors.Headers["Access-Control-Allow-Origin"]
Write-TestResult "CORS preflight" ($allowOrigin -ne $null) "Allow-Origin: $allowOrigin"
} catch {
Write-TestResult "CORS preflight" $false $_.Exception.Message
}
# Test 3: File Upload (if file provided)
if ($TestFile -and (Test-Path $TestFile) -and -not $SkipUpload) {
Write-TestHeader "Test 3: File Upload"
$fileName = Split-Path $TestFile -Leaf
$fileExt = [System.IO.Path]::GetExtension($fileName).ToLower()
$fileSize = (Get-Item $TestFile).Length
Write-Host "File: $fileName ($([math]::Round($fileSize/1024, 2)) KB)" -ForegroundColor Gray
# Determine content type
$contentType = switch ($fileExt) {
".pdf" { "application/pdf" }
".docx" { "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }
default { "application/octet-stream" }
}
try {
# Create multipart form data
$boundary = [System.Guid]::NewGuid().ToString()
$fileBytes = [System.IO.File]::ReadAllBytes($TestFile)
$fileBase64 = [Convert]::ToBase64String($fileBytes)
# Use curl for multipart (more reliable)
$curlCmd = "curl -s -w '`n%{http_code}' -X POST '$ApiBase/conversion' " +
"-H 'Origin: https://editor.nldoc.commonground.nu' " +
"-H 'X-Target-Content-Type: text/html' " +
"-F 'file=@$TestFile;type=$contentType' " +
"--max-time 30"
Write-Host "Uploading..." -ForegroundColor Gray
$result = Invoke-Expression $curlCmd 2>&1
$lines = $result -split "`n"
$httpCode = $lines[-1]
$body = ($lines[0..($lines.Length-2)]) -join "`n"
if ($httpCode -eq "200") {
Write-TestResult "File upload" $true "HTTP $httpCode"
# Parse response for document ID
try {
$json = $body | ConvertFrom-Json
$docId = $json.uuid
if ($docId) {
Write-Host " Document ID: $docId" -ForegroundColor Gray
$script:DocumentId = $docId
}
} catch {
Write-Host " Response: $($body.Substring(0, [Math]::Min(200, $body.Length)))..." -ForegroundColor Gray
}
} else {
Write-TestResult "File upload" $false "HTTP $httpCode - $body"
}
} catch {
Write-TestResult "File upload" $false $_.Exception.Message
}
}
# Test 4: SSE Stream (if we have a document ID)
if ($script:DocumentId) {
Write-TestHeader "Test 4: SSE Event Stream"
try {
Write-Host "Connecting to SSE stream for $($script:DocumentId)..." -ForegroundColor Gray
Write-Host "Waiting up to 120 seconds for events..." -ForegroundColor Gray
# Use curl for SSE
$sseCmd = "curl -s -N '$ApiBase/conversion/$($script:DocumentId)' " +
"-H 'Accept: text/event-stream' " +
"-H 'Origin: https://editor.nldoc.commonground.nu' " +
"--max-time 120"
$events = @()
$startTime = Get-Date
$timeout = 120
# Run curl and capture output
$job = Start-Job -ScriptBlock {
param($cmd)
Invoke-Expression $cmd
} -ArgumentList $sseCmd
$completed = $false
$lastEvent = ""
while (((Get-Date) - $startTime).TotalSeconds -lt $timeout) {
if ($job.State -eq "Completed") {
$output = Receive-Job $job
foreach ($line in ($output -split "`n")) {
if ($line -match "^data:\s*(.+)$") {
$eventData = $Matches[1]
try {
$event = $eventData | ConvertFrom-Json
$eventType = $event.type
Write-Host " Event: $eventType" -ForegroundColor Yellow
if ($eventType -match "done|complete") {
$completed = $true
$script:OutputLocation = $event.context.location
Write-Host " Output: $($script:OutputLocation)" -ForegroundColor Green
}
if ($eventType -match "error") {
Write-Host " Error: $($event.context.code)" -ForegroundColor Red
}
} catch {}
}
}
break
}
Start-Sleep -Milliseconds 500
}
Stop-Job $job -ErrorAction SilentlyContinue
Remove-Job $job -ErrorAction SilentlyContinue
Write-TestResult "SSE stream" $completed "Completed: $completed"
} catch {
Write-TestResult "SSE stream" $false $_.Exception.Message
}
}
# Test 5: Download Output (if we have location)
if ($script:OutputLocation) {
Write-TestHeader "Test 5: Download Output"
try {
$downloadUrl = "$ApiBase/file/$($script:OutputLocation)"
Write-Host "Downloading: $downloadUrl" -ForegroundColor Gray
$outFile = "test-output-$(Get-Date -Format 'yyyyMMdd-HHmmss').html"
Invoke-WebRequest -Uri $downloadUrl -OutFile $outFile -TimeoutSec 30
$fileSize = (Get-Item $outFile).Length
$content = Get-Content $outFile -Raw
$hasHtml = $content -match "<html"
$hasBody = $content -match "<body"
Write-TestResult "Download output" $true "Size: $([math]::Round($fileSize/1024, 2)) KB"
Write-TestResult "Valid HTML structure" ($hasHtml -and $hasBody)
Write-Host "`nOutput saved to: $outFile" -ForegroundColor Green
Write-Host "First 500 chars:" -ForegroundColor Gray
Write-Host $content.Substring(0, [Math]::Min(500, $content.Length)) -ForegroundColor Gray
} catch {
Write-TestResult "Download output" $false $_.Exception.Message
}
}
# Summary
Write-TestHeader "Test Summary"
Write-Host "Tests completed at $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" -ForegroundColor Gray
if ($script:OutputLocation) {
Write-Host "`n[SUCCESS] Pipeline completed!" -ForegroundColor Green
Write-Host "Output: $ApiBase/file/$($script:OutputLocation)" -ForegroundColor Green
} else {
Write-Host "`n[INCOMPLETE] Pipeline did not complete" -ForegroundColor Yellow
Write-Host "Check logs with:" -ForegroundColor Gray
Write-Host " kubectl -n nldoc logs -l app.kubernetes.io/component=worker.document-source --tail=20" -ForegroundColor Gray
Write-Host " kubectl -n nldoc logs folio-spec-worker --tail=20" -ForegroundColor Gray
}