Tests/Apply.Tests.ps1

#Requires -Modules Pester
# Applying plans: guards, backup-before-write, drift, MAA, failures, read-after-write lag, cancel, result files, audit, rollback.

BeforeAll {
    . (Join-Path $PSScriptRoot 'TestHelpers.ps1')

    function Initialize-ApplyTenant {
        param([string]$Mode = 'ReadWrite')
        $script:root = Connect-TestTenant -Mode $Mode
        InSTM { [void](Update-STMScopeTags); [void](Update-STMRoleAssignments) }
        $script:cat = @(Get-MockObjects -Type DeviceCategory | ForEach-Object id)
        $script:apps = @(Get-MockObjects -Type MobileApp | Where-Object { @($_.roleScopeTagIds) -notcontains '7' } | ForEach-Object id)
    }

    function New-SimplePlan {
        # Two device categories tagged 1 -> add Pilot.
        Set-MockTags -Type DeviceCategory -Id $cat[0] -Tags '1'
        Set-MockTags -Type DeviceCategory -Id $cat[1] -Tags '2', '4'
        New-STMChangePlan -Operations @((New-Op DeviceCategory $cat[0] Add @('Pilot')), (New-Op DeviceCategory $cat[1] Replace @('Region-West')))
    }

    function Get-RunFolder { param($Run) $Run.Folder }
    function Get-Audit { Get-Content (Join-Path $root 'audit.log') | ForEach-Object { $_ | ConvertFrom-Json } }
}

Describe 'Apply guards' {
    BeforeEach { Mock -ModuleName IntuneScopeTagManager Start-Sleep { } }

    It 'refuses to apply in ReadOnly mode and sends no PATCH' {
        Initialize-ApplyTenant -Mode ReadOnly
        $p = New-SimplePlan
        $p.CanApply | Should -BeTrue
        Reset-MockLog
        { Invoke-STMChangePlan -Plan $p -Confirm:$false } | Should -Throw '*read-only*'
        @((Get-MockState).Writes).Count | Should -Be 0
        Test-Path (Join-Path $root 'Runs') | Should -BeFalse
    }

    It 'Invoke-STMChangePlan -WhatIf writes nothing' {
        Initialize-ApplyTenant
        $p = New-SimplePlan
        $r = Invoke-STMChangePlan -Plan $p -WhatIf
        $r.WhatIf | Should -BeTrue
        $r.Ready | Should -Be 2
        @((Get-MockState).Writes).Count | Should -Be 0
    }

    It 'Start-STMRun needs the exact confirmation text' {
        Initialize-ApplyTenant
        $p = New-SimplePlan
        { InSTM { param($p) Start-STMRun -Plan $p -Confirmation 'apply 2' } -ArgumentList $p } | Should -Throw "*Type 'APPLY 2'*"
        { InSTM { param($p) Start-STMRun -Plan $p -Confirmation 'APPLY 3' } -ArgumentList $p } | Should -Throw "*Type 'APPLY 2'*"
    }

    It 'refuses a plan that cannot be applied' {
        Initialize-ApplyTenant
        Set-MockTags -Type DeviceCategory -Id $cat[0] -Tags '7'
        $p = New-STMChangePlan -Operations @(New-Op DeviceCategory $cat[0] Add @('8'))
        { Invoke-STMChangePlan -Plan $p -Confirm:$false } | Should -Throw '*cannot be applied*'
    }

    It 'refuses a plan whose items were changed in memory after it was built' {
        Initialize-ApplyTenant
        $p = New-SimplePlan
        $p.Items[0].After = @('1', '5')
        { Invoke-STMChangePlan -Plan $p -Confirm:$false } | Should -Throw '*fingerprint changed*'
        @((Get-MockState).Writes).Count | Should -Be 0
    }

    It 'refuses a second run while one is in progress' {
        Initialize-ApplyTenant
        $p = New-SimplePlan
        [void](InSTM { param($p) Start-STMRun -Plan $p -Confirmation 'APPLY 2' } -ArgumentList $p)
        { InSTM { param($p) Start-STMRun -Plan $p -Confirmation 'APPLY 2' } -ArgumentList $p } | Should -Throw '*Another run*'
    }

    It 'Regression: a cap-blocked plan edited to CanApply=true is still refused' {
        Initialize-ApplyTenant
        InSTM { $script:STM.Settings.MaxChangesPerRun = 1 }
        $p = New-SimplePlan
        $p.CanApply | Should -BeFalse
        $out = Export-STMChangePlan -Plan $p -Directory (Join-Path $TestDrive 'capplan')
        $j = Get-Content $out.Json -Raw | ConvertFrom-Json
        $j.CanApply = $true; $j.PlanIssues = @()
        $j | ConvertTo-Json -Depth 10 | Set-Content $out.Json
        { $q = Import-STMChangePlan -Path $out.Json; Invoke-STMChangePlan -Plan $q -Confirm:$false } | Should -Throw
        @((Get-MockState).Writes).Count | Should -Be 0
    }
}

Describe 'A successful run' {
    BeforeAll {
        Mock -ModuleName IntuneScopeTagManager Start-Sleep { }
        Initialize-ApplyTenant
        $script:plan = New-SimplePlan
        # Record which run files exist at the moment the first write leaves the client.
        InSTM {
            param($root)
            $script:FilesAtFirstWrite = $null
            $script:TestRoot = $root
            $script:STM.MockHandler = {
                param($m, $u, $b)
                if ($null -eq $script:FilesAtFirstWrite -and ($m -eq 'PATCH' -or ($u -match '\$batch$' -and $b -match '"method":"PATCH"'))) {
                    $script:FilesAtFirstWrite = @(Get-ChildItem -Path (Join-Path $script:TestRoot 'Runs') -Recurse -File | ForEach-Object Name)
                }
                Invoke-STMMockRequest -Method $m -Uri $u -Body $b
            }
        } -ArgumentList $root
        $script:run = Invoke-STMChangePlan -Plan $plan -Confirm:$false
    }

    It 'applies and verifies every Ready item' {
        $run.Done | Should -BeTrue
        $run.Counts.Applied | Should -Be 2
        $run.Counts.Failed | Should -Be 0
        Get-MockTags -Type DeviceCategory -Id $cat[0] | Should -Be '1,8'
        Get-MockTags -Type DeviceCategory -Id $cat[1] | Should -Be '5'
        @($run.Results | ForEach-Object Detail) | Should -Be @('Written and verified', 'Written and verified')
    }

    It 'wrote plan.json, backup.json and rollback.csv before the first PATCH' {
        $files = InSTM { $script:FilesAtFirstWrite }
        $files | Should -Contain 'plan.json'
        $files | Should -Contain 'backup.json'
        $files | Should -Contain 'rollback.csv'
    }

    It 'backup.json and rollback.csv hold the before state' {
        $backup = Get-Content (Join-Path $run.Folder 'backup.json') -Raw | ConvertFrom-Json
        @($backup).Count | Should -Be 2
        (($backup | Where-Object Id -eq $cat[1]).Before) -join ',' | Should -Be '2,4'
        $rb = @(Import-Csv (Join-Path $run.Folder 'rollback.csv'))
        $rb.Count | Should -Be 2
        ($rb | Where-Object ObjectId -eq $cat[1]).ScopeTags | Should -Be '2;4'
        ($rb | Where-Object ObjectId -eq $cat[0]).Mode | Should -Be 'Replace'
        @($rb[0].PSObject.Properties.Name) | Should -Be @('ObjectType', 'ObjectId', 'DisplayName', 'ScopeTags', 'Mode', 'Notes')
    }

    It 'writes results.csv and results.json' {
        $csv = @(Import-Csv (Join-Path $run.Folder 'results.csv'))
        $csv.Count | Should -Be 2
        @($csv | ForEach-Object Result) | Should -Be @('Applied', 'Applied')
        $json = @(Get-Content (Join-Path $run.Folder 'results.json') -Raw | ConvertFrom-Json)
        $json.Count | Should -Be 2
        ($json | Where-Object Id -eq $cat[0]).Planned | Should -Be '1;8'
        Test-Path (Join-Path $run.Folder 'run.log') | Should -BeTrue
    }

    It 'wrote each object exactly once' {
        $w = @((Get-MockState).Writes)
        $w.Count | Should -Be 2
        @($w.Path | Sort-Object -Unique).Count | Should -Be 2
    }

    It 'logs RunStarted and RunFinished to audit.log' {
        $a = @(Get-Audit)
        $start = @($a | Where-Object event -eq 'RunStarted')
        $end = @($a | Where-Object event -eq 'RunFinished')
        $start.Count | Should -Be 1
        $end.Count | Should -Be 1
        $start[0].runId | Should -Be $run.RunId
        $start[0].planHash | Should -Be $plan.Hash
        $end[0].counts.Applied | Should -Be 2
        $start[0].tenant | Should -Be '11111111-2222-4333-8444-000000000de0'
    }

    It 'shows up in Get-STMRun with a rollback file' {
        $h = Get-STMRun -RunId $run.RunId
        $h.Applied | Should -Be 2
        $h.HasRollback | Should -BeTrue
    }
}

Describe 'PATCH body' {
    BeforeEach { Mock -ModuleName IntuneScopeTagManager Start-Sleep { }; Initialize-ApplyTenant }

    It 'includes @odata.type for PatchWithODataType types and nothing else' {
        $dc = (Get-MockObjects -Type DeviceConfiguration | Where-Object { $_.supportsScopeTags -and @($_.roleScopeTagIds) -notcontains '7' })[0]
        Set-MockTags -Type DeviceConfiguration -Id $dc.id -Tags '1'
        Reset-MockLog
        $run = Invoke-STMChangePlan -Plan (New-STMChangePlan -Operations @(New-Op DeviceConfiguration $dc.id Add @('8'))) -Confirm:$false
        $run.Counts.Applied | Should -Be 1
        $patch = @(Get-MockRequests | Where-Object Method -eq 'PATCH')
        $patch.Count | Should -Be 1
        $body = $patch[0].Body | ConvertFrom-Json
        @($body.PSObject.Properties.Name | Sort-Object) | Should -Be @('@odata.type', 'roleScopeTagIds')
        $body.'@odata.type' | Should -Be $dc.'@odata.type'
    }

    It 'uses roleScopeTags for assignment filters' {
        $f = (Get-MockObjects -Type AssignmentFilter | Where-Object { @($_.roleScopeTags) -notcontains '7' })[0]
        Set-MockTags -Type AssignmentFilter -Id $f.id -Tags '1'
        Reset-MockLog
        $run = Invoke-STMChangePlan -Plan (New-STMChangePlan -Operations @(New-Op AssignmentFilter $f.id Add @('8'))) -Confirm:$false
        $run.Counts.Applied | Should -Be 1
        (@(Get-MockRequests | Where-Object Method -eq 'PATCH')[0].Body | ConvertFrom-Json).PSObject.Properties.Name | Should -Be @('roleScopeTags')
        Get-MockTags -Type AssignmentFilter -Id $f.id | Should -Be '1,8'
    }
}

Describe 'Run outcomes' {
    BeforeEach { Mock -ModuleName IntuneScopeTagManager Start-Sleep { }; Initialize-ApplyTenant }

    It 'skips an object that changed since the plan was built (drift)' {
        $p = New-SimplePlan
        Set-MockTags -Type DeviceCategory -Id $cat[0] -Tags '1', '2'
        $run = Invoke-STMChangePlan -Plan $p -Confirm:$false
        $r = $run.Results | Where-Object Id -eq $cat[0]
        $r.Result | Should -Be 'SkippedDrift'
        $r.Detail | Should -Match 'Changed since the plan was built'
        Get-MockTags -Type DeviceCategory -Id $cat[0] | Should -Be '1,2'
        ($run.Results | Where-Object Id -eq $cat[1]).Result | Should -Be 'Applied'
        @((Get-MockState).Writes).Count | Should -Be 1
    }

    It 'reports NoChange when the object already has the planned tags' {
        $p = New-SimplePlan
        Set-MockTags -Type DeviceCategory -Id $cat[0] -Tags '1', '8'
        $run = Invoke-STMChangePlan -Plan $p -Confirm:$false
        ($run.Results | Where-Object Id -eq $cat[0]).Result | Should -Be 'NoChange'
    }

    It 'skips an object that gained the protected tag since the plan' {
        $p = New-SimplePlan
        Set-MockTags -Type DeviceCategory -Id $cat[0] -Tags '1', '7'
        $run = Invoke-STMChangePlan -Plan $p -Confirm:$false
        ($run.Results | Where-Object Id -eq $cat[0]).Result | Should -Be 'SkippedProtected'
        Get-MockTags -Type DeviceCategory -Id $cat[0] | Should -Be '1,7'
    }

    It 'MaaIds -> Failed with a Multi Admin Approval explanation' {
        $p = New-SimplePlan
        InSTM { param($id) $script:STMMock.Options.MaaIds[$id] = $true } -ArgumentList $cat[0]
        $run = Invoke-STMChangePlan -Plan $p -Confirm:$false
        $r = $run.Results | Where-Object Id -eq $cat[0]
        $r.Result | Should -Be 'Failed'
        $r.Detail | Should -Match 'MultiAdminApprovalRequired'
        $r.Detail | Should -Match 'request approval in the Intune admin center'
        Get-MockTags -Type DeviceCategory -Id $cat[0] | Should -Be '1'
        ($run.Results | Where-Object Id -eq $cat[1]).Result | Should -Be 'Applied'
    }

    It 'FailIds (400) -> Failed without retries' {
        $p = New-SimplePlan
        InSTM { param($id) $script:STMMock.Options.FailIds[$id] = 400 } -ArgumentList $cat[0]
        Reset-MockLog
        $run = Invoke-STMChangePlan -Plan $p -Confirm:$false
        $r = $run.Results | Where-Object Id -eq $cat[0]
        $r.Result | Should -Be 'Failed'
        $r.Detail | Should -Match 'HTTP 400'
        @(Get-MockRequests | Where-Object { $_.Method -eq 'PATCH' -and $_.Uri -like "*$($cat[0])" }).Count | Should -Be 1
        $run.Counts.Failed | Should -Be 1
        $run.Counts.Applied | Should -Be 1
    }

    It 'FailIds (500) -> retried, then Failed' {
        $p = New-SimplePlan
        InSTM { param($id) $script:STMMock.Options.FailIds[$id] = 500 } -ArgumentList $cat[0]
        Reset-MockLog
        $run = Invoke-STMChangePlan -Plan $p -Confirm:$false
        ($run.Results | Where-Object Id -eq $cat[0]).Result | Should -Be 'Failed'
        ($run.Results | Where-Object Id -eq $cat[0]).Detail | Should -Match 'HTTP 500'
        @(Get-MockRequests | Where-Object { $_.Method -eq 'PATCH' -and $_.Uri -like "*$($cat[0])" }).Count | Should -Be 6
        @(Get-MockRequests | Where-Object { $_.Method -eq 'PATCH' -and $_.Uri -like "*$($cat[1])" }).Count | Should -Be 1
    }

    It 'LagIds -> applied-not-yet-confirmed after the batch, Applied after the final verify' {
        $ids = @($apps | Select-Object -First 21)
        foreach ($i in $ids) { Set-MockTags -Type MobileApp -Id $i -Tags '1' }
        $p = New-STMChangePlan -Operations @($ids | ForEach-Object { New-Op MobileApp $_ Add @('8') })
        # Reads: the drift check (served the old tags, which match the plan) and the post-write verify (stale) lag;
        # the final verify sees the new tags.
        InSTM { param($id) $script:STMMock.Options.LagIds[$id] = @{ Reads = 2; Old = @('1') } } -ArgumentList $ids[0]
        $v = InSTM { param($p) Start-STMRun -Plan $p -Confirmation 'APPLY 21' } -ArgumentList $p
        $v = InSTM { param($id) Step-STMRun -RunId $id } -ArgumentList $v.RunId
        $v.Done | Should -BeFalse
        $mid = $v.Results | Where-Object Id -eq $ids[0]
        $mid.Result | Should -Be 'AppliedUnverified'
        $mid.Detail | Should -Match 'Final verification runs at the end'
        $v.Counts.AppliedUnverified | Should -Be 1
        $v.Counts.Applied | Should -Be 19
        $v = InSTM { param($id) Step-STMRun -RunId $id } -ArgumentList $v.RunId
        $v.Done | Should -BeTrue
        $fin = $v.Results | Where-Object Id -eq $ids[0]
        $fin.Result | Should -Be 'Applied'
        $fin.Detail | Should -Match 'attempt 1'
        $v.Counts.Applied | Should -Be 21
        $v.Counts.AppliedUnverified | Should -Be 0
        Should -Invoke Start-Sleep -ModuleName IntuneScopeTagManager -ParameterFilter { $Seconds -eq 3 } -Times 1 -Exactly
        (@(Get-Content (Join-Path $v.Folder 'results.json') -Raw | ConvertFrom-Json) | Where-Object Id -eq $ids[0]).Result | Should -Be 'Applied'
    }

    It 'LagIds that never settle stay AppliedUnverified after the final verify attempts' {
        $p = New-SimplePlan
        InSTM { param($id) $script:STMMock.Options.LagIds[$id] = @{ Reads = 100; Old = @('1') } } -ArgumentList $cat[0]
        $run = Invoke-STMChangePlan -Plan $p -Confirm:$false
        $r = $run.Results | Where-Object Id -eq $cat[0]
        $r.Result | Should -Be 'AppliedUnverified'
        $r.Detail | Should -Match 'read-back did not match yet'
        $run.Counts.AppliedUnverified | Should -Be 1
        Should -Invoke Start-Sleep -ModuleName IntuneScopeTagManager -ParameterFilter { $Seconds -eq 3 } -Times 3 -Exactly
        Get-MockTags -Type DeviceCategory -Id $cat[0] | Should -Be '1,8'
    }
}

Describe 'Batches, stop and cancel' {
    BeforeEach {
        Mock -ModuleName IntuneScopeTagManager Start-Sleep { }
        Initialize-ApplyTenant
        $script:ids = @($apps | Select-Object -First 25)
        foreach ($i in $ids) { Set-MockTags -Type MobileApp -Id $i -Tags '1' }
        $script:plan = New-STMChangePlan -Operations @($ids | ForEach-Object { New-Op MobileApp $_ Add @('8') })
    }

    It 'processes 20 objects per step' {
        $plan.Summary.Ready | Should -Be 25
        $v = InSTM { param($p) Start-STMRun -Plan $p -Confirmation 'APPLY 25' } -ArgumentList $plan
        $v.Processed | Should -Be 0
        $v = InSTM { param($id) Step-STMRun -RunId $id } -ArgumentList $v.RunId
        $v.Processed | Should -Be 20
        $v.Done | Should -BeFalse
        @((Get-MockState).Writes).Count | Should -Be 20
        # Results are saved after every batch, not just at the end.
        @(Import-Csv (Join-Path $v.Folder 'results.csv')).Count | Should -Be 20
        $v = InSTM { param($id) Step-STMRun -RunId $id } -ArgumentList $v.RunId
        $v.Done | Should -BeTrue
        $v.Counts.Applied | Should -Be 25
    }

    It 'stopping mid-run marks the rest NotRun and writes nothing more' {
        $v = InSTM { param($p) Start-STMRun -Plan $p -Confirmation 'APPLY 25' } -ArgumentList $plan
        $v = InSTM { param($id) Step-STMRun -RunId $id } -ArgumentList $v.RunId
        $v = InSTM { param($id) Stop-STMRun -RunId $id } -ArgumentList $v.RunId
        $v.Done | Should -BeTrue
        $v.Cancelled | Should -BeTrue
        $v.Counts.Applied | Should -Be 20
        $v.Counts.NotRun | Should -Be 5
        @((Get-MockState).Writes).Count | Should -Be 20
        foreach ($i in $ids[20..24]) { Get-MockTags -Type MobileApp -Id $i | Should -Be '1' }
        # Further steps do nothing.
        $v2 = InSTM { param($id) Step-STMRun -RunId $id } -ArgumentList $v.RunId
        @((Get-MockState).Writes).Count | Should -Be 20
        $fin = @(Get-Content (Join-Path $root 'audit.log') | ForEach-Object { $_ | ConvertFrom-Json } | Where-Object event -eq 'RunFinished')
        $fin[-1].cancelled | Should -BeTrue
        @(Import-Csv (Join-Path $v.Folder 'results.csv') | Where-Object Result -eq 'NotRun').Count | Should -Be 5
    }

    It 'a cancelled run can be rolled back for the part that ran' {
        $v = InSTM { param($p) Start-STMRun -Plan $p -Confirmation 'APPLY 25' } -ArgumentList $plan
        $v = InSTM { param($id) Step-STMRun -RunId $id } -ArgumentList $v.RunId
        $v = InSTM { param($id) Stop-STMRun -RunId $id } -ArgumentList $v.RunId
        $undo = Undo-STMRun -RunId $v.RunId
        $undo.Summary.Ready | Should -Be 20
    }
}

Describe 'Undo-STMRun' {
    BeforeEach { Mock -ModuleName IntuneScopeTagManager Start-Sleep { }; Initialize-ApplyTenant }

    It 'restores the original tags exactly' {
        Set-MockTags -Type DeviceCategory -Id $cat[0] -Tags '1'
        Set-MockTags -Type DeviceCategory -Id $cat[1] -Tags '2', '4'
        Set-MockTags -Type DeviceCategory -Id $cat[2] -Tags '0', '3'
        $orig = @{ }; foreach ($i in $cat[0..2]) { $orig[$i] = Get-MockTags -Type DeviceCategory -Id $i }
        $p = New-STMChangePlan -Operations @(
            (New-Op DeviceCategory $cat[0] Add @('Pilot')),
            (New-Op DeviceCategory $cat[1] Replace @('Region-West')),
            (New-Op DeviceCategory $cat[2] Remove @('Default')))
        $run = Invoke-STMChangePlan -Plan $p -Confirm:$false
        $run.Counts.Applied | Should -Be 3
        Get-MockTags -Type DeviceCategory -Id $cat[2] | Should -Be '3'

        $undo = Undo-STMRun -RunId $run.RunId
        $undo.CanApply | Should -BeTrue
        $undo.Summary.Ready | Should -Be 3
        $undo.Title | Should -Match ([regex]::Escape($run.RunId))
        $back = Invoke-STMChangePlan -Plan $undo -Confirm:$false
        $back.Counts.Applied | Should -Be 3
        foreach ($i in $cat[0..2]) { Get-MockTags -Type DeviceCategory -Id $i | Should -Be $orig[$i] -Because $i }
    }

    It 'Regression: restores an object that originally had no explicit tags' {
        Set-MockTags -Type DeviceCategory -Id $cat[0] -Tags @()
        $run = Invoke-STMChangePlan -Plan (New-STMChangePlan -Operations @(New-Op DeviceCategory $cat[0] Add @('Corp-Windows'))) -Confirm:$false
        $run.Counts.Applied | Should -Be 1
        $back = Invoke-STMChangePlan -Plan (Undo-STMRun -RunId $run.RunId) -Confirm:$false
        $back.Counts.Applied | Should -Be 1
        Get-MockTags -Type DeviceCategory -Id $cat[0] | Should -Be ''
    }

    It 'Regression: Replace with no tags (-AllowNoExplicitTags) sends an empty tag list' {
        Set-MockTags -Type DeviceCategory -Id $cat[0] -Tags '1'
        Reset-MockLog
        $run = Invoke-STMChangePlan -Plan (New-STMChangePlan -Operations @(New-Op DeviceCategory $cat[0] Replace @()) -AllowNoExplicitTags) -Confirm:$false
        @(Get-MockRequests | Where-Object Method -eq 'PATCH')[0].Body | Should -Be '{"roleScopeTagIds":[]}'
        $run.Counts.Applied | Should -Be 1
        Get-MockTags -Type DeviceCategory -Id $cat[0] | Should -Be ''
    }

    It 'leaves out objects the run did not change' {
        $p = New-SimplePlan
        InSTM { param($id) $script:STMMock.Options.FailIds[$id] = 400 } -ArgumentList $cat[0]
        $run = Invoke-STMChangePlan -Plan $p -Confirm:$false
        $undo = Undo-STMRun -RunId $run.RunId
        @($undo.Items).Count | Should -Be 1
        $undo.Items[0].Id | Should -Be $cat[1]
    }

    It 'throws when the run changed nothing' {
        $p = New-SimplePlan
        InSTM { param($a, $b) $script:STMMock.Options.FailIds[$a] = 400; $script:STMMock.Options.FailIds[$b] = 400 } -ArgumentList $cat[0], $cat[1]
        $run = Invoke-STMChangePlan -Plan $p -Confirm:$false
        { Undo-STMRun -RunId $run.RunId } | Should -Throw '*changed nothing*'
    }
}