Treasury & Bank Transfer Management System — سامانه مدیریت خزانه‌داری و حواله‌های بانکی

1 param([Parameter(Mandatory=$true)][string]$SourceRoot)
2 $ErrorActionPreference='Stop'
3 $evidenceDir=Join-Path $env:RUNNER_TEMP 'treasuryflow-vm-evidence'
4 New-Item -ItemType Directory $evidenceDir -Force | Out-Null
5 $report=[ordered]@{installerPresent=$false;installerSha256=$null;silentInstall='NOT_RUN';
6 installedExecutableFound=$false;installedProcessLaunched=$false;httpReady=$false;
7 bankAdapterDisabled=$false;homeHtmlLoaded=$false;installSeconds=0;installTimeoutSeconds=240;installerProcessExited=$false;outcome='NOT_RUN'}
8 $target=Join-Path $env:RUNNER_TEMP 'TreasuryFlow-VM-Installed'
9 $p=$null
10 try {
11 $installers=@(Get-ChildItem -Path (Join-Path $SourceRoot 'release') -File -Filter 'TreasuryFlow-Setup-*-Windows-x64.exe')
12 if($installers.Count -ne 1){throw 'EXPECTED_EXACTLY_ONE_NSIS_SETUP_EXE'}
13 $setup=$installers[0].FullName
14 $report.installerPresent=$true
15 $report.installerSha256=(Get-FileHash $setup -Algorithm SHA256).Hash
16 if(Test-Path $target){throw 'INSTALL_TEST_DIRECTORY_ALREADY_EXISTS'}
17 # NSIS silent switch; /D=<path> must be the last argument.
18 $stopwatch=[System.Diagnostics.Stopwatch]::StartNew()
19 $setupProcess=Start-Process -FilePath $setup -ArgumentList @('/S',('/D='+$target)) -PassThru
20 # Wait only for the NSIS installer PID, not the entire inherited process tree.
21 # This follows Factor-easyPro's native Windows VM lab and has a hard timeout.
22 if(!$setupProcess.WaitForExit(240000)){
23 $report.installSeconds=[math]::Round($stopwatch.Elapsed.TotalSeconds,1)
24 $report.installedExeSeenAtTimeout=@(Get-ChildItem -LiteralPath $target -Recurse -File -Filter 'TreasuryFlow.exe' -ErrorAction SilentlyContinue).Count -gt 0
25 try{$setupProcess.Kill()}catch{}
26 throw 'NSIS_INSTALL_TIMEOUT_AFTER_240_SECONDS'
27 }
28 $stopwatch.Stop()
29 $report.installSeconds=[math]::Round($stopwatch.Elapsed.TotalSeconds,1)
30 $report.installerProcessExited=$true
31 if($setupProcess.ExitCode -ne 0){throw ('NSIS_INSTALLER_NONZERO_EXIT_'+$setupProcess.ExitCode)}
32 if(!(Test-Path $target)){throw 'NSIS_INSTALL_LOCATION_NOT_CREATED'}
33 $report.silentInstall='PASS'
34 $apps=@(Get-ChildItem $target -Recurse -File -Filter 'TreasuryFlow.exe' -ErrorAction SilentlyContinue)
35 if($apps.Count -ne 1){throw 'INSTALLED_EXE_NOT_FOUND'}
36 $report.installedExecutableFound=$true
37 $env:BANK_ADAPTER_MODE='disabled'
38 $env:TREASURYFLOW_DATA_FILE=Join-Path $evidenceDir 'isolated-installed.sqlite'
39 $p=Start-Process -FilePath $apps[0].FullName -WorkingDirectory $apps[0].DirectoryName -PassThru
40 $report.installedProcessLaunched=$true
41 for($i=0;$i -lt 90;$i++){
42 Start-Sleep -Milliseconds 700
43 try {
44 $ready=Invoke-RestMethod 'http://127.0.0.1:3210/api/ready' -TimeoutSec 2
45 if($ready.status -eq 'ready'){
46 $report.httpReady=$true
47 $report.bankAdapterDisabled=($ready.bankAdapter -eq 'disabled')
48 break
49 }
50 }catch{}
51 }
52 if(!$report.httpReady){throw 'INSTALLED_APP_READINESS_TIMEOUT'}
53 if(!$report.bankAdapterDisabled){throw 'INSTALLED_APP_BANK_NOT_DISABLED'}
54 $homePage=Invoke-WebRequest 'http://127.0.0.1:3210' -TimeoutSec 12
55 $report.homeHtmlLoaded=($homePage.StatusCode -eq 200 -and $homePage.Content -match 'TreasuryFlow')
56 if(!$report.homeHtmlLoaded){throw 'INSTALLED_APP_HTML_FAILED'}
57 $report.outcome='NSIS_INSTALL_AND_INSTALLED_RUNTIME_PASS'
58 'A REAL NSIS Setup was silently installed under runner.temp, and the installed TreasuryFlow executable passed local readiness and HTML checks.' >> $env:GITHUB_STEP_SUMMARY
59 }catch {
60 $report.outcome='FAIL'
61 $report.failureCode=$_.Exception.Message
62 throw
63 }finally {
64 if($p -and !$p.HasExited){try{& taskkill.exe /T /F /PID $p.Id | Out-Null}catch{}}
65 # NSIS and Electron can spawn an additional real app or NODE child process.
66 # The next CDP test must start from a clean disposable VM and free local port.
67 $orphans=@(Get-Process -Name 'TreasuryFlow' -ErrorAction SilentlyContinue)
68 $report.cleanupProcessCount=$orphans.Count
69 foreach($orphan in $orphans){try{Stop-Process -Id $orphan.Id -Force -ErrorAction Stop}catch{}}
70 $clear=$false
71 for($attempt=0;$attempt -lt 30;$attempt++){
72 $listeners=@(Get-NetTCPConnection -LocalPort 3210 -State Listen -ErrorAction SilentlyContinue)
73 if($listeners.Count -eq 0){$clear=$true;break}
74 Start-Sleep -Milliseconds 500
75 }
76 $report.localPort3210Freed=$clear
77 if(!$clear){$report.outcome='FAIL_CLEANUP_PORT_OCCUPIED'}
78 $report|ConvertTo-Json -Depth 4|Set-Content (Join-Path $evidenceDir 'installed-runtime.json') -Encoding UTF8
79 if(!$clear){throw 'INSTALLED_SMOKE_LEFT_PORT_3210_OCCUPIED'}
80 }