Files
maraphon-app/tests/Marathon.Application.Tests/UseCases/PullResultsUseCaseTests.cs
alexei.dolgolyov 9c5d3df1f2 feat(phase-8-backend): per-event results harvesting + EventPath plumbing
Implements Phase 8 Amendment 1: marathonbet.by has no public results archive
endpoint, so results must be harvested per-event by re-fetching the event
detail page until eventJsonInfo.matchIsComplete=true.

Backend changes:

* IOddsScraper:
  - ScrapeResultsAsync(DateRange) replaced with ScrapeEventResultAsync(Event)
    returning a nullable EventResult — null when match still in progress.
  - ScrapeEventOddsAsync now takes the full Event (so EventPath drives URL
    construction) instead of bare EventId.
  - New ScrapeLiveAsync() for the /su/live listing.

* Domain:
  - Event gains EventPath (nullable string) — the data-event-path attribute
    captured during scraping; required for reliable URL construction.

* Infrastructure:
  - New migration 20260506000000_AddEventPath adds the column.
  - EventEntity / EventConfiguration / Mapping / model-snapshot updated.
  - MarathonbetScraper: new ScrapeLiveAsync + ScrapeEventResultAsync; URL
    builder prefers EventPath, falls back to numeric ID for legacy rows.
  - EventListingParserBase extracts data-event-path on every listing row.

* Application:
  - PullResultsUseCase: branches on selection vs date-range, emits IProgress<
    PullResultsProgress>, returns ResultLoadOutcome (Loaded / AlreadyLoaded /
    NotYetComplete / Failed); idempotent (skips events whose result already
    exists).
  - PullLiveOddsUseCase now drives off the live listing (auto-discovers
    events that go live without ever appearing in the upcoming list) and
    backfills EventPath on legacy rows.
  - PullUpcomingEventsUseCase wires EventPath on persisted events.

* Workers: UpcomingEventsPoller updates persistence path accordingly.

* Tests: 17 net-new tests across Application + Infrastructure + Domain;
  all 293 still pass.
2026-05-09 15:10:27 +03:00

259 lines
11 KiB
C#

using FluentAssertions;
using Marathon.Application.Abstractions;
using Marathon.Application.Storage;
using Marathon.Application.UseCases;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
namespace Marathon.Application.Tests.UseCases;
public sealed class PullResultsUseCaseTests
{
private readonly IOddsScraper _scraper = Substitute.For<IOddsScraper>();
private readonly IEventRepository _eventRepo = Substitute.For<IEventRepository>();
private readonly IResultRepository _resultRepo = Substitute.For<IResultRepository>();
private static readonly DateRange AnyRange = new(
DateTimeOffset.UtcNow.AddDays(-1),
DateTimeOffset.UtcNow);
private PullResultsUseCase CreateSut() =>
new(_scraper, _eventRepo, _resultRepo,
NullLogger<PullResultsUseCase>.Instance);
// ── Selection mode ──────────────────────────────────────────────────────
[Fact]
public async Task Should_InspectOnlySelectedEvents_When_SelectionIsProvided()
{
// Arrange: 3 events in the DB; only 2 in the selection
var ev1 = TestFixtures.MakeEvent("11111111");
var ev2 = TestFixtures.MakeEvent("22222222");
var ev3 = TestFixtures.MakeEvent("33333333");
_eventRepo.GetAsync(ev1.Id, Arg.Any<CancellationToken>()).Returns(ev1);
_eventRepo.GetAsync(ev2.Id, Arg.Any<CancellationToken>()).Returns(ev2);
_eventRepo.GetAsync(ev3.Id, Arg.Any<CancellationToken>()).Returns(ev3);
_resultRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
_scraper.ScrapeEventResultAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
var selection = new List<EventId> { ev1.Id, ev2.Id };
var sut = CreateSut();
// Act
var (inspected, loaded, skipped) = await sut.ExecuteAsync(AnyRange, selection, CancellationToken.None);
// Assert: ev3 never resolved; only ev1+ev2 inspected
inspected.Should().Be(2);
loaded.Should().Be(0);
skipped.Should().Be(0);
await _eventRepo.DidNotReceive().GetAsync(ev3.Id, Arg.Any<CancellationToken>());
await _eventRepo.DidNotReceive().ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>());
}
// ── Bulk mode ───────────────────────────────────────────────────────────
[Fact]
public async Task Should_InspectAllEventsInRange_When_SelectionIsNull()
{
var ev1 = TestFixtures.MakeEvent("11111111");
var ev2 = TestFixtures.MakeEvent("22222222");
var ev3 = TestFixtures.MakeEvent("33333333");
var allEvents = new List<Event> { ev1, ev2, ev3 }.AsReadOnly();
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
.Returns(allEvents);
_resultRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
_scraper.ScrapeEventResultAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
var sut = CreateSut();
var (inspected, loaded, skipped) = await sut.ExecuteAsync(AnyRange, selection: null, CancellationToken.None);
inspected.Should().Be(3);
loaded.Should().Be(0, "scraper says none of them are complete yet");
skipped.Should().Be(0);
await _eventRepo.Received(1).ListByDateRangeAsync(AnyRange, Arg.Any<CancellationToken>());
}
[Fact]
public async Task Should_InspectAllEventsInRange_When_SelectionIsEmpty()
{
var ev1 = TestFixtures.MakeEvent("11111111");
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
.Returns(new List<Event> { ev1 }.AsReadOnly());
_resultRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
_scraper.ScrapeEventResultAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
var sut = CreateSut();
var (inspected, _, _) = await sut.ExecuteAsync(
AnyRange,
selection: Array.Empty<EventId>(),
CancellationToken.None);
inspected.Should().Be(1);
await _eventRepo.Received(1).ListByDateRangeAsync(AnyRange, Arg.Any<CancellationToken>());
}
// ── Idempotency ─────────────────────────────────────────────────────────
[Fact]
public async Task Should_SkipEventsWithExistingResult_And_BeIdempotent()
{
var ev1 = TestFixtures.MakeEvent("11111111");
var ev2 = TestFixtures.MakeEvent("22222222");
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
.Returns(new List<Event> { ev1, ev2 }.AsReadOnly());
_resultRepo.GetAsync(ev1.Id, Arg.Any<CancellationToken>())
.Returns(TestFixtures.MakeResult(ev1.Id));
_resultRepo.GetAsync(ev2.Id, Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
_scraper.ScrapeEventResultAsync(Arg.Any<Event>(), Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
var sut = CreateSut();
var (_, _, skipped1) = await sut.ExecuteAsync(AnyRange, null, CancellationToken.None);
var (_, _, skipped2) = await sut.ExecuteAsync(AnyRange, null, CancellationToken.None);
skipped1.Should().Be(1, "ev1 already has a result");
skipped2.Should().Be(1, "idempotent: ev1 still skipped on second run");
await _resultRepo.DidNotReceive().AddAsync(Arg.Any<EventResult>(), Arg.Any<CancellationToken>());
await _scraper.DidNotReceive()
.ScrapeEventResultAsync(
Arg.Is<Event>(e => e.Id == ev1.Id),
Arg.Any<CancellationToken>());
}
// ── Successful loads ────────────────────────────────────────────────────
[Fact]
public async Task Should_PersistResults_When_ScraperReturnsCompletedMatch()
{
var ev1 = TestFixtures.MakeEvent("11111111");
var result1 = TestFixtures.MakeResult(ev1.Id);
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
.Returns(new List<Event> { ev1 }.AsReadOnly());
_resultRepo.GetAsync(ev1.Id, Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
_scraper.ScrapeEventResultAsync(
Arg.Is<Event>(e => e.Id == ev1.Id),
Arg.Any<CancellationToken>())
.Returns(result1);
var sut = CreateSut();
var (inspected, loaded, skipped) = await sut.ExecuteAsync(AnyRange, null, CancellationToken.None);
inspected.Should().Be(1);
loaded.Should().Be(1);
skipped.Should().Be(0);
await _resultRepo.Received(1).AddAsync(result1, Arg.Any<CancellationToken>());
await _resultRepo.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
// ── Progress reporting ──────────────────────────────────────────────────
[Fact]
public async Task Should_ReportProgress_OncePerCandidate_With_CorrectOutcome()
{
var ev1 = TestFixtures.MakeEvent("11111111"); // already has result → AlreadyLoaded
var ev2 = TestFixtures.MakeEvent("22222222"); // scrape returns null → NotYetComplete
var ev3 = TestFixtures.MakeEvent("33333333"); // scrape returns res3 → Loaded
var result3 = TestFixtures.MakeResult(ev3.Id);
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
.Returns(new List<Event> { ev1, ev2, ev3 }.AsReadOnly());
_resultRepo.GetAsync(ev1.Id, Arg.Any<CancellationToken>())
.Returns(TestFixtures.MakeResult(ev1.Id));
_resultRepo.GetAsync(ev2.Id, Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
_resultRepo.GetAsync(ev3.Id, Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
_scraper.ScrapeEventResultAsync(
Arg.Is<Event>(e => e.Id == ev2.Id), Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
_scraper.ScrapeEventResultAsync(
Arg.Is<Event>(e => e.Id == ev3.Id), Arg.Any<CancellationToken>())
.Returns(result3);
var ticks = new List<PullResultsProgress>();
var progress = new Progress<PullResultsProgress>(ticks.Add);
var sut = CreateSut();
await sut.ExecuteAsync(AnyRange, null, progress, CancellationToken.None);
// Progress callback runs on the synchronization context — pump it
await Task.Delay(50);
ticks.Should().HaveCount(3);
ticks.Select(t => t.Total).Should().AllBeEquivalentTo(3);
ticks.Select(t => t.Processed).Should().Equal(1, 2, 3);
ticks.Select(t => t.Outcome).Should().Equal(
ResultLoadOutcome.AlreadyLoaded,
ResultLoadOutcome.NotYetComplete,
ResultLoadOutcome.Loaded);
ticks[2].Result.Should().Be(result3);
}
// ── Failure isolation ───────────────────────────────────────────────────
[Fact]
public async Task Should_ContinueAfterScrapeFailure_AndReportFailedOutcome()
{
var ev1 = TestFixtures.MakeEvent("11111111");
var ev2 = TestFixtures.MakeEvent("22222222");
var result2 = TestFixtures.MakeResult(ev2.Id);
_eventRepo.ListByDateRangeAsync(Arg.Any<DateRange>(), Arg.Any<CancellationToken>())
.Returns(new List<Event> { ev1, ev2 }.AsReadOnly());
_resultRepo.GetAsync(Arg.Any<EventId>(), Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
_scraper.ScrapeEventResultAsync(
Arg.Is<Event>(e => e.Id == ev1.Id), Arg.Any<CancellationToken>())
.ThrowsAsync(new HttpRequestException("network down"));
_scraper.ScrapeEventResultAsync(
Arg.Is<Event>(e => e.Id == ev2.Id), Arg.Any<CancellationToken>())
.Returns(result2);
var ticks = new List<PullResultsProgress>();
var progress = new Progress<PullResultsProgress>(ticks.Add);
var sut = CreateSut();
var (inspected, loaded, _) = await sut.ExecuteAsync(AnyRange, null, progress, CancellationToken.None);
await Task.Delay(50);
inspected.Should().Be(2);
loaded.Should().Be(1, "ev1 failed, ev2 loaded");
ticks.Should().HaveCount(2);
ticks[0].Outcome.Should().Be(ResultLoadOutcome.Failed);
ticks[1].Outcome.Should().Be(ResultLoadOutcome.Loaded);
}
}