using ZB.MOM.WW.ScadaBridge.Commons.Types;
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests;
///
/// Verifies the CSV parsing behaviour that backs instance import-overrides:
/// parse errors block the apply step; valid CSV produces the expected
/// attributeName → value dictionary that
/// consumes.
///
public class ImportOverridesTests
{
// ── error path ───────────────────────────────────────────────────────────
/// Verifies that a CSV with a missing header produces errors.
[Fact]
public void CsvWithMissingHeader_ProducesErrors()
{
var result = OverrideCsvParser.Parse("Speed,100");
Assert.NotEmpty(result.Errors);
Assert.Empty(result.Rows);
}
/// Verifies that a CSV row with a blank attribute name produces an error.
[Fact]
public void CsvWithBlankAttributeName_ProducesError()
{
const string csv = "AttributeName,Value\n,100";
var result = OverrideCsvParser.Parse(csv);
Assert.NotEmpty(result.Errors);
// The bad row is excluded; no rows flow through.
Assert.Empty(result.Rows);
}
/// Verifies that a CSV row with the wrong column count produces an error.
[Fact]
public void CsvWithWrongColumnCount_ProducesError()
{
const string csv = "AttributeName,Value\nSpeed,100,extra";
var result = OverrideCsvParser.Parse(csv);
Assert.NotEmpty(result.Errors);
Assert.Empty(result.Rows);
}
// ── happy path ───────────────────────────────────────────────────────────
/// Verifies that a valid CSV produces the expected overrides dictionary.
[Fact]
public void ValidCsv_ProducesCorrectOverridesDictionary()
{
const string csv = """
AttributeName,Value
Speed,100
Mode,
Label,Hello
""";
var parseResult = OverrideCsvParser.Parse(csv);
Assert.Empty(parseResult.Errors);
Assert.Equal(3, parseResult.Rows.Count);
// Build the same dictionary that BuildImportOverrides produces.
var overrides = parseResult.Rows.ToDictionary(r => r.AttributeName, r => r.Value);
Assert.Equal("100", overrides["Speed"]);
Assert.Null(overrides["Mode"]); // empty Value → null → clear the override
Assert.Equal("Hello", overrides["Label"]);
}
/// Verifies that the element type column of a valid CSV is ignored when building the overrides dictionary.
[Fact]
public void ValidCsvWithElementTypeColumn_OverridesDictIgnoresElementType()
{
const string csv = """
AttributeName,Value,ElementType
Speed,100,Float
Mode,,
""";
var parseResult = OverrideCsvParser.Parse(csv);
Assert.Empty(parseResult.Errors);
Assert.Equal(2, parseResult.Rows.Count);
// ElementType is carried on the row but SetInstanceOverridesCommand only
// takes attributeName → value; confirm the dict shape matches.
var overrides = parseResult.Rows.ToDictionary(r => r.AttributeName, r => r.Value);
Assert.Equal("100", overrides["Speed"]);
Assert.Null(overrides["Mode"]);
}
}