using System.Text.Json.Nodes; using Febe.Core.Collection.Readers; using Febe.Core.Collection.Recipes; using Febe.Core.Domain; using Febe.Core.Engine; using Febe.Data; using Febe.Tests.Collection.Support; using Febe.Tests.Support; namespace Febe.Tests.Collection; /// /// The collection writer: files of a series in their declared places, fresh addresses for files that expired or /// failed (posts, stories still on offer, highlight items, profile pictures). /// [TestClass] public sealed class CollectionWriterTests { private static readonly ShapeReader Reader = new(ReaderVocabulary.LoadTestSite()); private static readonly ScriptedAccount Marta = new("1001", "marta.prova"); [TestMethod] public async Task ASeriesChildWithoutVersionsKeepsItsPlaceAndPostSingoloFillsIt() { await using var bed = await Bed.CreateAsync(); var partial = Item(new ScriptedPost("500", Make.T0.AddDays(-1)) { Children = [("c1", Url("c1")), ("c2", null), ("c3", Url("c3"))] }); Assert.IsFalse(partial.IsComplete); CollectionAssert.AreEqual(new[] { 1, 3 }, ReadMapping.ToMedia(partial, Make.T0).Select(m => m.Position).ToArray(), "The unreadable second child leaves its place free."); var saved = await bed.Writer.SaveAsync(bed.Account, partial, EntryKind.Post, null, CancellationToken.None); Assert.AreEqual(SaveOutcome.Created, saved.Outcome); Assert.IsTrue((await bed.Db.Tasks.ListPendingAsync(10)).Any(t => t.Kind == EngineTaskKind.SinglePost), "«Post singolo» completes it."); var full = Item(new ScriptedPost("500", Make.T0.AddDays(-1)) { Children = [("c1", Url("c1", 2)), ("c2", Url("c2", 2)), ("c3", Url("c3", 2))] }); var changed = await bed.Writer.CompleteAsync(saved.Entry!, full, bed.Account, CancellationToken.None); Assert.AreEqual(1, changed, "Only the missing child is added."); var files = await bed.Db.Media.ListByEntryAsync(saved.Entry!.Id); CollectionAssert.AreEqual(new[] { 1, 2, 3 }, files.Select(f => f.Position).ToArray()); CollectionAssert.AreEqual(new[] { "c1", "c2", "c3" }, files.Select(f => f.InstagramId).ToArray(), "Each child in its place, the third never twice."); Assert.AreEqual(Url("c2", 2), files[1].SourceUrl); Assert.AreEqual(Url("c3"), files[2].SourceUrl, "A file still waiting keeps its address."); } [TestMethod] public async Task AFreshAddressGoesToTheFileWithTheSameIdentifierNotTheSamePlace() { await using var bed = await Bed.CreateAsync(); // Files recorded in consecutive places although the second child was missing (records written before). var entry = await bed.Db.Entries.AddAsync(Make.Entry(bed.Account.Id, "600", Make.T0.AddDays(-2)), [ Make.Photo(1, MediaState.Expired) with { InstagramId = "c1" }, Make.Photo(2, MediaState.Expired) with { InstagramId = "c3" }, ]); var fresh = Item(new ScriptedPost("600", Make.T0.AddDays(-2)) { Children = [("c1", Url("c1", 3)), ("c2", Url("c2", 3)), ("c3", Url("c3", 3))] }); Assert.AreEqual(SaveOutcome.Known, (await bed.Writer.SaveAsync(bed.Account, fresh, EntryKind.Post, null, CancellationToken.None)).Outcome); var files = await bed.Db.Media.ListByEntryAsync(entry.Stored.Entry.Id); Assert.AreEqual(Url("c1", 3), files[0].SourceUrl); Assert.AreEqual(Url("c3", 3), files[1].SourceUrl, "The record of the third child takes the third child's address."); Assert.IsTrue(files.All(f => f.State == MediaState.ToDownload)); var changed = await bed.Writer.CompleteAsync(entry.Stored.Entry, fresh, bed.Account, CancellationToken.None); Assert.AreEqual(1, changed); var completed = await bed.Db.Media.ListByEntryAsync(entry.Stored.Entry.Id); CollectionAssert.AreEquivalent(new[] { "c1", "c2", "c3" }, completed.Select(f => f.InstagramId).ToArray(), "The second child is added in a free place, never lost."); } [TestMethod] public async Task AFailedFileTakesTheFreshAddressOfANewReadingWhileItIsStillOnOffer() { await using var bed = await Bed.CreateAsync(); var post = await AddFailedAsync(bed, "p1", EntryKind.Post, attempts: 5); var broken = await AddFailedAsync(bed, "p9", EntryKind.Post, attempts: CollectionWriter.RevivalAttemptsLimit); var story = await AddFailedAsync(bed, "s2", EntryKind.Story, attempts: 1, expiresAt: Make.T0.AddHours(10)); var expired = await AddFailedAsync(bed, "s3", EntryKind.Story, attempts: 1, expiresAt: Make.T0.AddHours(-1)); foreach (var (id, kind) in new[] { ("p1", EntryKind.Post), ("p9", EntryKind.Post), ("s2", EntryKind.Story), ("s3", EntryKind.Story) }) { var item = Item(new ScriptedPost(id, Make.T0.AddHours(-5)) { Url = Url(id, 4) }); Assert.AreEqual(SaveOutcome.Known, (await bed.Writer.SaveAsync(bed.Account, item, kind, null, CancellationToken.None)).Outcome); } var revived = (await bed.Db.Media.ListByEntryAsync(post)).Single(); Assert.AreEqual(MediaState.ToDownload, revived.State, "A network outage does not lose a post for good."); Assert.AreEqual(Url("p1", 4), revived.SourceUrl); Assert.AreEqual(MediaState.ToDownload, (await bed.Db.Media.ListByEntryAsync(story)).Single().State, "A story shown again before it expires."); Assert.AreEqual(MediaState.Failed, (await bed.Db.Media.ListByEntryAsync(expired)).Single().State, "No recovery after the expiry (§6.5 point 5)."); Assert.AreEqual(MediaState.Failed, (await bed.Db.Media.ListByEntryAsync(broken)).Single().State, "A file broken for good is not tried at every check."); } [TestMethod] public async Task AHighlightItemThatWasAFailedStoryGivesTheStoryItsFreshAddress() { await using var bed = await Bed.CreateAsync(); var story = await AddFailedAsync(bed, "s1", EntryKind.Story, attempts: 1, expiresAt: Make.T0.AddDays(-3)); var item = await bed.Db.Entries.AddAsync(Make.Entry(bed.Account.Id, "e2", Make.T0.AddDays(-20), EntryKind.Highlight), [Make.Photo(1, MediaState.Expired)]); var highlight = await bed.Db.Highlights.UpsertAsync(new Highlight { AccountId = bed.Account.Id, InstagramId = "h1", Title = "Estate", DiscoveredAt = Make.T0 }); var shown = Reader.Read(new JsonObject { ["raccolta"] = new JsonObject { ["numero"] = "h1", ["titolo"] = "Estate", ["autore"] = ScriptedSite.Lite(Marta), ["contenuti"] = new JsonArray( ScriptedSite.Post(Marta, new ScriptedPost("s1", Make.T0.AddDays(-4)) { Url = Url("s1", 5) }), ScriptedSite.Post(Marta, new ScriptedPost("e2", Make.T0.AddDays(-20)) { Url = Url("e2", 5) })), }, }.ToJsonString()).Contents; var (linked, created) = await bed.Writer.LinkHighlightItemAsync(bed.Account, highlight, shown.Single(c => c.Id == "s1"), null, CancellationToken.None); Assert.AreEqual(story, linked!.Id, "The story is referenced (H9), not downloaded as a new entry…"); Assert.IsFalse(created); var storyFile = (await bed.Db.Media.ListByEntryAsync(story)).Single(); Assert.AreEqual(MediaState.ToDownload, storyFile.State, "…but its file, never saved, now comes from the collection."); Assert.AreEqual(Url("s1", 5), storyFile.SourceUrl); await bed.Writer.LinkHighlightItemAsync(bed.Account, highlight, shown.Single(c => c.Id == "e2"), null, CancellationToken.None); var itemFile = (await bed.Db.Media.ListByEntryAsync(item.Stored.Entry.Id)).Single(); Assert.AreEqual(MediaState.ToDownload, itemFile.State, "An expired collection item is renewed when the collection is opened again."); Assert.AreEqual(Url("e2", 5), itemFile.SourceUrl); } [TestMethod] public async Task AKnownProfilePictureWhoseFileExpiredTakesTheFreshAddress() { await using var bed = await Bed.CreateAsync(); ProfileInfo Profile(string signature) => new() { Id = "1001", Username = "marta.prova", Picture = new FileVersion($"https://cdn.esempio/v/ritratto.jpg?firma={signature}", 320, 320, null), }; var first = await bed.Writer.SaveProfilePictureAsync(bed.Account, Profile("uno"), CancellationToken.None); Assert.AreEqual(SaveOutcome.Created, first.Outcome); var file = (await bed.Db.Media.ListByEntryAsync(first.Entry!.Id)).Single(); await bed.Db.Media.MarkFailedAsync(file.Id, MediaState.Expired, "Indirizzo scaduto.", Make.T0); var second = await bed.Writer.SaveProfilePictureAsync(bed.Account, Profile("due"), CancellationToken.None); Assert.AreEqual(SaveOutcome.Known, second.Outcome, "Same picture, new signature."); var renewed = (await bed.Db.Media.ListByEntryAsync(first.Entry.Id)).Single(); Assert.AreEqual(MediaState.ToDownload, renewed.State); Assert.AreEqual("https://cdn.esempio/v/ritratto.jpg?firma=due", renewed.SourceUrl); } private static string Url(string id, int version = 1) => $"https://cdn.esempio/m/{id}-{version}.jpg?firma=x"; private static ContentItem Item(ScriptedPost post) => Reader.Read(new JsonObject { ["contenuto"] = ScriptedSite.Post(Marta, post) }.ToJsonString()).Contents.Single(c => c.Id == post.Id); private static async Task AddFailedAsync(Bed bed, string id, EntryKind kind, int attempts, DateTimeOffset? expiresAt = null) { var entry = Make.Entry(bed.Account.Id, id, Make.T0.AddHours(-5), kind) with { ExpiresAt = expiresAt }; var added = await bed.Db.Entries.AddAsync(entry, [Make.Photo(1, MediaState.Failed) with { Attempts = attempts, LastError = "Errore di rete." }]); return added.Stored.Entry.Id; } private sealed class Bed : IAsyncDisposable { private Bed(TempFolder temp, FebeDatabase db, Account account) { Temp = temp; Db = db; Account = account; Writer = new CollectionWriter(db, new ManualClock(Make.T0)); } public TempFolder Temp { get; } public FebeDatabase Db { get; } public Account Account { get; } public CollectionWriter Writer { get; } public static async Task CreateAsync() { var temp = new TempFolder("scrittore"); var db = await TestDatabase.OpenAsync(temp); var account = await db.Accounts.AddAsync(Make.Account("marta.prova", "1001")); return new Bed(temp, db, account); } public async ValueTask DisposeAsync() { await Db.DisposeAsync(); Temp.Dispose(); } } }