using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.Abstractions; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Core.Abstractions; using ZB.MOM.WW.OtOpcUa.Runtime.Historian; namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Tests; /// /// AVEVA's native TimeAverage / Total are served by default since the gateway's interval fix (historiangw#27) was /// live-verified (lmxopcua#694); ServerHistorian:Gateway:EnableNativeTimeWeighted=false is the kill switch that /// refuses them BadAggregateNotSupported (for a gateway older than the fix). /// public sealed class GatewayNativeTimeWeightedGateTests { private static readonly DateTime T0 = new(2026, 10, 22, 0, 0, 0, DateTimeKind.Utc); private static CancellationToken Ct => TestContext.Current.CancellationToken; [Theory] [InlineData(HistoryAggregateType.TimeAverage)] [InlineData(HistoryAggregateType.Total)] public async Task The_kill_switch_refuses_the_native_time_weighted_pair_unsent_and_unadvertised(HistoryAggregateType a) { var fake = new FakeHistorianGatewayClient(); var ds = new GatewayHistorianDataSource(fake, NullLogger.Instance, enableNativeTimeWeighted: false); ds.SupportedAggregates.ShouldNotContain(a); await Should.ThrowAsync( () => ds.ReadProcessedAsync("T", T0, T0.AddHours(1), TimeSpan.FromMinutes(1), a, Ct)); fake.ReadAggregateCallCount.ShouldBe(0); fake.TagExistsCallCount.ShouldBe(0); ds.GetHealthSnapshot().TotalQueries.ShouldBe(0, "a refused aggregate is a client request, not a backend read"); // Positive control: a raw-evaluated per-sample aggregate on the same source is still served. ds.SupportedAggregates.ShouldContain(HistoryAggregateType.Average); var avg = await ds.ReadProcessedAsync("T", T0, T0.AddHours(1), TimeSpan.FromMinutes(30), HistoryAggregateType.Average, Ct); avg.Samples.Count.ShouldBe(2); fake.ReadRawCallCount.ShouldBeGreaterThan(0); } [Theory] [InlineData(HistoryAggregateType.TimeAverage)] [InlineData(HistoryAggregateType.Total)] public async Task By_default_the_native_pair_is_served_and_advertised(HistoryAggregateType a) { var fake = new FakeHistorianGatewayClient(); var ds = new GatewayHistorianDataSource(fake, NullLogger.Instance); ds.SupportedAggregates.ShouldBe(GatewayHistorianDataSource.ServedAggregates); await ds.ReadProcessedAsync("T", T0, T0.AddHours(1), TimeSpan.FromMinutes(1), a, Ct); fake.ReadAggregateCallCount.ShouldBeGreaterThan(0); } /// /// lmxopcua#897: AVEVA refuses its native TimeAverage / Total retrieval modes on a discrete tag (live: SysPulse, /// StartQuery UnknownException 85 ⇒ gateway Internal), so the source declares both unsupported on a Boolean node and /// the server refuses them up front. Every other type keeps them, and the raw-evaluated aggregates keep Boolean. /// [Fact] public void The_native_pair_is_declared_unsupported_on_a_Boolean_node_only() { var ds = new GatewayHistorianDataSource(new FakeHistorianGatewayClient(), NullLogger.Instance); foreach (var a in GatewayHistorianDataSource.ServedAggregates) { var native = a is HistoryAggregateType.TimeAverage or HistoryAggregateType.Total; ds.SupportsAggregateFor(a, TypeCode.Boolean).ShouldBe(!native, $"{a} on a Boolean node"); foreach (var t in new TypeCode?[] { TypeCode.Double, TypeCode.Single, TypeCode.Int32, TypeCode.UInt16, TypeCode.String, null }) ds.SupportsAggregateFor(a, t).ShouldBeTrue($"{a} on {t?.ToString() ?? "an unknown type"}"); } } [Fact] public void The_default_set_is_all_twelve_and_the_kill_switch_leaves_the_ten_raw_evaluated() { var on = new GatewayHistorianDataSource(new FakeHistorianGatewayClient(), NullLogger.Instance); on.SupportedAggregates.Count.ShouldBe(12); on.SupportedAggregates.ShouldBe(GatewayHistorianDataSource.ServedAggregates); var off = new GatewayHistorianDataSource(new FakeHistorianGatewayClient(), NullLogger.Instance, enableNativeTimeWeighted: false); off.SupportedAggregates.Count.ShouldBe(10); off.SupportedAggregates.ShouldBe(GatewayHistorianDataSource.RawEvaluatedAggregates); GatewayHistorianDataSource.ServedAggregates.Except(off.SupportedAggregates).OrderBy(x => x) .ShouldBe([HistoryAggregateType.Total, HistoryAggregateType.TimeAverage]); } [Theory] [InlineData(null, true)] [InlineData("false", false)] [InlineData("true", true)] public void The_flag_binds_from_configuration_and_reaches_the_data_source(string? configured, bool expected) { var values = new Dictionary { ["ServerHistorian:Enabled"] = "true", ["ServerHistorian:Endpoint"] = "https://localhost:5222", ["ServerHistorian:ApiKey"] = "histgw_x_y", }; if (configured is not null) values["ServerHistorian:Gateway:EnableNativeTimeWeighted"] = configured; var opts = new ConfigurationBuilder().AddInMemoryCollection(values).Build() .GetSection(ServerHistorianOptions.SectionName).Get()!; using var services = new ServiceCollection().BuildServiceProvider(); var ds = Assert.IsType(GatewayHistorian.CreateDataSource(opts, services)); ds.SupportedAggregates.Contains(HistoryAggregateType.TimeAverage).ShouldBe(expected); } }