using Anubis.Core;
using Anubis.Db;
using Anubis.Platform;
namespace Anubis.Tests;
/// Export, import and backups (issue #9, SPEC 5.4).
[TestClass]
public sealed class ArchiveTests
{
private static readonly ManualClock _clock = new(new DateTimeOffset(2026, 10, 9, 6, 21, 0, TimeSpan.Zero));
/// Fills a database with rows of every column kind, including accounting rows and a reversal.
internal static async Task SeedAsync(Database db)
{
var (account, instrument) = await DatabaseTests.SeedAccountAsync(db).ConfigureAwait(false);
await db.WriteAsync(async s =>
{
var fill = Guid.CreateVersion7(_clock.UtcNow.AddSeconds(1));
var reversal = Guid.CreateVersion7(_clock.UtcNow.AddSeconds(2));
await s.ExecuteAsync(
"INSERT INTO fills (id, account_id, instrument_id, execution_id, side, quantity, price, fee, fee_currency, executed_at, created_at) VALUES (@id, @a, @i, 'EX-1', 'buy', @q, @p, @f, 'EUR', @t, @t)",
("id", fill), ("a", account), ("i", instrument), ("q", 0.0182m), ("p", 68950m), ("f", 1.254890000000000001m), ("t", _clock.UtcNow)).ConfigureAwait(false);
await s.ExecuteAsync(
"INSERT INTO fills (id, account_id, instrument_id, execution_id, side, quantity, price, fee, fee_currency, reverses_id, executed_at, created_at) VALUES (@id, @a, @i, 'EX-1-R', 'buy', @q, @p, @f, 'EUR', @r, @t, @t)",
("id", reversal), ("a", account), ("i", instrument), ("q", -0.0182m), ("p", 68950m), ("f", -1.254890000000000001m), ("r", fill), ("t", _clock.UtcNow)).ConfigureAwait(false);
for (var i = 0; i < 25; i++)
{
await s.ExecuteAsync("INSERT INTO cash_ledger (id, account_id, currency, amount, kind, note, occurred_at, created_at) VALUES (@id, @a, 'EUR', @v, 'deposit', @n, @t, @t)",
("id", Guid.CreateVersion7(_clock.UtcNow.AddSeconds(10 + i))), ("a", account), ("v", 100.01m * i - 333.333333333333333333m), ("n", i % 3 == 0 ? null : "nota \"" + i + "\" ;\n"),
("t", _clock.UtcNow.AddDays(-i))).ConfigureAwait(false);
}
await s.ExecuteAsync("INSERT INTO journal (id, occurred_at, kind, code, message, data, created_at) VALUES (@id, @t, 'Ordini', 'stop_raised', @m, @d, @t)",
("id", Guid.CreateVersion7(_clock.UtcNow)), ("t", _clock.UtcNow), ("m", "Alzato lo stop di BTC/EUR da 65.400 € a 66.180 €."), ("d", new DbJson("{\"from\": \"65400\", \"to\": \"66180\"}"))).ConfigureAwait(false);
await s.ExecuteAsync("INSERT INTO users (id, username, password_hash, must_change_password, totp_secret, created_at, updated_at) VALUES (@id, 'admin', 'h', @m, @s, @t, @t)",
("id", Guid.CreateVersion7(_clock.UtcNow)), ("m", true), ("s", new byte[] { 0, 1, 2, 255 }), ("t", _clock.UtcNow)).ConfigureAwait(false);
}).ConfigureAwait(false);
}
[TestMethod]
[TestCategory(Category.Integration)]
public async Task ExportThenImportOnTheSameEngineKeepsEverything()
{
await using var source = await DatabaseTests.MigratedAsync().ConfigureAwait(false);
await using var target = await TestDb.CreateAsync().ConfigureAwait(false);
await RoundTripAsync(source, target).ConfigureAwait(false);
}
[TestMethod]
[TestCategory(Category.Integration)]
public async Task ExportFromOneEngineImportsIntoTheOtherWithSameCountsAndTotals()
{
if (!await TestDb.PostgresAvailableAsync().ConfigureAwait(false))
{
Assert.Inconclusive("PostgreSQL per i test non raggiungibile: la prova tra motori gira in CI e con `tasks ci`.");
}
foreach (var (from, to) in new[] { (DbEngine.Sqlite, DbEngine.Postgres), (DbEngine.Postgres, DbEngine.Sqlite) })
{
await using var source = await TestDb.CreateAsync(from).ConfigureAwait(false);
await Migrations.MigrateAsync(source.Db, _clock, null, CancellationToken.None).ConfigureAwait(false);
await using var target = await TestDb.CreateAsync(to).ConfigureAwait(false);
await RoundTripAsync(source, target).ConfigureAwait(false);
}
}
private static async Task RoundTripAsync(TestDb source, TestDb target)
{
await SeedAsync(source.Db).ConfigureAwait(false);
var folder = TempFolder.Create("archive");
try
{
var index = await Archive.ExportAsync(source.Db, Path.Combine(folder, "a"), _clock).ConfigureAwait(false);
Assert.AreEqual(Migrations.Latest, index.SchemaVersion);
Assert.AreEqual(2L, index.Tables.Single(t => t.Name == "fills").Count);
Assert.AreEqual("0", index.AccountingTotals["fills.quantity"], "il fill e il suo storno si annullano");
await Archive.ImportAsync(target.Db, Path.Combine(folder, "a"), _clock).ConfigureAwait(false);
CollectionAssert.AreEquivalent(
(await Archive.CountsAsync(source.Db).ConfigureAwait(false)).ToList(),
(await Archive.CountsAsync(target.Db).ConfigureAwait(false)).ToList(), $"{source.Db.Engine} → {target.Db.Engine}");
CollectionAssert.AreEquivalent(
(await Archive.AccountingTotalsAsync(source.Db).ConfigureAwait(false)).ToList(),
(await Archive.AccountingTotalsAsync(target.Db).ConfigureAwait(false)).ToList());
// A second export of the target is identical row by row.
await Archive.ExportAsync(target.Db, Path.Combine(folder, "b"), _clock).ConfigureAwait(false);
foreach (var table in Schema.Tables)
{
CollectionAssert.AreEqual(
File.ReadAllLines(Path.Combine(folder, "a", table.Name + ".jsonl")),
File.ReadAllLines(Path.Combine(folder, "b", table.Name + ".jsonl")), table.Name);
}
}
finally
{
TempFolder.Delete(folder);
}
}
[TestMethod]
[TestCategory(Category.Integration)]
public async Task ImportRefusesANonEmptyDatabaseAndATamperedArchive()
{
await using var source = await DatabaseTests.MigratedAsync().ConfigureAwait(false);
await SeedAsync(source.Db).ConfigureAwait(false);
var folder = TempFolder.Create("archive-bad");
try
{
await Archive.ExportAsync(source.Db, Path.Combine(folder, "a"), _clock).ConfigureAwait(false);
await Assert.ThrowsAsync(() => Archive.ImportAsync(source.Db, Path.Combine(folder, "a"), _clock)).ConfigureAwait(false);
var cash = Path.Combine(folder, "a", "cash_ledger.jsonl");
var lines = File.ReadAllLines(cash);
File.WriteAllLines(cash, lines.Skip(1));
await using var target = await TestDb.CreateAsync().ConfigureAwait(false);
await Assert.ThrowsAsync(() => Archive.ImportAsync(target.Db, Path.Combine(folder, "a"), _clock)).ConfigureAwait(false);
Assert.AreEqual(0L, (await Archive.CountsAsync(target.Db).ConfigureAwait(false))["accounts"], "un'importazione fallita non lascia righe");
await Assert.ThrowsAsync(() => Archive.ExportAsync(source.Db, Path.Combine(folder, "a"), _clock)).ConfigureAwait(false);
}
finally
{
TempFolder.Delete(folder);
}
}
[TestMethod]
[TestCategory(Category.Integration)]
public async Task BackupsKeepTheLastFourteenAndCopyTheSqliteFile()
{
await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false);
await SeedAsync(test.Db).ConfigureAwait(false);
var folder = TempFolder.Create("backups");
try
{
var clock = new ManualClock(_clock.UtcNow);
var backups = new Backups(new DataDirectory(folder), clock);
for (var i = 0; i < 16; i++)
{
await backups.TakeAsync(test.Db, i == 0 ? "prima-della-migrazione" : "notturno").ConfigureAwait(false);
clock.Advance(TimeSpan.FromDays(1));
}
var list = backups.List();
Assert.HasCount(Backups.Keep, list);
Assert.AreEqual(clock.UtcNow.AddDays(-1).Date, list[0].TakenAt.UtcDateTime.Date, "la più recente è la prima");
Assert.AreEqual(test.Db.Engine == DbEngine.Sqlite, list[0].HasSqliteCopy);
Assert.IsFalse(Directory.EnumerateDirectories(folder, "*", SearchOption.AllDirectories).Any(d => d.EndsWith(".partial", StringComparison.Ordinal)));
if (test.Db.Engine == DbEngine.Sqlite)
{
// The copy made with the backup API opens and has the same rows.
await using var copy = Database.Open(new DbSettings { Engine = DbEngine.Sqlite, SqlitePath = Path.Combine(list[0].Folder, Backups.SqliteCopy) }, allowCreate: false);
CollectionAssert.AreEquivalent((await Archive.CountsAsync(test.Db).ConfigureAwait(false)).ToList(), (await Archive.CountsAsync(copy).ConfigureAwait(false)).ToList());
}
}
finally
{
TempFolder.Delete(folder);
}
}
[TestMethod]
[TestCategory(Category.Integration)]
public async Task MigratingADatabaseWithDataTakesABackupFirst()
{
await using var test = await TestDb.CreateAsync().ConfigureAwait(false);
var calls = 0;
await Migrations.MigrateAsync(test.Db, _clock, _ =>
{
calls++;
return Task.CompletedTask;
}, CancellationToken.None).ConfigureAwait(false);
Assert.AreEqual(0, calls, "un database vuoto non ha nulla da salvare");
// A failing backup stops the migration (simulated on an older schema by removing the version row).
await test.Db.WriteAsync(s => s.ExecuteAsync($"DELETE FROM {Migrations.VersionTable} WHERE version = @v", ("v", Migrations.Latest))).ConfigureAwait(false);
if (Migrations.Latest > 1)
{
await Assert.ThrowsAsync(() => Migrations.MigrateAsync(test.Db, _clock, _ => throw new IOException("disco pieno"), CancellationToken.None)).ConfigureAwait(false);
}
}
}