using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.CentralUI.Services;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Schemas;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Design;
///
/// Tests for the schema-library read-accessor that the schema-driven
/// value-entry forms reuse to resolve {"$ref":"lib:Name"} pointers. It projects
/// onto a name → schema-JSON map (and a single-name
/// lookup) over a fresh DI scope. The repository is substituted and registered scoped so
/// the service's IServiceScopeFactory resolves it exactly as it would at runtime.
///
public class SchemaLibraryQueryServiceTests
{
private static (SchemaLibraryQueryService Service, ISharedSchemaRepository Repo) Build()
{
var repo = Substitute.For();
var services = new ServiceCollection();
services.AddScoped(_ => repo);
var provider = services.BuildServiceProvider();
var service = new SchemaLibraryQueryService(provider.GetRequiredService());
return (service, repo);
}
/// Verifies that the schema map associates each library name with its schema JSON.
/// A task that represents the asynchronous operation.
[Fact]
public async Task GetSchemaMapAsync_MapsNameToSchemaJson()
{
var (service, repo) = Build();
repo.ListAsync(Arg.Any()).Returns(new List
{
new() { Id = 1, Name = "Address", SchemaJson = "{\"type\":\"object\"}" },
new() { Id = 2, Name = "GeoPoint", SchemaJson = "{\"type\":\"array\"}" },
});
var map = await service.GetSchemaMapAsync();
Assert.Equal(2, map.Count);
Assert.Equal("{\"type\":\"object\"}", map["Address"]);
Assert.Equal("{\"type\":\"array\"}", map["GeoPoint"]);
}
/// Verifies that an empty library yields an empty schema map.
/// A task that represents the asynchronous operation.
[Fact]
public async Task GetSchemaMapAsync_EmptyLibrary_ReturnsEmptyMap()
{
var (service, repo) = Build();
repo.ListAsync(Arg.Any()).Returns(new List());
var map = await service.GetSchemaMapAsync();
Assert.Empty(map);
}
/// Verifies that resolving a known name returns its schema JSON.
/// A task that represents the asynchronous operation.
[Fact]
public async Task ResolveAsync_KnownName_ReturnsSchemaJson()
{
var (service, repo) = Build();
repo.GetByNameAsync("Address", Arg.Any())
.Returns(new SharedSchema { Id = 1, Name = "Address", SchemaJson = "{\"type\":\"object\"}" });
var json = await service.ResolveAsync("Address");
Assert.Equal("{\"type\":\"object\"}", json);
}
/// Verifies that resolving an unknown name returns null.
/// A task that represents the asynchronous operation.
[Fact]
public async Task ResolveAsync_UnknownName_ReturnsNull()
{
var (service, repo) = Build();
repo.GetByNameAsync("Missing", Arg.Any()).Returns((SharedSchema?)null);
Assert.Null(await service.ResolveAsync("Missing"));
}
/// Verifies that resolving an empty name returns null without querying the repository.
/// A task that represents the asynchronous operation.
[Fact]
public async Task ResolveAsync_EmptyName_ReturnsNull_WithoutHittingRepo()
{
var (service, repo) = Build();
Assert.Null(await service.ResolveAsync(""));
await repo.DidNotReceive().GetByNameAsync(Arg.Any(), Arg.Any());
}
}