Files
ThreadPilot/Tests/ThreadPilot.Core.Tests/PersistentProcessRuleJsonStoreTests.cs
T
Prime Build bffbdeeccc CPU topology v2 phase 8: add persistent rules engine foundation (#14)
Add persistent rules engine foundation

Introduce the foundation for Apply at process start without adding a background loop, Windows Service, installer changes, registry affinity or anti-cheat bypass behavior.

- Add PersistentProcessRule and PersistentRuleApplyResult models
- Add process-name and executable-path rule matching
- Add PersistentRulesEngine using existing safe affinity and priority services
- Add JSON-backed persistent rule store
- Add safe Apply at process start user-facing copy
- Validate incomplete rules so missing affinity/priority payloads fail safely
- Add tests for matching, CpuSelection, legacy affinity, priority, Realtime guardrails, access denied, protected processes, process exit, disabled rules and storage roundtrip

No process monitor hook, UI management, Windows Service, registry/IFEO expansion, version bump or tag changes.
2026-05-20 16:45:43 +02:00

99 lines
3.2 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,
ApplyAffinityOnStart = true,
ApplyPriorityOnStart = 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.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);
}
}
}
}