using System.Text.Json.Nodes; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers; using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.DeviceForms; using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.Forms; using ZB.MOM.WW.OtOpcUa.AdminUI.Uns; using ZB.MOM.WW.OtOpcUa.AdminUI.Uns.TagEditors; using ZB.MOM.WW.OtOpcUa.Core.Abstractions; using ZB.MOM.WW.OtOpcUa.Driver.Heidenhain; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns; /// /// lmxopcua#803: the Heidenhain authoring surfaces (driver form, device form, tag editor, CSV, browse commit) /// write exactly what the driver factory and the deploy gate accept — the "registered but unauthorable" and /// "numerically-serialized enum" defect classes. /// [Trait("Category", "Unit")] public sealed class HeidenhainAuthoringTests { [Fact] public void Every_authoring_seam_is_registered() { DriverConfigFormMap.Resolve(DriverTypeNames.Heidenhain).ShouldBe(typeof(HeidenhainDriverForm)); DeviceFormMap.Resolve(DriverTypeNames.Heidenhain).ShouldBe(typeof(HeidenhainDeviceForm)); TagConfigEditorMap.Resolve(DriverTypeNames.Heidenhain).ShouldBe(typeof(Components.Shared.Uns.TagEditors.HeidenhainTagConfigEditor)); TagConfigValidator.Validate(DriverTypeNames.Heidenhain, "{}").ShouldNotBeNull(); CsvColumnMap.Resolve(DriverTypeNames.Heidenhain).ShouldNotBeNull(); } [Fact] public void A_default_driver_form_save_is_accepted_by_the_factory_and_preserves_devices() { var authored = """{"Devices":[{"Host":"10.0.0.5","DeviceName":"m1"}],"Custom":42}"""; var m = HeidenhainDriverFormModel.FromJson(authored); m.Validate().ShouldBeNull(); m.InspectPassword = "secret:heidenhain/inspect"; m.TimersEnabled = true; var json = m.ToJson(); var o = HeidenhainOptionsParser.Parse(json); HeidenhainOptionsParser.Validate(o).ShouldBeEmpty(); // the factory's ParseOptions gate o.Devices.Single().Host.ShouldBe("10.0.0.5"); o.Access.InspectPassword.ShouldBe("secret:heidenhain/inspect"); o.Polling.TimersEnabled.ShouldBeTrue(); JsonNode.Parse(json)!["Custom"]!.GetValue().ShouldBe(42); // no field is ever written as a number where the parser expects a name — there are no enums at all HeidenhainDriverFormModel.FromJson(json).ToJson().ShouldBe(json); } [Fact] public void Driver_form_refuses_literal_passwords_and_out_of_range_values() { var m = HeidenhainDriverFormModel.FromJson("{}"); m.DncPassword = "hunter2"; // #830: the credential refusal is the hosting modal's (the save gate), reported once — not the form's too. var refusal = DriverConfigSaveGate.Validate(DriverTypeNames.Heidenhain, m.ToJson()); refusal.ShouldNotBeNull(); refusal.ShouldNotContain("hunter2"); m.DncPassword = null; m.StatusMs = 10; m.Validate().ShouldNotBeNull(); } [Fact] public void Device_form_round_trips_and_validates_with_the_driver_rule() { var d = HeidenhainDeviceModel.FromJson("""{"Host":"cnc-7","Note":"keep"}"""); d.Port.ShouldBe(19000); d.Validate().ShouldBeNull(); d.DataPassword = "plain"; DriverConfigSaveGate.Validate(DriverTypeNames.Heidenhain, d.ToJson()).ShouldNotBeNull(); // #830: the modal's gate d.DataPassword = "env:OTOPCUA_HEIDENHAIN_DATA"; var json = d.ToJson(); JsonNode.Parse(json)!["Note"]!.GetValue().ShouldBe("keep"); var opts = HeidenhainOptionsParser.Parse($$"""{"Devices":[{{json}}]}"""); opts.Devices.Single().DataPassword.ShouldBe("env:OTOPCUA_HEIDENHAIN_DATA"); HeidenhainDeviceModel.FromJson("{}").Validate().ShouldNotBeNull(); // host required } [Theory] [InlineData("Axes/Positions")] [InlineData("Alarms/Messages")] [InlineData("Status/ExecutionState")] [InlineData("Capabilities/DncLogin")] public void Catalog_addresses_derive_type_and_rank_so_the_driver_accepts_them(string address) { var json = new HeidenhainTagConfigModel { Address = address }.ToJson(); HeidenhainTagDefinitionFactory.TryFromTagConfig("p", "d", json, out var def, out var error).ShouldBeTrue(error); HeidenhainCatalog.TryGet(address, out var e).ShouldBeTrue(); def!.DataType.ShouldBe(e!.DataType); def.IsArray.ShouldBe(e.IsArray); TagConfigValidator.Validate(DriverTypeNames.Heidenhain, json).ShouldBeNull(); } [Fact] public void Editor_writes_the_data_type_as_a_name_and_drops_a_stray_writable_flag() { var m = HeidenhainTagConfigModel.FromJson("""{"address":"PLC:WORD:2","dataType":"Int64","writable":true,"isHistorized":true}"""); m.Validate().ShouldNotBeNull(); // as loaded: the driver refuses writable:true var json = m.ToJson(); var o = JsonNode.Parse(json)!.AsObject(); o["dataType"]!.GetValue().ShouldBe("Int64"); o.ContainsKey("writable").ShouldBeFalse(); o["isHistorized"]!.GetValue().ShouldBeTrue(); HeidenhainTagConfigModel.FromJson(json).Validate().ShouldBeNull(); } [Fact] public void Editor_refuses_a_narrowing_binding() { var m = new HeidenhainTagConfigModel { Address = "PLC:DWORD:0", DataType = DriverDataType.Int16 }; m.Validate().ShouldNotBeNull(); } [Theory] [InlineData("Status/ExecutionState")] [InlineData("Axes/Names")] public void Browse_committed_leaves_open_in_the_editor_and_pass_the_driver_gate(string address) { var json = RawBrowseCommitMapper.BuildTagConfig(DriverTypeNames.Heidenhain, address); TagConfigValidator.Validate(DriverTypeNames.Heidenhain, json).ShouldBeNull(); HeidenhainTagConfigModel.FromJson(json).Address.ShouldBe(address); } /// /// lmxopcua#839: every ALARM: selector the browse picker offers commits as a complete condition tag — the /// paired alarm object seeded, Boolean, accepted by the driver's own factory and the editor's validator — /// so a browse-commit never lands a tag the deploy gate would refuse. /// [Fact] public void Browse_committed_alarm_selectors_are_complete_conditions_the_driver_accepts() { HeidenhainAlarmClasses.BrowseSelectors.Count.ShouldBeGreaterThan(2); foreach (var (_, name, address) in HeidenhainAlarmClasses.BrowseSelectors) { var row = RawBrowseCommitMapper.MapLeaf( DriverTypeNames.Heidenhain, address, name, nameof(DriverDataType.Boolean), "String", groupPrefix: null, folderPath: null, createGroups: false); row.Tag.DataType.ShouldBe("Boolean"); var json = row.Tag.TagConfig!; JsonNode.Parse(json)!["alarm"].ShouldNotBeNull(address); HeidenhainTagDefinitionFactory.TryFromTagConfig("p", "d", json, out var def, out var error).ShouldBeTrue(error); def!.IsAlarmCondition.ShouldBeTrue(); TagConfigValidator.Validate(DriverTypeNames.Heidenhain, json).ShouldBeNull(); } } }