using Anubis.Core;
using Anubis.Data;
using Anubis.Trading;
namespace Anubis.Tests;
///
/// Recovery after an absence (issue #49, SPEC 5.6): the bot is switched off for 3 hours, 20 hours and 3 days with a
/// simulated clock, and the rows of 5.6 whose jobs exist in this phase are checked at the restart.
///
[TestClass]
public sealed class AbsenceTests
{
private static readonly CancellationToken _none = CancellationToken.None;
private static readonly DateOnly LiveFrom = new(2026, 9, 1);
// The bot is switched off the evening before the first live day, at 23:00 UTC.
private static readonly DateTimeOffset Off = new(2026, 8, 31, 23, 0, 0, TimeSpan.Zero);
///
/// Off from 23:00 to 02:00 UTC: the crypto decision of 00:05 is made at the restart and its purchases go if the
/// price is within 2% of the one at 00:05; one that moved more is dropped. Both end in the diary with the reason.
/// The missed backup is taken once; the morning summary is not due yet; no notice on the Panoramica.
///
[TestMethod]
public async Task ThreeHoursOffRecoverTheCryptoDecisionAndDropAnEntryThatMovedTooFar()
{
await using var live = await LiveRig.CreateAsync(LiveFrom, LiveFrom.AddDays(39)).ConfigureAwait(false);
var jobs = await RestartAsync(live.Rig, Off.AddHours(3)).ConfigureAwait(false);
Assert.AreEqual("Sono stato fermo per 3 ore. Ho ripreso i lavori saltati con le regole del recupero.", await LastDiaryAsync(live.Rig, "absence").ConfigureAwait(false));
Assert.IsNull(await new AbsenceBook(live.Rig.Db, live.Rig.Clock).DeclaredAsync(_none).ConfigureAwait(false));
Assert.AreEqual(1, jobs.Backups);
Assert.AreEqual(0, jobs.Summaries);
Assert.AreEqual(0, jobs.Plain, "A job without recovery rules is not run for the time it missed.");
// The data step brings yesterday's bar; SOL has moved 3% from the price of 00:05, ETH has not.
await live.StoreBarAsync(LiveFrom.AddDays(-1)).ConfigureAwait(false);
live.Quotes(LiveFrom, b => b.Open);
var sol = (decimal)live.Bar("SOL-EUR", LiveFrom).Open * 1.03m;
live.Rig.Quote("SOL-EUR", sol * 0.9999m, sol * 1.0001m, 1000m);
await live.MinutesAsync(10).ConfigureAwait(false);
var runs = await live.Rig.QueryAsync("SELECT run_key, mode FROM engine_runs", r => (r.Text("run_key"), r.Text("mode"))).ConfigureAwait(false);
CollectionAssert.AreEqual(new[] { ("crypto_trend:2026-08-31", DecisionModes.Full) }, runs.ToList());
var fills = await live.LiveFillsAsync().ConfigureAwait(false);
Assert.IsTrue(fills.Any(f => f.Symbol == "ETH-EUR" && f.Side == OrderSide.Buy), "The recovered purchase goes.");
Assert.IsFalse(fills.Any(f => f.Symbol == "SOL-EUR"), "The purchase whose price moved 3% is dropped.");
var diary = await DiaryAsync(live.Rig).ConfigureAwait(false);
Assert.Contains("Recuperato un lavoro saltato: la chiusura giornaliera crypto (previsto alle 02:05).", diary);
Assert.Contains("Recuperato un lavoro saltato: l'acquisto di ETH/EUR (previsto alle 02:05).", diary);
Assert.IsTrue(diary.Any(d => d.StartsWith("Scartato l'acquisto di SOL/EUR: il prezzo si è mosso del 3", StringComparison.Ordinal)), string.Join("\n", diary));
var planned = await live.Rig.QueryAsync("SELECT outcome FROM planned_orders WHERE status = @s", r => r.Text("outcome"), ("s", PlannedStates.Dropped)).ConfigureAwait(false);
CollectionAssert.AreEqual(new[] { LiveReasons.PriceMoved }, planned.ToList());
}
///
/// Off for 20 hours, back at 19:00 UTC: the crypto decision is past 12:00 and only exits and stops are updated;
/// the meanrev purchase decided at the last close expired an hour after the open; the morning summary is
/// skipped (past 12:00 in Italy) and said so in the diary; the backup is taken once; no notice on the Panoramica.
///
[TestMethod]
public async Task TwentyHoursOffLetTheLateDecisionsUpdateOnlyExitsAndDropExpiredEntries()
{
await using (var live = await LiveRig.CreateAsync(LiveFrom, LiveFrom.AddDays(39)).ConfigureAwait(false))
{
var jobs = await RestartAsync(live.Rig, Off.AddHours(20)).ConfigureAwait(false);
Assert.AreEqual(1, jobs.Backups);
Assert.AreEqual(0, jobs.Summaries);
Assert.HasCount(1, jobs.Skipped);
Assert.AreEqual("Saltato: riepilogo del mattino (previsto alle 08:00). Il riavvio è arrivato oltre l'orario utile.",
AbsenceBook.MissedLine(jobs.Skipped[0], Zones.Rome));
Assert.IsNull(await new AbsenceBook(live.Rig.Db, live.Rig.Clock).DeclaredAsync(_none).ConfigureAwait(false));
// The engine pulses before the data step has brought yesterday's bar: the late decision waits for it and
// asks for it, instead of being written off as "no data" at the first pulse.
live.Quotes(LiveFrom, b => b.Close);
await live.MinutesAsync(3).ConfigureAwait(false);
Assert.IsEmpty(await live.Rig.QueryAsync("SELECT run_key FROM engine_runs", r => r.Text("run_key")).ConfigureAwait(false), "A late decision waits for its bars.");
await live.StoreBarAsync(LiveFrom.AddDays(-1)).ConfigureAwait(false);
await live.MinutesAsync(10).ConfigureAwait(false);
var runs = await live.Rig.QueryAsync("SELECT mode FROM engine_runs", r => r.Text("mode")).ConfigureAwait(false);
CollectionAssert.AreEqual(new[] { DecisionModes.ExitsOnly }, runs.ToList());
Assert.IsEmpty(await live.LiveFillsAsync().ConfigureAwait(false), "No purchase after 12:00 UTC: the next close decides.");
var decision = await LastDiaryAsync(live.Rig, "decision").ConfigureAwait(false);
Assert.Contains(ReasonCatalog.Text(LiveReasons.LateExitsOnly), decision!);
}
// The stock side: the bot goes off after the signals of the close and comes back the next day at 12:30 in New
// York, past the hour after the open in which a meanrev purchase is valid.
await using var stocks = await StockRig.CreateAsync(LiveFrom, LiveFrom.AddDays(41)).ConfigureAwait(false);
await stocks.SessionAsync(LiveFrom).ConfigureAwait(false);
var pending = await stocks.Rig.QueryAsync("SELECT id FROM planned_orders WHERE status = @s", r => r.Id("id"), ("s", PlannedStates.Pending)).ConfigureAwait(false);
Assert.HasCount(1, pending, "The close of the first day plans a purchase for the next open.");
stocks.Rig.Clock.Set(new DateTimeOffset(2026, 9, 2, 16, 30, 0, TimeSpan.Zero));
stocks.Quotes(LiveFrom.AddDays(1), b => b.Open);
await stocks.MinutesAsync(3).ConfigureAwait(false);
var expired = await stocks.Rig.QueryAsync("SELECT outcome FROM planned_orders WHERE status = @s", r => r.Text("outcome"), ("s", PlannedStates.Expired)).ConfigureAwait(false);
CollectionAssert.AreEqual(new[] { LiveReasons.EntryExpired }, expired.ToList());
Assert.IsEmpty(await stocks.LiveFillsAsync().ConfigureAwait(false));
Assert.IsTrue((await DiaryAsync(stocks.Rig).ConfigureAwait(false)).Any(d => d.StartsWith("Saltato: l'acquisto di BBB. Ingresso scaduto", StringComparison.Ordinal)));
}
///
/// Off for 3 days: only the decision of the restart day is made (the missed ones are not replayed), the backup is
/// taken once and not three times, and the Panoramica declares the absence until the user has seen it.
///
[TestMethod]
public async Task ThreeDaysOffAreDeclaredUntilSeenAndMissedRunsAreMadeOnce()
{
await using var live = await LiveRig.CreateAsync(LiveFrom, LiveFrom.AddDays(39)).ConfigureAwait(false);
var back = Off.AddDays(3);
var jobs = await RestartAsync(live.Rig, back).ConfigureAwait(false);
Assert.AreEqual(1, jobs.Backups, "Three nights missed, one backup at the restart.");
Assert.AreEqual("Sono stato fermo per 3 giorni. Ho ripreso i lavori saltati con le regole del recupero.", await LastDiaryAsync(live.Rig, "absence").ConfigureAwait(false));
var book = new AbsenceBook(live.Rig.Db, live.Rig.Clock);
var declared = await book.DeclaredAsync(_none).ConfigureAwait(false);
Assert.IsNotNull(declared);
Assert.AreEqual(TimeSpan.FromDays(3), declared.Length);
await book.AcknowledgeAsync(declared.Id, _none).ConfigureAwait(false);
Assert.IsNull(await book.DeclaredAsync(_none).ConfigureAwait(false));
for (var day = LiveFrom.AddDays(-1); day <= LiveFrom.AddDays(2); day = day.AddDays(1))
{
await live.StoreBarAsync(day).ConfigureAwait(false);
}
live.Quotes(LiveFrom.AddDays(2), b => b.Close);
await live.MinutesAsync(5).ConfigureAwait(false);
var runs = await live.Rig.QueryAsync("SELECT run_key FROM engine_runs", r => r.Text("run_key")).ConfigureAwait(false);
CollectionAssert.AreEqual(new[] { "crypto_trend:2026-09-02" }, runs.ToList());
}
private sealed class Counts
{
public int Backups { get; set; }
public int Summaries { get; set; }
public int Plain { get; set; }
public List Skipped { get; } = [];
}
///
/// The restart: the last sign of life was at ; the clock is now . The
/// absence is recorded, then the scheduler starts with three jobs that ran last at : the nightly
/// backup (made once whenever the restart comes), the morning summary (only until 12:00 in Italy) and a job without
/// recovery rules.
///
private static async Task RestartAsync(TradingRig rig, DateTimeOffset back)
{
await rig.Db.WriteAsync(s => s.ExecuteAsync(
"INSERT INTO jobs (id, name, description, last_due_at, last_started_at, last_finished_at, last_outcome, created_at, updated_at) VALUES (@id, 'motore', 'Motore', @t, @t, @t, 'completed', @t, @t)",
_none, ("id", Ids.NewId(rig.Clock)), ("t", Off)), _none).ConfigureAwait(false);
rig.Clock.Set(back);
await new AbsenceBook(rig.Db, rig.Clock).RecordAsync(_none).ConfigureAwait(false);
var counts = new Counts();
var bus = new EventBus();
using var runs = bus.Subscribe();
var scheduler = new Scheduler(rig.Clock, bus);
scheduler.Add(new ScheduledJob("backup", "Salvataggio del database", Schedule.Daily(new TimeOnly(3, 30), Zones.Rome), _ =>
{
counts.Backups++;
return Task.CompletedTask;
})
{ MaxLateness = TimeSpan.FromHours(2), CatchUpWithin = TimeSpan.FromDays(3650) }, Off);
scheduler.Add(new ScheduledJob("riepilogo", "Riepilogo del mattino", Schedule.Daily(new TimeOnly(8, 0), Zones.Rome), _ =>
{
counts.Summaries++;
return Task.CompletedTask;
})
{ CatchUpWithin = TimeSpan.FromHours(4) }, Off);
scheduler.Add(new ScheduledJob("pulizia", "Pulizia", Schedule.Daily(new TimeOnly(1, 0), Zones.Rome), _ =>
{
counts.Plain++;
return Task.CompletedTask;
}), Off);
await scheduler.RunDueAsync(_none).ConfigureAwait(false);
await scheduler.RunDueAsync(_none).ConfigureAwait(false);
while (runs.TryRead(out var run))
{
if (run!.Missed && run.Outcome == JobOutcome.Skipped)
{
counts.Skipped.Add(run);
}
else if (run.Missed)
{
Assert.AreEqual(JobOutcome.Completed, run.Outcome);
StringAssert.StartsWith(AbsenceBook.MissedLine(run, Zones.Rome), "Recuperato un lavoro saltato: salvataggio del database (previsto alle 03:30)");
}
}
return counts;
}
private static async Task> DiaryAsync(TradingRig rig) =>
[.. await rig.QueryAsync("SELECT message FROM journal ORDER BY occurred_at, id", r => r.Text("message")).ConfigureAwait(false)];
private static async Task LastDiaryAsync(TradingRig rig, string code) =>
(await rig.QueryAsync("SELECT message FROM journal WHERE code = @c ORDER BY occurred_at DESC, id DESC", r => r.Text("message"), ("c", code)).ConfigureAwait(false)) is { Count: > 0 } rows ? rows[0] : null;
}