The scheduler's first jobs finish about a minute after the bot starts, so a run started exactly at the window's start was reported as covering slightly less than 48 hours. A gap shorter than the allowed silence (15 minutes) at either edge now counts as covered, like one in the middle. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
93 lines
6.5 KiB
C#
93 lines
6.5 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 + SoakReporter.DecisionTime));
|
|
Assert.AreEqual(0, SoakReporter.DailyCloses(close, close.AddMinutes(10)), "A decision just started is not expected yet.");
|
|
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);
|
|
|
|
// A quiet run: the scheduler at work from the first minute to the end, the account reconciled and normal at the
|
|
// end (a clean round writes no reconciliation row), a crypto decision at each daily close.
|
|
await rig.Db.WriteAsync(async s =>
|
|
{
|
|
await s.ExecuteAsync("INSERT INTO jobs (id, name, description, last_due_at, last_started_at, last_finished_at, last_outcome, last_error, created_at, updated_at) " +
|
|
"VALUES (@id, 'riconciliazione', 'Riconciliazione dei conti', @to, @to, @to, 'ok', NULL, @from, @to)", CancellationToken.None,
|
|
("id", Guid.NewGuid()), ("from", From.AddMinutes(1)), ("to", To)).ConfigureAwait(false);
|
|
await s.ExecuteAsync("INSERT INTO account_status (id, account_id, mode, mode_since, mode_reason, differences, last_reconciled_at, last_outcome, created_at, updated_at) " +
|
|
"VALUES (@id, @a, 'normal', NULL, NULL, @d, @to, 'clean', @from, @to)", CancellationToken.None,
|
|
("id", Guid.NewGuid()), ("a", okx.AccountId), ("d", new Anubis.Db.DbJson("{\"Current\":[],\"Excluded\":[]}")), ("from", From), ("to", To)).ConfigureAwait(false);
|
|
foreach (var day in new[] { 12, 13 })
|
|
{
|
|
var at = new DateTimeOffset(2026, 11, day, 0, 5, 0, TimeSpan.Zero);
|
|
await 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);
|
|
}
|
|
}).ConfigureAwait(false);
|
|
|
|
var report = await SoakReporter.BuildAsync(rig.Db, From, To, TimeSpan.FromHours(48), Zones.Rome).ConfigureAwait(false);
|
|
var text = report.Markdown(Zones.Rome);
|
|
Assert.IsTrue(report.Passed, text);
|
|
Assert.Contains("**Esito: regolare.**", text);
|
|
Assert.Contains("Durata coperta dal lavoro del bot: 49 ore", text);
|
|
Assert.Contains("Al lavoro dall'inizio alla fine della finestra.", text);
|
|
Assert.Contains("Ogni conto riconciliato fino alla fine e in regola: ", 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);
|
|
|
|
// Off for 35 minutes, a guard intervention, an unexplained difference at the end: each one is named.
|
|
await rig.Db.WriteAsync(async s =>
|
|
{
|
|
var off = From.AddHours(8).AddMinutes(16);
|
|
await s.ExecuteAsync("INSERT INTO absences (id, started_at, ended_at, acknowledged_at, created_at) VALUES (@id, @s, @e, NULL, @e)", CancellationToken.None,
|
|
("id", Guid.NewGuid()), ("s", off), ("e", off.AddMinutes(35))).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);
|
|
await s.ExecuteAsync("INSERT INTO reconciliations (id, account_id, outcome, details, occurred_at, created_at) VALUES (@id, @a, @o, @d, @at, @at)",
|
|
CancellationToken.None, ("id", Guid.NewGuid()), ("a", okx.AccountId), ("o", ReconcileOutcomes.Different), ("d", new Anubis.Db.DbJson("{}")), ("at", To)).ConfigureAwait(false);
|
|
await s.ExecuteAsync("UPDATE account_status SET mode = 'prudent' WHERE account_id = @a", CancellationToken.None, ("a", okx.AccountId)).ConfigureAwait(false);
|
|
}).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.Contains("Spento: dal 12/11/2026 03:16 per 35 minuti.", failed);
|
|
Assert.Contains("Interventi della guardia: 1.", failed);
|
|
Assert.Contains("Riconciliazioni con differenze inspiegate: 1.", failed);
|
|
Assert.IsTrue(failed.Any(f => f.Contains("in modalità prudente", StringComparison.Ordinal)), string.Join(" | ", failed));
|
|
Assert.Contains("**Esito: da guardare.**", report.Markdown(Zones.Rome));
|
|
|
|
// A bot that fell silent before the end of the window: its last job and the account's last round say when.
|
|
var later = await SoakReporter.BuildAsync(rig.Db, From.AddHours(2), To.AddHours(2), TimeSpan.FromHours(48), Zones.Rome).ConfigureAwait(false);
|
|
Assert.IsTrue(later.Checks.Any(c => !c.Ok && c.Text.EndsWith("da allora il bot tace.", StringComparison.Ordinal)), later.Markdown(Zones.Rome));
|
|
Assert.IsTrue(later.Checks.Any(c => !c.Ok && c.Text.StartsWith("Durata coperta dal lavoro del bot: 46,4 ore", StringComparison.Ordinal)), later.Markdown(Zones.Rome));
|
|
}
|
|
}
|