Collegamento e motore: correzioni regole-1, regole-2, regole-6, websec-1, websec-5, websec-6, engine-1, engine-2, sched-1

Pagine aperte da Alberto non sono segnali (regole-1); cookie perso fuori sessione = disconnessione (regole-2); pagina a riposo quando Alberto lascia il browser (regole-6); la lease di «Accedi»/«Usa il browser» finisce da sola dopo 2 minuti senza vista remota né finestra (websec-1); ping con scadenza sulla vista remota (websec-5); aperture della vista remota serializzate (websec-6); orologio virtuale senza fine anticipata (engine-1); la sincronizzazione della coda non duplica il compito in corso (engine-2); raccolte in evidenza programmate (sched-1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
Claude committed 2026-10-14 00:21:13 +00:00
1 parent 78c92eb2a4
commit aff81dfac9
19 files changed
+1122 -92

No files matched your search

+137
View File
@@ -470,4 +470,141 @@ public sealed class EngineHostTests
Assert.AreEqual(CheckNowStatus.Refused, result.Status);
StringAssert.Contains(result.Message, "tetto giornaliero");
}
[TestMethod]
public async Task WhenAlbertoLetsGoOfTheBrowserThePageGoesBackToRestUnlessAVerificationKeepsIt()
{
var driving = false;
await using var bed = await EngineTestBed.CreateAsync(Morning, userIsDriving: () => driving);
await bed.AddAccountAsync("uno.demo", followed: true);
await bed.AddAccountAsync("due.demo", followed: false, Frequency.High);
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
EngineHost? host = null;
int? restsWhenTaken = null;
// He takes the browser in the middle of a recipe (a story viewer may be open): the session ends and leaves the
// page to him.
runner.Before = r =>
{
if (restsWhenTaken is null && r.Kind != RecipeKind.FollowingSweep)
{
restsWhenTaken = runner.Rests;
driving = true;
host!.NotifyUserDrivingChanged();
}
return Task.CompletedTask;
};
host = bed.Start(runner);
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 14, 12, 0));
Assert.IsNotNull(restsWhenTaken, "Alberto took the browser during a session.");
Assert.AreEqual(restsWhenTaken, runner.Rests, "While he drives the page is his.");
// He lets go during a pause: nothing starts, but nothing of what was open stays on the site either (§6.1, §6.3).
await host.PauseAsync();
driving = false;
host.NotifyUserDrivingChanged();
await bed.RunForAsync(TimeSpan.FromSeconds(1));
Assert.AreEqual(restsWhenTaken + 1, runner.Rests, "Back to rest as soon as he lets go.");
// After a verification the page stays as it is until «Riprendi» (§7.6), also when he lets go.
bed.Breaker.Report(new Signal(SignalLevel.Challenge, "challenge-url", "https://sito.prova/challenge", bed.Clock.Now));
await bed.Breaker.FlushAsync();
driving = true;
host.NotifyUserDrivingChanged();
await bed.RunForAsync(TimeSpan.FromMinutes(5));
driving = false;
host.NotifyUserDrivingChanged();
await bed.RunForAsync(TimeSpan.FromMinutes(5));
Assert.AreEqual(restsWhenTaken + 1, runner.Rests, "The verification page is left as it is.");
}
[TestMethod]
public async Task ChangingTheAccountsDuringARecipeDoesNotBringItsNextCheckForward()
{
await using var bed = await EngineTestBed.CreateAsync(Morning);
var watched = await bed.AddAccountAsync("uno.demo", followed: false, Frequency.Low, types: ContentTypes.PostPhotos | ContentTypes.PostVideos);
await bed.AddAccountAsync("due.demo", followed: false, Frequency.Low, types: ContentTypes.PostPhotos | ContentTypes.PostVideos);
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
EngineHost? host = null;
DateTimeOffset? checkedAt = null;
// While the profile check of uno.demo runs (its task is «Running»), Alberto saves the settings of an account.
runner.Before = async r =>
{
if (checkedAt is null && r.Kind == RecipeKind.ProfileCheck && r.AccountId == watched.Id)
{
checkedAt = bed.Clock.Now;
await host!.NotifyAccountsChangedAsync();
}
};
host = bed.Start(runner);
await bed.RunUntilAsync(Morning.AddDays(1));
Assert.IsNotNull(checkedAt);
var visits = runner.Runs
.Where(r => r.Request.Kind == RecipeKind.ProfileCheck && r.Request.AccountId == watched.Id && r.Status == RecipeStatus.Completed)
.Select(r => r.At)
.ToList();
// «Bassa», not followed: about every 12 hours, never sooner than 0.6 × 12 h.
for (var i = 1; i < visits.Count; i++)
{
Assert.IsTrue(visits[i] - visits[i - 1] >= TimeSpan.FromHours(7), $"Visit after {visits[i] - visits[i - 1]}: the interval of the frequency holds.");
}
var pending = (await bed.Db.Tasks.ListPendingAsync(1000))
.Where(t => t.Kind == EngineTaskKind.ProfileCheck && t.AccountId == watched.Id)
.ToList();
Assert.HasCount(1, pending, "One planned check, not a second one from the queue sync.");
Assert.IsTrue(pending[0].DueAt >= visits[^1] + TimeSpan.FromHours(7), $"Next check at {pending[0].DueAt:O}, after {visits[^1]:O}.");
}
[TestMethod]
public async Task HighlightCollectionsAreLookedAtOncePerIntervalAndNoLongerOnceGone()
{
await using var bed = await EngineTestBed.CreateAsync(Morning);
var account = await bed.AddAccountAsync("uno.demo", followed: true, Frequency.High);
await bed.Db.Highlights.UpsertAsync(new Highlight { AccountId = account.Id, InstagramId = "17900000001", Title = "Viaggi", DiscoveredAt = Morning });
var food = await bed.Db.Highlights.UpsertAsync(new Highlight { AccountId = account.Id, InstagramId = "17900000002", Title = "Cucina", Position = 1, DiscoveredAt = Morning });
var noStories = await bed.AddAccountAsync("due.demo", followed: true, Frequency.High, types: ContentTypes.PostPhotos);
await bed.Db.Highlights.UpsertAsync(new Highlight { AccountId = noStories.Id, InstagramId = "17900000003", Title = "Altro", DiscoveredAt = Morning });
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
// Like the real recipe: the collection is marked as looked at.
runner.Before = async r =>
{
if (r.Kind == RecipeKind.Highlight && await bed.Db.Highlights.FindAsync(account.Id, r.HighlightId!) is { } highlight)
{
await bed.Db.Highlights.UpsertAsync(highlight with { LastCheckedAt = bed.Clock.Now });
}
};
bed.Start(runner);
await bed.RunUntilAsync(Morning.AddDays(3));
List<RunRecord> Looks(string id) => [.. runner.Runs.Where(r => r.Request.Kind == RecipeKind.Highlight && r.Request.HighlightId == id)];
foreach (var id in new[] { "17900000001", "17900000002" })
{
var looks = Looks(id);
Assert.IsNotEmpty(looks, $"Collection {id} of an account that wants «Storie in evidenza» is looked at.");
Assert.IsTrue(looks.All(l => l.Request.AccountId == account.Id));
for (var i = 1; i < looks.Count; i++)
{
// «Alta»: every 24 hours or so, never sooner than 0.6 × 24 h.
Assert.IsTrue(looks[i].At - looks[i - 1].At >= TimeSpan.FromHours(14), $"Look after {looks[i].At - looks[i - 1].At}.");
}
}
Assert.IsEmpty(Looks("17900000003"), "«Storie in evidenza» off: not looked at.");
var all = runner.Runs.Where(r => r.Request.Kind == RecipeKind.Highlight).ToList();
Assert.AreEqual(all.Count, all.Select(r => r.SessionId).Distinct().Count(), "One collection per account and session.");
// A collection that left the profile is no longer looked at; the other one still is.
await bed.Db.Highlights.MarkRemovedAsync(food.Id, bed.Clock.Now);
var foodLooks = Looks("17900000002").Count;
var travelLooks = Looks("17900000001").Count;
await bed.RunUntilAsync(Morning.AddDays(6));
Assert.HasCount(foodLooks, Looks("17900000002"));
Assert.IsTrue(Looks("17900000001").Count > travelLooks);
}
}