Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
193 lines
5.0 KiB
C#
193 lines
5.0 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using Febe.Core.Abstractions;
|
|
|
|
namespace Febe.Tests.Browser.TestSupport;
|
|
|
|
/// <summary>Virtual clock: <see cref="Delay"/> advances time instantly, so timing can be asserted without waiting.</summary>
|
|
public sealed class VirtualClock(DateTimeOffset? start = null) : IClock
|
|
{
|
|
private readonly Lock _gate = new();
|
|
private DateTimeOffset _now = start ?? new DateTimeOffset(2026, 10, 13, 9, 0, 0, TimeSpan.Zero);
|
|
|
|
public DateTimeOffset Now
|
|
{
|
|
get
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _now;
|
|
}
|
|
}
|
|
}
|
|
|
|
public TimeZoneInfo LocalZone => TimeZoneInfo.Utc;
|
|
|
|
public List<TimeSpan> Delays { get; } = [];
|
|
|
|
public Task Delay(TimeSpan time, CancellationToken ct)
|
|
{
|
|
ct.ThrowIfCancellationRequested();
|
|
lock (_gate)
|
|
{
|
|
if (time > TimeSpan.Zero)
|
|
{
|
|
_now += time;
|
|
}
|
|
|
|
Delays.Add(time);
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public void Advance(TimeSpan time)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_now += time;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>A command recorded by <see cref="FakeCdpSession"/>.</summary>
|
|
public sealed record SentCommand(string Method, JsonNode? Parameters, DateTimeOffset At)
|
|
{
|
|
public string? Type => Parameters?["type"]?.GetValue<string>();
|
|
|
|
public double X => Parameters?["x"]?.GetValue<double>() ?? double.NaN;
|
|
|
|
public double Y => Parameters?["y"]?.GetValue<double>() ?? double.NaN;
|
|
|
|
public double DeltaY => Parameters?["deltaY"]?.GetValue<double>() ?? 0;
|
|
}
|
|
|
|
/// <summary>Records commands with the virtual time at which they were sent, and answers with canned results.</summary>
|
|
public sealed class FakeCdpSession(IClock clock) : ICdpSession
|
|
{
|
|
private readonly Lock _gate = new();
|
|
private readonly Dictionary<string, List<Action<JsonElement>>> _handlers = new(StringComparer.Ordinal);
|
|
|
|
private readonly List<SentCommand> _sent = [];
|
|
|
|
/// <summary>Snapshot of the commands sent so far (background pumps may add more concurrently).</summary>
|
|
public IReadOnlyList<SentCommand> Sent
|
|
{
|
|
get
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return [.. _sent];
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ClearSent()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_sent.Clear();
|
|
}
|
|
}
|
|
|
|
/// <summary>Returns the result for a command; null means an empty object.</summary>
|
|
public Func<string, JsonNode?, JsonNode?> Responder { get; set; } = (_, _) => null;
|
|
|
|
public Task<JsonElement> SendAsync(string method, object? parameters = null, CancellationToken ct = default)
|
|
{
|
|
ct.ThrowIfCancellationRequested();
|
|
var node = parameters switch
|
|
{
|
|
null => null,
|
|
JsonNode n => JsonNode.Parse(n.ToJsonString()),
|
|
_ => JsonSerializer.SerializeToNode(parameters, parameters.GetType()),
|
|
};
|
|
|
|
lock (_gate)
|
|
{
|
|
_sent.Add(new SentCommand(method, node, clock.Now));
|
|
}
|
|
|
|
var result = Responder(method, node) ?? new JsonObject();
|
|
return Task.FromResult(JsonSerializer.Deserialize<JsonElement>(result.ToJsonString()));
|
|
}
|
|
|
|
public IDisposable On(string eventName, Action<JsonElement> handler)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
if (!_handlers.TryGetValue(eventName, out var list))
|
|
{
|
|
list = [];
|
|
_handlers[eventName] = list;
|
|
}
|
|
|
|
list.Add(handler);
|
|
}
|
|
|
|
return new Unsubscribe(() =>
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_handlers[eventName].Remove(handler);
|
|
}
|
|
});
|
|
}
|
|
|
|
public void Raise(string eventName, JsonNode payload)
|
|
{
|
|
Action<JsonElement>[] handlers;
|
|
lock (_gate)
|
|
{
|
|
handlers = _handlers.TryGetValue(eventName, out var list) ? [.. list] : [];
|
|
}
|
|
|
|
var element = JsonSerializer.Deserialize<JsonElement>(payload.ToJsonString());
|
|
foreach (var handler in handlers)
|
|
{
|
|
handler(element);
|
|
}
|
|
}
|
|
|
|
public List<SentCommand> Mouse(string? type = null)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return [.. _sent.Where(c => c.Method == "Input.dispatchMouseEvent" && (type is null || c.Type == type))];
|
|
}
|
|
}
|
|
|
|
private sealed class Unsubscribe(Action action) : IDisposable
|
|
{
|
|
public void Dispose() => action();
|
|
}
|
|
}
|
|
|
|
/// <summary>Circuit breaker that only records what it receives.</summary>
|
|
public sealed class RecordingBreaker : ICircuitBreaker
|
|
{
|
|
private readonly Lock _gate = new();
|
|
private readonly List<Signal> _signals = [];
|
|
|
|
public BreakerState State => BreakerState.Normal;
|
|
|
|
public IReadOnlyList<Signal> Signals
|
|
{
|
|
get
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return [.. _signals];
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Report(Signal signal)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_signals.Add(signal);
|
|
}
|
|
}
|
|
}
|