| 1 | #requires -Version 7.0 |
| 2 | # Runs only on a disposable Windows test user. Does not start a VM. |
| 3 | [CmdletBinding()] |
| 4 | param([Parameter(Mandatory)][string]$Installer) |
| 5 | . (Join-Path $PSScriptRoot 'windows-common.ps1') |
| 6 | if (-not $IsWindows) { throw 'Installation smoke tests must run on Windows.' } |
| 7 | $target = Join-Path $env:LOCALAPPDATA ('Programs/Cage-test-' + [guid]::NewGuid().ToString('N')) |
| 8 | $savedKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\ClaudeCage' |
| 9 | if (Test-Path $savedKey) { throw 'Use a disposable Windows user without an existing Cage installation.' } |
| 10 | $fixture = Join-Path $PSScriptRoot 'personal-data-fixture.mjs' |
| 11 | $manifest = Join-Path ([IO.Path]::GetTempPath()) ('cage-personal-data-' + [guid]::NewGuid().ToString('N') + '.json') |
| 12 | Invoke-BuildTool 'node' @($fixture, 'seed', $env:USERPROFILE, $manifest) |
| 13 | function Assert-PersonalData { |
| 14 | Invoke-BuildTool 'node' @($fixture, 'verify', $env:USERPROFILE, $manifest) |
| 15 | } |
| 16 | function Run-Installer { |
| 17 | param([switch]$Update, [int]$ExpectedExitCode = 0, [string[]]$ShortcutOptions = @()) |
| 18 | $installationReport = Join-Path (Get-WindowsWorkbenchTestBase) ('cage-smoke-installation-' + [guid]::NewGuid().ToString('N') + '.json') |
| 19 | $arguments = @('/S', "/DIAGNOSTICS=$installationReport") |
| 20 | if ($Update) { $arguments += '/UPDATE' } |
| 21 | $arguments += $ShortcutOptions |
| 22 | $arguments += "/D=$target" |
| 23 | $process = Start-Process -FilePath $Installer -ArgumentList $arguments -PassThru |
| 24 | if (-not $process.WaitForExit(300000)) { throw 'Installer exceeded five minutes.' } |
| 25 | if ($process.ExitCode -ne $ExpectedExitCode) { |
| 26 | foreach ($path in @($installationReport, "$installationReport.helper.json")) { |
| 27 | try { |
| 28 | if ((Test-Path -LiteralPath $path -PathType Leaf) -and (Get-Item -LiteralPath $path).Length -le 8192) { |
| 29 | $value = Get-Content -LiteralPath $path -Raw | ConvertFrom-Json -AsHashtable |
| 30 | $safe = @{phase = $value['phase']; status = $value['status']; helperExit = $value['helperExit']; operation = $value['operation']; pointerSize = $value['pointerSize']} |
| 31 | if ($value['errors']) { $safe.errors = @($value['errors'] | Select-Object -First 4 | ForEach-Object { @{type = $_['type']; hresult = $_['hresult']} }) } |
| 32 | Write-Host ('Cage installer diagnostic: ' + ($safe | ConvertTo-Json -Depth 4 -Compress)) |
| 33 | } |
| 34 | } catch { Write-Warning 'The bounded installer diagnostic could not be read.' } |
| 35 | } |
| 36 | throw "Installer returned $($process.ExitCode), expected $ExpectedExitCode" |
| 37 | } |
| 38 | } |
| 39 | $desktopShortcut = Join-Path ([Environment]::GetFolderPath('DesktopDirectory')) 'Cage.lnk' |
| 40 | $startMenuDirectory = Join-Path ([Environment]::GetFolderPath('Programs')) 'Cage' |
| 41 | function Assert-Shortcuts { |
| 42 | param([bool]$Desktop, [bool]$StartMenu) |
| 43 | if ((Test-Path -LiteralPath $desktopShortcut) -ne $Desktop) { throw 'Desktop shortcut differs from the installer selection.' } |
| 44 | foreach ($name in @('Cage.lnk', 'Uninstall Cage.lnk')) { |
| 45 | if ((Test-Path -LiteralPath (Join-Path $startMenuDirectory $name)) -ne $StartMenu) { throw "Start menu shortcut differs from the installer selection: $name" } |
| 46 | } |
| 47 | if ($Desktop) { |
| 48 | $shell = New-Object -ComObject WScript.Shell |
| 49 | if ($shell.CreateShortcut($desktopShortcut).TargetPath -ne (Join-Path $target 'Cage.exe')) { throw 'Desktop shortcut targets a different installation.' } |
| 50 | } |
| 51 | } |
| 52 | # Cover the directory-picker path where the user creates an empty folder first. |
| 53 | New-Item -ItemType Directory -Path $target | Out-Null |
| 54 | Run-Installer |
| 55 | Assert-PersonalData |
| 56 | Assert-Shortcuts $true $true |
| 57 | if (-not (Test-Path -LiteralPath (Join-Path $target 'Cage.exe'))) { throw 'Installer did not create Cage.exe.' } |
| 58 | foreach ($file in @('new-windows-guest.ps1', 'windows-guest-media.ps1', 'install-guest-powershell.cmd', 'publish-windows-component-manifest.ps1')) { |
| 59 | if (!(Test-Path -LiteralPath (Join-Path $target "resources/guest-os/$file") -PathType Leaf)) { |
| 60 | throw "Installed app is missing a Windows guest setup file: $file" |
| 61 | } |
| 62 | } |
| 63 | $phoneImports = @' |
| 64 | import { pathToFileURL } from 'node:url'; |
| 65 | import { join } from 'node:path'; |
| 66 | for (const file of ['windows-phone-browser.mjs', 'modules/browser-phone.mjs']) |
| 67 | await import(pathToFileURL(join(process.argv[1], file))); |
| 68 | '@ |
| 69 | Invoke-BuildTool 'node' @('--input-type=module', '--eval', $phoneImports, (Join-Path $target 'resources/guest-os/payload')) |
| 70 | Invoke-BuildTool (Join-Path $target 'resources/cage.exe') @('--help') |
| 71 | Invoke-BuildTool (Join-Path $target 'resources/cage.exe') @('verify-vm-assets') |
| 72 | Invoke-BuildTool (Join-Path $target 'resources/cage.exe') @('verify-web-assets') |
| 73 | Invoke-BuildTool (Join-Path $target 'resources/mihomo.exe') @('-v') |
| 74 | Invoke-BuildTool (Join-Path $target 'resources/runtime/qemu/qemu-system-x86_64.exe') @('--version') |
| 75 | Invoke-BuildTool (Join-Path $target 'resources/runtime/openssh/ssh.exe') @('-V') |
| 76 | # Use the same isolated profile, process lifetime and retained diagnostics as |
| 77 | # build acceptance. The pre-existing default profile holds preservation markers. |
| 78 | Assert-WindowsWorkbench -AppDirectory $target |
| 79 | Assert-PersonalData |
| 80 | # A locked installed executable must fail before replacing the installation. |
| 81 | $before = (Get-FileHash -LiteralPath (Join-Path $target 'Cage.exe') -Algorithm SHA256).Hash |
| 82 | $locked = [IO.File]::Open((Join-Path $target 'Cage.exe'), [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read) |
| 83 | try { Run-Installer -Update -ExpectedExitCode 2 } finally { $locked.Dispose() } |
| 84 | Assert-PersonalData |
| 85 | if ((Get-FileHash -LiteralPath (Join-Path $target 'Cage.exe') -Algorithm SHA256).Hash -ne $before) { throw 'Failed update replaced the existing application.' } |
| 86 | # Reinstall through the real atomic replacement path. User data must survive. |
| 87 | Run-Installer -Update |
| 88 | Assert-PersonalData |
| 89 | Assert-Shortcuts $true $true |
| 90 | Run-Installer -Update -ShortcutOptions @('/DESKTOPSHORTCUT=0', '/STARTMENUSHORTCUT=0') |
| 91 | Assert-Shortcuts $false $false |
| 92 | Run-Installer -Update |
| 93 | Assert-Shortcuts $false $false |
| 94 | Run-Installer -Update -ShortcutOptions @('/DESKTOPSHORTCUT=1', '/STARTMENUSHORTCUT=0') |
| 95 | Assert-Shortcuts $true $false |
| 96 | Run-Installer -Update -ShortcutOptions @('/DESKTOPSHORTCUT=0', '/STARTMENUSHORTCUT=1') |
| 97 | Assert-Shortcuts $false $true |
| 98 | Run-Installer -Update -ShortcutOptions @('/DESKTOPSHORTCUT=1', '/STARTMENUSHORTCUT=1') |
| 99 | Assert-Shortcuts $true $true |
| 100 | Assert-PersonalData |
| 101 | $uninstall = Start-Process -FilePath (Join-Path $target 'Uninstall.exe') -ArgumentList @('/S') -PassThru |
| 102 | if (-not $uninstall.WaitForExit(120000) -or $uninstall.ExitCode -ne 0) { throw 'Uninstall failed.' } |
| 103 | # NSIS may relaunch its uninstaller from TEMP so that it can delete the original. |
| 104 | # The launcher can disappear before the worker clears registration and shortcuts. |
| 105 | $deadline = [DateTime]::UtcNow.AddSeconds(120) |
| 106 | while (((Test-Path -LiteralPath $target) -or (Test-Path $savedKey) -or |
| 107 | (Test-Path -LiteralPath $desktopShortcut) -or (Test-Path -LiteralPath $startMenuDirectory)) -and |
| 108 | [DateTime]::UtcNow -lt $deadline) { Start-Sleep -Milliseconds 200 } |
| 109 | if (Test-Path -LiteralPath (Join-Path $target 'Cage.exe')) { throw 'Uninstall left Cage.exe behind.' } |
| 110 | if (Test-Path -LiteralPath (Join-Path $target 'Uninstall.exe')) { throw 'Uninstall left its launcher behind.' } |
| 111 | if (Test-Path -LiteralPath $target) { throw 'Uninstall left packaged files or directories that would block installation into the same folder.' } |
| 112 | if (Test-Path $savedKey) { throw 'Uninstall left the Cage registration behind.' } |
| 113 | Assert-PersonalData |
| 114 | Assert-Shortcuts $false $false |
| 115 | Write-Host 'Install, WebView2 workbench, reinstall, uninstall and user-data preservation passed. VM startup was not tested.' |