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
This commit was merged in pull request #2.
This commit is contained in:
2026-10-14 22:11:19 +02:00
parent 6d67f157a5
commit f8afd11679
106 changed files with 6583 additions and 0 deletions
+107
View File
@@ -0,0 +1,107 @@
using FinalToken.Core.Collectors;
using Xunit;
namespace FinalToken.Core.Tests;
public class ParserTests
{
private static readonly DateTimeOffset Now = new(2026, 10, 14, 16, 0, 0, TimeSpan.Zero);
[Fact]
public void TranscriptReturnsOnlyMetadataAndCorrectCacheTotal()
{
var usage = TranscriptParser.Parse(Fixtures.Claude(), new());
Assert.NotNull(usage);
Assert.Equal(new TokenCounts(10, 3, 4, 2, 0, 19), usage.Counts);
Assert.Equal("fixture-model", usage.Model);
Assert.Equal("/project", usage.Project);
}
[Theory]
[InlineData("{")]
[InlineData("[]")]
[InlineData("null")]
[InlineData("{\"type\":\"assistant\",\"message\":{\"usage\":{\"input_tokens\":-1}}}")]
public void TranscriptMalformedInputDoesNotThrow(string line) => Assert.Null(TranscriptParser.Parse(line, new()));
[Fact]
public void TranscriptMalformedUsageReportsDiagnostic()
{
var diagnostics = new DiagnosticLog();
Assert.Null(TranscriptParser.Parse(Fixtures.Claude().Replace("\"input_tokens\":10", "\"input_tokens\":-1"), diagnostics));
Assert.Single(diagnostics.Recent);
}
[Theory]
[InlineData("Current session: 28% used · resets Oct 14, 7:30pm (Europe/Berlin)", 5, 17, 30)]
[InlineData("Current week (all models): 66% used · resets Oct 18, 8pm (Europe/Berlin)", 168, 18, 0)]
[InlineData("Current week (Fable): 0% used • resets Oct 18, 8:00pm (Europe/Berlin)", 168, 18, 0)]
public void ClaudeUsageParsesKnownFormatsAndIanaZone(string line, int hours, int resetHour, int resetMinute)
{
var window = Assert.Single(ClaudeUsageParser.ParseText(line, Now, new()));
Assert.Equal(TimeSpan.FromHours(hours), window.Length);
Assert.Equal(resetHour, window.ResetsAt!.Value.Hour);
Assert.Equal(resetMinute, window.ResetsAt.Value.Minute);
}
[Fact]
public void ClaudeInfersNewYearWithinWindow()
{
var windows = ClaudeUsageParser.ParseText("Current week (all models): 10% used · resets Jan 2, 8pm (Europe/Berlin)",
new(2026, 12, 31, 16, 0, 0, TimeSpan.Zero), new());
Assert.Equal(2027, Assert.Single(windows).ResetsAt!.Value.Year);
}
[Theory]
[InlineData("Current session: 101% used · resets Oct 14, 7:30pm (Europe/Berlin)")]
[InlineData("Current session: 10% used · resets Oct 14, 7:30pm (Unknown/Zone)")]
[InlineData("Current session: 10% used · resets Oct 18, 8pm (Europe/Berlin)")]
[InlineData("Current session: 10% used · resets Oct 14, 3pm (Europe/Berlin)")]
[InlineData("Current session: unknown format")]
public void ClaudeUnknownFormatsBecomeDiagnostics(string line)
{
var diagnostics = new DiagnosticLog();
Assert.Empty(ClaudeUsageParser.ParseText(line, Now, diagnostics));
Assert.Single(diagnostics.Recent);
}
[Fact] public void ClaudeEnvelopeIsLocalAndZeroTurns() => Assert.Equal(2, ClaudeUsageParser.ParseEnvelope(Fixtures.ClaudeEnvelope, Now, new()).Count);
[Theory]
[InlineData("{")]
[InlineData("{\"is_error\":true}")]
[InlineData("{\"local_command\":\"usage\",\"num_turns\":1,\"is_error\":false,\"result\":\"x\"}")]
public void ClaudeRejectsInvalidEnvelope(string json) => Assert.Empty(ClaudeUsageParser.ParseEnvelope(json, Now, new()));
[Fact]
public void RolloutTracksModelAndProjectAndCumulativeCounts()
{
var cursor = new ScanCursor();
foreach (var line in Fixtures.Meta.Split('\n', StringSplitOptions.RemoveEmptyEntries)) RolloutParser.Parse(line, cursor, Now, new());
var entry = RolloutParser.Parse(Fixtures.Codex(10), cursor, Now, new());
Assert.Equal("session-1", cursor.SessionId);
Assert.Equal("fixture-model", entry.CumulativeUsage!.Model);
Assert.Equal(12, entry.CumulativeUsage.Counts.Total);
Assert.Equal("codex:primary", Assert.Single(entry.Limits).Id);
}
[Theory]
[InlineData("{")]
[InlineData("null")]
[InlineData("[]")]
[InlineData("{\"type\":\"event_msg\",\"payload\":{\"type\":\"token_count\"}}")]
public void RolloutMalformedInputIsTotal(string line) => Assert.Null(RolloutParser.Parse(line, new(), Now, new()).CumulativeUsage);
[Fact]
public void CodexReadsSchemaCamelCaseResponse()
{
var window = Assert.Single(CodexLimitsParser.ParseResponse(Fixtures.CodexResponse, Now, new()));
Assert.Equal(24, window.UsedPercent);
Assert.Equal(TimeSpan.FromDays(7), window.Length);
}
[Fact]
public void CodexReadsMultipleBuckets()
{
var json = "{\"result\":{\"rateLimitsByLimitId\":{\"a\":{\"limitId\":\"a\",\"primary\":{\"usedPercent\":1}},\"b\":{\"limitId\":\"b\",\"secondary\":{\"usedPercent\":2}}}}}";
var windows = CodexLimitsParser.ParseResponse(json, Now, new());
Assert.Equal(2, windows.Count);
Assert.All(windows, w => Assert.Null(w.Length));
}
[Fact] public void CodexRejectsInvalidWindow() => Assert.Empty(CodexLimitsParser.ParseResponse(Fixtures.CodexResponse.Replace("\"usedPercent\":24", "\"usedPercent\":-1"), Now, new()));
[Fact] public void DiagnosticsAreBoundedAndExcludeInput()
{
var diagnostics = new DiagnosticLog(capacity: 2);
for (var i = 0; i < 4; i++) TranscriptParser.Parse("private fixture text", diagnostics);
Assert.Equal(4, Assert.Single(diagnostics.Recent).Count);
Assert.All(diagnostics.Recent, d => Assert.DoesNotContain("private fixture", d.Summary));
}
}