LimitAnalysis computes the use at reset at the average rate, a recent-rate forecast from the last 24 h of samples, and for windows longer than a day the use of each local day, today's use, the even pace and the daily budget. Pace now projects the date of reaching 100 % whenever usage is over pace, not only past the threshold. Samples whose resets lie within 15 minutes form one window instance, because Claude's reset times drift by a minute between probes. TokenSeriesByModel replaces one query per chart point, and a missing settings.json on first start is no longer a diagnostic. Refs #5
113 lines
6.2 KiB
C#
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);
|
|
}
|
|
}
|