using Febe.Core.Domain; using Febe.Core.Library; using Febe.Tests.Support; namespace Febe.Tests.Library; [TestClass] public sealed class LibraryIngestTests { [TestMethod] public async Task StoresFilesWithSchemeHashesThumbnailsAndJournal() { using var temp = new TempFolder(); await using var library = await TestLibrary.CreateAsync(temp); var account = await library.Db.Accounts.AddAsync(Make.Account("marta.demo", "100")); // 16:22:05 UTC on 10 October is 18:22:05 in Rome. var entry = Make.Entry(account.Id, "321", new DateTimeOffset(2026, 10, 10, 16, 22, 5, TimeSpan.Zero), caption: "Tramonto") with { Code = "DBx3kQ2" }; var saved = await library.SaveAsync(account, entry, [TestFile.Photo(1, 11), TestFile.Photo(2, 12, 1350, 1080), TestFile.Video(3)]); Assert.AreEqual(EntryState.Downloaded, saved.Entry.State); Assert.AreEqual("post/2026/2026-10-10_18-22-05_DBx3kQ2_1.jpg", saved.Media[0].RelativePath); Assert.AreEqual("post/2026/2026-10-10_18-22-05_DBx3kQ2_2.jpg", saved.Media[1].RelativePath); Assert.AreEqual("post/2026/2026-10-10_18-22-05_DBx3kQ2_3.mp4", saved.Media[2].RelativePath); var photoPath = library.Layout.FullPath(account, saved.Media[0].RelativePath!); Assert.AreEqual(entry.PublishedAt.UtcDateTime, File.GetLastWriteTimeUtc(photoPath), "La data del file è quella di pubblicazione."); Assert.IsNotNull(saved.Media[0].PerceptualHash); Assert.IsNull(saved.Media[2].PerceptualHash, "I video non hanno impronta visiva."); Assert.AreEqual(1350, saved.Media[1].Width); Assert.AreEqual(1080, saved.Media[1].Height); Assert.IsTrue(saved.Media[0].HasThumbnail); Assert.IsTrue(library.Thumbnails.Exists(saved.Media[0].Sha256!)); Assert.IsFalse(saved.Media[2].HasThumbnail, "Un video senza copertina mostra la tessera neutra."); Assert.AreEqual(2, library.Index.Count); var journal = await AccountJournal.ReadAsync(library.Layout.AccountFolder(account)); var line = journal.Lines.Single(); Assert.AreEqual(3, line.Files!.Count); Assert.AreEqual(saved.Media[2].Sha256, line.Files[2].Sha256); } [TestMethod] public async Task VideoCoversBecomeThumbnailsAndHighlightsUseTheirScheme() { using var temp = new TempFolder(); await using var library = await TestLibrary.CreateAsync(temp); var account = await library.Db.Accounts.AddAsync(Make.Account("marta.demo", "100")); var entry = Make.Entry(account.Id, "3391207", new DateTimeOffset(2025, 8, 2, 8, 11, 12, TimeSpan.Zero), EntryKind.Highlight); var added = await library.Db.Entries.AddAsync(entry, [Make.Video()]); var cover = new MemoryStream(TestFile.PhotoBytes(5, 1080, 1920)); var result = await library.Ingest.SaveMediaAsync(account, added.Stored.Entry, added.Stored.Media[0], new MediaContent { Content = new MemoryStream(new byte[] { 1, 2, 3 }), Extension = "mp4", Cover = cover }, "Estate 2025"); Assert.AreEqual(MediaSaveOutcome.Stored, result.Outcome); Assert.AreEqual("evidenza/Estate 2025/2025-08-02_10-11-12_3391207_1.mp4", result.File!.RelativePath); Assert.IsTrue(result.File.HasThumbnail); Assert.IsTrue(cover.CanRead, "Lo stream della copertina resta a chi lo ha passato."); } [TestMethod] public async Task AnIdenticalFileInTheSameAccountIsDiscarded() { using var temp = new TempFolder(); await using var library = await TestLibrary.CreateAsync(temp); var account = await library.Db.Accounts.AddAsync(Make.Account("marta.demo", "100")); var bytes = TestFile.PhotoBytes(21); var imported = await library.SaveAsync(account, Make.Entry(account.Id, "x", Make.T0) with { InstagramId = null, Provenance = Provenance.Imported }, [new TestFile(Make.Photo(1), bytes, ".jpg")]); Assert.AreEqual(EntryState.Imported, imported.Entry.State); var downloaded = await library.Db.Entries.AddAsync(Make.Entry(account.Id, "555", Make.T0), [Make.Photo()]); var result = await library.Ingest.SaveMediaAsync(account, downloaded.Stored.Entry, downloaded.Stored.Media[0], new MediaContent { Content = new MemoryStream(bytes), Extension = ".jpg" }); Assert.AreEqual(MediaSaveOutcome.IdenticalInAccount, result.Outcome); Assert.AreEqual(imported.Media[0].Id, result.Identical!.Media.Id); Assert.HasCount(1, Directory.GetFiles(library.Layout.AccountFolder(account), "*.jpg", SearchOption.AllDirectories)); Assert.AreEqual(MediaState.ToDownload, (await library.Db.Media.GetAsync(downloaded.Stored.Media[0].Id))!.State); // The same bytes in another account are a separate copy (spec §8.3). var other = await library.Db.Accounts.AddAsync(Make.Account("luca.demo", "200")); var tagged = await library.SaveAsync(other, Make.Entry(other.Id, "555", Make.T0, EntryKind.Tagged), [new TestFile(Make.Photo(1), bytes, ".jpg")]); Assert.AreEqual(EntryState.Downloaded, tagged.Entry.State); } [TestMethod] public async Task ASimilarPhotoIsKeptAndPairedForAlberto() { using var temp = new TempFolder(); await using var library = await TestLibrary.CreateAsync(temp); var account = await library.Db.Accounts.AddAsync(Make.Account("marta.demo", "100")); using var scene = TestImages.Scene(31, 1200, 900); var original = await library.SaveAsync(account, Make.Entry(account.Id, "1", Make.T0), [new TestFile(Make.Photo(1), TestImages.Jpeg(scene, 92), ".jpg")]); using var smaller = TestImages.Resize(scene, 600, 450); var copy = await library.SaveAsync(account, Make.Entry(account.Id, "2", Make.T0.AddDays(1)), [new TestFile(Make.Photo(1), TestImages.Jpeg(smaller, 60), ".jpg")]); Assert.AreEqual(EntryState.Downloaded, copy.Entry.State, "Restano entrambi: nessuna sostituzione automatica."); var pair = (await library.Db.Duplicates.ListPendingAsync()).Single(); Assert.AreEqual(original.Media[0].Id, pair.ExistingMediaId); Assert.AreEqual(copy.Media[0].Id, pair.CandidateMediaId); Assert.AreEqual(DuplicateReason.VisuallySimilar, pair.Reason); Assert.IsTrue(File.Exists(library.Layout.FullPath(account, original.Media[0].RelativePath!))); Assert.IsTrue(File.Exists(library.Layout.FullPath(account, copy.Media[0].RelativePath!))); } [TestMethod] public async Task ProfilePicturesAreNamedAfterTheirHashAndTooLargeVideosSkipped() { using var temp = new TempFolder(); await using var library = await TestLibrary.CreateAsync(temp); var account = await library.Db.Accounts.AddAsync(Make.Account("marta.demo", "100")); var bytes = TestFile.PhotoBytes(40, 320, 320); var saved = await library.SaveAsync(account, Make.Entry(account.Id, "pp1", Make.T0, EntryKind.ProfilePicture), [new TestFile(Make.Photo(1), bytes, ".jpg")]); var sha = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(bytes)); Assert.AreEqual($"profilo/2026-10-13_10-00-00_{sha[..8]}_1.jpg", saved.Media[0].RelativePath); // With a general scheme that puts everything in the account folder the pictures keep their own folder (#38). library.Ingest.Apply(LibraryNaming.Default with { General = NamingScheme.Parse(@"{account}\{data}_{codice}_{n}", NamingSchemeUse.General) }, true); var other = TestFile.PhotoBytes(41, 320, 320); var second = await library.SaveAsync(account, Make.Entry(account.Id, "pp2", Make.T0.AddDays(1), EntryKind.ProfilePicture), [new TestFile(Make.Photo(1), other, ".jpg")]); var otherSha = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(other)); Assert.AreEqual($"profilo/2026-10-14_10-00-00_{otherSha[..8]}_1.jpg", second.Media[0].RelativePath); var post = await library.SaveAsync(account, Make.Entry(account.Id, "p1", Make.T0) with { Code = "AbC1" }, [new TestFile(Make.Photo(1), TestFile.PhotoBytes(42, 320, 320), ".jpg")]); Assert.AreEqual("2026-10-13_AbC1_1.jpg", post.Media[0].RelativePath); var video = await library.Db.Entries.AddAsync(Make.Entry(account.Id, "big", Make.T0), [Make.Video()]); await Assert.ThrowsExactlyAsync(() => library.Ingest.SaveMediaAsync( account, video.Stored.Entry, video.Stored.Media[0], new MediaContent { Content = new MemoryStream(new byte[10_000]), Extension = ".mp4", MaxBytes = 1000 })); Assert.IsEmpty(Directory.GetFiles(library.Layout.AccountFolder(account), "*.mp4", SearchOption.AllDirectories)); } }