using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.AdminUI.DataConnections; using ZB.MOM.WW.OtOpcUa.Configuration.Validation; using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.DataConnections; /// H2 editor models: the HistoryRead checkbox, the history limits' defaults and the mixed-version banner. [Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "H2")] public sealed class GrpcDataHistoryEditorTests { [Fact] public void HistoryRead_is_authorable_and_new_drafts_do_not_grant_it() { DataOperationAuthoring.AuthorableOperations.ShouldContain(DataOperations.HistoryRead); // A2a/A2b made the alarm bits authorable (GrpcDataConditionEditorTests); AlarmShelve stays unauthorable. DataOperationAuthoring.AuthorableOperations.ShouldNotContain(DataOperations.AlarmShelve, "the shelve bit stays reserved"); // Every authorable bit is one the validator accepts — the editor never gets ahead of authoring. var all = DataOperationAuthoring.AuthorableOperations.Aggregate(DataOperations.None, (a, b) => a | b); (all & ~DataOperationSets.Understood).ShouldBe(DataOperations.None); new DataConnectionEditorModel().Value.AllowedOperations.HasFlag(DataOperations.HistoryRead).ShouldBeFalse(); new RemoteMappingEditorModel().Value.AllowedOperations.HasFlag(DataOperations.HistoryRead).ShouldBeFalse(); DataOperationAuthoring.Label(DataOperations.HistoryRead).ShouldBe("HistoryRead"); } [Fact] public void New_drafts_carry_the_plans_history_limits() { new DataConnectionEditorModel().Value.HistoryTimeoutMilliseconds.ShouldBe(30000); new RemoteMappingEditorModel().Value.MaxHistoryAssemblyBytesPerRequest.ShouldBe(16 * 1024 * 1024); } [Fact] public void The_banner_shows_only_while_a_post_v1_bit_is_ticked() { DataOperationAuthoring.FeatureBitBanner(DataOperationSets.V1).ShouldBeNull(); var banner = DataOperationAuthoring.FeatureBitBanner(DataOperations.Read | DataOperations.HistoryRead).ShouldNotBeNull(); banner.ShouldContain("every node of both clusters must run X0+ before authoring this", Case.Insensitive); } [Fact] public void A_HistoryRead_draft_validates_and_a_zero_limit_does_not() { var draft = GrpcDataEditorValidationTestsAccess.ValidSnapshot(DataOperations.Read | DataOperations.HistoryRead); DataConnectionValidator.Validate(draft).ShouldNotContain(x => x.Code.EndsWith("OperationsInvalid", StringComparison.Ordinal)); draft.DataConnections[0].HistoryTimeoutMilliseconds = 0; DataConnectionValidator.Validate(draft).ShouldContain(x => x.Code == "DataConnectionLimitInvalid"); } } /// A minimal valid draft for the editor tests. internal static class GrpcDataEditorValidationTestsAccess { public static DraftSnapshot ValidSnapshot(DataOperations operations) => new() { GenerationId = 1, ClusterId = "MAIN", DataConnections = [new() { DataConnectionId = "peer", ClusterId = "MAIN", Revision = 1, Enabled = true, AllowedOperations = operations, ConsumerNodeIds = [], EligibleTargetNodeIds = [], Endpoints = ["https://peer.example"] }], DataExports = [new() { DataExportId = "export", ClusterId = "MAIN", Enabled = true, AllowedOperations = operations, PrincipalId = "p", Revision = 1 }], RemoteNamespaceMappings = [new() { RemoteNamespaceMappingId = "mount", ClusterId = "MAIN", DataConnectionId = "peer", Enabled = true, AllowedOperations = operations, ConsumerNodeIds = [], Revision = 1 }], }; }