Files
FinalToken/tests/FinalToken.App.Tests/ResetViews.cs
T
schmidt.florian 82fece85f5 app: show the reset history of each long limit window (#16)
The developer asked for a detailed breakdown of every reset (feedback of
2026-10-18). Limits do not only reset at the end of their week: most Codex
weeks in the local history ended early.

- Core: a limit's samples form a window series (provider, limit name and
  length), so the history follows Codex's week across slots (D7). Instance
  spans are built once. One backward walk gives each instance, real or
  inferred, the start of the instance after it. The hour that holds a
  boundary goes to the instance that starts in it, or else to the one that
  ends in it (#9). A stale sample that would put its instance after a later
  one is ignored. The reset history lists the real instances with their
  figures, totals and early-reset hours (D12). API value uses a dated list
  price table; unknown models stay unpriced (D13).
- App: a Resets section per long window with totals, a timeline of
  burn-ups side by side with skipped time in lanes below, early resets by
  hour and weekday, and a sortable table (D8). The reset tooltip lists
  every model with its tokens and API value. Tall tooltips open beside the
  pointer, and Fluent's 320 px tooltip cap no longer wraps data values.
- Docs: D12 and D13 are new; D7 and D8 cover window series, boundary
  hours and the Resets section.

Closes #14
Closes #9
2026-10-18 11:12:38 +02:00

215 lines
14 KiB
C#

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)
{
VerifyEveryModelsTokensAndValue(save);
VerifyScenarios(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();
}
}
private static void VerifyEveryModelsTokensAndValue(Action<Window, string> save)
{
var fixture = ResetFixture.ManyModels();
var history = fixture.History.Resets[0];
var row = history.Instances[1];
Assert.False(row.Running); Assert.Equal(9, row.Models.Count);
foreach (var theme in new[] { ThemeVariant.Dark, ThemeVariant.Light })
{
var card = new ResetCard(history, new(ResetColumn.Start), new FixedClock(fixture.Now));
var window = new Window { Width = 1280, Height = 1030, RequestedThemeVariant = theme,
Content = new Border { Padding = new Thickness(24), Child = Ui.Card(Ui.Stack(Ui.Text("Codex · Week", 15), card)) } };
Ui.Theme(window); window.Show(); Dispatcher.UIThread.RunJobs();
using (window.CaptureRenderedFrame()) { }
var cell = card.GetVisualDescendants().OfType<Grid>().Single(g => g.Name == "ResetRows")
.Children.OfType<TextBlock>().Single(t => Grid.GetRow(t) == 2 && Grid.GetColumn(t) == 0);
var tip = Assert.IsType<DataTooltip>(ToolTip.GetTip(cell));
foreach (var model in row.Models)
{
var detail = Assert.Single(tip.Rows, r => r.Label == model.Model);
Assert.Equal(DataTooltip.Count(model.Tokens.Total) + " · " + Display.Money(model.ApiValue), detail.Value);
}
Assert.Equal(row.Models.Select(m => m.Model), tip.Rows.Where(r => row.Models.Any(m => m.Model == r.Label)).Select(r => r.Label));
Assert.DoesNotContain(tip.Rows, r => r.Label == "Other models");
var pointer = cell.TranslatePoint(new Point(12, cell.Bounds.Height / 2), window)!.Value;
window.MouseMove(pointer); ToolTip.SetIsOpen(cell, true); Dispatcher.UIThread.RunJobs();
using (window.CaptureRenderedFrame()) { }
Assert.True(ToolTip.GetIsOpen(cell)); Assert.True(tip.Bounds.Width > 320);
var bounds = new PixelRect(tip.PointToScreen(default), PixelSize.FromSize(tip.Bounds.Size, 1));
Assert.False(bounds.Contains(window.PointToScreen(pointer)));
Assert.True(window.Screens.ScreenFromWindow(window)!.WorkingArea.Contains(bounds), $"tooltip {bounds}");
save(window, $"resets-many-models-hover-{theme}.png");
ToolTip.SetIsOpen(cell, false); window.Close();
}
}
private static void VerifyScenarios(Action<Window, string> save)
{
foreach (var (name, fixture) in new[] { ("running-only", ResetFixture.RunningOnly()),
("on-time", ResetFixture.OnTimeOnly()), ("overlapping", ResetFixture.Overlapping()) })
foreach (var theme in new[] { ThemeVariant.Dark, ThemeVariant.Light })
{
var history = fixture.History.Resets[0];
var card = new ResetCard(history, new(ResetColumn.Start), new FixedClock(fixture.Now));
var window = new Window { Width = 1280, Height = 950, RequestedThemeVariant = theme,
Content = new Border { Padding = new Thickness(24), Child = Ui.Card(Ui.Stack(Ui.Text("Codex · Week", 15), card)) } };
Ui.Theme(window); window.Show(); Dispatcher.UIThread.RunJobs();
using (window.CaptureRenderedFrame()) { }
save(window, $"resets-{name}-{theme}.png");
TextBlock[] TileBlocks(string label) => card.GetVisualDescendants().OfType<StackPanel>()
.Select(s => s.Children.OfType<TextBlock>().ToArray())
.Single(blocks => blocks.Length == 3 && blocks[0].Text == label);
string[] Tile(string label) => TileBlocks(label).Select(t => t.Text!).ToArray();
Assert.Equal(history.Totals.Finished.ToString(), Tile("Resets")[1]);
var caption = history.Totals.Finished == 0 ? "none yet" : history.Totals.Early + "\u00a0early";
Assert.Equal(caption + " · 1\u00a0running", Tile("Resets")[2]);
var texts = card.GetVisualDescendants().OfType<TextBlock>().Select(t => t.Text).ToArray();
Assert.Contains("early resets " + history.Totals.Early + " of " + history.Totals.Finished,
texts.Single(t => t?.StartsWith("Median instance ", StringComparison.Ordinal) == true || t?.StartsWith("No finished instance yet", StringComparison.Ordinal) == true));
if (history.Totals.Finished == 0)
{
Assert.Equal(["Unused", "—", "no finished instance"], Tile("Unused"));
Assert.Equal(Ui.Colour("Muted", theme == ThemeVariant.Dark).ToString(), TileBlocks("Unused")[1].Foreground?.ToString());
Assert.Contains(texts, text => text?.StartsWith("No finished instance yet", StringComparison.Ordinal) == true);
}
if (history.Totals.Early == 0)
{
Assert.Equal(["Gift", "—", "no early reset"], Tile("Gift"));
Assert.Equal(Ui.Colour("Muted", theme == ThemeVariant.Dark).ToString(), TileBlocks("Gift")[1].Foreground?.ToString());
Assert.Contains("No early resets in this range", texts);
Assert.Empty(card.GetVisualDescendants().OfType<ResetHistogram>());
}
else
{
Assert.Equal(2, card.GetVisualDescendants().OfType<ResetHistogram>().Count());
Assert.Contains(Display.ResetDuration(history.Totals.Skipped) + " skipped", Tile("Gift")[2]);
Assert.DoesNotContain("≈ ", Tile("Gift")[2]);
var timeline = card.GetVisualDescendants().OfType<ResetTimeline>().Single();
var layout = ResetTimeline.CalculateLayout(history, fixture.Now);
foreach (var skipped in layout.Skipped)
{
ToolTip.SetIsOpen(timeline, false);
window.MouseMove(new Point(4, 4)); Dispatcher.UIThread.RunJobs();
var mid = skipped.Instance.End + skipped.Instance.Skipped / 2;
var x = 42 + (mid - layout.From).TotalSeconds / (layout.To - layout.From).TotalSeconds * (timeline.Bounds.Width - 58);
var pointer = timeline.TranslatePoint(new Point(x, 181 + skipped.Lane * 10), window)!.Value;
window.MouseMove(pointer);
Assert.True(ToolTip.GetTip(timeline) is DataTooltip, $"{name} {theme}, lane {skipped.Lane}, timeline {timeline.Bounds}");
var tooltip = Assert.IsType<DataTooltip>(ToolTip.GetTip(timeline));
// The tall tooltip opens beside the pointer; over it, it would take the pointer from the timeline.
Assert.True(ToolTip.GetIsOpen(timeline));
Assert.True(tooltip.Bounds.Width > 320, $"tooltip width {tooltip.Bounds.Width}");
var tipBounds = new PixelRect(tooltip.PointToScreen(default), PixelSize.FromSize(tooltip.Bounds.Size, 1));
Assert.False(tipBounds.Contains(window.PointToScreen(pointer)), $"{name} {theme}, lane {skipped.Lane}, tooltip {tipBounds}, pointer {window.PointToScreen(pointer)}, placement {ToolTip.GetPlacement(timeline)}");
if (skipped == layout.Skipped[0] || skipped == layout.Skipped[^1]) save(window, $"resets-{name}-hover-lane{skipped.Lane}-{theme}.png");
Assert.Contains(tooltip.Rows, r => r.Label == "End" && r.Value == Display.Projection(skipped.Instance.End, new FixedClock(fixture.Now)));
}
ToolTip.SetIsOpen(timeline, false);
window.MouseMove(timeline.TranslatePoint(new Point(timeline.Bounds.Width - 10, 80), window)!.Value);
Assert.Null(ToolTip.GetTip(timeline));
window.MouseMove(new Point(4, 4));
}
window.Close();
}
}
}