using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components.Server;
using Microsoft.AspNetCore.Components.Server.Circuits;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.MxGateway.Server;
using ZB.MOM.WW.MxGateway.Server.Dashboard;
using ZB.MOM.WW.MxGateway.Server.Dashboard.Hubs;
using ZB.MOM.WW.MxGateway.Tests.TestSupport;
namespace ZB.MOM.WW.MxGateway.Tests.Gateway.Dashboard;
public sealed class DashboardHubsRegistrationTests
{
/// Verifies that dashboard build maps all three hubs and token endpoint.
/// A task that represents the asynchronous operation.
[Fact]
public async Task Build_WhenDashboardEnabled_MapsAllThreeHubsAndTokenEndpoint()
{
await using WebApplication app = TestGatewayHost.Build([]);
IReadOnlyList endpoints = ((IEndpointRouteBuilder)app).DataSources
.SelectMany(source => source.Endpoints)
.OfType()
.ToList();
Assert.Contains(endpoints, endpoint => endpoint.RoutePattern.RawText == "/hubs/snapshot/negotiate");
Assert.Contains(endpoints, endpoint => endpoint.RoutePattern.RawText == "/hubs/alarms/negotiate");
Assert.Contains(endpoints, endpoint => endpoint.RoutePattern.RawText == "/hubs/events/negotiate");
Assert.Contains(endpoints, endpoint =>
endpoint.Metadata.GetMetadata()?.EndpointName == "DashboardHubToken");
}
///
/// Verifies that dashboard build registers the hub token service. It is a singleton
/// shared by the /hubs/token endpoint and HubTokenAuthenticationHandler;
/// there is deliberately no client-side hub-connection factory to resolve, because
/// server-rendered pages read the in-process feeds instead of dialling their own hubs.
///
/// A task that represents the asynchronous operation.
[Fact]
public async Task Build_WhenDashboardEnabled_RegistersHubTokenService()
{
await using WebApplication app = TestGatewayHost.Build([]);
HubTokenService tokens = app.Services.GetRequiredService();
Assert.NotNull(tokens);
}
///
/// The publish and in-process subscribe faces of the event mirror must resolve to
/// one instance: two would leave the session-details page reading a mirror the
/// session pipeline never publishes to, and the viewer gate would never open.
///
/// A task that represents the asynchronous operation.
[Fact]
public async Task Build_WhenDashboardEnabled_ResolvesBothEventMirrorInterfacesToOneInstance()
{
await using WebApplication app = TestGatewayHost.Build([]);
IDashboardEventBroadcaster broadcaster = app.Services.GetRequiredService();
IDashboardSessionEventSubscriber subscriber = app.Services
.GetRequiredService();
Assert.Same(broadcaster, subscriber);
Assert.Same(app.Services.GetRequiredService(), broadcaster);
}
///
/// A closed tab's circuit (and its Browse poll) is retained for 30 seconds, not the
/// framework's three minutes, and each circuit gets a connection tracker that is also
/// its , so the Browse poll can pause while disconnected.
///
/// A task that represents the asynchronous operation.
[Fact]
public async Task Build_WhenDashboardEnabled_ShortensCircuitRetentionAndTracksConnection()
{
await using WebApplication app = TestGatewayHost.Build([]);
CircuitOptions circuitOptions = app.Services.GetRequiredService>().Value;
Assert.Equal(TimeSpan.FromSeconds(30), circuitOptions.DisconnectedCircuitRetentionPeriod);
Assert.Equal(DashboardServiceCollectionExtensions.DisconnectedCircuitRetention, circuitOptions.DisconnectedCircuitRetentionPeriod);
using IServiceScope scope = app.Services.CreateScope();
DashboardCircuitConnectionTracker tracker = scope.ServiceProvider.GetRequiredService();
Assert.Contains(scope.ServiceProvider.GetServices(), handler => ReferenceEquals(handler, tracker));
Assert.True(tracker.IsConnected);
await tracker.OnConnectionDownAsync(null!, CancellationToken.None);
Assert.False(tracker.IsConnected);
await tracker.OnConnectionUpAsync(null!, CancellationToken.None);
Assert.True(tracker.IsConnected);
}
/// The Browse filter index is one shared instance, built once per Galaxy deploy.
/// A task that represents the asynchronous operation.
[Fact]
public async Task Build_WhenDashboardEnabled_SharesOneBrowseSearchIndex()
{
await using WebApplication app = TestGatewayHost.Build([]);
Assert.Same(
app.Services.GetRequiredService(),
app.Services.GetRequiredService());
}
}