Files
FinalToken/tests/FinalToken.Core.Tests/PartialSettingsTests.cs
T
schmidt.florian f8afd11679 feat: add FinalToken v1 for Claude and Codex usage (#2)
FinalToken v1: a NativeAOT tray app for Windows 11 and CachyOS that tracks
Claude Code and Codex usage and limits through their CLIs only, with no
provider sign-in and no tokens (R1-R7, D1-D11).

- Core: incremental collectors for Claude transcripts and Codex
  rollouts, limit probes through `claude -p /usage` and
  `codex app-server`, a journaled history store, the refresh engine,
  pace and popup decisions, and the snapshot.json contract.
- App (Avalonia): stats panel with limit bars, pace, projections, token
  totals, charts, limit history and breakdowns; tray, settings,
  non-activating pace popups, single-instance activation, start with
  the OS, and the Off/Ask/Automatic Stream Deck plugin install.
- Stream Deck plugin (SDK v2, NativeAOT) for Elgato and OpenDeck.
- finaltoken-cli, tools/publish.py, the documentation check and the
  agent review workflow.

Closes #1
2026-10-14 22:11:19 +02:00

96 lines
5.5 KiB
C#

using System.Text.Json;
using Xunit;
namespace FinalToken.Core.Tests;
public sealed class PartialSettingsTests
{
[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);
}
}