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.
This commit is contained in:
2026-05-09 15:10:27 +03:00
parent 1bbf4fcfed
commit 9c5d3df1f2
18 changed files with 638 additions and 217 deletions
@@ -78,12 +78,13 @@ public sealed class LiveOddsPollerTests
var eventRepo = Substitute.For<IEventRepository>();
var snapshotRepo = Substitute.For<ISnapshotRepository>();
// ScrapeUpcomingAsync called by use case internally
scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>())
.Returns(Array.Empty<Event>());
eventRepo.ListAsync(Arg.Any<CancellationToken>())
// Use case discovers live events via ScrapeLiveAsync (NOT ListAsync)
scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>())
.Returns(new List<Event> { ev }.AsReadOnly());
scraper.ScrapeEventOddsAsync(eventId, OddsSource.Live, Arg.Any<CancellationToken>())
eventRepo.GetAsync(eventId, Arg.Any<CancellationToken>())
.Returns(ev);
scraper.ScrapeEventOddsAsync(
Arg.Is<Event>(e => e.Id == eventId), OddsSource.Live, Arg.Any<CancellationToken>())
.Returns(MakeSnapshot(eventId));
var sp = BuildServiceProvider(scraper, eventRepo, snapshotRepo);
@@ -122,7 +123,7 @@ public sealed class LiveOddsPollerTests
// Assert — no snapshot attempts while disabled
await snapshotRepo.DidNotReceive().AddAsync(Arg.Any<OddsSnapshot>(), Arg.Any<CancellationToken>());
await eventRepo.DidNotReceive().ListAsync(Arg.Any<CancellationToken>());
await scraper.DidNotReceive().ScrapeLiveAsync(Arg.Any<CancellationToken>());
}
[Fact]
@@ -133,10 +134,11 @@ public sealed class LiveOddsPollerTests
var eventRepo = Substitute.For<IEventRepository>();
var snapshotRepo = Substitute.For<ISnapshotRepository>();
scraper.ScrapeUpcomingAsync(null, Arg.Any<CancellationToken>())
.Returns(Array.Empty<Event>());
eventRepo.ListAsync(Arg.Any<CancellationToken>())
.ThrowsAsync(new InvalidOperationException("DB unavailable"));
// Use case calls ScrapeLiveAsync first; if it throws, the cycle returns 0
// without hitting the repo. To exercise the "poller survives failures"
// path, make ScrapeLiveAsync itself throw.
scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>())
.ThrowsAsync(new InvalidOperationException("listing unavailable"));
var sp = BuildServiceProvider(scraper, eventRepo, snapshotRepo);
var opts = BuildOptions(enabled: true, intervalSeconds: 0);
@@ -153,7 +155,9 @@ public sealed class LiveOddsPollerTests
// Assert — StopAsync must not propagate the exception
await stopAct.Should().NotThrowAsync();
// DB was hit multiple times (poller didn't give up after first failure)
await eventRepo.Received().ListAsync(Arg.Any<CancellationToken>());
// Scraper was hit at least once (poller cycled at least one iteration).
// The use case swallows ScrapeLiveAsync exceptions and returns 0, so the
// poller's catch block is not triggered — but it still cycles.
await scraper.Received().ScrapeLiveAsync(Arg.Any<CancellationToken>());
}
}