using Febe.Core.Abstractions; using Febe.Core.Collection.Readers; using Febe.Core.Collection.Recipes; using Febe.Core.Collection.Search; using Febe.Core.Engine; using Febe.Tests.Collection.Support; using Febe.Tests.Support; namespace Febe.Tests.Collection; /// /// The profile search (issue #37) against the local test site, with a real Chromium on a private Xvfb display: the /// panel «Cerca», typing with key events, the answers in the shape of the cleaned sample, the photos from the CDN, and /// the rest page. Never Instagram. /// [TestClass] [TestCategory(SyntheticSite.Category)] [DoNotParallelize] public sealed class SearchIntegrationTests { private static SharedChromium? s_chromium; private static string? s_unavailable; 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(90_000, CooperativeCancellation = true)] public async Task TheSearchTypesInTheSitesBoxAndReadsItsAnswerWithoutOpeningAResult() { await using var h = await CollectionHarness.CreateAsync(Chromium); var first = await h.Search.SearchAsync(() => "marta", CancellationToken.None); Assert.AreEqual(SearchOutcome.Ready, first.Outcome, first.Message); Assert.AreEqual("marta.prova", first.Answer!.Hits[0].Profile.Username); Assert.AreEqual("/", await h.CurrentPathAsync(), "the page stays the home page: no result is opened"); Assert.AreEqual(PageUser.Search, h.Turns.LastUser); Assert.IsTrue(h.Governor.Granted(ActionKind.ProfileSearch) >= 3, "every gesture after a permit"); Assert.AreEqual(0, h.Site.Control.RecentSearchWrites, "Enter is never pressed and no result is clicked"); // Alberto keeps typing: «marta» → «ma»: three Backspaces, the same box, a wider answer. var second = await h.Search.SearchAsync(() => "ma", CancellationToken.None); Assert.AreEqual(SearchOutcome.Ready, second.Outcome, second.Message); Assert.IsTrue(second.Answer!.Hits.Count >= 1); var none = await h.Search.SearchAsync(() => "zzzz", CancellationToken.None); Assert.AreEqual(SearchOutcome.Empty, none.Outcome, none.Message); await h.Search.RestAsync(CancellationToken.None); Assert.IsFalse(await h.Driver.IsOnSiteAsync(CancellationToken.None), "back to the rest page"); } [TestMethod] [Timeout(90_000, CooperativeCancellation = true)] public async Task ThePhotosOfTheResultsComeFromTheCdnIntoMemory() { await using var h = await CollectionHarness.CreateAsync(Chromium); var result = await h.Search.SearchAsync(() => "prova", CancellationToken.None); Assert.AreEqual(SearchOutcome.Ready, result.Outcome, result.Message); var queue = h.Pipeline.SearchPhotos!; queue.Enqueue(result.Answer!.Hits.Take(3).Select(x => new SearchPhotoRequest(x.Profile.Id, new Uri(x.Profile.Picture!.Url)))); await TestWait.UntilAsync(() => h.SearchPhotos.Count == 3, "three photos in memory"); var key = h.SearchPhotos.KeyOf(result.Answer.Hits[0].Profile.Id)!; Assert.IsTrue(h.SearchPhotos.TryOpen(key, out var stream)); using (stream) { var head = new byte[2]; Assert.AreEqual(2, stream.Read(head)); CollectionAssert.AreEqual(new byte[] { 0xFF, 0xD8 }, head, "a JPEG"); } Assert.IsTrue(h.DataBudget.Used > 0, "the bytes count in the daily data budget"); } [TestMethod] [Timeout(120_000, CooperativeCancellation = true)] public async Task OutOfOrderAnswersAndThePagesMemoryStillGiveTheAnswerToTheWholeText() { await using var h = await CollectionHarness.CreateAsync(Chromium); // «ricerca/disordine»: shorter texts are answered later; «ricerca/memoria»: the page sends no request for a // text it has already searched. Both are read when the page loads: the first step opens the home page. h.Site.Control.SearchDisorder = true; h.Site.Control.SearchMemory = true; var first = await h.Search.SearchAsync(() => "marta", CancellationToken.None); Assert.AreEqual(SearchOutcome.Ready, first.Outcome, first.Message); Assert.IsTrue(first.Answer!.Hits.All(Matches), "only profiles that match «marta», never the answer to a prefix"); Assert.AreEqual(SearchOutcome.Ready, (await h.Search.SearchAsync(() => "ma", CancellationToken.None)).Outcome); var back = await h.Search.SearchAsync(() => "marta", CancellationToken.None); Assert.AreEqual(SearchOutcome.Ready, back.Outcome, back.Message); CollectionAssert.AreEqual( first.Answer.Hits.Select(x => x.Profile.Username).ToArray(), back.Answer!.Hits.Select(x => x.Profile.Username).ToArray(), "the page shows «marta» from its memory: Febe gives the answer it read on this page"); Assert.AreEqual(0, h.Site.Control.RecentSearchWrites); static bool Matches(SearchProfile hit) => hit.Profile.Username.Contains("marta", StringComparison.Ordinal) || (hit.Profile.DisplayName ?? string.Empty).Contains("marta", StringComparison.OrdinalIgnoreCase); } [TestMethod] [Timeout(120_000, CooperativeCancellation = true)] public async Task ARecipeAfterASearchStillWorks() { await using var h = await CollectionHarness.CreateAsync(Chromium); var marta = await h.AddAccountAsync("marta.prova"); Assert.AreEqual(SearchOutcome.Ready, (await h.Search.SearchAsync(() => "luca", CancellationToken.None)).Outcome); var check = await h.RunAsync(RecipeKind.ProfileCheck, marta.Id); Assert.AreEqual(RecipeStatus.Completed, check.Status, check.Message); Assert.AreEqual(PageUser.Engine, h.Turns.LastUser); Assert.IsFalse(h.Turns.SearchTouchedPage); } }