app: show the reset history of each long limit window
A Resets section after the window charts gives, per provider, totals for the listed instances, a timeline with each burn-up and the skipped time, early resets by local hour and weekday, and a sortable instance table with a detailed tooltip per row (D8, D12). The breakdown and reset tables share their sorting, header and expander code. Refs #14
This commit is contained in:
@@ -364,6 +364,7 @@ public sealed class HeadlessTests
|
||||
popupClock.Fire(); Dispatcher.UIThread.RunJobs();
|
||||
Assert.False(first.IsVisible); Assert.False(second.IsVisible);
|
||||
Assert.All(popupClock.Timers, timer => Assert.True(timer.Disposed));
|
||||
ResetViews.Verify((window, name) => Save(window, name));
|
||||
completion.SetResult();
|
||||
void SaveThemes(Window window, string stem)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
using System.Globalization;
|
||||
using FinalToken.Core;
|
||||
using Xunit;
|
||||
|
||||
namespace FinalToken.App.Tests;
|
||||
|
||||
public sealed class ResetTests
|
||||
{
|
||||
[Fact] public void HistoryQueriesOnlyLongWindowsAndFollowsProviderAndRange()
|
||||
{
|
||||
var fixture = ResetFixture.Create();
|
||||
var all = PanelViewModel.QueryHistory(fixture.Store, fixture.Snapshot, HistoryRange.All, null, fixture.Now);
|
||||
Assert.Equal(3, all.Resets.Count);
|
||||
Assert.All(all.Resets, r => Assert.True(r.Window.Length > TimeSpan.FromDays(1)));
|
||||
var filtered = PanelViewModel.QueryHistory(fixture.Store, fixture.Snapshot, HistoryRange.All, Provider.Codex, fixture.Now);
|
||||
Assert.Equal(2, filtered.Resets.Count); Assert.All(filtered.Resets, r => Assert.Equal(Provider.Codex, r.Window.Provider));
|
||||
var recent = PanelViewModel.QueryHistory(fixture.Store, fixture.Snapshot, HistoryRange.Hours24, Provider.Codex, fixture.Now);
|
||||
var week = recent.Resets.Single(r => r.Window.Id == "codex:primary");
|
||||
Assert.True(week.Instances.Count < filtered.Resets[0].Instances.Count);
|
||||
Assert.Contains(week.Instances, r => r.Start < fixture.Now.AddHours(-24) && r.End > fixture.Now.AddHours(-24));
|
||||
Assert.Equal(10, filtered.Resets[0].Instances.Count);
|
||||
}
|
||||
|
||||
[Fact] public void BurnUpQueriesReadTheSeriesAcrossCodexSlotsAndExcludeShortWindows()
|
||||
{
|
||||
var fixture = ResetFixture.Create();
|
||||
var history = PanelViewModel.QueryHistory(fixture.Store, fixture.Snapshot, HistoryRange.All, Provider.Codex, fixture.Now);
|
||||
var week = history.Limits.Single(l => l.Window!.Window.Id == "codex:primary" && l.Window.Window.Length > TimeSpan.FromDays(1));
|
||||
Assert.DoesNotContain(week.Values, v => v.Value == 99);
|
||||
Assert.True(week.Values.Count > 10);
|
||||
Assert.Equal(8, week.Analysis!.Past.Count);
|
||||
}
|
||||
|
||||
[Fact] public void MoneyAndLimitsStayInvariantOnAGermanDesktop()
|
||||
{
|
||||
var previous = CultureInfo.CurrentCulture;
|
||||
try
|
||||
{
|
||||
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
|
||||
Assert.Equal("≈ $1,234", Display.Money(1234)); Assert.Equal("≈ $99.25", Display.Money(99.25m));
|
||||
Assert.Equal("Unpriced", Display.Money(null)); Assert.Equal("≈ 3.1 limits", Display.Limits(310));
|
||||
Assert.Equal("0 min", Display.ResetDuration(TimeSpan.Zero));
|
||||
}
|
||||
finally { CultureInfo.CurrentCulture = previous; }
|
||||
}
|
||||
|
||||
[Fact] public void EveryInstanceColumnSortsByItsUnderlyingValue()
|
||||
{
|
||||
var fixture = ResetFixture.Create();
|
||||
var rows = fixture.History.Resets[0].Instances;
|
||||
Assert.Equal(rows.OrderBy(r => r.Start), ResetTable.SortRows(rows, ResetColumn.Start, false));
|
||||
Assert.Equal(rows.Max(r => r.Final.Used), ResetTable.SortRows(rows, ResetColumn.Final, true)[0].Final.Used);
|
||||
Assert.Equal(rows.Max(r => r.Skipped), ResetTable.SortRows(rows, ResetColumn.Reset, true)[0].Skipped);
|
||||
Assert.Equal(rows.Max(r => r.Duration), ResetTable.SortRows(rows, ResetColumn.Lasted, true)[0].Duration);
|
||||
foreach (var column in Enum.GetValues<ResetColumn>())
|
||||
{
|
||||
var ascending = ResetTable.SortRows(rows, column, false); var descending = ResetTable.SortRows(rows, column, true);
|
||||
Assert.Equal(rows.Count, ascending.Count); Assert.Equal(rows.Count, descending.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed record ResetFixture(HistoryStore Store, Snapshot Snapshot, DateTimeOffset Now, HistoryView History)
|
||||
{
|
||||
public static ResetFixture Create()
|
||||
{
|
||||
var start = new DateTimeOffset(2026, 9, 12, 6, 0, 0, TimeSpan.Zero);
|
||||
int[] durations = [7, 1, 2, 7, 1, 2, 1, 1, 2];
|
||||
var now = start.AddDays(durations.Sum()).AddHours(12);
|
||||
var store = new HistoryStore(Path.Combine(Path.GetTempPath(), "finaltoken-resets", Guid.NewGuid().ToString("N")), new(), new FixedClock(now));
|
||||
var currentStart = start; LimitWindow current = null!;
|
||||
for (var i = 0; i <= durations.Length; i++)
|
||||
{
|
||||
var used = i == 2 ? 100 : i == 4 ? 4 : 20 + i * 7;
|
||||
current = new(Provider.Codex, i < 4 ? "codex:secondary" : "codex:primary", "Week", used,
|
||||
currentStart.AddDays(7), TimeSpan.FromDays(7), currentStart.AddHours(6), "fixture");
|
||||
store.AddLimits([current with { SampleTime = currentStart.AddHours(1), UsedPercent = used / 2.0 }, current]);
|
||||
store.AddLimits([current with { Id = "codex_bengalfox:secondary", Label = "Week (Bengalfox)", UsedPercent = 0 }]);
|
||||
store.Add(new(Provider.Codex, currentStart.AddMinutes(30), "gpt-6.1-sol", "Fixture", new(10_000_000, 4_000_000, 0, 2_000_000, 300_000, 12_000_000), "a" + i));
|
||||
store.Add(new(Provider.Codex, currentStart.AddHours(2), "gpt-6-astra", "Fixture", new(1_000_000, 200_000, 0, 2_000_000, 300_000, 3_000_000), "b" + i));
|
||||
store.Add(new(Provider.Codex, currentStart.AddHours(2), "codex-auto-review", "Fixture", new(Input: 100_000, Total: 100_000), "c" + i));
|
||||
if (i < durations.Length) currentStart = currentStart.AddDays(durations[i]);
|
||||
}
|
||||
var shortWindow = current with { Id = "codex:primary", Length = TimeSpan.FromHours(5), ResetsAt = now.AddHours(2), UsedPercent = 99, SampleTime = now.AddMinutes(-1), Label = "Session" };
|
||||
store.AddLimits([shortWindow]);
|
||||
var claude = current with { Provider = Provider.Claude, Id = "week", Label = "Week (all models)" };
|
||||
var alternate = current with { Id = "codex_bengalfox:primary", Label = "Week (Bengalfox)", UsedPercent = 0 };
|
||||
var snapshot = FakeData.Snapshot(now) with { Providers =
|
||||
[new(Provider.Codex, ProviderStatus.Available, null, [new(current, Pace.Calculate(current, now)), new(alternate, Pace.Calculate(alternate, now)),
|
||||
new(shortWindow, Pace.Calculate(shortWindow, now))], new(), new(), new()),
|
||||
new(Provider.Claude, ProviderStatus.Available, null, [new(claude, Pace.Calculate(claude, now))], new(), null, new())] };
|
||||
return new(store, snapshot, now, PanelViewModel.QueryHistory(store, snapshot, HistoryRange.All, null, now));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Headless;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Styling;
|
||||
using Avalonia.Threading;
|
||||
using Avalonia.VisualTree;
|
||||
using FinalToken.Core;
|
||||
using Xunit;
|
||||
|
||||
namespace FinalToken.App.Tests;
|
||||
|
||||
internal static class ResetViews
|
||||
{
|
||||
public static void Verify(Action<Window, string> save)
|
||||
{
|
||||
var fixture = ResetFixture.Create();
|
||||
var clock = new FixedClock(fixture.Now);
|
||||
var model = new PanelViewModel { Snapshot = fixture.Snapshot, History = fixture.History, Range = HistoryRange.All };
|
||||
var panel = new PanelWindow(model, () => { }, () => { }, () => { }, () => { }, clock);
|
||||
panel.Show(); Dispatcher.UIThread.RunJobs();
|
||||
var headings = panel.GetVisualDescendants().OfType<TextBlock>().Where(t => t.FontSize == 20).Select(t => t.Text).ToArray();
|
||||
Assert.True(Array.IndexOf(headings, "Window charts") < Array.IndexOf(headings, "Resets"));
|
||||
Assert.True(Array.IndexOf(headings, "Resets") < Array.IndexOf(headings, "Breakdown"));
|
||||
var selector = panel.GetVisualDescendants().OfType<ComboBox>().Single(c => c.Name == "ResetWindowSelector");
|
||||
selector.SelectedIndex = 1;
|
||||
Assert.Contains(panel.GetVisualDescendants().OfType<TextBlock>(), t => t.Text == "Codex · Week (Bengalfox)");
|
||||
model.Snapshot = model.Snapshot with { MadeAt = fixture.Now.AddMinutes(-1) }; Dispatcher.UIThread.RunJobs();
|
||||
Assert.Equal(1, panel.GetVisualDescendants().OfType<ComboBox>().Single(c => c.Name == "ResetWindowSelector").SelectedIndex);
|
||||
panel.GetVisualDescendants().OfType<ComboBox>().Single(c => c.Name == "ResetWindowSelector").SelectedIndex = 0;
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
using (panel.CaptureRenderedFrame()) { }
|
||||
var card = panel.GetVisualDescendants().OfType<ResetCard>().First();
|
||||
var table = card.GetVisualDescendants().OfType<ResetTable>().Single();
|
||||
Grid Rows()
|
||||
{
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
using (panel.CaptureRenderedFrame()) { }
|
||||
return table.GetVisualDescendants().OfType<Grid>().Single(g => g.Name == "ResetRows");
|
||||
}
|
||||
void Click(string name)
|
||||
{
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
using (panel.CaptureRenderedFrame()) { }
|
||||
table.GetVisualDescendants().OfType<Button>().Single(b => b.Name == name).RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
using (panel.CaptureRenderedFrame()) { }
|
||||
}
|
||||
Assert.Equal(9, Rows().RowDefinitions.Count); // Header plus eight rows.
|
||||
var cells = Rows().Children.OfType<TextBlock>().ToArray();
|
||||
Assert.All(cells, text => Assert.IsType<DataTooltip>(ToolTip.GetTip(text)));
|
||||
Assert.Contains(cells, t => t.Text == "Running");
|
||||
Assert.Contains(cells, t => t.Text?.StartsWith("Early by ", StringComparison.Ordinal) == true);
|
||||
Assert.Contains(cells, t => t.Text?.Contains("100 %", StringComparison.Ordinal) == true);
|
||||
Click("ResetSortFinal");
|
||||
Assert.StartsWith("≈ 100 %", Rows().Children.OfType<TextBlock>().Single(t => Grid.GetRow(t) == 1 && Grid.GetColumn(t) == 4).Text);
|
||||
Click("ResetExpand");
|
||||
Assert.Equal(11, Rows().RowDefinitions.Count);
|
||||
Assert.Contains(Rows().Children.OfType<TextBlock>(), t => t.Text == "On time");
|
||||
model.Snapshot = model.Snapshot with { MadeAt = fixture.Now }; Dispatcher.UIThread.RunJobs();
|
||||
table = panel.GetVisualDescendants().OfType<ResetCard>().First().GetVisualDescendants().OfType<ResetTable>().Single();
|
||||
Assert.Equal(11, Rows().RowDefinitions.Count);
|
||||
Assert.Contains(table.GetVisualDescendants().OfType<Button>(), b => b.Content as string == "Final (peak) ↓");
|
||||
Click("ResetExpand");
|
||||
Click("ResetSortStart");
|
||||
foreach (var theme in new[] { ThemeVariant.Dark, ThemeVariant.Light })
|
||||
{
|
||||
panel.RequestedThemeVariant = theme; Dispatcher.UIThread.RunJobs();
|
||||
var viewport = panel.GetVisualDescendants().OfType<ScrollViewer>().First();
|
||||
var heading = panel.GetVisualDescendants().OfType<TextBlock>().Single(t => t.Text == "Resets" && t.FontSize == 20);
|
||||
viewport.Offset = new Vector(0, viewport.Offset.Y + heading.TranslatePoint(default, viewport)!.Value.Y - 16);
|
||||
save(panel, $"resets-panel-{theme}.png");
|
||||
}
|
||||
panel.Close();
|
||||
|
||||
var history = fixture.History.Resets[0];
|
||||
foreach (var width in new[] { 1280, 1500 })
|
||||
foreach (var theme in new[] { ThemeVariant.Dark, ThemeVariant.Light })
|
||||
{
|
||||
var content = new ResetCard(history, new(ResetColumn.Start), clock);
|
||||
var window = new Window { Width = width, Height = 1030, Content = new Border { Padding = new Thickness(24),
|
||||
Child = Ui.Stack(Ui.Text("Resets", 20), Ui.Card(Ui.Stack(Ui.Text("Codex · Week", 15), content))) }, RequestedThemeVariant = theme };
|
||||
Ui.Theme(window); window.Show();
|
||||
save(window, $"resets-{width}-{theme}.png");
|
||||
var timeline = content.GetVisualDescendants().OfType<ResetTimeline>().Single();
|
||||
window.MouseMove(timeline.TranslatePoint(new Point(timeline.Bounds.Width / 2, 80), window)!.Value);
|
||||
Assert.IsType<DataTooltip>(ToolTip.GetTip(timeline));
|
||||
var histogram = content.GetVisualDescendants().OfType<ResetHistogram>().First();
|
||||
window.MouseMove(histogram.TranslatePoint(new Point(40, 40), window)!.Value);
|
||||
Assert.IsType<DataTooltip>(ToolTip.GetTip(histogram));
|
||||
var tableTip = content.GetVisualDescendants().OfType<ResetTable>().Single().GetVisualDescendants().OfType<TextBlock>()
|
||||
.Select(t => ToolTip.GetTip(t)).OfType<DataTooltip>().First();
|
||||
Assert.Contains(tableTip.Rows, r => r.Label == "Unpriced tokens");
|
||||
Assert.Contains(tableTip.Rows, r => r.Label == "Tokens per percent");
|
||||
Assert.Contains(tableTip.Rows, r => r.Label == "Estimated figures");
|
||||
var oldest = history.Instances[^1];
|
||||
Assert.False(oldest.Early); Assert.False(oldest.Running);
|
||||
window.Close();
|
||||
}
|
||||
foreach (var theme in new[] { ThemeVariant.Dark, ThemeVariant.Light })
|
||||
{
|
||||
var tip = DataTooltip.Reset(history.Instances[1], clock);
|
||||
var window = new Window { Width = 580, Height = 1120, Content = new Border { Child = tip, Padding = new Thickness(16) }, RequestedThemeVariant = theme };
|
||||
Ui.Theme(window); window.Show(); save(window, $"resets-tooltip-{theme}.png"); window.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user