-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathvalidate-sample.ps1
More file actions
112 lines (94 loc) · 4.78 KB
/
Copy pathvalidate-sample.ps1
File metadata and controls
112 lines (94 loc) · 4.78 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
param(
[switch]$SkipInstall,
[switch]$SkipTests,
[switch]$SkipBrowser,
[switch]$KeepProcesses,
[switch]$Headed,
[int]$TimeoutSec = 120
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
. (Join-Path $PSScriptRoot '../../Tools/powershell/SampleValidation.ps1')
$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '../..')).Path
$toolRoot = Join-Path $repoRoot 'Tools/sample-validation'
$appRoot = $PSScriptRoot
$envFile = Join-Path $appRoot '.env'
$constantsFile = Join-Path $appRoot 'src/common/constants.ts'
$nodeEnvironment = Get-ValidationNodeEnvironment
$handles = @()
function Test-OcrFrontendConfigured {
param(
[Parameter(Mandatory = $true)]
[string]$ConstantsPath
)
if (-not (Test-Path $ConstantsPath)) {
return $false
}
$content = Get-Content -Path $ConstantsPath -Raw
$clientIdMatch = [regex]::Match($content, "CLIENT_ENTRA_APP_CLIENT_ID\s*=\s*'([^']*)'")
if (-not $clientIdMatch.Success) {
return $false
}
return -not [string]::IsNullOrWhiteSpace($clientIdMatch.Groups[1].Value)
}
try {
Write-Step 'Preflight checks'
Assert-CommandExists 'node'
Assert-CommandExists 'npm'
if (-not $SkipInstall) {
Write-Step 'Installing dependencies'
Invoke-ExternalCommand -FilePath 'npm' -Arguments @('install', '--legacy-peer-deps') -WorkingDirectory $appRoot -Environment $nodeEnvironment
}
Write-Step 'Building backend'
Invoke-ExternalCommand -FilePath 'npm' -Arguments @('run', 'build:backend') -WorkingDirectory $appRoot -Environment $nodeEnvironment
Write-Step 'Building frontend'
Invoke-ExternalCommand -FilePath 'npm' -Arguments @('run', 'build-cre') -WorkingDirectory $appRoot -Environment $nodeEnvironment
if ($SkipTests) {
Write-Host 'Skipping frontend tests because -SkipTests was specified.' -ForegroundColor Yellow
}
else {
Write-Step 'Running frontend tests'
Invoke-ExternalCommand -FilePath 'npm' -Arguments @('run', 'test-cre', '--', '--watchAll=false') -WorkingDirectory $appRoot -Environment (Merge-EnvironmentTables @($nodeEnvironment, @{ CI = 'true' }))
}
if (-not (Test-Path $envFile)) {
Write-Host 'Skipping runtime smoke checks because .env is missing.' -ForegroundColor Yellow
Write-ValidationSummary -Status 'SKIP_CONFIG' -Message 'Build and tests passed; runtime smoke skipped because .env is missing.'
return
}
Write-Step 'Starting backend'
$backendLog = New-ValidationLogPath -WorkingDirectory $appRoot -Name 'ocr-backend'
$backendHandle = Start-LoggedProcess -FilePath 'npm' -Arguments @('run', 'start:backend') -WorkingDirectory $appRoot -LogPath $backendLog -Environment (Merge-EnvironmentTables @($nodeEnvironment, @{ PORT = '3001' }))
$handles += $backendHandle
[void](Wait-ForHttpEndpoint -Url 'http://127.0.0.1:3001/api/echo' -TimeoutSec $TimeoutSec -AllowedStatusCodes @(200) -ProcessHandle $backendHandle)
if (-not (Test-OcrFrontendConfigured -ConstantsPath $constantsFile)) {
Write-Host 'Skipping frontend runtime smoke because src/common/constants.ts does not contain a configured CLIENT_ENTRA_APP_CLIENT_ID.' -ForegroundColor Yellow
Write-ValidationSummary -Status 'SKIP_CONFIG' -Message 'Build, tests, and backend smoke passed; frontend runtime smoke skipped because CLIENT_ENTRA_APP_CLIENT_ID is not configured.'
}
else {
Write-Step 'Starting frontend'
$frontendLog = New-ValidationLogPath -WorkingDirectory $appRoot -Name 'ocr-frontend'
$frontendHandle = Start-LoggedProcess -FilePath 'npm' -Arguments @('run', 'start-cre') -WorkingDirectory $appRoot -LogPath $frontendLog -Environment (Merge-EnvironmentTables @($nodeEnvironment, @{ BROWSER = 'none'; PORT = '3102' }))
$handles += $frontendHandle
[void](Wait-ForHttpEndpoint -Url 'http://127.0.0.1:3102' -TimeoutSec $TimeoutSec -AllowedStatusCodes @(200) -ProcessHandle $frontendHandle)
if ($SkipBrowser) {
Write-Host 'Skipping browser smoke because -SkipBrowser was specified.' -ForegroundColor Yellow
}
else {
Write-Step 'Running browser smoke'
Invoke-BrowserSmoke -ToolRoot $toolRoot -Url 'http://127.0.0.1:3102' -SkipInstall:$SkipInstall -Headed:$Headed -TimeoutSec $TimeoutSec -ExpectSelector '#root'
}
Write-ValidationSummary -Status 'PASS' -Message 'Build, tests, backend smoke, and frontend runtime smoke checks passed.'
}
Write-Host 'OCR sample validation completed.' -ForegroundColor Green
}
catch {
Write-ValidationSummary -Status 'FAIL' -Message $_.Exception.Message
throw
}
finally {
if (-not $KeepProcesses) {
foreach ($handle in ($handles | Sort-Object -Descending -Property LogPath)) {
Stop-LoggedProcess -Handle $handle
}
}
}