| 1 | #requires -Version 5.1 |
| 2 | $ErrorActionPreference = 'Stop' |
| 3 | $errors = $null |
| 4 | $ast = [Management.Automation.Language.Parser]::ParseFile((Join-Path $PSScriptRoot 'provision-claude-offline-windows.ps1'), [ref]$null, [ref]$errors) |
| 5 | if ($errors.Count) { throw $errors[0] } |
| 6 | foreach ($f in $ast.FindAll({param($n) $n -is [Management.Automation.Language.FunctionDefinitionAst]},$false)) { |
| 7 | . ([scriptblock]::Create($f.Extent.Text)) |
| 8 | } |
| 9 | function Reject([scriptblock]$operation, [string]$reason) { |
| 10 | try { $null = & $operation } catch { if ($_.Exception.Message.Contains($reason)) { return }; throw } |
| 11 | throw "Expected rejection: $reason" |
| 12 | } |
| 13 | $root = [IO.Path]::GetTempPath().TrimEnd([IO.Path]::DirectorySeparatorChar) |
| 14 | $diskFile = Join-Path $root 'system.vhdx' |
| 15 | $id = [guid]::NewGuid() |
| 16 | $vm = [pscustomobject]@{Id=$id; State='Off'; Generation=2; ConfigurationLocation=$root} |
| 17 | $receipt = [pscustomobject]@{VMId=$id; Edition='EnterpriseEval'; Disk=$diskFile; InstanceDirectory=$root} |
| 18 | $drive = @([pscustomobject]@{Path=$diskFile}) |
| 19 | $vhd = [pscustomobject]@{Path=$diskFile; VhdFormat='VHDX'; VhdType='Dynamic'; ParentPath=$null; Attached=$false; DiskNumber=17} |
| 20 | Assert-CageOfflineTarget $vm $drive @() $vhd $receipt $root $id |
| 21 | Reject { Assert-CageOfflineTarget $vm $drive @() $vhd $receipt $root ([guid]::NewGuid()) } 'do not match' |
| 22 | Reject { Assert-CageOfflineTarget $vm $drive @('checkpoint') $vhd $receipt $root $id } 'standalone' |
| 23 | $vm.State='Running'; Reject { Assert-CageOfflineTarget $vm $drive @() $vhd $receipt $root $id } 'stopped' |
| 24 | $vm.State='Off'; $vhd.ParentPath='parent'; Reject { Assert-CageOfflineTarget $vm $drive @() $vhd $receipt $root $id } 'standalone' |
| 25 | $vhd.ParentPath=$null; $vhd.Attached=$true; Reject { Assert-CageOfflineTarget $vm $drive @() $vhd $receipt $root $id } 'unattached' |
| 26 | $disk = [pscustomobject]@{Number=17; IsBoot=$false; IsSystem=$false; IsOffline=$false; IsReadOnly=$false; PartitionStyle='GPT'} |
| 27 | Assert-MountedGuestDisk $disk $vhd $diskFile |
| 28 | foreach ($property in @('IsBoot','IsSystem','IsOffline','IsReadOnly')) { |
| 29 | $disk.$property=$true; Reject { Assert-MountedGuestDisk $disk $vhd $diskFile } 'guest GPT disk'; $disk.$property=$false |
| 30 | } |
| 31 | $vhd.DiskNumber=99; Reject { Assert-MountedGuestDisk $disk $vhd $diskFile } 'guest GPT disk' |
| 32 | $vhd.DiskNumber=17; Reject { Assert-MountedGuestDisk $disk $vhd 'unrelated.vhdx' } 'guest GPT disk' |
| 33 | $part = [pscustomobject]@{GptType='{EBD0A0A2-B9E5-4433-87C0-68B6B72699C7}';DiskNumber=17;PartitionNumber=3;IsBoot=$false;IsSystem=$false} |
| 34 | $recovery = [pscustomobject]@{GptType='{DE94BBA4-06D1-4D40-A16A-BFD50179D6AC}';DiskNumber=17;PartitionNumber=4;IsBoot=$false;IsSystem=$false} |
| 35 | if ((Select-GuestWindowsPartition @($part,$recovery) 17).PartitionNumber -ne 3) { throw 'Wrong guest partition' } |
| 36 | Reject { Select-GuestWindowsPartition @($part,$part) 17 } 'exactly one' |
| 37 | Reject { Select-GuestWindowsPartition @($part) 5 } 'exactly one' |
| 38 | |
| 39 | $script:expectedImage = Join-Path $root 'isolated-guest-mount' |
| 40 | $script:fullName = 'Claude_4.23118.0.0_x64__pzs8sxrjxfjjc' |
| 41 | $script:provisioned = @() |
| 42 | $script:writes=0 |
| 43 | $image = @{Path=$script:expectedImage; ScratchDirectory=(Join-Path $root 'qa-scratch'); LogPath=(Join-Path $root 'qa.log'); ErrorAction='Stop'} |
| 44 | function Get-AppxProvisionedPackage { |
| 45 | param($Path,$ScratchDirectory,$LogPath,$ErrorAction,[switch]$Online) |
| 46 | if ($Path -ne $script:expectedImage -or $Online -or !$ScratchDirectory -or !$LogPath) { throw 'Incorrect DISM target' } |
| 47 | $script:provisioned |
| 48 | } |
| 49 | function Add-AppxProvisionedPackage { |
| 50 | param($Path,$PackagePath,[switch]$SkipLicense,$Regions,$ScratchDirectory,$LogPath,$ErrorAction,[switch]$Online) |
| 51 | if ($Path -ne $script:expectedImage -or $Online -or $Regions -ne 'all' -or !$ScratchDirectory -or !$LogPath) { throw 'Incorrect DISM write target' } |
| 52 | $script:writes++ |
| 53 | if ($PackagePath -eq 'fixture.msix') { $script:provisioned=@([pscustomobject]@{DisplayName='Claude';PackageName=$script:fullName}) } |
| 54 | } |
| 55 | Reject { Install-OfflineClaude $image 'missing-readback.msix' $script:fullName } 'did not list' |
| 56 | $first=Install-OfflineClaude $image 'fixture.msix' $script:fullName |
| 57 | $second=Install-OfflineClaude $image 'fixture.msix' $script:fullName |
| 58 | if ($first.AlreadyProvisioned -or !$second.AlreadyProvisioned -or $script:writes -ne 2) { throw 'Unexpected repeat provision' } |
| 59 | $script:provisioned[0].PackageName='Claude_wrong_publisher' |
| 60 | Reject { Install-OfflineClaude $image 'fixture.msix' $script:fullName } 'preserved' |
| 61 | if ($script:writes -ne 2) { throw 'Different package was overwritten' } |
| 62 | Write-Output 'Passed offline VM, disk, partition and DISM target guards, exact readback, idempotence and foreign package preservation (simulated).' |