using System.IO;
using System.Linq;
using AcDream.UI.Abstractions.Input;
using AcDream.UI.Abstractions.Panels.Settings;
using AcDream.UI.Abstractions.Tests.Input;
using Silk.NET.Input;
namespace AcDream.UI.Abstractions.Tests.Panels.Settings;
///
/// K.3: owns the click-to-rebind state machine
/// for the Settings panel. It holds a draft copy of the active
/// ; rebinds modify the draft. Save commits to
/// the supplied callback (which writes to disk + replaces the live
/// dispatcher's table); Cancel reverts the draft.
///
public sealed class SettingsVMTests
{
private static (SettingsVM vm, FakeKeyboardSource kb, InputDispatcher dispatcher, KeyBindings persisted, System.Collections.Generic.List savedHistory)
Build(KeyBindings? persisted = null)
{
persisted ??= MakeMinimalBindings();
var kb = new FakeKeyboardSource();
var mouse = new FakeMouseSource();
var dispatcher = new InputDispatcher(kb, mouse, persisted);
var savedHistory = new System.Collections.Generic.List();
var vm = new SettingsVM(persisted, dispatcher, b => savedHistory.Add(b));
return (vm, kb, dispatcher, persisted, savedHistory);
}
private static KeyBindings MakeMinimalBindings()
{
var b = new KeyBindings();
b.Add(new Binding(new KeyChord(Key.W, ModifierMask.None), InputAction.MovementForward));
b.Add(new Binding(new KeyChord(Key.A, ModifierMask.None), InputAction.MovementTurnLeft));
b.Add(new Binding(new KeyChord(Key.S, ModifierMask.None), InputAction.MovementStop));
return b;
}
[Fact]
public void Constructor_clones_persisted_into_draft()
{
var (vm, _, _, persisted, _) = Build();
Assert.Equal(persisted.All.Count, vm.Draft.All.Count);
Assert.False(vm.HasUnsavedChanges);
}
[Fact]
public void BeginRebind_enters_capture_mode()
{
var (vm, _, dispatcher, _, _) = Build();
var original = vm.Draft.ForAction(InputAction.MovementForward).First();
vm.BeginRebind(InputAction.MovementForward, original);
Assert.True(dispatcher.IsCapturing);
Assert.Equal(InputAction.MovementForward, vm.RebindInProgress);
Assert.Equal(original, vm.RebindOriginal);
}
[Fact]
public void BeginRebind_then_chord_with_no_conflict_applies_rebind()
{
var (vm, kb, _, _, _) = Build();
var original = vm.Draft.ForAction(InputAction.MovementForward).First();
vm.BeginRebind(InputAction.MovementForward, original);
// User presses Q — not bound to anything in our minimal table.
kb.EmitKeyDown(Key.Q, ModifierMask.None);
Assert.Null(vm.RebindInProgress);
Assert.Null(vm.PendingConflict);
var binds = vm.Draft.ForAction(InputAction.MovementForward).ToList();
Assert.Single(binds);
Assert.Equal(new KeyChord(Key.Q, ModifierMask.None), binds[0].Chord);
Assert.True(vm.HasUnsavedChanges);
}
[Fact]
public void BeginRebind_then_Escape_cancels_with_no_change()
{
var (vm, kb, _, _, _) = Build();
var original = vm.Draft.ForAction(InputAction.MovementForward).First();
vm.BeginRebind(InputAction.MovementForward, original);
kb.EmitKeyDown(Key.Escape, ModifierMask.None);
Assert.Null(vm.RebindInProgress);
Assert.Null(vm.PendingConflict);
var binds = vm.Draft.ForAction(InputAction.MovementForward).ToList();
Assert.Single(binds);
Assert.Equal(new KeyChord(Key.W, ModifierMask.None), binds[0].Chord);
Assert.False(vm.HasUnsavedChanges);
}
[Fact]
public void BeginRebind_with_conflict_surfaces_PendingConflict()
{
var (vm, kb, _, _, _) = Build();
var original = vm.Draft.ForAction(InputAction.MovementForward).First();
// Bind chord that conflicts with MovementTurnLeft (which has Key.A).
vm.BeginRebind(InputAction.MovementForward, original);
kb.EmitKeyDown(Key.A, ModifierMask.None);
Assert.NotNull(vm.PendingConflict);
var c = vm.PendingConflict!.Value;
Assert.Equal(InputAction.MovementForward, c.NewAction);
Assert.Equal(new KeyChord(Key.A, ModifierMask.None), c.NewChord);
Assert.Equal(InputAction.MovementTurnLeft, c.ConflictingAction);
// Rebind has NOT been applied yet — still on W.
var binds = vm.Draft.ForAction(InputAction.MovementForward).ToList();
Assert.Equal(new KeyChord(Key.W, ModifierMask.None), binds[0].Chord);
}
[Fact]
public void ResolveConflict_replace_true_removes_conflict_and_applies_rebind()
{
var (vm, kb, _, _, _) = Build();
var original = vm.Draft.ForAction(InputAction.MovementForward).First();
vm.BeginRebind(InputAction.MovementForward, original);
kb.EmitKeyDown(Key.A, ModifierMask.None);
vm.ResolveConflict(replace: true);
Assert.Null(vm.PendingConflict);
Assert.Null(vm.RebindInProgress);
// MovementForward now bound to A.
var fwd = vm.Draft.ForAction(InputAction.MovementForward).ToList();
Assert.Single(fwd);
Assert.Equal(new KeyChord(Key.A, ModifierMask.None), fwd[0].Chord);
// MovementTurnLeft no longer bound to A (conflict removed).
var left = vm.Draft.ForAction(InputAction.MovementTurnLeft).ToList();
Assert.Empty(left);
}
[Fact]
public void ResolveConflict_replace_false_cancels_rebind()
{
var (vm, kb, _, _, _) = Build();
var original = vm.Draft.ForAction(InputAction.MovementForward).First();
vm.BeginRebind(InputAction.MovementForward, original);
kb.EmitKeyDown(Key.A, ModifierMask.None);
vm.ResolveConflict(replace: false);
Assert.Null(vm.PendingConflict);
Assert.Null(vm.RebindInProgress);
// MovementForward still bound to W.
var fwd = vm.Draft.ForAction(InputAction.MovementForward).ToList();
Assert.Equal(new KeyChord(Key.W, ModifierMask.None), fwd[0].Chord);
// MovementTurnLeft still bound to A.
var left = vm.Draft.ForAction(InputAction.MovementTurnLeft).ToList();
Assert.Equal(new KeyChord(Key.A, ModifierMask.None), left[0].Chord);
}
[Fact]
public void ResetActionToDefault_restores_single_action_to_RetailDefaults()
{
// Build a draft that's been mutated for MovementForward; ensure
// ResetActionToDefault restores W (and Up-arrow per retail).
var (vm, kb, _, _, _) = Build(KeyBindings.RetailDefaults());
var original = vm.Draft.ForAction(InputAction.MovementForward).First();
vm.BeginRebind(InputAction.MovementForward, original);
// F7 is unbound in retail-default (only Ctrl+F7 is acdream debug);
// pick it deliberately to avoid triggering a conflict prompt that
// would block the rebind from applying.
kb.EmitKeyDown(Key.F7, ModifierMask.None);
Assert.True(vm.HasUnsavedChanges);
vm.ResetActionToDefault(InputAction.MovementForward);
var fwd = vm.Draft.ForAction(InputAction.MovementForward).ToList();
Assert.Contains(fwd, x => x.Chord == new KeyChord(Key.W, ModifierMask.None));
Assert.Contains(fwd, x => x.Chord == new KeyChord(Key.Up, ModifierMask.None));
}
[Fact]
public void ResetAllToDefaults_replaces_entire_draft()
{
var (vm, _, _, _, _) = Build();
vm.ResetAllToDefaults();
// Should now include retail-default size set (~149 bindings).
Assert.True(vm.Draft.All.Count >= 100);
Assert.True(vm.HasUnsavedChanges);
}
[Fact]
public void Save_invokes_callback_with_draft()
{
var (vm, kb, _, _, savedHistory) = Build();
var original = vm.Draft.ForAction(InputAction.MovementForward).First();
vm.BeginRebind(InputAction.MovementForward, original);
kb.EmitKeyDown(Key.Q, ModifierMask.None);
vm.Save();
Assert.Single(savedHistory);
var saved = savedHistory[0];
var fwd = saved.ForAction(InputAction.MovementForward).ToList();
Assert.Equal(new KeyChord(Key.Q, ModifierMask.None), fwd[0].Chord);
}
[Fact]
public void Cancel_reverts_draft_to_persisted()
{
var (vm, kb, _, _, _) = Build();
var original = vm.Draft.ForAction(InputAction.MovementForward).First();
vm.BeginRebind(InputAction.MovementForward, original);
kb.EmitKeyDown(Key.Q, ModifierMask.None);
Assert.True(vm.HasUnsavedChanges);
vm.Cancel();
Assert.False(vm.HasUnsavedChanges);
var fwd = vm.Draft.ForAction(InputAction.MovementForward).ToList();
Assert.Equal(new KeyChord(Key.W, ModifierMask.None), fwd[0].Chord);
}
[Fact]
public void Cancel_during_active_capture_clears_dispatcher_capture_state()
{
var (vm, _, dispatcher, _, _) = Build();
var original = vm.Draft.ForAction(InputAction.MovementForward).First();
vm.BeginRebind(InputAction.MovementForward, original);
Assert.True(dispatcher.IsCapturing);
vm.Cancel();
Assert.False(dispatcher.IsCapturing);
Assert.Null(vm.RebindInProgress);
}
[Fact]
public void HasUnsavedChanges_false_initially_and_after_save_sync()
{
var (vm, _, _, _, _) = Build();
Assert.False(vm.HasUnsavedChanges);
}
}