Files
maraphon-app/tests/Marathon.Domain.Tests/Entities/PlacedBetTests.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

121 lines
3.6 KiB
C#

using FluentAssertions;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
namespace Marathon.Domain.Tests.Entities;
/// <summary>
/// Invariants and derived-property tests for <see cref="PlacedBet"/>.
/// </summary>
public sealed class PlacedBetTests
{
private static readonly TimeSpan MoscowOffset = TimeSpan.FromHours(3);
private static readonly DateTimeOffset MoscowMoment =
new(2026, 5, 16, 12, 0, 0, MoscowOffset);
private static PlacedBet Make(BetOutcome outcome, decimal rate = 2.10m, decimal stake = 100m) =>
new(
Id: Guid.NewGuid(),
EventId: new EventId("12345678"),
Selection: new Bet(MatchScope.Instance, BetType.Win, Side.Side1,
value: null, new OddsRate(rate)),
Stake: stake,
PlacedAt: MoscowMoment,
Outcome: outcome,
Notes: null);
[Fact]
public void Should_ComputeWonReturn_As_StakeTimesRate()
{
var bet = Make(BetOutcome.Won, rate: 2.10m, stake: 100m);
bet.GrossReturn.Should().Be(210m);
bet.NetProfit.Should().Be(110m);
}
[Fact]
public void Should_ComputeLossReturn_As_Zero()
{
var bet = Make(BetOutcome.Lost, stake: 50m);
bet.GrossReturn.Should().Be(0m);
bet.NetProfit.Should().Be(-50m);
}
[Fact]
public void Should_ReturnStake_When_Outcome_IsVoid()
{
var bet = Make(BetOutcome.Void, stake: 75m);
bet.GrossReturn.Should().Be(75m);
bet.NetProfit.Should().Be(0m);
}
[Fact]
public void Should_ReturnNullProfit_When_OutcomeIsPending()
{
var bet = Make(BetOutcome.Pending);
bet.GrossReturn.Should().BeNull();
bet.NetProfit.Should().BeNull();
}
[Fact]
public void WithOutcome_Should_ReturnNewInstance_With_GradedOutcome()
{
var pending = Make(BetOutcome.Pending);
var graded = pending.WithOutcome(BetOutcome.Won);
graded.Should().NotBeSameAs(pending);
graded.Outcome.Should().Be(BetOutcome.Won);
graded.Id.Should().Be(pending.Id);
graded.Stake.Should().Be(pending.Stake);
}
[Fact]
public void Should_Throw_When_StakeIsZeroOrNegative()
{
var act = () => new PlacedBet(
Id: Guid.NewGuid(),
EventId: new EventId("11111111"),
Selection: new Bet(MatchScope.Instance, BetType.Win, Side.Side1, null, new OddsRate(2m)),
Stake: 0m,
PlacedAt: MoscowMoment,
Outcome: BetOutcome.Pending,
Notes: null);
act.Should().Throw<ArgumentOutOfRangeException>().WithMessage("*Stake*");
}
[Fact]
public void Should_Throw_When_PlacedAt_IsNotMoscowOffset()
{
var act = () => new PlacedBet(
Id: Guid.NewGuid(),
EventId: new EventId("11111111"),
Selection: new Bet(MatchScope.Instance, BetType.Win, Side.Side1, null, new OddsRate(2m)),
Stake: 100m,
PlacedAt: DateTimeOffset.UtcNow, // UTC offset, not Moscow
Outcome: BetOutcome.Pending,
Notes: null);
act.Should().Throw<ArgumentException>().WithMessage("*Moscow*");
}
[Fact]
public void Should_NormaliseWhitespace_Notes_To_Null()
{
var bet = new PlacedBet(
Id: Guid.NewGuid(),
EventId: new EventId("11111111"),
Selection: new Bet(MatchScope.Instance, BetType.Win, Side.Side1, null, new OddsRate(2m)),
Stake: 100m,
PlacedAt: MoscowMoment,
Outcome: BetOutcome.Pending,
Notes: " ");
bet.Notes.Should().BeNull();
}
}