Files
maraphon-app/tests/Marathon.Application.Tests/Betting/ClosingLineValueCalculatorTests.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

97 lines
3.5 KiB
C#

using FluentAssertions;
using Marathon.Application.Betting;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
namespace Marathon.Application.Tests.Betting;
/// <summary>
/// Unit tests for <see cref="ClosingLineValueCalculator"/> covering the math
/// itself and the snapshot-matching path used by the report use case.
/// </summary>
public sealed class ClosingLineValueCalculatorTests
{
private static readonly EventId EventId = new("11111111");
[Fact]
public void Compute_Should_ReturnPositive_When_TakenRate_BeatsClose()
{
// Taken 2.20 (implied 0.4545); closed 2.00 (implied 0.5000) → CLV = +0.0455
var clv = ClosingLineValueCalculator.Compute(takenRate: 2.20m, closingRate: 2.00m);
clv.Should().BeGreaterThan(0m);
clv.Should().BeApproximately(0.04545m, 0.00001m);
}
[Fact]
public void Compute_Should_ReturnNegative_When_TakenRate_WorseThanClose()
{
// Taken 1.80 (0.5556); closed 2.00 (0.5000) → CLV = -0.0556
var clv = ClosingLineValueCalculator.Compute(takenRate: 1.80m, closingRate: 2.00m);
clv.Should().BeLessThan(0m);
clv.Should().BeApproximately(-0.05556m, 0.00001m);
}
[Fact]
public void Compute_Should_ReturnZero_When_RatesMatch()
{
ClosingLineValueCalculator.Compute(2.00m, 2.00m).Should().Be(0m);
}
[Theory]
[InlineData(0)]
[InlineData(-1)]
public void Compute_Should_Throw_When_AnyRateIsZeroOrNegative(decimal rate)
{
((Action)(() => ClosingLineValueCalculator.Compute(rate, 2m)))
.Should().Throw<ArgumentOutOfRangeException>();
((Action)(() => ClosingLineValueCalculator.Compute(2m, rate)))
.Should().Throw<ArgumentOutOfRangeException>();
}
[Fact]
public void TryCompute_Should_ReturnNull_When_NoMatchingBetInSnapshot()
{
var taken = new Bet(MatchScope.Instance, BetType.Win, Side.Side1,
value: null, new OddsRate(2.20m));
// Snapshot contains only Side2 — no match for Side1 Win.
var snapshot = new OddsSnapshot(EventId, DateTimeOffset.UtcNow, OddsSource.PreMatch,
new[] { new Bet(MatchScope.Instance, BetType.Win, Side.Side2, null, new OddsRate(1.70m)) });
var clv = ClosingLineValueCalculator.TryCompute(2.20m, taken, snapshot);
clv.Should().BeNull();
}
[Fact]
public void TryCompute_Should_ReturnNull_When_SnapshotIsNull()
{
var taken = new Bet(MatchScope.Instance, BetType.Win, Side.Side1, null, new OddsRate(2m));
ClosingLineValueCalculator.TryCompute(2m, taken, closingSnapshot: null).Should().BeNull();
}
[Fact]
public void TryCompute_Should_MatchOnScopeTypeSideAndValue()
{
// Two handicap markets with different thresholds — pick the right one.
var taken = new Bet(MatchScope.Instance, BetType.WinFora, Side.Side1,
new OddsValue(-1.5m), new OddsRate(2.20m));
var snapshot = new OddsSnapshot(EventId, DateTimeOffset.UtcNow, OddsSource.PreMatch,
new[]
{
new Bet(MatchScope.Instance, BetType.WinFora, Side.Side1,
new OddsValue(-2.5m), new OddsRate(3.50m)), // wrong threshold
new Bet(MatchScope.Instance, BetType.WinFora, Side.Side1,
new OddsValue(-1.5m), new OddsRate(2.00m)), // match
});
var clv = ClosingLineValueCalculator.TryCompute(2.20m, taken, snapshot);
clv.Should().BeApproximately(0.04545m, 0.00001m);
}
}