Files
ThreadPilot/Tests/ThreadPilot.Core.Tests/PersistentProcessRuleJsonStoreTests.cs
T
Prime Build 5f9f7b5409 Add process memory priority foundation (#15)
Add process memory priority foundation

Introduce backend support for process memory priority using Windows GetProcessInformation and SetProcessInformation while keeping the feature testable and safe.

- Add ProcessMemoryPriority model with documented Windows memory priority levels
- Add fakeable native API wrapper for process memory priority
- Add ProcessMemoryPriorityService with safe error handling
- Add ProcessOperationResult for process operation outcomes
- Extend persistent process rules with memory priority support
- Apply memory priority through PersistentRulesEngine when configured
- Preserve anti-cheat/protected-process safety messaging
- Add tests for native get/set, invalid process, access denied, unsupported API, enum mapping, persistent rules and JSON roundtrip

No UI redesign, registry/IFEO persistence, Windows Service, preset generator, migration, version or tag changes.
2026-05-20 21:45:21 +02:00

104 lines
3.5 KiB
C#

/*
* ThreadPilot - persistent process rule JSON store tests.
*/
namespace ThreadPilot.Core.Tests
{
using System.Diagnostics;
using ThreadPilot.Models;
using ThreadPilot.Services;
public sealed class PersistentProcessRuleJsonStoreTests
{
[Fact]
public async Task LoadAsync_WithMissingFile_ReturnsEmptyList()
{
var filePath = CreateTemporaryFilePath();
var store = new PersistentProcessRuleJsonStore(() => filePath);
var rules = await store.LoadAsync();
Assert.Empty(rules);
}
[Fact]
public async Task SaveAndLoadAsync_RoundTripsCpuSelectionAndLegacyAffinityMask()
{
var filePath = CreateTemporaryFilePath();
var store = new PersistentProcessRuleJsonStore(() => filePath);
var rule = new PersistentProcessRule
{
Id = "rule-a",
Name = "Game",
IsEnabled = true,
ProcessName = "game.exe",
CpuSelection = new CpuSelection
{
LogicalProcessors = [new ProcessorRef(0, 0, 0)],
GlobalLogicalProcessorIndexes = [0],
},
LegacyAffinityMask = 3,
Priority = ProcessPriorityClass.AboveNormal,
MemoryPriority = ProcessMemoryPriority.BelowNormal,
ApplyAffinityOnStart = true,
ApplyPriorityOnStart = true,
ApplyMemoryPriorityOnStart = true,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow,
Description = ProcessOperationUserMessages.PersistentRulesDescription,
};
try
{
await store.SaveAsync([rule]);
var loaded = await store.LoadAsync();
var loadedRule = Assert.Single(loaded);
Assert.Equal("rule-a", loadedRule.Id);
Assert.Equal(3, loadedRule.LegacyAffinityMask);
Assert.Equal(ProcessPriorityClass.AboveNormal, loadedRule.Priority);
Assert.Equal(ProcessMemoryPriority.BelowNormal, loadedRule.MemoryPriority);
Assert.True(loadedRule.ApplyMemoryPriorityOnStart);
Assert.NotNull(loadedRule.CpuSelection);
Assert.Equal(0, loadedRule.CpuSelection.GlobalLogicalProcessorIndexes.Single());
}
finally
{
DeleteFile(filePath);
}
}
[Fact]
public async Task LoadAsync_WithCorruptJson_ReturnsEmptyList()
{
var filePath = CreateTemporaryFilePath();
Directory.CreateDirectory(Path.GetDirectoryName(filePath)!);
await File.WriteAllTextAsync(filePath, "{ not json");
var store = new PersistentProcessRuleJsonStore(() => filePath);
try
{
var rules = await store.LoadAsync();
Assert.Empty(rules);
}
finally
{
DeleteFile(filePath);
}
}
private static string CreateTemporaryFilePath() =>
Path.Combine(Path.GetTempPath(), $"threadpilot-rules-{Guid.NewGuid():N}", "rules.json");
private static void DeleteFile(string filePath)
{
var directory = Path.GetDirectoryName(filePath);
if (directory != null && Directory.Exists(directory))
{
Directory.Delete(directory, recursive: true);
}
}
}
}