diff --git a/src/Predictalytics.Application.Tests/Predictalytics.Application.Tests.csproj b/src/Predictalytics.Application.Tests/Predictalytics.Application.Tests.csproj index b1b3031..35d2cad 100644 --- a/src/Predictalytics.Application.Tests/Predictalytics.Application.Tests.csproj +++ b/src/Predictalytics.Application.Tests/Predictalytics.Application.Tests.csproj @@ -1,4 +1,4 @@ - + net10.0 @@ -10,6 +10,7 @@ + @@ -24,6 +25,7 @@ + \ No newline at end of file diff --git a/src/Predictalytics.Application.Tests/Services/PositionPnLEngineTests.cs b/src/Predictalytics.Application.Tests/Services/PositionPnLEngineTests.cs index ac6ac76..2e09892 100644 --- a/src/Predictalytics.Application.Tests/Services/PositionPnLEngineTests.cs +++ b/src/Predictalytics.Application.Tests/Services/PositionPnLEngineTests.cs @@ -12,10 +12,10 @@ namespace Predictalytics.Application.Tests.Services; public class PositionPnLEngineTests { - private AppDbContext CreateDbContext() + private AppDbContext CreateDbContext(string? dbName = null) { var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) + .UseInMemoryDatabase(databaseName: dbName ?? Guid.NewGuid().ToString()) .Options; return new AppDbContext(options); } @@ -283,10 +283,375 @@ public class PositionPnLEngineTests var updatedTrader = await db.Traders.Include(t => t.Analytics).FirstOrDefaultAsync(t => t.Id == 1); // PnL7d should be OverallPnL (150) - Snapshot (100) = 50 - Assert.Equal(50m, updatedTrader!.Analytics!.OverallPnL); + Assert.Equal(50m, updatedTrader!.Analytics!.OverallPnL); // Wait, the test above doesn't have initial PnL of 100 on the trader. RecalculateTraderPositionsAsync recalculates from scratch. // It will see 1 winning trade => OverallPnL = 50. // Then PnL7d = OverallPnL (50) - SnapshotPnL (100) = -50. Assert.Equal(-50m, updatedTrader.Analytics.PnL7d); } + + // ═════════════════════════════════════════════════════════════════════════ + // Invariant tests added 2026-07-09 (review round 4). + // Each test pins the REQUIRED behavior for a confirmed, still-open defect. + // They are EXPECTED TO BE RED until the corresponding fix lands. + // Fix the engine — never weaken these assertions to make them pass. + // ═════════════════════════════════════════════════════════════════════════ + + /// + /// Defect 1: The totals (and the virtual-payout pass) iterate only over + /// positions rebuilt from the remaining trades. A position whose trades were + /// removed by the retention worker must still contribute its RealizedPnl to + /// OverallPnL — the position IS the compressed replacement for its history. + /// + [Fact] + public async Task RecalculateTraderPositionsAsync_PositionWithoutRemainingTrades_IsIncludedInOverallPnl() + { + // Arrange + var dbName = Guid.NewGuid().ToString(); + using (var db = CreateDbContext(dbName)) + { + var trader = new Trader { Id = 1, PlatformUserId = "0x1", DisplayName = "Trader 1" }; + + // Outcome 100: closed position, all of its trades pruned by retention. + var market1 = new Market { Id = 10, PlatformMarketId = 1L, Question = "Old market" }; + market1.Outcomes.Add(new MarketOutcome { Id = 100, MarketId = 10, Label = "Yes", TokenId = "t100", CurrentPrice = 0.60m }); + + // Outcome 101: one live trade, so the engine runs its full path. + var market2 = new Market { Id = 11, PlatformMarketId = 2L, Question = "Live market" }; + market2.Outcomes.Add(new MarketOutcome { Id = 101, MarketId = 11, Label = "Yes", TokenId = "t101", CurrentPrice = 0.50m }); + + db.Traders.Add(trader); + db.Markets.AddRange(market1, market2); + + db.TraderPositions.Add(new TraderPosition + { + Id = 1, TraderId = 1, MarketOutcomeId = 100, + SharesHeld = 0, AvgCost = 0, RealizedPnl = 50m, + LastAppliedTradeId = 999, LastTradeExecutedAt = DateTime.UtcNow.AddDays(-40), + IsHistoryPruned = true + }); + + db.Trades.Add(new Trade + { + Id = 1000, TraderId = 1, DbMarketId = 11, MarketOutcomeId = 101, + Side = TradeSide.Buy, Price = 0.50m, Size = 100m, Amount = 50m, + ExecutedAt = DateTime.UtcNow + }); + await db.SaveChangesAsync(); + } + + // Act (fresh context, like the worker does) + using (var db = CreateDbContext(dbName)) + { + var pnlEngine = new PositionPnLEngine(db, NullLogger.Instance); + await pnlEngine.RecalculateTraderPositionsAsync(1); + } + + // Assert: 50 realized (orphaned position) + 0 unrealized (live buy at current price) + using (var db = CreateDbContext(dbName)) + { + var analytics = await db.TraderAnalytics.SingleAsync(a => a.TraderId == 1); + Assert.Equal(50m, analytics.OverallPnL); + + var trader = await db.Traders.SingleAsync(t => t.Id == 1); + Assert.Equal(50m, trader.TotalPnl); + } + } + + /// + /// Defect 1 (virtual-payout variant): A position with open shares in a + /// RESOLVED market must receive its virtual payout even when none of its + /// trades exist anymore. Requires the engine to load positions with their + /// MarketOutcome/Market instead of relying on entities tracked via trades. + /// + [Fact] + public async Task RecalculateTraderPositionsAsync_VirtualPayout_AppliesToPositionWithoutRemainingTrades() + { + // Arrange + var dbName = Guid.NewGuid().ToString(); + using (var db = CreateDbContext(dbName)) + { + var trader = new Trader { Id = 1, PlatformUserId = "0x1", DisplayName = "Trader 1" }; + + var resolvedMarket = new Market + { + Id = 10, PlatformMarketId = 1L, Question = "Resolved market", + IsResolved = true, ResolutionOutcome = "Yes" + }; + resolvedMarket.Outcomes.Add(new MarketOutcome { Id = 100, MarketId = 10, Label = "Yes", TokenId = "t100", CurrentPrice = 0.99m }); + + var liveMarket = new Market { Id = 11, PlatformMarketId = 2L, Question = "Live market" }; + liveMarket.Outcomes.Add(new MarketOutcome { Id = 101, MarketId = 11, Label = "Yes", TokenId = "t101", CurrentPrice = 0.50m }); + + db.Traders.Add(trader); + db.Markets.AddRange(resolvedMarket, liveMarket); + + // Winning position, bought at 0.40, never redeemed, trades pruned. + db.TraderPositions.Add(new TraderPosition + { + Id = 1, TraderId = 1, MarketOutcomeId = 100, + SharesHeld = 100m, AvgCost = 0.40m, RealizedPnl = 0m, + LastAppliedTradeId = 999, LastTradeExecutedAt = DateTime.UtcNow.AddDays(-40), + IsHistoryPruned = true + }); + + db.Trades.Add(new Trade + { + Id = 1000, TraderId = 1, DbMarketId = 11, MarketOutcomeId = 101, + Side = TradeSide.Buy, Price = 0.50m, Size = 10m, Amount = 5m, + ExecutedAt = DateTime.UtcNow + }); + await db.SaveChangesAsync(); + } + + // Act + using (var db = CreateDbContext(dbName)) + { + var pnlEngine = new PositionPnLEngine(db, NullLogger.Instance); + await pnlEngine.RecalculateTraderPositionsAsync(1); + } + + // Assert: virtual payout of 100 × (1.00 − 0.40) = 60 was booked. + using (var db = CreateDbContext(dbName)) + { + var pos = await db.TraderPositions.SingleAsync(p => p.TraderId == 1 && p.MarketOutcomeId == 100); + Assert.Equal(60m, pos.RealizedPnl); + Assert.Equal(0m, pos.SharesHeld); + + var analytics = await db.TraderAnalytics.SingleAsync(a => a.TraderId == 1); + Assert.Equal(60m, analytics.OverallPnL); + } + } + + /// + /// Defect 3: If an external actor (the reconciliation worker) zeroes the + /// checkpoint of a position whose history is pruned, the engine must NOT + /// re-apply the remaining trades on top of the existing position state. + /// A pruned position can never be replayed — the engine has to detect the + /// inconsistent state, keep the stored values and restore a valid checkpoint. + /// This guard must live in the engine even if the reconciliation worker is + /// also fixed to skip pruned positions (defense in depth). + /// + [Fact] + public async Task RecalculateTraderPositionsAsync_PrunedPositionWithResetCheckpoint_DoesNotDoubleCount() + { + // Arrange + var dbName = Guid.NewGuid().ToString(); + using (var db = CreateDbContext(dbName)) + { + var trader = new Trader { Id = 1, PlatformUserId = "0x1", DisplayName = "Trader 1" }; + var market = new Market { Id = 10, PlatformMarketId = 1L, Question = "Q?" }; + market.Outcomes.Add(new MarketOutcome { Id = 100, MarketId = 10, Label = "Yes", TokenId = "t100", CurrentPrice = 0.50m }); + db.Traders.Add(trader); + db.Markets.Add(market); + + // This sell was already applied in an earlier run (its +25 PnL is part + // of RealizedPnl below). Earlier buys were pruned (IsHistoryPruned). + db.Trades.Add(new Trade + { + Id = 10, TraderId = 1, DbMarketId = 10, MarketOutcomeId = 100, + Side = TradeSide.Sell, Price = 0.75m, Size = 100m, Amount = 75m, + ExecutedAt = DateTime.UtcNow.AddDays(-1) + }); + + db.TraderPositions.Add(new TraderPosition + { + Id = 1, TraderId = 1, MarketOutcomeId = 100, + SharesHeld = 100m, AvgCost = 0.50m, RealizedPnl = 25m, + LastAppliedTradeId = 0, // externally reset, e.g. by TradeReconciliationWorker + LastTradeExecutedAt = DateTime.UtcNow.AddDays(-1), + IsHistoryPruned = true + }); + await db.SaveChangesAsync(); + } + + // Act + using (var db = CreateDbContext(dbName)) + { + var pnlEngine = new PositionPnLEngine(db, NullLogger.Instance); + await pnlEngine.RecalculateTraderPositionsAsync(1); + } + + // Assert: values unchanged — the sell must not be booked a second time. + using (var db = CreateDbContext(dbName)) + { + var pos = await db.TraderPositions.SingleAsync(p => p.TraderId == 1 && p.MarketOutcomeId == 100); + Assert.Equal(25m, pos.RealizedPnl); + Assert.Equal(100m, pos.SharesHeld); + + var analytics = await db.TraderAnalytics.SingleAsync(a => a.TraderId == 1); + Assert.Equal(25m, analytics.OverallPnL); // 25 realized + 0 unrealized (price == cost) + } + } + + /// + /// Defect 4: MERGE burns shares and returns cash — it is the mirror image of + /// SPLIT, not a buy. The Polymarket activity API always delivers positive + /// sizes, so branching on the sign of Size sends every merge through the + /// buy branch (shares up, cash out). Booking must branch on TradeSide. + /// + [Fact] + public async Task RecalculateTraderPositionsAsync_MergeTrade_ReducesSharesAndReturnsCash() + { + // Arrange + var dbName = Guid.NewGuid().ToString(); + using (var db = CreateDbContext(dbName)) + { + var trader = new Trader { Id = 1, PlatformUserId = "0x1", DisplayName = "Trader 1" }; + var market = new Market { Id = 10, PlatformMarketId = 1L, Question = "Q?" }; + market.Outcomes.Add(new MarketOutcome { Id = 100, MarketId = 10, Label = "Yes", TokenId = "t100", CurrentPrice = 0.40m }); + db.Traders.Add(trader); + db.Markets.Add(market); + + db.Trades.Add(new Trade + { + Id = 10, TraderId = 1, DbMarketId = 10, MarketOutcomeId = 100, + Side = TradeSide.Buy, Price = 0.40m, Size = 100m, Amount = 40m, + ExecutedAt = DateTime.UtcNow.AddHours(-2) + }); + + // Merge of 100 shares — size is POSITIVE, exactly as the API delivers it. + db.Trades.Add(new Trade + { + Id = 20, TraderId = 1, DbMarketId = 10, MarketOutcomeId = 100, + Side = TradeSide.Merge, Price = 0.40m, Size = 100m, Amount = 40m, + ExecutedAt = DateTime.UtcNow.AddHours(-1) + }); + await db.SaveChangesAsync(); + } + + // Act + using (var db = CreateDbContext(dbName)) + { + var pnlEngine = new PositionPnLEngine(db, NullLogger.Instance); + await pnlEngine.RecalculateTraderPositionsAsync(1); + } + + // Assert: buy −40, merge +40 → flat position, flat cash. + using (var db = CreateDbContext(dbName)) + { + var pos = await db.TraderPositions.SingleAsync(p => p.TraderId == 1 && p.MarketOutcomeId == 100); + Assert.Equal(0m, pos.SharesHeld); + Assert.Equal(0m, pos.RealizedPnl); // merged out at cost basis + + var analytics = await db.TraderAnalytics.SingleAsync(a => a.TraderId == 1); + Assert.Equal(0m, analytics.CurrentBalance); + Assert.Equal(40m, analytics.EstimatedBankroll); // max cash drawdown was the buy + } + } + + /// + /// Defect 6: A checkpoint reset causes the engine to replay trades whose + /// cashflows are already contained in the persisted CurrentBalance. The + /// replay must not book those cashflows a second time. + /// + [Fact] + public async Task RecalculateTraderPositionsAsync_CheckpointResetAndReplay_DoesNotDoubleCountBalance() + { + // Arrange + var dbName = Guid.NewGuid().ToString(); + using (var db = CreateDbContext(dbName)) + { + var trader = new Trader { Id = 1, PlatformUserId = "0x1", DisplayName = "Trader 1" }; + var market = new Market { Id = 10, PlatformMarketId = 1L, Question = "Q?" }; + market.Outcomes.Add(new MarketOutcome { Id = 100, MarketId = 10, Label = "Yes", TokenId = "t100", CurrentPrice = 0.50m }); + db.Traders.Add(trader); + db.Markets.Add(market); + + db.Trades.Add(new Trade + { + Id = 10, TraderId = 1, DbMarketId = 10, MarketOutcomeId = 100, + Side = TradeSide.Buy, Price = 0.50m, Size = 100m, Amount = 50m, + ExecutedAt = DateTime.UtcNow.AddHours(-1) + }); + await db.SaveChangesAsync(); + } + + // First run — applies the buy, balance goes to −50. + using (var db = CreateDbContext(dbName)) + { + var pnlEngine = new PositionPnLEngine(db, NullLogger.Instance); + await pnlEngine.RecalculateTraderPositionsAsync(1); + } + + // Simulate the reconciliation worker resetting the checkpoint + // (it does exactly this whenever it links orphaned trades). + using (var db = CreateDbContext(dbName)) + { + var pos = await db.TraderPositions.SingleAsync(p => p.TraderId == 1 && p.MarketOutcomeId == 100); + pos.LastAppliedTradeId = 0; + await db.SaveChangesAsync(); + } + + // Second run — engine resets the position and replays the same buy. + using (var db = CreateDbContext(dbName)) + { + var pnlEngine = new PositionPnLEngine(db, NullLogger.Instance); + await pnlEngine.RecalculateTraderPositionsAsync(1); + } + + // Assert: balance must equal the single-run result. + using (var db = CreateDbContext(dbName)) + { + var analytics = await db.TraderAnalytics.SingleAsync(a => a.TraderId == 1); + Assert.Equal(-50m, analytics.CurrentBalance); + Assert.Equal(50m, analytics.EstimatedBankroll); + + var pos = await db.TraderPositions.SingleAsync(p => p.TraderId == 1 && p.MarketOutcomeId == 100); + Assert.Equal(100m, pos.SharesHeld); + Assert.Equal(0.50m, pos.AvgCost); + } + } + + /// + /// Defect 5: For a newly discovered trader there is no snapshot older than + /// the window, and the fallback of 0 turns the LIFETIME PnL into the + /// 24h/7d/30d PnL. A trader whose entire activity is older than the window + /// must report 0 for that window, not his all-time PnL. + /// + [Fact] + public async Task RecalculateTraderPositionsAsync_NewlyDiscoveredTraderWithOldHistory_WindowPnlIsNotLifetimePnl() + { + // Arrange: profitable round trip 60/50 days ago, no snapshots (first analysis). + var dbName = Guid.NewGuid().ToString(); + using (var db = CreateDbContext(dbName)) + { + var trader = new Trader { Id = 1, PlatformUserId = "0x1", DisplayName = "Trader 1" }; + var market = new Market { Id = 10, PlatformMarketId = 1L, Question = "Q?" }; + market.Outcomes.Add(new MarketOutcome { Id = 100, MarketId = 10, Label = "Yes", TokenId = "t100", CurrentPrice = 0.70m }); + db.Traders.Add(trader); + db.Markets.Add(market); + + db.Trades.Add(new Trade + { + Id = 10, TraderId = 1, DbMarketId = 10, MarketOutcomeId = 100, + Side = TradeSide.Buy, Price = 0.20m, Size = 100m, Amount = 20m, + ExecutedAt = DateTime.UtcNow.AddDays(-60) + }); + db.Trades.Add(new Trade + { + Id = 20, TraderId = 1, DbMarketId = 10, MarketOutcomeId = 100, + Side = TradeSide.Sell, Price = 0.70m, Size = 100m, Amount = 70m, + ExecutedAt = DateTime.UtcNow.AddDays(-50) + }); + await db.SaveChangesAsync(); + } + + // Act + using (var db = CreateDbContext(dbName)) + { + var pnlEngine = new PositionPnLEngine(db, NullLogger.Instance); + await pnlEngine.RecalculateTraderPositionsAsync(1); + } + + // Assert: lifetime PnL is 50, but no trading happened inside any window. + using (var db = CreateDbContext(dbName)) + { + var analytics = await db.TraderAnalytics.SingleAsync(a => a.TraderId == 1); + Assert.Equal(50m, analytics.OverallPnL); + Assert.Equal(0m, analytics.PnL30d); + Assert.Equal(0m, analytics.PnL7d); + Assert.Equal(0m, analytics.PnL24h); + } + } } diff --git a/src/Predictalytics.Application.Tests/Services/TradeRetentionWorkerTests.cs b/src/Predictalytics.Application.Tests/Services/TradeRetentionWorkerTests.cs new file mode 100644 index 0000000..b6994d3 --- /dev/null +++ b/src/Predictalytics.Application.Tests/Services/TradeRetentionWorkerTests.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging.Abstractions; +using Predictalytics.Domain.Entities; +using Predictalytics.Domain.Enums; +using Predictalytics.Infrastructure.Data; +using Predictalytics.Infrastructure.Services; +using Predictalytics.Worker.Services; +using Xunit; + +namespace Predictalytics.Application.Tests.Services; + +/// +/// Invariant tests for the retention/compaction worker (added 2026-07-09). +/// Uses SQLite in-memory instead of the InMemory provider because the worker +/// relies on ExecuteDeleteAsync/ExecuteUpdateAsync (relational-only). +/// +public class TradeRetentionWorkerTests +{ + /// + /// Defect 2: Compaction bumps the position checkpoint to the aggregate + /// trade's Id — which is the highest Id in the table. Any UNAPPLIED real + /// trade with a smaller Id silently falls below the checkpoint and is never + /// booked (IsHistoryPruned additionally blocks the reset self-heal). + /// + /// Invariant: after compaction plus a PnL engine run, every previously + /// unapplied trade must be reflected in the position. Valid fixes include + /// skipping compaction while unapplied trades exist for the position, or + /// marking the aggregate as pre-applied without moving the checkpoint past + /// unapplied trades. EXPECTED TO BE RED until fixed. + /// + [Fact] + public async Task RunOptimizationAsync_CompactionWithUnappliedTrades_DoesNotLoseThem() + { + // ── Arrange: shared SQLite in-memory database ──────────────────────── + using var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + var baseDate = DateTime.UtcNow.Date; + + using (var setup = new AppDbContext(options)) + { + setup.Database.EnsureCreated(); + + var ev = new Event { Id = 1, Platform = PlatformType.Polymarket, Slug = "e", Title = "E" }; + setup.Set().Add(ev); + + var market = new Market { Id = 10, EventId = 1, PlatformMarketId = 1L, Question = "Q?" }; + market.Outcomes.Add(new MarketOutcome { Id = 100, MarketId = 10, Label = "Yes", TokenId = "t100", CurrentPrice = 0.45m }); + setup.Markets.Add(market); + + // Bot trader → target of the compaction pass. + setup.Traders.Add(new Trader { Id = 1, PlatformUserId = "0x1", DisplayName = "Bot", IsSuspectedBot = true }); + + // Two APPLIED trades, older than the compaction cutoff, same day/side + // → they form a compactable group. + setup.Trades.Add(new Trade + { + Id = 10, TraderId = 1, DbMarketId = 10, MarketOutcomeId = 100, PlatformTradeId = "tx10", + Side = TradeSide.Buy, Price = 0.40m, Size = 50m, Amount = 20m, + ExecutedAt = baseDate.AddDays(-20).AddHours(10) + }); + setup.Trades.Add(new Trade + { + Id = 11, TraderId = 1, DbMarketId = 10, MarketOutcomeId = 100, PlatformTradeId = "tx11", + Side = TradeSide.Buy, Price = 0.40m, Size = 50m, Amount = 20m, + ExecutedAt = baseDate.AddDays(-20).AddHours(11) + }); + + // One UNAPPLIED trade (imported but not yet analyzed): Id 12 is above + // the checkpoint (11) but below the aggregate's future Id. + setup.Trades.Add(new Trade + { + Id = 12, TraderId = 1, DbMarketId = 10, MarketOutcomeId = 100, PlatformTradeId = "tx12", + Side = TradeSide.Buy, Price = 0.50m, Size = 100m, Amount = 50m, + ExecutedAt = baseDate.AddDays(-5).AddHours(10) + }); + + // Position reflects exactly the two applied trades. + setup.TraderPositions.Add(new TraderPosition + { + Id = 1, TraderId = 1, MarketOutcomeId = 100, + SharesHeld = 100m, AvgCost = 0.40m, RealizedPnl = 0m, + LastAppliedTradeId = 11, + LastTradeExecutedAt = baseDate.AddDays(-20).AddHours(11), + IsHistoryPruned = false + }); + + setup.SaveChanges(); + } + + var services = new ServiceCollection(); + services.AddScoped(_ => new AppDbContext(options)); + using var provider = services.BuildServiceProvider(); + + var config = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary + { + ["RetentionSettings:RetentionDays"] = "90", + ["RetentionSettings:CompactionDays"] = "14" + }).Build(); + + var worker = new TradeRetentionWorker(provider, config, NullLogger.Instance); + + // RunOptimizationAsync is private; invoked via reflection on purpose so + // this test exercises the real production code path. Making the method + // internal (+ InternalsVisibleTo) instead of this reflection call is a + // welcome refactor. + var method = typeof(TradeRetentionWorker).GetMethod("RunOptimizationAsync", BindingFlags.Instance | BindingFlags.NonPublic); + Assert.NotNull(method); + + // ── Act 1: retention/compaction pass ───────────────────────────────── + await (Task)method!.Invoke(worker, new object[] { CancellationToken.None })!; + + // ── Act 2: next analytics run ──────────────────────────────────────── + using (var engineCtx = new AppDbContext(options)) + { + var engine = new PositionPnLEngine(engineCtx, NullLogger.Instance); + await engine.RecalculateTraderPositionsAsync(1); + } + + // ── Assert: the unapplied trade (Id 12) must now be part of the position: + // 100 shares @0.40 (compacted or not) + 100 shares @0.50 → 200 @ 0.45. + using (var assertCtx = new AppDbContext(options)) + { + var pos = await assertCtx.TraderPositions.SingleAsync(p => p.TraderId == 1 && p.MarketOutcomeId == 100); + Assert.Equal(200m, pos.SharesHeld); + Assert.Equal(0.45m, pos.AvgCost); + } + } +}