Files
wwtools/mbproxy/tests/Mbproxy.Tests/Admin/SignalRFakes.cs
T
Joseph Doherty b222362ce0 mbproxy: remediate the 2026-05-16 code-review findings
Fixes every finding from the codereviews/2026-05-16 multi-agent review
(2 Critical, 20 Major, 38 Minor) and adds that review to the repo.

Highlights: dashboard XSS escape; response cache invalidated on the
write request (not just the response); ReloadValidator now runs at
startup so port collisions / duplicate names / malformed Resilience
profiles fail fast; AdminPort 0 genuinely disables the admin endpoint;
PlcListener accept-loop faults propagate to the supervisor's faulted
path; reconciler Restart builds before removing; Resilience pipelines
are restart-only from a frozen snapshot; multiplexer connect-race leak,
watchdog party-list snapshot, backend-response and FC16 framing
validation; frontend reconnect retry and util.js load guard; plus the
log-event/doc drift sweep and test-port hygiene.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-16 18:08:06 -04:00

80 lines
3.3 KiB
C#

using System.Collections.Concurrent;
using System.Security.Claims;
using Mbproxy.Admin;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.SignalR;
namespace Mbproxy.Tests.Admin;
/// <summary>
/// Minimal hand-written test doubles for the SignalR surface <see cref="StatusHub"/>
/// and <see cref="StatusBroadcaster"/> touch. The project carries no mocking framework,
/// so these record just enough to assert behaviour.
/// </summary>
internal sealed class FakeHubCallerContext : HubCallerContext
{
public FakeHubCallerContext(string connectionId) => ConnectionId = connectionId;
public override string ConnectionId { get; }
public override string? UserIdentifier => null;
public override ClaimsPrincipal? User => null;
public override IDictionary<object, object?> Items { get; } = new Dictionary<object, object?>();
public override IFeatureCollection Features { get; } = new FeatureCollection();
public override CancellationToken ConnectionAborted => CancellationToken.None;
public override void Abort() { }
}
/// <summary>Records every group join so tests can assert membership changes.</summary>
internal sealed class FakeGroupManager : IGroupManager
{
public List<(string ConnectionId, string Group)> Added { get; } = [];
public Task AddToGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default)
{
Added.Add((connectionId, groupName));
return Task.CompletedTask;
}
// StatusHub.OnDisconnectedAsync never calls RemoveFromGroupAsync — SignalR removes a
// disconnected connection from its groups implicitly. Nothing to record here.
public Task RemoveFromGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default)
=> Task.CompletedTask;
}
/// <summary>Records every push so <see cref="StatusBroadcaster"/> tests can assert routing.</summary>
internal sealed class FakeStatusPushSink : IStatusPushSink
{
private readonly ConcurrentBag<StatusResponse> _fleet = [];
private readonly ConcurrentBag<(string Plc, PlcDetailResponse Detail)> _plc = [];
public IReadOnlyCollection<StatusResponse> FleetPushes => _fleet;
public IReadOnlyCollection<(string Plc, PlcDetailResponse Detail)> PlcPushes => _plc;
public Task PushFleetAsync(StatusResponse snapshot, CancellationToken ct)
{
_fleet.Add(snapshot);
return Task.CompletedTask;
}
public Task PushPlcAsync(string plcName, PlcDetailResponse detail, CancellationToken ct)
{
_plc.Add((plcName, detail));
return Task.CompletedTask;
}
}
/// <summary>
/// An <see cref="IStatusPushSink"/> whose every push fails with an exception from the
/// supplied factory — used to prove <see cref="StatusBroadcaster.PushOnceAsync"/> swallows
/// a transport fault (and that its <c>when (ex is not OperationCanceledException)</c> filter
/// still lets a cancellation propagate).
/// </summary>
internal sealed class ThrowingStatusPushSink(Func<Exception> exceptionFactory) : IStatusPushSink
{
public Task PushFleetAsync(StatusResponse snapshot, CancellationToken ct)
=> throw exceptionFactory();
public Task PushPlcAsync(string plcName, PlcDetailResponse detail, CancellationToken ct)
=> throw exceptionFactory();
}