Files
maraphon-app/tests/Marathon.UI.Tests/Services/AnomalyBrowsingServiceTests.cs
T
alexei.dolgolyov 34cc72fd2d feat(anomalies): filter the feed by detector kind
Adds a detector-kind chip row to the anomaly feed (SuspensionFlip / SteamMove /
SuspensionFreeze / OverroundCompression), multi-select like the sport filter — so
with four detectors live you can slice the feed to a single signal type. The kind
set lives on AnomalyFilter and filters in-memory alongside severity/sport, persisted
via AnomalyBrowsingState like the other filters.

- AnomalyFilter.Kinds + AnomalyBrowsingService in-memory Where clause; feed chip
  row + ToggleKind/KindLabel; en/ru resx (Anomaly.Filter.Kind).
- 2 tests: kind-filtered subset + no-filter returns all kinds.
2026-05-29 11:38:06 +03:00

78 lines
3.0 KiB
C#

using FluentAssertions;
using Marathon.Application.Abstractions;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
using Marathon.UI.Services;
using NSubstitute;
namespace Marathon.UI.Tests.Services;
/// <summary>
/// Covers the in-memory filtering of <see cref="AnomalyBrowsingService.ListAsync"/> —
/// specifically the detector-kind filter added for the feed.
/// </summary>
public sealed class AnomalyBrowsingServiceTests
{
private static readonly DateTimeOffset T0 = new(2026, 5, 20, 18, 0, 0, TimeSpan.FromHours(3));
// Minimal valid 2-way evidence so TryProject succeeds (kind is what we filter on).
private const string Evidence = """
{"suspensionGapSeconds":90,
"preSuspension":{"capturedAt":"2026-05-20T18:00:00+03:00","p1":0.6,"p2":0.4,"rate1":1.6,"rate2":2.5},
"postSuspension":{"capturedAt":"2026-05-20T18:02:00+03:00","p1":0.4,"p2":0.6,"rate1":2.5,"rate2":1.6}}
""";
private readonly IAnomalyRepository _anomalies = Substitute.For<IAnomalyRepository>();
private readonly IEventRepository _events = Substitute.For<IEventRepository>();
private AnomalyBrowsingService CreateSut() => new(_anomalies, _events);
private int _seq;
private Anomaly Make(AnomalyKind kind) =>
new(Guid.NewGuid(), new EventId($"evt-{_seq++}"), T0, kind, 0.70m, Evidence);
/// <summary>Stubs the anomaly query AND a matching event per anomaly (events exist via FK).</summary>
private void Setup(params Anomaly[] anomalies)
{
_anomalies
.ListByDateRangeAsync(Arg.Any<DateTimeOffset?>(), Arg.Any<DateTimeOffset?>(), Arg.Any<CancellationToken>())
.Returns(anomalies);
var events = anomalies.ToDictionary(
a => a.EventId,
a => new Event(a.EventId, new SportCode(11), "England", "league", string.Empty, T0.AddDays(1), "Home", "Away"));
_events
.GetManyAsync(Arg.Any<IReadOnlyCollection<EventId>>(), Arg.Any<CancellationToken>())
.Returns(events);
}
[Fact]
public async Task ListAsync_FiltersByKind()
{
Setup(
Make(AnomalyKind.SuspensionFlip),
Make(AnomalyKind.SteamMove),
Make(AnomalyKind.OverroundCompression));
var filter = new AnomalyFilter(Kinds: new[] { AnomalyKind.SuspensionFlip, AnomalyKind.OverroundCompression });
var items = await CreateSut().ListAsync(filter, CancellationToken.None);
items.Select(i => i.Kind).Should()
.BeEquivalentTo(new[] { AnomalyKind.SuspensionFlip, AnomalyKind.OverroundCompression });
}
[Fact]
public async Task ListAsync_NoKindFilter_ReturnsAllKinds()
{
Setup(
Make(AnomalyKind.SuspensionFlip),
Make(AnomalyKind.SteamMove),
Make(AnomalyKind.OverroundCompression));
var items = await CreateSut().ListAsync(new AnomalyFilter(), CancellationToken.None);
items.Should().HaveCount(3);
}
}