Settle what the checks of the review fixes found

A difference that comes and goes no longer makes the account ask about a stop
the user has already chosen on. The balance of the restrictions adds each
period to what was published, so a change of capital does not rewrite the
past. An image is published only under a tag that exists, and a file that is
not part of it no longer marks it as unclean.

refs #70 #74 #75

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-01 23:16:14 +01:00
1 parent d26d945818
commit 8d7efe425d
8 files changed
+199 -23

No files matched your search

+80 -1
View File
@@ -69,6 +69,9 @@ public sealed class QuarterlyReviewTests
public Exception? Failure { get; set; }
/// <summary>The program is stopped while the history is prepared.</summary>
public CancellationTokenSource? StopWhilePreparing { get; set; }
public List<DateOnly> Prepared { get; } = [];
public Database Db => Live.Rig.Db;
@@ -101,6 +104,8 @@ public sealed class QuarterlyReviewTests
Review = new QuarterlyReview(Db, Clock, Lab, Live.Stages, Live.Learning, Folder, (to, _) =>
{
Prepared.Add(to);
StopWhilePreparing?.Cancel();
StopWhilePreparing?.Token.ThrowIfCancellationRequested();
return Failure is { } failure ? Task.FromException<string?>(failure) : Task.FromResult(Missing);
});
Lab.ReviewTrialEnded = Review.ConcludeAsync;
@@ -246,8 +251,11 @@ public sealed class QuarterlyReviewTests
await rig.NightAsync(At(2027, 1, 6));
rig.Clock.Set(At(2027, 1, 17));
using var stop = new CancellationTokenSource();
await stop.CancelAsync();
rig.StopWhilePreparing = stop;
var prepared = rig.Prepared.Count;
await Assert.ThrowsAsync<OperationCanceledException>(() => rig.Review.RunIfDueAsync(stop.Token));
rig.StopWhilePreparing = null;
Assert.HasCount(prepared + 1, rig.Prepared, "Stopped while preparing, not before.");
Assert.AreEqual(ReviewStatuses.Preparing, (await QuarterlyReview.ListAsync(rig.Db, _none)).Single().Status);
rig.Failure = new InvalidOperationException("broken archive at C:\\x");
@@ -768,6 +776,77 @@ public sealed class QuarterlyReviewTests
Assert.AreEqual(Line(two, CorrectiveReasons.CalendarEvent), Line(ever, CorrectiveReasons.CalendarEvent));
}
/// <summary>What was said of a period stays: later blocks, and trades, of the same example do not move it.</summary>
[TestMethod]
[TestCategory(Category.Unit)]
public void WhatWasSettledStaysUnderTheRowOfThen()
{
var first = _t0.AddDays(3);
var never = Case("AAA", null, null, null) with { LabelledAt = _t0.AddDays(2) };
var delayed = Case("BBB", false, -0.1, 2) with { LabelledAt = _t0.AddDays(2.5) };
IReadOnlyList<BlockedEntry> before = [Block(never, NewsReasons.Negative), Block(delayed, NewsReasons.Negative)];
IReadOnlyList<BlockedEntry> after = [.. before, Block(never, NewsReasons.Severe, minutes: 5 * 24 * 60)];
RestrictionReport Of(IReadOnlyList<BlockedEntry> blocks, IReadOnlyList<HeldTrade> trades, DateTimeOffset from, DateTimeOffset until) =>
RestrictionBalance.Of(blocks, [never, delayed], trades, _capital, from, until);
var one = Of(before, [], DateTimeOffset.UnixEpoch, first);
Assert.AreEqual(new RestrictionLine(NewsReasons.Negative, 1, 80m, 0m, 0, 1, 0, 0), one.Lines.Single());
HeldTrade[] bought = [new(Crypto, "AAA", _t0.AddDays(8), null), new(Crypto, "BBB", _t0.AddDays(8), null)];
Assert.IsTrue(Of(after, bought, first, _t0.AddDays(30)).IsEmpty);
Assert.AreEqual(one.Lines.Single(), Of(after, bought, DateTimeOffset.UnixEpoch, _t0.AddDays(30)).Lines.Single());
}
[TestMethod]
[TestCategory(Category.Unit)]
public void TheWholeIsWhatWasPublishedPlusThePeriod()
{
var before = new RestrictionReport([new(NewsReasons.Severe, 2, 80m, 20m, 3, 1, 2, 4), new(CorrectiveReasons.CalendarEvent, 0, 0m, 0m, 2, 0, 0, 0)]);
var period = new RestrictionReport([new(NewsReasons.Severe, 1, 0m, 5m, 1, 2, 3, 1), new(NewsReasons.Negative, 1, 7m, 0m, 0, 0, 0, 0)]);
var ever = RestrictionBalance.Add(before, period);
CollectionAssert.AreEqual(new RestrictionLine[] { new(NewsReasons.Severe, 3, 80m, 25m, 1, 3, 5, 5), new(NewsReasons.Negative, 1, 7m, 0m, 0, 0, 0, 0) }, ever.Lines.ToList(),
"What is open is what is open now.");
CollectionAssert.AreEqual(period.Lines.ToList(), RestrictionBalance.Add(null, period).Lines.ToList());
}
/// <summary>Capital and prices change between two reviews: what the first one published is not counted again at the new ones.</summary>
[TestMethod]
[TestCategory(Category.Integration)]
public async Task TheWholeDoesNotMoveWithTheCapitalOfToday()
{
await using var rig = await ReviewRig.CreateAsync();
async Task StoppedAsync(string symbol, DateTimeOffset at)
{
rig.Clock.Set(at);
var decision = Guid.NewGuid();
await rig.Db.WriteAsync(s => DecisionLog.WriteAsync(s, rig.Clock, new DecisionRow(decision, Crypto, null, symbol, DecisionStages.Order, DecisionOutcomes.Refused, [NewsReasons.Severe],
new Dictionary<string, object?>(), at), _none), _none);
var example = new Example(Guid.NewGuid(), Crypto, symbol, CaseSources.Live, DateOnly.FromDateTime(at.UtcDateTime).AddDays(-1), at, "trend_entry", 0.2m, null, null, new Dictionary<string, double>())
{
Stage = StrategyStages.Simulation,
DecisionId = decision,
};
await rig.Db.WriteAsync(s => LearningStore.AddAsync(s, rig.Clock, example, _none), _none);
rig.Clock.Set(at.AddDays(5));
await rig.Db.WriteAsync(s => LearningStore.AddLabelAsync(s, rig.Clock, example.Id, new CaseLabel(false, -0.1, example.Day.AddDays(1), example.Day.AddDays(4), "x"), _none), _none);
}
await StoppedAsync("ETH-EUR", new DateTimeOffset(2026, 10, 1, 10, 0, 0, TimeSpan.Zero));
var one = (await rig.ReviewAsync(passed: false)).Report!;
Assert.IsGreaterThan(0m, one.RestrictionsEver.Avoided);
Assert.AreEqual(one.Restrictions.Avoided, one.RestrictionsEver.Avoided);
await StoppedAsync("SOL-EUR", rig.Clock.UtcNow.AddDays(20));
await rig.Db.WriteAsync(s => s.ExecuteAsync("UPDATE strategy_state SET capital = 12345", _none), _none);
await rig.NightAsync(At(2027, 4, 5));
var second = (await QuarterlyReview.ListAsync(rig.Db, _none))[0];
await rig.EndAsync(second, passed: false);
await rig.Review.ConcludeAsync(_none);
var two = (await QuarterlyReview.ListAsync(rig.Db, _none)).Single(r => r.Id == second.Id).Report!;
Assert.IsGreaterThan(0m, two.Restrictions.Avoided);
Assert.AreNotEqual(one.Restrictions.Avoided, two.Restrictions.Avoided);
Assert.AreEqual(one.RestrictionsEver.Avoided + two.Restrictions.Avoided, two.RestrictionsEver.Avoided);
Assert.AreEqual(2, two.RestrictionsEver.Lines.Single().Stopped);
}
[TestMethod]
[TestCategory(Category.Integration)]
public async Task TheFirstReviewCountsTheRestrictionsFromTheStart()