The store rewrote all of history.json, 19 MB here, on every 60 s refresh. It now appends each refresh's changes to a journal and compacts into monthly files and a state checkpoint at 8 MiB, so an idle refresh writes only snapshot.json. Bucket updates are keyed, unchanged limit samples are kept at most hourly, Codex replays are bounded by session totals, and Claude dedupe keys expire after 60 days. Readers of atomically replaced files allow delete sharing, and the replace retries briefly, so the Stream Deck plugin reading snapshot.json cannot make a refresh fail. Claude's <synthetic> entries are skipped, and repeated diagnostics fold into one entry with a count. 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("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));
|
|
}
|
|
}
|