using System.Text.Json; using System.Text.Json.Serialization; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers; using ZB.MOM.WW.OtOpcUa.Driver.FOCAS; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests; public sealed class FocasDriverPageFormSerializationTests { private static readonly JsonSerializerOptions _opts = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = false, }; private static readonly JsonSerializerOptions TestJsonOpts = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip, }; [Fact] public void RoundTrip_PreservesKnownFields() { var original = new FocasDriverOptions { Timeout = TimeSpan.FromSeconds(3), Probe = new FocasProbeOptions { Enabled = false, Interval = TimeSpan.FromSeconds(10), Timeout = TimeSpan.FromSeconds(4), }, ProbeTimeoutSeconds = 30, AlarmProjection = new FocasAlarmProjectionOptions { Enabled = true, PollInterval = TimeSpan.FromSeconds(5), }, HandleRecycle = new FocasHandleRecycleOptions { Enabled = true, Interval = TimeSpan.FromMinutes(30), }, FixedTree = new FocasFixedTreeOptions { Enabled = true, PollInterval = TimeSpan.FromMilliseconds(500), ProgramPollInterval = TimeSpan.FromSeconds(2), TimerPollInterval = TimeSpan.FromSeconds(60), }, Devices = [], Tags = [], }; var json = JsonSerializer.Serialize(original, _opts); var back = JsonSerializer.Deserialize(json, _opts); back.ShouldNotBeNull(); back.Timeout.ShouldBe(TimeSpan.FromSeconds(3)); back.Probe.Enabled.ShouldBeFalse(); back.Probe.Interval.ShouldBe(TimeSpan.FromSeconds(10)); back.Probe.Timeout.ShouldBe(TimeSpan.FromSeconds(4)); back.ProbeTimeoutSeconds.ShouldBe(30); back.AlarmProjection.Enabled.ShouldBeTrue(); back.AlarmProjection.PollInterval.ShouldBe(TimeSpan.FromSeconds(5)); back.HandleRecycle.Enabled.ShouldBeTrue(); back.HandleRecycle.Interval.ShouldBe(TimeSpan.FromMinutes(30)); back.FixedTree.Enabled.ShouldBeTrue(); back.FixedTree.PollInterval.ShouldBe(TimeSpan.FromMilliseconds(500)); back.FixedTree.ProgramPollInterval.ShouldBe(TimeSpan.FromSeconds(2)); back.FixedTree.TimerPollInterval.ShouldBe(TimeSpan.FromSeconds(60)); back.Devices.ShouldBeEmpty(); back.Tags.ShouldBeEmpty(); } [Fact] public void Deserialize_DropsUnknownFields() { var jsonWithExtra = """{"unknownField":"old-value","probeTimeoutSeconds":18}"""; var optsSkip = new JsonSerializerOptions(_opts) { UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip, }; var back = JsonSerializer.Deserialize(jsonWithExtra, optsSkip); back.ShouldNotBeNull(); back.ProbeTimeoutSeconds.ShouldBe(18); } [Fact] public void FormModel_RoundTrip_PreservesEditableFields() { var opts = new FocasDriverOptions { Timeout = TimeSpan.FromSeconds(4), Probe = new FocasProbeOptions { Enabled = true, Interval = TimeSpan.FromSeconds(8), Timeout = TimeSpan.FromSeconds(3), }, ProbeTimeoutSeconds = 25, AlarmProjection = new FocasAlarmProjectionOptions { Enabled = true, PollInterval = TimeSpan.FromSeconds(3), }, HandleRecycle = new FocasHandleRecycleOptions { Enabled = false, Interval = TimeSpan.FromHours(2), }, FixedTree = new FocasFixedTreeOptions { Enabled = true, PollInterval = TimeSpan.FromMilliseconds(200), ProgramPollInterval = TimeSpan.FromSeconds(5), TimerPollInterval = TimeSpan.FromSeconds(45), }, }; var form = ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers .FocasDriverPage.FormModel.FromOptions(opts); var roundTripped = form.ToOptions([], []); roundTripped.Timeout.ShouldBe(TimeSpan.FromSeconds(4)); roundTripped.Probe.Enabled.ShouldBeTrue(); roundTripped.Probe.Interval.ShouldBe(TimeSpan.FromSeconds(8)); roundTripped.Probe.Timeout.ShouldBe(TimeSpan.FromSeconds(3)); roundTripped.ProbeTimeoutSeconds.ShouldBe(25); roundTripped.AlarmProjection.Enabled.ShouldBeTrue(); roundTripped.AlarmProjection.PollInterval.ShouldBe(TimeSpan.FromSeconds(3)); roundTripped.HandleRecycle.Enabled.ShouldBeFalse(); roundTripped.HandleRecycle.Interval.ShouldBe(TimeSpan.FromHours(2)); roundTripped.FixedTree.Enabled.ShouldBeTrue(); roundTripped.FixedTree.PollInterval.ShouldBe(TimeSpan.FromMilliseconds(200)); roundTripped.FixedTree.ProgramPollInterval.ShouldBe(TimeSpan.FromSeconds(5)); roundTripped.FixedTree.TimerPollInterval.ShouldBe(TimeSpan.FromSeconds(45)); } [Fact] public void DeviceRow_round_trips_through_definition() { var row = new FocasDriverPage.FocasDeviceRow { HostAddress = "192.168.0.10:8193", Series = FocasCncSeries.Thirty_i, DeviceName = "CNC1", }; var def = row.ToDefinition(); var back = FocasDriverPage.FocasDeviceRow.FromDefinition(def); back.HostAddress.ShouldBe("192.168.0.10:8193"); back.Series.ShouldBe(FocasCncSeries.Thirty_i); back.DeviceName.ShouldBe("CNC1"); } [Fact] public void DeviceRow_preserves_unedited_fields() { var original = new FocasDeviceOptions("192.168.0.10:8193", "CNC1", FocasCncSeries.Thirty_i); var row = FocasDriverPage.FocasDeviceRow.FromDefinition(original); row.HostAddress = "192.168.0.20:8193"; var back = row.ToDefinition(); back.HostAddress.ShouldBe("192.168.0.20:8193"); back.DeviceName.ShouldBe("CNC1"); back.Series.ShouldBe(FocasCncSeries.Thirty_i); } [Fact] public void TagRow_round_trips_through_definition() { var row = new FocasDriverPage.FocasTagRow { Name = "MacroVar", DeviceHostAddress = "192.168.0.10:8193", Address = "MACRO:500", DataType = FocasDataType.Float64, Writable = true, }; var def = row.ToDefinition(); var back = FocasDriverPage.FocasTagRow.FromDefinition(def); back.Name.ShouldBe("MacroVar"); back.DeviceHostAddress.ShouldBe("192.168.0.10:8193"); back.Address.ShouldBe("MACRO:500"); back.DataType.ShouldBe(FocasDataType.Float64); back.Writable.ShouldBeTrue(); } [Fact] public void TagRow_preserves_unedited_fields() { var original = new FocasTagDefinition( "MacroVar", "192.168.0.10:8193", "MACRO:500", FocasDataType.Float64, Writable: true, WriteIdempotent: true); var row = FocasDriverPage.FocasTagRow.FromDefinition(original); row.Name = "Renamed"; var back = row.ToDefinition(); back.Name.ShouldBe("Renamed"); back.WriteIdempotent.ShouldBeTrue(); } [Fact] public void ValidateDeviceRow_rejects_duplicate_host() { var rows = new List { new() { HostAddress = "192.168.0.10:8193" } }; FocasDriverPage.FocasDeviceRow.ValidateRow(new() { HostAddress = "192.168.0.10:8193" }, rows, null) .ShouldNotBeNull(); } [Fact] public void ValidateTagRow_rejects_duplicate_name() { var rows = new List { new() { Name = "MacroVar" } }; FocasDriverPage.FocasTagRow.ValidateRow(new() { Name = "MacroVar" }, rows, null) .ShouldNotBeNull(); } [Fact] public void Device_and_tag_lists_survive_options_serialize_round_trip() { var devices = new List { new("192.168.0.10:8193", "CNC1", FocasCncSeries.Thirty_i), new("192.168.0.20:8193", "CNC2", FocasCncSeries.Zero_i_F), }; var tags = new List { new("MacroVar", "192.168.0.10:8193", "MACRO:500", FocasDataType.Float64), new("Flag", "192.168.0.20:8193", "X0.0", FocasDataType.Bit), }; var opts = new FocasDriverPage.FormModel().ToOptions(devices, tags); var json = JsonSerializer.Serialize(opts, TestJsonOpts); var back = JsonSerializer.Deserialize(json, TestJsonOpts)!; back.Devices.Count.ShouldBe(2); back.Devices[0].HostAddress.ShouldBe("192.168.0.10:8193"); back.Devices[0].Series.ShouldBe(FocasCncSeries.Thirty_i); back.Tags.Count.ShouldBe(2); back.Tags[0].Name.ShouldBe("MacroVar"); back.Tags[0].Address.ShouldBe("MACRO:500"); } }