跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

test-windows-install.ps1

115 行 · 7.5 KB历史下载
1#requires -Version 7.0
2# Runs only on a disposable Windows test user. Does not start a VM.
3[CmdletBinding()]
4param([Parameter(Mandatory)][string]$Installer)
5. (Join-Path $PSScriptRoot 'windows-common.ps1')
6if (-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'
9if (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')
12Invoke-BuildTool 'node' @($fixture, 'seed', $env:USERPROFILE, $manifest)
13function Assert-PersonalData {
14 Invoke-BuildTool 'node' @($fixture, 'verify', $env:USERPROFILE, $manifest)
15}
16function 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'
41function 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.
53New-Item -ItemType Directory -Path $target | Out-Null
54Run-Installer
55Assert-PersonalData
56Assert-Shortcuts $true $true
57if (-not (Test-Path -LiteralPath (Join-Path $target 'Cage.exe'))) { throw 'Installer did not create Cage.exe.' }
58foreach ($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 = @'
64import { pathToFileURL } from 'node:url';
65import { join } from 'node:path';
66for (const file of ['windows-phone-browser.mjs', 'modules/browser-phone.mjs'])
67 await import(pathToFileURL(join(process.argv[1], file)));
68'@
69Invoke-BuildTool 'node' @('--input-type=module', '--eval', $phoneImports, (Join-Path $target 'resources/guest-os/payload'))
70Invoke-BuildTool (Join-Path $target 'resources/cage.exe') @('--help')
71Invoke-BuildTool (Join-Path $target 'resources/cage.exe') @('verify-vm-assets')
72Invoke-BuildTool (Join-Path $target 'resources/cage.exe') @('verify-web-assets')
73Invoke-BuildTool (Join-Path $target 'resources/mihomo.exe') @('-v')
74Invoke-BuildTool (Join-Path $target 'resources/runtime/qemu/qemu-system-x86_64.exe') @('--version')
75Invoke-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.
78Assert-WindowsWorkbench -AppDirectory $target
79Assert-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)
83try { Run-Installer -Update -ExpectedExitCode 2 } finally { $locked.Dispose() }
84Assert-PersonalData
85if ((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.
87Run-Installer -Update
88Assert-PersonalData
89Assert-Shortcuts $true $true
90Run-Installer -Update -ShortcutOptions @('/DESKTOPSHORTCUT=0', '/STARTMENUSHORTCUT=0')
91Assert-Shortcuts $false $false
92Run-Installer -Update
93Assert-Shortcuts $false $false
94Run-Installer -Update -ShortcutOptions @('/DESKTOPSHORTCUT=1', '/STARTMENUSHORTCUT=0')
95Assert-Shortcuts $true $false
96Run-Installer -Update -ShortcutOptions @('/DESKTOPSHORTCUT=0', '/STARTMENUSHORTCUT=1')
97Assert-Shortcuts $false $true
98Run-Installer -Update -ShortcutOptions @('/DESKTOPSHORTCUT=1', '/STARTMENUSHORTCUT=1')
99Assert-Shortcuts $true $true
100Assert-PersonalData
101$uninstall = Start-Process -FilePath (Join-Path $target 'Uninstall.exe') -ArgumentList @('/S') -PassThru
102if (-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)
106while (((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 }
109if (Test-Path -LiteralPath (Join-Path $target 'Cage.exe')) { throw 'Uninstall left Cage.exe behind.' }
110if (Test-Path -LiteralPath (Join-Path $target 'Uninstall.exe')) { throw 'Uninstall left its launcher behind.' }
111if (Test-Path -LiteralPath $target) { throw 'Uninstall left packaged files or directories that would block installation into the same folder.' }
112if (Test-Path $savedKey) { throw 'Uninstall left the Cage registration behind.' }
113Assert-PersonalData
114Assert-Shortcuts $false $false
115Write-Host 'Install, WebView2 workbench, reinstall, uninstall and user-data preservation passed. VM startup was not tested.'