$repositoryRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path $wallpaperScript = Join-Path $repositoryRoot 'scripts\Set-SguWelcomeWallpaper.ps1' $source = Get-Content -LiteralPath $wallpaperScript -Raw $tokens = $null $parseErrors = $null $ast = [Management.Automation.Language.Parser]::ParseFile($wallpaperScript, [ref]$tokens, [ref]$parseErrors) if ($parseErrors.Count) { throw ($parseErrors -join [Environment]::NewLine) } $lookup = $ast.Find({ param($node) $node -is [Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Get-DirectoryWelcomeMetadata' }, $true) function Invoke-WelcomeFixture { param($DirectoryGender, [string]$ExplicitGender) # Replace only the external directory lookup. Execute the actual script, # including its validated parameters, metadata assignment and JPEG renderer. $fixtureJson = [pscustomobject]@{ DisplayName = 'Usuario de prueba' Gender = $DirectoryGender Location = 'Sala de pruebas' OrganizationalUnit = 'Laboratorio' } | ConvertTo-Json -Compress $fixtureFunction = 'function Get-DirectoryWelcomeMetadata { param($UserName, $MachineName); ConvertFrom-Json ''' + $fixtureJson.Replace("'", "''") + ''' }' $fixtureSource = $source.Remove($lookup.Extent.StartOffset, $lookup.Extent.EndOffset - $lookup.Extent.StartOffset). Insert($lookup.Extent.StartOffset, $fixtureFunction) $testScript = Join-Path $TestDrive ('wallpaper-' + [Guid]::NewGuid().ToString('N') + '.ps1') [IO.File]::WriteAllText($testScript, $fixtureSource, [Text.UTF8Encoding]::new($false)) $parameters = @{ BaseImagePath = Join-Path $repositoryRoot 'assets\branding\darkblue.jpg' FontsPath = Join-Path $repositoryRoot 'assets\branding\fonts' OutputPath = Join-Path $TestDrive ([IO.Path]::GetFileNameWithoutExtension($testScript) + '.jpg') CanvasWidth = 640 CanvasHeight = 480 SkipApply = $true } if ($ExplicitGender) { $parameters.Gender = $ExplicitGender } $previousLocalAppData = $env:LOCALAPPDATA try { $env:LOCALAPPDATA = $TestDrive & $testScript @parameters } finally { $env:LOCALAPPDATA = $previousLocalAppData } } Describe 'Welcome wallpaper with AD metadata' { It 'renders a neutral JPEG when AD has no gender' { $result = Invoke-WelcomeFixture -DirectoryGender $null $result.WelcomeHeading | Should Be 'Te damos la bienvenida,' $result.Applied | Should Be $false $bitmap = [Drawing.Image]::FromFile($result.OutputPath) try { $bitmap.Width | Should Be 640; $bitmap.Height | Should Be 480 } finally { $bitmap.Dispose() } } It 'uses neutral wording for empty or unrecognized metadata' { foreach ($value in @('', 'Unknown')) { (Invoke-WelcomeFixture -DirectoryGender $value).WelcomeHeading | Should Be 'Te damos la bienvenida,' } } It 'keeps the gendered greetings for recognized directory values' { (Invoke-WelcomeFixture -DirectoryGender 'Female').WelcomeHeading | Should Be 'Bienvenida,' (Invoke-WelcomeFixture -DirectoryGender 'Male').WelcomeHeading | Should Be 'Bienvenido,' } It 'honors an explicit gender over directory metadata' { (Invoke-WelcomeFixture -DirectoryGender 'Female' -ExplicitGender 'Male').WelcomeHeading | Should Be 'Bienvenido,' } }