using JdeScoping.DataSync.Dev.Configuration;
using JdeScoping.DataSync.Dev.Contracts;
using JdeScoping.DataSync.Dev.Services;
using JdeScoping.DataSync.Etl.Pipeline;
using JdeScoping.DataSync.Etl.Results;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using Shouldly;
namespace JdeScoping.DataSync.Dev.Tests;
///
/// Tests for DevEtlRegistry.
///
public class DevEtlRegistryTests : IDisposable
{
private readonly IDevEtlPipelineFactory _mockFactory;
private readonly ILogger _logger;
private readonly string _tempDirectory;
public DevEtlRegistryTests()
{
_mockFactory = Substitute.For();
_logger = NullLogger.Instance;
// Create a temp directory for tests
_tempDirectory = Path.Combine(Path.GetTempPath(), $"DevEtlRegistryTests_{Guid.NewGuid():N}");
Directory.CreateDirectory(_tempDirectory);
}
[Fact]
public void Constructor_WithValidArguments_Succeeds()
{
// Arrange
_mockFactory.GetAvailableTables().Returns(new[] { "Branch" });
// Act
var registry = new DevEtlRegistry(_mockFactory, _tempDirectory, _logger);
// Assert
registry.ShouldNotBeNull();
}
[Fact]
public void Constructor_WithNullFactory_ThrowsArgumentNullException()
{
// Act & Assert
Should.Throw(() => new DevEtlRegistry(null!, _tempDirectory, _logger));
}
[Fact]
public void Constructor_WithEmptyCacheDirectory_ThrowsArgumentException()
{
// Act & Assert
Should.Throw(() => new DevEtlRegistry(_mockFactory, string.Empty, _logger));
}
[Fact]
public void Constructor_WithNonExistentCacheDirectory_ThrowsDirectoryNotFoundException()
{
// Arrange
var nonExistentPath = "/nonexistent/cache/directory";
// Act & Assert
Should.Throw(() => new DevEtlRegistry(_mockFactory, nonExistentPath, _logger));
}
[Fact]
public void GetAvailableTables_DelegatesToFactory()
{
// Arrange
var expectedTables = new[] { "Branch", "Item", "Lot" };
_mockFactory.GetAvailableTables().Returns(expectedTables);
var registry = new DevEtlRegistry(_mockFactory, _tempDirectory, _logger);
// Act
var tables = registry.GetAvailableTables().ToList();
// Assert
tables.ShouldBe(expectedTables);
_mockFactory.Received(1).GetAvailableTables();
}
public void Dispose()
{
// Cleanup temp directory
if (Directory.Exists(_tempDirectory))
{
try
{
Directory.Delete(_tempDirectory, recursive: true);
}
catch
{
// Ignore cleanup errors
}
}
}
}