Files
maraphon-app/tests/Marathon.Application.Tests/UseCases/ResolvePendingBetsUseCaseTests.cs
T
alexei.dolgolyov 1ad896b07e feat(my-bets): personal bet journal with CLV tracking
Adds a manual bet-tracking journal that turns the analyzer into an actual
bet tracker. Users record wagers; the journal auto-grades them when event
results land and computes per-bet Closing-Line-Value against the latest
pre-match snapshot — the strongest long-run indicator of betting skill.

Domain:
- PlacedBet entity (reuses Bet vocabulary for Scope/Type/Side/Value/Rate)
  with stake, placed-at, outcome, and notes. Derived GrossReturn / NetProfit.
- BetOutcome enum (Pending / Won / Lost / Void).
- BetOutcomeResolver: pure function grading any Match-scope bet against an
  EventResult. Handles 1X2, draws, handicap (incl. push), and totals.
  Period-scope bets stay manual since EventResult only carries full-time.

Application:
- IPlacedBetRepository abstraction.
- ClosingLineValueCalculator: pure CLV math (implied-probability delta) +
  snapshot-matching predicate by Scope/Type/Side/Value.
- BetJournalReport + BetJournalStats records.
- Four use cases: Record / ResolvePending / BuildReport / Delete.
- New ISnapshotRepository.GetLatestPreMatchAsync pushes the closing-line
  pick into a single SQLite query rather than materialising the 30-day
  window in memory per event.
- ROI turnover excludes Void stakes — pushes are not real turnover and
  including them would dilute the user's edge.

Infrastructure:
- PlacedBetEntity / Configuration / Repository / Mapping helpers.
- 20260516 migration adding the PlacedBets table with EventCode and
  Outcome indices. Intentionally NO foreign key to Events — the journal
  is user data and must survive snapshot-retention pruning. Covered by an
  explicit round-trip test.

UI:
- Pages/MyBets/Journal.razor: hero header, 4-card KPI strip (ROI / strike
  rate / avg CLV / net profit, tinted by tone), inline add-bet form with
  the same invariants as the Bet record, drill-down table with per-row
  outcome pills, CLV percentage-points column, P&L, notes underline, and
  inline-confirm delete. RU + EN i18n.
- Nav entry under Analysis.

Tests: +55 across Domain / Application / Infrastructure (resolver math
including handicap push and total push boundaries, PlacedBet invariants
and derived properties, CLV math + null-handling, four use cases under
NSubstitute, EF round-trip including survives-event-deletion). All 379
tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-16 17:45:42 +03:00

92 lines
3.5 KiB
C#

using FluentAssertions;
using Marathon.Application.Abstractions;
using Marathon.Application.UseCases;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
namespace Marathon.Application.Tests.UseCases;
public sealed class ResolvePendingBetsUseCaseTests
{
private static readonly TimeSpan MoscowOffset = TimeSpan.FromHours(3);
private static readonly DateTimeOffset Placed =
new(2026, 5, 16, 12, 0, 0, MoscowOffset);
private readonly IPlacedBetRepository _bets = Substitute.For<IPlacedBetRepository>();
private readonly IResultRepository _results = Substitute.For<IResultRepository>();
private ResolvePendingBetsUseCase CreateSut() =>
new(_bets, _results, NullLogger<ResolvePendingBetsUseCase>.Instance);
private static PlacedBet MakePending(EventId id, Side side = Side.Side1) =>
new(
Guid.NewGuid(), id,
new Bet(MatchScope.Instance, BetType.Win, side, null, new OddsRate(2.10m)),
100m, Placed, BetOutcome.Pending, null);
[Fact]
public async Task Should_ReturnZero_When_NoPendingBets()
{
_bets.ListByOutcomeAsync(BetOutcome.Pending, Arg.Any<CancellationToken>())
.Returns(Array.Empty<PlacedBet>().ToList().AsReadOnly());
var count = await CreateSut().ExecuteAsync(CancellationToken.None);
count.Should().Be(0);
await _bets.DidNotReceive().UpdateAsync(Arg.Any<PlacedBet>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Should_GradeBetsWithResults_AndLeaveOthersAlone()
{
var idGraded = new EventId("event-1");
var idUngraded = new EventId("event-2");
_bets.ListByOutcomeAsync(BetOutcome.Pending, Arg.Any<CancellationToken>())
.Returns(new[]
{
MakePending(idGraded, side: Side.Side1),
MakePending(idUngraded, side: Side.Side2),
}.ToList().AsReadOnly());
_results.GetAsync(idGraded, Arg.Any<CancellationToken>())
.Returns(new EventResult(idGraded, 2, 1, Side.Side1, DateTimeOffset.UtcNow));
_results.GetAsync(idUngraded, Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
var count = await CreateSut().ExecuteAsync(CancellationToken.None);
count.Should().Be(1, "only event-1 has a result");
await _bets.Received(1).UpdateAsync(
Arg.Is<PlacedBet>(b => b.EventId == idGraded && b.Outcome == BetOutcome.Won),
Arg.Any<CancellationToken>());
await _bets.DidNotReceive().UpdateAsync(
Arg.Is<PlacedBet>(b => b.EventId == idUngraded),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task Should_CacheResultLookups_PerEvent()
{
// Two pending bets on the same event — only one result fetch should fire.
var id = new EventId("event-shared");
_bets.ListByOutcomeAsync(BetOutcome.Pending, Arg.Any<CancellationToken>())
.Returns(new[]
{
MakePending(id, side: Side.Side1),
MakePending(id, side: Side.Side2),
}.ToList().AsReadOnly());
_results.GetAsync(id, Arg.Any<CancellationToken>())
.Returns(new EventResult(id, 2, 1, Side.Side1, DateTimeOffset.UtcNow));
await CreateSut().ExecuteAsync(CancellationToken.None);
await _results.Received(1).GetAsync(id, Arg.Any<CancellationToken>());
}
}