using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.Sql.Contracts;

namespace ZB.MOM.WW.OtOpcUa.Driver.Sql.Tests;

public class SqlQueryPlanImmutabilityTests
{
    // A plan is documented as cacheable across polls. If it stayed aliased to the planner's working
    // List<T>, a consumer downcast could corrupt every later poll that reused it.
    [Fact]
    public void Plan_doesNotAliasTheCallersLists()
    {
        var names = new List<string> { "@k0" };
        var values = new List<object?> { "v" };
        var members = new List<SqlTagDefinition>
        {
            new("raw", SqlTagModel.KeyValue, "t") { KeyColumn = "k", KeyValue = "v", ValueColumn = "c" },
        };
        var selected = new List<string> { "k", "c" };

        var plan = new SqlQueryPlan(SqlTagModel.KeyValue, "gk", "SELECT 1", names, values, members, selected);

        names.Add("@k1");
        values.Add("other");
        members.Add(members[0]);
        selected.Add("extra");

        plan.ParameterNames.Count.ShouldBe(1);
        plan.Parameters.Count.ShouldBe(1);
        plan.Members.Count.ShouldBe(1);
        plan.SelectedColumns.Count.ShouldBe(2);
    }
}
