using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.PlcFamilies;
namespace ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.IntegrationTests;
///
/// End-to-end smoke tests against the ab_server PCCC Docker container.
/// Promotes the AB Legacy driver from unit-only coverage (FakeAbLegacyTag )
/// to wire-level: real libplctag PCCC stack over real TCP against the ab_server
/// simulator. Parametrised over all three families (SLC 500 / MicroLogix / PLC-5)
/// via [AbLegacyTheory] + [MemberData] .
///
[Collection(AbLegacyServerCollection.Name)]
[Trait("Category", "Integration")]
[Trait("Simulator", "ab_server-PCCC")]
public sealed class AbLegacyReadSmokeTests(AbLegacyServerFixture sim)
{
public static IEnumerable Profiles =>
KnownProfiles.All.Select(p => new object[] { p });
[AbLegacyTheory]
[MemberData(nameof(Profiles))]
public async Task Driver_reads_seeded_N_file_from_ab_server_PCCC(AbLegacyServerProfile profile)
{
if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);
// PCCC PLCs use empty cip-path, but AbLegacyHostAddress still requires the
// /cip-path suffix to parse.
var deviceUri = $"ab://{sim.Host}:{sim.Port}/";
await using var drv = new AbLegacyDriver(new AbLegacyDriverOptions
{
Devices = [new AbLegacyDeviceOptions(deviceUri, profile.Family)],
Tags = [
new AbLegacyTagDefinition(
Name: "IntCounter",
DeviceHostAddress: deviceUri,
Address: "N7:0",
DataType: AbLegacyDataType.Int),
],
Timeout = TimeSpan.FromSeconds(5),
Probe = new AbLegacyProbeOptions { Enabled = false },
}, driverInstanceId: $"ablegacy-smoke-{profile.Family}");
await drv.InitializeAsync("{}", TestContext.Current.CancellationToken);
var snapshots = await drv.ReadAsync(
["IntCounter"], TestContext.Current.CancellationToken);
snapshots.Single().StatusCode.ShouldBe(AbLegacyStatusMapper.Good,
$"N7:0 read must succeed against the {profile.Family} compose profile");
drv.GetHealth().State.ShouldBe(DriverState.Healthy);
}
[AbLegacyFact]
public async Task Slc500_write_then_read_round_trip_on_N7_scratch_register()
{
if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);
// PCCC PLCs use empty cip-path, but AbLegacyHostAddress still requires the
// /cip-path suffix to parse.
var deviceUri = $"ab://{sim.Host}:{sim.Port}/";
await using var drv = new AbLegacyDriver(new AbLegacyDriverOptions
{
Devices = [new AbLegacyDeviceOptions(deviceUri, AbLegacyPlcFamily.Slc500)],
Tags = [
new AbLegacyTagDefinition(
Name: "Scratch",
DeviceHostAddress: deviceUri,
Address: "N7:5",
DataType: AbLegacyDataType.Int,
Writable: true),
],
Timeout = TimeSpan.FromSeconds(5),
Probe = new AbLegacyProbeOptions { Enabled = false },
}, driverInstanceId: "ablegacy-smoke-rw");
await drv.InitializeAsync("{}", TestContext.Current.CancellationToken);
const short probe = 0x1234;
var writeResults = await drv.WriteAsync(
[new WriteRequest("Scratch", probe)],
TestContext.Current.CancellationToken);
writeResults.Single().StatusCode.ShouldBe(AbLegacyStatusMapper.Good,
"PCCC N7:5 write must succeed end-to-end");
var readResults = await drv.ReadAsync(
["Scratch"], TestContext.Current.CancellationToken);
readResults.Single().StatusCode.ShouldBe(AbLegacyStatusMapper.Good);
Convert.ToInt32(readResults.Single().Value).ShouldBe(probe);
}
}