Files
schmidt.florian adb8f5b88d app: redesign the panel and add limit forecasts and daily week use (#6)
Redesign the stats panel after the developer's feedback on v1 and add
limit forecasts, daily week use and token-based estimates (D7, D8).

- Panel: a fixed header with range and provider controls, limit tiles
  with pace chips, meters and forecasts, week use per day, token KPIs
  and charts, one burn-up chart per limit window with past instances,
  and a sortable breakdown whose "Other" row expands. One tooltip
  component for every chart, tile and table.
- Core (D7): average and recent-rate forecasts, the reach date whenever
  use is over pace, days of a long window with even pace and daily
  budget, window instances derived from resets with a 15-minute
  tolerance, and estimates from price-weighted tokens where samples are
  missing. Forecasts start at 10 % of a window, or as soon as it is
  ahead, so a Codex week far ahead right after its reset warns too.
- Settings, popups and the version (1.1.0) follow the new design.

Closes #5
Closes #7
2026-10-15 19:06:04 +02:00

113 lines
6.2 KiB
C#

using System.Text.Json;
using Xunit;
namespace FinalToken.Core.Tests;
public sealed class PartialSettingsTests
{
[Fact]
public void MissingSettingsIsNormalFirstStart()
{
using var directory = new TestDirectory();
var diagnostics = new DiagnosticLog();
Assert.Equal(new Settings(), SettingsFile.Load(directory.Path, diagnostics));
Assert.Empty(diagnostics.Recent);
}
[Fact]
public void PresentMalformedSettingsIsDiagnostic()
{
using var directory = new TestDirectory();
directory.Write("settings.json", "{broken");
var diagnostics = new DiagnosticLog();
Assert.Equal(new Settings(), SettingsFile.Load(directory.Path, diagnostics));
Assert.Equal("settings-defaults", Assert.Single(diagnostics.Recent).Code);
}
[Theory]
[InlineData("{}")]
[InlineData("{\"refreshInterval\":\"00:00:30\"}")]
[InlineData("{\"claude\":{\"enabled\":false},\"codex\":{\"cliPath\":\"fixture\"}}")]
[InlineData("{\"claude\":{},\"codex\":{\"popupBehind\":false}}")]
[InlineData("{\"refreshInterval\":\"00:00:30\",\"future\":123,\"claude\":{\"popupAhead\":false,\"future\":{}}}")]
public void PartialFilesKeepValuesAndDefaultAbsentProperties(string json)
{
using var directory = new TestDirectory();
directory.Write("settings.json", json);
var diagnostics = new DiagnosticLog();
var settings = SettingsFile.Load(directory.Path, diagnostics);
var expected = new Settings();
using var doc = JsonDocument.Parse(json);
if (doc.RootElement.TryGetProperty("refreshInterval", out _)) expected = expected with { RefreshInterval = TimeSpan.FromSeconds(30) };
if (json.Contains("\"enabled\":false")) expected = expected with { Claude = expected.Claude with { Enabled = false } };
if (json.Contains("\"cliPath\"")) expected = expected with { Codex = expected.Codex with { CliPath = "fixture" } };
if (json.Contains("\"popupAhead\":false")) expected = expected with { Claude = expected.Claude with { PopupAhead = false } };
if (json.Contains("\"popupBehind\":false")) expected = expected with { Codex = expected.Codex with { PopupBehind = false } };
Assert.Equal(expected, settings);
Assert.Empty(diagnostics.Recent);
}
[Theory]
[InlineData("\"Automatic\"", InstallMode.Automatic)]
[InlineData("\"Ask\"", InstallMode.Ask)]
[InlineData("\"Off\"", InstallMode.Off)]
[InlineData("2", InstallMode.Automatic)]
public void InstallModeAcceptsNamesAndExistingNumericFiles(string value, InstallMode expected)
{
using var directory = new TestDirectory();
directory.Write("settings.json", "{\"streamDeckInstallMode\":" + value + "}");
var diagnostics = new DiagnosticLog();
Assert.Equal(new Settings { StreamDeckInstallMode = expected }, SettingsFile.Load(directory.Path, diagnostics));
Assert.Empty(diagnostics.Recent);
}
[Theory]
[InlineData("\"probeInterval\":\"00:00:10\"")]
[InlineData("\"paceThreshold\":0")]
[InlineData("\"probeInterval\":\"invalid\"")]
[InlineData("\"streamDeckInstallMode\":99")]
[InlineData("\"streamDeckInstallMode\":\"Unknown\"")]
[InlineData("\"claude\":null")]
[InlineData("\"codex\":{\"enabled\":\"invalid\"}")]
public void OneInvalidPropertyStillFallsBackForWholeFile(string invalid)
{
using var directory = new TestDirectory();
directory.Write("settings.json", "{\"refreshInterval\":\"00:00:30\"," + invalid + "}");
var diagnostics = new DiagnosticLog();
Assert.Equal(new Settings(), SettingsFile.Load(directory.Path, diagnostics));
Assert.Equal("settings-defaults", Assert.Single(diagnostics.Recent).Code);
}
[Fact]
public void SnapshotKeepsOptionalVersionInitializer()
{
using var directory = new TestDirectory();
directory.Write("snapshot.json", """{"madeAt":"2026-10-14T16:00:00Z","refreshInterval":"00:01:00","providers":[]}""");
var snapshot = SnapshotFile.Read(directory.Path).Snapshot;
Assert.NotNull(snapshot); Assert.Equal(1, snapshot.V); Assert.Null(snapshot.AppPath);
}
[Theory]
[InlineData("{\"v\":1,\"refreshInterval\":\"00:01:00\",\"providers\":[]}")]
[InlineData("{\"v\":1,\"madeAt\":\"2026-10-14T16:00:00Z\",\"providers\":[]}")]
[InlineData("{\"v\":1,\"madeAt\":\"2026-10-14T16:00:00Z\",\"refreshInterval\":\"00:01:00\"}")]
public void SnapshotStillRejectsMissingRequiredFields(string json)
{
using var directory = new TestDirectory();
directory.Write("snapshot.json", json);
Assert.Null(SnapshotFile.Read(directory.Path).Snapshot);
}
[Fact]
public void HistoryTypesKeepDefaultsForAbsentProperties()
{
var state = JsonSerializer.Deserialize("{\"sequence\":2}", CoreJsonContext.Default.HistoryState)!;
Assert.Equal(1, state.V); Assert.Equal(2, state.Sequence);
Assert.Empty(state.ClaudeKeys); Assert.Empty(state.CodexTotals); Assert.Empty(state.CodexTimes);
Assert.Empty(state.Cursors); Assert.Empty(state.LatestWindows); Assert.Empty(state.LastSamples);
var partition = JsonSerializer.Deserialize("{}", CoreJsonContext.Default.HistoryPartition)!;
Assert.Equal(1, partition.V); Assert.Empty(partition.Buckets); Assert.Empty(partition.Limits);
var transaction = JsonSerializer.Deserialize("{\"sequence\":3}", CoreJsonContext.Default.HistoryTransaction)!;
Assert.Equal(1, transaction.V); Assert.Equal(3, transaction.Sequence);
Assert.Empty(transaction.Buckets); Assert.Empty(transaction.Limits); Assert.Empty(transaction.ClaudeKeys);
Assert.Empty(transaction.ExpiredClaudeKeys); Assert.Empty(transaction.CodexTotals); Assert.Empty(transaction.CodexTimes);
Assert.Empty(transaction.Cursors); Assert.Empty(transaction.LatestWindows); Assert.Empty(transaction.LastSamples);
state = JsonSerializer.Deserialize("{\"cursors\":{\"fixture\":{\"model\":\"fixture\"}}}", CoreJsonContext.Default.HistoryState)!;
Assert.Equal(-1, state.Cursors["fixture"].ObservedLength); Assert.Equal("", state.Cursors["fixture"].PrefixHash);
Assert.Empty(state.Cursors["fixture"].LatestLimits);
}
}