feat: add FinalToken v1 for Claude and Codex usage (#2)

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
This commit was merged in pull request #2.
This commit is contained in:
2026-10-14 22:11:19 +02:00
parent 6d67f157a5
commit f8afd11679
106 changed files with 6583 additions and 0 deletions
@@ -0,0 +1,129 @@
using FinalToken.Core.StreamDeck;
using Xunit;
namespace FinalToken.Core.Tests.StreamDeck;
public sealed class InstallerTests : IDisposable
{
private readonly string root = Path.Combine(Path.GetTempPath(), "finaltoken-installer-" + Guid.NewGuid().ToString("N"));
private string Shipped => Path.Combine(root, "shipped");
private string Plugins => Path.Combine(root, "plugins");
private string Installed => Path.Combine(Plugins, PluginInstaller.FolderName);
public InstallerTests() => Write(Shipped, "1.0.0.0", "new");
[Fact]
public async Task ReleasePluginReportsOldDevelopmentCopyAsOutdated()
{
var shipped = Environment.GetEnvironmentVariable("FINALTOKEN_TEST_SHIPPED_PLUGIN");
if (shipped is null)
{
Write(Shipped, typeof(PluginInstaller).Assembly.GetName().Version!.ToString(), "new");
shipped = Shipped;
}
Write(Installed, "1.0.0.1", "old development copy");
var process = new FakeProcesses();
var installer = new PluginInstaller(shipped, new(Plugins, "fake", "fake"), process);
var status = await installer.GetStatusAsync(TestContext.Current.CancellationToken);
Assert.Equal(PluginInstallState.Outdated, status.State);
Assert.Equal(typeof(PluginInstaller).Assembly.GetName().Version!.ToString(), status.ShippedVersion);
Assert.Equal("1.0.0.1", status.InstalledVersion);
Assert.Equal(0, process.Stops); Assert.Equal(0, process.Starts);
}
[Fact]
public async Task FreshInstallDoesNotStartStoppedHost()
{
var process = new FakeProcesses();
var installer = Create(process);
Assert.Equal(PluginInstallState.NotInstalled, (await installer.GetStatusAsync(TestContext.Current.CancellationToken)).State);
await installer.InstallOrUpdateAsync(TestContext.Current.CancellationToken);
Assert.Equal("new", File.ReadAllText(Path.Combine(Installed, "binary")));
Assert.Equal(PluginInstallState.UpToDate, (await installer.GetStatusAsync(TestContext.Current.CancellationToken)).State);
Assert.Equal(0, process.Stops); Assert.Equal(0, process.Starts);
}
[Fact]
public async Task UpdateStopsAndRestartsRunningHost()
{
Write(Installed, "0.9.0.0", "old");
var process = new FakeProcesses { Running = true };
var installer = Create(process);
var outcomes = new List<InstallOutcome>(); installer.Outcome += outcomes.Add;
Assert.Equal(PluginInstallState.Outdated, (await installer.GetStatusAsync(TestContext.Current.CancellationToken)).State);
await installer.InstallOrUpdateAsync(TestContext.Current.CancellationToken);
Assert.Equal("new", File.ReadAllText(Path.Combine(Installed, "binary")));
Assert.Equal(1, process.Stops); Assert.Equal(1, process.Starts);
Assert.Contains(InstallOutcome.Updated, outcomes);
Assert.Single(Directory.GetDirectories(Plugins));
}
[Fact]
public async Task CurrentAndNewerVersionsAreNotReplaced()
{
Write(Installed, "2.0.0.0", "newer");
var process = new FakeProcesses { Running = true };
await Create(process).InstallOrUpdateAsync(TestContext.Current.CancellationToken);
Assert.Equal("newer", File.ReadAllText(Path.Combine(Installed, "binary")));
Assert.Equal(0, process.Stops);
}
[Fact]
public async Task RestartFailureRollsBackAndRestartsOldPlugin()
{
Write(Installed, "0.9.0.0", "old");
var process = new FakeProcesses { Running = true, FailFirstStart = true };
var installer = Create(process);
var outcomes = new List<InstallOutcome>(); installer.Outcome += outcomes.Add;
await Assert.ThrowsAsync<IOException>(() => installer.InstallOrUpdateAsync(TestContext.Current.CancellationToken));
Assert.Equal("old", File.ReadAllText(Path.Combine(Installed, "binary")));
Assert.Equal(2, process.Starts);
Assert.Contains(InstallOutcome.RolledBack, outcomes);
Assert.Single(Directory.GetDirectories(Plugins));
}
[Fact]
public async Task NoHostAndMissingShippedPluginHaveExplicitStatus()
{
var installer = new PluginInstaller(Shipped, null);
Assert.Equal(PluginInstallState.NoHostFound, (await installer.GetStatusAsync(TestContext.Current.CancellationToken)).State);
await installer.InstallOrUpdateAsync(TestContext.Current.CancellationToken);
var missing = new PluginInstaller(Path.Combine(root, "missing"), new(Plugins, "fake", "fake"));
Assert.Equal(PluginInstallState.NoShippedPlugin, (await missing.GetStatusAsync(TestContext.Current.CancellationToken)).State);
await Assert.ThrowsAsync<InvalidOperationException>(() => missing.InstallOrUpdateAsync(TestContext.Current.CancellationToken));
}
[Fact]
public async Task StopFailureKeepsOldFolderAndAttemptsRestart()
{
Write(Installed, "0.9.0.0", "old");
var process = new FakeProcesses { Running = true, FailStop = true };
await Assert.ThrowsAsync<IOException>(() => Create(process).InstallOrUpdateAsync(TestContext.Current.CancellationToken));
Assert.Equal("old", File.ReadAllText(Path.Combine(Installed, "binary")));
Assert.Equal(1, process.Starts);
}
[Theory]
[InlineData(InstallMode.Off, InstallAction.Nothing)]
[InlineData(InstallMode.Ask, InstallAction.Ask)]
[InlineData(InstallMode.Automatic, InstallAction.Install)]
public void PolicyHonorsInstallMode(InstallMode mode, InstallAction expected) =>
Assert.Equal(expected, InstallPolicy.Decide(mode, new(PluginInstallState.NotInstalled, "1"), null));
[Fact]
public void PolicyHonorsDismissedVersionAndUnavailableHost()
{
Assert.Equal(InstallAction.Nothing, InstallPolicy.Decide(InstallMode.Ask, new(PluginInstallState.Outdated, "1"), "1"));
Assert.Equal(InstallAction.Nothing, InstallPolicy.Decide(InstallMode.Automatic, new(PluginInstallState.NoHostFound), null));
}
private PluginInstaller Create(FakeProcesses process) => new(Shipped, new(Plugins, "fake", "fake"), process);
private static void Write(string directory, string version, string binary)
{
Directory.CreateDirectory(directory);
File.WriteAllText(Path.Combine(directory, "manifest.json"), "{\"Version\":\"" + version + "\"}");
File.WriteAllText(Path.Combine(directory, "binary"), binary);
}
public void Dispose() { if (Directory.Exists(root)) Directory.Delete(root, true); }
private sealed class FakeProcesses : IHostProcesses
{
public bool Running { get; init; }
public bool FailFirstStart { get; init; }
public bool FailStop { get; init; }
public int Stops { get; private set; }
public int Starts { get; private set; }
public Task<bool> IsRunningAsync(PluginHost host, CancellationToken ct) => Task.FromResult(Running);
public Task StopAsync(PluginHost host, CancellationToken ct) { Stops++; if (FailStop) throw new IOException("stop failed"); return Task.CompletedTask; }
public Task StartAsync(PluginHost host, CancellationToken ct) { Starts++; if (FailFirstStart && Starts == 1) throw new IOException("start failed"); return Task.CompletedTask; }
}
}