using System.IO; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy; using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.PlcFamilies; namespace ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Tests.Import; /// /// Coverage for — the /// extension method that opens a CSV file and concatenates the resulting tag definitions /// onto an existing . /// [Trait("Category", "Unit")] public sealed class AbLegacyDriverFactoryAddRsLogixImportTests { [Fact] public void AddRsLogixImport_appends_tags_and_preserves_existing_options() { // Existing options have one device + one hand-rolled tag. The importer should // append on top — never replace — so the device + the original tag survive. var path = Path.Combine(Path.GetTempPath(), $"rslogix-import-{Guid.NewGuid():N}.csv"); File.WriteAllText(path, """ Symbol,Address,Description,DataType,Scope New1,N7:0,desc,INT,Global New2,F8:0,desc,REAL,Global """); try { var existingTag = new AbLegacyTagDefinition( Name: "Manual", DeviceHostAddress: "ab://10.0.0.1/1,0", Address: "S:0", DataType: AbLegacyDataType.Int); var options = new AbLegacyDriverOptions { Devices = [new AbLegacyDeviceOptions("ab://10.0.0.1/1,0", AbLegacyPlcFamily.Slc500)], Tags = [existingTag], }; var updated = options.AddRsLogixImport(path, "ab://10.0.0.1/1,0", out var result); // Imported counts surface on the result. result.ParsedCount.ShouldBe(2); // Devices + the original Manual tag are preserved on the returned options. updated.Devices.Count.ShouldBe(1); updated.Tags.Count.ShouldBe(3); updated.Tags[0].Name.ShouldBe("Manual"); updated.Tags[1].Name.ShouldBe("New1"); updated.Tags[2].Name.ShouldBe("New2"); // Original options object is unchanged (immutability guarantee). options.Tags.Count.ShouldBe(1); } finally { try { File.Delete(path); } catch { /* best-effort */ } } } [Fact] public void AddRsLogixImportWithResult_returns_tuple() { var path = Path.Combine(Path.GetTempPath(), $"rslogix-import-{Guid.NewGuid():N}.csv"); File.WriteAllText(path, """ Symbol,Address,Description,DataType,Scope T,N7:0,desc,INT,Global """); try { var options = new AbLegacyDriverOptions(); var (updated, result) = options.AddRsLogixImportWithResult(path, "ab://10.0.0.1/1,0"); result.ParsedCount.ShouldBe(1); updated.Tags.Count.ShouldBe(1); } finally { try { File.Delete(path); } catch { /* best-effort */ } } } }