File: DashboardUIHelpersTests.cs
Web Access
Project: src\tests\Aspire.Dashboard.Tests\Aspire.Dashboard.Tests.csproj (Aspire.Dashboard.Tests)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using Aspire.Dashboard.Utils;
using Xunit;
 
namespace Aspire.Dashboard.Tests;
 
public class DashboardUIHelpersTests
{
    [Theory]
    [InlineData("redis://:password@localhost:6379", "redis://localhost:6379")]
    [InlineData("https://user:p%40ssword@[::1]:7443/a%2Fb?token=secret#access_token=secret", "https://[::1]:7443/a%2Fb?token=MASK")]
    [InlineData("HTTPS://User:password@Host/a@b?token=secret&key=secret", "HTTPS://Host/a@b?token=MASK&key=MASK")]
    [InlineData("https://host/path#access_token=secret?other=secret", "https://host/path")]
    [InlineData("https://host/path?token=secret#access_token=secret", "https://host/path?token=MASK")]
    [InlineData("tcp://host:6379/path%23fragment?token=secret", "tcp://host:6379/path%23fragment?token=MASK")]
    [InlineData("https://host/a@b?token=secret@host", "https://host/a@b?token=MASK")]
    [InlineData("https://host/path", "https://host/path")]
    public void MaskUrlForDisplay_RemovesCredentialsAndFragmentsAndMasksQueryValues(string url, string expected)
    {
        Assert.Equal(expected.Replace("MASK", new string('\u25cf', 8)), DashboardUIHelpers.MaskUrlForDisplay(url));
    }
 
    [Theory]
    [InlineData(200_000, 200_000)]
    [InlineData(600_000, 200_000)]
    public void GetVirtualizedItemCount_LimitsLargeResultSets(int totalItemCount, int expected)
    {
        Assert.Equal(expected, DashboardUIHelpers.GetVirtualizedItemCount(totalItemCount));
    }
 
    [Theory]
    [InlineData(0, 0)]
    [InlineData(1000, 1000)]
    [InlineData(1.5, 1)]
    [InlineData(999.9, 999)]
    [InlineData(-1000, -1000)]
    [InlineData(-1.5, -1)]
    public void SafeConvertToMilliseconds_NormalValues_ReturnsExpectedValue(double milliseconds, int expected)
    {
        // Arrange
        var duration = TimeSpan.FromMilliseconds(milliseconds);
 
        // Act
        var result = DashboardUIHelpers.SafeConvertToMilliseconds(duration);
 
        // Assert
        Assert.Equal(expected, result);
    }
 
    [Fact]
    public void SafeConvertToMilliseconds_ValueExceedsIntMaxValue_ReturnsIntMaxValue()
    {
        // Arrange
        var duration = TimeSpan.FromMilliseconds((double)int.MaxValue + 1000);
 
        // Act
        var result = DashboardUIHelpers.SafeConvertToMilliseconds(duration);
 
        // Assert
        Assert.Equal(int.MaxValue, result);
    }
 
    [Fact]
    public void SafeConvertToMilliseconds_ValueEqualsIntMaxValue_ReturnsIntMaxValue()
    {
        // Arrange
        var duration = TimeSpan.FromMilliseconds(int.MaxValue);
 
        // Act
        var result = DashboardUIHelpers.SafeConvertToMilliseconds(duration);
 
        // Assert
        Assert.Equal(int.MaxValue, result);
    }
 
    [Fact]
    public void SafeConvertToMilliseconds_ValueBelowIntMinValue_ReturnsIntMinValue()
    {
        // Arrange
        var duration = TimeSpan.FromMilliseconds((double)int.MinValue - 1000);
 
        // Act
        var result = DashboardUIHelpers.SafeConvertToMilliseconds(duration);
 
        // Assert
        Assert.Equal(int.MinValue, result);
    }
 
    [Fact]
    public void SafeConvertToMilliseconds_VeryLargeDuration_ReturnsIntMaxValue()
    {
        // Arrange - Create a duration that would overflow int when converted to milliseconds
        // TimeSpan.MaxValue is about 10,675,199 days or roughly 922 trillion milliseconds
        var duration = TimeSpan.MaxValue;
 
        // Act
        var result = DashboardUIHelpers.SafeConvertToMilliseconds(duration);
 
        // Assert
        Assert.Equal(int.MaxValue, result);
    }
 
    [Fact]
    public void SafeConvertToMilliseconds_VerySmallDuration_ReturnsIntMinValue()
    {
        // Arrange
        var duration = TimeSpan.MinValue;
 
        // Act
        var result = DashboardUIHelpers.SafeConvertToMilliseconds(duration);
 
        // Assert
        Assert.Equal(int.MinValue, result);
    }
 
    [Fact]
    public void GetMaskingText_ReturnsClassedMarkupAndPlainText()
    {
        var mask = DashboardUIHelpers.GetMaskingText(3);
 
        Assert.Equal("<span class=\"masked-content\">&#x25cf;&#x25cf;&#x25cf;</span>", mask.MarkupString.Value);
        Assert.Equal("●●●", mask.Text);
    }
}