using System.Text.Json; using System.Text.Json.Nodes; using Febe.Core.Abstractions; namespace Febe.Tests.Browser.TestSupport; /// Virtual clock: advances time instantly, so timing can be asserted without waiting. 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 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; } } } /// A command recorded by . public sealed record SentCommand(string Method, JsonNode? Parameters, DateTimeOffset At) { public string? Type => Parameters?["type"]?.GetValue(); public double X => Parameters?["x"]?.GetValue() ?? double.NaN; public double Y => Parameters?["y"]?.GetValue() ?? double.NaN; public double DeltaY => Parameters?["deltaY"]?.GetValue() ?? 0; } /// Records commands with the virtual time at which they were sent, and answers with canned results. public sealed class FakeCdpSession(IClock clock) : ICdpSession { private readonly Lock _gate = new(); private readonly Dictionary>> _handlers = new(StringComparer.Ordinal); private readonly List _sent = []; /// Snapshot of the commands sent so far (background pumps may add more concurrently). public IReadOnlyList Sent { get { lock (_gate) { return [.. _sent]; } } } public void ClearSent() { lock (_gate) { _sent.Clear(); } } /// Returns the result for a command; null means an empty object. public Func Responder { get; set; } = (_, _) => null; public Task 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(result.ToJsonString())); } public IDisposable On(string eventName, Action 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[] handlers; lock (_gate) { handlers = _handlers.TryGetValue(eventName, out var list) ? [.. list] : []; } var element = JsonSerializer.Deserialize(payload.ToJsonString()); foreach (var handler in handlers) { handler(element); } } public List 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(); } } /// Circuit breaker that only records what it receives. public sealed class RecordingBreaker : ICircuitBreaker { private readonly Lock _gate = new(); private readonly List _signals = []; public BreakerState State => BreakerState.Normal; public IReadOnlyList Signals { get { lock (_gate) { return [.. _signals]; } } } public void Report(Signal signal) { lock (_gate) { _signals.Add(signal); } } }