- RunSafety (Application) holds the rules for single runs, History re-runs, reverts and recipes: Dangerous always asks with typing; Caution asks unless turned off; a restore point is offered only for risky admin commands or catalog-flagged ones — never for per-user tweaks, where it only added a UAC prompt. - Type-to-confirm accepts the title Unicode-normalized and case-insensitively, or the command id: Vietnamese input methods that type decomposed accents (and long CJK titles typed through an IME) could not confirm before. Run is a CanExecute gate in the view-model, Cancel is the default/Esc button, and the typing box gets focus. - Recipes no longer run Dangerous steps after a single Yes/No: an overview prompt (No by default) is followed by the same confirmation each risky step gets on its own, and one restore point before the chain. Steps unavailable on this PC are kept when the recipe is saved again. - Restore points: "Windows already made one in the last 24 hours" is reported as skipped (exit 2) instead of as created; any other failure asks before running the command anyway (No default). - A run that does not start (already running, stopped at the restore point) is never recorded in Recent/History; History's Run again/Undo are disabled while a command runs; Clear asks first. - Console output goes into an OutputBuffer: one notification per batch instead of per line, capped at 10,000 lines; an unexpected failure is reported in the console instead of crashing. - Every run is logged as a one-line summary (id, exit code, duration — never its output). - New IDialogService/IClipboardService/IShellService/IFileDialogService keep view-models free of MessageBox/Clipboard/Process calls (and testable); new strings in all five languages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
98 lines
3.5 KiB
C#
98 lines
3.5 KiB
C#
using System.Text;
|
|
using CommandForge.Domain;
|
|
using CommandForge.Wpf.ViewModels;
|
|
|
|
namespace CommandForge.Tests;
|
|
|
|
/// <summary>Tests the confirmation gating, especially the type-to-confirm for Dangerous commands.</summary>
|
|
public sealed class ConfirmationViewModelTests
|
|
{
|
|
private static CommandDefinition Command(DangerLevel level, bool requiresAdmin = false, string id = "x") => new()
|
|
{
|
|
Id = id,
|
|
CategoryId = "c",
|
|
TitleKey = "t",
|
|
DescriptionKey = "d",
|
|
Executable = "x",
|
|
DangerLevel = level,
|
|
RequiresAdmin = requiresAdmin,
|
|
};
|
|
|
|
[Fact]
|
|
public void Caution_CanRun_IsAlwaysTrue()
|
|
{
|
|
var viewModel = new ConfirmationViewModel(Command(DangerLevel.Caution), "Repair image", "desc", defaultCreateRestorePoint: true);
|
|
|
|
Assert.True(viewModel.CanRun);
|
|
Assert.True(viewModel.RunCommand.CanExecute(null));
|
|
}
|
|
|
|
[Fact]
|
|
public void Dangerous_CanRun_RequiresTypedTitle()
|
|
{
|
|
var viewModel = new ConfirmationViewModel(Command(DangerLevel.Dangerous), "Minimize telemetry", "desc", defaultCreateRestorePoint: true);
|
|
|
|
Assert.False(viewModel.CanRun);
|
|
Assert.False(viewModel.RunCommand.CanExecute(null));
|
|
|
|
viewModel.ConfirmationInput = "wrong";
|
|
Assert.False(viewModel.CanRun);
|
|
|
|
viewModel.ConfirmationInput = "Minimize telemetry";
|
|
Assert.True(viewModel.CanRun);
|
|
Assert.True(viewModel.RunCommand.CanExecute(null));
|
|
}
|
|
|
|
[Fact]
|
|
public void Dangerous_TypedTitle_IgnoresCaseAndSurroundingSpace()
|
|
{
|
|
var viewModel = new ConfirmationViewModel(Command(DangerLevel.Dangerous), "Minimize telemetry", "desc", defaultCreateRestorePoint: true)
|
|
{
|
|
ConfirmationInput = " minimize TELEMETRY ",
|
|
};
|
|
|
|
Assert.True(viewModel.CanRun);
|
|
}
|
|
|
|
[Fact]
|
|
public void Dangerous_TypedTitle_MatchesDecomposedVietnamese()
|
|
{
|
|
// Some Vietnamese input methods produce decomposed (NFD) accents that look identical.
|
|
const string title = "Đặt lại tường lửa";
|
|
var viewModel = new ConfirmationViewModel(Command(DangerLevel.Dangerous), title, "desc", defaultCreateRestorePoint: true)
|
|
{
|
|
ConfirmationInput = title.Normalize(NormalizationForm.FormD),
|
|
};
|
|
|
|
Assert.True(viewModel.CanRun);
|
|
}
|
|
|
|
[Fact]
|
|
public void Dangerous_CommandId_IsAcceptedInsteadOfTitle()
|
|
{
|
|
var viewModel = new ConfirmationViewModel(Command(DangerLevel.Dangerous, id: "sec.firewall.off"), "ファイアウォールを無効にする", "desc", defaultCreateRestorePoint: true)
|
|
{
|
|
ConfirmationInput = "sec.firewall.off",
|
|
};
|
|
|
|
Assert.True(viewModel.CanRun);
|
|
}
|
|
|
|
[Fact]
|
|
public void CreateRestorePoint_DefaultsToProvidedValue_ForRiskyAdminCommands()
|
|
{
|
|
Assert.True(new ConfirmationViewModel(Command(DangerLevel.Caution, requiresAdmin: true), "t", "d", true).CreateRestorePoint);
|
|
Assert.False(new ConfirmationViewModel(Command(DangerLevel.Caution, requiresAdmin: true), "t", "d", false).CreateRestorePoint);
|
|
}
|
|
|
|
[Fact]
|
|
public void RestorePoint_IsNotOffered_ForPerUserCommands()
|
|
{
|
|
// Creating a restore point needs admin rights: offering it for a per-user tweak would add a UAC prompt.
|
|
var viewModel = new ConfirmationViewModel(Command(DangerLevel.Caution, requiresAdmin: false), "t", "d", true);
|
|
|
|
Assert.False(viewModel.ShowRestorePointOption);
|
|
Assert.False(viewModel.CreateRestorePoint);
|
|
}
|
|
}
|