Tests/TestHelpers.ps1

# Shared helpers for the Pester suite. Dot-source from BeforeAll in each test file.
# Everything runs against the module's built-in mock tenant; nothing reaches Microsoft Graph.

$script:ModuleRoot = Split-Path -Parent $PSScriptRoot
$script:ModuleManifest = Join-Path $script:ModuleRoot 'IntuneScopeTagManager.psd1'

Import-Module $script:ModuleManifest -Force -ErrorAction Stop

function InSTM {
    # Runs a scriptblock inside the module's scope (private functions, $script:STM, $script:STMMock).
    param([Parameter(Mandatory)][scriptblock]$Script, [object[]]$ArgumentList = @())
    & (Get-Module IntuneScopeTagManager) $Script @ArgumentList
}

function Connect-TestTenant {
    # Fresh demo tenant with its own DataRoot under TestDrive.
    param([int]$DeviceCount = 20, [ValidateSet('ReadOnly', 'ReadWrite')][string]$Mode = 'ReadWrite', [string]$Profile, [string]$Name)
    if (-not $Name) { $Name = [guid]::NewGuid().ToString('N').Substring(0, 8) }
    $root = Join-Path $TestDrive "data-$Name"
    $p = @{ DeviceCount = $DeviceCount; Mode = $Mode; DataRoot = $root }
    if ($Profile) { $p.Profile = $Profile }
    [void](Connect-STMDemo @p)
    return $root
}

function Get-MockState { InSTM { $script:STMMock } }
function Get-STMState { InSTM { $script:STM } }

function Get-MockObjects {
    # Mock objects of one registry type (the live list the mock serves).
    param([Parameter(Mandatory)][string]$Type)
    InSTM { param($t) $def = Get-STMTypeDef -Key $t; @($script:STMMock.Collections[$def.path]) } -ArgumentList $Type
}

function Get-MockTags {
    # Returns the object's tags in the mock tenant as a sorted, comma-joined string ('' when none).
    param([Parameter(Mandatory)][string]$Type, [Parameter(Mandatory)][string]$Id)
    InSTM {
        param($t, $i)
        $def = Get-STMTypeDef -Key $t
        $prop = if ($def.PSObject.Properties['tagProperty']) { $def.tagProperty } else { 'roleScopeTagIds' }
        $o = $script:STMMock.ById["$($def.path)/$i"]
        (@(@($o.$prop) | ForEach-Object { [string]$_ } | Sort-Object) -join ',')
    } -ArgumentList $Type, $Id
}

function Set-MockTags {
    # Sets an object's tags in the mock tenant and drops any loaded copy, so the next plan reads it live.
    param([Parameter(Mandatory)][string]$Type, [Parameter(Mandatory)][string]$Id, [AllowEmptyCollection()][string[]]$Tags = @())
    InSTM {
        param($t, $i, $tags)
        $def = Get-STMTypeDef -Key $t
        $prop = if ($def.PSObject.Properties['tagProperty']) { $def.tagProperty } else { 'roleScopeTagIds' }
        $script:STMMock.ById["$($def.path)/$i"].$prop = @($tags)
        [void]$script:STM.Inventory.Remove("$t|$i")
    } -ArgumentList $Type, $Id, @($Tags)
}

function Reset-MockLog { InSTM { $script:STMMock.Requests.Clear(); $script:STMMock.Writes.Clear() } }

function Get-MockRequests { InSTM { $script:STMMock.Requests.ToArray() } }

function Get-BatchSubRequests {
    # Parses every $batch POST the mock received and returns its sub-requests (one array per batch call).
    $out = [System.Collections.Generic.List[object]]::new()
    foreach ($r in (Get-MockRequests)) {
        if ($r.Method -eq 'POST' -and $r.Uri -match '/\$batch$') { $out.Add(@(($r.Body | ConvertFrom-Json).requests)) }
    }
    , $out
}

function New-Op {
    param([string]$Type, [string]$Id, [string]$Mode, [AllowEmptyCollection()][object[]]$Tags = @(), [string]$Note)
    [pscustomobject]@{ Type = $Type; Id = $Id; Mode = $Mode; Tags = @($Tags); Note = $Note }
}