using System.Security.Cryptography; using Febe.Core.Domain; using Febe.Core.Import; using Febe.Core.Library; using Febe.Tests.Import.Support; using Febe.Tests.Support; namespace Febe.Tests.Import; /// «Tieni il più grande» when the source file is larger: it replaces the library file (rule 12, spec §9.3). [TestClass] public sealed class ImportReplacementTests { /// /// Two versions of the same photo in the source (the original and a smaller export), both paired with one smaller /// library file and both decided «Tieni il più grande»: only the largest takes its place, whatever the order of /// the files, and with «Sposta» the larger original is never lost. /// [TestMethod] [DataRow(ImportMode.Copy, true)] [DataRow(ImportMode.Move, true)] [DataRow(ImportMode.Copy, false)] [DataRow(ImportMode.Move, false)] public async Task TwoSourceFilesPairedWithTheSameLibraryFileLeaveOnlyTheLargest(ImportMode mode, bool largestFirst) { using var temp = new TempFolder("importa"); await using var bed = await ImportTestBed.CreateAsync(temp); var (marta, library) = await bed.AccountWithPhotoAsync("marta.demo", "60", ImportTestFiles.Resized(60, 800, 600, 400, 300, 70)); var largest = ImportTestFiles.Photo(60, 800, 600, 92); var export = ImportTestFiles.Resized(60, 800, 600, 600, 450, 80); var largestPath = bed.Write(largestFirst ? "marta.demo/1-originale.jpg" : "marta.demo/2-originale.jpg", largest); var exportPath = bed.Write(largestFirst ? "marta.demo/2-esportata.jpg" : "marta.demo/1-esportata.jpg", export); var run = await bed.AnalyzeAsync(); await bed.Review.SetModeAsync(run.Id, mode); var cases = await bed.Review.ListDuplicatesAsync(run.Id); Assert.HasCount(2, cases); Assert.IsTrue(cases.All(c => c.Existing.MediaId == library.Media[0].Id), "Both source files are paired with the library file."); Assert.AreEqual(2, await bed.Review.DecideAsync(run.Id, cases[0].Pair.Id, DuplicateDecision.KeepLarger, DecisionScope.AllPending)); var plan = await bed.Review.GetPlanAsync(run.Id); Assert.AreEqual(1, plan.ToReplace, "One library file, one replacement."); Assert.AreEqual(1, plan.ToImport); await bed.Copier.CopyAsync(run.Id); var media = (await bed.Library.Db.Media.GetAsync(library.Media[0].Id))!; var largestSha = Sha(largest); Assert.AreEqual(largestSha, media.Sha256, "The library keeps the largest version."); Assert.AreEqual((800, 600), (media.Width, media.Height)); var onDisk = bed.Library.Layout.FullPath(marta, media.RelativePath!); Assert.AreEqual(largestSha, Sha(File.ReadAllBytes(onDisk)), "The record points to a file that exists and holds the largest version."); var folder = bed.Library.Layout.AccountFolder(marta); Assert.HasCount(1, Directory.GetFiles(folder, "*.jpg", SearchOption.AllDirectories), "One version in the library: no leftovers."); var files = await bed.FilesAsync(run.Id); var winner = files.Single(f => f.Sha256 == largestSha); Assert.AreEqual(mode == ImportMode.Move ? ImportFileState.Moved : ImportFileState.Copied, winner.State, winner.LastError); StringAssert.StartsWith(winner.LastError, ImportText.NoteReplaced); Assert.AreEqual(media.RelativePath, winner.DestinationPath); var loser = files.Single(f => f.Sha256 == Sha(export)); Assert.AreEqual(ImportFileState.Skipped, loser.State, "The smaller export never takes the place of the original."); Assert.AreEqual(ImportText.NoteLargerInSource, loser.LastError); Assert.AreEqual(mode == ImportMode.Copy, File.Exists(largestPath), "With «Sposta» the original goes only once its copy is verified."); Assert.IsTrue(File.Exists(exportPath), "A skipped file is never deleted, not even with «Sposta»."); CollectionAssert.AreEqual(mode == ImportMode.Move ? new[] { largestPath } : Array.Empty(), bed.FileSystem.Deleted); var journal = await AccountJournal.ReadAsync(folder); Assert.AreEqual(media.RelativePath, journal.Lines.Last(l => l.Entry.Id == "60").Files!.Single().Path); // Running the copy again changes nothing. await bed.Copier.CopyAsync(run.Id); Assert.AreEqual(largestSha, (await bed.Library.Db.Media.GetAsync(media.Id))!.Sha256); Assert.HasCount(1, Directory.GetFiles(folder, "*.jpg", SearchOption.AllDirectories)); } /// /// The plan compares sizes before the copy starts; the copy looks again at the file in the library right before /// replacing it, and a larger file that arrived meanwhile stays. /// [TestMethod] public async Task AReplacementNeverRemovesALargerFileThatArrivedMeanwhile() { using var temp = new TempFolder("importa"); await using var bed = await ImportTestBed.CreateAsync(temp); var (marta, library) = await bed.AccountWithPhotoAsync("marta.demo", "62", ImportTestFiles.Resized(62, 800, 600, 400, 300, 70)); bed.Write("marta.demo/a-altra.jpg", ImportTestFiles.Photo(63)); var candidate = bed.Write("marta.demo/b-grande.jpg", ImportTestFiles.Photo(62, 800, 600, 92)); var run = await bed.AnalyzeAsync(); var pair = (await bed.Review.ListDuplicatesAsync(run.Id)).Single(); await bed.Review.DecideAsync(run.Id, pair.Pair.Id, DuplicateDecision.KeepLarger); Assert.AreEqual(1, (await bed.Review.GetPlanAsync(run.Id)).ToReplace); // Right after the first file, a larger version of the photo takes the place of the library file. var huge = ImportTestFiles.Photo(62, 1600, 1200, 92); var hugeRelative = "post/arrivata-nel-frattempo.jpg"; var hugePath = bed.Library.Layout.FullPath(marta, hugeRelative); var swapped = false; var progress = new SyncProgress(p => { if (swapped || p.Phase != ImportPhase.Copying || p.CurrentFile != "marta.demo/a-altra.jpg") { return; } swapped = true; Directory.CreateDirectory(Path.GetDirectoryName(hugePath)!); File.WriteAllBytes(hugePath, huge); bed.Library.Db.Media.MarkStoredAsync( library.Media[0].Id, new StoredFile(hugeRelative, huge.Length, Sha(huge), null, 1600, 1200, null, false), MediaState.Downloaded, bed.Library.Clock.Now).GetAwaiter().GetResult(); }); await bed.Copier.CopyAsync(run.Id, progress); Assert.IsTrue(swapped); var media = (await bed.Library.Db.Media.GetAsync(library.Media[0].Id))!; Assert.AreEqual(Sha(huge), media.Sha256, "The larger file stays in the library."); Assert.IsTrue(File.Exists(hugePath)); var file = await bed.FileAsync(run.Id, "marta.demo/b-grande.jpg"); Assert.AreEqual(ImportFileState.Skipped, file.State); Assert.AreEqual(ImportText.NoteLibraryLarger, file.LastError); Assert.IsTrue(File.Exists(candidate)); } /// /// An imported entry without Instagram id (with only a code, or with nothing but its file) whose file is replaced /// by a larger one: after losing the database the rebuild finds one entry with the new file, on every run. /// [TestMethod] [DataRow(false)] [DataRow(true)] public async Task AReplacedImportedFileIsOneEntryAfterARebuild(bool withCode) { using var temp = new TempFolder("importa"); await using var bed = await ImportTestBed.CreateAsync(temp); var marta = await bed.Library.Db.Accounts.AddAsync(Make.Account("marta.demo", "900")); var name = withCode ? "2026-10-10_18-22-05_DBx3kQ2_1.jpg" : "IMG_0001.jpg"; var small = bed.Write("marta.demo/" + name, ImportTestFiles.Resized(64, 800, 600, 400, 300, 70)); await bed.Copier.CopyAsync((await bed.AnalyzeAsync()).Id); var first = (await bed.Library.Db.Entries.ListByAccountAsync(marta.Id)).Single(); Assert.AreEqual(withCode ? "DBx3kQ2" : null, first.Code); Assert.IsNull(first.InstagramId); File.Delete(small); bed.Write("marta.demo/originali/" + name, ImportTestFiles.Photo(64, 800, 600, 92)); var run = await bed.AnalyzeAsync(); var pair = (await bed.Review.ListDuplicatesAsync(run.Id)).Single(); await bed.Review.DecideAsync(run.Id, pair.Pair.Id, DuplicateDecision.KeepLarger); await bed.Copier.CopyAsync(run.Id); var replaced = (await bed.Library.Db.Media.ListByEntryAsync(first.Id)).Single(); Assert.AreEqual((800, 600), (replaced.Width, replaced.Height), "Replaced by the larger file."); await bed.Library.LoseDatabaseAsync(); var rebuilder = new LibraryRebuilder(bed.Library.Db, bed.Library.Layout, bed.Library.Clock); var report = await rebuilder.RebuildAsync(); Assert.AreEqual(1, report.EntriesCreated, string.Join("; ", report.Notes)); Assert.AreEqual(0, report.FilesMissing, "The replaced file is not reported as missing."); var account = (await bed.Library.Db.Accounts.FindByInstagramIdAsync("900"))!; var entry = (await bed.Library.Db.Entries.ListByAccountAsync(account.Id)).Single(); Assert.AreEqual(replaced.RelativePath, (await bed.Library.Db.Media.ListByEntryAsync(entry.Id)).Single().RelativePath); Assert.AreEqual(0, (await rebuilder.RebuildAsync()).EntriesCreated, "A second run adds nothing."); Assert.HasCount(1, await bed.Library.Db.Entries.ListByAccountAsync(account.Id)); } private static string Sha(byte[] bytes) => Convert.ToHexStringLower(SHA256.HashData(bytes)); private sealed class SyncProgress(Action report) : IProgress { public void Report(ImportProgress value) => report(value); } }