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>
This commit is contained in:
2026-05-16 17:45:42 +03:00
parent 292223174c
commit 1ad896b07e
36 changed files with 3315 additions and 0 deletions
@@ -0,0 +1,127 @@
using FluentAssertions;
using Marathon.Domain.Betting;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
namespace Marathon.Domain.Tests.Betting;
/// <summary>
/// Unit tests for <see cref="BetOutcomeResolver"/> across every bet type +
/// every important boundary (handicap push, total push, period-scope null).
/// </summary>
public sealed class BetOutcomeResolverTests
{
private static readonly EventId EventId = new("12345678");
// ── Win bets ─────────────────────────────────────────────────────────────
[Theory]
[InlineData(Side.Side1, Side.Side1, BetOutcome.Won)]
[InlineData(Side.Side1, Side.Side2, BetOutcome.Lost)]
[InlineData(Side.Side1, Side.Draw, BetOutcome.Lost)]
[InlineData(Side.Side2, Side.Side2, BetOutcome.Won)]
[InlineData(Side.Side2, Side.Side1, BetOutcome.Lost)]
public void Should_GradeWinBet(Side selectionSide, Side winner, BetOutcome expected)
{
var bet = MakeBet(BetType.Win, selectionSide);
var result = MakeResult(winner);
BetOutcomeResolver.Resolve(bet, result).Should().Be(expected);
}
// ── Draw bets ────────────────────────────────────────────────────────────
[Theory]
[InlineData(Side.Draw, BetOutcome.Won)]
[InlineData(Side.Side1, BetOutcome.Lost)]
[InlineData(Side.Side2, BetOutcome.Lost)]
public void Should_GradeDrawBet(Side winner, BetOutcome expected)
{
var bet = MakeBet(BetType.Draw, Side.Draw);
var result = MakeResult(winner, 1, 1);
BetOutcomeResolver.Resolve(bet, result).Should().Be(expected);
}
// ── Handicap (WinFora) ───────────────────────────────────────────────────
[Theory]
// Side1 with +1.5 handicap; final 0-2 → adjusted 1.5 vs 2 → loss
[InlineData(Side.Side1, 1.5, 0, 2, BetOutcome.Lost)]
// Side1 with +1.5; final 1-2 → adjusted 2.5 vs 2 → win
[InlineData(Side.Side1, 1.5, 1, 2, BetOutcome.Won)]
// Side1 with -1.5; final 3-1 → adjusted 1.5 vs 1 → win
[InlineData(Side.Side1, -1.5, 3, 1, BetOutcome.Won)]
// Whole-number handicap that ties: Side1 +1, final 1-2 → 2 vs 2 → push (Void)
[InlineData(Side.Side1, 1, 1, 2, BetOutcome.Void)]
// Side2 with -1; final 0-2 → adjusted Side2 1 vs Side1 0 → win
[InlineData(Side.Side2, -1, 0, 2, BetOutcome.Won)]
public void Should_GradeHandicapBet(
Side side, double handicap, int s1, int s2, BetOutcome expected)
{
var bet = new Bet(MatchScope.Instance, BetType.WinFora, side,
new OddsValue((decimal)handicap), new OddsRate(1.85m));
var result = new EventResult(EventId, s1, s2,
DeriveWinner(s1, s2), DateTimeOffset.UtcNow);
BetOutcomeResolver.Resolve(bet, result).Should().Be(expected);
}
// ── Totals ───────────────────────────────────────────────────────────────
[Theory]
// Over 2.5; total 3 → win
[InlineData(Side.More, 2.5, 1, 2, BetOutcome.Won)]
// Over 2.5; total 2 → loss
[InlineData(Side.More, 2.5, 0, 2, BetOutcome.Lost)]
// Over 3.0; total 3 → push (Void)
[InlineData(Side.More, 3.0, 1, 2, BetOutcome.Void)]
// Under 2.5; total 2 → win
[InlineData(Side.Less, 2.5, 1, 1, BetOutcome.Won)]
// Under 2.5; total 3 → loss
[InlineData(Side.Less, 2.5, 1, 2, BetOutcome.Lost)]
public void Should_GradeTotalBet(
Side side, double threshold, int s1, int s2, BetOutcome expected)
{
var bet = new Bet(MatchScope.Instance, BetType.Total, side,
new OddsValue((decimal)threshold), new OddsRate(1.85m));
var result = new EventResult(EventId, s1, s2,
DeriveWinner(s1, s2), DateTimeOffset.UtcNow);
BetOutcomeResolver.Resolve(bet, result).Should().Be(expected);
}
// ── Period-scope guard ───────────────────────────────────────────────────
[Fact]
public void Should_ReturnNull_When_BetScopeIsPeriod()
{
var bet = new Bet(new PeriodScope(1), BetType.Win, Side.Side1,
value: null, new OddsRate(2.10m));
var result = MakeResult(Side.Side1);
BetOutcomeResolver.Resolve(bet, result).Should().BeNull(
"period scope cannot be graded from full-time score alone");
}
[Fact]
public void Should_Throw_When_BetOrResultIsNull()
{
((Action)(() => BetOutcomeResolver.Resolve(null!, MakeResult(Side.Side1))))
.Should().Throw<ArgumentNullException>();
((Action)(() => BetOutcomeResolver.Resolve(MakeBet(BetType.Win, Side.Side1), null!)))
.Should().Throw<ArgumentNullException>();
}
// ── Helpers ──────────────────────────────────────────────────────────────
private static Bet MakeBet(BetType type, Side side) =>
new(MatchScope.Instance, type, side, value: null, new OddsRate(2.00m));
private static EventResult MakeResult(Side winner, int s1 = 1, int s2 = 0) =>
new(EventId, s1, s2, winner, DateTimeOffset.UtcNow);
private static Side DeriveWinner(int s1, int s2) =>
s1 == s2 ? Side.Draw : (s1 > s2 ? Side.Side1 : Side.Side2);
}
@@ -0,0 +1,120 @@
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();
}
}