Files
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

172 lines
10 KiB
C#

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);
}
[Theory]
[InlineData("de-DE", "≈ 4,5 limits", "≈ 94,1 %")]
[InlineData("en-US", "≈ 4.5 limits", "≈ 94.1 %")]
public void ResetFiguresFollowTheUsersCultureExceptUsd(string culture, string limits, string percent)
{
var previous = CultureInfo.CurrentCulture;
try
{
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture);
Assert.Equal("≈ $1,234", Display.Money(1234)); Assert.Equal("≈ $99.25", Display.Money(99.25m));
Assert.Equal("Unpriced", Display.Money(null)); Assert.Equal(limits, Display.Limits(450));
Assert.Equal(percent, Display.ResetPercent(94.1, UseOrigin.Estimated));
Assert.Equal(percent[2..], Display.ResetPercent(94.1, UseOrigin.Measured));
}
finally { CultureInfo.CurrentCulture = previous; }
}
[Fact] public void ResetDurationsCannotWrapInsideTheFigure()
{
Assert.Equal("0\u00a0min", Display.ResetDuration(TimeSpan.Zero));
Assert.Equal("39\u00a0d\u00a00\u00a0h", Display.ResetDuration(TimeSpan.FromDays(39)));
Assert.Equal("≈\u00a015,1\u00a0M\u00a0tokens", Display.ResetFigure("≈ 15,1 M tokens"));
}
[Fact] public void OverlappingSkippedSpansGetSeparateLanesInTimeOrder()
{
var fixture = ResetFixture.Overlapping();
var history = fixture.History.Resets[0];
var layout = ResetTimeline.CalculateLayout(history, fixture.Now);
Assert.Equal(5, history.Totals.Early);
Assert.True(history.Instances.Max(r => r.Start) - history.Instances.Min(r => r.Start) < TimeSpan.FromDays(10));
Assert.Equal(5, layout.LaneCount);
Assert.Equal([0, 1, 2, 3, 4], layout.Skipped.Select(s => s.Lane));
Assert.Equal(history.Instances.Where(r => r.Early).OrderBy(r => r.End), layout.Skipped.Select(s => s.Instance));
}
[Fact] public void SkippedSpansReuseTheFirstLaneAsSoonAsItIsFree()
{
var fixture = ResetFixture.Create();
var layout = ResetTimeline.CalculateLayout(fixture.History.Resets[0], fixture.Now);
Assert.Equal(4, layout.LaneCount);
Assert.Equal([0, 1, 0, 1, 2, 3, 0], layout.Skipped.Select(s => s.Lane));
Assert.Equal(layout.Skipped[2].Instance.Reset, layout.Skipped[^1].Instance.End);
foreach (var lane in layout.Skipped.GroupBy(s => s.Lane))
{
var rows = lane.Select(s => s.Instance).ToArray();
for (var i = 1; i < rows.Length; i++) Assert.True(rows[i - 1].Reset <= rows[i].End);
}
}
[Fact] public void TimelineEndsAtNowOrTheLastEarlyNominalResetAndExcludesTheRunningReset()
{
var running = ResetFixture.RunningOnly();
var history = running.History.Resets[0];
var layout = ResetTimeline.CalculateLayout(history, running.Now);
Assert.Equal(history.Instances[0].Start, layout.From);
Assert.Equal(running.Now, layout.To); Assert.Empty(layout.Skipped);
Assert.True(layout.To < history.Instances[0].Reset);
var early = ResetFixture.Overlapping();
history = early.History.Resets[0];
layout = ResetTimeline.CalculateLayout(history, early.Now);
Assert.Equal(history.Instances.Min(r => r.Start), layout.From);
Assert.Equal(history.Instances.Where(r => r.Early).Max(r => r.Reset), layout.To);
Assert.True(layout.To > early.Now); Assert.True(layout.To < history.Instances[0].Reset);
var later = early.Now.AddDays(30);
Assert.Equal(later, ResetTimeline.CalculateLayout(history, later).To);
var onTime = ResetFixture.OnTimeOnly();
history = onTime.History.Resets[0];
history = history with { Instances = history.Instances.Where(r => !r.Running).ToArray() };
Assert.Equal(onTime.Now, ResetTimeline.CalculateLayout(history, onTime.Now).To);
}
[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()
=> Create([7, 1, 2, 7, 1, 2, 1, 1, 2]);
public static ResetFixture RunningOnly() => Create([]);
public static ResetFixture OnTimeOnly() => Create([7, 7]);
public static ResetFixture Overlapping() => Create([1, 1, 1, 1, 1]);
public static ResetFixture ManyModels()
{
var fixture = OnTimeOnly();
var start = fixture.History.Resets[0].Instances[1].Start;
string[] models = ["gpt-6-sol", "gpt-6-luna", "gpt-5.6-sol", "gpt-5.6-terra", "gpt-5.6-luna", "gpt-5.4-mini"];
for (var i = 0; i < models.Length; i++)
{
var count = 2_000_123 - i * 200_001;
fixture.Store.Add(new(Provider.Codex, start.AddHours(2), models[i], "Fixture", new(Input: count, Total: count), "extra" + i));
}
return fixture with { History = PanelViewModel.QueryHistory(fixture.Store, fixture.Snapshot, HistoryRange.All, null, fixture.Now) };
}
private static ResetFixture Create(int[] durations)
{
var start = new DateTimeOffset(2026, 9, 12, 6, 0, 0, TimeSpan.Zero);
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));
}
}