using Flypaper.Core.Models; using Flypaper.Storage.Dashboard; using Flypaper.Tests.Fakes; using Flypaper.Tests.Mcp; using Shouldly; namespace Flypaper.Tests.Storage; public sealed class RecentCatchesTests : IDisposable { private readonly McpTestData _data = new(); private readonly DashboardQueries _queries; public RecentCatchesTests() => _queries = new DashboardQueries(_data.Factory, new FixedTimeProvider(McpTestData.Now)); [Fact] public async Task Without_filters_returns_all_events_newest_first() { var recent = await _queries.GetRecentAsync(new RecentFilter()); recent.Total.ShouldBe(6); recent.Items.Select(e => e.StartedAt).ShouldBeInOrder(SortDirection.Descending); recent.Countries.Select(c => c.Code).ShouldBe(["CN", "TW", "US"], ignoreOrder: true); } [Fact] public async Task Combines_filters() { var recent = await _queries.GetRecentAsync(new RecentFilter(Window: TimeSpan.FromHours(24), CountryCode: "TW", Outcome: TrapOutcome.Rejected)); recent.Total.ShouldBe(1); recent.Items.ShouldHaveSingleItem().RemoteIp.ShouldBe("198.51.100.7"); } [Theory] [InlineData(4134L, null, null, 2)] [InlineData(null, "203.0.113.9", null, 2)] [InlineData(null, null, "GET /.env", 2)] [InlineData(4134L, null, "GET /.env", 1)] public async Task Matches_provider_ip_and_target_exactly(long? asn, string? ip, string? target, int expected) { var recent = await _queries.GetRecentAsync(new RecentFilter(Asn: asn, Ip: ip, Target: target)); recent.Total.ShouldBe(expected); } [Theory] [InlineData("chinanet", 2)] [InlineData("CURL", 2)] [InlineData("198.51", 2)] [InlineData("wp-login", 1)] [InlineData("100%", 0)] public async Task Search_is_case_insensitive_over_ip_provider_target_and_user_agent(string search, int expected) { var recent = await _queries.GetRecentAsync(new RecentFilter(Search: search)); recent.Total.ShouldBe(expected); } [Fact] public async Task Returns_an_empty_result_when_nothing_matches() { var recent = await _queries.GetRecentAsync(new RecentFilter(CountryCode: "US", Outcome: TrapOutcome.Rejected)); recent.Total.ShouldBe(0); recent.Items.ShouldBeEmpty(); } [Fact] public async Task Limits_the_items_but_counts_all_matches() { var recent = await _queries.GetRecentAsync(new RecentFilter(Limit: 2)); recent.Items.Count.ShouldBe(2); recent.Total.ShouldBe(6); } public void Dispose() => _data.Dispose(); }