diff --git a/tests/ScadaLink.CentralUI.Tests/DataConnectionFormTests.cs b/tests/ScadaLink.CentralUI.Tests/DataConnectionFormTests.cs new file mode 100644 index 0000000..5e13757 --- /dev/null +++ b/tests/ScadaLink.CentralUI.Tests/DataConnectionFormTests.cs @@ -0,0 +1,100 @@ +using System.Security.Claims; +using System.Text.Json; +using Bunit; +using Microsoft.AspNetCore.Components.Authorization; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; +using ScadaLink.Commons.Entities.Sites; +using ScadaLink.Commons.Interfaces.Repositories; +using DataConnectionForm = ScadaLink.CentralUI.Components.Pages.Admin.DataConnectionForm; + +namespace ScadaLink.CentralUI.Tests; + +public class DataConnectionFormTests : BunitContext +{ + private readonly ISiteRepository _siteRepo = Substitute.For(); + + public DataConnectionFormTests() + { + Services.AddSingleton(_siteRepo); + AddTestAuth(); + var sites = new List { new("Plant-A", "plant-a") { Id = 1 } }; + _siteRepo.GetAllSitesAsync(Arg.Any()) + .Returns(Task.FromResult>(sites)); + } + + private void AddTestAuth() + { + var claims = new[] + { + new Claim("Username", "tester"), + new Claim(ClaimTypes.Role, "Admin") + }; + var user = new ClaimsPrincipal(new ClaimsIdentity(claims, "TestAuth")); + Services.AddSingleton(new TestAuthStateProvider(user)); + Services.AddAuthorizationCore(); + } + + [Fact] + public void NoProtocolDropdown_IsRendered() + { + var cut = Render(p => p.Add(f => f.SiteId, 1)); + Assert.DoesNotContain("Custom", cut.Markup); + var labels = cut.FindAll("label").Select(l => l.TextContent.Trim()).ToList(); + Assert.DoesNotContain(labels, l => l == "Protocol"); + } + + [Fact] + public async Task Save_InvalidPrimaryUrl_DoesNotCallRepo() + { + var cut = Render(p => p.Add(f => f.SiteId, 1)); + cut.FindAll("input[type='text']") + .First(i => i.GetAttribute("placeholder")?.StartsWith("opc.tcp") == true) + .Change("not-a-url"); + + // Name field (the first text input that is NOT the OPC URL) + cut.FindAll("input[type='text']") + .First(i => i.GetAttribute("placeholder") is null + || !i.GetAttribute("placeholder")!.StartsWith("opc.tcp")) + .Change("My Connection"); + + await cut.FindAll("button") + .First(b => b.TextContent.Trim() == "Save").ClickAsync(new()); + + await _siteRepo.DidNotReceive().AddDataConnectionAsync(Arg.Any()); + Assert.Contains("Endpoint URL must be a valid", cut.Markup); + } + + [Fact] + public async Task Save_ValidConfig_PersistsTypedJsonAndProtocolOpcUa() + { + DataConnection? captured = null; + await _siteRepo.AddDataConnectionAsync( + Arg.Do(d => captured = d)); + + var cut = Render(p => p.Add(f => f.SiteId, 1)); + + // Name + cut.FindAll("input[type='text']") + .First(i => i.GetAttribute("placeholder") is null + || !i.GetAttribute("placeholder")!.StartsWith("opc.tcp")) + .Change("PLC-1"); + // Endpoint URL + cut.FindAll("input[type='text']") + .First(i => i.GetAttribute("placeholder")?.StartsWith("opc.tcp") == true) + .Change("opc.tcp://plant-a:4840"); + + await cut.FindAll("button") + .First(b => b.TextContent.Trim() == "Save").ClickAsync(new()); + + Assert.NotNull(captured); + Assert.Equal("OpcUa", captured!.Protocol); + Assert.NotNull(captured.PrimaryConfiguration); + + using var doc = JsonDocument.Parse(captured.PrimaryConfiguration!); + Assert.Equal("opc.tcp://plant-a:4840", + doc.RootElement.GetProperty("endpointUrl").GetString()); + Assert.Equal(60000, + doc.RootElement.GetProperty("sessionTimeoutMs").GetInt32()); + } +}