using Febe.Core.Domain; using Febe.Core.Engine; using Febe.Tests.Engine.Support; using Febe.Tests.Support; namespace Febe.Tests.Engine; /// /// «Cosa scaricare prima» (spec §7.2, §6.5, tappa D1): the order of the kinds, its weight on the visits after the Giro dei /// seguiti (on the virtual clock, with a fixed seed) and on the queue of the files. /// [TestClass] public sealed class DownloadOrderTests { private static readonly DateTimeOffset Morning = EngineTestBed.Local(2026, 10, 14, 6, 0); [TestMethod] public void TheDefaultOrderIsStoriesHighlightsPostsReelsTagged() { Assert.AreEqual("storie,evidenza,post,reel,taggati", DownloadOrder.Default.ToString()); Assert.IsTrue(DownloadOrder.Parse(null).IsDefault); Assert.IsTrue(DownloadOrder.Parse(" ").IsDefault); Assert.AreEqual(16, DownloadOrder.Default.Weight(DownloadKind.Stories)); Assert.AreEqual(8, DownloadOrder.Default.Weight(DownloadKind.Highlights)); Assert.AreEqual(4, DownloadOrder.Default.Weight(DownloadKind.Posts)); Assert.AreEqual(2, DownloadOrder.Default.Weight(DownloadKind.Reels)); Assert.AreEqual(1, DownloadOrder.Default.Weight(DownloadKind.Tagged)); } [TestMethod] [DataRow("taggati,post", "taggati,post,storie,evidenza,reel")] [DataRow("POST, post ,qualcosa,reel", "post,reel,storie,evidenza,taggati")] [DataRow("reel,taggati,post,evidenza,storie", "reel,taggati,post,evidenza,storie")] public void AnyStoredTextGivesTheFiveKindsOnce(string stored, string expected) { var order = DownloadOrder.Parse(stored); Assert.AreEqual(expected, order.ToString()); Assert.HasCount(5, order.Kinds); Assert.AreEqual(order.ToString(), DownloadOrder.Parse(order.ToString()).ToString()); } [TestMethod] public void EveryTaskAndEntryKindHasItsPlace() { Assert.IsNull(DownloadOrder.KindOf(EngineTaskKind.FollowingSweep), "il giro resta primo, nessun tipo lo pesa"); Assert.IsNull(DownloadOrder.KindOf(EngineTaskKind.FollowingList)); Assert.AreEqual(DownloadKind.Stories, DownloadOrder.KindOf(EngineTaskKind.AccountStories)); Assert.AreEqual(DownloadKind.Highlights, DownloadOrder.KindOf(EngineTaskKind.HighlightCheck)); Assert.AreEqual(DownloadKind.Posts, DownloadOrder.KindOf(EngineTaskKind.ProfileCheck)); Assert.AreEqual(DownloadKind.Posts, DownloadOrder.KindOf(EngineTaskKind.Backfill)); Assert.AreEqual(DownloadKind.Reels, DownloadOrder.KindOf(EngineTaskKind.ReelsCheck)); Assert.AreEqual(DownloadKind.Tagged, DownloadOrder.KindOf(EngineTaskKind.TaggedCheck)); Assert.AreEqual(DownloadKind.Posts, DownloadOrder.KindOf(EntryKind.ProfilePicture), "le foto del profilo vanno con i post"); var queue = DownloadOrder.Parse("taggati,post").EntryKinds(); CollectionAssert.AreEqual(new[] { EntryKind.Tagged, EntryKind.Post, EntryKind.ProfilePicture, EntryKind.Story, EntryKind.Highlight }, queue.ToArray()); } [TestMethod] public async Task TheQueueOfTheFilesFollowsTheOrder() { using var temp = new TempFolder(); await using var db = await TestDatabase.OpenAsync(temp); var a = await db.Accounts.AddAsync(Make.Account("marta.demo")); await db.Entries.AddAsync(Make.Entry(a.Id, "post", Make.T0), [Make.Photo(1), Make.Photo(2)]); await db.Entries.AddAsync(Make.Entry(a.Id, "storia", Make.T0, EntryKind.Story) with { ExpiresAt = Make.T0.AddHours(2) }, [Make.Video()]); await db.Entries.AddAsync(Make.Entry(a.Id, "taggato", Make.T0, EntryKind.Tagged), [Make.Photo()]); var standard = await db.Media.ListPendingDownloadsAsync(10, DownloadOrder.Default.EntryKinds()); CollectionAssert.AreEqual(new[] { EntryKind.Story, EntryKind.Post, EntryKind.Post, EntryKind.Tagged }, standard.Select(p => p.Kind).ToArray()); var tagged = await db.Media.ListPendingDownloadsAsync(10, DownloadOrder.Parse("taggati,post,storie").EntryKinds()); CollectionAssert.AreEqual(new[] { EntryKind.Tagged, EntryKind.Post, EntryKind.Post, EntryKind.Story }, tagged.Select(p => p.Kind).ToArray()); Assert.AreEqual(1, tagged[1].Media.Position, "dentro un tipo l'ordine di sempre"); } [TestMethod] public async Task TheOrderWeighsTheVisitsAfterTheSweepWithoutMakingThemFixed() { var standard = await FirstVisitsAsync(null); var postsFirst = await FirstVisitsAsync("post,storie"); Assert.IsGreaterThan(10, postsFirst.Sessions, "abbastanza sessioni con le due cose da fare"); Assert.IsGreaterThan(standard.PostsFirst + 0.2, postsFirst.PostsFirst, $"post per primi: {postsFirst.PostsFirst:P0} contro {standard.PostsFirst:P0}"); Assert.IsLessThan(1.0, postsFirst.PostsFirst, "l'ordine pesa ma non diventa fisso"); Assert.IsGreaterThan(0.0, standard.PostsFirst, "anche con le storie in cima i post vengono talvolta prima"); Assert.IsGreaterThan(0.75, postsFirst.SweepFirst, "il giro resta di norma il primo"); Assert.AreEqual(standard.SweepFirst, postsFirst.SweepFirst, 0.15, "la composizione delle sessioni non cambia"); } [TestMethod] public async Task AKindWithNothingDueIsNotVisited() { await using var bed = await EngineTestBed.CreateAsync(Morning); await bed.Db.Settings.SetAsync(DownloadOrder.SettingKey, "evidenza,taggati,reel,post,storie"); await bed.AddAccountAsync("seguito.demo", followed: true); for (var i = 0; i < 4; i++) { await bed.AddAccountAsync($"post{i}.demo", followed: false, Frequency.High, types: ContentTypes.PostPhotos); } var runner = new ScriptedRunner(bed.Governor, bed.Clock); bed.Start(runner); await bed.RunUntilAsync(Morning.AddDays(2)); Assert.IsFalse(runner.Runs.Any(r => r.Request.Kind == RecipeKind.Highlight), "nessuna raccolta in evidenza da controllare"); Assert.IsTrue(runner.Runs.Any(r => r.Request.Kind == RecipeKind.ProfileCheck), "il resto del lavoro va avanti"); } /// /// Three days of the engine with accounts that want only their stories and accounts that want only their posts: in /// how many sessions with both, the first of them after the sweep was a profile check. /// private static async Task<(double PostsFirst, double SweepFirst, int Sessions)> FirstVisitsAsync(string? order) { await using var bed = await EngineTestBed.CreateAsync(Morning, seed: 11); if (order is not null) { await bed.Db.Settings.SetAsync(DownloadOrder.SettingKey, order); } await bed.AddAccountAsync("seguito.demo", followed: true); for (var i = 0; i < 6; i++) { await bed.AddAccountAsync($"storie{i}.demo", followed: false, Frequency.High, types: ContentTypes.Stories); await bed.AddAccountAsync($"post{i}.demo", followed: false, Frequency.High, types: ContentTypes.PostPhotos); } var runner = new ScriptedRunner(bed.Governor, bed.Clock); bed.Start(runner); await bed.RunUntilAsync(Morning.AddDays(3)); var sessions = runner.Runs.GroupBy(r => r.SessionId).ToList(); var sweepFirst = sessions.Count(g => g.First().Request.Kind == RecipeKind.FollowingSweep) / (double)sessions.Count; var both = sessions .Select(g => g.Where(r => r.Request.Kind is RecipeKind.AccountStories or RecipeKind.ProfileCheck).ToList()) .Where(v => v.Any(r => r.Request.Kind == RecipeKind.AccountStories) && v.Any(r => r.Request.Kind == RecipeKind.ProfileCheck)) .ToList(); var postsFirst = both.Count == 0 ? 0 : both.Count(v => v[0].Request.Kind == RecipeKind.ProfileCheck) / (double)both.Count; return (postsFirst, sweepFirst, both.Count); } }