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:
@@ -11,6 +11,7 @@ namespace FinalToken.App;
|
||||
public sealed class PanelWindow : Window
|
||||
{
|
||||
private readonly PanelViewModel model;
|
||||
private readonly TimeProvider clock;
|
||||
private readonly StackPanel body = new() { Spacing = 24, Margin = new Thickness(24) };
|
||||
private readonly StackPanel statuses = new() { Orientation = Orientation.Horizontal, Spacing = 8 };
|
||||
private readonly TextBlock updated = Ui.Text("", 12, "Muted");
|
||||
@@ -22,9 +23,10 @@ public sealed class PanelWindow : Window
|
||||
private readonly Dictionary<Provider, string> selectedDailyWindow = [];
|
||||
private readonly BreakdownState modelBreakdown = new(), projectBreakdown = new();
|
||||
|
||||
public PanelWindow(PanelViewModel model, Action refresh, Action settings, Action quit, Action reload)
|
||||
public PanelWindow(PanelViewModel model, Action refresh, Action settings, Action quit, Action reload, TimeProvider? clock = null)
|
||||
{
|
||||
this.model = model;
|
||||
this.clock = clock ?? TimeProvider.System;
|
||||
Title = $"FinalToken {typeof(Program).Assembly.GetName().Version?.ToString(3)}";
|
||||
Width = 1280; Height = 900; MinWidth = 960; MinHeight = 500; Ui.Theme(this);
|
||||
var brand = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 8, VerticalAlignment = VerticalAlignment.Center };
|
||||
@@ -83,7 +85,7 @@ public sealed class PanelWindow : Window
|
||||
limitColumns = LimitColumnCount((Bounds.Width > 0 ? Bounds.Width : Width) - 48);
|
||||
var content = new List<Control>();
|
||||
statuses.Children.Clear(); refreshButton.IsEnabled = !model.Refreshing;
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var now = clock.GetUtcNow();
|
||||
updated.Text = model.Snapshot is { } s ? "Updated " + (now - s.MadeAt < TimeSpan.FromMinutes(1) ? "just now" : Display.Duration(now - s.MadeAt) + " ago") : model.Message;
|
||||
ToolTip.SetTip(updated, model.Snapshot?.MadeAt.LocalDateTime.ToString("F"));
|
||||
if (model.Snapshot is { } snapshot)
|
||||
@@ -163,15 +165,15 @@ public sealed class PanelWindow : Window
|
||||
var seriesNames = h.Hourly.Concat(h.Daily).Select(v => v.Series).Distinct().Order(StringComparer.Ordinal).ToArray();
|
||||
Control TokenChart(string title, IReadOnlyList<ChartValue> values, bool daily) => Ui.Card(Ui.Stack(Ui.Text(title, 15),
|
||||
Legend(seriesNames), new UsageChart { Values = values.Select(v => v with { Value = Math.Max(0, v.Value - (model.IncludeCacheReads ? 0 : v.CacheRead)) }).ToArray(),
|
||||
SeriesNames = seriesNames, Interval = daily ? TimeSpan.FromDays(1) : TimeSpan.FromHours(1) }));
|
||||
Clock = clock, SeriesNames = seriesNames, Interval = daily ? TimeSpan.FromDays(1) : TimeSpan.FromHours(1) }));
|
||||
content.Add(Ui.Columns([TokenChart("Tokens per hour", h.Hourly, false), TokenChart("Tokens per day", h.Daily, true)], wide));
|
||||
var windows = h.Limits.Where(l => l.Analysis?.Current.HasData == true).ToArray();
|
||||
if (windows.Length > 0)
|
||||
{
|
||||
content.Add(Ui.Text("Window charts", 20));
|
||||
content.Add(Ui.Columns(windows.Select(limit => Ui.Card(Ui.Stack(Ui.Text(limit.Label, 15),
|
||||
Ui.Text("Measured · dashed: estimated · faint: previous · dotted: forecast", 12, "Muted"), new BurnUpChart(limit),
|
||||
Ui.Text("Past instances · peak use", 12, "Muted"), new InstancePeaks(limit.Analysis!.Past, limit.Provider)))), wide));
|
||||
Ui.Text("Measured · dashed: estimated · faint: previous · dotted: forecast", 12, "Muted"), new BurnUpChart(limit with { Now = now }, clock),
|
||||
Ui.Text("Past instances · peak use", 12, "Muted"), new InstancePeaks(limit.Analysis!.Past, limit.Provider, clock)))), wide));
|
||||
}
|
||||
content.Add(Ui.Text("Breakdown", 20));
|
||||
content.Add(Ui.Card(new BreakdownTable("By model", h.Models, false, model.ProviderFilter, modelBreakdown)));
|
||||
@@ -192,11 +194,11 @@ public sealed class PanelWindow : Window
|
||||
var title = new DockPanel();
|
||||
var chip = new Border { Child = Ui.Mark(Display.State(pace.State), Brush.Parse("#0b0b0b")), Background = PaceBar.Colour(pace.State), CornerRadius = new CornerRadius(6), Padding = new Thickness(8, 4) };
|
||||
DockPanel.SetDock(chip, Dock.Right); title.Children.Add(chip); title.Children.Add(Ui.Text(w.Provider + " · " + w.Label, 15));
|
||||
var card = Ui.Stack(title, Ui.Text($"{w.UsedPercent:0.#} %", 36), Ui.Text("used", 12, "Muted"), new PaceBar(window) { Height = 10 });
|
||||
card.Children.Add(Ui.Text(w.ResetsAt is { } reset ? $"Resets in {Display.Duration(reset - now)} · {Display.Projection(reset)}" : "Reset unknown", 13, "Secondary"));
|
||||
var card = Ui.Stack(title, Ui.Text($"{w.UsedPercent:0.#} %", 36), Ui.Text("used", 12, "Muted"), new PaceBar(window, now) { Height = 10 });
|
||||
card.Children.Add(Ui.Text(w.ResetsAt is { } reset ? $"Resets in {Display.Duration(reset - now)} · {Display.Projection(reset, clock)}" : "Reset unknown", 13, "Secondary"));
|
||||
if (pace.Projection is { } projection)
|
||||
{
|
||||
var caption = Display.Forecast(projection, pace.State) + $" ({Display.Duration(w.ResetsAt!.Value - projection)} before reset)";
|
||||
var caption = Display.Forecast(projection, pace.State, clock) + $" ({Display.Duration(w.ResetsAt!.Value - projection)} before reset)";
|
||||
if (pace.State == PaceState.Ahead)
|
||||
{
|
||||
var line = new DockPanel(); var mark = Ui.Mark("⚠ ", Brush.Parse("#d03b3b"), 13);
|
||||
@@ -206,11 +208,11 @@ public sealed class PanelWindow : Window
|
||||
}
|
||||
else if (LimitAnalysis.AverageUseAtReset(w, now, pace) is { } average)
|
||||
card.Children.Add(Ui.Text($"≈ {average:0.#} % at reset", 13, "Secondary"));
|
||||
else if (Display.ForecastWaiting(window, now) is { } waiting)
|
||||
else if (Display.ForecastWaiting(window, now, clock) is { } waiting)
|
||||
card.Children.Add(Ui.Text(waiting, 13, "Muted"));
|
||||
if (detail?.Recent is { } recent)
|
||||
{
|
||||
var line = Ui.Text($"Last {Display.Span(recent.Span)}: +{recent.Increase:0.#} %" + (recent.ReachesLimit is { } date ? $" · 100 % on {Display.Projection(date)}" : $" · ≈ {recent.UseAtReset:0.#} % at reset"), 13, "Secondary");
|
||||
var line = Ui.Text($"Last {Display.Span(recent.Span)}: +{recent.Increase:0.#} %" + (recent.ReachesLimit is { } date ? $" · 100 % on {Display.Projection(date, clock)}" : $" · ≈ {recent.UseAtReset:0.#} % at reset"), 13, "Secondary");
|
||||
ToolTip.SetTip(line, new DataTooltip("Recent rate", new("Rate", $"{recent.PercentPerDay:0.##} %/day"), new("Observed span", Display.Duration(recent.Span)))); card.Children.Add(line);
|
||||
}
|
||||
if (detail?.Days is { } days)
|
||||
@@ -223,7 +225,7 @@ public sealed class PanelWindow : Window
|
||||
if (now - w.SampleTime > model.ProbeInterval * 2) card.Children.Add(Ui.Text($"⚠ sample {Display.Duration(now - w.SampleTime)} old", 12, "Muted"));
|
||||
var tile = Ui.Card(card); tile.Name = $"LimitTile-{w.Provider}-{w.Id}";
|
||||
ToolTip.SetTip(tile, new DataTooltip(w.Provider + " · " + w.Label, new("Used", $"{w.UsedPercent:0.##} %"), new("Expected", $"{pace.ElapsedFraction * 100:0.##} %"),
|
||||
new("State", Display.State(pace.State)), new("Reset", w.ResetsAt is { } r ? Display.Projection(r) : "Unknown"), new("Sample", w.SampleTime.LocalDateTime.ToString("g")))); return tile;
|
||||
new("State", Display.State(pace.State)), new("Reset", w.ResetsAt is { } r ? Display.Projection(r, clock) : "Unknown"), new("Sample", w.SampleTime.LocalDateTime.ToString("g")))); return tile;
|
||||
}
|
||||
private Control TokenTile(ProviderSnapshot provider, string label, TokenCounts counts)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user