From 09d4e2f1a1bc206c80d8d3287d070c70ec7fd63d Mon Sep 17 00:00:00 2001
From: chunhualin1 <chunhualin1@lenovo.com>
Date: 星期五, 14 八月 2026 20:00:52 +0800
Subject: [PATCH] Merge branch 'master' of http://git.linmoli.cn/r/Moli.Desktop

---
 src/Moli.Desktop.Tests/Hotkeys/HotkeyServiceTests.cs |  169 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 169 insertions(+), 0 deletions(-)

diff --git a/src/Moli.Desktop.Tests/Hotkeys/HotkeyServiceTests.cs b/src/Moli.Desktop.Tests/Hotkeys/HotkeyServiceTests.cs
new file mode 100644
index 0000000..5f1b986
--- /dev/null
+++ b/src/Moli.Desktop.Tests/Hotkeys/HotkeyServiceTests.cs
@@ -0,0 +1,169 @@
+using Moli.Desktop.Diagnostics;
+using Moli.Desktop.Hotkeys;
+using Moli.Desktop.Runtime;
+
+namespace Moli.Desktop.Tests.Hotkeys;
+
+public class HotkeyServiceTests
+{
+    [Fact]
+    public async Task ReplaceBindingsAsync_RegistersEnabledAndSkipsConflicts()
+    {
+        var diagnostics = new InMemoryDiagnostics();
+        var api = new FakeNativeHotkeyApi();
+        api.ConflictChords.Add("Ctrl+Shift+K");
+        using var service = CreateService(api, diagnostics);
+
+        await service.ReplaceBindingsAsync(
+        [
+            HotkeysDocument.CreateBuiltinPaletteBinding(),
+            new HotkeyBinding
+            {
+                Id = "user-edit",
+                Keys = "Ctrl+Alt+E",
+                Action = HotkeyActions.RuntimeToggleEditMode,
+                Enabled = true,
+                Source = HotkeySource.User
+            },
+            new HotkeyBinding
+            {
+                Id = "disabled",
+                Keys = "Ctrl+Alt+D",
+                Action = HotkeyActions.RuntimeToggleSafeInputMode,
+                Enabled = false,
+                Source = HotkeySource.User
+            }
+        ]);
+
+        Assert.Equal(HotkeyRegistrationState.Conflict, service.Registrations.Single(item => item.BindingId == "llm-palette-toggle-interactable").State);
+        Assert.Equal(HotkeyRegistrationState.Registered, service.Registrations.Single(item => item.BindingId == "user-edit").State);
+        Assert.Equal(HotkeyRegistrationState.Disabled, service.Registrations.Single(item => item.BindingId == "disabled").State);
+        Assert.Contains(diagnostics.Snapshot(), entry => entry.Code == DiagnosticCodes.HotkeyConflict);
+        Assert.Contains(diagnostics.Snapshot(), entry => entry.Code == DiagnosticCodes.HotkeyRegistered);
+        Assert.Single(api.Registered);
+        Assert.Equal(new IntPtr(1), api.LastHwnd);
+    }
+
+    [Fact]
+    public async Task PauseAndResume_UnregistersThenRegistersAgain()
+    {
+        var api = new FakeNativeHotkeyApi();
+        using var service = CreateService(api);
+        await service.ReplaceBindingsAsync([HotkeysDocument.CreateBuiltinPaletteBinding()]);
+
+        service.Pause();
+        Assert.Empty(api.Registered);
+        Assert.All(service.Registrations, item => Assert.Equal(HotkeyRegistrationState.Disabled, item.State));
+
+        service.Resume();
+        Assert.Single(api.Registered);
+        Assert.Equal(HotkeyRegistrationState.Registered, Assert.Single(service.Registrations).State);
+    }
+
+    [Fact]
+    public async Task ReplaceBindingsAsync_DoesNotRegisterAgainstZeroHwnd()
+    {
+        var diagnostics = new InMemoryDiagnostics();
+        var api = new FakeNativeHotkeyApi();
+        using var service = CreateService(api, diagnostics, hwnd: IntPtr.Zero);
+
+        await service.ReplaceBindingsAsync([HotkeysDocument.CreateBuiltinPaletteBinding()]);
+
+        Assert.Empty(api.Registered);
+        Assert.Equal(new IntPtr(-1), api.LastHwnd);
+        Assert.Equal(HotkeyRegistrationState.Invalid, Assert.Single(service.Registrations).State);
+        Assert.Contains(diagnostics.Snapshot(), entry => entry.Code == DiagnosticCodes.HotkeyMessageWindowFailed);
+    }
+
+    [Fact]
+    public async Task PostedHotkey_RaisesHotkeyPressed()
+    {
+        var api = new FakeNativeHotkeyApi();
+        var window = new FakeHotkeyMessageWindow(new IntPtr(1));
+        using var service = new HotkeyService(new InMemoryDiagnostics(), api, window);
+        var tcs = new TaskCompletionSource<HotkeyBinding>(TaskCreationOptions.RunContinuationsAsynchronously);
+        service.HotkeyPressed += binding =>
+        {
+            tcs.TrySetResult(binding);
+            return Task.CompletedTask;
+        };
+
+        await service.ReplaceBindingsAsync([HotkeysDocument.CreateBuiltinPaletteBinding()]);
+        window.Post(1);
+
+        var invoked = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(2));
+        Assert.Equal(HotkeysDocument.BuiltinPaletteToggleId, invoked.Id);
+    }
+
+    private static HotkeyService CreateService(
+        FakeNativeHotkeyApi api,
+        IDiagnosticsService? diagnostics = null,
+        IntPtr? hwnd = null)
+        => new(diagnostics ?? new InMemoryDiagnostics(), api, new FakeHotkeyMessageWindow(hwnd ?? new IntPtr(1)));
+}
+
+internal sealed class FakeHotkeyMessageWindow : IHotkeyMessageWindow
+{
+    public FakeHotkeyMessageWindow(IntPtr hwnd) => Handle = hwnd;
+
+    public IntPtr Handle { get; }
+
+    public event Action<int>? HotkeyPosted;
+
+    public void EnsureCreated()
+    {
+    }
+
+    public void Post(int id) => HotkeyPosted?.Invoke(id);
+
+    public void Dispose()
+    {
+    }
+}
+
+internal sealed class FakeNativeHotkeyApi : INativeHotkeyApi
+{
+    public HashSet<string> ConflictChords { get; } = new(StringComparer.OrdinalIgnoreCase);
+    public Dictionary<int, (uint Modifiers, uint VirtualKey)> Registered { get; } = [];
+    public IntPtr LastHwnd { get; private set; } = new(-1);
+
+    public bool Register(IntPtr hwnd, int id, uint modifiers, uint virtualKey, out int win32Error)
+    {
+        LastHwnd = hwnd;
+        if (ConflictChords.Contains("Ctrl+Shift+K") && virtualKey == 'K' && (modifiers & HotkeyChord.ModControl) != 0 && (modifiers & HotkeyChord.ModShift) != 0)
+        {
+            win32Error = 1409;
+            return false;
+        }
+
+        Registered[id] = (modifiers, virtualKey);
+        win32Error = 0;
+        return true;
+    }
+
+    public bool Unregister(IntPtr hwnd, int id) => Registered.Remove(id);
+}
+
+internal sealed class InMemoryDiagnostics : IDiagnosticsService
+{
+    private readonly List<DiagnosticEntry> _entries = [];
+
+    public DiagnosticEntry Record(
+        DiagnosticSeverity severity,
+        string code,
+        string message,
+        IReadOnlyDictionary<string, string>? properties = null)
+    {
+        var entry = new DiagnosticEntry
+        {
+            Code = code,
+            Severity = severity,
+            Message = message,
+            Properties = properties ?? new Dictionary<string, string>()
+        };
+        _entries.Add(entry);
+        return entry;
+    }
+
+    public IReadOnlyList<DiagnosticEntry> Snapshot() => [.. _entries];
+}

--
Gitblit v1.9.1