Files
FinalToken/tests/FinalToken.Core.Tests/TestSupport.cs
T
Florian Schmidt 15c7e684bb build: one version for the app and the plugin
D11 replaces an installed plugin only when the shipped manifest is
newer, but the manifest version was kept by hand and still said
1.0.0.1, the version of a test build installed before the snapshot
sharing and appPath fixes, so that stale plugin was never replaced.
Directory.Build.props now holds the only version, 1.0.1; publish.py
stamps it into the shipped manifest and the app shows it in its title.

Tests that waited on real time (file retry, popup expiry) use
controlled timers, and real process waits have wider margins.

Refs #1
2026-10-14 21:08:22 +02:00

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";
}