Files
FinalToken/tests/FinalToken.Core.Tests/EarlyForecastTests.cs
T
Florian Schmidt 33458a40ed
agent-review agent review: pass
fix: forecast windows that are ahead early in their window (D7)
The reach date, the dotted forecast and the popup warning showed only
for Claude. Codex had reset its week ten hours before and stood at
39 %, ahead by 33 points, but every forecast shared a gate that
opened only after 10 % of the window. Nothing in the forecast paths
was provider-specific; Codex was just the window early in its week.

The gate now also opens as soon as the window is ahead, with the
configured threshold, so it never disagrees with the chip. Average,
recent and popup forecasts all use it. Until forecasts start, the
tile and the window chart say when they will.

Refs #5
2026-10-15 18:58:06 +02:00

82 lines
3.7 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 AheadCodexWeekProjectsBeforeTenPercent()
{
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(); 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 ForecastStartIsTenPercentIntoWindow()
{
var window = Week(1);
Assert.Equal(window.ResetsAt - TimeSpan.FromDays(6.3), 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));
}
}