Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.DeploymentManager.Tests/DeploymentComparisonTests.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

140 lines
6.3 KiB
C#

using NSubstitute;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening;
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Validation;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Templates;
namespace ZB.MOM.WW.ScadaBridge.DeploymentManager.Tests;
/// <summary>
/// Tests for deployed vs template-derived state comparison.
///
/// Adds behavioral coverage proving the read-only
/// staleness/comparison path can flatten (and thus hash) an instance whose
/// script does NOT compile — because <c>validateScripts: false</c> skips the
/// expensive Roslyn <see cref="ValidationService.ValidateScriptCompilation"/>
/// step while keeping structural/semantic validation. The default (deploy) path
/// still compiles.
/// </summary>
public class DeploymentComparisonTests
{
/// <summary>Verifies that a comparison with matching hashes is not stale.</summary>
[Fact]
public void DeploymentComparisonResult_MatchingHashes_NotStale()
{
var result = new DeploymentComparisonResult(
1, "sha256:abc", "sha256:abc", false, DateTimeOffset.UtcNow);
Assert.False(result.IsStale);
Assert.Equal("sha256:abc", result.DeployedRevisionHash);
Assert.Equal("sha256:abc", result.CurrentRevisionHash);
}
/// <summary>Verifies that a comparison with different hashes is stale.</summary>
[Fact]
public void DeploymentComparisonResult_DifferentHashes_IsStale()
{
var result = new DeploymentComparisonResult(
1, "sha256:old", "sha256:new", true, DateTimeOffset.UtcNow);
Assert.True(result.IsStale);
Assert.NotEqual(result.DeployedRevisionHash, result.CurrentRevisionHash);
}
/// <summary>Verifies that a comparison result carries the deployed timestamp.</summary>
[Fact]
public void DeploymentComparisonResult_ContainsDeployedTimestamp()
{
var deployedAt = new DateTimeOffset(2026, 3, 16, 12, 0, 0, TimeSpan.Zero);
var result = new DeploymentComparisonResult(1, "h1", "h2", true, deployedAt);
Assert.Equal(deployedAt, result.DeployedAt);
}
// ── read-only paths skip Roslyn script compilation ──
private const int InstanceId = 1;
private const int TemplateId = 10;
private const int SiteId = 100;
// A deliberately non-compiling script body. The deploy gate
// (validateScripts: true) flags this as a ScriptCompilation error; the
// read-only staleness/comparison path (validateScripts: false) must skip
// the compile entirely and still produce a flattened config + hash.
private const string NonCompilingScript = "int x = ;";
private static FlatteningPipeline BuildPipeline(
ITemplateEngineRepository templateRepo,
ISiteRepository siteRepo,
ISharedSchemaRepository sharedSchemaRepo)
{
sharedSchemaRepo.ListAsync(Arg.Any<CancellationToken>()).Returns([]);
return new FlatteningPipeline(
templateRepo,
siteRepo,
new FlatteningService(),
new ValidationService(),
new RevisionHashService(),
sharedSchemaRepo,
new TemplateGraphWatermark());
}
private static (ITemplateEngineRepository, ISiteRepository, ISharedSchemaRepository) ArrangeNonCompilingScript()
{
var templateRepo = Substitute.For<ITemplateEngineRepository>();
var siteRepo = Substitute.For<ISiteRepository>();
var sharedSchemaRepo = Substitute.For<ISharedSchemaRepository>();
var template = new Template("Tank") { Id = TemplateId };
template.Scripts.Add(new TemplateScript("BadScript", NonCompilingScript));
var instance = new Instance("Tank-01") { Id = InstanceId, TemplateId = TemplateId, SiteId = SiteId };
templateRepo.GetInstanceByIdAsync(InstanceId, Arg.Any<CancellationToken>()).Returns(instance);
templateRepo.GetTemplateWithChildrenAsync(TemplateId, Arg.Any<CancellationToken>()).Returns(template);
templateRepo.GetCompositionsByTemplateIdAsync(TemplateId, Arg.Any<CancellationToken>()).Returns([]);
templateRepo.GetAllSharedScriptsAsync(Arg.Any<CancellationToken>()).Returns([]);
siteRepo.GetDataConnectionsBySiteIdAsync(SiteId, Arg.Any<CancellationToken>()).Returns([]);
return (templateRepo, siteRepo, sharedSchemaRepo);
}
/// <summary>Verifies that flattening with script validation off succeeds with a hash and no compilation error for a non-compiling script.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task FlattenAndValidate_ValidateScriptsFalse_NonCompilingScript_SucceedsWithHashAndNoCompilationError()
{
var (templateRepo, siteRepo, sharedSchemaRepo) = ArrangeNonCompilingScript();
var sut = BuildPipeline(templateRepo, siteRepo, sharedSchemaRepo);
var result = await sut.FlattenAndValidateAsync(InstanceId, validateScripts: false);
Assert.True(result.IsSuccess);
Assert.False(string.IsNullOrEmpty(result.Value.RevisionHash));
// The Roslyn compile step was skipped, so no ScriptCompilation error is raised
// even though the script body does not compile.
Assert.DoesNotContain(result.Value.Validation.Errors,
e => e.Category == ValidationCategory.ScriptCompilation);
}
/// <summary>Verifies that flattening with the default script validation reports a compilation error for a non-compiling script.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task FlattenAndValidate_DefaultValidateScripts_NonCompilingScript_ReportsCompilationError()
{
var (templateRepo, siteRepo, sharedSchemaRepo) = ArrangeNonCompilingScript();
var sut = BuildPipeline(templateRepo, siteRepo, sharedSchemaRepo);
// Default (deploy) path — compilation is the authoritative gate.
var result = await sut.FlattenAndValidateAsync(InstanceId);
Assert.True(result.IsSuccess);
Assert.Contains(result.Value.Validation.Errors,
e => e.Category == ValidationCategory.ScriptCompilation);
}
}