Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Components/InstanceConfigureCsvImportTests.cs
T
Joseph Doherty 513fa1aa16 docs(tests): fix CommentChecker findings across tests/ — 13,392 → 0 (repo-wide scan clean)
Comment/XML-doc changes only:
- add summaries to ~10k test methods, fixtures, fakes and helpers
  (+ <param> on theories, <returns> on async Task methods,
  <inheritdoc/> on interface implementations the checker can resolve)
- strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N,
  #NN, Component-0NN) from comments, keeping the explanatory text;
  reword false positives in comments only
- real docs on fakes implementing BCL/library interfaces
  (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime)
- reword the last 5 src/ false positives (CWE-1236, failures-1,
  year-0001, M365)

Only non-comment edits: four one-line nested classes split across lines
so members can carry docs. Build 0 errors / 0 warnings.
2026-10-07 15:05:49 -04:00

187 lines
8.3 KiB
C#

using ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.Deployment;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
using ZB.MOM.WW.ScadaBridge.Commons.Types;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Components;
/// <summary>
/// The Instance Configure page accepts a CSV of attribute overrides via
/// an <c>&lt;InputFile&gt;</c>, parses it with the shared
/// <see cref="OverrideCsvParser"/>, validates each row against the instance's
/// overridable attributes (name must exist + not be template-locked, value must be
/// type-compatible — reusing <see cref="AttributeValueCodec"/>), and — all-or-nothing
/// — either submits the parsed override dict through the SAME
/// <c>InstanceService.SetAttributeOverrideAsync</c> path the manual editor already
/// uses, or shows the per-line error list (parser + validation) without applying
/// anything.
///
/// <para>
/// <c>InstanceConfigure</c> is a heavyweight page (≈7 injected services incl.
/// <c>InstanceService</c> + the flattening pipeline), so — consistent with the
/// list-override and native-alarm coverage — the parse→validate→build-dict
/// core is extracted to an <c>internal static</c> helper exercised directly here,
/// plus structural assertions over the component source that pin the InputFile +
/// reuse-the-existing-submit-path wiring.
/// </para>
/// </summary>
public class InstanceConfigureCsvImportTests
{
private static string InstanceConfigureMarkup
{
get
{
var dir = AppContext.BaseDirectory;
for (var i = 0; i < 6 && dir is not null; i++)
dir = Directory.GetParent(dir)?.FullName;
return File.ReadAllText(Path.Combine(dir!, "src", "ZB.MOM.WW.ScadaBridge.CentralUI",
"Components", "Pages", "Deployment", "InstanceConfigure.razor"));
}
}
private static List<TemplateAttribute> Attributes() => new()
{
new TemplateAttribute("Setpoint") { DataType = DataType.Float, IsLocked = false },
new TemplateAttribute("Name") { DataType = DataType.String, IsLocked = false },
new TemplateAttribute("Counts") { DataType = DataType.List, ElementDataType = DataType.Int32, IsLocked = false },
new TemplateAttribute("SerialNumber") { DataType = DataType.String, IsLocked = true },
};
// ── Core: valid CSV → override dict, no errors ──────────────────────────
/// <summary>Verifies that a valid CSV builds the override dictionary with no errors.</summary>
[Fact]
public void ValidCsv_BuildsOverrideDict_WithNoErrors()
{
var csv = "AttributeName,Value\nSetpoint,12.5\nName,Pump-A\n";
var parsed = OverrideCsvParser.Parse(csv);
var outcome = InstanceConfigure.BuildCsvOverrideImport(parsed, Attributes());
Assert.False(outcome.HasErrors);
Assert.Empty(outcome.Errors);
Assert.Equal(2, outcome.Overrides.Count);
Assert.Equal("12.5", outcome.Overrides["Setpoint"]);
Assert.Equal("Pump-A", outcome.Overrides["Name"]);
}
/// <summary>Verifies that a valid list-valued CSV row builds canonical JSON that round-trips through the value codec.</summary>
[Fact]
public void ValidListCsv_BuildsCanonicalJson_PerCodecRoundTrip()
{
// A List attribute value is supplied as canonical JSON in the Value column
// and round-trips through AttributeValueCodec just like the manual editor.
var csv = "AttributeName,Value\nCounts,\"[1,2,3]\"\n";
var parsed = OverrideCsvParser.Parse(csv);
var outcome = InstanceConfigure.BuildCsvOverrideImport(parsed, Attributes());
Assert.False(outcome.HasErrors);
var decoded = AttributeValueCodec.Decode(outcome.Overrides["Counts"], DataType.List, DataType.Int32);
Assert.Equal(new[] { 1, 2, 3 }, Assert.IsType<List<int>>(decoded));
}
// ── Core: bad rows → errors, NO dict (all-or-nothing) ───────────────────
/// <summary>Verifies that a row naming an unknown attribute produces an error and applies nothing.</summary>
[Fact]
public void UnknownAttribute_ProducesError_AndAppliesNothing()
{
var csv = "AttributeName,Value\nDoesNotExist,42\nSetpoint,1.0\n";
var parsed = OverrideCsvParser.Parse(csv);
var outcome = InstanceConfigure.BuildCsvOverrideImport(parsed, Attributes());
Assert.True(outcome.HasErrors);
Assert.Empty(outcome.Overrides); // all-or-nothing: nothing applied
Assert.Contains(outcome.Errors, e => e.Contains("DoesNotExist") && e.Contains("Line 2"));
}
/// <summary>Verifies that a template-locked attribute is rejected, matching the manual editor.</summary>
[Fact]
public void TemplateLockedAttribute_IsRejected_LikeTheEditor()
{
// SerialNumber is IsLocked → excluded from the overridable set, so a CSV
// targeting it is rejected exactly as the manual editor would never list it.
var csv = "AttributeName,Value\nSerialNumber,ABC123\n";
var parsed = OverrideCsvParser.Parse(csv);
var outcome = InstanceConfigure.BuildCsvOverrideImport(parsed, Attributes());
Assert.True(outcome.HasErrors);
Assert.Empty(outcome.Overrides);
Assert.Contains(outcome.Errors, e => e.Contains("SerialNumber"));
}
/// <summary>Verifies that a type-incompatible value produces an error and applies nothing.</summary>
[Fact]
public void TypeIncompatibleValue_ProducesError_AndAppliesNothing()
{
// "not-a-number" is not a valid Float.
var csv = "AttributeName,Value\nSetpoint,not-a-number\nName,ok\n";
var parsed = OverrideCsvParser.Parse(csv);
var outcome = InstanceConfigure.BuildCsvOverrideImport(parsed, Attributes());
Assert.True(outcome.HasErrors);
Assert.Empty(outcome.Overrides);
Assert.Contains(outcome.Errors, e => e.Contains("Setpoint") && e.Contains("Line 2"));
}
/// <summary>Verifies that malformed list JSON produces an error and applies nothing.</summary>
[Fact]
public void MalformedListJson_ProducesError_AndAppliesNothing()
{
var csv = "AttributeName,Value\nCounts,\"[1,oops]\"\n";
var parsed = OverrideCsvParser.Parse(csv);
var outcome = InstanceConfigure.BuildCsvOverrideImport(parsed, Attributes());
Assert.True(outcome.HasErrors);
Assert.Empty(outcome.Overrides);
Assert.Contains(outcome.Errors, e => e.Contains("Counts"));
}
/// <summary>Verifies that parser errors propagate through the helper and nothing is applied.</summary>
[Fact]
public void ParserErrors_PropagateThrough_AndApplyNothing()
{
// A bad header makes the parser emit an error and zero rows; the import
// must surface that error and apply nothing.
var csv = "Wrong,Header\nSetpoint,1.0\n";
var parsed = OverrideCsvParser.Parse(csv);
var outcome = InstanceConfigure.BuildCsvOverrideImport(parsed, Attributes());
Assert.True(outcome.HasErrors);
Assert.Empty(outcome.Overrides);
Assert.NotEmpty(outcome.Errors);
}
// ── Structural: InputFile + reuse-the-existing-submit-path wiring ───────
/// <summary>Verifies that the page source wires up the CSV input file control with its test hooks.</summary>
[Fact]
public void Page_WiresCsvInputFile_WithTestHooks()
{
var markup = InstanceConfigureMarkup;
Assert.Contains("<InputFile", markup);
Assert.Contains("data-test=\"csv-import-input\"", markup);
Assert.Contains("data-test=\"csv-import-result\"", markup);
Assert.Contains("accept=\".csv\"", markup);
// File-selected handler reads + parses the upload.
Assert.Contains("OverrideCsvParser.Parse", markup);
}
/// <summary>Verifies that the page applies the imported overrides through the existing set-attribute-override path.</summary>
[Fact]
public void Page_AppliesViaExistingSetAttributeOverridePath()
{
var markup = InstanceConfigureMarkup;
// Reuse the EXACT submit path the manual editor uses — no new server method.
Assert.Contains("InstanceService.SetAttributeOverrideAsync", markup);
// The import calls the shared core.
Assert.Contains("BuildCsvOverrideImport", markup);
}
}