namespace GmRelay.Bot.Tests.Web;

public sealed class HomePageSourceTests
{
    [Fact]
    public async Task HomePage_ShouldShowPlatformBadgeForGroups()
    {
        var source = await ReadRepositoryFileAsync("src/GmRelay.Web/Components/Pages/Home.razor");

        Assert.Contains("platform-badge", source, StringComparison.Ordinal);
        Assert.Contains("FormatPlatform", source, StringComparison.Ordinal);
        Assert.Contains("group.Platform", source, StringComparison.Ordinal);
    }

    [Fact]
    public async Task SessionService_ShouldUseSessionTitleWhenDiscordGroupNameIsOnlyId()
    {
        var source = await ReadRepositoryFileAsync("src/GmRelay.Web/Services/SessionService.cs");

        Assert.Contains("latest_session.title", source, StringComparison.Ordinal);
        Assert.Contains("NULLIF(g.name, g.external_group_id)", source, StringComparison.Ordinal);
    }

    private static async Task<string> ReadRepositoryFileAsync(string relativePath)
    {
        var dir = AppContext.BaseDirectory;
        while (!string.IsNullOrEmpty(dir) && !File.Exists(Path.Combine(dir, "Directory.Build.props")))
        {
            dir = Directory.GetParent(dir)?.FullName;
        }

        var repoRoot = dir ?? throw new InvalidOperationException("Could not find repo root");
        return await File.ReadAllTextAsync(Path.Combine(repoRoot, relativePath));
    }
}
