using Febe.Core.Collection; using Febe.Core.Collection.Readers; using Febe.Core.Domain; using Febe.Core.Engine; using Febe.Core.Storage; using Febe.Tests.Engine.Support; using Febe.Tests.Support; namespace Febe.Tests.Engine; /// /// The collection configuration in use (reader vocabulary and target map of the site, as loaded): what the log says of /// it, the copies in the Data folder that hide newer embedded files, and the suspensions counted against other files, /// cleared once when it changes. /// [TestClass] public sealed class CollectionConfigurationTests { private static readonly DateTimeOffset Morning = EngineTestBed.Local(2026, 10, 14, 6, 0); [TestMethod] public async Task AChangedCollectionConfigurationLiftsTheSuspensionsOfTheOldFilesOnce() { await using var bed = await EngineTestBed.CreateAsync(Morning); await bed.AddAccountAsync("strano.demo", followed: false, Frequency.High); // What the first Instagram files left in Alberto's database: suspended recipes and failures counted. var since = Morning.AddDays(-3).ToUnixTimeMilliseconds(); await bed.Db.Settings.SetAsync(EngineSettingKeys.SuspendedRecipes, $"AccountStories={since};ProfileCheck={since}"); await bed.Db.Settings.SetAsync(EngineSettingKeys.RecipeFailures, "AccountStories=3;Backfill=2;ProfileCheck=3"); var runner = new ScriptedRunner(bed.Governor, bed.Clock); var changed = false; var host = await bed.StartAsync(runner, async h => changed = await h.ApplyCollectionConfigurationAsync("impronta-1")); Assert.IsTrue(changed, "The first start that knows the configuration counts as a change."); Assert.IsEmpty(host.SuspendedRecipes); Assert.IsNull(await bed.Db.Settings.GetAsync(EngineSettingKeys.SuspendedRecipes)); Assert.IsNull(await bed.Db.Settings.GetAsync(EngineSettingKeys.RecipeFailures)); Assert.AreEqual("impronta-1", await bed.Db.Settings.GetAsync(EngineSettingKeys.CollectionConfiguration)); var line = (await bed.Db.Events.ListAsync(new EventQuery())).Single(e => e.Code == EngineEventCodes.CollectionConfigurationChanged); Assert.AreEqual("La configurazione della raccolta è cambiata: le ricette sospese ripartono («Le storie», «Il controllo del profilo»).", line.Message); Assert.AreEqual(EventSeverity.Info, line.Severity); await bed.RunUntilAsync(Morning.AddDays(1)); Assert.IsTrue(runner.Runs.Any(r => r.Request.Kind == RecipeKind.ProfileCheck && r.Status == RecipeStatus.Completed), "The recipe runs again."); Assert.IsFalse(await host.ApplyCollectionConfigurationAsync("impronta-1"), "Same files: nothing to do."); // A restart with the same files changes nothing; new files with nothing to clear say nothing. await bed.ReopenAsync(8); var again = true; var reopened = await bed.StartAsync(new ScriptedRunner(bed.Governor, bed.Clock), async h => again = await h.ApplyCollectionConfigurationAsync("impronta-1")); Assert.IsFalse(again); Assert.IsTrue(await reopened.ApplyCollectionConfigurationAsync("impronta-2")); Assert.AreEqual("impronta-2", await bed.Db.Settings.GetAsync(EngineSettingKeys.CollectionConfiguration)); Assert.AreEqual(1, (await bed.Db.Events.ListAsync(new EventQuery())).Count(e => e.Code == EngineEventCodes.CollectionConfigurationChanged)); } [TestMethod] public async Task FailuresAloneAreClearedWithTheirOwnLine() { await using var bed = await EngineTestBed.CreateAsync(Morning); await bed.Db.Settings.SetAsync(EngineSettingKeys.RecipeFailures, "ProfileCheck=2"); var host = new EngineHost(bed.Services, new ScriptedRunner(bed.Governor, bed.Clock)); await using (host) { Assert.IsTrue(await host.ApplyCollectionConfigurationAsync("impronta")); } Assert.IsNull(await bed.Db.Settings.GetAsync(EngineSettingKeys.RecipeFailures)); var line = (await bed.Db.Events.ListAsync(new EventQuery())).Single(e => e.Code == EngineEventCodes.CollectionConfigurationChanged); Assert.AreEqual("La configurazione della raccolta è cambiata: i problemi delle ricette contati finora non valgono più.", line.Message); } [TestMethod] public void TheEmbeddedFilesAreDescribedWithoutTheirContentAndFingerprinted() { var configuration = CollectionConfiguration.Describe("prova", ReaderVocabulary.LoadTestSite(), SiteMap.LoadTestSite()); Assert.IsFalse(configuration.VocabularyFile.IsOverride); Assert.IsFalse(configuration.MapFile.IsOverride); Assert.IsEmpty(configuration.Blocked); Assert.IsEmpty(configuration.Warnings); CollectionAssert.AreEqual( new[] { "Vocabolario dei lettori per «prova»: il file incorporato in Febe (lettori.prova.json), confermato.", "Mappa dei bersagli per «prova»: il file incorporato in Febe (bersagli.prova.json), tutte le voci compilate.", "Con questa configurazione tutte le ricette possono girare.", }, configuration.Lines.ToArray()); Assert.MatchesRegex("^[0-9a-f]{64}$", configuration.Fingerprint); Assert.AreEqual(configuration.Fingerprint, CollectionConfiguration.Describe("prova", ReaderVocabulary.LoadTestSite(), SiteMap.LoadTestSite()).Fingerprint); } [TestMethod] public void ACopyInTheDataFolderIsNamedAndOneThatHidesABetterEmbeddedFileIsAWarning() { using var folder = new TempFolder("configurazione"); var vocabularyText = SiteMap.ReadEmbeddedText("lettori.prova.json"); var mapText = SiteMap.ReadEmbeddedText("bersagli.prova.json"); var embedded = CollectionConfiguration.Describe("prova", ReaderVocabulary.LoadTestSite(), SiteMap.LoadTestSite()); // The same map with other line endings is the same configuration. var mapPath = folder.Combine("bersagli.prova.json"); File.WriteAllText(mapPath, mapText.Contains("\r\n", StringComparison.Ordinal) ? mapText.Replace("\r\n", "\n", StringComparison.Ordinal) : mapText.Replace("\n", "\r\n", StringComparison.Ordinal)); var sameMap = Loaded(folder, "prova"); Assert.IsTrue(sameMap.MapFile.IsOverride); Assert.AreEqual(mapPath, sameMap.MapFile.OverridePath); Assert.AreEqual(embedded.Fingerprint, sameMap.Fingerprint); Assert.IsEmpty(sameMap.Warnings); StringAssert.Contains(sameMap.Lines[1], $"la copia nella cartella dei dati ({mapPath})"); // A stale vocabulary, still a hypothesis, hides the confirmed embedded one: every recipe would be refused. var vocabularyPath = folder.Combine("lettori.prova.json"); File.WriteAllText(vocabularyPath, vocabularyText.Replace("\"confermato\"", "\"ipotesi\"", StringComparison.Ordinal)); var stale = Loaded(folder, "prova"); Assert.AreNotEqual(embedded.Fingerprint, stale.Fingerprint); CollectionAssert.AreEqual(Enum.GetValues(), stale.Blocked.ToArray()); Assert.HasCount(1, stale.Warnings); StringAssert.Contains(stale.Warnings[0], vocabularyPath); StringAssert.Contains(stale.Warnings[0], "è ancora un'ipotesi, mentre quello incorporato in Febe è confermato"); StringAssert.EndsWith(stale.Lines[0], ", ipotesi."); StringAssert.StartsWith(stale.Lines[2], "Con questa configurazione non possono girare 9 ricette su 9: Giro dei seguiti, "); // A map that lacks what the embedded one has leaves recipes still that could run. File.Delete(vocabularyPath); File.WriteAllText(mapPath, """{ "site": "prova", "pages": { "home": "/" } }"""); var poor = Loaded(folder, "prova"); CollectionAssert.Contains(poor.Blocked.ToArray(), RecipeKind.ProfileCheck); CollectionAssert.DoesNotContain(poor.Blocked.ToArray(), RecipeKind.FollowingSweep); Assert.HasCount(1, poor.Warnings); StringAssert.Contains(poor.Warnings[0], $"La mappa dei bersagli in {mapPath} lascia ferme ricette che con quella incorporata in Febe potrebbero girare ("); StringAssert.Contains(poor.Warnings[0], "Controllo del profilo"); // Never what is in the files. var note = ReaderVocabulary.LoadTestSite().Note!; foreach (var text in stale.Lines.Concat(stale.Warnings).Concat(poor.Lines).Concat(poor.Warnings)) { Assert.IsFalse(text.Contains(note, StringComparison.Ordinal), text); Assert.IsFalse(text.Contains("/profilo/", StringComparison.Ordinal), text); } } /// The files loaded as the collection pipeline loads them: the Data-folder copy when it exists. private static CollectionConfiguration Loaded(TempFolder folder, string site) { var vocabulary = ReaderVocabulary.LoadOrEmbedded(folder.Combine($"lettori.{site}.json"), $"lettori.{site}.json"); var map = SiteMap.LoadOrEmbedded(folder.Combine($"bersagli.{site}.json"), $"bersagli.{site}.json"); return CollectionConfiguration.Describe(site, vocabulary, map); } }