using Febe.Core.Domain; using Febe.Core.Library; using Febe.Tests.Support; namespace Febe.Tests.Library; /// /// «Ricostruisci il database dalla libreria» (spec §8.5) on libraries that changed after their files were saved: files /// removed or replaced by Alberto's choices, files that cannot be read. The rebuild respects the choices, never stops /// on one file, and a second run adds nothing. /// [TestClass] public sealed class LibraryRebuildRecoveryTests { /// One file of a series removed by «Tieni quello in libreria» stays removed, not «file mancante». [TestMethod] public async Task AFileOfASeriesRemovedByADuplicateDecisionStaysRemoved() { using var temp = new TempFolder("ricostruzione"); await using var lib = await TestLibrary.CreateAsync(temp); var account = await lib.Db.Accounts.AddAsync(Make.Account("doppie.prova", "500")); await lib.SaveAsync(account, Make.Entry(account.Id, "100", Make.T0.AddDays(-3)), [TestFile.Photo(1, 41, 1080, 1350)]); var series = await lib.SaveAsync(account, Make.Entry(account.Id, "200", Make.T0), [TestFile.Photo(1, 41, 720, 900), TestFile.Photo(2, 77, 1080, 1080)]); var review = new DownloadDuplicateReview(lib.Db, lib.Layout, lib.Journal, lib.Clock, lib.Thumbnails, lib.Index); var pair = (await review.ListAsync()).Single(c => c.Candidate.EntryId == series.Entry.Id); Assert.AreEqual(1, (await review.DecideAsync(pair.Pair.Id, DuplicateDecision.KeepExisting)).FilesRemoved); var before = (await lib.Db.Entries.GetWithMediaAsync(series.Entry.Id))!; await lib.LoseDatabaseAsync(); var rebuilder = new LibraryRebuilder(lib.Db, lib.Layout, lib.Clock, lib.Thumbnails); var report = await rebuilder.RebuildAsync(); Assert.AreEqual(0, report.FilesMissing, string.Join("; ", report.Notes)); var accountAgain = (await lib.Db.Accounts.FindByInstagramIdAsync("500"))!; var entry = (await lib.Db.Entries.FindAsync(accountAgain.Id, EntryKind.Post, "200"))!; var media = await lib.Db.Media.ListByEntryAsync(entry.Id); Assert.AreEqual(MediaState.Deleted, media.Single(m => m.Position == 1).State, "Alberto's removal is respected: never looked for or downloaded again."); Assert.AreEqual(MediaState.Downloaded, media.Single(m => m.Position == 2).State); Assert.AreEqual(before.Media.Single(m => m.Position == 2).RelativePath, media.Single(m => m.Position == 2).RelativePath); Assert.AreEqual(before.Entry.State, entry.State); var again = await rebuilder.RebuildAsync(); Assert.AreEqual(0, again.EntriesCreated); } /// /// A journal written before replacements closed the old file: an imported entry without identifier appears twice, /// once with the file that was replaced. Only the entry with its file comes back, on every run. /// [TestMethod] public async Task AnEntryWithoutIdentifierWhoseFileWasReplacedComesBackOnce() { using var temp = new TempFolder("ricostruzione"); await using var lib = await TestLibrary.CreateAsync(temp); var account = await lib.Db.Accounts.AddAsync(Make.Account("marta.demo", "600")); var folder = lib.Layout.AccountFolder(account); var bytes = TestFile.PhotoBytes(5, 800, 600); Directory.CreateDirectory(Path.Combine(folder, "importati")); await File.WriteAllBytesAsync(Path.Combine(folder, "importati", "IMG_1 (2).jpg"), bytes); var entry = new Entry { AccountId = account.Id, Kind = EntryKind.Post, PublishedAt = Make.T0.AddDays(-400), Provenance = Provenance.Imported, DiscoveredAt = Make.T0, }; Media Imported(string path, string sha) => Make.Photo() with { State = MediaState.Imported, RelativePath = path, Sha256 = sha, Bytes = 100 }; await lib.Journal.AppendSavedAsync(folder, account, entry, [Imported("importati/IMG_1.jpg", Make.Sha(1))]); await lib.Journal.AppendSavedAsync(folder, account, entry, [Imported("importati/IMG_1 (2).jpg", Make.Sha(2))]); await lib.LoseDatabaseAsync(); var rebuilder = new LibraryRebuilder(lib.Db, lib.Layout, lib.Clock); var report = await rebuilder.RebuildAsync(); Assert.AreEqual(1, report.EntriesCreated); Assert.AreEqual(1, report.FilesMissing, "The old file is reported..."); Assert.IsTrue(report.Notes.Any(n => n.Contains("importati/IMG_1.jpg", StringComparison.Ordinal) && n.Contains("non ripristinato", StringComparison.Ordinal))); var accountAgain = (await lib.Db.Accounts.FindByInstagramIdAsync("600"))!; var entries = await lib.Db.Entries.ListByAccountAsync(accountAgain.Id); Assert.HasCount(1, entries, "...but not restored as an entry of its own."); Assert.AreEqual("importati/IMG_1 (2).jpg", (await lib.Db.Media.ListByEntryAsync(entries[0].Id)).Single().RelativePath); var again = await rebuilder.RebuildAsync(); Assert.AreEqual(0, again.EntriesCreated, "A second run adds nothing."); Assert.HasCount(1, await lib.Db.Entries.ListByAccountAsync(accountAgain.Id)); } /// A remembered deletion of an imported entry without identifier is not added again by every run. [TestMethod] public async Task ARememberedDeletionWithoutIdentifierIsRestoredOnce() { using var temp = new TempFolder("ricostruzione"); await using var lib = await TestLibrary.CreateAsync(temp); var account = await lib.Db.Accounts.AddAsync(Make.Account("marta.demo", "700")); var imported = await lib.SaveAsync(account, Make.Entry(account.Id, "x", Make.T0.AddDays(-300)) with { InstagramId = null, Code = null, Provenance = Provenance.Imported }, [TestFile.Photo(1, 9)]); await new LibraryDeletion(lib.Db, lib.Layout, lib.Journal, lib.Clock).DeleteAsync(imported.Entry.Id, DeletionMode.Remember); await lib.LoseDatabaseAsync(); var rebuilder = new LibraryRebuilder(lib.Db, lib.Layout, lib.Clock); Assert.AreEqual(1, (await rebuilder.RebuildAsync()).EntriesDeleted); var again = await rebuilder.RebuildAsync(); Assert.AreEqual(0, again.EntriesCreated); var accountAgain = (await lib.Db.Accounts.FindByInstagramIdAsync("700"))!; var entries = await lib.Db.Entries.ListByAccountAsync(accountAgain.Id); Assert.HasCount(1, entries); Assert.AreEqual(EntryState.Deleted, entries[0].State); Assert.AreEqual(imported.Media[0].Sha256, (await lib.Db.Media.ListByEntryAsync(entries[0].Id)).Single().Sha256, "The SHA-256 is remembered: an import does not bring it back."); } /// /// A file that cannot be read (here: held exclusively by another program) and an unreadable journal do not stop /// the rebuild of everything else; the file stays linked as the journal recorded it. /// [TestMethod] public async Task AnUnreadableFileOrJournalDoesNotStopTheRebuild() { using var temp = new TempFolder("ricostruzione"); await using var lib = await TestLibrary.CreateAsync(temp); var marta = await lib.Db.Accounts.AddAsync(Make.Account("marta.demo", "800")); var luca = await lib.Db.Accounts.AddAsync(Make.Account("luca.demo", "801")); var zoe = await lib.Db.Accounts.AddAsync(Make.Account("zoe.demo", "802")); var locked = await lib.SaveAsync(marta, Make.Entry(marta.Id, "a1", Make.T0.AddDays(-2)), [TestFile.Photo(1, 11)]); var fine = await lib.SaveAsync(marta, Make.Entry(marta.Id, "a2", Make.T0.AddDays(-1)), [TestFile.Photo(1, 12)]); await lib.SaveAsync(luca, Make.Entry(luca.Id, "b1", Make.T0), [TestFile.Photo(1, 13)]); await lib.SaveAsync(zoe, Make.Entry(zoe.Id, "c1", Make.T0), [TestFile.Photo(1, 14)]); await lib.LoseDatabaseAsync(); var lockedPath = lib.Layout.FullPath(marta, locked.Media[0].RelativePath!); var report = await WhileHeldAsync( [lockedPath, AccountJournal.PathFor(lib.Layout.AccountFolder(luca))], () => new LibraryRebuilder(lib.Db, lib.Layout, lib.Clock, lib.Thumbnails).RebuildAsync()); Assert.AreEqual(1, report.FilesUnreadable, string.Join("; ", report.Notes)); Assert.AreEqual(2, report.FilesVerified, "The other files of the folder and of the following folders are verified."); Assert.IsTrue(report.Notes.Any(n => n.Contains("luca.demo", StringComparison.Ordinal) && n.Contains("_febe.jsonl", StringComparison.Ordinal))); var martaAgain = (await lib.Db.Accounts.FindByInstagramIdAsync("800"))!; var restored = (await lib.Db.Media.ListByEntryAsync((await lib.Db.Entries.FindAsync(martaAgain.Id, EntryKind.Post, "a1"))!.Id)).Single(); Assert.AreEqual(MediaState.Downloaded, restored.State, "The file is where the journal says: linked again, not to be downloaded."); Assert.AreEqual(locked.Media[0].RelativePath, restored.RelativePath); Assert.AreEqual(locked.Media[0].Sha256, restored.Sha256); StringAssert.Contains(restored.LastError, "illeggibile"); Assert.IsNotNull(await lib.Db.Entries.FindAsync(martaAgain.Id, EntryKind.Post, "a2")); Assert.IsNotNull(await lib.Db.Accounts.FindByInstagramIdAsync("802"), "The folders after the unreadable one are rebuilt too."); Assert.IsNull(await lib.Db.Accounts.FindByInstagramIdAsync("801"), "The folder with the unreadable journal waits for the next run."); var later = await new LibraryRebuilder(lib.Db, lib.Layout, lib.Clock, lib.Thumbnails).RebuildAsync(); Assert.AreEqual(1, later.EntriesCreated, "Once readable, the skipped folder is rebuilt."); _ = fine; } /// Runs while other handles hold the files with no sharing at all. private static async Task WhileHeldAsync(IReadOnlyList paths, Func> action) { var handles = paths.Select(p => new FileStream(p, FileMode.Open, FileAccess.ReadWrite, FileShare.None)).ToList(); try { return await action(); } finally { foreach (var handle in handles) { await handle.DisposeAsync(); } } } }