Write the manuals and fix what following them found

The manual, the runbook and the go-live list are written and checked claim by
claim against the code, and the installation is done from zero by following
them on both engines. That found real defects, now fixed:

- with SQLite a missing database file left no way to restore a backup: the
  choices appear, and the file is created only by the user's choice;
- an account whose difference vanished after the 15 minutes stayed prudent
  with no button, a missing stop could be left out for good, and a choice
  made while the broker was silent used old numbers;
- after "Ferma tutto" the positions could not be closed without restarting;
- a refused key looked like a silent broker: it has its own notice now;
- the split of the capital between the strategies of a broker (SPEC 13.5)
  could not be changed from the interface.

refs #72 #68

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-01 21:38:33 +01:00
1 parent 2257c3e16c
commit 24e20584d4
28 files changed
+1147 -57

No files matched your search

+72 -1
View File
@@ -127,9 +127,18 @@ public sealed class ReconcilerTests
// otherwise copy numbers that are no longer true. The choice compares again before it acts.
s.Broker.Missing.Clear();
Assert.AreEqual(AccountModes.Prudent, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Mode);
Assert.IsEmpty((await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false)).Differences);
state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
Assert.IsEmpty(state.Differences);
Assert.IsTrue(state.WaitsForUser(s.Rig.Clock.UtcNow, s.Reconciler.Grace), "Also with nothing left to align: otherwise it would stay prudent forever.");
Assert.IsFalse(state.HasChoices);
s.Broker.Missing["BTC-EUR"] = 0.004m;
// With the broker silent nothing is compared, so the choice changes nothing.
s.Broker.Down = true;
Assert.AreEqual(ReconcileResolution.NotCompared, await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false));
Assert.AreEqual(AccountModes.Prudent, (await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false)).Mode);
s.Broker.Down = false;
// Align: the ledger takes the account's number with an adjustment, and the account is normal again.
await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false);
Assert.AreEqual(0.00598m, (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, StrategyNames.CryptoTrend, _none).ConfigureAwait(false))!.Quantity);
@@ -192,6 +201,33 @@ public sealed class ReconcilerTests
Assert.AreEqual(AccountModes.Prudent, report.Mode);
}
[TestMethod]
[TestCategory(Category.Simulation)]
public async Task AMissingStopIsNeverLeftOutByTheUsersChoice()
{
await using var s = await HoldingAsync().ConfigureAwait(false);
var stop = (await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false)).Single();
await s.Paper.CancelOrderAsync(stop.ClientOrderId, stop.Symbol, _none).ConfigureAwait(false);
s.Broker.RefuseStops = true;
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
s.Rig.Clock.Advance(TimeSpan.FromMinutes(16));
var report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
Assert.IsTrue(report.Differences.Any(d => d.Kind == DifferenceKinds.Stop), string.Join("; ", report.Differences.Select(d => d.Message)));
foreach (var choice in new[] { ReconcileChoice.Align, ReconcileChoice.Exclude })
{
Assert.AreEqual(ReconcileResolution.StopsMissing, await s.Reconciler.ResolveAsync(s.Manager, choice, "test", _none).ConfigureAwait(false));
var state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
Assert.AreEqual((AccountModes.Prudent, DifferenceKinds.Stop), (state.Mode, state.ModeReason));
Assert.IsFalse(state.Excluded.Any(e => e.StartsWith(DifferenceKinds.Stop, StringComparison.Ordinal)), string.Join("; ", state.Excluded));
}
// The stop is back within the new 15 minutes: normal by itself.
s.Broker.RefuseStops = false;
s.Rig.Clock.Advance(TimeSpan.FromMinutes(5));
report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
Assert.AreEqual(AccountModes.Normal, report.Mode, string.Join("; ", report.Differences.Select(d => d.Message)));
}
[TestMethod]
[TestCategory(Category.Simulation)]
public async Task AKeyThatMayWithdrawStopsPurchasesUntilItIsReplaced()
@@ -230,4 +266,39 @@ public sealed class ReconcilerTests
Assert.HasCount(1, await CodesAsync(s.Rig, "reconcile_unreachable").ConfigureAwait(false));
Assert.AreEqual(AccountModes.Normal, (await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false)).Mode, "Silence is not a difference.");
}
[TestMethod]
[TestCategory(Category.Simulation)]
public async Task AKeyTheBrokerNoLongerAcceptsIsSaidToTheUserOnce()
{
await using var s = await HoldingAsync().ConfigureAwait(false);
s.Broker.Down = true;
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
s.Broker.KeyRefused = true;
// At the start the key is read first, and the account may never get to its reconciliation.
Assert.IsNull(await s.Reconciler.CheckKeyAsync(s.Manager, _none).ConfigureAwait(false));
Assert.AreEqual(ReconcileOutcomes.KeyRefused, (await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false)).LastOutcome);
Assert.AreEqual(ReconcileOutcomes.KeyRefused, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Outcome);
Assert.HasCount(1, await CodesAsync(s.Rig, "key_refused").ConfigureAwait(false));
var sent = await s.Rig.Db.ReadAsync(r => r.CountAsync("SELECT COUNT(*) FROM notifications WHERE message LIKE @b", ("b", "%non accetta più la chiave%")), _none).ConfigureAwait(false);
Assert.AreEqual(1, sent, "It reaches the phone too.");
s.Broker.Down = false;
Assert.AreEqual(ReconcileOutcomes.Clean, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Outcome);
}
[TestMethod]
[TestCategory(Category.Unit)]
[DataRow(401, """{"code":"50111","msg":"Invalid OK-ACCESS-KEY","data":[]}""", true)]
[DataRow(401, """{"code":"50110","msg":"Your IP is not included in your API key's IP whitelist.","data":[]}""", true)]
[DataRow(401, """{"code":"50102","msg":"Timestamp request expired","data":[]}""", false)]
[DataRow(403, """{"message":"forbidden."}""", true)]
[DataRow(404, """{"message":"position does not exist"}""", false)]
public void ARefusedKeyIsToldFromOtherAnswers(int status, string body, bool refused)
{
using var client = new HttpClient();
var http = new TradingHttp(client, new SystemClock(), "broker", (_, _) => true);
Assert.AreEqual(refused, http.Failure(new Uri("https://broker.example/v2/positions"), new HttpAnswer((System.Net.HttpStatusCode)status, body)).KeyRefused);
}
}