Files
Academic-Affairs-System/scripts/smoke-test.ps1
T
2026-07-24 13:11:09 +08:00

100 lines
3.6 KiB
PowerShell

param(
[string] $ApiExecutablePath,
[string] $ApiContentRoot
)
$ErrorActionPreference = 'Stop'
$workspaceRoot = Split-Path -Parent $PSScriptRoot
$apiExecutable = if ([string]::IsNullOrWhiteSpace($ApiExecutablePath)) {
Join-Path $workspaceRoot 'src/Jiaowu.Api/bin/Debug/net10.0/Jiaowu.Api.exe'
}
else {
[System.IO.Path]::GetFullPath($ApiExecutablePath, $workspaceRoot)
}
$apiWorkingDirectory = if ([string]::IsNullOrWhiteSpace($ApiContentRoot)) {
Join-Path $workspaceRoot 'src/Jiaowu.Api'
}
else {
[System.IO.Path]::GetFullPath($ApiContentRoot, $workspaceRoot)
}
$stdoutPath = Join-Path $env:TEMP 'jiaowu-api-smoke.out.log'
$stderrPath = Join-Path $env:TEMP 'jiaowu-api-smoke.err.log'
$env:ASPNETCORE_ENVIRONMENT = 'Development'
$env:ASPNETCORE_URLS = 'http://localhost:5255'
$startParameters = @{
FilePath = $apiExecutable
WorkingDirectory = $apiWorkingDirectory
WindowStyle = 'Hidden'
RedirectStandardOutput = $stdoutPath
RedirectStandardError = $stderrPath
PassThru = $true
}
$process = Start-Process @startParameters
try {
$healthy = $false
for ($attempt = 0; $attempt -lt 30; $attempt++) {
Start-Sleep -Milliseconds 500
try {
$health = Invoke-RestMethod -Uri 'http://localhost:5255/health' -TimeoutSec 2
$healthy = $true
break
}
catch {
# The API may still be starting.
}
}
if (-not $healthy) {
Get-Content -LiteralPath $stdoutPath -ErrorAction SilentlyContinue
Get-Content -LiteralPath $stderrPath -ErrorAction SilentlyContinue
throw 'API did not become healthy.'
}
$loginBody = @{
userName = 'admin'
password = 'Admin@123456'
} | ConvertTo-Json
$loginParameters = @{
Method = 'Post'
Uri = 'http://localhost:5255/api/auth/login'
ContentType = 'application/json'
Body = $loginBody
}
$login = Invoke-RestMethod @loginParameters
$headers = @{ Authorization = "Bearer $($login.token)" }
$dashboard = Invoke-RestMethod -Uri 'http://localhost:5255/api/dashboard' -Headers $headers
$campuses = Invoke-RestMethod -Uri 'http://localhost:5255/api/base-data/campuses' -Headers $headers
$teachers = Invoke-RestMethod -Uri 'http://localhost:5255/api/personnel/teachers?page=1&pageSize=10' -Headers $headers
$students = Invoke-RestMethod -Uri 'http://localhost:5255/api/personnel/students?page=1&pageSize=10' -Headers $headers
$courses = Invoke-RestMethod -Uri 'http://localhost:5255/api/courses?page=1&pageSize=10' -Headers $headers
$frontend = Invoke-WebRequest -Uri 'http://localhost:5255/' -TimeoutSec 5
$spaFallback = Invoke-WebRequest -Uri 'http://localhost:5255/base-data' -TimeoutSec 5
$unknownApiParameters = @{
Uri = 'http://localhost:5255/api/does-not-exist'
SkipHttpErrorCheck = $true
TimeoutSec = 5
}
$unknownApi = Invoke-WebRequest @unknownApiParameters
[pscustomobject]@{
Health = $health.status
Database = $health.database
User = $login.user.displayName
Term = $dashboard.currentTerm.name
Campuses = @($campuses).Count
Teachers = $teachers.total
Students = $students.total
Courses = $courses.total
StaticIndex = $frontend.Content.Contains('明序教务管理系统')
SpaFallback = $spaFallback.StatusCode
ApiNotFound = $unknownApi.StatusCode
} | Format-List
}
finally {
if (-not $process.HasExited) {
Stop-Process -Id $process.Id -Force
}
}