# Tests\ConfigService.Tests.ps1 # Unit Tests for ConfigService Module BeforeAll { $scriptDir = Split-Path -Parent $PSScriptRoot . (Join-Path $scriptDir "Modules\ConfigService.ps1") # Backup original config path and existing config $script:originalConfigPath = $global:AppConfigPath $testConfigDir = Join-Path ([System.IO.Path]::GetTempPath()) "ModernAdminStudioTests_$([System.Guid]::NewGuid().ToString('N'))" New-Item -ItemType Directory -Path $testConfigDir -Force | Out-Null } AfterAll { # Restore original config path $global:AppConfigPath = $script:originalConfigPath if (Test-Path $testConfigDir) { Remove-Item -Path $testConfigDir -Recurse -Force -ErrorAction SilentlyContinue } } Describe "ConfigService Unit Tests" -Tag "Mock", "Unit" { Context "Default Configuration Generation" { BeforeEach { $global:AppConfigPath = Join-Path $testConfigDir "new_config_$([System.Guid]::NewGuid().ToString('N')).json" if (Test-Path $global:AppConfigPath) { Remove-Item $global:AppConfigPath -Force } } It "generates a default configuration file if not present" { Test-Path $global:AppConfigPath | Should -BeFalse $cfg = Get-AppConfig Test-Path $global:AppConfigPath | Should -BeTrue $cfg | Should -Not -BeNullOrEmpty } It "includes all 4 required service sections in default config" { $cfg = Get-AppConfig $cfg.Application | Should -Not -BeNullOrEmpty $cfg.ActiveDirectory | Should -Not -BeNullOrEmpty $cfg.NetApp | Should -Not -BeNullOrEmpty $cfg.MsSql | Should -Not -BeNullOrEmpty } It "sets LiveMode to false by default for safe local development" { $cfg = Get-AppConfig $cfg.Application.LiveMode | Should -BeFalse $cfg.ActiveDirectory.UseLive | Should -BeFalse $cfg.NetApp.UseLive | Should -BeFalse $cfg.MsSql.UseLive | Should -BeFalse } It "contains valid default connection parameters" { $cfg = Get-AppConfig $cfg.ActiveDirectory.DomainController | Should -Be "dc01.corp.local" $cfg.ActiveDirectory.BaseOU | Should -Be "DC=corp,DC=local" $cfg.NetApp.DefaultSvm | Should -Be "svm_cifs_prod01" $cfg.MsSql.DefaultDatabase | Should -Be "master" } } Context "Configuration Loading & Persistence" { BeforeEach { $global:AppConfigPath = Join-Path $testConfigDir "persist_config.json" $dummyConfig = @{ Application = @{ LiveMode = $false Theme = "Dark" AutoRefreshSeconds = 45 } ActiveDirectory = @{ UseLive = $false DomainController = "dc-test.corp.local" Domain = "corp.local" BaseOU = "DC=corp,DC=local" UseLdaps = $true Username = "admin_test" Password = "SecretPassword123!" } NetApp = @{ UseLive = $false ClusterEndpoint = "https://10.200.10.99" Port = 443 DefaultSvm = "svm_test01" Username = "netapp_admin" Password = "" IgnoreSslErrors = $true } MsSql = @{ UseLive = $false Server = "sql-cluster.corp.local" AuthType = "AdImpersonated" Domain = "CORP" Username = "svc_sql_admin" Password = "SqlPassword456!" DefaultDatabase = "PROD_ERP" ConnectionTimeout = 15 BackupDirectory = "D:\SqlBackups" } } $json = $dummyConfig | ConvertTo-Json -Depth 6 [System.IO.File]::WriteAllText($global:AppConfigPath, $json, [System.Text.Encoding]::UTF8) } It "loads existing json configuration accurately" { $loaded = Get-AppConfig $loaded.ActiveDirectory.DomainController | Should -Be "dc-test.corp.local" $loaded.ActiveDirectory.UseLdaps | Should -BeTrue $loaded.MsSql.AuthType | Should -Be "AdImpersonated" $loaded.MsSql.DefaultDatabase | Should -Be "PROD_ERP" } It "saves modified configuration and persists changes to disk" { $loaded = Get-AppConfig $loaded.Application.AutoRefreshSeconds = 120 $loaded.NetApp.DefaultSvm = "svm_updated_svm" $saveResult = Save-AppConfig -Config $loaded $saveResult | Should -BeTrue # Reload and verify $reloaded = Get-AppConfig $reloaded.Application.AutoRefreshSeconds | Should -Be 120 $reloaded.NetApp.DefaultSvm | Should -Be "svm_updated_svm" } It "handles corrupt json gracefully without throwing unhandled exceptions" { [System.IO.File]::WriteAllText($global:AppConfigPath, "{ Invalid Json Content ::::", [System.Text.Encoding]::UTF8) $res = Get-AppConfig $res | Should -BeNullOrEmpty } } }