Refactor feature label handling to utilize language catalogs (#890)

This commit is contained in:
Jeffrey
2026-11-08 17:55:46 +01:00
committed by GitHub
parent cf691cda94
commit e6184a2597
29 changed files with 173 additions and 677 deletions
+34
View File
@@ -23,6 +23,40 @@ BeforeAll {
}
Describe 'FeatureId parameter contracts' {
It 'keeps localized text out of feature definitions and uses category names for membership' {
$config = Get-Content -LiteralPath $script:FeaturesPath -Raw | ConvertFrom-Json
$objects = @($config.Categories) + @($config.Features) + @($config.UiGroups) + @($config.UiGroups.Values)
foreach ($item in $objects) {
foreach ($field in 'Label', 'ToolTip', 'ApplyText', 'UndoLabel', 'ApplyUndoText') {
$item.PSObject.Properties.Name | Should -Not -Contain $field
}
}
foreach ($item in (@($config.Features) + @($config.UiGroups) | Where-Object { $_.Category })) {
$config.Categories.Name | Should -Contain $item.Category
}
}
It 'has English text for feature operations, groups, options, and categories directly in the language catalogs' {
$config = Get-Content -LiteralPath $script:FeaturesPath -Raw | ConvertFrom-Json
$catalog = Get-Content -LiteralPath (Join-Path $script:RepoRoot 'Config\Languages\en-US\Features.json') -Raw | ConvertFrom-Json
$categories = Get-Content -LiteralPath (Join-Path $script:RepoRoot 'Config\Languages\en-US\Categories.json') -Raw | ConvertFrom-Json
foreach ($feature in $config.Features) {
$entry = $catalog.Features.($feature.FeatureId)
$entry.ApplyText | Should -Not -BeNullOrEmpty -Because $feature.FeatureId
if ($feature.RegistryUndoKey -or $feature.FeatureId -in @('EnableWindowsSandbox', 'EnableWindowsSubsystemForLinux', 'DisableStoreSearchSuggestions')) {
$entry.UndoLabel | Should -Not -BeNullOrEmpty -Because $feature.FeatureId
$entry.ApplyUndoText | Should -Not -BeNullOrEmpty -Because $feature.FeatureId
}
}
foreach ($group in $config.UiGroups) {
$entry = $catalog.UiGroups.($group.GroupId)
$entry.Label | Should -Not -BeNullOrEmpty
$entry.ToolTip | Should -Not -BeNullOrEmpty
foreach ($value in $group.Values) { $entry.Values.($value.FeatureIds[0]) | Should -Not -BeNullOrEmpty }
}
foreach ($category in $config.Categories) { $categories.($category.CategoryId).Label | Should -Not -BeNullOrEmpty }
}
It 'declares every FeatureId as a parameter on Win11Debloat.ps1' {
$parameterNames = Get-ScriptParameterNames -Path $script:Win11DebloatPath
$missing = @($script:Features.FeatureId | Where-Object { $parameterNames -notcontains $_ })
+8 -4
View File
@@ -100,6 +100,10 @@ Describe 'Get-RebootFeatureLabels' {
}
It 'includes reboot-required selections once and uses undo labels for undo operations' {
foreach ($feature in $script:Features.Values) {
$feature.PSObject.Properties.Remove('Label')
$feature.PSObject.Properties.Remove('UndoLabel')
}
$result = @(Get-RebootFeatureLabels)
$result | Should -HaveCount 2
@@ -118,7 +122,7 @@ Describe 'Get-RebootFeatureLabels' {
$result | Should -Contain 'Apply feature'
}
It 'falls back to the regular label when an undo selection has no undo label' {
It 'uses the translation key when an undo selection has no undo label' {
$script:Params = @{}
$script:UndoParams = @{ ApplyFeature = $true }
$script:Features.ApplyFeature.UndoLabel = $null
@@ -127,10 +131,10 @@ Describe 'Get-RebootFeatureLabels' {
$result = @(Get-RebootFeatureLabels)
$result | Should -HaveCount 1
$result | Should -Contain 'Apply feature'
$result | Should -Contain 'ApplyFeature'
}
It 'falls back to the regular label when the feature has no UndoLabel property' {
It 'uses the translation key when the feature has no UndoLabel property' {
$script:Params = @{}
$script:UndoParams = @{ MissingUndoLabelFeature = $true }
$script:Features.MissingUndoLabelFeature = [PSCustomObject]@{
@@ -142,7 +146,7 @@ Describe 'Get-RebootFeatureLabels' {
$result = @(Get-RebootFeatureLabels)
$result | Should -HaveCount 1
$result | Should -Contain 'Feature without undo label'
$result | Should -Contain 'MissingUndoLabelFeature'
}
It 'returns no labels when there are no selected parameters' {
+9 -1
View File
@@ -35,6 +35,7 @@ BeforeAll {
$entry = @{}
foreach ($field in 'ApplyText', 'UndoLabel', 'ApplyUndoText') {
if ($null -ne $feature.$field) { $entry[$field] = [string]$feature.$field }
$feature.PSObject.Properties.Remove($field)
}
$langFeatures[$featureId] = [PSCustomObject]$entry
}
@@ -112,7 +113,7 @@ Describe 'Invoke-FeatureApply' {
It 'imports a registry-backed feature' {
Invoke-FeatureApply -FeatureId 'RegistryFeature'
Should -Invoke Import-RegistryFile -Times 1 -Exactly -ParameterFilter { $path -eq 'feature.reg' }
Should -Invoke Import-RegistryFile -Times 1 -Exactly -ParameterFilter { $path -eq 'feature.reg' -and $Message -eq '> Apply registry feature...' }
Should -Invoke Remove-SelectedApps -Times 0 -Exactly
}
@@ -352,6 +353,7 @@ Describe 'Invoke-FeatureUndo' {
DisableTelemetry = [PSCustomObject]@{}
DisableStoreSearchSuggestions = [PSCustomObject]@{}
}
Sync-TestLangFeatures
Mock Set-StoreSearchSuggestionsEnabledForAllUsers { $true }
Mock Set-StoreSearchSuggestionsEnabled { $true }
Mock Get-StoreAppsDatabasePathForUser { 'store.db' }
@@ -380,6 +382,12 @@ Describe 'Invoke-FeatureUndo' {
$script:disabledFeatures | Should -Be @('Microsoft-Windows-Subsystem-Linux', 'VirtualMachinePlatform')
}
It 'uses the language catalog for undo messages without display metadata' {
Invoke-FeatureUndo -FeatureId 'EnableWindowsSandbox' | Should -BeTrue
Should -Invoke Write-Host -Times 1 -Exactly -ParameterFilter { $Object -eq '> Disable Sandbox...' }
}
It 'disables Sandbox and re-enables telemetry tasks' {
$script:Features.DisableTelemetry = [PSCustomObject]@{ ApplyUndoText = 'Enable telemetry'; RegistryUndoKey = 'enable-telemetry.reg' }
Invoke-FeatureUndo -FeatureId 'EnableWindowsSandbox'
+1 -11
View File
@@ -37,17 +37,7 @@ BeforeAll {
}
}
Describe 'Get-UndoFeatureLabel' {
BeforeEach {
$script:UndoFeatureLabelLookup = @{ DisableTelemetry = 'Enable telemetry' }
$script:FeatureLabelLookup = @{ DisableTelemetry = 'Disable telemetry'; DisableWidgets = 'Disable widgets' }
}
It 'prefers undo labels and falls back to feature labels' {
Get-UndoFeatureLabel -FeatureId 'DisableTelemetry' | Should -Be 'Enable telemetry'
Get-UndoFeatureLabel -FeatureId 'DisableWidgets' | Should -Be 'Disable widgets'
}
Describe 'Main window helpers' {
It 'reads selected app IDs from string or array settings and removes blanks' {
$stringSettings = [PSCustomObject]@{ Settings = @([PSCustomObject]@{ Name = 'Apps'; Value = ' One.App, ,Two.App ' }) }
$arraySettings = [PSCustomObject]@{ Settings = @([PSCustomObject]@{ Name = 'Apps'; Value = @(' One.App ', '', 'Two.App') }) }
+6 -6
View File
@@ -43,13 +43,13 @@ BeforeAll {
}
}
Describe 'Legacy tweak categories' {
It 'uses the category Name as CategoryId when building controls without a CategoryId' {
Describe 'Tweak category identifiers' {
It 'uses the defined CategoryId for controls and presets when it differs from Name' {
$script:Lang = Import-LanguageFile -LanguageCode 'en-US'
$feature = $script:Features['DisableTelemetry'] | Select-Object *
$feature.Category = 'Legacy Category'
$legacyCatalog = [PSCustomObject]@{
Categories = @([PSCustomObject]@{ Name = 'Legacy Category'; Icon = '' })
Categories = @([PSCustomObject]@{ Name = 'Legacy Category'; CategoryId = 'PrivacySuggestedContent'; Icon = '' })
Features = @($feature)
}
Mock Import-JsonFile { $legacyCatalog } -ParameterFilter { $filePath -eq $script:FeaturesFilePath }
@@ -59,8 +59,8 @@ Describe 'Legacy tweak categories' {
$controlName = 'Feature_DisableTelemetry_Combo'
$script:UiControlMappings.Count | Should -Be 1
$script:UiControlMappings[$controlName].CategoryId | Should -Be 'Legacy Category'
$map = Get-CategoryTweakPresetMap -Window $window -CategoryId 'Legacy Category'
$script:UiControlMappings[$controlName].CategoryId | Should -Be 'PrivacySuggestedContent'
$map = Get-CategoryTweakPresetMap -Window $window -CategoryId 'PrivacySuggestedContent'
$map.Count | Should -Be 1
$map[$controlName].Control | Should -Be ($window.FindName($controlName))
$map[$controlName].DesiredValue | Should -BeTrue
@@ -175,7 +175,7 @@ Describe 'Language changes on existing controls' {
$disabled.ToolTip.Text | Should -Be (Get-Translation -Key 'TweaksAlreadyAppliedTooltip')
[object]::ReferenceEquals($group.SelectedItem, $selectedItem) | Should -BeTrue
$group.SelectedIndex | Should -Be 2
$selectedItem.Content | Should -Be (Get-GroupValueTranslation -GroupId 'SearchIcon' -FeatureId $script:UiControlMappings['Group_SearchIconCombo'].Values[1].FeatureIds[0] -FallbackLabel 'fallback')
$selectedItem.Content | Should -Be (Get-GroupValueTranslation -GroupId 'SearchIcon' -FeatureId $script:UiControlMappings['Group_SearchIconCombo'].Values[1].FeatureIds[0])
@(Get-PendingTweakActions -Window $window -ShowAppliedTweaksMode $true | ForEach-Object { "$($_.Action):$($_.FeatureId)" } | Sort-Object) | Should -Be $before
$script:FeatureLabelLookup['DisableTelemetry'] | Should -Be $checkbox.Content
foreach ($category in $script:CategoryLanguageControls) {
+11 -3
View File
@@ -1,4 +1,6 @@
BeforeAll {
. (Join-Path $PSScriptRoot '..\Scripts\FileIO\Import-JsonFile.ps1')
. (Join-Path $PSScriptRoot '..\Scripts\FileIO\Import-LanguageFile.ps1')
function Get-UserName { 'Alice' }
function Generate-AppsList { @() }
function Add-Parameter { param($Name, $Value) }
@@ -20,7 +22,7 @@ Describe 'Show-CliMenuOptions' {
BeforeEach {
$script:Params = @{}
$script:ControlParams = @('WhatIf', 'Silent')
$script:Features = @{ DisableTelemetry = [PSCustomObject]@{ Label = 'Disable telemetry' }; CreateRestorePoint = [PSCustomObject]@{ Label = 'Create restore point' } }
$script:Lang = Import-LanguageFile -LanguageCode 'en-US' -LanguagesPath (Join-Path $PSScriptRoot '..\Config\Languages')
$script:SelectedApps = @('One.App', 'Two.App')
$script:SavedSettingsFilePath = 'C:\Settings\LastUsedSettings.json'
$script:DefaultSettingsFilePath = 'C:\Settings\DefaultSettings.json'
@@ -53,7 +55,7 @@ Describe 'Show-CliMenuOptions' {
}
It 'summarizes selected features and app removal before confirmation' {
$script:Params = @{ DisableTelemetry = $true; RemoveApps = $true; Apps = 'Default' }
$script:Params = @{ DisableTelemetry = $true; CreateRestorePoint = $true; RemoveApps = $true; Apps = 'Default' }
$script:ControlParams = @('WhatIf', 'Silent', 'Apps')
$script:InputQueue = @('')
@@ -61,7 +63,13 @@ Describe 'Show-CliMenuOptions' {
Should -Invoke Generate-AppsList -Times 1 -Exactly
Should -Invoke Read-Host -Times 1 -Exactly
Should -Invoke Write-Output -Times 2 -ParameterFilter { $InputObject -like '- *' }
Should -Invoke Write-Output -Times 3 -ParameterFilter { $InputObject -like '- *' }
Should -Invoke Write-Output -Times 1 -Exactly -ParameterFilter {
$InputObject -eq "- $(Get-Translation -Key 'CreateRestorePoint' -Field 'Label' -Section 'Features')"
}
Should -Invoke Write-Output -Times 1 -Exactly -ParameterFilter {
$InputObject -eq "- $(Get-Translation -Key 'DisableTelemetry' -Field 'Label' -Section 'Features')"
}
}
It 'returns the saved-settings menu option only when the file is available' {
+69
View File
@@ -7,6 +7,75 @@ BeforeAll {
$script:LanguagesPath = Join-Path $script:RepoRoot 'Config/Languages'
}
Describe 'CLI language routing' {
BeforeAll {
function Show-CliMenuOptions { '0' }
function Show-MainWindow {}
$tokens = $null
$parseErrors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile(
"$script:RepoRoot/Win11Debloat.ps1", [ref]$tokens, [ref]$parseErrors)
$parseErrors | Should -BeNullOrEmpty
# Execute only language initialization and mode routing, avoiding Windows startup/mutations.
$initialization = $ast.EndBlock.Statements | Where-Object {
$_ -is [System.Management.Automation.Language.AssignmentStatementAst] -and
$_.Left.Extent.Text -in @('$script:UiPreferences', '$script:Lang')
}
$script:LanguageStartup = [scriptblock]::Create(($initialization.Extent.Text -join "`n"))
$routing = $ast.EndBlock.Statements | Where-Object {
$_ -is [System.Management.Automation.Language.IfStatementAst] -and
$_.Extent.Text -match '\$result = Show-MainWindow'
}
$script:LanguageRouting = [scriptblock]::Create($routing.Extent.Text)
}
BeforeEach {
$script:UiPreferencesFilePath = "$TestDrive/preferences.json"
Save-UiPreferences -Preferences @{ Theme = 'Dark'; Language = 'nl-NL' }
$script:Params = @{ CLI = $true }
$controlParamsCount = 1
$launchInCLI = $true
$CLI = $true
$Silent = $true
$Language = 'pt-BR'
$RunDefaults = $false
$RunDefaultsLite = $false
$RunSavedSettings = $false
$Config = $null
$script:MenuLanguage = $null
$script:GuiLanguage = $null
Mock Show-CliMenuOptions { $script:MenuLanguage = $script:Lang.LanguageCode; '0' }
Mock Show-MainWindow { throw 'Simulated GUI startup failure' }
Mock Write-Warning {}
}
It 'uses English in CLI despite saved and explicit GUI languages' {
& $script:LanguageStartup
& $script:LanguageRouting
$script:MenuLanguage | Should -Be 'en-US'
$script:UiPreferences.Theme | Should -Be 'Dark'
(Get-UiPreferences).Language | Should -Be 'nl-NL'
Should -Invoke Show-MainWindow -Times 0 -Exactly
}
It 'restores English before falling back from the selected GUI language to CLI' {
$launchInCLI = $false
$CLI = $false
Mock Show-MainWindow {
$script:GuiLanguage = $script:Lang.LanguageCode
throw 'Simulated GUI startup failure'
}
& $script:LanguageStartup
& $script:LanguageRouting
Should -Invoke Show-MainWindow -Times 1 -Exactly
$script:GuiLanguage | Should -Be 'pt-BR'
$script:MenuLanguage | Should -Be 'en-US'
(Get-UiPreferences).Language | Should -Be 'nl-NL'
}
}
Describe 'UI preference initialization' {
BeforeEach {
$script:UiPreferencesFilePath = "$TestDrive/preferences.json"