# Offline only: load function definitions via AST; never execute either script's entry point.
$ErrorActionPreference='Stop'
if($PSVersionTable.PSEdition-ne'Desktop'-or$PSVersionTable.PSVersion.Major-ne5-or$PSVersionTable.PSVersion.Minor-ne1){throw'Run these checks with Windows PowerShell 5.1 (powershell.exe).'}
$options=Get-OfficeDeploymentOptions@{OfficeSubscription='Microsoft 365 Business Premium';ValidateBeforeChanges=$value}
Assert-OfficeTest($options.ValidateBeforeChanges-is[bool]-and!$options.ValidateBeforeChanges)"Explicit opt-out '$value' was ignored."
}
Assert-OfficeTest((Get-OfficeDeploymentOptions@{OfficeSubscription='Microsoft 365 Business Premium'}).ValidateBeforeChanges)'Missing validation input enabled changes.'
Assert-OfficeThrows{Get-OfficeDeploymentOptions@{OfficeSubscription='Microsoft 365 Business Premium';ValidateBeforeChanges='maybe'}}'Invalid \$ValidateBeforeChanges'
$expected=@{
'Microsoft 365 Business Standard'='O365BusinessRetail'
'Microsoft 365 Business Premium'='O365BusinessRetail'
'Microsoft 365 Apps for business'='O365BusinessEEANoTeamsRetail'
'Microsoft 365 Apps for enterprise'='O365ProPlusEEANoTeamsRetail'
'Microsoft/Office 365 E3/E5'='O365ProPlusRetail'
'Microsoft 365 E3'='O365ProPlusRetail'
'Microsoft 365 E5'='O365ProPlusRetail'
'Office 365 E3'='O365ProPlusRetail'
'Office 365 E5'='O365ProPlusRetail'
}
foreach($planin@('Microsoft/Office 365 E3/E5','Microsoft/Office 365 E3/E5 (no Teams)')){
Assert-OfficeThrows{Get-OfficeDeploymentOptions@{OfficeSubscription='Microsoft 365 Business Standard';OfficeActivation='SharedComputer'}}'requires Business Premium'
Assert-OfficeThrows{Get-OfficeDeploymentOptions@{OfficeSubscription='Microsoft 365 Apps for business';OfficeActivation='SharedComputer'}}'requires Business Premium'
foreach($planin@('Microsoft 365 Business Premium','Microsoft 365 Business Premium (no Teams)','Microsoft 365 Apps for enterprise')){
$options=Get-OfficeDeploymentOptions@{OfficeSubscription='Microsoft 365 Business Premium';OfficeArchitecture='32';COMPANY='A & B <Company> "quoted"';OfficeActivation='SharedComputer';OfficeAppShutdown='Force'}
Assert-OfficeTest($roundTrip.Configuration.Add.Version-eq'16.0.12345.67890')'Downloaded version must be pinned.'
foreach($appin@('OneDrive','Groove','Teams','OutlookForWindows')){Assert-OfficeTest($app-in$roundTrip.Configuration.Add.Product.ExcludeApp.ID)"ODT must leave $app to the explicit companion installation stage."}
# Exercise actual ZIP extraction in media staging under the Tools.ps1 command scope.
# Network requests, Microsoft signatures, and executable launches are mocked; files are inert.
&{
$archiveCommand=Get-CommandExpand-Archive
Assert-OfficeTest($archiveCommand.CommandType-eq'Function'-and!$archiveCommand.Parameters.ContainsKey('LiteralPath'))'The real Tools archive override must be present for this regression.'
Assert-OfficeTest((Get-Content-LiteralPath$media.Scrubber-Raw)-eq'Inert scrubber fixture; never executed.')'Get Help ZIP did not extract correctly with Tools loaded and a literal path.'
Assert-OfficeTest($media.Scrubber-in$script:mediaSignatures-and$media.Setup-in$script:mediaSignatures)'Extracted Get Help and ODT must still pass signature checks.'
Assert-OfficeTest($media.Version-eq'16.0.12345.67890'-and$media.Companions.Count-eq3-and!$media.Companions.ContainsKey('Teams'))'Media staging lost its build or no-Teams selection.'
Assert-OfficeTest(([xml](Get-Content-LiteralPath$media.Config-Raw)).Configuration.Add.Version-eq$media.Version)'Staged configuration must pin the downloaded build.'
Assert-OfficeTest(($script:mediaProcesses-join',')-eq'odt.exe,setup.exe')'Staging attempted to run a scrubber or companion installer.'
Assert-OfficeTest($media.Count-eq4-and$script:staged.Count-eq4-and($script:staged-join',')-eq($script:verified-join','))'Every companion installer must be staged and signed before removal.'
foreach($planin@('Microsoft 365 Business Standard (no Teams)','Microsoft 365 Business Premium (no Teams)','Microsoft/Office 365 E3/E5 (no Teams)','Microsoft 365 Apps for business','Microsoft 365 Apps for enterprise','Office 365 E3 (no Teams)')){
Assert-OfficeTest($null-eq$failure-and$result.ExitCode-eq$expectedCode)"Unexpected companion result for $script:appMode : $failure"
Assert-OfficeTest($result.Versions.OneDrive-eq'26.1.2.3'-and$result.Versions.Teams-eq'1.0.0.0'-and$result.Versions.NewOutlook-eq'1.0.0.0')'Companion verification receipt is incomplete.'
Assert-OfficeTest(($script:appCalls-join'|')-eq'WebView2 /silent /install|OneDrive /allusers /silent|Teams -p|NewOutlook --provision true --quiet --start-')'Companion installers must run silently and for all users.'
}else{
Assert-OfficeTest($null-ne$failure)"Companion failure was swallowed: $script:appMode"
if($script:appMode-notlike'*NewOutlook'){Assert-OfficeTest(!($script:appCalls-match'^NewOutlook '))'Install continued after a companion failure.'}
Assert-OfficeTest(!($script:appCalls-match'^WebView2 '))'Existing machine WebView2 runtime was unnecessarily installed.'
foreach($planin@('Microsoft 365 Business Standard (no Teams)','Microsoft 365 Business Premium (no Teams)','Microsoft/Office 365 E3/E5 (no Teams)','Microsoft 365 Apps for business','Microsoft 365 Apps for enterprise','Office 365 E5 (no Teams)')){
Add-Type-TypeDefinition'using System; public static class OfficeArgumentProbe { public static int Main(string[] args) { Console.Write(String.Join("|", args)); return 0; } }'-OutputAssembly$argumentProbe-OutputTypeConsoleApplication
functionNew-ScheduledTaskAction{param($Execute,$Argument)Assert-OfficeTest($Execute-like'*\WindowsPowerShell\v1.0\powershell.exe')'User cleanup must use Windows PowerShell 5.1.';[pscustomobject]@{Execute=$Execute;Arguments=$Argument}}
functionNew-ScheduledTaskPrincipal{param($UserId,$LogonType,$RunLevel)Assert-OfficeTest($LogonType-eq'Interactive'-and$RunLevel-eq'Limited')'User task must run limited and interactively.';$UserId}
Assert-OfficeTest($script:registered.Count-eq2-and$script:started.Count-eq1-and$script:removed.Count-eq1)'User scheduling or completed task cleanup is incorrect.'
Assert-OfficeTest($pending.Count-eq1-and$pending[0]-eq'logged-off')'Logged-off user cleanup was reported complete.'
}
# Test orchestration with every machine mutation mocked.
functionStart-OfficeUserCleanup{$script:events+='user';if($script:mode-eq'user'){throw'user still running'};if($script:mode-eq'pending'){return,@('S-1-5-21-test')};return,@()}
functionSet-OfficeReinstallPhase{$script:events+='phase';if($script:mode-eq'phase'){throw'user still holds cleanup lock'}}
Assert-OfficeTest($result.ExitCode-eq$code)"Wrong result code for $script:mode"
}else{Assert-OfficeTest($null-ne$errorResult)"Failure swallowed in $script:mode"}
if($script:mode-notin@('success','pending','install','install-companions','companion-reboot')){Assert-OfficeTest('install'-notin$script:events)"Install ran after $script:mode failure/reboot."}
if($script:mode-notin@('success','pending','install-companions','companion-reboot')){Assert-OfficeTest('install-companions'-notin$script:events)"Companion install ran after $script:mode failure/reboot."}
if($script:mode-eq'download'){Assert-OfficeTest('companions'-notin$script:events-and'scrub'-notin$script:events)'Removal ran after download failed.'}
if($script:mode-eq'success'){Assert-OfficeTest(($script:events-join',')-eq'download,apps,companions,scrub,identity,user,phase,install,install-companions')'Deployment order changed.'}
}
$script:mode='success';$script:events=@()
$options=Get-OfficeDeploymentOptions@{OfficeSubscription='Microsoft/Office 365 E3/E5 (no Teams)';OfficeAppShutdown='Force';ValidateBeforeChanges='false'}
$result=Invoke-OfficeDeployment$options
Assert-OfficeTest(!$script:teamsRequested-and!$result.TeamsSelected)'Deployment did not pass the no-Teams choice to companion installation/reporting.'
foreach($shutdownin@('Abort','Force')){
foreach($validatein@($null,'','true')){
$script:events=@()
$options=Get-OfficeDeploymentOptions@{OfficeSubscription='Microsoft/Office 365 E3/E5 (no Teams)';OfficeAppShutdown=$shutdown;ValidateBeforeChanges=$validate}
$result=Invoke-OfficeDeployment$options
Assert-OfficeTest($result.OfficeDeploymentStatus-eq'ValidationPassed'-and$result.ExitCode-eq0-and$result.ValidateBeforeChanges)'Validation did not produce a distinct result.'
Assert-OfficeTest(($script:events-join',')-eq'download')'Validation reached app shutdown, removal, identities, user tasks, phase changes, or installation.'
Assert-OfficeTest(!$result.TeamsSelected-and$result.ProfileCount-eq1-and$result.ExistingOffice[0]-eq'Synthetic Office')'Validation report lost the deployment plan.'
Assert-OfficeTest($selected.Count-eq1-and$selected[0].ProductId-eq'O365BusinessRetail'-and$selected[0].Company-eq$COMPANY)'Run-Script lost RMM caller variables.'
}
# Run the real shim -> original Run-Script -> main entry point in a child process.
# Replace only the destructive workload; mock WebClient rather than making network calls.
Assert-OfficeTest($receipt.ProductId-eq'O365ProPlusEEANoTeamsRetail'-and$receipt.Company-ceq$probeValues.COMPANY-and$receipt.Activation-eq'SharedComputer')'Shim lost injected strings before main workload.'
Assert-OfficeTest($receipt.ValidateBeforeChanges-eq($case.Validate-ne'false'))'Shim lost the validation gate.'
Assert-OfficeTest($probeLog-match'Mock action diagnostic'-and$probeLog-match'"OfficeDeploymentStatus"'-and$probeText-notmatch'Mock action diagnostic')'Diagnostics or receipt did not reach the correct log/console destination.'
Assert-OfficeTest($probeLog-notmatch'Validation only:|Diagnostic log:')'Console-only output was unexpectedly logged.'
}elseif($case.Mode-eq'missing'){
Assert-OfficeTest($probeText-match'did not return a verified result')'Missing workload receipt was not detected.'
}else{
Assert-OfficeTest((Get-Content-LiteralPath(Join-Path$testRoot'probe-management.log')-Raw)-match'Error: Office deployment failed: Simulated deployment failure'-and$probeText-match'Simulated deployment failure')'Failure must reach both Tools log and Syncro console.'
}
}
# Exercise the native-host command with the same network mock and an inert child workload.
Assert-OfficeTest($receipt.ValidateBeforeChanges-and$receipt.LogFile-eq$Global:LogFile-and(Get-Content-LiteralPath$Global:LogFile-Raw)-match'Native logging diagnostic')'Native-host command must reload Tools and preserve validation and the parent log.'
# Simulate Syncro's 32-bit Windows PowerShell launching the native 64-bit 5.1 host.
# Only the relaunch payload is replaced; host selection and process exit handling are real.
Assert-OfficeTest($hostResult.Edition-eq'Desktop'-and$hostResult.Version-like'5.1.*'-and$hostResult.Is64Bit)'Syncro relaunch must use native Windows PowerShell 5.1.'
}
Write-Output"PASS: $script:assertions assertions in Windows PowerShell $($PSVersionTable.PSVersion). No downloads, Office changes, credentials, registry writes, or scheduled tasks touched."
}finally{
$resolved=[IO.Path]::GetFullPath($testRoot)
if(!$resolved.StartsWith([IO.Path]::GetFullPath($env:TEMP).TrimEnd('\')+'\OfficeDeploymentTests-',[StringComparison]::OrdinalIgnoreCase)){throw'Unsafe test cleanup target.'}