D11 replaces an installed plugin only when the shipped manifest is newer, but the manifest version was kept by hand and still said 1.0.0.1, the version of a test build installed before the snapshot sharing and appPath fixes, so that stale plugin was never replaced. Directory.Build.props now holds the only version, 1.0.1; publish.py stamps it into the shipped manifest and the app shows it in its title. Tests that waited on real time (file retry, popup expiry) use controlled timers, and real process waits have wider margins. Refs #1
138 lines
9.9 KiB
C#
138 lines
9.9 KiB
C#
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Headless;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.Media.Imaging;
|
|
using Avalonia.Styling;
|
|
using Avalonia.Themes.Fluent;
|
|
using Avalonia.Threading;
|
|
using Avalonia.VisualTree;
|
|
using FinalToken.Core;
|
|
using FinalToken.Core.StreamDeck;
|
|
using Xunit;
|
|
|
|
namespace FinalToken.App.Tests;
|
|
|
|
public sealed class HeadlessTests
|
|
{
|
|
[Fact]
|
|
public async Task ViewsRenderAndInteractionsApplySettingsAndPopupActions()
|
|
{
|
|
var completion = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
var thread = new Thread(() =>
|
|
{
|
|
try
|
|
{
|
|
AppBuilder.Configure<TestApplication>().UseSkia().UseHarfBuzz().UseHeadless(new() { UseHeadlessDrawing = false }).SetupWithoutStarting();
|
|
var root = new DirectoryInfo(AppContext.BaseDirectory);
|
|
while (root.Parent is not null && !File.Exists(Path.Combine(root.FullName, "FinalToken.slnx"))) root = root.Parent;
|
|
var shots = Environment.GetEnvironmentVariable("FINALTOKEN_SCREENSHOT_DIR") ?? Path.Combine(root.Parent!.FullName, "runs", "shots", "w2");
|
|
Directory.CreateDirectory(shots);
|
|
var model = FakeData.Model();
|
|
var panel = new PanelWindow(model, () => { }, () => { }, () => { }, () => { }) { Height = 2450 };
|
|
panel.RequestedThemeVariant = ThemeVariant.Light; panel.Show(); Save(panel, "panel-light.png");
|
|
Assert.Equal($"FinalToken {typeof(Program).Assembly.GetName().Version!.ToString(3)}", panel.Title);
|
|
Assert.Contains(panel.GetVisualDescendants().OfType<TextBlock>(), t => t.Text == "Claude");
|
|
Assert.Contains(panel.GetVisualDescendants().OfType<TextBlock>(), t => t.Text == "Tokens per day");
|
|
var filters = panel.GetVisualDescendants().OfType<ComboBox>().ToArray();
|
|
filters[0].SelectedIndex = 4; filters[1].SelectedIndex = 2;
|
|
Assert.Equal(HistoryRange.All, model.Range); Assert.Equal(Provider.Codex, model.ProviderFilter);
|
|
filters[0].SelectedIndex = 1; filters[1].SelectedIndex = 0;
|
|
panel.RequestedThemeVariant = ThemeVariant.Dark; Save(panel, "panel-dark.png");
|
|
model.Snapshot = FakeData.Snapshot(DateTimeOffset.UtcNow, true); Save(panel, "panel-unavailable.png"); panel.Close();
|
|
Settings? saved = null;
|
|
var initial = new Settings { StartWithOs = true, StreamDeckInstallMode = InstallMode.Automatic, DismissedPluginVersion = "0.9" };
|
|
var settings = new SettingsWindow(initial, new(PluginInstallState.Outdated, "1.0", "0.9"), value => { saved = value; return Task.CompletedTask; }) { Height = 1600 };
|
|
settings.Show(); Save(settings, "settings.png");
|
|
var refresh = settings.GetVisualDescendants().OfType<NumericUpDown>().First(); refresh.Value = 25;
|
|
settings.GetVisualDescendants().OfType<CheckBox>().First().IsChecked = false;
|
|
settings.GetVisualDescendants().OfType<TextBox>().Single(t => t.Name == "ClaudeCliPath").Text = "claude-fixture";
|
|
settings.GetVisualDescendants().OfType<Button>().Single(b => b.Content as string == "Save").RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
|
|
Assert.NotNull(saved); Assert.Equal(TimeSpan.FromSeconds(25), saved.RefreshInterval); Assert.True(saved.StartWithOs);
|
|
Assert.Equal(initial.Claude with { CliPath = "claude-fixture" }, saved.Claude); Assert.False(saved.PopupAhead); Assert.Equal("0.9", saved.DismissedPluginVersion);
|
|
Exception? cancellationFailure = null;
|
|
void CatchCancellation(object? sender, DispatcherUnhandledExceptionEventArgs e)
|
|
{ cancellationFailure = e.Exception; e.Handled = true; }
|
|
Dispatcher.UIThread.UnhandledException += CatchCancellation;
|
|
var cancelled = new SettingsWindow(new(), new(PluginInstallState.NoHostFound), _ => Task.FromException(new OperationCanceledException()));
|
|
cancelled.Show();
|
|
cancelled.GetVisualDescendants().OfType<Button>().Single(b => b.Content as string == "Save").RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
|
|
Dispatcher.UIThread.RunJobs();
|
|
Dispatcher.UIThread.UnhandledException -= CatchCancellation;
|
|
Assert.Null(cancellationFailure);
|
|
Assert.Contains(cancelled.GetVisualDescendants().OfType<TextBlock>(), t => t.Text == "Settings save was cancelled."); cancelled.Close();
|
|
var opened = 0;
|
|
var ahead = PopupWindow.Pace(new(Provider.Claude, "session", "Session", PaceState.Ahead, 22, DateTimeOffset.UtcNow.AddHours(1)), () => opened++);
|
|
ahead.Show(); Save(ahead, "popup-ahead.png"); Assert.False(ahead.ShowActivated);
|
|
ahead.MouseDown(new Point(100, 60), Avalonia.Input.MouseButton.Left); Assert.Equal(1, opened); Assert.False(ahead.IsVisible);
|
|
var behind = PopupWindow.Pace(new(Provider.Codex, "primary", "Primary", PaceState.Behind, -18, null), () => { });
|
|
behind.Show(); Save(behind, "popup-behind.png"); behind.Close();
|
|
var installed = 0; var dismissed = 0;
|
|
var ask = new PopupWindow("Stream Deck plugin", "Version 1.0 is ready.", () => { }, "Install", () => installed++, () => dismissed++);
|
|
ask.Show(); Save(ask, "popup-streamdeck-ask.png");
|
|
ask.GetVisualDescendants().OfType<Button>().Single(b => b.Content as string == "Not now").RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
|
|
Assert.Equal(0, installed); Assert.Equal(1, dismissed);
|
|
var install = new PopupWindow("Stream Deck plugin", "Update ready", () => { }, "Update", () => installed++);
|
|
install.Show(); install.GetVisualDescendants().OfType<Button>().Single(b => b.Content as string == "Update").RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
|
|
Assert.Equal(1, installed);
|
|
var popupClock = new PopupClock();
|
|
using var stack = new PopupStack(() => { }, popupClock);
|
|
var first = PopupWindow.Pace(new(Provider.Claude, "a", "Session", PaceState.Ahead, 20, null), () => { });
|
|
var second = PopupWindow.Pace(new(Provider.Codex, "b", "Week", PaceState.Behind, -20, null), () => { });
|
|
stack.Show(first); stack.Show(second); Assert.True(second.Position.Y < first.Position.Y);
|
|
var chart = new UsageChart { Values = [new(DateTimeOffset.UtcNow, "Exact", 123456)] };
|
|
var chartWindow = new Window { Content = chart, Width = 400, Height = 180 };
|
|
chartWindow.Show();
|
|
using (chartWindow.CaptureRenderedFrame()) { }
|
|
chartWindow.MouseMove(new Point(65, 80));
|
|
Assert.Contains(123456.ToString("N0"), ToolTip.GetTip(chart)?.ToString()); chartWindow.Close();
|
|
var polishShots = Path.Combine(root.Parent!.FullName, "runs", "shots", "w1");
|
|
Directory.CreateDirectory(polishShots);
|
|
var emptyChart = new Window { Content = Ui.Card(Ui.Stack(Ui.Text("Tokens per hour", 19), new UsageChart())), Width = 620, Height = 260,
|
|
RequestedThemeVariant = ThemeVariant.Light };
|
|
emptyChart.Show(); Save(emptyChart, "chart-empty.png", polishShots); emptyChart.Close();
|
|
var time = new DateTimeOffset(2026, 10, 14, 12, 0, 0, TimeSpan.Zero);
|
|
var smallChart = new Window { Content = Ui.Card(Ui.Stack(Ui.Text("Tokens per hour", 19), new UsageChart
|
|
{ Values = [new(time, "Claude", 1), new(time.AddHours(1), "Claude", 2), new(time.AddHours(2), "Claude", 3)] })),
|
|
Width = 620, Height = 260, RequestedThemeVariant = ThemeVariant.Light };
|
|
smallChart.Show(); Save(smallChart, "chart-max-3.png", polishShots); smallChart.Close();
|
|
popupClock.Fire(); Dispatcher.UIThread.RunJobs();
|
|
Assert.False(first.IsVisible); Assert.False(second.IsVisible);
|
|
Assert.All(popupClock.Timers, timer => Assert.True(timer.Disposed));
|
|
completion.SetResult();
|
|
void Save(Window window, string name, string? destination = null)
|
|
{
|
|
Dispatcher.UIThread.RunJobs();
|
|
using var bitmap = window.CaptureRenderedFrame(); Assert.NotNull(bitmap);
|
|
using var output = File.Create(Path.Combine(destination ?? shots, name)); bitmap.Save(output, PngBitmapEncoderOptions.Default);
|
|
Assert.True(output.Length > 2000);
|
|
}
|
|
}
|
|
catch (Exception e) { completion.SetException(e); }
|
|
});
|
|
if (OperatingSystem.IsWindows()) thread.SetApartmentState(ApartmentState.STA);
|
|
thread.Start(); await completion.Task; thread.Join();
|
|
}
|
|
public sealed class TestApplication : Application
|
|
{ public override void Initialize() => Styles.Add(new FluentTheme()); }
|
|
private sealed class PopupClock : TimeProvider
|
|
{
|
|
public List<PopupTimer> Timers { get; } = [];
|
|
public override ITimer CreateTimer(TimerCallback callback, object? state, TimeSpan dueTime, TimeSpan period)
|
|
{
|
|
Assert.Equal(TimeSpan.FromSeconds(8), dueTime);
|
|
Assert.Equal(Timeout.InfiniteTimeSpan, period);
|
|
var timer = new PopupTimer(callback, state); Timers.Add(timer); return timer;
|
|
}
|
|
public void Fire() { foreach (var timer in Timers.Where(t => !t.Disposed)) timer.Fire(); }
|
|
}
|
|
private sealed class PopupTimer(TimerCallback callback, object? state) : ITimer
|
|
{
|
|
public bool Disposed { get; private set; }
|
|
public void Fire() => callback(state);
|
|
public bool Change(TimeSpan dueTime, TimeSpan period) => throw new NotSupportedException();
|
|
public void Dispose() => Disposed = true;
|
|
public ValueTask DisposeAsync() { Dispose(); return ValueTask.CompletedTask; }
|
|
}
|
|
}
|