123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
using Forge.Features.Browse;
using Forge.Git;
namespace Forge.Tests;
public class InsightsTests
{
[Fact]
public void Languages_are_shared_by_bytes_and_ignore_data_and_vendored_files()
{
var shares = RepoInsights.ComputeLanguages(
[
new FileEntry("src/App.cs", 600),
new FileEntry("src/Deep/Other.cs", 150),
new FileEntry("web/site.js", 250),
new FileEntry("README.md", 10_000),
new FileEntry("package.json", 10_000),
new FileEntry("node_modules/left-pad/index.js", 50_000),
new FileEntry("web/wwwroot/lib/highlight/highlight.min.js", 50_000),
new FileEntry("web/jquery.min.js", 50_000),
new FileEntry("empty.py", 0),
]);
Assert.Equal(["csharp", "javascript"], shares.Select(s => s.Key));
Assert.Equal("C#", shares[0].Name);
Assert.Equal(75.0, shares[0].Percent, 3);
Assert.Equal(25.0, shares[1].Percent, 3);
}
[Fact]
public void Languages_beyond_the_first_six_are_grouped_as_other()
{
string[] extensions = [".cs", ".js", ".ts", ".py", ".go", ".rs", ".java", ".rb"];
var files = extensions.Select((ext, i) => new FileEntry("file" + ext, 1000 - i * 100)).ToList();
var shares = RepoInsights.ComputeLanguages(files);
Assert.Equal(7, shares.Count);
Assert.Equal("other", shares[^1].Key);
Assert.Null(shares[^1].Name);
Assert.Equal(100.0, shares.Sum(s => s.Percent), 3);
}
[Fact]
public void A_repository_without_code_has_no_languages()
{
Assert.Empty(RepoInsights.ComputeLanguages([new FileEntry("README.md", 100), new FileEntry("data.json", 5)]));
}
[Fact]
public void Activity_counts_people_by_email_and_commits_by_utc_day()
{
var day = new DateTimeOffset(2026, 3, 10, 23, 30, 0, TimeSpan.Zero);
var activity = RepoInsights.ComputeActivity(
[
("Jessie", "jessie@example.com", day.AddHours(1)), // Already the 11th in UTC.
("jessie (laptop)", "Jessie@Example.com", day),
("Sam", "sam@example.com", day.AddDays(-2)),
("Jessie", "jessie@example.com", day.AddDays(-3)),
], limit: 100);
Assert.Equal(4, activity.Commits);
Assert.False(activity.Truncated);
Assert.Equal(["Jessie", "Sam"], activity.Contributors.Select(c => c.Name));
Assert.Equal(3, activity.Contributors[0].Commits);
Assert.Equal(day.AddDays(-3), activity.Contributors[0].First);
Assert.Equal(day.AddHours(1), activity.Contributors[0].Last);
Assert.Equal(1, activity.CommitsByDay[new DateOnly(2026, 3, 11)]);
Assert.Equal(1, activity.CommitsByDay[new DateOnly(2026, 3, 10)]);
Assert.False(activity.CommitsByDay.ContainsKey(new DateOnly(2026, 3, 9)));
Assert.True(RepoInsights.ComputeActivity([("A", "a@example.com", day)], limit: 1).Truncated);
}
[Fact]
public void File_list_keeps_full_paths_and_skips_submodules()
{
var output =
"100644 blob 1111111111111111111111111111111111111111 12\tREADME.md\0"
+ "160000 commit 2222222222222222222222222222222222222222 -\tvendor/sub\0"
+ "100755 blob 3333333333333333333333333333333333333333 2048\tsrc/dir with space/run.sh\0";
var files = GitParsers.ParseFileList(output);
Assert.Equal([new FileEntry("README.md", 12), new FileEntry("src/dir with space/run.sh", 2048)], files);
}
}