Files
Anubis/tests/Anubis.Tests/SoakReportTests.cs
T
Alby96andClaude Opus 5.5 3d174931e9 Add the endurance run report and record the Phase 6 choices (refs #53 #54)
"anubis soak" reads the bot's records over a time window and says whether
the run kept control. It changes nothing, so it can run beside the working
bot with docker exec. It exits with 4 when something needs a look. It
checks:
- the hours covered and silences longer than 15 minutes;
- orders or decisions made twice;
- unexplained reconciliation differences and each account's last outcome;
- absences and guard interventions;
- one crypto decision per daily close.

ADR 0012 records what the specification leaves open in this phase: the
live engine shares the backtest's decision call, run keys, recovery after
an absence, the guard and its drill seams, the diary templates, the
"senza bot" comparison, Telegram and the heartbeat, the screens and the
endurance run. ROADMAP ticks the Phase 6 items and criteria with the tests
that prove them, STATE moves on to Phase 7, and SVILUPPO explains the
guard and the report.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:36:22 +01:00

86 lines
4.7 KiB
C#

using Anubis.Core;
using Anubis.Data;
using Anubis.Supervision;
using Anubis.Trading;
namespace Anubis.Tests;
/// <summary>The report of the endurance run (SPEC 17, phase 6): regular when the bot kept working, and it says what was not.</summary>
[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<Guid>();
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<Guid> 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;
}
}