Files
poli0981andClaude Opus 5.5 4507faa6b3 fix(a11y): screen readers, keyboard, names and text size across the app
An accessibility review of every screen, with the keyboard alone, Narrator
and Axe.Windows:

- Screen readers hear what appears: a run starting, waiting for admin
  permission and finishing (result, exit code, time, restart needed), a
  search's result count, the Command Palette's highlighted result and its
  position, every notification and an update's progress. WPF ignores
  LiveSetting, so these go through AutomationPeer.RaiseNotificationEvent.
- The console and the Log viewer are read line by line; an error line says
  so and has a bar at its left, not only a colour; a blank line is read as
  one.
- Lists and buttons are read by their text instead of a type name; catalog
  rows carry their risk and badges as item status; a button repeated on
  every row names its row ("Undo: Flush DNS cache").
- WPF-UI template parts get names: the pane toggle and its two lists, the
  caption buttons, the clear button, the card expander headers. Icon glyphs
  (private-use characters) are kept out of the UI Automation tree
  (DecorativeIcon, GlyphHost), and an info bar's title and message, which
  the tree leaves out, are read through its severity icon.
- Keyboard: F6 moves between the pane, the page and the console; the focus
  moves into the page after navigation; Enter opens a command in the
  catalog and in History; Tab reaches a row's buttons; the splitters move
  with the arrow keys; every window and dialog starts with the keyboard
  inside it, a Caution confirmation on Cancel; Ctrl+Q does not quit while
  typing; page frames are no longer empty Tab stops.
- Seeing the screen: Windows' Text size multiplies the app's font size
  (the navigation pane widens at the largest sizes, badges and filters
  wrap); dimmed text is drawn at full strength under contrast themes;
  secondary text is darker; cut-off text has a tooltip; page transitions
  follow Windows' Animation effects setting.
- Tests: XAML rules (names, list items, headings, splitters, colours,
  icons) and the pure helpers behind the announcements and sizes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:14:20 +07:00

237 lines
7.9 KiB
C#

using System.Threading.Channels;
using CommandForge.Application;
using CommandForge.Application.Ports;
using CommandForge.Application.UseCases;
using CommandForge.Desktop.Services;
using CommandForge.Desktop.ViewModels;
using CommandForge.Domain;
using CommandForge.Infrastructure;
using CommandForge.Infrastructure.History;
using CommandForge.Infrastructure.Recipes;
namespace CommandForge.Tests;
/// <summary>
/// A recipe must never weaken the per-command safety gate (golden rule #8): every risky step gets the
/// confirmation a single run would, and cancelling any of them runs nothing.
/// </summary>
public sealed class RecipeConfirmationTests : IDisposable
{
private static readonly CommandDefinition SafeStep = Step("t.safe", DangerLevel.Safe, admin: false);
private static readonly CommandDefinition CautionStep = Step("t.caution", DangerLevel.Caution, admin: false);
private static readonly CommandDefinition DangerousStep = Step("t.dangerous", DangerLevel.Dangerous, admin: true);
private static readonly CommandDefinition RestorePoint = Step(RunSafety.RestorePointCommandId, DangerLevel.Safe, admin: true);
private readonly string _directory = Path.Combine(Path.GetTempPath(), $"cf-recipe-{Guid.NewGuid():N}");
private readonly List<string> _executed = [];
private readonly FakeDialogs _dialogs = new();
private readonly FakeConfirmation _confirmation = new();
private readonly JsonSettingsService _settings;
public RecipeConfirmationTests()
{
Directory.CreateDirectory(_directory);
_settings = new JsonSettingsService(Path.Combine(_directory, "config.json"));
}
public void Dispose() => Directory.Delete(_directory, recursive: true);
[Fact]
public async Task DecliningTheOverview_AsksNothingElseAndRunsNothing()
{
_dialogs.Answer = false;
await RunRecipeAsync(SafeStep, CautionStep, DangerousStep);
Assert.Empty(_confirmation.Asked);
Assert.Empty(_executed);
}
[Fact]
public async Task EveryRiskyStep_IsConfirmedLikeASingleRun()
{
await RunRecipeAsync(SafeStep, CautionStep, DangerousStep);
Assert.Equal([CautionStep.Id, DangerousStep.Id], _confirmation.Asked);
Assert.Equal([SafeStep.Id, CautionStep.Id, DangerousStep.Id], _executed);
}
[Fact]
public async Task CancellingOneStep_RunsNoStepAtAll()
{
_confirmation.Cancel.Add(DangerousStep.Id);
await RunRecipeAsync(SafeStep, CautionStep, DangerousStep);
Assert.Empty(_executed);
}
[Fact]
public async Task TurningOffCautionPrompts_StillConfirmsDangerousSteps()
{
_settings.ConfirmCaution = false;
await RunRecipeAsync(CautionStep, DangerousStep);
Assert.Equal([DangerousStep.Id], _confirmation.Asked);
}
[Fact]
public async Task ARequestedRestorePoint_IsCreatedOnceBeforeTheFirstStep()
{
_confirmation.CreateRestorePoint = true;
await RunRecipeAsync(CautionStep, DangerousStep);
Assert.Equal([RestorePoint.Id, CautionStep.Id, DangerousStep.Id], _executed);
}
private async Task RunRecipeAsync(params CommandDefinition[] steps)
{
var items = new[] { SafeStep, CautionStep, DangerousStep }
.ToDictionary(c => c.Id, c => new CommandItemViewModel(c, c.Id, string.Empty), StringComparer.Ordinal);
var elevation = new RecordingElevation(_executed);
var execution = new ExecutionViewModel(
new RunCommandUseCase(new RecordingExecutor(_executed), elevation),
elevation,
_dialogs,
new NoClipboard(),
new NoFiles(),
new NoShell(),
new NoNotifications());
var recipes = new RecipesViewModel(
items,
execution,
new JsonRecipeStore(Path.Combine(_directory, "recipes.json")),
new JsonExecutionHistoryService(Path.Combine(_directory, "history.json")),
new SystemClock(),
_confirmation,
_dialogs,
_settings,
RestorePoint);
foreach (var step in steps)
{
recipes.Steps.Add(items[step.Id]);
}
// Fakes answer instantly; the time limit only turns a regression that hangs into a failure.
await recipes.RunRecipeCommand.ExecuteAsync(null).WaitAsync(TimeSpan.FromSeconds(30), TestContext.Current.CancellationToken);
}
private static CommandDefinition Step(string id, DangerLevel level, bool admin) => new()
{
Id = id,
CategoryId = "c",
TitleKey = "Recipe_ConfirmTitle",
DescriptionKey = "Recipe_ConfirmTitle",
Executable = "x",
DangerLevel = level,
ConfirmBeforeRun = level != DangerLevel.Safe,
RequiresAdmin = admin,
};
private static ExecutionResult Ok() => new() { ExitCode = 0, Success = true, Duration = TimeSpan.Zero };
private sealed class FakeDialogs : IDialogService
{
public bool Answer { get; set; } = true;
public Task<bool> ConfirmAsync(string title, string message, DialogKind kind = DialogKind.Warning) => Task.FromResult(Answer);
public Task ShowMessageAsync(string title, string message, DialogKind kind = DialogKind.Information) => Task.CompletedTask;
}
private sealed class FakeConfirmation : IConfirmationService
{
public List<string> Asked { get; } = [];
public HashSet<string> Cancel { get; } = [];
public bool CreateRestorePoint { get; set; }
public Task<ConfirmationOutcome?> ConfirmAsync(CommandDefinition command, bool defaultCreateRestorePoint)
{
Asked.Add(command.Id);
return Task.FromResult(Cancel.Contains(command.Id) ? null : new ConfirmationOutcome(CreateRestorePoint));
}
}
private sealed class RecordingExecutor(List<string> executed) : ICommandExecutor
{
public Task<ExecutionResult> ExecuteAsync(CommandDefinition command, ChannelWriter<OutputLine> output, CancellationToken cancellationToken = default)
{
lock (executed)
{
executed.Add(command.Id);
}
output.TryComplete();
return Task.FromResult(Ok());
}
}
private sealed class RecordingElevation(List<string> executed) : IElevationService
{
public bool IsSessionActive => false;
public event EventHandler? SessionChanged
{
add
{
// A fake session never starts or ends.
}
remove
{
// A fake session never starts or ends.
}
}
public Task EndSessionAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
public Task<ExecutionResult> RunElevatedAsync(CommandDefinition command, ChannelWriter<OutputLine> output, CancellationToken cancellationToken = default)
{
lock (executed)
{
executed.Add(command.Id);
}
output.TryComplete();
return Task.FromResult(Ok());
}
}
private sealed class NoClipboard : IClipboardService
{
public bool TrySetText(string text) => true;
}
private sealed class NoFiles : IFileDialogService
{
public string? PickSaveFile(string title, string filter, string defaultFileName) => null;
public string? PickOpenFile(string title, string filter) => null;
}
private sealed class NoShell : IShellService
{
public bool TryOpen(string target) => true;
public bool TryRestartWindows() => false;
}
private sealed class NoNotifications : INotificationService
{
public void Show(string message, NotificationKind kind = NotificationKind.Information, bool announce = true)
{
// Not shown in tests.
}
public void Announce(string message, bool important = false)
{
// Not read in tests.
}
}
}