[CmdletBinding()] param( [Parameter(Mandatory)][string]$FailedSetupDirectory, [Parameter(Mandatory)][string]$CorrectedSetupDirectory, [Parameter(Mandatory)][string]$ReportPath ) $ErrorActionPreference='Stop' $release=Get-Content -Raw (Join-Path $PSScriptRoot 'Release.json') | ConvertFrom-Json $checks=[Collections.Generic.List[string]]::new() function Assert-Upgrade([bool]$condition,[string]$message){if(!$condition){throw "Upgrade regression failed: $message"};$checks.Add($message)} function Com($object,[string]$member,[object[]]$arguments,[switch]$Property){$values=@(foreach($v in $arguments){$v.PSObject.BaseObject});$flags=if($Property){[Reflection.BindingFlags]::GetProperty}else{[Reflection.BindingFlags]::InvokeMethod};$object.GetType().InvokeMember($member,$flags,$null,$object,$values)} function Rows($database,[string]$table){ $view=Com $database 'OpenView' @('SELECT * FROM `'+$table+'`');$null=Com $view 'Execute' @() $columns=Com $view 'ColumnInfo' @(0) -Property;$count=Com $columns 'FieldCount' @() -Property;$rows=@() while($null -ne ($record=Com $view 'Fetch' @())){$row=[ordered]@{};for($i=1;$i -le $count;$i++){$row[(Com $columns 'StringData' @($i) -Property)]=Com $record 'StringData' @($i) -Property};$rows+=,[pscustomobject]$row} $null=Com $view 'Close' @();return $rows } function Authoring($installer,[string]$path){ # OpenDatabase mode0 only. Never open an installation session or execute actions. $db=Com $installer 'OpenDatabase' @($path,0) $properties=@{};foreach($row in @(Rows $db 'Property')){$properties[$row.Property]=$row.Value} $features=@(Rows $db 'Feature');$upgrade=@(Rows $db 'Upgrade');$sequence=@(Rows $db 'InstallExecuteSequence') $problems=[Collections.Generic.List[string]]::new() if($properties.ADDLOCAL -ne 'Complete'){$problems.Add('No explicit local Complete feature selection')} if(@($features | Where-Object {$_.Feature -eq 'Complete' -and ([int]$_.Attributes -band 8) -eq 8 -and ([int]$_.Attributes -band 16) -eq 16 -and [int]$_.Level -gt 0}).Count -ne 1){$problems.Add('Complete can be advertised or absent')} if(@($upgrade | Where-Object {([int]$_.Attributes -band 1) -ne 0}).Count){$problems.Add('Old advertised feature state is eligible for migration')} if(@($sequence | Where-Object Action -eq 'MigrateFeatureStates').Count){$problems.Add('MigrateFeatureStates action is scheduled')} return @{problems=@($problems);properties=$properties;files=@(Rows $db 'File');components=@(Rows $db 'FeatureComponents')} } $fixture=Get-Content -Raw (Join-Path $PSScriptRoot 'Fixtures\Advertised-Version10-Upgrade.txt') Assert-Upgrade ($fixture -match "setting feature 'Complete' to 'Advertise'" -and $fixture -match 'Request: Null; Action: Null') 'Regression fixture records the real advertised-feature/no-file-action failure' $installer=New-Object -ComObject WindowsInstaller.Installer $failed=Authoring $installer (Join-Path $FailedSetupDirectory 'PlotDirectorCompanion.msi') $corrected=Authoring $installer (Join-Path $CorrectedSetupDirectory 'PlotDirectorCompanion.msi') Assert-Upgrade ($failed.problems.Count -eq 4) 'Negative control: original released version12 MSI fails all four local-install invariants' Assert-Upgrade ($corrected.problems.Count -eq 0) 'Corrected MSI cannot inherit the recorded advertised feature selection' Assert-Upgrade ($failed.properties.ProductCode -ne $corrected.properties.ProductCode -and $corrected.properties.ProductCode -eq $release.productCode -and $corrected.properties.ProductVersion -eq $release.msiVersion) 'New release has its own major-upgrade identity, distinct from the failed version12 package' $exe=Join-Path $CorrectedSetupDirectory 'PlotDirectorCompanionSetup.exe' $assembly=[Reflection.Assembly]::Load([IO.File]::ReadAllBytes($exe)) $policy=$assembly.GetType('CompanionUpgradePolicy',$true) foreach($scenario in @(@{name='version10 advertised-feature major upgrade';registered=$false},@{name='same-release advertised repair';registered=$true})){ # Invoke only the compiled pure policy, never Setup.Main or any MSI API action. $properties=[string]$policy.GetMethod('InstallProperties').Invoke($null,@([bool]$scenario.registered)) Assert-Upgrade ($properties -match '(^| )ADDLOCAL=Complete( |$)' -and $properties -match 'REINSTALLMODE=vamus' -and $properties -notmatch 'ADVERTISE=') "Compiled bootstrapper requests local files: $($scenario.name)" Assert-Upgrade (($properties -match 'REINSTALL=ALL') -eq [bool]$scenario.registered) "Compiled bootstrapper chooses correct repair mode: $($scenario.name)" } foreach($versions in @(@{word='1.0.0.10';agent='1.0.0.10'},@{word='1.0.0.12';agent='1.0.0.12'},@{word='1.0.0.13';agent='1.0.0.13'},@{word='1.0.0.14';agent='1.0.0.14'},@{word=$release.version;agent='1.0.0.10'},@{word='1.0.0.10';agent=$release.version})){ $message=$null;try{$policy.GetMethod('VerifyVersions').Invoke($null,@($release.version,$versions.word,$versions.agent))}catch{$message=$_.Exception.GetBaseException().Message} Assert-Upgrade ($message.Contains('Expected '+$release.version) -and $message.Contains('Word add-in detected '+$versions.word) -and $message.Contains('tray executable detected '+$versions.agent)) "Strict check rejects and diagnoses old/mixed files: $($versions.word) / $($versions.agent)" } $null=$policy.GetMethod('VerifyVersions').Invoke($null,@($release.version,$release.version,$release.version));$checks.Add('Strict check accepts matching release files') $transition=$policy.GetMethod('EnsureLocalFeature') $query=[Func[int]]{$script:stateValue};$logger=[Action[string]]{param($message)} foreach($initial in @(1,2,4)){ $script:stateValue=$initial;$script:configureCalls=0 $configure=[Func[uint32]]{$script:configureCalls++;$script:stateValue=3;return [uint32]0} $null=$transition.Invoke($null,@($query,$configure,$logger)) Assert-Upgrade ($script:configureCalls -eq 1 -and $script:stateValue -eq 3) "Compiled control flow explicitly promotes feature state $initial to Local before accepting versions" } $script:stateValue=3;$script:configureCalls=0 $null=$transition.Invoke($null,@($query,$configure,$logger)) Assert-Upgrade ($script:configureCalls -eq 0) 'Already Local feature is not needlessly configured again' foreach($failure in @('native-error','still-advertised','unknown-state')){ $script:stateValue=if($failure -eq 'unknown-state'){-1}else{1};$script:configureCalls=0 $configure=[Func[uint32]]{$script:configureCalls++;if($failure -eq 'native-error'){return [uint32]1603};return [uint32]0} $refused=$false;try{$null=$transition.Invoke($null,@($query,$configure,$logger))}catch{$refused=$true} Assert-Upgrade $refused "Compiled feature transition refuses $failure instead of trusting result0 or old files" if($failure -eq 'unknown-state'){Assert-Upgrade ($script:configureCalls -eq 0) 'Unknown native state is rejected before any configuration request'} } $bootstrap=Get-Content -Raw (Join-Path $PSScriptRoot 'Setup.cs') Assert-Upgrade ($bootstrap.IndexOf('CompanionUpgradePolicy.EnsureLocalFeature(') -lt $bootstrap.IndexOf('CompanionUpgradePolicy.VerifyVersions(') -and $bootstrap.Contains('MsiConfigureFeature(SetupRelease.ProductCode, "Complete", 3)')) 'Bootstrapper wires explicit native Local configuration before strict version validation' $required=@('PlotDirector.WordAddIn.dll','PlotDirector.Companion.Agent.exe') foreach($name in $required){$file=@($corrected.files | Where-Object {($_.FileName -split '\|')[-1] -eq $name});Assert-Upgrade ($file.Count -eq 1 -and @($corrected.components | Where-Object {$_.Component_ -eq $file[0].Component_ -and $_.Feature_ -eq 'Complete'}).Count -eq 1) "Required file belongs to the locally selected Complete feature: $name"} $copy=$assembly.GetType('CompanionFileCopyPreflight',$true) $verifyCopy=$copy.GetMethod('Verify');$guardMethod=$copy.GetMethod('AcquireAgentGuard') $scratch=Join-Path ([IO.Path]::GetTempPath()) ('PlotDirector-copy-regression-'+[guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $scratch | Out-Null $dll=Join-Path $scratch 'Microsoft.Office.Tools.Common.dll' [IO.File]::WriteAllBytes($dll,[byte[]](1,2,3,4));$originalHash=(Get-FileHash $dll).Hash try { $null=$verifyCopy.Invoke($null,@([string]$scratch,[string[]]@('Microsoft.Office.Tools.Common.dll','missing.dll'),$logger)) Assert-Upgrade ((Get-FileHash $dll).Hash -eq $originalHash) 'Replacement preflight accepts unlocked/missing payload without changing bytes' $locked=[IO.File]::Open($dll,[IO.FileMode]::Open,[IO.FileAccess]::Read,[IO.FileShare]::Read) try { $message=$null;try{$null=$verifyCopy.Invoke($null,@([string]$scratch,[string[]]@('Microsoft.Office.Tools.Common.dll'),$logger))}catch{$message=$_.Exception.ToString()} Assert-Upgrade ($message -like '*Microsoft.Office.Tools.Common.dll*Windows error 32*No registration or MSI changes*') 'Actual Windows sharing lock on the reported DLL blocks setup before registration/MSI changes' } finally {$locked.Dispose()} [IO.File]::SetAttributes($dll,[IO.FileAttributes]::ReadOnly) try { $message=$null;try{$null=$verifyCopy.Invoke($null,@([string]$scratch,[string[]]@('Microsoft.Office.Tools.Common.dll'),$logger))}catch{$message=$_.Exception.ToString()} Assert-Upgrade ($message -like '*Windows error 5*No registration or MSI changes*') 'Read-only replacement refusal reports access denied without relaxing permissions' } finally {[IO.File]::SetAttributes($dll,[IO.FileAttributes]::Normal)} $guard=$guardMethod.Invoke($null,@([string]$scratch,7)) try { $blocked=$false;try{$race=[IO.File]::Open((Join-Path $scratch 'agent-7.lock'),[IO.FileMode]::OpenOrCreate,[IO.FileAccess]::ReadWrite,[IO.FileShare]::None);$race.Dispose()}catch{$blocked=$true} Assert-Upgrade $blocked 'Installer holds the existing tray singleton contract against shortcut/sign-in restart during copying' } finally {$guard.Dispose()} $next=$guardMethod.Invoke($null,@([string]$scratch,7));$next.Dispose() Assert-Upgrade ((Get-FileHash $dll).Hash -eq $originalHash) 'Singleton guard releases for later tray launch and payload bytes remain unchanged' } finally { # Only these exact generated fixture files, never the installed application. foreach($fixturePath in @($dll,(Join-Path $scratch 'agent-7.lock'))){if(Test-Path -LiteralPath $fixturePath){Remove-Item -LiteralPath $fixturePath -Force}} Remove-Item -LiteralPath $scratch } $releaseType=$assembly.GetType('SetupRelease',$true) $names=$releaseType.GetField('PayloadFiles',[Reflection.BindingFlags]'Static,NonPublic').GetValue($null) Assert-Upgrade ($names.Count -eq $corrected.files.Count -and $names -contains 'Microsoft.Office.Tools.Common.dll' -and $names -contains 'Agent\PlotDirector.Companion.Agent.exe') 'Compiled replacement preflight inventory covers the whole MSI payload including the failed DLL and tray' Assert-Upgrade ($bootstrap.IndexOf('CompanionFileCopyPreflight.Verify(') -lt $bootstrap.IndexOf('BackupRegistration(state)') -and $bootstrap.IndexOf('CompanionFileCopyPreflight.Verify(') -lt $bootstrap.IndexOf('RemoveOldDeployment(state)') -and $bootstrap.IndexOf('CompanionFileCopyPreflight.Verify(') -lt $bootstrap.IndexOf('MsiInstallProduct(msi, properties)')) 'Copy preflight precedes registration and all VSTO/MSI mutation' Assert-Upgrade ($bootstrap.Contains('finally { if (upgradeGuard != null) upgradeGuard.Dispose(); }') -and $bootstrap.IndexOf('upgradeGuard.Dispose(); upgradeGuard = null;') -lt $bootstrap.IndexOf('Process.Start(new ProcessStartInfo(agent')) 'Singleton guard releases on failure and before the installed tray is started' @{passed=$checks;rejectedOriginalAuthoring=$failed.problems;correctedSetupSha256=(Get-FileHash $exe).Hash;scope='Read-only MSI tables, compiled policy and disposable Windows file-lock fixtures only. No installed upgrade was executed.'} | ConvertTo-Json -Depth 6 | Set-Content -LiteralPath $ReportPath -Encoding UTF8 Write-Output "PASS exact advertised-feature upgrade regression: $($checks.Count) assertions; original package rejected, corrected package accepted without installation."