跳到主要内容

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

浏览文件

test-windows-installation-files.ps1

344 行 · 26.3 KB历史下载
1#requires -Version 5.1
2param([string]$HelperShell = (Get-Process -Id $PID).Path, [switch]$InstallerSmoke)
3$ErrorActionPreference = 'Stop'
4$helper = Join-Path $PSScriptRoot '../packaging/windows/installation-files.ps1'
5$root = Join-Path ([IO.Path]::GetTempPath()) ('cage-install-files-' + [guid]::NewGuid().ToString('N'))
6$checks = 0
7$windows = $env:OS -eq 'Windows_NT'
8function Check([bool]$Value, [string]$Message) {
9 if (!$Value) { throw $Message }
10 $script:checks++
11}
12function Write-File([string]$Path, [string]$Value) {
13 [IO.Directory]::CreateDirectory((Split-Path $Path -Parent)) | Out-Null
14 [IO.File]::WriteAllText($Path, $Value)
15}
16function New-FileLink([string]$Path, [string]$Target) {
17 if ($windows) {
18 & cmd.exe /c mklink $Path.Replace('/', '\') $Target.Replace('/', '\') | Out-Null
19 if ($LASTEXITCODE -ne 0) { throw 'Could not create the isolated file link fixture.' }
20 } else { [IO.File]::CreateSymbolicLink($Path, $Target) | Out-Null }
21}
22function Read-Link([string]$Path) {
23 $entry = Get-Item -LiteralPath $Path -Force
24 if ($windows) { return ([string]@($entry.Target)[0]).Replace('\', '/') }
25 return $entry.LinkTarget
26}
27function Remove-TestTree([string]$Path) {
28 foreach ($entry in Get-ChildItem -LiteralPath $Path -Force) {
29 if (!$windows -and $entry.Attributes -band [IO.FileAttributes]::ReparsePoint) {
30 [IO.File]::Delete($entry.FullName)
31 } elseif ($entry.Attributes -band [IO.FileAttributes]::Directory) {
32 if (!($entry.Attributes -band [IO.FileAttributes]::ReparsePoint)) { Remove-TestTree $entry.FullName }
33 [IO.Directory]::Delete($entry.FullName)
34 } else { [IO.File]::Delete($entry.FullName) }
35 }
36}
37function App([string]$Path, [string]$Version) {
38 Write-File (Join-Path $Path 'Cage.install-id') 'io.github.lee-xeo.claude-cage'
39 Write-File (Join-Path $Path 'program.txt') $Version
40 Write-File (Join-Path $Path 'resources/customizable.txt') ('default-' + $Version)
41 Write-File (Join-Path $Path 'Uninstall.exe') ('uninstaller-' + $Version)
42 $files = @(Get-ChildItem -LiteralPath $Path -File -Recurse | ForEach-Object {
43 @{path = $_.FullName.Substring($Path.TrimEnd('\','/').Length + 1).Replace('\', '/'); sha256 = (Get-FileHash $_.FullName).Hash.ToLowerInvariant()}
44 })
45 @{version = 1; files = $files} | ConvertTo-Json -Depth 4 | Set-Content (Join-Path $Path 'Cage.install-files.json')
46}
47function Invoke-Helper([string[]]$Arguments, [int]$Expected = 0, [switch]$WithoutFileHash) {
48 $entry = $helper
49 if ($WithoutFileHash) {
50 $entry = Join-Path $root 'helper-without-filehash.ps1'
51 Write-File $entry @'
52$ErrorActionPreference = 'Stop'
53function global:Get-FileHash { throw [Management.Automation.CommandNotFoundException]::new('Get-FileHash is intentionally unavailable in this native helper fixture.') }
54$remaining = @($args | Select-Object -Skip 1)
55& $args[0] @remaining
56'@
57 $Arguments = @($helper) + $Arguments
58 }
59 try {
60 $ErrorActionPreference = 'Continue'
61 $output = & $HelperShell -NoProfile -File $entry @Arguments 2>&1 | Out-String
62 } finally { $ErrorActionPreference = 'Stop' }
63 if ($LASTEXITCODE -ne $Expected) { throw "Helper returned $LASTEXITCODE, expected $Expected. $output" }
64}
65function Prepare([string]$Old, [string]$Stage, [string]$Receipt) {
66 Invoke-Helper @('-Operation','prepare','-Source',$Old,'-Target',$Stage,'-Receipt',$Receipt,'-Backup',($Old+'-backup'))
67}
68function Commit([string]$Old, [string]$Stage, [string]$Receipt) {
69 $backup = $Old + '-backup'
70 [IO.Directory]::Move($Old, $backup)
71 Invoke-Helper @('-Operation','move','-Source',$backup,'-Target',$Stage,'-Receipt',$Receipt)
72 [IO.Directory]::Move($Stage, $Old)
73 Invoke-Helper @('-Operation','cleanup','-Source',$backup,'-Target',$Old,'-Receipt',$Receipt)
74 return $backup
75}
76function Test-InstallerTransaction {
77 if (!$windows -or $PSVersionTable.PSVersion.Major -lt 7) { throw 'The optional NSIS fixture requires PowerShell 7 on a disposable Windows user.' }
78 $key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\ClaudeCage'
79 $desktop = Join-Path ([Environment]::GetFolderPath('DesktopDirectory')) 'Cage.lnk'
80 $menu = Join-Path ([Environment]::GetFolderPath('Programs')) 'Cage'
81 if ((Test-Path $key) -or (Test-Path -LiteralPath $desktop) -or (Test-Path -LiteralPath $menu)) { throw 'The NSIS fixture refuses an existing Cage registration or shortcuts.' }
82 $runtime = $false
83 foreach ($hive in @([Microsoft.Win32.RegistryHive]::LocalMachine, [Microsoft.Win32.RegistryHive]::CurrentUser)) {
84 $base = [Microsoft.Win32.RegistryKey]::OpenBaseKey($hive, [Microsoft.Win32.RegistryView]::Registry32)
85 $client = $base.OpenSubKey('SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}')
86 try { if ($client -and [string]$client.GetValue('pv') -notin @('','0.0.0.0')) { $runtime = $true } }
87 finally { if ($client) { $client.Dispose() }; $base.Dispose() }
88 }
89 if (!$runtime) { Write-Output 'SKIP: quick NSIS transaction needs an existing WebView2 runtime; full installer acceptance is still required.'; return }
90 $scenario = Join-Path $root 'nsis-transaction'; $app = Join-Path $scenario 'payload'; $target = Join-Path $scenario 'empty target'
91 $setup = Join-Path $scenario 'fixture-setup.exe'
92 $reportBase = if ($env:RUNNER_TEMP) { $env:RUNNER_TEMP } else { [IO.Path]::GetTempPath() }
93 $report = Join-Path $reportBase ('cage-smoke-installation-fixture-' + [guid]::NewGuid().ToString('N') + '.json')
94 Write-File (Join-Path $app 'LICENSE.txt') 'Compile-only text payload; no Cage application or VM.'
95 App $app 'fixture'
96 $version = (Get-Content (Join-Path $PSScriptRoot '../VERSION') -Raw).Trim()
97 & (Get-Command makensis.exe -ErrorAction Stop).Source /V2 /INPUTCHARSET UTF8 "/DAPP_DIR=$app" "/DVERSION=$version" "/DVERSION_NUMERIC=$(($version -split '-')[0]).0" "/DFILE_MANIFEST=$app\Cage.install-files.json" "/DOUTPUT_FILE=$setup" (Join-Path $PSScriptRoot '../packaging/windows/Cage.nsi')
98 if ($LASTEXITCODE -ne 0) { throw 'The production NSIS fixture did not compile.' }
99 function Run-Fixture([string]$Executable, [string[]]$Arguments) {
100 $process = Start-Process -FilePath $Executable -ArgumentList $Arguments -PassThru
101 try {
102 if (!$process.WaitForExit(30000)) { $process.Kill($true); $process.WaitForExit(); throw 'The owned NSIS fixture exceeded 30 seconds.' }
103 if ($process.ExitCode -ne 0) {
104 foreach ($path in @($report, "$report.helper.json", "$report.uninstall.json", "$report.uninstall.json.helper.json")) {
105 if ((Test-Path -LiteralPath $path) -and (Get-Item -LiteralPath $path).Length -le 8192) { Write-Output ('Quick NSIS diagnostic: ' + (Get-Content -LiteralPath $path -Raw)) }
106 }
107 throw "The NSIS fixture returned $($process.ExitCode)."
108 }
109 } finally { $process.Dispose() }
110 }
111 try {
112 [IO.Directory]::CreateDirectory($target) | Out-Null
113 Run-Fixture $setup @('/S','/DESKTOPSHORTCUT=0','/STARTMENUSHORTCUT=0',"/DIAGNOSTICS=`"$report`"","/D=$target")
114 Check ((Get-Content (Join-Path $target 'program.txt') -Raw) -eq 'fixture') 'NSIS did not publish its text payload.'
115 Check (!(Test-Path -LiteralPath ($target + '.cage-installation.json'))) 'NSIS left the first-install receipt unfinished.'
116 Check ((Get-ItemProperty $key).InstallLocation -eq $target) 'NSIS registered a different fixture target.'
117 } finally {
118 if (Test-Path $key) {
119 if ((Get-ItemProperty $key).InstallLocation -ne $target) { throw 'NSIS fixture registration changed owner; retained for inspection.' }
120 Run-Fixture (Join-Path $target 'Uninstall.exe') @('/S',('/DIAGNOSTICS="' + $report + '.uninstall.json"'))
121 # NSIS can return from its bootstrapper while the TEMP worker is
122 # still running. Its helper removes the target before NSIS clears
123 # registration and shortcuts, so target disappearance is not done.
124 $deadline = [DateTime]::UtcNow.AddSeconds(120)
125 while (((Test-Path -LiteralPath $target) -or (Test-Path $key) -or (Test-Path -LiteralPath $desktop) -or (Test-Path -LiteralPath $menu)) -and [DateTime]::UtcNow -lt $deadline) { Start-Sleep -Milliseconds 100 }
126 }
127 }
128 $remaining = @{target = (Test-Path -LiteralPath $target); registration = (Test-Path $key); desktopShortcut = (Test-Path -LiteralPath $desktop); startMenu = (Test-Path -LiteralPath $menu)}
129 if ($remaining.Values -contains $true) {
130 foreach ($path in @("$report.uninstall.json", "$report.uninstall.json.helper.json")) {
131 if ((Test-Path -LiteralPath $path) -and (Get-Item -LiteralPath $path).Length -le 8192) { Write-Output ('Quick NSIS uninstall diagnostic: ' + (Get-Content -LiteralPath $path -Raw)) }
132 }
133 }
134 Check (!($remaining.Values -contains $true)) ('NSIS fixture did not finish uninstalling owned artifacts: ' + ($remaining | ConvertTo-Json -Compress))
135 Write-Output 'PASS: production NSIS silently installed/uninstalled its text-only fixture through PowerShell 5.1 transactions; no application or VM was started.'
136}
137try {
138 if ($InstallerSmoke) { Test-InstallerTransaction }
139 # A new install has no backup path or existing ownership manifest.
140 $scenario = Join-Path $root 'first-install'
141 $old = Join-Path $scenario 'empty-destination'; $stage = Join-Path $scenario 'staged'; $receipt = Join-Path $scenario 'receipt.json'
142 [IO.Directory]::CreateDirectory($old) | Out-Null
143 App $stage 'fresh'
144 Invoke-Helper @('-Operation','prepare','-Source',$old,'-Target',$stage,'-Receipt',$receipt)
145 $transfer = Get-Content -LiteralPath $receipt -Raw | ConvertFrom-Json
146 Check (!$transfer.backupPath -and @($transfer.moves).Count -eq 0) 'First install invented a backup or personal file transfer.'
147 [IO.Directory]::Delete($old); [IO.Directory]::Move($stage, $old)
148 Invoke-Helper @('-Operation','complete','-Target',$old,'-Receipt',$receipt) -WithoutFileHash
149 Check (!(Test-Path -LiteralPath $receipt)) 'First install did not finish its transaction.'
150
151 $diagnostic = Join-Path $scenario 'diagnostic.json'
152 Write-File (Join-Path $old 'Cage.install-files.json') 'invalid-private-manifest'
153 Invoke-Helper @('-Operation','prepare','-Target',$old,'-Receipt',$receipt,'-Diagnostics',$diagnostic) 1
154 $text = Get-Content -LiteralPath $diagnostic -Raw
155 $failure = $text | ConvertFrom-Json
156 Check ($failure.operation -eq 'prepare' -and $failure.phase -eq 'reading_new_manifest' -and @($failure.errors).Count -gt 0) 'Helper omitted its safe failure classification.'
157 Check (!$text.Contains($root) -and !$text.Contains('invalid-private-manifest') -and !$text.Contains('message')) 'Helper diagnostic contains file paths or private error text.'
158
159 foreach ($manifestState in @('current','missing','invalid')) {
160 $scenario = Join-Path $root $manifestState
161 $old = Join-Path $scenario 'installed'; $stage = Join-Path $scenario 'staged'; $receipt = Join-Path $scenario 'receipt.json'
162 App $old 'old'; App $stage 'new'
163 $manifest = Join-Path $old 'Cage.install-files.json'
164 if ($manifestState -eq 'missing') { Remove-Item $manifest }
165 if ($manifestState -eq 'invalid') { Write-File $manifest '{broken' }
166 Write-File (Join-Path $old 'my-project/work.txt') 'project data'
167 Write-File (Join-Path $old 'resources/customizable.txt') 'user customization'
168 Write-File (Join-Path $scenario 'outside/config.txt') 'outside data'
169 [IO.Directory]::CreateDirectory((Join-Path $old 'empty-project')) | Out-Null
170 foreach ($file in @('state/guest-media/system.img','state/lima/instance/disk','cache/lima/image')) {
171 Write-File (Join-Path $old ('CageData/' + $file)) ('persistent-' + $file)
172 }
173 Prepare $old $stage $receipt
174 Check ((Get-Content (Join-Path $old 'program.txt') -Raw) -eq 'old') 'Preparation modified the original installation.'
175 $backup = Commit $old $stage $receipt
176 Check ((Get-Content (Join-Path $old 'program.txt') -Raw) -eq 'new') 'New program was overwritten by legacy files.'
177 Check ((Get-Content (Join-Path $old 'my-project/work.txt') -Raw) -eq 'project data') 'Project data was lost.'
178 Check ((Get-Content (Join-Path $scenario 'outside/config.txt') -Raw) -eq 'outside data') 'External profile was modified.'
179 Check (Test-Path (Join-Path $old 'empty-project') -PathType Container) 'Empty project directory was lost.'
180 $copies = @(Get-ChildItem (Join-Path $old 'Cage-preserved-files') -File -Recurse | Where-Object { (Get-Content $_.FullName -Raw) -eq 'user customization' })
181 Check ($copies.Count -eq 1) 'Modified same-name program content was not retained exactly once.'
182 Check (!(Test-Path $backup)) 'Completed transfer left a full old application copy.'
183 foreach ($file in @('state/guest-media/system.img','state/lima/instance/disk','cache/lima/image')) {
184 Check ((Get-Content (Join-Path $old ('CageData/' + $file)) -Raw) -eq ('persistent-' + $file)) 'Update changed CageData.'
185 }
186 $embedded = Join-Path $scenario 'embedded.json'
187 Copy-Item (Join-Path $old 'Cage.install-files.json') $embedded
188 Write-File (Join-Path $old 'resources/customizable.txt') 'new customization'
189 Invoke-Helper @('-Operation','uninstall','-Target',$old,'-Manifest',$embedded)
190 Check (!(Test-Path (Join-Path $old 'program.txt'))) 'Uninstaller retained an unchanged program file.'
191 Check ((Get-Content (Join-Path $old 'my-project/work.txt') -Raw) -eq 'project data') 'Uninstall deleted a project.'
192 Check ((Get-Content (Join-Path $old 'resources/customizable.txt') -Raw) -eq 'new customization') 'Uninstall deleted a modified program file.'
193 Check (Test-Path (Join-Path $old 'empty-project') -PathType Container) 'Uninstall deleted an unknown empty project.'
194 Check (Test-Path (Join-Path $old 'Cage.install-id')) 'Retained data directory cannot be recognized on reinstall.'
195 foreach ($file in @('state/guest-media/system.img','state/lima/instance/disk','cache/lima/image')) {
196 Check ((Get-Content (Join-Path $old ('CageData/' + $file)) -Raw) -eq ('persistent-' + $file)) 'Uninstall changed CageData.'
197 }
198 App $stage 'reinstalled'
199 Prepare $old $stage $receipt
200 $null = Commit $old $stage $receipt
201 Check ((Get-Content (Join-Path $old 'my-project/work.txt') -Raw) -eq 'project data') 'Reinstall after uninstall lost the project.'
202 foreach ($file in @('state/guest-media/system.img','state/lima/instance/disk','cache/lima/image')) {
203 Check ((Get-Content (Join-Path $old ('CageData/' + $file)) -Raw) -eq ('persistent-' + $file)) 'Reinstall changed CageData.'
204 }
205 }
206 $scenario = Join-Path $root 'concurrent'
207 $old = Join-Path $scenario 'installed'; $stage = Join-Path $scenario 'staged'; $receipt = Join-Path $scenario 'receipt.json'
208 App $old 'old'; App $stage 'new'
209 Write-File (Join-Path $old 'project.txt') 'first'
210 Prepare $old $stage $receipt
211 Write-File (Join-Path $old 'project.txt') 'late edit'
212 Write-File (Join-Path $old 'new-project.txt') 'created during install'
213 [IO.Directory]::CreateDirectory((Join-Path $old 'late-empty-project')) | Out-Null
214 $null = Commit $old $stage $receipt
215 $late = @(Get-ChildItem (Join-Path $old 'Cage-preserved-files') -File -Recurse)
216 Check ((Get-Content (Join-Path $old 'project.txt') -Raw) -eq 'late edit') 'Concurrent edit was deleted.'
217 Check (@($late | Where-Object { (Get-Content $_.FullName -Raw) -eq 'created during install' }).Count -eq 1) 'Concurrent new file was deleted.'
218 Check (@(Get-ChildItem (Join-Path $old 'Cage-preserved-files') -Directory -Recurse | Where-Object Name -eq 'late-empty-project').Count -eq 1) 'Concurrent empty project was deleted.'
219 $scenario = Join-Path $root 'rollback'
220 $old = Join-Path $scenario 'installed'; $stage = Join-Path $scenario 'staged'; $receipt = Join-Path $scenario 'receipt.json'
221 App $old 'old'; App $stage 'new'
222 Write-File (Join-Path $old 'project.txt') 'rollback project'
223 Write-File (Join-Path $old 'CageData/state/lima/disk') 'rollback disk'
224 Prepare $old $stage $receipt
225 $backup = $old + '-backup'
226 [IO.Directory]::Move($old, $backup)
227 Remove-Item -LiteralPath $stage -Recurse
228 try { [IO.Directory]::Move($stage, $old); throw 'Expected the missing stage to fail.' }
229 catch [IO.DirectoryNotFoundException] { [IO.Directory]::Move($backup, $old) }
230 Invoke-Helper @('-Operation','complete','-Target',$old,'-Receipt',$receipt)
231 Check ((Get-Content (Join-Path $old 'project.txt') -Raw) -eq 'rollback project') 'Failed replacement lost the original project.'
232 Check ((Get-Content (Join-Path $old 'program.txt') -Raw) -eq 'old') 'Failed replacement changed the original program.'
233 Check ((Get-Content (Join-Path $old 'CageData/state/lima/disk') -Raw) -eq 'rollback disk') 'Failed replacement changed CageData.'
234 $stage = Join-Path $scenario 'staged-again'; App $stage 'new'
235 $locked = [IO.File]::Open((Join-Path $old 'program.txt'), 'Open', 'ReadWrite', 'None')
236 try { Invoke-Helper @('-Operation','prepare','-Source',$old,'-Target',$stage,'-Receipt',$receipt) 1 }
237 finally { $locked.Dispose() }
238 Check ((Get-Content (Join-Path $old 'project.txt') -Raw) -eq 'rollback project') 'Failed preparation changed a locked project.'
239 $fresh = Join-Path $root 'fresh'; App $fresh 'fresh'
240 $embedded = Join-Path $root 'fresh-embedded.json'; Copy-Item (Join-Path $fresh 'Cage.install-files.json') $embedded
241 Invoke-Helper @('-Operation','uninstall','-Target',$fresh,'-Manifest',$embedded)
242 Check (!(Test-Path $fresh)) 'An unmodified installation was not fully removed.'
243 $scenario = Join-Path $root 'links'
244 $old = Join-Path $scenario 'installed'; $stage = Join-Path $scenario 'staged'; $receipt = Join-Path $scenario 'receipt.json'
245 App $old 'old'; App $stage 'new'
246 Write-File (Join-Path $scenario 'outside/target.txt') 'external link target'
247 [IO.Directory]::CreateDirectory((Join-Path $old 'my-project')) | Out-Null
248 New-FileLink (Join-Path $old 'my-project/relative.txt') '../../outside/target.txt'
249 New-FileLink (Join-Path $old 'my-project/missing.txt') 'missing-target'
250 $junction = Join-Path $old 'my-project/junction'
251 if ($windows) {
252 & cmd.exe /c mklink /J $junction (Join-Path $scenario 'outside') | Out-Null
253 if ($LASTEXITCODE -ne 0) { throw 'Could not create the isolated junction fixture.' }
254 } else { [IO.Directory]::CreateSymbolicLink($junction, (Join-Path $scenario 'outside')) | Out-Null }
255 Prepare $old $stage $receipt
256 $null = Commit $old $stage $receipt
257 Check ((Read-Link (Join-Path $old 'my-project/relative.txt')) -eq '../../outside/target.txt') 'Relative symlink text changed during installation.'
258 Check ((Get-Content (Join-Path $old 'my-project/relative.txt') -Raw) -eq 'external link target') 'Relative symlink no longer reaches its target.'
259 Check ((Read-Link (Join-Path $old 'my-project/missing.txt')) -eq 'missing-target') 'Dangling symlink was lost.'
260 Check ((Get-Content (Join-Path $junction 'target.txt') -Raw) -eq 'external link target') 'Directory link was not retained.'
261 $embedded = Join-Path $scenario 'embedded.json'; Copy-Item (Join-Path $old 'Cage.install-files.json') $embedded
262 Invoke-Helper @('-Operation','uninstall','-Target',$old,'-Manifest',$embedded)
263 Check ((Get-Content (Join-Path $scenario 'outside/target.txt') -Raw) -eq 'external link target') 'Uninstall changed an external link target.'
264 Check ((Read-Link (Join-Path $old 'my-project/relative.txt')) -eq '../../outside/target.txt') 'Uninstall removed a project link.'
265 $scenario = Join-Path $root 'linked-program-directory'
266 $old = Join-Path $scenario 'installed'; App $old 'old'
267 $embedded = Join-Path $scenario 'embedded.json'; Copy-Item (Join-Path $old 'Cage.install-files.json') $embedded
268 Write-File (Join-Path $scenario 'outside/customizable.txt') 'default-old'
269 Remove-Item (Join-Path $old 'resources') -Recurse
270 if ($windows) {
271 & cmd.exe /c mklink /J (Join-Path $old 'resources') (Join-Path $scenario 'outside') | Out-Null
272 if ($LASTEXITCODE -ne 0) { throw 'Could not create the isolated junction fixture.' }
273 } else { [IO.Directory]::CreateSymbolicLink((Join-Path $old 'resources'), (Join-Path $scenario 'outside')) | Out-Null }
274 Invoke-Helper @('-Operation','uninstall','-Target',$old,'-Manifest',$embedded)
275 Check ((Get-Content (Join-Path $scenario 'outside/customizable.txt') -Raw) -eq 'default-old') 'Uninstall followed a replaced program directory link.'
276 Check ([bool]((Get-Item (Join-Path $old 'resources') -Force).Attributes -band [IO.FileAttributes]::ReparsePoint)) 'Uninstall removed a user directory link.'
277 $scenario = Join-Path $root 'linked-installation-root'
278 $actual = Join-Path $scenario 'actual'; $old = Join-Path $scenario 'installed'; $stage = Join-Path $scenario 'staged'; $receipt = Join-Path $scenario 'receipt.json'
279 App $actual 'old'; App $stage 'new'
280 Write-File (Join-Path $actual 'my-project/work.txt') 'external installation project'
281 if ($windows) {
282 & cmd.exe /c mklink /J $old $actual | Out-Null
283 if ($LASTEXITCODE -ne 0) { throw 'Could not create the isolated installation junction.' }
284 } else { [IO.Directory]::CreateSymbolicLink($old, $actual) | Out-Null }
285 Invoke-Helper @('-Operation','prepare','-Source',$old,'-Target',$stage,'-Receipt',$receipt) 1
286 Check ((Get-Content (Join-Path $actual 'my-project/work.txt') -Raw) -eq 'external installation project') 'Preparation followed the installation root link and changed its target.'
287 Check (!(Test-Path $receipt)) 'Preparation published a transaction for a redirected installation root.'
288 $scenario = Join-Path $root 'move-rollback'
289 $old = Join-Path $scenario 'installed'; $stage = Join-Path $scenario 'staged'; $receipt = Join-Path $scenario 'receipt.json'
290 App $old 'old'; App $stage 'new'
291 Write-File (Join-Path $old 'first-project/work.txt') 'first user project'
292 Write-File (Join-Path $old 'second-project/work.txt') 'second user project'
293 New-FileLink (Join-Path $old 'first-project/link.txt') 'work.txt'
294 Prepare $old $stage $receipt
295 $moves = @((Get-Content $receipt -Raw | ConvertFrom-Json).moves)
296 $backup = $old + '-backup'; [IO.Directory]::Move($old, $backup)
297 [IO.Directory]::CreateDirectory((Join-Path $stage $moves[-1].destination)) | Out-Null
298 Invoke-Helper @('-Operation','move','-Source',$backup,'-Target',$stage,'-Receipt',$receipt) 1
299 Invoke-Helper @('-Operation','restore','-Source',$stage,'-Target',$backup,'-Receipt',$receipt)
300 [IO.Directory]::Move($backup, $old)
301 Check ((Get-Content (Join-Path $old 'first-project/work.txt') -Raw) -eq 'first user project') 'Rollback missed an already moved project.'
302 Check ((Get-Content (Join-Path $old 'second-project/work.txt') -Raw) -eq 'second user project') 'Rollback changed the project whose move failed.'
303 Check ((Read-Link (Join-Path $old 'first-project/link.txt')) -eq 'work.txt') 'Rollback changed a relative project link.'
304 $scenario = Join-Path $root 'type-conflicts'
305 $old = Join-Path $scenario 'installed'; $stage = Join-Path $scenario 'staged'; $receipt = Join-Path $scenario 'receipt.json'
306 App $old 'old'; App $stage 'new'
307 Remove-Item (Join-Path $old 'Cage.install-files.json')
308 Write-File (Join-Path $old 'obsolete.dll') 'obsolete library'
309 Write-File (Join-Path $old 'resources/new-tool/data.txt') 'user directory'
310 Write-File (Join-Path $stage 'resources/new-tool') 'new program file'
311 Prepare $old $stage $receipt
312 $null = Commit $old $stage $receipt
313 Check (!(Test-Path (Join-Path $old 'obsolete.dll'))) 'Legacy unknown DLL remained in the new application loader path.'
314 Check ((Get-Content (Join-Path $old 'resources/new-tool') -Raw) -eq 'new program file') 'A user directory replaced a new program file.'
315 $retained = @(Get-ChildItem (Join-Path $old 'Cage-preserved-files') -File -Recurse)
316 Check (@($retained | Where-Object { (Get-Content $_.FullName -Raw) -eq 'obsolete library' }).Count -eq 1) 'Legacy DLL bytes were lost.'
317 Check (@($retained | Where-Object { (Get-Content $_.FullName -Raw) -eq 'user directory' }).Count -eq 1) 'Conflicting user directory was lost.'
318 $scenario = Join-Path $root 'legacy-automatic-rollback'
319 $old = Join-Path $scenario 'installed'; $stage = Join-Path $scenario 'restored'; $receipt = Join-Path $scenario 'receipt.json'
320 App $stage 'old-program'
321 Remove-Item (Join-Path $stage 'Cage.install-files.json')
322 Write-File (Join-Path $stage 'my-project/work.txt') 'before attempted update'
323 Write-File (Join-Path $stage 'personal.txt') 'old personal file'
324 App $old 'failed-new-program'
325 Write-File (Join-Path $old 'my-project/work.txt') 'saved while the new version was running'
326 Write-File (Join-Path $old 'personal.txt') 'latest personal file'
327 New-FileLink (Join-Path $old 'my-project/latest-link.txt') 'work.txt'
328 Invoke-Helper @('-Operation','prepare','-Source',$old,'-Target',$stage,'-Receipt',$receipt,'-Backup',($old+'-backup'),'-LegacyTarget')
329 $null = Commit $old $stage $receipt
330 Check ((Get-Content (Join-Path $old 'program.txt') -Raw) -eq 'old-program') 'Legacy recovery did not restore the previous program.'
331 Check ((Get-Content (Join-Path $old 'my-project/work.txt') -Raw) -eq 'saved while the new version was running') 'Legacy recovery put an old project at the active path.'
332 Check ((Get-Content (Join-Path $old 'personal.txt') -Raw) -eq 'latest personal file') 'Legacy recovery put an old personal file at the active path.'
333 Check ((Read-Link (Join-Path $old 'my-project/latest-link.txt')) -eq 'work.txt') 'Legacy recovery lost the current relative link.'
334 $previousCopies = @(Get-ChildItem (Join-Path $old 'Cage-preserved-files') -File -Recurse)
335 Check (@($previousCopies | Where-Object { (Get-Content $_.FullName -Raw) -eq 'before attempted update' }).Count -eq 1) 'Legacy recovery deleted the previous project version.'
336 Check (@($previousCopies | Where-Object { (Get-Content $_.FullName -Raw) -eq 'old personal file' }).Count -eq 1) 'Legacy recovery deleted the previous personal-file version.'
337 if ($windows) {
338 & $HelperShell -NoProfile -File (Join-Path $PSScriptRoot 'test-windows-verified-file-deletion.ps1')
339 if ($LASTEXITCODE -ne 0) { throw 'Verified-handle deletion regression failed.' }
340 }
341 Write-Output "Installation file ownership: $checks checks passed."
342} finally {
343 if (Test-Path -LiteralPath $root) { Remove-TestTree $root; [IO.Directory]::Delete($root) }
344}