Files
FinalToken/tests/FinalToken.Core.Tests/EarlyForecastTests.cs
T
schmidt.florian 0a08b508a2 core: plan the daily budget per day, start forecasts at 2 % and follow the limit week (#15)
The developer's feedback of 2026-10-18 named four problems at the top
of the panel: late forecasts, a daily budget that shrank with every use,
unequal Claude and Codex tiles, and an unclear "This week".

- Core: forecasts start at 2 % of a window (D7). Each day of a long
  instance has a share, an even pace and a budget. Today's budget starts
  from the use at the start of the day and stays put while today is
  spent. An estimated start-of-day use counts only the whole hours before
  the first sample's hour, so later samples and tokens do not move it.
  Week token totals follow the provider's limit week, with the calendar
  week as the named fallback; the snapshot carries `weekStart` and
  `weekWindow` (D9).
- App: tiles of one window kind show the same rows, with muted
  placeholders that say why (D8). The stale warning sits in the Used row.
  Week charts draw even pace and budget as steps per day, mark the
  instance start and reset, and label full-day rates. Token tiles say
  since when.
- CLI: prints which week it uses and since when.

Closes #13
2026-10-18 10:53:00 +02:00

91 lines
4.2 KiB
C#

using Xunit;
namespace FinalToken.Core.Tests;
public class EarlyForecastTests
{
private static readonly DateTimeOffset Now = new(2026, 10, 15, 16, 45, 0, TimeSpan.Zero);
private static LimitWindow Week(double used = 39) => new(Provider.Codex, "primary", "Week", used,
new(2026, 10, 22, 6, 18, 0, TimeSpan.Zero), TimeSpan.FromDays(7), Now, "fixture");
[Fact] public void AheadCodexWeekProjectsEarly()
{
var window = Week(); var pace = Pace.Calculate(window, Now);
Assert.Equal(PaceState.Ahead, pace.State);
Assert.InRange(pace.ElapsedFraction!.Value, .061, .063);
Assert.NotNull(pace.Projection);
Assert.InRange(pace.Projection.Value, Now, window.ResetsAt!.Value);
}
[Fact] public void AheadCodexWeekHasAverageForecast() => Assert.NotNull(LimitAnalysis.AverageUseAtReset(Week(), Now));
[Fact] public void AheadCodexWeekHasRecentForecast()
{
var window = Week();
var recent = LimitAnalysis.Recent(window, [window with { SampleTime = Now.AddHours(-4), UsedPercent = 20 }], Now);
Assert.NotNull(recent); Assert.NotNull(recent.ReachesLimit);
Assert.Equal(TimeSpan.FromHours(4), recent.Span);
}
[Fact] public void AheadCodexPopupIncludesProjection()
{
var window = Week();
var popup = new PopupDecider().Observe(window, Pace.Calculate(window, Now), new Settings());
Assert.NotNull(popup); Assert.NotNull(popup.Projection);
}
[Fact] public void OnePercentOneHourIntoWeekDoesNotForecast()
{
var window = Week(1) with { ResetsAt = Now.AddDays(7).AddHours(-1) };
Assert.Equal(PaceState.OnTrack, Pace.Calculate(window, Now).State);
Assert.Null(Pace.Calculate(window, Now).Projection);
Assert.Null(LimitAnalysis.AverageUseAtReset(window, Now));
Assert.Null(LimitAnalysis.Recent(window, [], Now));
}
[Theory]
[InlineData(10, true)]
[InlineData(40, false)]
public void AllForecastsFollowConfiguredThreshold(double threshold, bool eligible)
{
var window = Week() with { Length = TimeSpan.FromDays(28), ResetsAt = Now.AddDays(28).AddHours(-4) }; var pace = Pace.Calculate(window, Now, threshold);
Assert.Equal(eligible, pace.State == PaceState.Ahead);
Assert.Equal(eligible, Pace.CanForecast(window, Now, threshold));
Assert.Equal(eligible, pace.Projection is not null);
Assert.Equal(eligible, LimitAnalysis.AverageUseAtReset(window, Now, pace) is not null);
Assert.Equal(eligible, LimitAnalysis.Recent(window,
[window with { SampleTime = Now.AddHours(-4), UsedPercent = 20 }], Now, pace) is not null);
}
[Fact] public void ExactlyAheadThresholdEnablesEarlyForecasts()
{
var window = Week(); var threshold = Pace.Calculate(window, Now).Delta!.Value;
var pace = Pace.Calculate(window, Now, threshold);
Assert.Equal(PaceState.Ahead, pace.State);
Assert.NotNull(pace.Projection);
Assert.NotNull(LimitAnalysis.AverageUseAtReset(window, Now, pace));
Assert.NotNull(LimitAnalysis.Recent(window, [window with { SampleTime = Now.AddHours(-4), UsedPercent = 20 }], Now, pace));
}
[Fact] public void UnderPaceCodexAtSixPointFourPercentShowsUseAtReset()
{
var window = Week(4) with { ResetsAt = Now + TimeSpan.FromTicks(TimeSpan.FromDays(7).Ticks * 936 / 1000) };
var pace = Pace.Calculate(window, Now);
Assert.Equal(PaceState.OnTrack, pace.State);
Assert.Null(pace.Projection);
Assert.Equal(62.5, LimitAnalysis.AverageUseAtReset(window, Now)!.Value, 8);
}
[Fact] public void ForecastStartIsTwoPercentIntoWindow()
{
var window = Week(1);
Assert.Equal(window.ResetsAt - TimeSpan.FromTicks(TimeSpan.FromDays(7).Ticks * 49 / 50), Pace.ForecastStartsAt(window));
Assert.Null(Pace.ForecastStartsAt(window with { Length = null }));
Assert.Null(Pace.ForecastStartsAt(window with { ResetsAt = null }));
Assert.Null(Pace.ForecastStartsAt(window with { Length = TimeSpan.Zero }));
var start = Pace.ForecastStartsAt(window)!.Value;
Assert.False(Pace.CanForecast(window, start.AddTicks(-1), 40));
Assert.True(Pace.CanForecast(window, start, 40));
}
}