Files
ThreadPilot/Tests/ThreadPilot.Core.Tests/KeyboardShortcutDefaultsTests.cs
Kool 27e7b04913 Harden persistence, monitoring, and shutdown reliability (#46)
Addresses the audit findings across persistent-rule recovery, settings merge behavior, CPU identity, monitoring lifecycle, notifications, autostart, tray updates, and shutdown disposal. Includes regression coverage and the corrected disabled-monitoring view. CI DevSecOps and CodeQL pass.
2026-08-12 01:00:17 +02:00

96 lines
3.5 KiB
C#

namespace ThreadPilot.Core.Tests
{
using System.Windows.Input;
using Microsoft.Extensions.Logging;
using Moq;
using ThreadPilot.Models;
using ThreadPilot.Services;
public sealed class KeyboardShortcutDefaultsTests
{
[Fact]
public async Task LoadShortcutsFromSettingsAsync_WithEmptyList_AttemptsTheDefaultShortcuts()
{
var logger = new RecordingLogger<KeyboardShortcutService>();
using var service = CreateService(logger, new ApplicationSettingsModel());
await service.LoadShortcutsFromSettingsAsync();
var attempted = logger.Messages.Count(message => message.Contains("Skipped registering shortcut", StringComparison.Ordinal));
Assert.Equal(service.GetDefaultShortcuts().Count, attempted);
}
[Fact]
public async Task LoadShortcutsFromSettingsAsync_WithConfiguredShortcuts_DoesNotFallBackToDefaults()
{
var logger = new RecordingLogger<KeyboardShortcutService>();
var settings = new ApplicationSettingsModel
{
KeyboardShortcuts =
[
new KeyboardShortcut
{
ActionName = ShortcutActions.ShowMainWindow,
Key = Key.F8,
Modifiers = ModifierKeys.Control,
IsEnabled = true,
IsGlobal = true,
},
],
};
using var service = CreateService(logger, settings);
await service.LoadShortcutsFromSettingsAsync();
var attempted = logger.Messages.Count(message => message.Contains("Skipped registering shortcut", StringComparison.Ordinal));
Assert.Equal(1, attempted);
}
[Fact]
public void GetDefaultShortcuts_AreAllEnabledAndGlobal()
{
var logger = new RecordingLogger<KeyboardShortcutService>();
using var service = CreateService(logger, new ApplicationSettingsModel());
var defaults = service.GetDefaultShortcuts();
Assert.NotEmpty(defaults);
Assert.All(defaults.Values, shortcut =>
{
Assert.True(shortcut.IsEnabled);
Assert.True(shortcut.IsGlobal);
Assert.False(string.IsNullOrWhiteSpace(shortcut.ActionName));
});
}
private static KeyboardShortcutService CreateService(
ILogger<KeyboardShortcutService> logger,
ApplicationSettingsModel settings)
{
var settingsService = new Mock<IApplicationSettingsService>(MockBehavior.Strict);
settingsService.SetupGet(service => service.Settings).Returns(settings);
return new KeyboardShortcutService(logger, settingsService.Object);
}
private sealed class RecordingLogger<T> : ILogger<T>
{
public List<string> Messages { get; } = new();
public IDisposable? BeginScope<TState>(TState state)
where TState : notnull => null;
public bool IsEnabled(LogLevel logLevel) => true;
public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception? exception,
Func<TState, Exception?, string> formatter)
{
this.Messages.Add(formatter(state, exception));
}
}
}
}