using System.Runtime.CompilerServices;
using System.Security.Claims;
using Microsoft.AspNetCore.Components.Authorization;
using ZB.MOM.WW.MxGateway.Server.Dashboard;
using ZB.MOM.WW.MxGateway.Server.Security.Authentication;
using ZB.MOM.WW.MxGateway.Server.Security.Authorization;
internal sealed class StubSnapshotService : IDashboardSnapshotService
{
///
public DashboardSnapshot GetSnapshot() => new(
GeneratedAt: DateTimeOffset.UnixEpoch,
GatewayStartedAt: DateTimeOffset.UnixEpoch,
GatewayUptime: TimeSpan.Zero,
GatewayStatus: "Healthy",
GatewayVersion: "test",
Sessions: [],
Workers: [],
Metrics: [],
Faults: [],
ApiKeys:
[
new DashboardApiKeySummary(
KeyId: "synthetic-tagged",
DisplayName: "Tags only",
Scopes: new HashSet(StringComparer.Ordinal),
Constraints: ApiKeyConstraints.Empty with { DashboardTags = ["team-a", "team-b"] },
CreatedUtc: DateTimeOffset.UnixEpoch,
LastUsedUtc: null,
RevokedUtc: null),
new DashboardApiKeySummary(
KeyId: "synthetic-plain",
DisplayName: "Unconstrained",
Scopes: new HashSet(StringComparer.Ordinal),
Constraints: ApiKeyConstraints.Empty,
CreatedUtc: DateTimeOffset.UnixEpoch,
LastUsedUtc: null,
RevokedUtc: null),
],
Configuration: null!,
Galaxy: null!);
///
public IAsyncEnumerable WatchSnapshotsAsync(CancellationToken cancellationToken) =>
new IdleSnapshotFeed().WatchAsync(cancellationToken);
}
// Parks until the page is disposed, so the base page's watch loop neither spins nor pushes a
// second snapshot mid-render.
internal sealed class IdleSnapshotFeed : IDashboardSnapshotFeed
{
///
public async IAsyncEnumerable WatchAsync(
[EnumeratorCancellation] CancellationToken cancellationToken)
{
await Task.Delay(Timeout.Infinite, cancellationToken).ConfigureAwait(false);
yield break;
}
}
// Grants management so the create dialog is reachable; no test here exercises a mutation.
internal sealed class ManagingApiKeyService : IDashboardApiKeyManagementService
{
///
public bool CanManage(ClaimsPrincipal user) => true;
///
public Task CreateAsync(
ClaimsPrincipal user,
DashboardApiKeyManagementRequest request,
CancellationToken cancellationToken) =>
Task.FromResult(DashboardApiKeyManagementResult.Fail("not exercised"));
///
public Task RevokeAsync(
ClaimsPrincipal user,
string keyId,
CancellationToken cancellationToken) =>
Task.FromResult(DashboardApiKeyManagementResult.Fail("not exercised"));
///
public Task RotateAsync(
ClaimsPrincipal user,
string keyId,
CancellationToken cancellationToken) =>
Task.FromResult(DashboardApiKeyManagementResult.Fail("not exercised"));
///
public Task DeleteAsync(
ClaimsPrincipal user,
string keyId,
CancellationToken cancellationToken) =>
Task.FromResult(DashboardApiKeyManagementResult.Fail("not exercised"));
}
internal sealed class StubAuthenticationStateProvider : AuthenticationStateProvider
{
///
public override Task GetAuthenticationStateAsync() =>
Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(
[new Claim(ClaimTypes.Name, "admin-user"), new Claim(ClaimTypes.Role, DashboardRoles.Admin)],
authenticationType: "test",
nameType: ClaimTypes.Name,
roleType: ClaimTypes.Role))));
}
// In-memory Galaxy projection and read-only fake values for the actual BrowsePage.
internal sealed class FixtureGalaxyCache : ZB.MOM.WW.GalaxyRepository.IGalaxyHierarchyCache
{
public ZB.MOM.WW.GalaxyRepository.GalaxyHierarchyCacheEntry Current { get; }
public FixtureGalaxyCache()
{
var plant = new ZB.MOM.WW.GalaxyRepository.Grpc.GalaxyObject { GobjectId = 1, TagName = "SyntheticPlant", BrowseName = "SyntheticPlant", IsArea = true };
var pump = new ZB.MOM.WW.GalaxyRepository.Grpc.GalaxyObject { GobjectId = 2, ParentGobjectId = 1, TagName = "Pump01", BrowseName = "Pump01" };
pump.Attributes.Add(new ZB.MOM.WW.GalaxyRepository.Grpc.GalaxyAttribute { AttributeName = "PV", FullTagReference = "Pump01.PV", DataTypeName = "Float" });
pump.Attributes.Add(new ZB.MOM.WW.GalaxyRepository.Grpc.GalaxyAttribute { AttributeName = new string('x', 200), FullTagReference = "Pump01." + new string('x', 200), DataTypeName = "Float" });
var sibling = new ZB.MOM.WW.GalaxyRepository.Grpc.GalaxyObject { GobjectId = 3, ParentGobjectId = 1, TagName = "Sibling01", BrowseName = "Sibling01" };
sibling.Attributes.Add(new ZB.MOM.WW.GalaxyRepository.Grpc.GalaxyAttribute { AttributeName = "PV", FullTagReference = "Sibling01.PV", DataTypeName = "Float" });
ZB.MOM.WW.GalaxyRepository.Grpc.GalaxyObject[] rows = [plant, pump, sibling];
Current = ZB.MOM.WW.GalaxyRepository.GalaxyHierarchyCacheEntry.Empty with {
Status = ZB.MOM.WW.GalaxyRepository.GalaxyCacheStatus.Healthy, Sequence = 1,
Objects = rows, Index = ZB.MOM.WW.GalaxyRepository.GalaxyHierarchyIndex.Build(rows), ObjectCount = rows.Length, AttributeCount = 3,
LastSuccessAt = DateTimeOffset.UtcNow,
};
}
public Task RefreshAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public Task WaitForFirstLoadAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
internal sealed class FixtureLiveData : IDashboardLiveDataService
{
public Task ReadAsync(IReadOnlyCollection tagAddresses, CancellationToken cancellationToken) =>
Task.FromResult(new DashboardLiveReadResult(tagAddresses.Select(tag => new DashboardTagValue(tag, true, "[redacted]", "Float", 192, true, DateTimeOffset.UnixEpoch, null)).ToArray(), null, "synthetic-no-worker", null));
public Task QueryAlarmsAsync(CancellationToken cancellationToken) =>
Task.FromResult(new DashboardAlarmQueryResult([], null, null));
}