test(overrides): case-variant override names verified end to end through the flattener; UI single path gets the hint (ScadaBridge#105)

Re-verified #105 item 2 on the real write paths: ManagementActor bulk attribute
overrides (CLI set-overrides / management API) and InstanceService.SetAttributeOverrideAsync
(Central UI single editor) both match the template member ordinally and refuse a
case-variant name, so nothing is stored for the flattener to drop. Native-source
overrides already validate against the flattener's own output.

- New theory tests drive each path, feed whatever it stored into the real
  FlatteningService, and assert every stored override is applied (exact-case row is
  the positive control). Mutation: accepting case variants (OrdinalIgnoreCase) fails
  them with "stored ... but the flattener dropped it as unresolved".
- The did-you-mean hint moves to TemplateEngine.OverrideNameMatching and is now also
  on the UI single-attribute path; ambiguous matches get no hint.

Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
This commit is contained in:
Joseph Doherty
2026-11-13 02:13:10 -05:00
parent bba3c348bc
commit 3a59c3f638
5 changed files with 164 additions and 5 deletions
@@ -133,6 +133,74 @@ public class InstanceServiceTests
Assert.Equal("99", result.Value.OverrideValue);
}
/// <summary>
/// ScadaBridge#105, end to end for the single-attribute path the Central UI uses
/// (<see cref="InstanceService.SetAttributeOverrideAsync"/>): whatever it stores must be
/// applied by the real flattener. The exact-case row is the positive control; a name
/// differing from the member only by case is refused with a hint and nothing is stored.
/// </summary>
/// <param name="requestedName">The override name the caller sends.</param>
/// <param name="accepted">Whether the write is expected to succeed.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
[Theory]
[InlineData("Threshold", true)]
[InlineData("threshold", false)]
[InlineData("THRESHOLD", false)]
public async Task SetAttributeOverride_EveryStoredOverride_IsAppliedByTheFlattener(string requestedName, bool accepted)
{
var template = new Template("Base") { Id = 1 };
template.Attributes.Add(new TemplateAttribute("Threshold") { TemplateId = 1, DataType = DataType.Double, Value = "50.0" });
var instance = new Instance("Inst1") { Id = 1, TemplateId = 1, SiteId = 1 };
_repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>())).ReturnsAsync(instance);
_repoMock.Setup(r => r.GetAttributesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
.ReturnsAsync(template.Attributes.ToList());
_repoMock.Setup(r => r.GetOverridesByInstanceIdAsync(1, It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<InstanceAttributeOverride>());
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>())).ReturnsAsync(1);
var stored = new List<InstanceAttributeOverride>();
_repoMock.Setup(r => r.AddInstanceAttributeOverrideAsync(It.IsAny<InstanceAttributeOverride>(), It.IsAny<CancellationToken>()))
.Callback<InstanceAttributeOverride, CancellationToken>((o, _) => stored.Add(o))
.Returns(Task.CompletedTask);
var result = await _sut.SetAttributeOverrideAsync(1, requestedName, "75.0", "admin");
foreach (var o in stored) instance.AttributeOverrides.Add(o);
var flattened = new ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening.FlatteningService().Flatten(
instance, [template],
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
new Dictionary<int, IReadOnlyList<Template>>(),
new Dictionary<int, ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites.DataConnection>());
Assert.True(flattened.IsSuccess);
foreach (var o in stored)
Assert.True(
flattened.Value.Attributes.Any(a => a.CanonicalName == o.AttributeName && a.Source == "Override"),
$"Override '{o.AttributeName}' was stored but the flattener dropped it as unresolved.");
var threshold = Assert.Single(flattened.Value.Attributes, a => a.CanonicalName == "Threshold");
if (accepted)
{
Assert.True(result.IsSuccess);
Assert.Equal("75.0", threshold.Value);
Assert.Equal("Override", threshold.Source);
}
else
{
Assert.True(result.IsFailure);
Assert.Contains("did you mean 'Threshold'", result.Error);
Assert.Empty(stored);
Assert.Equal("50.0", threshold.Value);
}
}
/// <summary>ScadaBridge#105: an ambiguous case-insensitive match (two candidates differing only by case) gets no hint.</summary>
[Fact]
public void CaseMismatchHint_NamesOnlyAnUnambiguousMatch()
{
Assert.Contains("did you mean 'Setpoint'", OverrideNameMatching.CaseMismatchHint("setpoint", ["Setpoint", "Other"]));
Assert.Equal(string.Empty, OverrideNameMatching.CaseMismatchHint("setpoint", ["Setpoint", "SETPOINT"]));
Assert.Equal(string.Empty, OverrideNameMatching.CaseMismatchHint("missing", ["Setpoint"]));
}
// --- SetAlarmOverrideAsync validation regression ---
private static Template TemplateWithAlarms(int id, params TemplateAlarm[] alarms)