Files
ThreadPilot/Tests/ThreadPilot.Core.Tests/ProcessPowerPlanAssociationViewModelTests.cs
T
PrimeBuild-pc df23cd1be9 fix: buttons that do what they say, and rules you can actually see
Three findings from a pass over the main dashboard.

"+ Custom" in the affinity picker did nothing at all. CreateCustomMask was
searching the visual tree for a TabControl and setting SelectedIndex; the
window has been a NavigationView for several versions, so the search returned
null - no navigation, no error, no feedback. It now raises OpenMasksRequested,
mirroring the OpenRulesRequested event the window already handles. The dead
FindVisualChild helper goes with it.

Clearing CPU Sets left the core chips advertising the assignment that had just
been removed until the process happened to be reselected - and a rule saved in
between would have captured those cores.

Rules saved from the Process tab context menu were invisible: nothing listed
them, and the Rules tab shows power plan associations, which are a different
thing entirely. They now have their own tab there, saying in plain words what
each rule applies and how it differs from the associations, with a delete that
shares one implementation with the context menu path so the two can never
disagree about which rule belongs to a process. The list is re-read on every
visit to the tab, which is enough while the two views cannot be on screen at
the same time.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-26 23:38:13 +02:00

116 lines
5.1 KiB
C#

namespace ThreadPilot.Core.Tests
{
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using ThreadPilot.Models;
using ThreadPilot.Services;
using ThreadPilot.ViewModels;
public sealed class ProcessPowerPlanAssociationViewModelTests
{
[Fact]
public void Constructor_IgnoresConfigurationReloadUntilViewIsInitialized()
{
var associations = new Mock<IProcessPowerPlanAssociationService>(MockBehavior.Loose);
var powerPlans = new Mock<IPowerPlanService>(MockBehavior.Strict);
var processes = new Mock<IProcessService>(MockBehavior.Strict);
var monitor = new Mock<IProcessMonitorManagerService>(MockBehavior.Loose);
var masks = new Mock<ICoreMaskService>(MockBehavior.Strict);
_ = new ProcessPowerPlanAssociationViewModel(
NullLogger<ProcessPowerPlanAssociationViewModel>.Instance,
associations.Object,
powerPlans.Object,
processes.Object,
monitor.Object,
masks.Object);
associations.Raise(
service => service.ConfigurationChanged += null,
new ConfigurationChangedEventArgs("Loaded"));
powerPlans.Verify(service => service.GetPowerPlansAsync(), Times.Never);
processes.Verify(service => service.GetProcessesAsync(), Times.Never);
masks.Verify(service => service.InitializeAsync(), Times.Never);
}
[Fact]
public async Task RefreshSavedProcessRulesAsync_ListsWhatEachRuleActuallyApplies()
{
var store = new FakeRuleStore(
[
new PersistentProcessRule
{
Id = "rule-1",
ProcessName = "cs2",
IsEnabled = true,
ApplyAffinityOnStart = true,
CpuAssignmentMode = CpuAssignmentMode.AffinityMask,
ApplyMemoryPriorityOnStart = true,
MemoryPriority = ProcessMemoryPriority.Low,
},
]);
var viewModel = CreateViewModel(store, new Mock<IProcessRuleCreationService>().Object);
await viewModel.RefreshSavedProcessRulesAsync();
var row = Assert.Single(viewModel.SavedProcessRules);
Assert.Equal("cs2", row.ProcessName);
Assert.Contains("CPU assignment", row.Applies);
Assert.Contains("memory Low", row.Applies);
Assert.Equal("AffinityMask", row.Mode);
Assert.True(viewModel.HasSavedProcessRules);
}
[Fact]
public async Task DeleteSavedProcessRuleCommand_RemovesTheRuleAndReloadsTheList()
{
var store = new FakeRuleStore(
[
new PersistentProcessRule { Id = "rule-1", ProcessName = "cs2", IsEnabled = true },
]);
var ruleService = new Mock<IProcessRuleCreationService>();
ruleService
.Setup(service => service.DeleteRuleByIdAsync("rule-1", It.IsAny<CancellationToken>()))
.ReturnsAsync(() =>
{
store.Rules = [];
return new ProcessRuleCreationResult { Success = true, UserMessage = "Deleted saved rule for cs2." };
});
var viewModel = CreateViewModel(store, ruleService.Object);
await viewModel.RefreshSavedProcessRulesAsync();
await viewModel.DeleteSavedProcessRuleCommand.ExecuteAsync(viewModel.SavedProcessRules[0]);
ruleService.Verify(service => service.DeleteRuleByIdAsync("rule-1", It.IsAny<CancellationToken>()), Times.Once);
Assert.Empty(viewModel.SavedProcessRules);
Assert.False(viewModel.HasSavedProcessRules);
}
private static ProcessPowerPlanAssociationViewModel CreateViewModel(
IPersistentProcessRuleStore store,
IProcessRuleCreationService ruleService) =>
new(
NullLogger<ProcessPowerPlanAssociationViewModel>.Instance,
new Mock<IProcessPowerPlanAssociationService>(MockBehavior.Loose).Object,
new Mock<IPowerPlanService>(MockBehavior.Loose).Object,
new Mock<IProcessService>(MockBehavior.Loose).Object,
new Mock<IProcessMonitorManagerService>(MockBehavior.Loose).Object,
new Mock<ICoreMaskService>(MockBehavior.Loose).Object,
persistentRuleStore: store,
ruleCreationService: ruleService);
private sealed class FakeRuleStore(IReadOnlyList<PersistentProcessRule> rules) : IPersistentProcessRuleStore
{
public IReadOnlyList<PersistentProcessRule> Rules { get; set; } = rules;
public Task<IReadOnlyList<PersistentProcessRule>> LoadAsync() => Task.FromResult(this.Rules);
public Task SaveAsync(IReadOnlyList<PersistentProcessRule> rules)
{
this.Rules = rules;
return Task.CompletedTask;
}
}
}
}