diff --git a/src/CommandForge.Domain/CommandDefinition.cs b/src/CommandForge.Domain/CommandDefinition.cs index e38dabd..ae72a57 100644 --- a/src/CommandForge.Domain/CommandDefinition.cs +++ b/src/CommandForge.Domain/CommandDefinition.cs @@ -64,4 +64,23 @@ public sealed record CommandDefinition /// Free-form search tags. public IReadOnlyList Tags { get; init; } = []; + + /// + /// Inclusive lowest Windows build this command applies to (e.g. 22000 for Windows 11+). + /// means no lower bound. + /// + public int? MinOsBuild { get; init; } + + /// + /// Inclusive highest Windows build this command applies to (e.g. 19045 for Windows 10 only). + /// means no upper bound. + /// + public int? MaxOsBuild { get; init; } + + /// + /// Whether this command applies to the given OS build. Pure logic (no Win32) so the presentation + /// layer can hide commands that do not apply to the current Windows version. + /// + public bool AppliesToOsBuild(int build) + => (MinOsBuild is null || build >= MinOsBuild) && (MaxOsBuild is null || build <= MaxOsBuild); } diff --git a/src/CommandForge.Infrastructure/Catalog/CatalogJson.cs b/src/CommandForge.Infrastructure/Catalog/CatalogJson.cs index 38489f4..03c6ed4 100644 --- a/src/CommandForge.Infrastructure/Catalog/CatalogJson.cs +++ b/src/CommandForge.Infrastructure/Catalog/CatalogJson.cs @@ -33,6 +33,8 @@ internal sealed record CommandDto public string? DocUrl { get; init; } public string[]? Tags { get; init; } public RegistryValueDto[]? AffectedRegistryValues { get; init; } + public int? MinOsBuild { get; init; } + public int? MaxOsBuild { get; init; } } /// JSON shape of a registry-value reference (for before/after comparison). diff --git a/src/CommandForge.Infrastructure/Catalog/JsonCatalogProvider.cs b/src/CommandForge.Infrastructure/Catalog/JsonCatalogProvider.cs index 397c85f..75d1650 100644 --- a/src/CommandForge.Infrastructure/Catalog/JsonCatalogProvider.cs +++ b/src/CommandForge.Infrastructure/Catalog/JsonCatalogProvider.cs @@ -73,6 +73,9 @@ public static class CatalogLoader if (string.IsNullOrWhiteSpace(dto.DescriptionKey)) { errors.Add($"Command '{dto.Id}' is missing descriptionKey."); continue; } if (string.IsNullOrWhiteSpace(dto.Executable)) { errors.Add($"Command '{dto.Id}' is missing executable."); continue; } if (dto.Restart == RestartPolicy.FromOutputRegex && !IsValidRegex(dto.RestartRegex)) { errors.Add($"Command '{dto.Id}' has an invalid restartRegex."); continue; } + if (dto.MinOsBuild is <= 0) { errors.Add($"Command '{dto.Id}' has a non-positive minOsBuild."); continue; } + if (dto.MaxOsBuild is <= 0) { errors.Add($"Command '{dto.Id}' has a non-positive maxOsBuild."); continue; } + if (dto.MinOsBuild is { } minBuild && dto.MaxOsBuild is { } maxBuild && maxBuild < minBuild) { errors.Add($"Command '{dto.Id}' has maxOsBuild ({maxBuild}) less than minOsBuild ({minBuild})."); continue; } commands.Add(new CommandDefinition { @@ -100,6 +103,8 @@ public static class CatalogLoader .Where(r => !string.IsNullOrWhiteSpace(r.Path)) .Select(r => new RegistryValueRef { Path = r.Path!, Name = r.Name ?? string.Empty }) .ToList(), + MinOsBuild = dto.MinOsBuild, + MaxOsBuild = dto.MaxOsBuild, }); } diff --git a/src/CommandForge.Infrastructure/Catalog/commands.json b/src/CommandForge.Infrastructure/Catalog/commands.json index f4d764d..e46f273 100644 --- a/src/CommandForge.Infrastructure/Catalog/commands.json +++ b/src/CommandForge.Infrastructure/Catalog/commands.json @@ -920,6 +920,7 @@ "restart": "No", "restartRegex": null, "revertCommandId": "tweak.win11.modernmenu", + "minOsBuild": 22000, "estimatedDuration": "Short", "docUrl": null, "tags": ["windows 11", "context menu", "classic", "registry", "tweak"] @@ -939,6 +940,7 @@ "restart": "No", "restartRegex": null, "revertCommandId": "tweak.win11.classicmenu", + "minOsBuild": 22000, "estimatedDuration": "Short", "docUrl": null, "tags": ["windows 11", "context menu", "modern", "registry", "tweak"] @@ -1766,6 +1768,7 @@ "restart": "No", "restartRegex": null, "revertCommandId": "tweak.win11.taskbarcenter", + "minOsBuild": 22000, "estimatedDuration": "Short", "docUrl": null, "tags": ["windows 11", "taskbar", "align", "left", "registry", "tweak"], @@ -1788,6 +1791,7 @@ "restart": "No", "restartRegex": null, "revertCommandId": "tweak.win11.taskbarleft", + "minOsBuild": 22000, "estimatedDuration": "Short", "docUrl": null, "tags": ["windows 11", "taskbar", "align", "center", "registry", "tweak"], @@ -1810,6 +1814,7 @@ "restart": "No", "restartRegex": null, "revertCommandId": "tweak.clockseconds.off", + "minOsBuild": 22621, "estimatedDuration": "Short", "docUrl": null, "tags": ["windows 11", "clock", "seconds", "taskbar", "registry", "tweak"], @@ -1832,6 +1837,7 @@ "restart": "No", "restartRegex": null, "revertCommandId": "tweak.clockseconds.on", + "minOsBuild": 22621, "estimatedDuration": "Short", "docUrl": null, "tags": ["windows 11", "clock", "seconds", "taskbar", "registry", "tweak"], diff --git a/src/CommandForge.Wpf/ViewModels/CatalogViewModelBuilder.cs b/src/CommandForge.Wpf/ViewModels/CatalogViewModelBuilder.cs index c756476..673d901 100644 --- a/src/CommandForge.Wpf/ViewModels/CatalogViewModelBuilder.cs +++ b/src/CommandForge.Wpf/ViewModels/CatalogViewModelBuilder.cs @@ -17,7 +17,12 @@ internal sealed record CatalogViewModels( /// internal static class CatalogViewModelBuilder { - public static CatalogViewModels Build(ICatalogProvider catalog) + /// + /// Projects the catalog to view-models, hiding commands that do not apply to the current OS build + /// (e.g. Windows 11-only tweaks on Windows 10). The hidden commands never reach the list, palette, + /// category counts or any dashboard. + /// + public static CatalogViewModels Build(ICatalogProvider catalog, int currentOsBuild) { var categoryTitles = catalog.GetCategories() .ToDictionary(c => c.Id, c => Strings.Get(c.TitleKey), StringComparer.Ordinal); @@ -26,6 +31,11 @@ internal static class CatalogViewModelBuilder var searchable = new List(); foreach (var command in catalog.GetCommands()) { + if (!command.AppliesToOsBuild(currentOsBuild)) + { + continue; + } + var title = Strings.Get(command.TitleKey); var description = Strings.Get(command.DescriptionKey); items.Add(new CommandItemViewModel(command, title, description)); diff --git a/src/CommandForge.Wpf/ViewModels/CommandPaletteViewModel.cs b/src/CommandForge.Wpf/ViewModels/CommandPaletteViewModel.cs index c831a09..fa79584 100644 --- a/src/CommandForge.Wpf/ViewModels/CommandPaletteViewModel.cs +++ b/src/CommandForge.Wpf/ViewModels/CommandPaletteViewModel.cs @@ -12,10 +12,11 @@ public sealed partial class CommandPaletteViewModel : ObservableObject private readonly IReadOnlyList _searchable; private readonly Dictionary _itemsById; - public CommandPaletteViewModel(ICatalogProvider catalog) + public CommandPaletteViewModel(ICatalogProvider catalog, ISystemInfoService systemInfo) { ArgumentNullException.ThrowIfNull(catalog); - var vms = CatalogViewModelBuilder.Build(catalog); + ArgumentNullException.ThrowIfNull(systemInfo); + var vms = CatalogViewModelBuilder.Build(catalog, systemInfo.GetStatus().OsBuild); _searchable = vms.Searchable; _itemsById = vms.Items.ToDictionary(i => i.Command.Id, StringComparer.Ordinal); ApplyFilter(); diff --git a/src/CommandForge.Wpf/ViewModels/MainViewModel.cs b/src/CommandForge.Wpf/ViewModels/MainViewModel.cs index 6eb0319..dc0a8e7 100644 --- a/src/CommandForge.Wpf/ViewModels/MainViewModel.cs +++ b/src/CommandForge.Wpf/ViewModels/MainViewModel.cs @@ -31,6 +31,7 @@ public sealed partial class MainViewModel : ObservableObject private readonly IRegistryService _registry; private readonly Dictionary _itemsById; private readonly CommandDefinition? _restorePointCommand; + private readonly int _osBuild; private IReadOnlyList _searchable; private IReadOnlyDictionary _categoryTitles; @@ -89,7 +90,8 @@ public sealed partial class MainViewModel : ObservableObject } }; - var vms = CatalogViewModelBuilder.Build(catalog); + _osBuild = systemInfo.GetStatus().OsBuild; + var vms = CatalogViewModelBuilder.Build(catalog, _osBuild); _searchable = vms.Searchable; _categoryTitles = vms.CategoryTitles; _itemsById = vms.Items.ToDictionary(i => i.Command.Id, StringComparer.Ordinal); @@ -472,7 +474,7 @@ public sealed partial class MainViewModel : ObservableObject { // Rebuild the search corpus + category-title map in the new language, and refresh the // already-displayed item/category/detail view-models in place. - var vms = CatalogViewModelBuilder.Build(_catalog); + var vms = CatalogViewModelBuilder.Build(_catalog, _osBuild); _searchable = vms.Searchable; _categoryTitles = vms.CategoryTitles; diff --git a/tests/CommandForge.Tests/CatalogValidationErrorsTests.cs b/tests/CommandForge.Tests/CatalogValidationErrorsTests.cs index e084de1..889c4c1 100644 --- a/tests/CommandForge.Tests/CatalogValidationErrorsTests.cs +++ b/tests/CommandForge.Tests/CatalogValidationErrorsTests.cs @@ -47,4 +47,72 @@ public sealed class CatalogValidationErrorsTests Assert.Empty(result.Errors); } + + [Fact] + public void Build_MaxOsBuildBelowMinOsBuild_ProducesError() + { + var categories = new[] { new CategoryDto { Id = "cat", TitleKey = "Cat_K" } }; + var commands = new[] + { + new CommandDto + { + Id = "c1", + CategoryId = "cat", + TitleKey = "T", + DescriptionKey = "D", + Executable = "cmd", + MinOsBuild = 22000, + MaxOsBuild = 19045, + }, + }; + + var result = CatalogLoader.Build(categories, commands); + + Assert.Contains(result.Errors, e => e.Contains("c1", StringComparison.Ordinal) && e.Contains("maxOsBuild", StringComparison.Ordinal)); + } + + [Fact] + public void Build_NonPositiveOsBuild_ProducesError() + { + var categories = new[] { new CategoryDto { Id = "cat", TitleKey = "Cat_K" } }; + var commands = new[] + { + new CommandDto + { + Id = "c1", + CategoryId = "cat", + TitleKey = "T", + DescriptionKey = "D", + Executable = "cmd", + MinOsBuild = 0, + }, + }; + + var result = CatalogLoader.Build(categories, commands); + + Assert.Contains(result.Errors, e => e.Contains("c1", StringComparison.Ordinal) && e.Contains("minOsBuild", StringComparison.Ordinal)); + } + + [Fact] + public void Build_ValidOsBuildRange_HasNoErrors() + { + var categories = new[] { new CategoryDto { Id = "cat", TitleKey = "Cat_K" } }; + var commands = new[] + { + new CommandDto + { + Id = "c1", + CategoryId = "cat", + TitleKey = "T", + DescriptionKey = "D", + Executable = "cmd", + MinOsBuild = 22000, + }, + }; + + var result = CatalogLoader.Build(categories, commands); + + Assert.Empty(result.Errors); + Assert.Equal(22000, result.Commands[0].MinOsBuild); + } } diff --git a/tests/CommandForge.Tests/CommandDefinitionTests.cs b/tests/CommandForge.Tests/CommandDefinitionTests.cs new file mode 100644 index 0000000..c07d31f --- /dev/null +++ b/tests/CommandForge.Tests/CommandDefinitionTests.cs @@ -0,0 +1,73 @@ +using CommandForge.Domain; +using CommandForge.Infrastructure.Catalog; + +namespace CommandForge.Tests; + +/// Verifies the OS-build applicability predicate and the embedded catalog's version gating. +public sealed class CommandDefinitionTests +{ + private static CommandDefinition Command(int? min = null, int? max = null) => new() + { + Id = "t", + CategoryId = "c", + TitleKey = "T", + DescriptionKey = "D", + Executable = "cmd", + MinOsBuild = min, + MaxOsBuild = max, + }; + + [Theory] + [InlineData(19045, true)] // no bounds -> always applies + [InlineData(22631, true)] + public void AppliesToOsBuild_NoBounds_AlwaysTrue(int build, bool expected) + => Assert.Equal(expected, Command().AppliesToOsBuild(build)); + + [Theory] + [InlineData(19045, false)] // Windows 10 excluded by min 22000 + [InlineData(21999, false)] + [InlineData(22000, true)] // inclusive lower bound + [InlineData(26100, true)] + public void AppliesToOsBuild_MinOnly_GatesBelow(int build, bool expected) + => Assert.Equal(expected, Command(min: 22000).AppliesToOsBuild(build)); + + [Theory] + [InlineData(19045, true)] // inclusive upper bound (Windows 10 only) + [InlineData(22000, false)] // Windows 11 excluded by max 19045 + public void AppliesToOsBuild_MaxOnly_GatesAbove(int build, bool expected) + => Assert.Equal(expected, Command(max: 19045).AppliesToOsBuild(build)); + + [Theory] + [InlineData(21999, false)] + [InlineData(22000, true)] + [InlineData(22621, true)] + [InlineData(22622, false)] + public void AppliesToOsBuild_Range_GatesBothEnds(int build, bool expected) + => Assert.Equal(expected, Command(min: 22000, max: 22621).AppliesToOsBuild(build)); + + [Theory] + [InlineData("tweak.win11.classicmenu", 22000)] + [InlineData("tweak.win11.modernmenu", 22000)] + [InlineData("tweak.win11.taskbarleft", 22000)] + [InlineData("tweak.win11.taskbarcenter", 22000)] + [InlineData("tweak.clockseconds.on", 22621)] + [InlineData("tweak.clockseconds.off", 22621)] + public void EmbeddedCatalog_Win11Commands_AreGated(string id, int expectedMinBuild) + { + var catalog = CatalogLoader.LoadEmbedded(); + var command = catalog.Commands.Single(c => c.Id == id); + Assert.Equal(expectedMinBuild, command.MinOsBuild); + } + + [Fact] + public void EmbeddedCatalog_Win11Tweaks_HiddenOnWindows10() + { + const int windows10Build = 19045; + var catalog = CatalogLoader.LoadEmbedded(); + var hiddenOnWin10 = catalog.Commands.Where(c => !c.AppliesToOsBuild(windows10Build)).Select(c => c.Id).ToHashSet(); + + Assert.Contains("tweak.win11.taskbarleft", hiddenOnWin10); + Assert.Contains("tweak.clockseconds.on", hiddenOnWin10); + Assert.DoesNotContain("dism.checkhealth", hiddenOnWin10); // ungated commands stay visible + } +}