Public Access
Nothing in this repository loaded a .axaml, so the one class of defect this window has actually shipped — a control arranged past the edge of its container, where it cannot be clicked — was the one class nothing could catch. The setup screens rendered sliced once, with their buttons unreachable. The vault column is the next candidate: 340 pixels wide, two lists and two editors, and the only thing keeping it from clipping its own Save button at the window's 520-pixel minimum is a state rule that one editor may be open at a time. That rule was added on the strength of an argument. This adds an Avalonia.Headless project that lays real XAML out at a real size and reports what a user could not reach, and the argument is now a number: with both editors open the column overflows, so the rule is load-bearing rather than defensive. BothEditorsAtOnce_DoNotFit_WhichIsWhyTheRuleExists is the test, and it says what to do if it ever starts passing — the column has room, so delete the rule, not the test. Two findings arrived by measuring rather than by reasoning, and the first one changed the design. MainWindow cannot be shown headlessly at all. Showing it attaches the terminal's NativeWebView, whose Win32 adapter initialises WebView2 on attach, and WebView2 refuses a non-STA thread — which is exactly why Program.Main carries [STAThread] and is written down in that comment. A HeadlessUnitTestSession owns its dispatcher thread and offers no apartment choice, so the whole window is out of reach at any size. That is pinned as a test asserting RPC_E_CHANGED_MODE by HResult rather than by message, so a future Avalonia that makes the adapter lazy will fail it and the harness can be widened. So the column had to become its own control to be measurable, which is the extraction the type-selector rework wanted anyway. Keyboard release moved with it: MainWindow used to call Focus() on HostList by name, and now asks VaultColumn.KeyboardTarget. The window decides that the keyboard should leave the terminal and the column decides where it lands — which is the seam the rework needs, because once the column shows one list at a time, "which list owns the keyboard" is a question only the column can answer. The second finding is the way this kind of test lies quietly. The hint class lived in MainWindow.Styles and carries TextWrapping. A Window's styles reach its whole tree, so nothing about the application depended on where it lived — but a control laid out on its own loses them, and every hint paragraph would have measured as a single line. The harness would have passed while measuring heights that were all too small. The three shared classes now live in App.axaml, which changes no rendering and makes the measurement honest. The detector is calibrated in both directions, because a clipping detector that never fires reads as a guarantee: a deliberately clipped Save button is caught by name, and a list longer than its viewport is exempt. Scrolling is how a list is supposed to handle more rows than fit, and without that exemption the host list would fail the moment it had content. It also mis-fired once and the rule is narrower for it — an empty ListBox is zero pixels tall and correct, so "arranged with no size" now applies only to controls the theme gives a height to. Skia rather than the headless drawing stub, deliberately. The stub's font manager invents glyph metrics, and text height is an input to every stacked panel in this column, so measuring against it would produce numbers that are self-consistent and unrelated to the application. A separate test project rather than more tests in DodoSSH.Client.App.Tests. Avalonia's application, dispatcher and platform are process-global singletons initialised once, and that project's identity is the shell's state machine without Avalonia — the whole reason sign-in is a delegate. The fakes needed to reach a real unlocked vault are shared from DodoSSH.Client.Session.Tests by source link: a project reference would make one test project a library of another, and a copy would be a third implementation of the same decision table drifting from the other two. 855 tests green, 10 of them new. Zero warnings, dotnet format clean. Not done, and this is groundwork rather than the item itself: the type selector. The column still holds both lists at once, so a third item type would still recreate the defect the one-editor rule works around. What is different is that the rework can now be checked instead of eyeballed — including the claim it is being made for, that one editor at a time stops being a runtime rule and becomes a fact about what is in the visual tree. What this harness will never catch is the terminal's native child window compositing over Avalonia content. That is a Win32 property of a real window, no headless surface reproduces it, and it is the reason the WebView is collapsed rather than covered.
175 lines
6.7 KiB
C#
175 lines
6.7 KiB
C#
using Avalonia.Controls;
|
|
using DodoSSH.Client.App.ViewModels;
|
|
using DodoSSH.Client.App.Views;
|
|
using DodoSSH.Client.Session;
|
|
using DodoSSH.Client.Session.Tests;
|
|
using DodoSSH.Client.Ssh;
|
|
using DodoSSH.Client.Storage;
|
|
using DodoSSH.Client.Terminal;
|
|
using DodoSSH.Crypto;
|
|
using NSubstitute;
|
|
|
|
namespace DodoSSH.Client.App.Layout.Tests;
|
|
|
|
/// <summary>
|
|
/// Whether the vault column fits in the space the window gives it.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// The column is 340 pixels wide and holds a list and an editor per item type, and the only thing keeping it
|
|
/// from clipping its own Save button at the window's minimum height is a state rule — one editor open at a
|
|
/// time. That rule was added on the strength of an argument, not a measurement, and this suite is the
|
|
/// measurement.
|
|
/// </para>
|
|
/// <para>
|
|
/// A real <c>VaultViewModel</c> over a real unlocked vault, rather than a stand-in. Compiled bindings resolve
|
|
/// against the declared data type, so a stand-in would have to be the same type anyway — and the editors'
|
|
/// height depends on real content: a key with a real armour block in the box is taller than an empty one.
|
|
/// </para>
|
|
/// </remarks>
|
|
public sealed class VaultColumnLayoutTests : IAsyncLifetime
|
|
{
|
|
private const string Passphrase = "a sufficiently long passphrase";
|
|
private const string ServerUrl = "https://dodossh.example";
|
|
|
|
/// <remarks>Far below the shipped profile: nothing here attacks a wrap.</remarks>
|
|
private static readonly Argon2Profile CheapProfile =
|
|
Argon2Profile.FromStoredParameters(memoryKibibytes: 8 * 1024, passes: 1, parallelism: 1);
|
|
|
|
private readonly FakeAccountServer server = new();
|
|
private readonly StubKeyBinding keyBinding = new();
|
|
private readonly VaultKnownHostStore knownHosts = new();
|
|
|
|
private ClientCacheFactory caches = null!;
|
|
private TerminalWorkspace workspace = null!;
|
|
private VaultSession session = null!;
|
|
private VaultViewModel vault = null!;
|
|
|
|
private static CancellationToken Token => TestContext.Current.CancellationToken;
|
|
|
|
/// <inheritdoc />
|
|
public async ValueTask InitializeAsync()
|
|
{
|
|
caches = ClientCacheFactory.ForMemory($"layout-{Guid.CreateVersion7():N}");
|
|
await caches.MigrateAsync(Token);
|
|
|
|
await new AccountProvisioner(server, keyBinding, caches, TimeProvider.System, CheapProfile)
|
|
.EnrollAsync(ServerUrl, Passphrase, "laptop", "Personal", Token);
|
|
|
|
var outcome = await new SessionOpener(caches, TimeProvider.System).UnlockAsync(Passphrase, Token);
|
|
outcome.IsUnlocked.ShouldBeTrue(outcome.Message);
|
|
session = outcome.Session!;
|
|
|
|
// Never started and never connected through: the column's layout does not depend on the terminal, and
|
|
// the substitute is here only because the view model's constructor asks for one.
|
|
workspace = new TerminalWorkspace(
|
|
new InMemoryTerminalAssetProvider(new Dictionary<string, TerminalAsset>(StringComparer.Ordinal)),
|
|
Substitute.For<ISshConnectionFactory>(),
|
|
TimeProvider.System);
|
|
|
|
await knownHosts.OpenAsync(session, Token);
|
|
|
|
// Offline. A null connection is what the column shows on a laptop with no network, and it keeps every
|
|
// sync pass out of a suite that is only measuring rectangles.
|
|
vault = new VaultViewModel(session, workspace, knownHosts, static () => null);
|
|
|
|
await SeedAsync();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
await vault.DisposeAsync();
|
|
knownHosts.Close();
|
|
await workspace.DisposeAsync();
|
|
await session.DisposeAsync();
|
|
caches.Dispose();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheColumnFitsWithNoEditorOpen()
|
|
{
|
|
await MeasureAsync(faults => faults.ShouldBeEmpty());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheColumnFitsWithTheHostEditorOpen()
|
|
{
|
|
vault.NewHostCommand.Execute(null);
|
|
vault.IsEditing.ShouldBeTrue();
|
|
|
|
await MeasureAsync(faults => faults.ShouldBeEmpty());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheColumnFitsWithTheKeyEditorOpen()
|
|
{
|
|
// The tall one: a private key needs a real text area, and this is the editor the MaxHeight on the key
|
|
// list exists to make room for.
|
|
vault.NewKeyCommand.Execute(null);
|
|
vault.IsEditingKey.ShouldBeTrue();
|
|
|
|
vault.KeyEditorPrivateKey = string.Join(
|
|
'\n',
|
|
Enumerable.Repeat("b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gt", 6));
|
|
|
|
await MeasureAsync(faults => faults.ShouldBeEmpty());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task BothEditorsAtOnce_DoNotFit_WhichIsWhyTheRuleExists()
|
|
{
|
|
// The justification for KeyEditorIsInTheWay/HostEditorIsInTheWay, turned from an argument in a comment
|
|
// into a number. The flags are set directly because the commands refuse this on purpose — the point is
|
|
// to measure what the refusal is protecting.
|
|
//
|
|
// If this test ever starts passing, the rule has become unnecessary and the comments claiming it is
|
|
// load-bearing have become false. That is a finding, not a flake: read it as "the column has room
|
|
// now", check what changed, and delete the rule rather than this test.
|
|
vault.IsEditing = true;
|
|
vault.IsEditingKey = true;
|
|
|
|
await MeasureAsync(faults => faults.ShouldNotBeEmpty(
|
|
"one editor at a time is a workaround for a column that cannot hold two"));
|
|
}
|
|
|
|
/// <summary>Lays the column out at the size the window gives it and hands the faults to an assertion.</summary>
|
|
private Task MeasureAsync(Action<IReadOnlyList<string>> assert) =>
|
|
LayoutHarness.OnTheUiThreadAsync(
|
|
() =>
|
|
{
|
|
var window = LayoutHarness.HostAtMinimumSize(
|
|
new VaultColumn { DataContext = vault },
|
|
LayoutHarness.VaultColumnWidth,
|
|
LayoutHarness.VaultColumnHeight);
|
|
|
|
try
|
|
{
|
|
assert(LayoutHarness.Unreachable(window));
|
|
}
|
|
finally
|
|
{
|
|
window.Close();
|
|
}
|
|
},
|
|
Token);
|
|
|
|
/// <remarks>
|
|
/// Enough rows that the lists are not empty, because an empty list is the easiest case and the one least
|
|
/// worth certifying.
|
|
/// </remarks>
|
|
private async Task SeedAsync()
|
|
{
|
|
for (var i = 0; i < 6; i++)
|
|
{
|
|
vault.NewHostCommand.Execute(null);
|
|
vault.EditorLabel = $"host-{i}";
|
|
vault.EditorHostname = $"host-{i}.internal";
|
|
vault.EditorUsername = "deploy";
|
|
await vault.SaveHostCommand.ExecuteAsync(null);
|
|
}
|
|
|
|
await vault.LoadAsync(Token);
|
|
}
|
|
}
|