core: add self-hosted weekly dashboard and machine workers
Reuse the existing weekly pace, reset history and daily budgets across multiple machines without multiplying shared account quota. Coordinate profile-bound official CLI logins through transient authorization responses while keeping provider credentials local to each CLI. Release as version 1.2.0. Refs #19
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
using FinalToken.Core;
|
||||
using FinalToken.Core.Processes;
|
||||
using FinalToken.Fleet;
|
||||
using Xunit;
|
||||
|
||||
namespace FinalToken.Fleet.Tests;
|
||||
|
||||
public sealed class LoginTests
|
||||
{
|
||||
private static readonly DateTimeOffset Now=new(2026,11,14,12,0,0,TimeSpan.Zero);
|
||||
private static FleetState State => new([FleetTests.Machine("a",FleetTests.Profile()),FleetTests.Machine("b",FleetTests.Profile())]);
|
||||
private static WorkerProfile Profile => new("main",Provider.Claude,"[email protected]",Path.GetTempPath(),Path.GetTempPath());
|
||||
[Fact] public void LoginBelongsToInitiatingBrowserSession()
|
||||
{
|
||||
var c=new LoginCoordinator(FleetTests.Config());var p=c.Start("browser-a","a","main",State,Now);
|
||||
Assert.Throws<KeyNotFoundException>(()=>c.Get(p.Id,"browser-b",Now));
|
||||
Assert.Throws<KeyNotFoundException>(()=>c.Respond(p.Id,"browser-b","code#state",Now));
|
||||
Assert.Throws<KeyNotFoundException>(()=>c.Cancel(p.Id,"browser-b",Now));
|
||||
}
|
||||
[Fact] public void ResponseTravelsOnlyToExactMachineProfileAndOnceAcknowledgedIsCleared()
|
||||
{
|
||||
var c=new LoginCoordinator(FleetTests.Config());var p=c.Start("browser","a","main",State,Now);
|
||||
c.Exchange("b","boot",[new(p.Id,"main","WaitingForCode")],Now);
|
||||
Assert.Equal("Queued",c.Get(p.Id,"browser",Now).State);
|
||||
c.Exchange("a","boot",[new(p.Id,"wrong","WaitingForCode")],Now);
|
||||
Assert.Equal("Queued",c.Get(p.Id,"browser",Now).State);
|
||||
c.Exchange("a","boot",[new(p.Id,"main","WaitingForCode","https://claude.ai/oauth/authorize?state=abc")],Now);
|
||||
c.Respond(p.Id,"browser","code#state",Now);
|
||||
Assert.Empty(c.Exchange("b","boot",[],Now).Commands);
|
||||
Assert.Equal("code#state",c.Exchange("a","boot",[],Now).Commands.Single().Response);
|
||||
Assert.Throws<InvalidDataException>(()=>c.Respond(p.Id,"browser","again",Now));
|
||||
c.Exchange("a","boot",[new(p.Id,"main","Verifying",InputAccepted:true)],Now);
|
||||
Assert.Null(c.Exchange("a","boot",[],Now).Commands.Single().Response);
|
||||
}
|
||||
[Fact] public void ConcurrentLoginOnOneProfileIsRejectedButOtherMachinesAreIndependent()
|
||||
{
|
||||
var c=new LoginCoordinator(FleetTests.Config());c.Start("browser","a","main",State,Now);
|
||||
Assert.Throws<InvalidDataException>(()=>c.Start("other","a","main",State,Now));
|
||||
Assert.NotNull(c.Start("browser","b","main",State,Now));
|
||||
}
|
||||
[Fact] public void TimeoutAndCancellationSendCancellationWithoutRetainingCode()
|
||||
{
|
||||
var c=new LoginCoordinator(FleetTests.Config());var p=c.Start("browser","a","main",State,Now);
|
||||
Assert.Equal("TimedOut",c.Get(p.Id,"browser",Now.AddMinutes(16)).State);
|
||||
Assert.True(c.Exchange("a","boot",[],Now.AddMinutes(16)).Commands.Single().Cancel);
|
||||
var second=c.Start("browser","a","main",State,Now);
|
||||
c.Cancel(second.Id,"browser",Now);Assert.Equal("Cancelling",c.Get(second.Id,"browser",Now).State);
|
||||
Assert.True(c.Exchange("a","boot",[],Now).Commands.Single(x=>x.Id==second.Id).Cancel);
|
||||
c.Exchange("a","boot",[new(second.Id,"main","Cancelled")],Now);
|
||||
Assert.Equal("Cancelled",c.Get(second.Id,"browser",Now).State);
|
||||
}
|
||||
[Fact] public void RestartDoesNotReplayOldLoginAgainstNewWorkerProcess()
|
||||
{
|
||||
var c=new LoginCoordinator(FleetTests.Config());var p=c.Start("browser","a","main",State,Now);
|
||||
Assert.Empty(c.Exchange("a","new-boot",[],Now).Commands);
|
||||
Assert.Equal("WorkerRestarted",c.Get(p.Id,"browser",Now).State);
|
||||
}
|
||||
[Fact] public void WrongAccountIsExplicitFailureEvenIfWorkerClaimsSuccess()
|
||||
{
|
||||
var c=new LoginCoordinator(FleetTests.Config());var p=c.Start("browser","a","main",State,Now);
|
||||
c.Exchange("a","boot",[new(p.Id,"main","Succeeded",Email:"[email protected]")],Now);
|
||||
Assert.Equal("AccountMismatch",c.Get(p.Id,"browser",Now).State);
|
||||
}
|
||||
[Fact] public void OnlyOfficialUrlsReachTheBrowser()
|
||||
{
|
||||
Assert.False(LoginOutput.OfficialUrl(Provider.Claude,"https://evil.test/oauth/authorize"));
|
||||
Assert.False(LoginOutput.OfficialUrl(Provider.Claude,"https://claude.ai.evil.test/oauth/authorize"));
|
||||
Assert.False(LoginOutput.OfficialUrl(Provider.Codex,"http://auth.openai.com/codex/device"));
|
||||
Assert.True(LoginOutput.OfficialUrl(Provider.Codex,"https://auth.openai.com/codex/device"));
|
||||
}
|
||||
[Fact] public void ClaudePromptWithoutNewlineAndAnsiDeviceOutputAreParsed()
|
||||
{
|
||||
var claude=LoginOutput.Parse("id",Profile,"Open https://claude.ai/oauth/authorize?state=abc\nPaste code here if prompted: ",false);
|
||||
Assert.Equal("WaitingForCode",claude.State);Assert.NotNull(claude.Url);
|
||||
var codex=LoginOutput.Parse("id",Profile with {Provider=Provider.Codex},"\u001b[94mhttps://auth.openai.com/codex/device\u001b[0m\n\u001b[94mABCD-12345\u001b[0m",false);
|
||||
Assert.Equal("ABCD-12345",codex.DeviceCode);Assert.Equal("WaitingForBrowser",codex.State);
|
||||
}
|
||||
[Fact] public void UnknownExpiryAndWrongIdentityRemainExplicit()
|
||||
{
|
||||
var health=CliAuthentication.Parse(Profile,new ProcessResult(0,"{\"loggedIn\":true,\"email\":\"[email protected]\"}"),Now);
|
||||
Assert.Equal("AccountMismatch",health.State);
|
||||
Assert.Equal("Unknown",CliAuthentication.Parse(Profile,new(0,"{}"),Now).State);
|
||||
Assert.Equal("Unknown",CliAuthentication.Parse(Profile,new(0,"{\"loggedIn\":true}"),Now).State);
|
||||
}
|
||||
[Fact] public async Task WorkerRetriesCannotStartTwoProcessesOrRouteInputToWrongProfile()
|
||||
{
|
||||
var count=0;FakeLogin? fake=null;
|
||||
await using var worker=new WorkerLogins([Profile],(c,p)=>{count++;return fake=new(c.Id,p.Id);});
|
||||
var command=new LoginCommand("id","main",Now.AddMinutes(15));
|
||||
await worker.ApplyAsync(new([command,command]),CancellationToken.None);
|
||||
Assert.Equal(1,count);
|
||||
await worker.ApplyAsync(new([command with {ProfileId="wrong",Response="secret"}]),CancellationToken.None);
|
||||
Assert.Null(fake!.Response);
|
||||
await worker.ApplyAsync(new([command with {Response="code#state"},command with {Response="code#state"}]),CancellationToken.None);
|
||||
Assert.Equal(1,fake.Inputs);
|
||||
fake.State="Succeeded";
|
||||
Assert.True(worker.TakeIdentityChanges());Assert.False(worker.TakeIdentityChanges());
|
||||
await worker.ApplyAsync(new([command with {Cancel=true}]),CancellationToken.None);Assert.True(fake.Cancelled);
|
||||
}
|
||||
private sealed class FakeLogin(string id,string profile):ILoginProcess
|
||||
{
|
||||
public string? Response;public int Inputs;public bool Cancelled;public string State="WaitingForCode";
|
||||
public LoginUpdate Current=>new(id,profile,State);
|
||||
public Task SubmitAsync(string response,CancellationToken token){if(Response is null){Response=response;Inputs++;}return Task.CompletedTask;}
|
||||
public void Cancel()=>Cancelled=true;
|
||||
public ValueTask DisposeAsync()=>ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user