// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.CommandLine.Parsing;
using AzureMcp.Acr.Commands.Registry;
using AzureMcp.Acr.Services;
using AzureMcp.Core.Models.Command;
using AzureMcp.Core.Options;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;
namespace AzureMcp.Acr.UnitTests.Registry;
public class RegistryListCommandTests
{
private readonly IServiceProvider _serviceProvider;
private readonly IAcrService _service;
private readonly ILogger<RegistryListCommand> _logger;
private readonly RegistryListCommand _command;
private readonly CommandContext _context;
private readonly Parser _parser;
public RegistryListCommandTests()
{
_service = Substitute.For<IAcrService>();
_logger = Substitute.For<ILogger<RegistryListCommand>>();
var collection = new ServiceCollection().AddSingleton(_service);
_serviceProvider = collection.BuildServiceProvider();
_command = new(_logger);
_context = new(_serviceProvider);
_parser = new(_command.GetCommand());
}
[Fact]
public void Constructor_InitializesCommandCorrectly()
{
var command = _command.GetCommand();
Assert.Equal("list", command.Name);
Assert.NotNull(command.Description);
Assert.NotEmpty(command.Description);
}
[Theory]
[InlineData("--subscription sub", true)]
[InlineData("--subscription sub --resource-group rg", true)]
[InlineData("", false)]
public async Task ExecuteAsync_ValidatesInputCorrectly(string args, bool shouldSucceed)
{
// Arrange
if (shouldSucceed)
{
_service.ListRegistries(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<RetryPolicyOptions>())
.Returns(new List<AzureMcp.Acr.Models.AcrRegistryInfo>
{
new("registry1", "eastus", "registry1.azurecr.io", "Basic", "Basic"),
new("registry2", "eastus2", "registry2.azurecr.io", "Standard", "Standard")
});
}
var parseResult = _parser.Parse(args.Split(' ', StringSplitOptions.RemoveEmptyEntries));
// Act
var response = await _command.ExecuteAsync(_context, parseResult);
// Assert
Assert.Equal(shouldSucceed ? 200 : 400, response.Status);
if (shouldSucceed)
{
Assert.NotNull(response.Results);
}
else
{
Assert.Contains("required", response.Message.ToLower());
}
}
[Fact]
public async Task ExecuteAsync_HandlesServiceErrors()
{
// Arrange
_service.ListRegistries(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<RetryPolicyOptions>())
.Returns(Task.FromException<List<AzureMcp.Acr.Models.AcrRegistryInfo>>(new Exception("Test error")));
var parseResult = _parser.Parse(["--subscription", "sub"]);
// Act
var response = await _command.ExecuteAsync(_context, parseResult);
// Assert
Assert.Equal(500, response.Status);
Assert.Contains("Test error", response.Message);
Assert.Contains("troubleshooting", response.Message);
}
[Fact]
public async Task ExecuteAsync_FiltersById_ReturnsFilteredRegistries()
{
// Arrange
var expectedRegistries = new List<AzureMcp.Acr.Models.AcrRegistryInfo> { new("registry1", null, null, null, null) };
_service.ListRegistries("sub", "rg", Arg.Any<string>(), Arg.Any<RetryPolicyOptions>())
.Returns(expectedRegistries);
var parseResult = _parser.Parse(["--subscription", "sub", "--resource-group", "rg"]);
// Act
var response = await _command.ExecuteAsync(_context, parseResult);
// Assert
Assert.Equal(200, response.Status);
Assert.NotNull(response.Results);
await _service.Received(1).ListRegistries("sub", "rg", Arg.Any<string>(), Arg.Any<RetryPolicyOptions>());
}
[Fact]
public async Task ExecuteAsync_EmptyList_ReturnsNullResults()
{
// Arrange
_service.ListRegistries("sub", null, Arg.Any<string>(), Arg.Any<RetryPolicyOptions>())
.Returns(new List<AzureMcp.Acr.Models.AcrRegistryInfo>());
var parseResult = _parser.Parse(["--subscription", "sub"]);
// Act
var response = await _command.ExecuteAsync(_context, parseResult);
// Assert
Assert.Equal(200, response.Status);
Assert.Null(response.Results);
}
[Fact]
public async Task ExecuteAsync_ReturnsExpectedRegistryProperties()
{
// Arrange
var registry = new AzureMcp.Acr.Models.AcrRegistryInfo("myregistry", "eastus", "myregistry.azurecr.io", "Basic", "Basic");
_service.ListRegistries("sub", null, Arg.Any<string>(), Arg.Any<RetryPolicyOptions>())
.Returns(new List<AzureMcp.Acr.Models.AcrRegistryInfo> { registry });
var parseResult = _parser.Parse(["--subscription", "sub"]);
// Act
var response = await _command.ExecuteAsync(_context, parseResult);
// Assert
Assert.Equal(200, response.Status);
Assert.NotNull(response.Results);
}
}