// Copyright (c) Microsoft. All rights reserved.
using System.Linq;
using System.Net.Sockets;
using Aspire.Hosting.ApplicationModel;
namespace Aspire.Hosting.AgentFramework.DevUI.UnitTests;
///
/// Unit tests for the class.
///
public class DevUIResourceTests
{
#region Constructor Tests
///
/// Verifies that the resource name is correctly set.
///
[Fact]
public void Constructor_WithName_SetsName()
{
// Arrange & Act
var resource = new DevUIResource("test-devui");
// Assert
Assert.Equal("test-devui", resource.Name);
}
///
/// Verifies that the resource implements IResourceWithEndpoints.
///
[Fact]
public void Resource_ImplementsIResourceWithEndpoints()
{
// Arrange & Act
var resource = new DevUIResource("test-devui");
// Assert
Assert.IsAssignableFrom(resource);
}
///
/// Verifies that the resource implements IResourceWithWaitSupport.
///
[Fact]
public void Resource_ImplementsIResourceWithWaitSupport()
{
// Arrange & Act
var resource = new DevUIResource("test-devui");
// Assert
Assert.IsAssignableFrom(resource);
}
#endregion
#region Endpoint Annotation Tests
///
/// Verifies that the resource has an HTTP endpoint annotation when port is specified.
///
[Fact]
public void Constructor_WithPort_AddsEndpointAnnotation()
{
// Arrange & Act
var resource = CreateResourceWithPort(8090);
// Assert
var endpoint = resource.Annotations.OfType().FirstOrDefault();
Assert.NotNull(endpoint);
Assert.Equal("http", endpoint.Name);
Assert.Equal(8090, endpoint.Port);
}
///
/// Verifies that the endpoint annotation has correct protocol type.
///
[Fact]
public void EndpointAnnotation_HasTcpProtocol()
{
// Arrange
var resource = CreateResourceWithPort(8080);
// Act
var endpoint = resource.Annotations.OfType().First();
// Assert
Assert.Equal(ProtocolType.Tcp, endpoint.Protocol);
}
///
/// Verifies that the endpoint annotation has HTTP URI scheme.
///
[Fact]
public void EndpointAnnotation_HasHttpUriScheme()
{
// Arrange
var resource = CreateResourceWithPort(8080);
// Act
var endpoint = resource.Annotations.OfType().First();
// Assert
Assert.Equal("http", endpoint.UriScheme);
}
///
/// Verifies that the endpoint is not proxied.
///
[Fact]
public void EndpointAnnotation_IsNotProxied()
{
// Arrange
var resource = CreateResourceWithPort(8080);
// Act
var endpoint = resource.Annotations.OfType().First();
// Assert
Assert.False(endpoint.IsProxied);
}
///
/// Verifies that the endpoint target host is localhost.
///
[Fact]
public void EndpointAnnotation_TargetHostIsLocalhost()
{
// Arrange
var resource = CreateResourceWithPort(8080);
// Act
var endpoint = resource.Annotations.OfType().First();
// Assert
Assert.Equal("localhost", endpoint.TargetHost);
}
///
/// Verifies that the endpoint has no fixed port when null is passed.
///
[Fact]
public void Constructor_WithNullPort_EndpointHasNullPort()
{
// Arrange & Act
var resource = CreateResourceWithPort(null);
// Assert
var endpoint = resource.Annotations.OfType().FirstOrDefault();
Assert.NotNull(endpoint);
Assert.Null(endpoint.Port);
}
#endregion
#region PrimaryEndpoint Tests
///
/// Verifies that PrimaryEndpoint returns an endpoint reference.
///
[Fact]
public void PrimaryEndpoint_ReturnsEndpointReference()
{
// Arrange
var resource = CreateResourceWithPort(8080);
// Act
var endpoint = resource.PrimaryEndpoint;
// Assert
Assert.NotNull(endpoint);
Assert.Same(resource, endpoint.Resource);
}
///
/// Verifies that PrimaryEndpoint returns the same instance on multiple calls.
///
[Fact]
public void PrimaryEndpoint_MultipleCalls_ReturnsSameInstance()
{
// Arrange
var resource = CreateResourceWithPort(8080);
// Act
var endpoint1 = resource.PrimaryEndpoint;
var endpoint2 = resource.PrimaryEndpoint;
// Assert
Assert.Same(endpoint1, endpoint2);
}
#endregion
private static DevUIResource CreateResourceWithPort(int? port) => new("test-devui", port);
}