Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests/Deployment/DeployCompileValidatorTests.cs
T
Joseph Doherty 099b2221d6 fix(dcl): ASCII-only connection actor names, guarded ActorOf, and gate rejection of colliding connection names (review 2026-10-21 lane A M2)
ConnectionActorNames.Sanitize (shared by the DCL manager and the site deploy
gate) keeps ASCII letters/digits and -_.*+:@&=,!~';() only. The manager fails a
connection whose sanitised name is invalid or already used (Error + site
event) instead of restarting; DeployCompileValidator rejects a config whose
connection names collide after sanitising.
2026-10-22 09:10:26 -04:00

168 lines
6.7 KiB
C#

using System.Text.Json;
using Microsoft.Extensions.Logging.Abstractions;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Deployment;
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Scripts;
namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests.Deployment;
/// <summary>
/// Site Runtime S3: the pure deploy-time compile validator. It mirrors the
/// compile work <see cref="ZB.MOM.WW.ScadaBridge.SiteRuntime.Actors.InstanceActor"/>
/// does at runtime (scripts, on-trigger scripts, Expression triggers) so the
/// Deployment Manager can reject a non-compiling deployment before any actor is
/// created — WITHOUT touching actor state (safe on a Task.Run thread).
/// </summary>
public class DeployCompileValidatorTests
{
private static DeployCompileValidator NewValidator() =>
new(new ScriptCompilationService(NullLogger<ScriptCompilationService>.Instance));
private static string ConfigWith(string scriptCode) => JsonSerializer.Serialize(new FlattenedConfiguration
{
Scripts = new List<ResolvedScript>
{
new() { CanonicalName = "S1", Code = scriptCode, TriggerType = "Call" }
},
Attributes = new List<ResolvedAttribute>(),
Alarms = new List<ResolvedAlarm>(),
NativeAlarmSources = new List<ResolvedNativeAlarmSource>()
});
/// <summary>Verifies that broken script, returns errors.</summary>
[Fact]
public void BrokenScript_ReturnsErrors()
{
var errors = NewValidator().Validate(ConfigWith("this is not C# ~~~"));
Assert.NotEmpty(errors);
Assert.Contains(errors, e => e.Contains("S1"));
}
/// <summary>Verifies that valid script, returns no errors.</summary>
[Fact]
public void ValidScript_ReturnsNoErrors()
{
Assert.Empty(NewValidator().Validate(ConfigWith("return 1 + 1;")));
}
/// <summary>Verifies that broken trigger expression, returns errors.</summary>
[Fact]
public void BrokenTriggerExpression_ReturnsErrors()
{
// One Expression-triggered script whose trigger expression "1 +" does not compile.
var cfg = JsonSerializer.Serialize(new FlattenedConfiguration
{
Scripts = new List<ResolvedScript>
{
new()
{
CanonicalName = "S1",
Code = "return 1;",
TriggerType = "Expression",
TriggerConfiguration = JsonSerializer.Serialize(new { expression = "1 +" })
}
},
Attributes = new List<ResolvedAttribute>(),
Alarms = new List<ResolvedAlarm>(),
NativeAlarmSources = new List<ResolvedNativeAlarmSource>()
});
var errors = NewValidator().Validate(cfg);
Assert.NotEmpty(errors);
Assert.Contains(errors, e => e.Contains("script-trigger-S1"));
}
/// <summary>Verifies that malformed json, returns single deserialize error.</summary>
[Fact]
public void MalformedJson_ReturnsSingleDeserializeError()
{
var errors = NewValidator().Validate("{ this is not json");
Assert.Single(errors);
Assert.Contains("deserialize", errors[0], StringComparison.OrdinalIgnoreCase);
}
/// <summary>Verifies that missing on trigger script, returns error.</summary>
[Fact]
public void MissingOnTriggerScript_ReturnsError()
{
var cfg = JsonSerializer.Serialize(new FlattenedConfiguration
{
Scripts = new List<ResolvedScript>(),
Attributes = new List<ResolvedAttribute>(),
Alarms = new List<ResolvedAlarm>
{
new()
{
CanonicalName = "A1",
TriggerType = "HiLo",
OnTriggerScriptCanonicalName = "DoesNotExist"
}
},
NativeAlarmSources = new List<ResolvedNativeAlarmSource>()
});
var errors = NewValidator().Validate(cfg);
Assert.Contains(errors, e => e.Contains("A1") && e.Contains("DoesNotExist"));
}
/// <summary>
/// Review 2026-10-21 01/SR-C1: names that become Akka actor names are checked by the gate,
/// with Akka's own rule (so <c>Pump(A)</c>, which Akka accepts, is not rejected).
/// </summary>
[Theory]
[InlineData("a b", true)]
[InlineData("Furnace #3", true)]
[InlineData("Ofen Süd", true)]
[InlineData("$Pump", true)]
[InlineData("Pump(A)", false)]
[InlineData("Line-1_A.B", false)]
public void InstanceName_IsCheckedAsAnActorName(string instanceName, bool rejected)
{
var errors = NewValidator().Validate(ConfigWith("return 1;"), instanceName);
Assert.Equal(rejected, errors.Any(DeployCompileValidator.IsActorNameError));
if (rejected)
Assert.Contains(errors, e => e.Contains(instanceName));
}
/// <summary>Script, alarm and native-source canonical names are checked too.</summary>
[Fact]
public void ChildNames_AreCheckedAsActorNames()
{
var cfg = JsonSerializer.Serialize(new FlattenedConfiguration
{
Scripts = [new ResolvedScript { CanonicalName = "My Script", Code = "return 1;", TriggerType = "Call" }],
Alarms = [new ResolvedAlarm { CanonicalName = "Hi/Temp", TriggerType = "ValueMatch" }],
NativeAlarmSources = [new ResolvedNativeAlarmSource { CanonicalName = "Src#1", ConnectionName = "c" }],
Attributes = []
});
var nameErrors = NewValidator().Validate(cfg).Where(DeployCompileValidator.IsActorNameError).ToList();
Assert.Equal(3, nameErrors.Count);
Assert.Contains(nameErrors, e => e.Contains("My Script"));
Assert.Contains(nameErrors, e => e.Contains("Hi/Temp"));
Assert.Contains(nameErrors, e => e.Contains("Src#1"));
}
/// <summary>
/// Lane A M2: connection names that sanitise to the same DataConnectionActor name are
/// rejected by the gate; distinct ones (including non-ASCII) pass.
/// </summary>
[Fact]
public void ConnectionNames_CollidingAfterSanitising_AreRejected()
{
static string Cfg(params string[] names) => JsonSerializer.Serialize(new FlattenedConfiguration
{
Scripts = [], Alarms = [], NativeAlarmSources = [], Attributes = [],
Connections = names.ToDictionary(n => n, _ => new ConnectionConfig { Protocol = "OpcUa" })
});
var collide = NewValidator().Validate(Cfg("PLC A", "PLC-A")).Where(DeployCompileValidator.IsActorNameError).ToList();
Assert.Single(collide);
Assert.Contains("PLC A", collide[0]);
Assert.Contains("PLC-A", collide[0]);
Assert.Empty(NewValidator().Validate(Cfg("PLC A", "Ofen Süd")));
}
}