using Shouldly;
using TwinCAT.Ads;
using TwinCAT.Ads.SumCommand;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.TwinCAT;
namespace ZB.MOM.WW.OtOpcUa.Driver.TwinCAT.Tests;
///
/// hw2 live gate (2026-10-08, TC3 Z35474): every ADS sum-read chunk failed with
/// thrown by Beckhoff's own constructor, because the
/// IList<IgIoSumReadEntity> overload casts its argument to IList<SumDataEntity> — which
/// works for an array (covariance) and not for a List<IgIoSumReadEntity>. The driver turned it into
/// BadCommunicationError for every batched tag on every poll. The fake client never builds a real
/// , so these tests construct one directly; the constructor does no I/O, so an unconnected
/// is enough.
///
[Trait("Category", "Unit")]
public sealed class TwinCATSumReadConstructionTests
{
[Fact]
public void CreateSumRead_builds_a_real_Beckhoff_sum_command_without_a_cast_failure()
{
using var client = new AdsClient();
var sum = AdsTwinCATClient.CreateSumRead(client, [(0x4040u, 0x10u, 2), (0x4040u, 0x20u, 4), (0x4020u, 0x0u, 81)]);
sum.ShouldNotBeNull();
}
[Fact]
public void The_Beckhoff_list_overload_really_throws_for_a_List_which_is_why_CreateSumRead_passes_an_array()
{
// Falsifiability control: if a future Beckhoff release fixes the cast, this test goes red and the remark
// on CreateSumRead can be retired — but the array form keeps working either way.
using var client = new AdsClient();
var list = new List { new(0x4040, 0x10, 2), new(0x4040, 0x20, 4) };
Should.Throw(() => new SumRead(client, list, SumCommandErrorStrategy.Relaxed));
}
}