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
96 lines
5.3 KiB
C#
96 lines
5.3 KiB
C#
using FinalToken.Core.Processes;
|
|
using System.Threading.Channels;
|
|
|
|
namespace FinalToken.Core.Tests;
|
|
|
|
public sealed class TestDirectory : IDisposable
|
|
{
|
|
public string Path { get; } = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "finaltoken-test-" + Guid.NewGuid().ToString("N"));
|
|
public TestDirectory() => Directory.CreateDirectory(Path);
|
|
public string Write(string relative, string contents)
|
|
{
|
|
var path = System.IO.Path.GetFullPath(System.IO.Path.Combine(Path, relative));
|
|
Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path)!);
|
|
File.WriteAllText(path, contents);
|
|
return path;
|
|
}
|
|
public void Dispose() => Directory.Delete(Path, true);
|
|
}
|
|
|
|
public sealed class TestClock : TimeProvider
|
|
{
|
|
private readonly Channel<TimeSpan> scheduled = Channel.CreateUnbounded<TimeSpan>();
|
|
public ValueTask<TimeSpan> NextTimerAsync(CancellationToken cancellationToken) => scheduled.Reader.ReadAsync(cancellationToken);
|
|
private readonly Lock gate = new();
|
|
private DateTimeOffset now = new(2026, 10, 14, 16, 0, 0, TimeSpan.Zero);
|
|
private readonly List<TestTimer> timers = [];
|
|
public override DateTimeOffset GetUtcNow() { lock (gate) return now; }
|
|
public void Advance(TimeSpan amount)
|
|
{
|
|
List<TestTimer> due;
|
|
lock (gate)
|
|
{
|
|
now += amount;
|
|
due = timers.Where(t => t.Next <= now && !t.Disposed).ToList();
|
|
foreach (var timer in due) timer.Next = timer.Period > TimeSpan.Zero ? now + timer.Period : DateTimeOffset.MaxValue;
|
|
}
|
|
foreach (var timer in due) timer.Callback(timer.State);
|
|
}
|
|
public override ITimer CreateTimer(TimerCallback callback, object? state, TimeSpan dueTime, TimeSpan period)
|
|
{
|
|
lock (gate)
|
|
{
|
|
var timer = new TestTimer(this, callback, state, period);
|
|
timer.Change(dueTime, period);
|
|
timers.Add(timer);
|
|
scheduled.Writer.TryWrite(dueTime);
|
|
return timer;
|
|
}
|
|
}
|
|
private sealed class TestTimer(TestClock owner, TimerCallback callback, object? state, TimeSpan period) : ITimer
|
|
{
|
|
public TimerCallback Callback { get; } = callback;
|
|
public object? State { get; } = state;
|
|
public TimeSpan Period { get; private set; } = period;
|
|
public DateTimeOffset Next { get; set; }
|
|
public bool Disposed { get; private set; }
|
|
public bool Change(TimeSpan dueTime, TimeSpan newPeriod)
|
|
{
|
|
lock (owner.gate)
|
|
{
|
|
if (Disposed) return false;
|
|
Period = newPeriod;
|
|
Next = dueTime == Timeout.InfiniteTimeSpan ? DateTimeOffset.MaxValue : owner.now + dueTime;
|
|
return true;
|
|
}
|
|
}
|
|
public void Dispose() { lock (owner.gate) Disposed = true; }
|
|
public ValueTask DisposeAsync() { Dispose(); return ValueTask.CompletedTask; }
|
|
}
|
|
}
|
|
|
|
public sealed class FakeRunner : IProcessRunner
|
|
{
|
|
public List<ProcessRequest> Requests { get; } = [];
|
|
public Func<ProcessRequest, CancellationToken, Task<ProcessResult>>? Handler { get; set; }
|
|
public Task<ProcessResult> RunAsync(ProcessRequest request, CancellationToken cancellationToken)
|
|
{
|
|
Requests.Add(request);
|
|
return Handler?.Invoke(request, cancellationToken) ?? Task.FromResult(new ProcessResult(0,
|
|
request.CodexRpc ? Fixtures.CodexResponse : Fixtures.ClaudeEnvelope));
|
|
}
|
|
}
|
|
|
|
internal static class Fixtures
|
|
{
|
|
public const string ClaudeEnvelope = "{\"local_command\":\"usage\",\"num_turns\":0,\"is_error\":false,\"result\":\"Current session: 28% used · resets Oct 14, 7:30pm (Europe/Berlin)\\nCurrent week (all models): 66% used · resets Oct 18, 8pm (Europe/Berlin)\"}";
|
|
public const string CodexResponse = "{\"id\":2,\"result\":{\"rateLimits\":{\"limitId\":\"codex\",\"primary\":{\"usedPercent\":24,\"windowDurationMins\":10080,\"resetsAt\":1792479531},\"secondary\":null}}}";
|
|
public static string Claude(string id = "a", string request = "r", int input = 10, string time = "2026-10-14T16:00:00Z") =>
|
|
"""{"type":"assistant","timestamp":"@time","cwd":"/project","requestId":"@request","message":{"id":"@id","model":"fixture-model","usage":{"input_tokens":@input,"output_tokens":2,"cache_read_input_tokens":3,"cache_creation_input_tokens":4}}}"""
|
|
.Replace("@time", time).Replace("@request", request).Replace("@id", id).Replace("@input", input.ToString(System.Globalization.CultureInfo.InvariantCulture)) + "\n";
|
|
public const string Meta = "{\"type\":\"session_meta\",\"payload\":{\"id\":\"session-1\",\"cwd\":\"/project\"}}\n{\"type\":\"turn_context\",\"payload\":{\"model\":\"fixture-model\"}}\n";
|
|
public static string Codex(int input, string time = "2026-10-14T16:00:00Z") =>
|
|
"""{"type":"event_msg","timestamp":"@time","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":@input,"cached_input_tokens":0,"output_tokens":2,"reasoning_output_tokens":1,"total_tokens":@total}},"rate_limits":{"limit_id":"codex","primary":{"used_percent":24,"window_minutes":10080,"resets_at":1792479531},"secondary":null}}}"""
|
|
.Replace("@time", time).Replace("@input", input.ToString(System.Globalization.CultureInfo.InvariantCulture)).Replace("@total", (input + 2).ToString(System.Globalization.CultureInfo.InvariantCulture)) + "\n";
|
|
}
|