using Anubis.Core; using Anubis.Data; using Anubis.Supervision; using Anubis.Trading; namespace Anubis.Tests; /// The report of the endurance run (SPEC 17, phase 6): regular when the bot kept working, and it says what was not. [TestClass] public sealed class SoakReportTests { private static readonly DateTimeOffset From = new(2026, 11, 11, 18, 0, 0, TimeSpan.Zero); private static readonly DateTimeOffset To = From.AddHours(49); [TestMethod] public void TheDailyCryptoClosesAreCountedInTheWindow() { Assert.AreEqual(2, SoakReporter.DailyCloses(From, To)); // 12 and 13 November at 00:05 UTC var close = new DateTimeOffset(2026, 11, 11, 0, 5, 0, TimeSpan.Zero); Assert.AreEqual(1, SoakReporter.DailyCloses(close, close)); Assert.AreEqual(3, SoakReporter.DailyCloses(close, To)); Assert.AreEqual(0, SoakReporter.DailyCloses(From, From.AddHours(5))); } [TestMethod] [TestCategory(Category.Integration)] public async Task AQuietRunIsRegularAndTroublesAreNamed() { await using var rig = await TradingRig.CreateAsync().ConfigureAwait(false); var okx = await rig.AccountAsync("okx", "demo").ConfigureAwait(false); var reconciliations = new List(); for (var at = From.AddMinutes(1); at <= To; at += TimeSpan.FromMinutes(5)) { reconciliations.Add(await ReconciledAsync(rig, okx.AccountId, at, ReconcileOutcomes.Clean).ConfigureAwait(false)); } foreach (var day in new[] { 12, 13 }) { var at = new DateTimeOffset(2026, 11, day, 0, 5, 0, TimeSpan.Zero); await rig.Db.WriteAsync(s => s.ExecuteAsync( "INSERT INTO engine_runs (id, strategy, run_key, decision_day, decision_id, mode, state, proposed, created_at) VALUES (@id, @s, @k, @d, @dec, @m, NULL, NULL, @at)", CancellationToken.None, ("id", Guid.NewGuid()), ("s", StrategyNames.CryptoTrend), ("k", $"{StrategyNames.CryptoTrend}:2026-11-{day - 1}"), ("d", $"2026-11-{day - 1}"), ("dec", Guid.NewGuid()), ("m", DecisionModes.Observation), ("at", at))).ConfigureAwait(false); } var report = await SoakReporter.BuildAsync(rig.Db, From, To, TimeSpan.FromHours(48), Zones.Rome).ConfigureAwait(false); Assert.IsTrue(report.Passed, report.Markdown(Zones.Rome)); var text = report.Markdown(Zones.Rome); Assert.Contains("**Esito: regolare.**", text); Assert.Contains("Decisioni di crypto_trend: 2 su 2 chiusure giornaliere.", text); Assert.Contains("crypto_trend 2 (in osservazione)", text); Assert.Contains("Ordini: nessuno.", text); // Half an hour of silence, an unexplained difference at the end and a guard intervention: each one is named. await rig.Db.WriteAsync(async s => { foreach (var id in reconciliations.Skip(100).Take(6)) { await s.ExecuteAsync("DELETE FROM reconciliations WHERE id = @id", CancellationToken.None, ("id", id)).ConfigureAwait(false); } rig.Clock.Set(From.AddHours(20)); await Diary.WriteAsync(s, rig.Clock, DiaryKinds.System, "guard_intervention", "La guardia è intervenuta.", null, CancellationToken.None).ConfigureAwait(false); }).ConfigureAwait(false); await ReconciledAsync(rig, okx.AccountId, To, ReconcileOutcomes.Different).ConfigureAwait(false); report = await SoakReporter.BuildAsync(rig.Db, From, To, TimeSpan.FromHours(48), Zones.Rome).ConfigureAwait(false); Assert.IsFalse(report.Passed); var failed = report.Checks.Where(c => !c.Ok).Select(c => c.Text).ToList(); Assert.HasCount(4, failed, string.Join(" | ", failed)); Assert.IsTrue(failed.Any(f => f.StartsWith("Silenzi più lunghi di 15 minuti: dal 12/11/2026 03:16 per 35 minuti", StringComparison.Ordinal)), string.Join(" | ", failed)); Assert.Contains("Riconciliazioni con differenze inspiegate: 1.", failed); Assert.Contains("Interventi della guardia: 1.", failed); Assert.IsTrue(failed.Any(f => f.Contains("con differenze", StringComparison.Ordinal))); Assert.Contains("**Esito: da guardare.**", report.Markdown(Zones.Rome)); } private static async Task ReconciledAsync(TradingRig rig, Guid account, DateTimeOffset at, string outcome) { var id = Guid.NewGuid(); await rig.Db.WriteAsync(s => s.ExecuteAsync("INSERT INTO reconciliations (id, account_id, outcome, details, occurred_at, created_at) VALUES (@id, @a, @o, @d, @at, @at)", CancellationToken.None, ("id", id), ("a", account), ("o", outcome), ("d", new Anubis.Db.DbJson("{}")), ("at", at))).ConfigureAwait(false); return id; } }