using Febe.Core.Abstractions;
using Febe.Core.Collection.Recipes;
using Febe.Core.Domain;
using Febe.Core.Engine;
using Febe.Core.Storage;
using Febe.SitoDiProva;
using Febe.Tests.Collection.Support;
using Febe.Tests.Support;
namespace Febe.Tests.Collection;
///
/// Every recipe of spec §6.3 against the local test site, with a real Chromium on a private Xvfb display. Never
/// Instagram. Time is compressed (a 2.2 s wait lasts about 0.2 s); the browser is shared and the tests run one at a time.
///
[TestClass]
[TestCategory(SyntheticSite.Category)]
[DoNotParallelize]
public sealed class RecipeIntegrationTests
{
private static SharedChromium? s_chromium;
private static string? s_unavailable;
public TestContext TestContext { get; set; } = null!;
private static SharedChromium Chromium => s_chromium ?? throw new InvalidOperationException("Browser non avviato.");
[ClassInitialize]
public static async Task StartAsync(TestContext context)
{
s_unavailable = SharedChromium.UnavailableReason();
if (s_unavailable is null)
{
s_chromium = await SharedChromium.StartAsync();
}
}
[ClassCleanup]
public static async Task StopAsync()
{
if (s_chromium is not null)
{
await s_chromium.DisposeAsync();
s_chromium = null;
}
}
[TestInitialize]
public void RequireBrowser()
{
if (s_unavailable is not null)
{
Assert.Inconclusive(s_unavailable);
}
}
[TestMethod]
[Timeout(60_000, CooperativeCancellation = true)]
public async Task ProfileCheckReadsTheFirstPageSkipsPinnedPostsAndStopsAtKnownContent()
{
await using var h = await CollectionHarness.CreateAsync(Chromium);
var marta = await h.AddAccountAsync("marta.prova");
var first = await h.RunAsync(RecipeKind.ProfileCheck, marta.Id);
Assert.AreEqual(RecipeStatus.Completed, first.Status, first.Message);
Assert.AreEqual(1, first.CountedActions, "One profile opened.");
Assert.AreEqual(1, h.Governor.Granted(ActionKind.ProfileOpen));
var posts = await h.Db.Entries.ListByAccountAsync(marta.Id, new EntryListOptions { Kind = EntryKind.Post });
Assert.AreEqual(12, posts.Count, "The first page of the grid: 2 pinned and 10 recent posts.");
Assert.AreEqual(1, (await h.Db.Entries.ListByAccountAsync(marta.Id, new EntryListOptions { Kind = EntryKind.ProfilePicture })).Count);
Assert.AreEqual(13, first.NewEntries);
var highlights = await h.Db.Highlights.ListAsync(marta.Id);
CollectionAssert.AreEqual(new[] { "Estate", "Viaggi" }, highlights.Select(x => x.Title).ToArray());
var account = await h.Db.Accounts.GetAsync(marta.Id);
Assert.AreEqual("c12", account!.BackfillCursor, "The backfill resumes after the first page.");
Assert.AreEqual(18, account.PostCount);
Assert.IsTrue(first.AccountsWithStories.Contains(marta.Id), "Marta has active stories.");
// Largest image version, best video with audio, every file of the series.
var series = posts.First(p => p.Code is not null && h.Site.World.FindPost(p.Code)!.Files.Count == 3);
var files = await h.Db.Media.ListByEntryAsync(series.Id);
Assert.AreEqual(3, files.Count);
Assert.AreEqual(960, files[0].Width);
Assert.AreEqual(MediaKind.Video, files[1].Kind);
Assert.AreEqual(720, files[1].Width, "The 1080 rendition has no audio: not chosen.");
StringAssert.DoesNotMatch(files[1].SourceUrl!, new System.Text.RegularExpressions.Regex("muto"));
// A new post on top; pinned posts are known but do not stop the check before reaching it.
lock (h.Site.World.Gate)
{
h.Site.World.Publish(h.Site.World.FindByUsername("marta.prova")!, "foto", DateTimeOffset.UtcNow);
}
var second = await h.RunAsync(RecipeKind.ProfileCheck, marta.Id);
Assert.AreEqual(RecipeStatus.Completed, second.Status, second.Message);
Assert.AreEqual(1, second.NewEntries, "Only the new post.");
Assert.AreEqual(13, (await h.Db.Entries.ListByAccountAsync(marta.Id, new EntryListOptions { Kind = EntryKind.Post })).Count);
// A new profile picture: a second copy, named by the first eight hex digits of its SHA-256 once downloaded.
lock (h.Site.World.Gate)
{
h.Site.World.FindByUsername("marta.prova")!.PictureVersion++;
}
var third = await h.RunAsync(RecipeKind.ProfileCheck, marta.Id);
Assert.AreEqual(1, third.NewEntries, third.Message);
Assert.AreEqual(2, (await h.Db.Entries.ListByAccountAsync(marta.Id, new EntryListOptions { Kind = EntryKind.ProfilePicture })).Count);
}
[TestMethod]
[Timeout(60_000, CooperativeCancellation = true)]
public async Task UnreachableProfilesAreRecognizedAndNotTouched()
{
await using var h = await CollectionHarness.CreateAsync(Chromium);
var hidden = await h.AddAccountAsync("riservato.prova", withId: false);
var missing = await h.Db.Accounts.AddAsync(Account.Create("nessuno.prova", StartingPoint.EverythingAndKeepUpdated, h.Clock.Now, h.Clock.LocalZone));
var privateOutcome = await h.RunAsync(RecipeKind.ProfileCheck, hidden.Id);
Assert.AreEqual(RecipeStatus.AccountUnreachable, privateOutcome.Status, privateOutcome.Message);
var stored = await h.Db.Accounts.GetAsync(hidden.Id);
Assert.AreEqual(AccountState.Unreachable, stored!.State);
Assert.AreEqual(UnreachableReason.PrivateNotFollowed, stored.UnreachableReason);
Assert.AreEqual("1006", stored.InstagramId, "The identifier is learnt from the data.");
Assert.AreEqual(0, (await h.Db.Entries.ListByAccountAsync(hidden.Id)).Count);
var missingOutcome = await h.RunAsync(RecipeKind.ProfileCheck, missing.Id);
Assert.AreEqual(RecipeStatus.AccountUnreachable, missingOutcome.Status, missingOutcome.Message);
Assert.AreEqual(UnreachableReason.NotFound, (await h.Db.Accounts.GetAsync(missing.Id))!.UnreachableReason);
}
[TestMethod]
[Timeout(90_000, CooperativeCancellation = true)]
public async Task FollowingSweepSavesRecentPostsFlagsStoriesAndStopsAtKnownContent()
{
await using var h = await CollectionHarness.CreateAsync(Chromium);
var marta = await h.AddAccountAsync("marta.prova");
var nadia = await h.AddAccountAsync("nadia.prova");
var luca = await h.AddAccountAsync("luca.prova");
var first = await h.RunAsync(RecipeKind.FollowingSweep);
Assert.AreEqual(RecipeStatus.Completed, first.Status, first.Message);
Assert.AreEqual(1, first.CountedActions, "Only the home is counted.");
Assert.AreEqual(1, h.Governor.Granted(ActionKind.Home));
Assert.IsTrue(first.NewEntries > 0);
CollectionAssert.IsSubsetOf(new[] { marta.Id, nadia.Id, luca.Id }, first.AccountsWithStories.ToArray(), "The stories bar flags followed accounts with active stories.");
Assert.IsTrue(first.AccountsWithNewPosts.Contains(marta.Id));
var mine = await h.Db.Accounts.GetMineAsync();
Assert.AreEqual("alberto.prova", mine?.Username, "The connected account is learnt from the data (H10).");
// Series arrive incomplete in the home: saved with what came, completed later by «Post singolo».
var tasks = await h.Db.Tasks.ListPendingAsync(100);
Assert.IsTrue(tasks.Any(t => t.Kind == EngineTaskKind.SinglePost), "An incomplete series queues «Post singolo».");
var second = await h.RunAsync(RecipeKind.FollowingSweep);
Assert.AreEqual(RecipeStatus.Completed, second.Status, second.Message);
Assert.AreEqual(0, second.NewEntries, "Known content at the top: nothing new.");
Assert.IsTrue(h.Site.Control.DataRequests.Count(r => r.StartsWith("/dati/inizio", StringComparison.Ordinal)) >= 2);
}
[TestMethod]
[Timeout(90_000, CooperativeCancellation = true)]
public async Task SinglePostCompletesASeriesThatArrivedIncomplete()
{
await using var h = await CollectionHarness.CreateAsync(Chromium);
await h.AddAccountAsync("marta.prova");
await h.AddAccountAsync("luca.prova");
await h.AddAccountAsync("giulia.prova");
Assert.AreEqual(RecipeStatus.Completed, (await h.RunAsync(RecipeKind.FollowingSweep)).Status);
var task = (await h.Db.Tasks.ListPendingAsync(100)).First(t => t.Kind == EngineTaskKind.SinglePost);
var entryId = long.Parse(task.Payload!, System.Globalization.CultureInfo.InvariantCulture);
Assert.AreEqual(1, (await h.Db.Media.ListByEntryAsync(entryId)).Count);
var outcome = await h.RunAsync(RecipeKind.SinglePost, task.AccountId, entryId);
Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message);
Assert.AreEqual(1, h.Governor.Granted(ActionKind.SinglePost));
var files = await h.Db.Media.ListByEntryAsync(entryId);
Assert.AreEqual(3, files.Count, "The whole series.");
Assert.IsTrue(files.All(f => f.SourceUrl is not null && f.State == MediaState.ToDownload));
}
[TestMethod]
[Timeout(90_000, CooperativeCancellation = true)]
public async Task StoriesAreWatchedOneByOneSavedOnlyWhenShownAndTheViewerIsClosed()
{
await using var h = await CollectionHarness.CreateAsync(Chromium);
var marta = await h.AddAccountAsync("marta.prova");
var outcome = await h.RunAsync(RecipeKind.AccountStories, marta.Id);
Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message);
var stories = await h.Db.Entries.ListByAccountAsync(marta.Id, new EntryListOptions { Kind = EntryKind.Story });
Assert.AreEqual(4, stories.Count);
Assert.AreEqual(4, h.Governor.Granted(ActionKind.StoryView), "One permit per story shown.");
Assert.AreEqual(1, h.Governor.Granted(ActionKind.ProfileOpen));
Assert.IsTrue(stories.All(s => s.ExpiresAt is not null));
Assert.AreEqual("/profilo/marta.prova/", await h.CurrentPathAsync(), "The viewer was closed before it moved on to the next account.");
Assert.IsFalse(h.Site.Control.DataRequests.Any(r => r.StartsWith("/dati/storie/nadia", StringComparison.Ordinal)), "The next account was never opened.");
Assert.IsNotNull((await h.Db.Accounts.GetAsync(marta.Id))!.LastStoriesCheckAt);
}
[TestMethod]
[Timeout(90_000, CooperativeCancellation = true)]
public async Task ARefusedStoryPermitStopsInOrderAndSavesOnlyTheStoriesShown()
{
await using var h = await CollectionHarness.CreateAsync(Chromium);
var giulia = await h.AddAccountAsync("giulia.prova");
h.Governor.ReportedRemaining = 50;
h.Governor.Limit(ActionKind.StoryView, 2);
// The budget view says 2 left: fewer than ten, the stories are not even opened.
var refused = await h.RunAsync(RecipeKind.AccountStories, giulia.Id);
Assert.AreEqual(RecipeStatus.Denied, refused.Status, refused.Message);
Assert.AreEqual(0, h.Governor.Granted(ActionKind.StoryView));
// With a budget view that allows more, the Governor still refuses after two: stop in order.
await using var h2 = await CollectionHarness.CreateAsync(Chromium);
var g2 = await h2.AddAccountAsync("giulia.prova");
var governor = new LimitedButOptimisticGovernor(h2.Governor, ActionKind.StoryView, 2);
var runner = new RecipeRunner(h2.Runner.Env with { Governor = governor, Budget = null });
var outcome = await runner.RunAsync(new RecipeRequest(RecipeKind.AccountStories, g2.Id), h2.Session, CancellationToken.None);
Assert.AreEqual(RecipeStatus.Denied, outcome.Status, outcome.Message);
Assert.AreEqual(2, (await h2.Db.Entries.ListByAccountAsync(g2.Id, new EntryListOptions { Kind = EntryKind.Story })).Count, "Two shown, two saved: never the whole list received.");
StringAssert.StartsWith(await h2.CurrentPathAsync(), "/profilo/giulia.prova/", "The viewer is closed when the recipe stops.");
}
[TestMethod]
[Timeout(90_000, CooperativeCancellation = true)]
public async Task AStoryOfAnotherAccountIsNotSavedAndTheViewerIsClosed()
{
await using var h = await CollectionHarness.CreateAsync(Chromium, factor: 4);
var nadia = await h.AddAccountAsync("nadia.prova");
await h.AddAccountAsync("luca.prova");
// The viewer moves on by itself faster than the recipe watches: it reaches the next account (Luca).
h.Site.Control.StoryDuration = TimeSpan.FromMilliseconds(300);
var outcome = await h.RunAsync(RecipeKind.AccountStories, nadia.Id);
Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message);
var luca = await h.Db.Accounts.FindByUsernameAsync("luca.prova");
Assert.AreEqual(0, (await h.Db.Entries.ListByAccountAsync(luca!.Id, new EntryListOptions { Kind = EntryKind.Story })).Count, "Stories of another account are never saved.");
Assert.IsTrue((await h.Db.Entries.ListByAccountAsync(nadia.Id, new EntryListOptions { Kind = EntryKind.Story })).Count <= 3);
var events = await h.Db.Events.ListAsync(new EventQuery());
Assert.IsTrue(events.Any(e => e.Code == "visualizzatore-altro-account"), "The event is recorded.");
Assert.IsFalse((await h.CurrentPathAsync()).StartsWith("/storie/", StringComparison.Ordinal), "The viewer is closed.");
}
[TestMethod]
[Timeout(90_000, CooperativeCancellation = true)]
public async Task StoriesOpenFromTheStoriesBarWhenTheHomeIsOnScreen()
{
await using var h = await CollectionHarness.CreateAsync(Chromium);
var nadia = await h.AddAccountAsync("nadia.prova");
Assert.AreEqual(RecipeStatus.Completed, (await h.RunAsync(RecipeKind.FollowingSweep)).Status);
var opens = h.Governor.Granted(ActionKind.ProfileOpen);
var outcome = await h.RunAsync(RecipeKind.AccountStories, nadia.Id);
Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message);
Assert.AreEqual(opens, h.Governor.Granted(ActionKind.ProfileOpen), "Opened from the bar: no profile page.");
Assert.AreEqual(3, (await h.Db.Entries.ListByAccountAsync(nadia.Id, new EntryListOptions { Kind = EntryKind.Story })).Count);
Assert.AreEqual("/", await h.CurrentPathAsync(), "Back on the home, viewer closed.");
}
[TestMethod]
[Timeout(90_000, CooperativeCancellation = true)]
public async Task AHighlightReferencesAStoryAlreadySavedInsteadOfDownloadingItAgain()
{
await using var h = await CollectionHarness.CreateAsync(Chromium);
var marta = await h.AddAccountAsync("marta.prova");
Assert.AreEqual(RecipeStatus.Completed, (await h.RunAsync(RecipeKind.AccountStories, marta.Id)).Status);
string summerId;
string sharedStory;
lock (h.Site.World.Gate)
{
var summer = h.Site.World.FindByUsername("marta.prova")!.Highlights[0];
summerId = summer.Id;
sharedStory = summer.Items[2].Id;
}
var outcome = await h.RunAsync(RecipeKind.Highlight, marta.Id, highlightId: summerId);
Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message);
Assert.AreEqual(2, outcome.NewEntries, "Two new contents; the third is a story already saved (H9).");
var highlight = await h.Db.Highlights.FindAsync(marta.Id, summerId);
var items = await h.Db.Highlights.ListItemsAsync(highlight!.Id);
Assert.AreEqual(3, items.Count);
var linked = await h.Db.Entries.GetAsync(items[2].EntryId);
Assert.AreEqual(EntryKind.Story, linked!.Kind);
Assert.AreEqual(sharedStory, linked.InstagramId);
Assert.IsNull(await h.Db.Entries.FindAsync(marta.Id, EntryKind.Highlight, sharedStory));
StringAssert.StartsWith(await h.CurrentPathAsync(), "/profilo/marta.prova/", "Viewer closed after the last content.");
}
[TestMethod]
[Timeout(90_000, CooperativeCancellation = true)]
public async Task BackfillTakesOnePagePerPermitAndResumesFromTheStoredPoint()
{
await using var h = await CollectionHarness.CreateAsync(Chromium);
var luca = await h.AddAccountAsync("luca.prova");
var first = await h.RunAsync(RecipeKind.Backfill, luca.Id, maxPages: 1);
Assert.AreEqual(RecipeStatus.Completed, first.Status, first.Message);
Assert.IsFalse(first.BackfillFinished);
Assert.AreEqual(1, h.Governor.Granted(ActionKind.ProfileOpen));
Assert.AreEqual(1, h.Governor.Granted(ActionKind.BackfillPage), "The profile page, then one more page for one permit.");
Assert.AreEqual(24, (await h.Db.Entries.ListByAccountAsync(luca.Id, new EntryListOptions { Kind = EntryKind.Post })).Count);
Assert.AreEqual("c24", (await h.Db.Accounts.GetAsync(luca.Id))!.BackfillCursor);
h.Site.Control.ClearRequests();
var second = await h.RunAsync(RecipeKind.Backfill, luca.Id, maxPages: 3);
Assert.AreEqual(RecipeStatus.Completed, second.Status, second.Message);
Assert.IsTrue(second.BackfillFinished, "The beginning of the profile was reached.");
Assert.IsTrue(h.Site.Control.DataRequests[0].Contains("dopo=c24", StringComparison.Ordinal), "Resumed from the stored point: " + h.Site.Control.DataRequests[0]);
var posts = await h.Db.Entries.ListByAccountAsync(luca.Id, new EntryListOptions { Kind = EntryKind.Post });
Assert.AreEqual(40, posts.Count);
Assert.AreEqual(40, posts.Select(p => p.InstagramId).Distinct().Count());
var account = await h.Db.Accounts.GetAsync(luca.Id);
Assert.IsNotNull(account!.BackfillCompletedAt);
Assert.IsNull(account.BackfillCursor);
var third = await h.RunAsync(RecipeKind.Backfill, luca.Id);
Assert.IsTrue(third.BackfillFinished);
Assert.AreEqual(0, third.CountedActions, "A finished backfill does not touch the site.");
}
[TestMethod]
[Timeout(60_000, CooperativeCancellation = true)]
public async Task BackfillStopsWhileTooManyFilesWaitForDownload()
{
await using var h = await CollectionHarness.CreateAsync(Chromium, new RecipeOptions { MaxPendingDownloadsForBackfill = 5 });
var luca = await h.AddAccountAsync("luca.prova");
var outcome = await h.RunAsync(RecipeKind.Backfill, luca.Id, maxPages: 3);
Assert.AreEqual(RecipeStatus.Denied, outcome.Status, outcome.Message);
StringAssert.Contains(outcome.Message, "aspettano di essere scaricati");
Assert.AreEqual(12, (await h.Db.Entries.ListByAccountAsync(luca.Id, new EntryListOptions { Kind = EntryKind.Post })).Count, "Stopped after the first page.");
Assert.AreEqual(0, h.Governor.Granted(ActionKind.BackfillPage));
Assert.AreEqual("c12", (await h.Db.Accounts.GetAsync(luca.Id))!.BackfillCursor, "The resume point is kept.");
}
[TestMethod]
[Timeout(90_000, CooperativeCancellation = true)]
public async Task TaggedAndReelsTabsWorkLikeTheGrid()
{
await using var h = await CollectionHarness.CreateAsync(Chromium);
var marta = await h.AddAccountAsync("marta.prova", a => a with { ContentTypes = ContentTypes.All });
var tagged = await h.RunAsync(RecipeKind.Tagged, marta.Id);
Assert.AreEqual(RecipeStatus.Completed, tagged.Status, tagged.Message);
Assert.IsTrue(tagged.BackfillFinished);
var entries = await h.Db.Entries.ListByAccountAsync(marta.Id, new EntryListOptions { Kind = EntryKind.Tagged });
Assert.AreEqual(3, entries.Count);
Assert.IsTrue(entries.All(e => e.AuthorUsername is "luca.prova" or "paolo.prova"), "Tagged posts keep their author.");
var reels = await h.RunAsync(RecipeKind.Reels, marta.Id);
Assert.AreEqual(RecipeStatus.Completed, reels.Status, reels.Message);
var posts = await h.Db.Entries.ListByAccountAsync(marta.Id, new EntryListOptions { Kind = EntryKind.Post });
Assert.AreEqual(4, posts.Count, "Marta's four reels, stored as posts.");
// A reel found again in the grid is the same entry: the grid adds the others only.
var check = await h.RunAsync(RecipeKind.ProfileCheck, marta.Id);
Assert.AreEqual(RecipeStatus.Completed, check.Status, check.Message);
posts = await h.Db.Entries.ListByAccountAsync(marta.Id, new EntryListOptions { Kind = EntryKind.Post });
Assert.AreEqual(posts.Count, posts.Select(p => p.InstagramId).Distinct().Count());
}
[TestMethod]
[Timeout(90_000, CooperativeCancellation = true)]
public async Task TheFollowingListIsReadOverSeveralSessionsWithItsProgressKept()
{
await using var h = await CollectionHarness.CreateAsync(Chromium);
Assert.AreEqual(RecipeStatus.Completed, (await h.RunAsync(RecipeKind.FollowingSweep)).Status, "The sweep learns Alberto's account.");
var first = await h.RunAsync(RecipeKind.FollowingList, maxPages: 2);
Assert.AreEqual(RecipeStatus.Completed, first.Status, first.Message);
Assert.AreEqual("Letti 20 profili, continuo alla prossima sessione", first.Message);
Assert.IsFalse(first.BackfillFinished);
Assert.AreEqual(2, h.Governor.Granted(ActionKind.FollowingListPage));
Assert.AreEqual(20, await h.Db.Following.CountAsync());
h.Site.Control.ClearRequests();
var second = await h.RunAsync(RecipeKind.FollowingList, maxPages: 2);
Assert.AreEqual(RecipeStatus.Completed, second.Status, second.Message);
Assert.IsTrue(second.BackfillFinished, "End of the list.");
StringAssert.Contains(h.Site.Control.DataRequests[0], "dopo=c20");
var list = await h.Db.Following.ListAsync();
Assert.AreEqual(28, list.Count);
Assert.AreEqual("marta.prova", list[0].Username);
Assert.IsNull(await h.Db.Settings.GetAsync(RecipeRunner.FollowingKeys.Cursor));
}
[TestMethod]
[Timeout(90_000, CooperativeCancellation = true)]
public async Task AFollowedProfileThatChangedNameIsRecognizedByItsIdentifier()
{
await using var h = await CollectionHarness.CreateAsync(Chromium);
var marta = await h.AddAccountAsync("marta.prova");
lock (h.Site.World.Gate)
{
h.Site.World.FindByUsername("marta.prova")!.Username = "marta.nuova";
}
var outcome = await h.RunAsync(RecipeKind.FollowingSweep);
Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message);
var renamed = await h.Db.Accounts.GetAsync(marta.Id);
Assert.AreEqual("marta.nuova", renamed!.Username);
Assert.AreEqual("marta.prova", renamed.PreviousUsername);
Assert.IsTrue(renamed.Notices.HasFlag(AccountNotices.Renamed));
Assert.IsTrue((await h.Db.Events.ListAsync(new EventQuery())).Any(e => e.Code == "account-rinominato"));
}
/// A Governor whose budget view is optimistic but which refuses a category after some grants.
private sealed class LimitedButOptimisticGovernor(PermissiveGovernor inner, ActionKind kind, int grants) : IGovernor
{
private int _granted;
public Task RequestAsync(ActionKind requested, long? accountId, CancellationToken ct)
{
if (requested == kind && Interlocked.Increment(ref _granted) > grants)
{
return Task.FromResult(Permit.Deny(requested, accountId, DenialReason.HourlyBudgetReached, "Tetto orario delle storie raggiunto (prova)."));
}
return inner.RequestAsync(requested, accountId, ct);
}
}
}