11950b0a8e
Final themed batch. 5 well-localised correctness fixes. Serialisation precision: - ESG-020: DatabaseGateway.JsonElementToParameterValue probes TryGetInt64 → TryGetDecimal → GetDouble, so a script's high-precision decimal SQL parameter survives the cached-write retry round-trip without silent precision loss. 3 new regression tests. Template engine correctness: - TE-018: DiffService gains ComputeConnectionsDiff over FlattenedConfiguration.Connections, mirroring the existing entity-diff shape and pairing with the Theme 1 TE-017 hash-coverage fix. A ConfigurationDiff record extension in Commons is flagged as a follow-up. - TE-019: TemplateResolver.BuildInheritanceChain now walks via the int? ParentTemplateId directly — only null means "no parent". A real Id of 0 (the prior special-cased sentinel) now walks the chain like any other node, matching the TemplateEngine-013 CycleDetector fix. Regression of TE-013 closed. - TE-020: All 5 Create* paths in TemplateService + SharedScriptService re-ordered to save-first → log-with-real-Id → save-audit (matching the InstanceService pattern). Create* audit rows no longer carry a literal "0" EntityId. Doc deferral: - Transport-012: Component-Transport.md §Audit Trail now spells out that the BundleImportId repository filter IS wired (in CentralUiRepository), but the Audit-Log-Viewer UI dropdown + summary-row hyperlink are a deferred CentralUI follow-up. CLI workaround documented (audit query --bundle-import-id). 11+ new regression tests (3 ESG, 4 DiffService, 3 TemplateResolver, 4 TemplateService, 1 SharedScriptService). Build clean; ESG 72/72, TemplateEngine 324/324. README regenerated: 1 pending of 481 total. Session-to-date: 135 of 136 originally-open Theme findings closed across 10 themes in 10 commits.
204 lines
8.1 KiB
C#
204 lines
8.1 KiB
C#
using Moq;
|
|
using ScadaLink.Commons.Entities.Scripts;
|
|
using ScadaLink.Commons.Interfaces.Repositories;
|
|
using ScadaLink.Commons.Interfaces.Services;
|
|
|
|
namespace ScadaLink.TemplateEngine.Tests;
|
|
|
|
public class SharedScriptServiceTests
|
|
{
|
|
private readonly Mock<ITemplateEngineRepository> _repoMock;
|
|
private readonly Mock<IAuditService> _auditMock;
|
|
private readonly SharedScriptService _service;
|
|
|
|
public SharedScriptServiceTests()
|
|
{
|
|
_repoMock = new Mock<ITemplateEngineRepository>();
|
|
_auditMock = new Mock<IAuditService>();
|
|
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>())).ReturnsAsync(1);
|
|
_service = new SharedScriptService(_repoMock.Object, _auditMock.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateSharedScript_Success()
|
|
{
|
|
_repoMock.Setup(r => r.GetSharedScriptByNameAsync("Helpers", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync((SharedScript?)null);
|
|
|
|
var result = await _service.CreateSharedScriptAsync(
|
|
"Helpers", "public static int Add(int a, int b) { return a + b; }", null, null, "admin");
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal("Helpers", result.Value.Name);
|
|
_repoMock.Verify(r => r.AddSharedScriptAsync(It.IsAny<SharedScript>(), It.IsAny<CancellationToken>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateSharedScript_EmptyName_Fails()
|
|
{
|
|
var result = await _service.CreateSharedScriptAsync("", "code", null, null, "admin");
|
|
Assert.True(result.IsFailure);
|
|
Assert.Contains("name is required", result.Error);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateSharedScript_EmptyCode_Fails()
|
|
{
|
|
var result = await _service.CreateSharedScriptAsync("Test", "", null, null, "admin");
|
|
Assert.True(result.IsFailure);
|
|
Assert.Contains("code is required", result.Error);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateSharedScript_DuplicateName_Fails()
|
|
{
|
|
_repoMock.Setup(r => r.GetSharedScriptByNameAsync("Helpers", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new SharedScript("Helpers", "existing code"));
|
|
|
|
var result = await _service.CreateSharedScriptAsync("Helpers", "new code", null, null, "admin");
|
|
|
|
Assert.True(result.IsFailure);
|
|
Assert.Contains("already exists", result.Error);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateSharedScript_UnbalancedBraces_Fails()
|
|
{
|
|
_repoMock.Setup(r => r.GetSharedScriptByNameAsync("Bad", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync((SharedScript?)null);
|
|
|
|
var result = await _service.CreateSharedScriptAsync("Bad", "public void Run() {", null, null, "admin");
|
|
|
|
Assert.True(result.IsFailure);
|
|
Assert.Contains("Syntax error", result.Error);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateSharedScript_Success()
|
|
{
|
|
var existing = new SharedScript("Helpers", "old code") { Id = 1 };
|
|
_repoMock.Setup(r => r.GetSharedScriptByIdAsync(1, It.IsAny<CancellationToken>())).ReturnsAsync(existing);
|
|
|
|
var result = await _service.UpdateSharedScriptAsync(1, "return 42;", null, null, "admin");
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal("return 42;", result.Value.Code);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateSharedScript_NotFound_Fails()
|
|
{
|
|
_repoMock.Setup(r => r.GetSharedScriptByIdAsync(999, It.IsAny<CancellationToken>())).ReturnsAsync((SharedScript?)null);
|
|
|
|
var result = await _service.UpdateSharedScriptAsync(999, "code", null, null, "admin");
|
|
|
|
Assert.True(result.IsFailure);
|
|
Assert.Contains("not found", result.Error);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteSharedScript_Success()
|
|
{
|
|
var existing = new SharedScript("Helpers", "code") { Id = 1 };
|
|
_repoMock.Setup(r => r.GetSharedScriptByIdAsync(1, It.IsAny<CancellationToken>())).ReturnsAsync(existing);
|
|
|
|
var result = await _service.DeleteSharedScriptAsync(1, "admin");
|
|
|
|
Assert.True(result.IsSuccess);
|
|
_repoMock.Verify(r => r.DeleteSharedScriptAsync(1, It.IsAny<CancellationToken>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteSharedScript_NotFound_Fails()
|
|
{
|
|
_repoMock.Setup(r => r.GetSharedScriptByIdAsync(999, It.IsAny<CancellationToken>())).ReturnsAsync((SharedScript?)null);
|
|
|
|
var result = await _service.DeleteSharedScriptAsync(999, "admin");
|
|
|
|
Assert.True(result.IsFailure);
|
|
Assert.Contains("not found", result.Error);
|
|
}
|
|
|
|
// Syntax validation unit tests
|
|
|
|
[Theory]
|
|
[InlineData("return 42;", null)]
|
|
[InlineData("public void Run() { }", null)]
|
|
[InlineData("var x = new int[] { 1, 2, 3 };", null)]
|
|
[InlineData("if (a > b) { return a; } else { return b; }", null)]
|
|
public void ValidateSyntax_ValidCode_ReturnsNull(string code, string? expected)
|
|
{
|
|
Assert.Equal(expected, SharedScriptService.ValidateSyntax(code));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("public void Run() {")]
|
|
[InlineData("return a + b);")]
|
|
[InlineData("var x = new int[] { 1, 2 ;")]
|
|
public void ValidateSyntax_InvalidCode_ReturnsError(string code)
|
|
{
|
|
var result = SharedScriptService.ValidateSyntax(code);
|
|
Assert.NotNull(result);
|
|
Assert.Contains("Syntax error", result);
|
|
}
|
|
|
|
// --- TemplateEngine-007 regression: string/comment-literal awareness ---
|
|
|
|
[Theory]
|
|
[InlineData("var s = \"a } brace\"; { }")] // brace inside a normal string
|
|
[InlineData("var s = \"a ) paren ] bracket\";")] // paren/bracket inside a string
|
|
[InlineData("var s = @\"verbatim } brace\"; { }")] // brace inside a verbatim string
|
|
[InlineData("var x = 1; var s = $\"hole {x} literal}}\"; { }")] // interpolated string with braces
|
|
[InlineData("var c = '}'; if (true) { }")] // char literal containing a brace
|
|
[InlineData("// a stray } here\nvar x = 1;")] // brace inside a line comment
|
|
[InlineData("/* a stray ) here */ var x = 1;")] // paren inside a block comment
|
|
public void ValidateSyntax_DelimiterInsideStringOrComment_ReturnsNull(string code)
|
|
{
|
|
Assert.Null(SharedScriptService.ValidateSyntax(code));
|
|
}
|
|
|
|
// --- TemplateEngine-020 regression: audit row carries the real script Id ---
|
|
|
|
[Fact]
|
|
public async Task CreateSharedScript_AuditRowCarriesRealScriptIdNotLiteralZero()
|
|
{
|
|
// Pre-020: AddSharedScriptAsync → LogAsync("0", ...) → SaveChangesAsync.
|
|
// The audit row was queued with EntityId = "0" because EF Core had
|
|
// not yet populated the auto-generated key. Post-020: save first,
|
|
// then log with the real Id, then save the audit row.
|
|
_repoMock.Setup(r => r.GetSharedScriptByNameAsync("Helpers", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync((SharedScript?)null);
|
|
|
|
SharedScript? added = null;
|
|
_repoMock.Setup(r => r.AddSharedScriptAsync(It.IsAny<SharedScript>(), It.IsAny<CancellationToken>()))
|
|
.Callback<SharedScript, CancellationToken>((s, _) => added = s)
|
|
.Returns(Task.CompletedTask);
|
|
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
|
.Callback<CancellationToken>(_ =>
|
|
{
|
|
if (added != null && added.Id == 0) added.Id = 314;
|
|
})
|
|
.ReturnsAsync(1);
|
|
|
|
string? auditedEntityId = null;
|
|
_auditMock.Setup(a => a.LogAsync(
|
|
It.IsAny<string>(),
|
|
"Create",
|
|
"SharedScript",
|
|
It.IsAny<string>(),
|
|
It.IsAny<string>(),
|
|
It.IsAny<object?>(),
|
|
It.IsAny<CancellationToken>()))
|
|
.Callback<string, string, string, string, string, object?, CancellationToken>(
|
|
(_, _, _, entityId, _, _, _) => auditedEntityId = entityId)
|
|
.Returns(Task.CompletedTask);
|
|
|
|
var result = await _service.CreateSharedScriptAsync(
|
|
"Helpers", "public static int Add(int a, int b) { return a + b; }", null, null, "admin");
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal("314", auditedEntityId);
|
|
Assert.NotEqual("0", auditedEntityId);
|
|
}
|
|
}
|