Files
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

60 lines
3.5 KiB
C#

using System.Text.Json;
using Xunit;
namespace FinalToken.Core.Tests;
public sealed class WeekTotalsTests
{
[Theory]
[InlineData("limit", true)]
[InlineData("family", false)]
[InlineData("expired", false)]
[InlineData("missing", false)]
[InlineData("mixed", true)]
public async Task EngineSelectsProviderWeekOrCalendarFallback(string kind, bool limit)
{
using var directory = new TestDirectory();
var clock = new WeekClock(); var now = clock.GetUtcNow();
await using var engine = new RefreshEngine(directory.Path, clock, claudeDirectory: directory.Path, codexDirectory: directory.Path,
cliDiscovery: (_, _) => new(null, "Fixture CLI missing."));
var start = new DateTimeOffset(2026, 10, 10, 22, 40, 0, TimeSpan.Zero);
var week = new LimitWindow(Provider.Codex, "week", "Week", 30, start.AddDays(7), TimeSpan.FromDays(7), now, "fixture");
var family = week with { Id = "family", Label = "Week (Fable)", Length = TimeSpan.FromDays(8) };
engine.History.AddLimits(kind switch
{
"limit" => [week], "family" => [family], "expired" => [week with { ResetsAt = now.AddHours(-1) }],
"mixed" => [family, week, week with { Id = "short", Length = TimeSpan.FromDays(2) }], _ => []
});
foreach (var (time, count) in new[] { (now.AddDays(-5), 10), (now.AddDays(-3), 20), (now.AddDays(-1), 30) })
engine.History.Add(new(Provider.Codex, time, "fixture", "fixture", new(Total: count), count.ToString()));
var snapshot = await engine.RefreshNowAsync(true, TestContext.Current.CancellationToken);
var provider = snapshot.Providers.Single(p => p.Provider == Provider.Codex);
Assert.Equal(limit ? 50 : 30, provider.Week.Total);
Assert.Equal(limit ? start : new DateTimeOffset(2026, 10, 12, 0, 0, 0, TimeSpan.Zero), provider.WeekStart);
Assert.Equal(limit ? "week" : null, provider.WeekWindow);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void SourceGeneratedJsonReadsNewAndLegacyWeekFields(bool limit)
{
var start = new DateTimeOffset(2026, 10, 10, 22, 40, 0, TimeSpan.Zero);
var snapshot = new Snapshot { MadeAt = start.AddDays(2), RefreshInterval = TimeSpan.FromMinutes(1),
Providers = [new(Provider.Codex, ProviderStatus.Available, null, [], new(), null, new(Total: 30), start, limit ? "week" : null)] };
var json = JsonSerializer.Serialize(snapshot, CoreJsonContext.Default.Snapshot);
Assert.Contains("\"weekStart\"", json);
Assert.Equal(limit, json.Contains("\"weekWindow\"", StringComparison.Ordinal));
var provider = Assert.Single(JsonSerializer.Deserialize(json, CoreJsonContext.Default.Snapshot)!.Providers);
Assert.Equal(start, provider.WeekStart); Assert.Equal(limit ? "week" : null, provider.WeekWindow);
const string old = """{"v":1,"madeAt":"2026-10-14T16:00:00Z","refreshInterval":"00:01:00","providers":[{"provider":1,"status":0,"windows":[],"today":{},"session":null,"week":{"total":30}}]}""";
var legacy = JsonSerializer.Deserialize(old, CoreJsonContext.Default.Snapshot)!;
Assert.Equal(1, legacy.V); Assert.Equal(30, legacy.Providers[0].Week.Total);
Assert.Equal(default, legacy.Providers[0].WeekStart); Assert.Null(legacy.Providers[0].WeekWindow);
}
private sealed class WeekClock : TimeProvider
{
public override DateTimeOffset GetUtcNow() => new(2026, 10, 14, 16, 0, 0, TimeSpan.Zero);
public override TimeZoneInfo LocalTimeZone => TimeZoneInfo.Utc;
}
}