using FluentAssertions;
using SatelliteProvider.IntegrationTests;

namespace SatelliteProvider.Tests;

public class PerfBootstrapPt10Tests
{
    [Fact]
    public void Percentile_MatchesHarnessFormula_AZ1124_AC2()
    {
        var values = new long[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

        PerfBootstrap.Percentile(values, 50).Should().Be(50);
        PerfBootstrap.Percentile(values, 95).Should().Be(100);
    }

    [Fact]
    public void PerfScript_DoesNotInlineJwtMint_AZ1124_AC6()
    {
        var path = LocateRepoFile(Path.Combine("scripts", "run-performance-tests.sh"));
        path.Should().NotBeNull();

        var content = File.ReadAllText(path!);
        content.Should().Contain("--run-pt10");
        content.Should().NotContain("JwtSecurityToken");
        content.Should().NotContain("new JwtSecurityToken(");
    }

    [Fact]
    public void Program_DispatchesRunPt10Subcommand_AZ1124_AC1()
    {
        var path = LocateRepoFile(Path.Combine("SatelliteProvider.IntegrationTests", "Program.cs"));
        path.Should().NotBeNull();

        var content = File.ReadAllText(path!);
        content.Should().Contain("--run-pt10");
        content.Should().Contain("RunPt10Async");
    }

    private static string? LocateRepoFile(string relativePath)
    {
        var dir = new DirectoryInfo(Directory.GetCurrentDirectory());
        while (dir is not null)
        {
            var candidate = Path.Combine(dir.FullName, relativePath);
            if (File.Exists(candidate))
            {
                return candidate;
            }

            dir = dir.Parent;
        }

        return null;
    }
}
