- A GUI-subsystem host has no console code page, so .NET decoded the probes' UTF-8 output with code page 0 and Claude's /usage separators never matched: Claude showed as unavailable in the app but not in the CLI. Probe output is now decoded as UTF-8 and every probe gets valid standard handles, with stdin closed unless it is the Codex RPC. A GUI-subsystem test host reproduces it. - The /usage parser stops at the contributing section and only reports lines that start like a limit line. - snapshot.json no longer defaults appPath to the reader's own path. The app passes its path to the engine and a CLI refresh keeps it, so a Stream Deck key always starts the app (D9, D10). - The CLI is finaltoken-cli on every platform: FinalToken.exe and finaltoken.exe cannot share a folder on Windows. - Day series follow the local calendar day, as Today does. - The dev-only migration from the first store format is gone. - README.md is a short user guide. Refs #1
108 lines
5.4 KiB
C#
108 lines
5.4 KiB
C#
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));
|
|
}
|
|
}
|