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:
1 parent
d26d945818
commit
8d7efe425d
8 files changed
+199
-23
No files matched your search
@@ -324,6 +324,16 @@ public sealed partial class DeliveryTests
|
||||
StringAssert.Contains(tasks, "$\"ANUBIS_VERSION={Version()}\"");
|
||||
StringAssert.Contains(tasks, "$\"ANUBIS_COMMIT={Commit()}\"");
|
||||
StringAssert.Contains(tasks, "\"-dirty\"");
|
||||
|
||||
// Only what goes into the image makes it unclean: a file of the user's, or a note, does not.
|
||||
var parts = Regex.Match(tasks, "string\\[\\] imageSources = \\[(.*?)\\];", RegexOptions.Singleline).Groups[1].Value;
|
||||
var copied = Text("deploy/docker/Dockerfile").Split('\n').Where(l => l.StartsWith("COPY ", StringComparison.Ordinal) && !l.Contains("--from", StringComparison.Ordinal))
|
||||
.SelectMany(l => l.Split(' ', StringSplitOptions.RemoveEmptyEntries).Skip(1).SkipLast(1)).Where(p => !p.StartsWith("--", StringComparison.Ordinal)).ToList();
|
||||
Assert.IsNotEmpty(copied);
|
||||
foreach (var path in copied.Append(".dockerignore").Append("deploy/docker/Dockerfile"))
|
||||
{
|
||||
Assert.IsTrue(Regex.Matches(parts, "\"([^\"]+)\"").Any(m => path.TrimEnd('/') == m.Groups[1].Value || path.StartsWith(m.Groups[1].Value + "/", StringComparison.Ordinal)), path);
|
||||
}
|
||||
var build = (string)Steps("image").Single(s => ((string)s["run"]).Contains("docker build", StringComparison.Ordinal))["run"];
|
||||
StringAssert.Contains(build, "version=${REF_NAME#v}");
|
||||
StringAssert.Contains(build, "\"ANUBIS_VERSION=$version\"");
|
||||
@@ -412,6 +422,7 @@ public sealed partial class DeliveryTests
|
||||
[DataRow("v0.9.0", "0.9.0", "fedcba9876543210fedcba9876543210fedcba98", false)]
|
||||
[DataRow("v0.9.0", "0.9.0", "", false)]
|
||||
[DataRow("v0.9.0", "0.9.0", PublishRun.Commit, true)]
|
||||
[DataRow("v0.9.0", "0.9.0", PublishRun.TagObject, false)]
|
||||
public async Task TheScriptRefusesAnImageThatIsNotTheOneOfTheTag(string tag, string version, string revision, bool tagged)
|
||||
{
|
||||
using var run = new PublishRun("v0.7.0", version: version, revision: revision, tagged: tagged);
|
||||
@@ -427,25 +438,48 @@ public sealed partial class DeliveryTests
|
||||
[OSCondition(OperatingSystems.Linux)]
|
||||
[DataRow("v0.9.0", PublishRun.TagCommit, true)]
|
||||
[DataRow("v0.9.0-rc.1", PublishRun.TagCommit, true)]
|
||||
[DataRow("v0.9.0-rc.1", PublishRun.Commit, false)]
|
||||
[DataRow("v0.9.0-rc.1", PublishRun.RemoteCommit, false)]
|
||||
public async Task TheScriptTakesTheImageOfTheTasksAtTheCommitOfTheTag(string tag, string revision, bool tagged)
|
||||
{
|
||||
// The commit of the tag when the tag is here, otherwise the one checked out: in the workflow they are the same.
|
||||
// The commit of the tag when the tag is here, otherwise the one the repository on Gitea has for it.
|
||||
using var run = new PublishRun("v0.7.0", version: "0.9.0", revision: revision, tagged: tagged);
|
||||
var (code, output) = await run.RunAsync(tag, PublishRun.Token);
|
||||
Assert.AreEqual(0, code, output);
|
||||
Assert.IsNotEmpty(run.Pushed());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Integration)]
|
||||
[OSCondition(OperatingSystems.Linux)]
|
||||
public async Task TheScriptRefusesATagThatIsNowhere()
|
||||
{
|
||||
// The commit checked out is not asked: an image of it would be published under a tag nobody made.
|
||||
using var run = new PublishRun("v0.7.0", revision: PublishRun.Commit, pushed: false);
|
||||
var (code, output) = await run.RunAsync("v0.9.0", PublishRun.Token);
|
||||
Assert.AreEqual(1, code, output);
|
||||
StringAssert.Contains(output, "Pubblicazione non riuscita: il tag v0.9.0 non c'è");
|
||||
Assert.IsEmpty(run.Pushed());
|
||||
Assert.AreEqual("", run.Input, "Not even the login.");
|
||||
|
||||
// A light tag names its commit directly.
|
||||
using var light = new PublishRun("v0.7.0", revision: PublishRun.TagObject, light: true);
|
||||
(code, output) = await light.RunAsync("v0.9.0", PublishRun.Token);
|
||||
Assert.AreEqual(0, code, output);
|
||||
}
|
||||
|
||||
private sealed class PublishRun : IDisposable
|
||||
{
|
||||
// The annotated tag on Gitea, and the commit it names.
|
||||
public const string TagObject = "1111111111222222222233333333334444444444";
|
||||
public const string RemoteCommit = "5555555555666666666677777777778888888888";
|
||||
|
||||
public const string Token = "a-token-only-for-the-test";
|
||||
public const string Commit = "0123456789abcdef0123456789abcdef01234567";
|
||||
public const string TagCommit = "89abcdef0123456789abcdef0123456789abcdef";
|
||||
|
||||
private readonly string _root = Path.Combine(Path.GetTempPath(), "anubis-publish-" + Guid.NewGuid().ToString("N"));
|
||||
|
||||
public PublishRun(string others, bool gitFails = false, string? version = null, string revision = Commit, bool tagged = false)
|
||||
public PublishRun(string others, bool gitFails = false, string? version = null, string revision = RemoteCommit, bool tagged = false, bool pushed = true, bool light = false)
|
||||
{
|
||||
Directory.CreateDirectory(Path.Combine(_root, "deploy", "docker"));
|
||||
Directory.CreateDirectory(Path.Combine(_root, "bin"));
|
||||
@@ -455,7 +489,8 @@ public sealed partial class DeliveryTests
|
||||
Tool("docker", "printf '%s\\n' \"$*\" >> \"$RECORD/calls\"\nif [ \"$1\" = login ]; then cat > \"$RECORD/input\"; printf '%s' \"$DOCKER_CONFIG\" > \"$RECORD/config\"; fi\n" +
|
||||
$"case \"$*\" in *image.version*) printf '%s\\n' \"$VERSION\" ;; *image.revision*) printf '%s\\n' '{revision}' ;; esac\n");
|
||||
var tagCommit = tagged ? $"printf '%s\\n' {TagCommit}; exit 0" : "exit 1";
|
||||
Tool("git", $"case \"$*\" in *rev-parse*refs/tags/*) {tagCommit} ;; *rev-parse*HEAD*) printf '%s\\n' {Commit}; exit 0 ;; esac\n" +
|
||||
var remote = !pushed ? "" : $"printf '{TagObject}\\t%s\\n' \"refs/tags/$TAGGED\"; " + (light ? "" : $"printf '{RemoteCommit}\\t%s\\n' \"refs/tags/$TAGGED^{{}}\"; ");
|
||||
Tool("git", $"case \"$*\" in *rev-parse*refs/tags/*) {tagCommit} ;; *rev-parse*HEAD*) printf '%s\\n' {Commit}; exit 0 ;; *ls-remote*\\^{{}}*) {(gitFails ? "exit 128" : remote + "exit 0")} ;; esac\n" +
|
||||
(gitFails ? "exit 128\n" : string.Concat(others.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(o => $"printf 'abc\\trefs/tags/{o}\\n'\n"))));
|
||||
}
|
||||
|
||||
@@ -478,6 +513,7 @@ public sealed partial class DeliveryTests
|
||||
info.ArgumentList.Add(tag);
|
||||
info.Environment["PATH"] = Path.Combine(_root, "bin") + ":" + Environment.GetEnvironmentVariable("PATH");
|
||||
info.Environment["RECORD"] = _root;
|
||||
info.Environment["TAGGED"] = tag;
|
||||
info.Environment["VERSION"] = _version ?? tag[1..];
|
||||
info.Environment["REGISTRY"] = "registry.test";
|
||||
info.Environment["REGISTRY_USER"] = "someone";
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -329,11 +329,40 @@ public sealed class ReconcilerTests
|
||||
Assert.IsFalse(state.WaitsForUser(s.Rig.Clock.UtcNow, s.Reconciler.Grace), "It may still explain itself.");
|
||||
s.Rig.Clock.Advance(TimeSpan.FromMinutes(16));
|
||||
s.Broker.Missing.Clear();
|
||||
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
||||
state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
|
||||
Assert.AreEqual(DifferenceKinds.Stop, state.Differences.Single().Kind);
|
||||
Assert.IsTrue(state.WaitsForUser(s.Rig.Clock.UtcNow, s.Reconciler.Grace), "Gone too late.");
|
||||
Assert.AreNotEqual(Reconciler.StopReason, state.ModeReason);
|
||||
s.Broker.RefuseStops = false;
|
||||
Assert.AreEqual(AccountModes.Prudent, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Mode);
|
||||
Assert.AreEqual(ReconcileResolution.Normal, await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Simulation)]
|
||||
public async Task ADifferenceThatComesAndGoesDoesNotMakeTheStopAwaitedAskAgain()
|
||||
{
|
||||
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));
|
||||
Assert.AreEqual(ReconcileResolution.StopsMissing, await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false));
|
||||
|
||||
// Coin leaves the account and is back 5 minutes later, the stop still refused: the user has nothing to choose.
|
||||
s.Broker.Missing["BTC-EUR"] = 0.004m;
|
||||
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
||||
s.Rig.Clock.Advance(TimeSpan.FromMinutes(5));
|
||||
s.Broker.Missing.Clear();
|
||||
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
||||
Assert.AreEqual(Reconciler.StopReason, (await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false)).ModeReason);
|
||||
s.Rig.Clock.Advance(TimeSpan.FromMinutes(20));
|
||||
s.Broker.RefuseStops = false;
|
||||
Assert.AreEqual(AccountModes.Normal, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Mode);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Simulation)]
|
||||
public async Task AKeyThatMayWithdrawStopsPurchasesUntilItIsReplaced()
|
||||
|
||||
Reference in new issue
Block a user