app: render the panel with an injected clock so tests do not depend on the date (#12)

The headless forecast test failed from 2026-10-16 on because the panel
read the real clock while its fixture fixes the time.

- App: PanelWindow takes an optional TimeProvider (the system clock by
  default) and passes it to the pace bar, burn-up, peak and usage charts
  and every forecast label. The burn-up chart uses the panel's render
  time as now.
- Tests: headless panels run on a fixed clock at fixture time, with a
  check before, at and after the forecast start. App and Core tests
  with fixed-date Claude events pin HistoryStore's clock, so the 60-day
  retention cannot drop them later.

Closes #11
This commit was merged in pull request #12.
This commit is contained in:
2026-10-17 09:28:15 +02:00
parent adb8f5b88d
commit d5ad3ecee9
12 changed files with 90 additions and 65 deletions
+30 -12
View File
@@ -30,7 +30,8 @@ public sealed class HeadlessTests
var shots = Environment.GetEnvironmentVariable("FINALTOKEN_SCREENSHOT_DIR") ?? Path.Combine(root.Parent!.FullName, "runs", "shots", "app");
Directory.CreateDirectory(shots);
var model = FakeData.Model();
var panel = new PanelWindow(model, () => { }, () => { }, () => { }, () => { });
var clock = new FixedClock(model.Snapshot!.MadeAt);
var panel = new PanelWindow(model, () => { }, () => { }, () => { }, () => { }, clock);
panel.Resources["SystemAccentColor"] = Avalonia.Media.Colors.Red;
panel.RequestedThemeVariant = ThemeVariant.Light; panel.Show(); Save(panel, "panel-light.png");
Assert.Equal($"FinalToken {typeof(Program).Assembly.GetName().Version!.ToString(3)}", panel.Title);
@@ -142,16 +143,33 @@ public sealed class HeadlessTests
}
}
var prior = viewport.Offset;
model.Snapshot = model.Snapshot! with { MadeAt = DateTimeOffset.UtcNow }; Dispatcher.UIThread.RunJobs();
model.Snapshot = model.Snapshot! with { MadeAt = clock.GetUtcNow() }; Dispatcher.UIThread.RunJobs();
Assert.Equal(prior, viewport.Offset);
}
var forecastModel = FakeData.ForecastModel();
var forecastPanel = new PanelWindow(forecastModel, () => { }, () => { }, () => { }, () => { });
var forecastClock = new FixedClock(forecastModel.Snapshot!.MadeAt);
var forecastPanel = new PanelWindow(forecastModel, () => { }, () => { }, () => { }, () => { }, forecastClock);
forecastPanel.Show(); Dispatcher.UIThread.RunJobs();
var aheadTile = forecastPanel.GetVisualDescendants().OfType<Border>().Single(b => b.Name == "LimitTile-Codex-primary");
Assert.Contains(aheadTile.GetVisualDescendants().OfType<TextBlock>(), t => t.Text?.StartsWith("100 % on ") == true);
Assert.Contains(aheadTile.GetVisualDescendants().OfType<TextBlock>(), t => t.Text == "⚠ ");
Assert.NotNull(forecastModel.History.Limits.Single(l => l.Window!.Window.Id == "primary").Forecast);
var secondary = forecastModel.Snapshot!.Providers[0].Windows.Single(w => w.Window.Id == "secondary");
var forecastStart = Pace.ForecastStartsAt(secondary.Window)!.Value;
foreach (var (renderTime, waiting) in new[] { (forecastStart.AddTicks(-1), true), (forecastStart, false),
(forecastStart.AddTicks(1), false), (secondary.Window.ResetsAt!.Value, false), (secondary.Window.ResetsAt.Value.AddDays(365), false) })
{
forecastClock.UtcNow = renderTime;
forecastPanel.RenderModel(); Dispatcher.UIThread.RunJobs();
var tile = forecastPanel.GetVisualDescendants().OfType<Border>().Single(b => b.Name == "LimitTile-Codex-secondary");
Assert.Equal(waiting, tile.GetVisualDescendants().OfType<TextBlock>().Any(t => t.Text?.StartsWith("Forecast from ") == true));
var reset = Display.Projection(secondary.Window.ResetsAt!.Value, forecastClock);
Assert.Contains(tile.GetVisualDescendants().OfType<TextBlock>(), t => t.Text?.StartsWith("Resets in ") == true && t.Text.EndsWith(reset, StringComparison.Ordinal));
var burnUp = forecastPanel.GetVisualDescendants().OfType<BurnUpChart>().Last();
Assert.Equal(waiting, burnUp.GetVisualDescendants().OfType<TextBlock>().Any(t => t.Text?.StartsWith("Forecast from ") == true));
}
forecastClock.UtcNow = forecastModel.Snapshot.MadeAt;
forecastPanel.RenderModel(); Dispatcher.UIThread.RunJobs();
var waitingTile = forecastPanel.GetVisualDescendants().OfType<Border>().Single(b => b.Name == "LimitTile-Codex-secondary");
var note = Assert.Single(waitingTile.GetVisualDescendants().OfType<TextBlock>(), t => t.Text?.StartsWith("Forecast from ") == true).Text;
Assert.Contains(forecastPanel.GetVisualDescendants().OfType<BurnUpChart>(), c => c.GetVisualDescendants().OfType<TextBlock>().Any(t => t.Text == note));
@@ -175,7 +193,7 @@ public sealed class HeadlessTests
}
forecastPanel.Close();
var earlyModel = FakeData.EarlyResetModel();
var earlyPanel = new PanelWindow(earlyModel, () => { }, () => { }, () => { }, () => { });
var earlyPanel = new PanelWindow(earlyModel, () => { }, () => { }, () => { }, () => { }, new FixedClock(earlyModel.Snapshot!.MadeAt));
earlyPanel.Show();
var earlyTile = earlyPanel.GetVisualDescendants().OfType<Border>().Single(b => b.Name == "LimitTile-Codex-secondary");
Assert.DoesNotContain(earlyTile.GetVisualDescendants().OfType<TextBlock>(), t => t.Text?.Contains("at reset") == true || t.Text?.Contains("100 % on") == true || t.Text?.StartsWith("Last ") == true);
@@ -212,7 +230,7 @@ public sealed class HeadlessTests
{
var weekData = originalHistory.Details.Single(d => d.Provider == Provider.Claude && d.Id == "week").Days!;
Assert.Contains(weekData.Current, d => d.Origin == UseOrigin.Estimated);
var future = DataTooltip.Day(new(DateTimeOffset.Now.AddDays(1), DateTimeOffset.Now.AddDays(2), null, 42, false, true));
var future = DataTooltip.Day(new(clock.GetLocalNow().AddDays(1), clock.GetLocalNow().AddDays(2), null, 42, false, true));
Assert.DoesNotContain(future.Rows, row => row.Label == "Cumulative");
var types = new (string Name, DataTooltip Tip)[]
{
@@ -224,9 +242,9 @@ public sealed class HeadlessTests
("tokens", DataTooltip.Tokens("Claude - This week", originalSnapshot.Providers[0].Week)),
};
var limit = originalHistory.Limits.Single(l => l.Provider == Provider.Claude && l.Window!.Window.Id == "week");
CaptureChartTip("burnup", new BurnUpChart(limit), new Point(240, 100));
CaptureChartTip("past-instance", new InstancePeaks(limit.Analysis!.Past, Provider.Claude), new Point(30, 30));
CaptureChartTip("past-limit-reached", new InstancePeaks(limit.Analysis!.Past, Provider.Claude), new Point(600, 30));
CaptureChartTip("burnup", new BurnUpChart(limit, clock), new Point(240, 100));
CaptureChartTip("past-instance", new InstancePeaks(limit.Analysis!.Past, Provider.Claude, clock), new Point(30, 30));
CaptureChartTip("past-limit-reached", new InstancePeaks(limit.Analysis!.Past, Provider.Claude, clock), new Point(600, 30));
CaptureChartTip("hourly", new UsageChart { Values = originalHistory.Hourly }, new Point(60, 170));
CaptureChartTip("daily", new UsageChart { Values = originalHistory.Daily, Interval = TimeSpan.FromDays(1) }, new Point(60, 170));
CaptureChartTip("composition", new CompositionBar(originalSnapshot.Providers[0].Week, Provider.Claude, true) { Height = 8 }, new Point(100, 4));
@@ -248,7 +266,7 @@ public sealed class HeadlessTests
tooltipWindow.Show(); Save(tooltipWindow, $"tooltip-{name}-{theme}.png"); tooltipWindow.Close();
}
}
model.Snapshot = FakeData.Snapshot(DateTimeOffset.UtcNow, true);
model.Snapshot = FakeData.Snapshot(clock.GetUtcNow(), true);
Dispatcher.UIThread.RunJobs(); panel.GetVisualDescendants().OfType<ScrollViewer>().First().Offset = default;
Save(panel, "panel-unavailable.png");
foreach (var (width, height) in new[] { (1280, 900), (1500, 1000) })
@@ -258,7 +276,7 @@ public sealed class HeadlessTests
panel.GetVisualDescendants().OfType<ScrollViewer>().First().Offset = default;
Save(panel, $"panel-{width}x{height}-{theme}-unavailable.png");
}
model.Diagnostics = [new(DateTimeOffset.UtcNow, Provider.Claude, "fixture", "Probe timed out. Last good sample retained.")];
model.Diagnostics = [new(clock.GetUtcNow(), Provider.Claude, "fixture", "Probe timed out. Last good sample retained.")];
Dispatcher.UIThread.RunJobs(); var expander = panel.GetVisualDescendants().OfType<Expander>().Single();
expander.BringIntoView(); Save(panel, "panel-diagnostics-collapsed.png");
expander.IsExpanded = true; panel.UpdateLayout();
@@ -302,7 +320,7 @@ public sealed class HeadlessTests
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++);
var ahead = PopupWindow.Pace(new(Provider.Claude, "session", "Session", PaceState.Ahead, 22, clock.GetUtcNow().AddHours(1)), () => opened++);
ahead.Show(); SaveThemes(ahead, "popup-ahead"); 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), () => { });
@@ -320,7 +338,7 @@ public sealed class HeadlessTests
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 chart = new UsageChart { Values = [new(clock.GetUtcNow(), "Exact", 123456)] };
var chartWindow = new Window { Content = chart, Width = 400, Height = 180 };
chartWindow.Show();
using (chartWindow.CaptureRenderedFrame()) { }