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 string Scene { get; set; } = "mixed"; public DashboardSnapshot GetSnapshot() { string scene = Scene; var at = new DateTimeOffset(2026, 11, 13, 20, 0, 0, TimeSpan.Zero); bool mixed = scene == "mixed"; return new( GeneratedAt: at, GatewayStartedAt: at.AddHours(-3), GatewayUptime: TimeSpan.FromHours(3), GatewayStatus: mixed ? "Degraded" : "Healthy", GatewayVersion: "test", Sessions: scene == "zero" ? [] : [new("fixture-session", "synthetic", mixed ? ZB.MOM.WW.MxGateway.Contracts.Proto.SessionState.Faulted : ZB.MOM.WW.MxGateway.Contracts.Proto.SessionState.Ready, null, null, null, at, at, null, 123, null, null, 0, mixed ? "Synthetic fixture fault" : null)], Workers: [], Metrics: [new("mxgateway.sessions.open", scene == "zero" ? 0 : 1), new("mxgateway.workers.running", scene == "zero" ? 0 : 1), new("mxgateway.events.worker_queue.depth", mixed ? 3 : 0), new("mxgateway.commands.failed", 104), new("mxgateway.events.received", 105), new("mxgateway.faults", 106), new("mxgateway.queues.overflows", 107)], Faults: mixed ? [new("Worker", "fixture-session", 123, "Faulted", "Synthetic worker stopped responding. " + new string('x', 300) + " ", at)] : [], 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: DashboardGalaxySummary.Unknown with { Status = DashboardGalaxyStatus.Healthy, LastDeployTime = at, LastSuccessAt = at, LastQueriedAt = at, ObjectCount = 201, AreaCount = 202, AttributeCount = 203, HistorizedAttributeCount = 204, AlarmAttributeCount = 205 }); } /// 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)); }