Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests/Deployment/DeployCompileValidatorTests.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

108 lines
4.0 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"));
}
}