core: add the solution, Core engine, CLI and tests
Core implements the CLI-only collectors for Claude and Codex (D2 to D4), the history store (D5), the refresh engine (D6), pace and popup decisions (D7, D8), the snapshot contract (D9) and the Stream Deck installer contract (D11). finaltoken status runs one refresh and prints the snapshot. The app and plugin projects are placeholders so the solution file is complete before they are built in parallel. Refs #1
This commit is contained in:
@@ -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("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(2, diagnostics.Recent.Count);
|
||||
Assert.All(diagnostics.Recent, d => Assert.DoesNotContain("private fixture", d.Summary));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user