feat(adminui): editable AbLegacy device + tag lists via CollectionEditor

This commit is contained in:
Joseph Doherty
2026-05-29 09:26:25 -04:00
parent 534d670b21
commit 15f3797f1e
2 changed files with 263 additions and 34 deletions
@@ -2,6 +2,7 @@ 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;
@@ -15,6 +16,12 @@ public sealed class AbLegacyDriverPageFormSerializationTests
WriteIndented = false,
};
private static readonly JsonSerializerOptions TestJsonOpts = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip,
};
[Fact]
public void RoundTrip_PreservesKnownFields()
{
@@ -78,4 +85,102 @@ public sealed class AbLegacyDriverPageFormSerializationTests
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");
}
[Fact]
public void TagRow_round_trips_through_definition()
{
var row = new AbLegacyDriverPage.AbLegacyTagRow
{
Name = "Level", DeviceHostAddress = "10.0.0.10", Address = "N7:5",
DataType = AbLegacyDataType.Int, Writable = true,
};
var def = row.ToDefinition();
var back = AbLegacyDriverPage.AbLegacyTagRow.FromDefinition(def);
back.Name.ShouldBe("Level");
back.DeviceHostAddress.ShouldBe("10.0.0.10");
back.Address.ShouldBe("N7:5");
back.DataType.ShouldBe(AbLegacyDataType.Int);
back.Writable.ShouldBeTrue();
}
[Fact]
public void TagRow_preserves_unedited_fields()
{
var original = new AbLegacyTagDefinition(
"Level", "10.0.0.10", "N7:5", AbLegacyDataType.Int,
Writable: true, WriteIdempotent: true);
var row = AbLegacyDriverPage.AbLegacyTagRow.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<AbLegacyDriverPage.AbLegacyDeviceRow> { new() { HostAddress = "10.0.0.10" } };
AbLegacyDriverPage.AbLegacyDeviceRow.ValidateRow(new() { HostAddress = "10.0.0.10" }, rows, null)
.ShouldNotBeNull();
}
[Fact]
public void ValidateTagRow_rejects_duplicate_name()
{
var rows = new List<AbLegacyDriverPage.AbLegacyTagRow> { new() { Name = "Level" } };
AbLegacyDriverPage.AbLegacyTagRow.ValidateRow(new() { Name = "Level" }, rows, null)
.ShouldNotBeNull();
}
[Fact]
public void Device_and_tag_lists_survive_options_serialize_round_trip()
{
var devices = new List<AbLegacyDeviceOptions>
{
new("10.0.0.10", AbLegacyPlcFamily.Slc500, "PLC-1"),
new("10.0.0.11", AbLegacyPlcFamily.MicroLogix, "PLC-2"),
};
var tags = new List<AbLegacyTagDefinition>
{
new("Level", "10.0.0.10", "N7:5", AbLegacyDataType.Int),
new("Pump", "10.0.0.11", "B3:0/0", AbLegacyDataType.Bit),
};
var opts = new AbLegacyDriverPage.FormModel().ToOptions(devices, tags);
var json = JsonSerializer.Serialize(opts, TestJsonOpts);
var back = JsonSerializer.Deserialize<AbLegacyDriverOptions>(json, TestJsonOpts)!;
back.Devices.Count.ShouldBe(2);
back.Devices[0].HostAddress.ShouldBe("10.0.0.10");
back.Tags.Count.ShouldBe(2);
back.Tags[0].Name.ShouldBe("Level");
}
}