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.AbLegacy; using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.PlcFamilies; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests; public sealed class AbLegacyDriverPageFormSerializationTests { 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 AbLegacyDriverOptions { Timeout = TimeSpan.FromSeconds(4), Probe = new AbLegacyProbeOptions { Enabled = true, Interval = TimeSpan.FromSeconds(8), Timeout = TimeSpan.FromSeconds(3), ProbeAddress = "N7:0", }, ProbeTimeoutSeconds = 10, Devices = [ new AbLegacyDeviceOptions("10.0.0.10", AbLegacyPlcFamily.Slc500, "PLC-A"), new AbLegacyDeviceOptions("10.0.0.11", AbLegacyPlcFamily.MicroLogix), ], }; var json = JsonSerializer.Serialize(original, _opts); var back = JsonSerializer.Deserialize(json, _opts); back.ShouldNotBeNull(); back.Timeout.ShouldBe(TimeSpan.FromSeconds(4)); back.Probe.Enabled.ShouldBeTrue(); back.Probe.Interval.ShouldBe(TimeSpan.FromSeconds(8)); back.Probe.Timeout.ShouldBe(TimeSpan.FromSeconds(3)); back.Probe.ProbeAddress.ShouldBe("N7:0"); back.ProbeTimeoutSeconds.ShouldBe(10); back.Devices.Count.ShouldBe(2); back.Devices[0].HostAddress.ShouldBe("10.0.0.10"); back.Devices[0].PlcFamily.ShouldBe(AbLegacyPlcFamily.Slc500); back.Devices[0].DeviceName.ShouldBe("PLC-A"); back.Devices[1].PlcFamily.ShouldBe(AbLegacyPlcFamily.MicroLogix); } [Fact] public void Deserialize_DropsUnknownFields() { var jsonWithExtra = """{"unknownField":"old-value","probeTimeoutSeconds":10}"""; var optsWithSkip = new JsonSerializerOptions(_opts) { UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip, }; var back = JsonSerializer.Deserialize(jsonWithExtra, optsWithSkip); back.ShouldNotBeNull(); back.ProbeTimeoutSeconds.ShouldBe(10); } [Fact] public void DeviceRow_round_trips_through_definition() { var row = new AbLegacyDriverPage.AbLegacyDeviceRow { HostAddress = "10.0.0.10", PlcFamily = AbLegacyPlcFamily.MicroLogix, DeviceName = "PLC-A", }; var def = row.ToDefinition(); var back = AbLegacyDriverPage.AbLegacyDeviceRow.FromDefinition(def); back.HostAddress.ShouldBe("10.0.0.10"); back.PlcFamily.ShouldBe(AbLegacyPlcFamily.MicroLogix); back.DeviceName.ShouldBe("PLC-A"); } [Fact] public void DeviceRow_preserves_unedited_fields() { var original = new AbLegacyDeviceOptions("10.0.0.10", AbLegacyPlcFamily.Plc5, "PLC-A"); var row = AbLegacyDriverPage.AbLegacyDeviceRow.FromDefinition(original); row.HostAddress = "10.0.0.20"; var back = row.ToDefinition(); back.HostAddress.ShouldBe("10.0.0.20"); back.PlcFamily.ShouldBe(AbLegacyPlcFamily.Plc5); back.DeviceName.ShouldBe("PLC-A"); } // v3 Batch-1 migration: the pre-declared Tags editor (AbLegacyDriverPage.AbLegacyTagRow + // AbLegacyDriverOptions.Tags) was retired — tags moved to the Raw tree (/raw, Batch 2). The former // TagRow_* / ValidateTagRow_* tests and the tag leg of the list-serialization test were removed. // The multi-device Devices editor is RETAINED, so its coverage (DeviceRow_* + ValidateDeviceRow_* // + the device-list serialization below, all carrying the AbLegacyPlcFamily enum) stays. [Fact] public void ValidateDeviceRow_rejects_duplicate_host() { var rows = new List { new() { HostAddress = "10.0.0.10" } }; AbLegacyDriverPage.AbLegacyDeviceRow.ValidateRow(new() { HostAddress = "10.0.0.10" }, rows, null) .ShouldNotBeNull(); } [Fact] public void Device_list_survives_options_serialize_round_trip() { var devices = new List { new("10.0.0.10", AbLegacyPlcFamily.Slc500, "PLC-1"), new("10.0.0.11", AbLegacyPlcFamily.MicroLogix, "PLC-2"), }; var opts = new AbLegacyDriverPage.FormModel().ToOptions(devices); var json = JsonSerializer.Serialize(opts, TestJsonOpts); var back = JsonSerializer.Deserialize(json, TestJsonOpts)!; back.Devices.Count.ShouldBe(2); back.Devices[0].HostAddress.ShouldBe("10.0.0.10"); back.Devices[0].PlcFamily.ShouldBe(AbLegacyPlcFamily.Slc500); back.Devices[1].PlcFamily.ShouldBe(AbLegacyPlcFamily.MicroLogix); } }