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
92 lines
5.0 KiB
C#
92 lines
5.0 KiB
C#
using FinalToken.Core.Processes;
|
|
|
|
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 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);
|
|
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";
|
|
}
|