using System.Globalization; using System.Text.Json.Nodes; using Febe.Core.Abstractions; using Febe.Core.Collection; using Febe.Core.Collection.Diagnostics; using Febe.Core.Collection.Readers; using Febe.Core.Collection.Recipes; using Febe.Core.Collection.Requests; using Febe.Core.Domain; using Febe.Core.Engine; using Febe.Core.Storage; using Febe.Tests.Collection.Support; using Febe.Tests.Support; using VirtualClock = Febe.Tests.Browser.TestSupport.VirtualClock; namespace Febe.Tests.Collection; /// /// The recipes by requests (issue #36, S3) on the cleaned samples of the test site, through the scripted site client: /// the page is never touched, every request has its own permit in the category of its operation, the answers go to the /// readers and the stop cases of §7.6 stop the run where it is. /// [TestClass] public sealed class RequestRecipeTests { [TestMethod] public async Task TheProfileCheckIsOneRequestThatGivesProfileGridAndCollections() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); var outcome = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message); Assert.AreEqual(0, bed.Page.Touched, "The page is never touched."); CollectionAssert.AreEqual(new[] { "ProvaProfiloQuery" }, bed.Scripted!.Sent.Select(s => s.Operation).ToArray()); CollectionAssert.AreEqual(new[] { ActionKind.ProfileOpen }, bed.Governor.Requests.Select(r => r.Kind).ToArray()); Assert.AreEqual(1, outcome.CountedActions); Assert.AreEqual(12, (await bed.Db.Entries.ListByAccountAsync(account.Id, new EntryListOptions { Kind = EntryKind.Post, Limit = 100 })).Count); Assert.AreEqual(2, (await bed.Db.Highlights.ListAsync(account.Id)).Count, "The collections came in the same answer."); Assert.IsNotNull(await bed.Db.Settings.GetAsync($"raccolte.vassoio.{Id(account)}")); var stored = (await bed.Db.Accounts.GetAsync(account.Id))!; Assert.AreEqual("n17", stored.BackfillCursor, "The history starts from the cursor of the first page."); } [TestMethod] public async Task ASecondCheckWithNothingNewAsksOnceMore() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); var outcome = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message); Assert.AreEqual(0, outcome.NewEntries); Assert.AreEqual(2, bed.Scripted!.Sent.Count, "Known posts: no gap to fill."); } [TestMethod] public async Task TheGapBelowANewFirstPageGoesOnByItsCursor() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(a => a with { BackfillCursor = "c40", State = AccountState.Backfilling }); var outcome = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message); var sent = bed.Scripted!.Sent; CollectionAssert.AreEqual(new[] { "ProvaProfiloQuery", "profilo-altri" }, sent.Select(s => s.Operation).ToArray()); Assert.AreEqual("n17", sent[1].Values[CaseValues.Cursor]); Assert.AreEqual("ndkwq.shzgd", sent[1].Values[CaseValues.Username]); CollectionAssert.AreEqual(new[] { ActionKind.ProfileOpen, ActionKind.BackfillPage }, bed.Governor.Requests.Select(r => r.Kind).ToArray()); Assert.AreEqual("n17", await bed.Db.Settings.GetAsync($"raccolta.lacuna.{Id(account)}.cursore"), "The page after was not of the account: the point stays."); Assert.AreEqual(1, bed.Clock.Delays.Count(d => d >= TimeSpan.FromSeconds(2)), "One wait between the two pages, none before the first request."); } [TestMethod] public async Task TheHistoryStartsWithTheProfileAndGoesOnByRequests() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(a => a with { State = AccountState.Backfilling }); var outcome = await bed.RunAsync(RecipeKind.Backfill, account.Id, maxPages: 3); Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message); Assert.AreEqual(0, bed.Page.Touched); CollectionAssert.AreEqual(new[] { "ProvaProfiloQuery", "profilo-altri" }, bed.Scripted!.Sent.Select(s => s.Operation).ToArray()); CollectionAssert.AreEqual(new[] { ActionKind.ProfileOpen, ActionKind.BackfillPage }, bed.Governor.Requests.Select(r => r.Kind).ToArray(), "The first page comes with the profile."); Assert.AreEqual("n17", (await bed.Db.Accounts.GetAsync(account.Id))!.BackfillCursor); } [TestMethod] public async Task AStoredCursorIsAskedForDirectly() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(a => a with { BackfillCursor = "c24", State = AccountState.Backfilling }); await bed.RunAsync(RecipeKind.Backfill, account.Id); var sent = bed.Scripted!.Sent.Single(); Assert.AreEqual("profilo-altri", sent.Operation); Assert.AreEqual("c24", sent.Values[CaseValues.Cursor]); Assert.AreEqual(ActionKind.BackfillPage, bed.Governor.Requests.Single().Kind); } [TestMethod] public async Task TaggedAndReelsEndAtTheLastPageAndAreMarkedComplete() { await using var bed = await RequestBed.CreateAsync(); var marta = await bed.AddSampleAccountAsync(a => a with { ContentTypes = a.ContentTypes | ContentTypes.Tagged }); var reels = await bed.AddAccountAsync("6104", "lmxm.bgwrl"); var tagged = await bed.RunAsync(RecipeKind.Tagged, marta.Id); var reel = await bed.RunAsync(RecipeKind.Reels, reels.Id); Assert.AreEqual(RecipeStatus.Completed, tagged.Status, tagged.Message); Assert.AreEqual(RecipeStatus.Completed, reel.Status, reel.Message); Assert.AreEqual(0, bed.Page.Touched); CollectionAssert.AreEqual(new[] { "taggati", "reel" }, bed.Scripted!.Sent.Select(s => s.Operation).ToArray()); CollectionAssert.AreEqual(new[] { ActionKind.BackfillPage, ActionKind.BackfillPage }, bed.Governor.Requests.Select(r => r.Kind).ToArray()); Assert.AreEqual(3, (await bed.Db.Entries.ListByAccountAsync(marta.Id, new EntryListOptions { Kind = EntryKind.Tagged, Limit = 100 })).Count); Assert.AreEqual(6, (await bed.Db.Entries.ListByAccountAsync(reels.Id, new EntryListOptions { Kind = EntryKind.Post, Limit = 100 })).Count); Assert.IsNotNull(await bed.Db.Settings.GetAsync($"raccolta.taggati.{Id(marta)}.completo")); Assert.IsNotNull(await bed.Db.Settings.GetAsync($"raccolta.reel.{Id(reels)}.completo")); } [TestMethod] public async Task TheStoriesOfAnAccountAreOneRequestWithoutTheMinimumOfTen() { await using var bed = await RequestBed.CreateAsync(); bed.Governor.ReportedRemaining = 1; // by the viewer the recipe would not even start var account = await bed.AddSampleAccountAsync(); var outcome = await bed.RunAsync(RecipeKind.AccountStories, account.Id); Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message); Assert.AreEqual(0, bed.Page.Touched); Assert.AreEqual("storie", bed.Scripted!.Sent.Single().Operation); Assert.AreEqual(ActionKind.StoryView, bed.Governor.Requests.Single().Kind, "One StoryView for all the stories of the answer."); Assert.AreEqual(4, outcome.NewEntries); Assert.AreEqual(4, (await bed.Db.Entries.ListByAccountAsync(account.Id, new EntryListOptions { Kind = EntryKind.Story, Limit = 100 })).Count); Assert.IsNotNull((await bed.Db.Accounts.GetAsync(account.Id))!.LastStoriesCheckAt); } [TestMethod] public async Task TheStoriesOfTwoAccountsInARowDoNotWaitBeforeTheirRequest() { // §6.3: the first request of a recipe does not wait, the Governor keeps the distance (unlike the viewer, which // waits 1–4 s between two accounts). await using var bed = await RequestBed.CreateAsync(); var first = await bed.AddSampleAccountAsync(); var second = await bed.AddAccountAsync("6666", "altra.prova"); Assert.AreEqual(RecipeStatus.Completed, (await bed.RunAsync(RecipeKind.AccountStories, first.Id)).Status); var before = bed.Clock.Now; await bed.RunAsync(RecipeKind.AccountStories, second.Id); Assert.HasCount(2, bed.Scripted!.Sent); Assert.AreEqual(before, bed.Clock.Now, "No wait between the two accounts."); } [TestMethod] public async Task NoStoriesIsAQuietDayNotAnEmptyPage() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); bed.Scripted!.Then(ScriptedReply.NotFoundJson); var outcome = await bed.RunAsync(RecipeKind.AccountStories, account.Id); Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message); Assert.AreEqual(0, outcome.NewEntries); Assert.IsNotNull((await bed.Db.Accounts.GetAsync(account.Id))!.LastStoriesCheckAt); } [TestMethod] public async Task ACollectionNotKnownYetWaitsForTheProfileCheck() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); var outcome = await bed.RunAsync(RecipeKind.Highlight, account.Id, highlightId: "3688348931"); Assert.AreEqual(RecipeStatus.Completed, outcome.Status); StringAssert.Contains(outcome.Message, "non è ancora nota"); Assert.AreEqual(0, bed.Scripted!.Sent.Count); Assert.AreEqual(0, bed.Governor.Requests.Count); } [TestMethod] public async Task AKnownCollectionIsReadByItsIdentifier() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); var outcome = await bed.RunAsync(RecipeKind.Highlight, account.Id, highlightId: "3688348931"); Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message); var sent = bed.Scripted!.Sent[^1]; Assert.AreEqual("evidenza", sent.Operation); Assert.AreEqual("3688348931", sent.Values[CaseValues.HighlightId]); Assert.AreEqual(ActionKind.StoryView, bed.Governor.Requests[^1].Kind); Assert.AreEqual(3, outcome.NewEntries); var highlight = (await bed.Db.Highlights.FindAsync(account.Id, "3688348931"))!; Assert.IsNotNull(highlight.LastCheckedAt); } [TestMethod] public async Task ASinglePostIsAskedByItsCode() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); var entry = await AddPostAsync(bed, account, "6686720181", "TqwwCvG"); var outcome = await bed.RunAsync(RecipeKind.SinglePost, entryId: entry); Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message); StringAssert.StartsWith(outcome.Message, "Post completato"); Assert.AreEqual("TqwwCvG", bed.Scripted!.Sent.Single().Values[CaseValues.Code]); Assert.AreEqual(ActionKind.SinglePost, bed.Governor.Requests.Single().Kind); } [TestMethod] public async Task APostWithoutCodeGoesByThePageWhenThePageCanRun() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); var entry = await AddPostAsync(bed, account, "6686720181", null); var outcome = await bed.RunAsync(RecipeKind.SinglePost, entryId: entry); Assert.AreEqual(0, bed.Scripted!.Sent.Count); Assert.IsTrue(bed.Page.Touched > 0, "The page route was taken."); Assert.AreEqual(RecipeStatus.Failed, outcome.Status, "The page of the bed refuses everything."); Assert.IsTrue((await bed.EventCodesAsync()).Contains("richieste-valore-mancante")); } [TestMethod] public async Task APostWithoutCodeWaitsWhenThePageCannotRun() { var map = new SiteMap("prova", new Dictionary { [SitePages.Profile] = "/profilo/{username}/" }, [], SiteTargets.All); await using var bed = await RequestBed.CreateAsync(map); var account = await bed.AddSampleAccountAsync(); var entry = await AddPostAsync(bed, account, "6686720181", null); var outcome = await bed.RunAsync(RecipeKind.SinglePost, entryId: entry); Assert.AreEqual(RecipeStatus.Completed, outcome.Status, "Never NotConfigured: the engine would stop planning it."); StringAssert.Contains(outcome.Message, "il codice del post"); Assert.AreEqual(0, bed.Page.Touched); Assert.AreEqual(0, bed.Scripted!.Sent.Count); } [TestMethod] public async Task TheFollowingListStoresItsCursorAndAsksForItNextTime() { await using var bed = await RequestBed.CreateAsync(); await bed.AddAccountAsync("17841400000000077", "alberto.prova", isMine: true); var first = await bed.RunAsync(RecipeKind.FollowingList, maxPages: 1); var second = await bed.RunAsync(RecipeKind.FollowingList, maxPages: 1); Assert.AreEqual(RecipeStatus.Completed, first.Status, first.Message); StringAssert.StartsWith(first.Message, "Letti 10 profili"); Assert.AreEqual(RecipeStatus.Completed, second.Status, second.Message); var sent = bed.Scripted!.Sent; Assert.IsFalse(sent[0].Values.ContainsKey(CaseValues.Cursor), "The first page without «dopo»."); Assert.AreEqual("b44", sent[1].Values[CaseValues.Cursor]); Assert.AreEqual(0, bed.Page.Touched); CollectionAssert.AreEqual(new[] { ActionKind.FollowingListPage, ActionKind.FollowingListPage }, bed.Governor.Requests.Select(r => r.Kind).ToArray()); } [TestMethod] [DataRow(ScriptedReply.TooManyRequests, SignalLevel.Throttle)] [DataRow(ScriptedReply.RedirectToLogin, SignalLevel.Challenge)] [DataRow(ScriptedReply.RedirectToChallenge, SignalLevel.Challenge)] [DataRow(ScriptedReply.HtmlInsteadOfJson, SignalLevel.Challenge)] [DataRow(ScriptedReply.MissingRootField, SignalLevel.Challenge)] [DataRow(ScriptedReply.SessionCookieDeleted, SignalLevel.Challenge)] public async Task AStopSignalInTheAnswerStopsTheRunWhereItIs(ScriptedReply reply, SignalLevel level) { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(a => a with { BackfillCursor = "c40", State = AccountState.Backfilling }); bed.Scripted!.Then(reply); var outcome = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); Assert.AreEqual(RecipeStatus.StoppedBySignal, outcome.Status, outcome.Message); Assert.AreEqual(1, bed.Scripted.Sent.Count, "No other request after the signal."); Assert.AreEqual(level, bed.Breaker.Signals.Single().Level); Assert.AreEqual(0, outcome.NewEntries); Assert.AreEqual(0, bed.Page.Touched); } [TestMethod] public async Task AProfileTheSiteDoesNotHaveMakesTheAccountUnreachable() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); bed.Scripted!.Then(ScriptedReply.NotFoundJson); var outcome = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); Assert.AreEqual(RecipeStatus.AccountUnreachable, outcome.Status, outcome.Message); var stored = (await bed.Db.Accounts.GetAsync(account.Id))!; Assert.AreEqual(AccountState.Unreachable, stored.State); Assert.AreEqual(UnreachableReason.NotFound, stored.UnreachableReason); Assert.AreEqual(0, bed.Breaker.Signals.Count, "A «not found» is not a stop signal."); } [TestMethod] [DataRow(ScriptedReply.ServerError500, "(500)")] [DataRow(ScriptedReply.ServerError503, "(503)")] [DataRow(ScriptedReply.Timeout, "nessuna risposta in tempo")] [DataRow(ScriptedReply.Unreachable, "collegamento non riuscito")] [DataRow(ScriptedReply.TooLarge, "risposta troppo grande")] [DataRow(ScriptedReply.Broken, "risposta rovinata")] public async Task AnErrorOfTheSiteFailsTheRunWithoutRetrying(ScriptedReply reply, string said) { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); bed.Scripted!.Then(reply); var outcome = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); Assert.AreEqual(RecipeStatus.Failed, outcome.Status, outcome.Message); StringAssert.Contains(outcome.Message, said); Assert.AreEqual(1, bed.Scripted.Sent.Count, "Never tried again in the run."); Assert.IsTrue((await bed.EventCodesAsync()).Contains("ricetta-fallita")); Assert.AreEqual(0, bed.Breaker.Signals.Count, "One error is an empty page, not a signal."); } [TestMethod] public async Task ThreeEmptyAnswersInARowAreAThrottle() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); bed.Scripted!.Then(ScriptedReply.ServerError500, ScriptedReply.ServerError500, ScriptedReply.ServerError500); await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); var third = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); Assert.AreEqual(RecipeStatus.StoppedBySignal, third.Status, third.Message); Assert.AreEqual(SignalLevel.Throttle, bed.Breaker.Signals.Single().Level); } [TestMethod] public async Task AnAnswerNoReaderKnowsIsAnUnrecognizedFormat() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); bed.Scripted!.Then(ScriptedReply.UnknownShape); var outcome = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); Assert.AreEqual(RecipeStatus.UnrecognizedFormat, outcome.Status, outcome.Message); Assert.IsTrue((await bed.EventCodesAsync()).Contains("formato-non-riconosciuto")); } [TestMethod] public async Task ARefusedPermitSendsNothing() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); bed.Governor.Limit(ActionKind.ProfileOpen, 0); var outcome = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); Assert.AreEqual(RecipeStatus.Denied, outcome.Status); Assert.AreEqual(0, bed.Scripted!.Sent.Count); } [TestMethod] public async Task TheGapWaitsWhenThePermitOfItsPageIsRefused() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(a => a with { BackfillCursor = "c40", State = AccountState.Backfilling }); bed.Governor.Limit(ActionKind.BackfillPage, 0); var outcome = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); Assert.AreEqual(RecipeStatus.Completed, outcome.Status, "The check itself is done."); Assert.AreEqual(1, bed.Scripted!.Sent.Count); Assert.AreEqual("n17", await bed.Db.Settings.GetAsync($"raccolta.lacuna.{Id(account)}.cursore")); } [TestMethod] public async Task WithoutTheSiteClientEveryRecipeGoesByThePage() { await using var bed = await RequestBed.CreateAsync(requests: false); var account = await bed.AddSampleAccountAsync(); var outcome = await bed.RunAsync(RecipeKind.AccountStories, account.Id); Assert.IsTrue(bed.Page.Touched > 0); Assert.AreEqual(0, bed.Scripted!.Sent.Count); Assert.AreEqual(RecipeStatus.Failed, outcome.Status); } [TestMethod] public async Task TheFollowingSweepNeverGoesByRequests() { await using var bed = await RequestBed.CreateAsync(); Assert.IsFalse(bed.Runner.HasRequestRoute(RecipeKind.FollowingSweep)); Assert.IsNull(RecipeRunner.RequestOperations(RequestCatalog.LoadTestSite(), RecipeKind.FollowingSweep)); } [TestMethod] public async Task TheDiagnosticReportSeesTheRequestsByShapeOnly() { var clock = new VirtualClock(new DateTimeOffset(2026, 10, 13, 16, 0, 0, TimeSpan.Zero)); long? current = null; using var diagnostics = new CollectionDiagnostics(clock, () => current) { SiteMap = SiteMap.LoadTestSite() }; await using var bed = await RequestBed.CreateAsync(diagnostics: diagnostics, clock: clock); current = bed.Session.SessionId; diagnostics.SessionStarted(bed.Session.SessionId, clock.Now, 1.0); var account = await bed.AddSampleAccountAsync(a => a with { BackfillCursor = "c40", State = AccountState.Backfilling }); await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); await bed.RunAsync(RecipeKind.Highlight, account.Id, highlightId: "3688348931"); diagnostics.SessionEnded(bed.Session.SessionId, clock.Now, SessionOutcome.Completed, SessionStopCause.PlannedEnd, RecipeStatus.Completed); using var stream = new MemoryStream(); DiagnosticReport.Write(stream, diagnostics.Collect(), new DiagnosticReportHeader("0.0.0-prove", "docker", clock.Now, TestZones.Rome)); var text = System.Text.Encoding.UTF8.GetString(stream.ToArray()); Assert.AreEqual(0, diagnostics.Faults); StringAssert.Contains(text, "/api/graphql", "The request is there, by the shape of its path."); StringAssert.Contains(text, "/api/{testo}/{testo}/{testo}", "The other requests too, by the shapes of their paths."); foreach (var value in new[] { "ndkwq.shzgd", "3688348931", "n17", "c40", "5555", "TqwwCvG" }) { Assert.IsFalse(text.Contains(value, StringComparison.Ordinal), $"«{value}» is in the report."); } } [TestMethod] public async Task StoriesNotFoundAskTheProfileAndArePlainlyNoStoriesForAPublicOne() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); for (var i = 0; i < 3; i++) { bed.Scripted!.Then(ScriptedReply.NotFoundJson); var outcome = await bed.RunAsync(RecipeKind.AccountStories, account.Id); Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message); } CollectionAssert.AreEqual(new[] { "storie", "ProvaProfiloQuery", "storie", "ProvaProfiloQuery", "storie", "ProvaProfiloQuery" }, bed.Scripted!.Sent.Select(s => s.Operation).ToArray()); CollectionAssert.AreEqual(new[] { ActionKind.StoryView, ActionKind.ProfileOpen }, bed.Governor.Requests.Take(2).Select(r => r.Kind).ToArray(), "The profile says why, as by the page."); Assert.AreEqual(0, bed.Breaker.Signals.Count, "Three «not found» of the stories are not three empty pages."); var stored = (await bed.Db.Accounts.GetAsync(account.Id))!; Assert.AreNotEqual(AccountState.Unreachable, stored.State); Assert.IsNotNull(stored.LastStoriesCheckAt); } [TestMethod] public async Task StoriesNotFoundOfAProfileNotFoundMakeTheAccountUnreachable() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); bed.Scripted!.Then(ScriptedReply.NotFoundJson, ScriptedReply.NotFoundJson); var outcome = await bed.RunAsync(RecipeKind.AccountStories, account.Id); Assert.AreEqual(RecipeStatus.AccountUnreachable, outcome.Status, outcome.Message); Assert.AreEqual(UnreachableReason.NotFound, (await bed.Db.Accounts.GetAsync(account.Id))!.UnreachableReason); } [TestMethod] public async Task AStoriesAnswerStartsTheCountOfEmptyPagesAgain() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(); bed.Scripted!.Then(ScriptedReply.ServerError500, ScriptedReply.ServerError500); await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); var stories = await bed.RunAsync(RecipeKind.AccountStories, account.Id); bed.Scripted.Then(ScriptedReply.ServerError500); var third = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); Assert.AreEqual(RecipeStatus.Completed, stories.Status, stories.Message); Assert.AreEqual(RecipeStatus.Failed, third.Status, third.Message); Assert.AreEqual(0, bed.Breaker.Signals.Count, "The stories in between were data: the count of empty pages started again."); } [TestMethod] public async Task ASignalOnTheSecondPageOfTheHistoryKeepsThePointOfTheFirst() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(a => a with { State = AccountState.Backfilling }); bed.Scripted!.Then(ScriptedReply.Sample, ScriptedReply.TooManyRequests); var outcome = await bed.RunAsync(RecipeKind.Backfill, account.Id, maxPages: 3); Assert.AreEqual(RecipeStatus.StoppedBySignal, outcome.Status, outcome.Message); Assert.AreEqual(2, bed.Scripted.Sent.Count, "Nothing after the signal."); Assert.AreEqual(SignalLevel.Throttle, bed.Breaker.Signals.Single().Level); Assert.AreEqual("n17", (await bed.Db.Accounts.GetAsync(account.Id))!.BackfillCursor, "The point of the last page saved."); Assert.AreEqual(12, (await bed.Db.Entries.ListByAccountAsync(account.Id, new EntryListOptions { Kind = EntryKind.Post, Limit = 100 })).Count); } [TestMethod] public async Task ASignalOnThePageOfTheGapKeepsTheGapPoint() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(a => a with { BackfillCursor = "c40", State = AccountState.Backfilling }); bed.Scripted!.Then(ScriptedReply.Sample, ScriptedReply.RedirectToLogin); var outcome = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); Assert.AreEqual(RecipeStatus.StoppedBySignal, outcome.Status, outcome.Message); Assert.AreEqual(2, bed.Scripted.Sent.Count); Assert.AreEqual(SignalLevel.Challenge, bed.Breaker.Signals.Single().Level); Assert.AreEqual("n17", await bed.Db.Settings.GetAsync($"raccolta.lacuna.{Id(account)}.cursore")); Assert.AreEqual(12, (await bed.Db.Entries.ListByAccountAsync(account.Id, new EntryListOptions { Kind = EntryKind.Post, Limit = 100 })).Count, "The first page was saved before."); } [TestMethod] public async Task ATrayOfItsOwnIsAskedOnceADayAfterTheFirstPage() { await using var bed = await RequestBed.CreateAsync(catalog: TrayOfItsOwn()); var account = await bed.AddSampleAccountAsync(); var first = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); var second = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); Assert.AreEqual(RecipeStatus.Completed, first.Status, first.Message); Assert.AreEqual(RecipeStatus.Completed, second.Status, second.Message); CollectionAssert.AreEqual(new[] { "ProvaProfiloQuery", "profilo", "ProvaProfiloQuery" }, bed.Scripted!.Sent.Select(s => s.Operation).ToArray()); CollectionAssert.AreEqual(new[] { ActionKind.ProfileOpen, ActionKind.ProfileOpen, ActionKind.ProfileOpen }, bed.Governor.Requests.Select(r => r.Kind).ToArray()); Assert.AreEqual(2, (await bed.Db.Highlights.ListAsync(account.Id)).Count); } [TestMethod] public async Task AManualCheckAsksNoTrayForAnAccountThatDoesNotWantItsCollections() { await using var bed = await RequestBed.CreateAsync(catalog: TrayOfItsOwn()); var account = await bed.AddSampleAccountAsync(a => a with { ContentTypes = a.ContentTypes & ~ContentTypes.Highlights }); var outcome = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id, manual: true); Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message); Assert.AreEqual("ProvaProfiloQuery", bed.Scripted!.Sent.Single().Operation); } [TestMethod] public async Task ATrayRefusedOrFailedLosesNotTheFirstPage() { await using var refused = await RequestBed.CreateAsync(catalog: TrayOfItsOwn()); var one = await refused.AddSampleAccountAsync(); refused.Governor.Limit(ActionKind.ProfileOpen, 1); var denied = await refused.RunAsync(RecipeKind.ProfileCheck, one.Id); await using var failing = await RequestBed.CreateAsync(catalog: TrayOfItsOwn()); var two = await failing.AddSampleAccountAsync(); failing.Scripted!.Then(ScriptedReply.Sample, ScriptedReply.ServerError503); var failed = await failing.RunAsync(RecipeKind.ProfileCheck, two.Id); Assert.AreEqual(RecipeStatus.Completed, denied.Status, "The check is done; the list waits for the next one."); Assert.IsNull(await refused.Db.Settings.GetAsync($"raccolte.vassoio.{Id(one)}")); Assert.AreEqual(RecipeStatus.Failed, failed.Status, failed.Message); foreach (var (bed, account) in new[] { (refused, one), (failing, two) }) { Assert.AreEqual(12, (await bed.Db.Entries.ListByAccountAsync(account.Id, new EntryListOptions { Kind = EntryKind.Post, Limit = 100 })).Count, "The first page was saved before the list."); } } [TestMethod] public async Task ATrayTheReadersDoNotKnowIsAnUnrecognizedFormat() { await using var bed = await RequestBed.CreateAsync(catalog: TrayOfItsOwn()); var account = await bed.AddSampleAccountAsync(); bed.Scripted!.Then(ScriptedReply.Sample, ScriptedReply.UnknownShape); var outcome = await bed.RunAsync(RecipeKind.ProfileCheck, account.Id); Assert.AreEqual(RecipeStatus.UnrecognizedFormat, outcome.Status, outcome.Message); } [TestMethod] public async Task AStoredPointWithoutItsNextPageGoesByThePagesWhenTheyCanRun() { await using var bed = await RequestBed.CreateAsync(); var account = await bed.AddSampleAccountAsync(a => a with { ContentTypes = a.ContentTypes | ContentTypes.Tagged }); await bed.Db.Settings.SetAsync($"raccolta.taggati.{Id(account)}.cursore", "t12"); await bed.RunAsync(RecipeKind.Tagged, account.Id); Assert.AreEqual(0, bed.Scripted!.Sent.Count); Assert.IsTrue(bed.Page.Touched > 0, "The pages go on from the stored point."); Assert.IsTrue((await bed.EventCodesAsync()).Contains("richieste-pagine-successive")); } [TestMethod] public async Task AStoredPointWithoutItsNextPageStillReadsTheFirstPage() { var map = new SiteMap("prova", new Dictionary { [SitePages.Profile] = "/profilo/{username}/" }, [], SiteTargets.All); await using var bed = await RequestBed.CreateAsync(map); var account = await bed.AddSampleAccountAsync(a => a with { ContentTypes = a.ContentTypes | ContentTypes.Tagged }); await bed.Db.Settings.SetAsync($"raccolta.taggati.{Id(account)}.cursore", "t12"); var outcome = await bed.RunAsync(RecipeKind.Tagged, account.Id); Assert.AreEqual(RecipeStatus.Completed, outcome.Status, outcome.Message); Assert.AreEqual(0, bed.Page.Touched); var sent = bed.Scripted!.Sent.Single(); Assert.AreEqual("taggati", sent.Operation, "What is new on top is read."); Assert.IsFalse(sent.Values.ContainsKey(CaseValues.Cursor)); Assert.AreEqual(3, (await bed.Db.Entries.ListByAccountAsync(account.Id, new EntryListOptions { Kind = EntryKind.Tagged, Limit = 100 })).Count); Assert.IsNotNull(await bed.Db.Settings.GetAsync($"raccolta.taggati.{Id(account)}.completo"), "The whole tab fits in its first page: it is complete."); } /// The test site's catalog with the list of collections asked by an operation of its own (the shape of Instagram's). private static RequestCatalog TrayOfItsOwn() { var file = JsonNode.Parse(SiteMap.ReadEmbeddedText("richieste.prova.json"))!.AsObject(); foreach (var operation in file["operations"]!.AsArray()) { switch (operation!["name"]!.GetValue()) { case "ProvaProfiloQuery": operation["roles"] = new JsonArray("profile", "posts"); break; case "profilo": operation["roles"] = new JsonArray("highlights"); break; } } return RequestCatalog.Parse(file.ToJsonString(), "richieste.prova.json"); } private static async Task AddPostAsync(RequestBed bed, Account account, string instagramId, string? code) { var result = await bed.Db.Entries.AddAsync( new Entry { AccountId = account.Id, Kind = EntryKind.Post, InstagramId = instagramId, Code = code, PublishedAt = bed.Clock.Now.AddDays(-3), DiscoveredAt = bed.Clock.Now.AddDays(-3), }, []); return result.Stored.Entry.Id; } private static string Id(Account account) => account.Id.ToString(CultureInfo.InvariantCulture); }