using Febe.Core.Domain; using Febe.Core.Storage; using Febe.Data; using Febe.Tests.Support; namespace Febe.Tests.Data; /// /// «Tutte le foto» (spec §4.6): one tile per file of the library, in the order of the entries and then of the series, /// with keyset paging that may stop and start again inside a post. /// [TestClass] public sealed class LibraryFilesTests { [TestMethod] public async Task EveryFileOfTheLibraryIsATileInTheOrderOfTheEntriesAndOfTheSeries() { using var temp = new TempFolder(); await using var db = await TestDatabase.OpenAsync(temp); var (carousel, single, partial, story) = await FillAsync(db); var page = await db.Entries.QueryLibraryAsync(new LibraryQuery { Mode = MosaicMode.Files }); var keys = page.Tiles.Select(t => (t.EntryId, t.FilePosition)).ToList(); CollectionAssert.AreEqual( new (long, int?)[] { (story, 1), (carousel, 1), (carousel, 2), (carousel, 3), (partial, 1), (partial, 3), (single, 1) }, keys, "Newest entry first, each by position; the file still to download of the partial post is not a tile."); Assert.IsNull(page.Older); Assert.IsNull(page.Newer); // A file tile describes that file, and keeps the count of the files of its post («2/3»). var second = page.Tiles[2]; Assert.AreEqual(3, second.MediaCount); Assert.AreEqual(MediaKind.Video, second.CoverKind); Assert.AreEqual(Make.Sha(12), second.CoverSha256); Assert.AreEqual(TimeSpan.FromSeconds(15), second.CoverDuration); Assert.AreEqual(3, page.Tiles[5].FilePosition); Assert.AreEqual(3, page.Tiles[5].MediaCount); // The grouped mosaic is what it was: one tile per entry. var grouped = await db.Entries.QueryLibraryAsync(new LibraryQuery()); CollectionAssert.AreEqual(new[] { story, carousel, partial, single }, grouped.Tiles.Select(t => t.EntryId).ToArray()); Assert.IsTrue(grouped.Tiles.All(t => t.FilePosition is null)); } [TestMethod] public async Task WindowsOfTwoWalkEveryFileOnceInBothDirectionsAlsoFromInsideAPost() { using var temp = new TempFolder(); await using var db = await TestDatabase.OpenAsync(temp); await FillAsync(db); var all = (await db.Entries.QueryLibraryAsync(new LibraryQuery { Mode = MosaicMode.Files })).Tiles.Select(Key).ToList(); // Older, two at a time from the top: the second window starts in the middle of the carousel. var walked = new List(); LibraryCursor? cursor = null; var windows = 0; do { var page = await db.Entries.QueryLibraryAsync(new LibraryQuery { Mode = MosaicMode.Files, Limit = 2, Cursor = cursor }); walked.AddRange(page.Tiles.Select(Key)); Assert.AreEqual(windows > 0, page.Newer is not null, "a window after the first has something newer"); cursor = page.Older; windows++; } while (cursor is not null); CollectionAssert.AreEqual(all, walked); Assert.AreEqual(4, windows); // Newer, two at a time from the last file, back to the top. var last = (await db.Entries.QueryLibraryAsync(new LibraryQuery { Mode = MosaicMode.Files, Limit = 1000 })).Tiles[^1]; cursor = new LibraryCursor(last.PublishedAt, last.EntryId, last.FilePosition!.Value); var back = new List(); while (cursor is not null) { var page = await db.Entries.QueryLibraryAsync(new LibraryQuery { Mode = MosaicMode.Files, Limit = 2, Cursor = cursor, Direction = LibraryDirection.Newer }); Assert.IsNotNull(page.Older, "a window newer than a file has that file after it"); back.InsertRange(0, page.Tiles.Select(Key)); cursor = page.Newer; } CollectionAssert.AreEqual(all.Take(all.Count - 1).ToList(), back); } [TestMethod] public async Task FiltersCountsAndDaysCountTheFiles() { using var temp = new TempFolder(); await using var db = await TestDatabase.OpenAsync(temp); var (carousel, _, _, _) = await FillAsync(db); // «Video» shows only the videos, «Foto» only the photos (of posts, as in the grouped mosaic). var videos = await db.Entries.QueryLibraryAsync(new LibraryQuery { Mode = MosaicMode.Files, Filter = new LibraryFilter { Kind = LibraryKindFilter.Videos } }); CollectionAssert.AreEqual(new[] { $"{carousel}.2" }, videos.Tiles.Select(Key).ToArray()); var photos = await db.Entries.QueryLibraryAsync(new LibraryQuery { Mode = MosaicMode.Files, Filter = new LibraryFilter { Kind = LibraryKindFilter.Photos } }); Assert.HasCount(5, photos.Tiles); Assert.IsTrue(photos.Tiles.All(t => t.CoverKind == MediaKind.Photo)); Assert.AreEqual(7, await FileTotalAsync(db, new LibraryFilter())); Assert.AreEqual(1, await FileTotalAsync(db, new LibraryFilter { Kind = LibraryKindFilter.Videos })); Assert.AreEqual(5, await FileTotalAsync(db, new LibraryFilter { Kind = LibraryKindFilter.Photos })); Assert.AreEqual(1, await FileTotalAsync(db, new LibraryFilter { Kind = LibraryKindFilter.Stories })); Assert.AreEqual(new LibraryTotals(4, 1), await db.Entries.GetLibraryTotalsAsync(new LibraryFilter()), "the totals stay entries"); var days = await db.Entries.CountLibraryByDayAsync(new LibraryFilter(), TestZones.Rome, MosaicMode.Files); CollectionAssert.AreEqual(new[] { new DayCount(new DateOnly(2026, 10, 13), 6), new DayCount(new DateOnly(2026, 10, 12), 1) }, days.ToArray()); var entryDays = await db.Entries.CountLibraryByDayAsync(new LibraryFilter(), TestZones.Rome); CollectionAssert.AreEqual(new[] { new DayCount(new DateOnly(2026, 10, 13), 3), new DayCount(new DateOnly(2026, 10, 12), 1) }, entryDays.ToArray()); // With a filter on accounts the files are counted entry by entry: the same numbers. var accountId = (await db.Entries.GetAsync(carousel))!.AccountId; var other = await db.Accounts.AddAsync(Make.Account("luca.demo")); await db.Entries.AddAsync(Make.Entry(other.Id, "o", new DateTimeOffset(2026, 10, 13, 8, 0, 0, TimeSpan.Zero)), [Make.Stored(Make.Photo(), Make.Sha(41), "o/o.jpg")]); var mine = new LibraryFilter { AccountIds = [accountId] }; Assert.AreEqual(7, await FileTotalAsync(db, mine)); Assert.AreEqual(8, await FileTotalAsync(db, new LibraryFilter())); CollectionAssert.AreEqual(days.ToArray(), (await db.Entries.CountLibraryByDayAsync(mine, TestZones.Rome, MosaicMode.Files)).ToArray()); Assert.AreEqual(8, (await db.Entries.CountLibraryByDayAsync(new LibraryFilter(), TestZones.Rome, MosaicMode.Files)).Sum(d => d.Count)); Assert.AreEqual(1, await FileTotalAsync(db, mine with { Kind = LibraryKindFilter.Videos })); // A day whose files are all of another kind has no row in «Video». var videoDays = await db.Entries.CountLibraryByDayAsync(new LibraryFilter { Kind = LibraryKindFilter.Videos }, TestZones.Rome, MosaicMode.Files); CollectionAssert.AreEqual(new[] { new DayCount(new DateOnly(2026, 10, 13), 1) }, videoDays.ToArray()); // The cursor at the end of a day (what the mosaic asks first for a day) starts with that day's newest file. var dayStart = LocalCalendar.StartOfDay(new DateOnly(2026, 10, 13), TestZones.Rome); var fromDay = await db.Entries.QueryLibraryAsync(new LibraryQuery { Mode = MosaicMode.Files, Limit = 1, Cursor = new LibraryCursor(dayStart, 0) }); Assert.AreEqual(new DateOnly(2026, 10, 12), LocalCalendar.Today(fromDay.Tiles.Single().PublishedAt, TestZones.Rome)); } [TestMethod] public async Task GroupedPhotoAndVideoCountsAgreeWithTheirWindowsWithAndWithoutAccounts() { // The counts of the whole library read one list of the entries with such files, those of an account an EXISTS // per post, the windows always the EXISTS: the same entries every way. using var temp = new TempFolder(); await using var db = await TestDatabase.OpenAsync(temp); var (carousel, single, partial, _) = await FillAsync(db); var accountId = (await db.Entries.GetAsync(carousel))!.AccountId; var other = await db.Accounts.AddAsync(Make.Account("luca.demo")); await db.Entries.AddAsync(Make.Entry(other.Id, "v", new DateTimeOffset(2026, 10, 11, 8, 0, 0, TimeSpan.Zero)), [Make.Stored(Make.Video(), Make.Sha(51), "o/v.mp4")]); foreach (var (kind, mine, everyone) in new[] { (LibraryKindFilter.Videos, new[] { carousel }, 2), (LibraryKindFilter.Photos, new[] { carousel, partial, single }, 3), }) { var all = new LibraryFilter { Kind = kind }; var own = all with { AccountIds = [accountId] }; Assert.AreEqual(new LibraryTotals(everyone, kind == LibraryKindFilter.Videos ? 2 : 1), await db.Entries.GetLibraryTotalsAsync(all), $"{kind}"); Assert.AreEqual(everyone, (await db.Entries.CountLibraryByDayAsync(all, TestZones.Rome)).Sum(d => d.Count)); Assert.AreEqual(everyone, (await db.Entries.QueryLibraryAsync(new LibraryQuery { Filter = all })).Tiles.Count); Assert.AreEqual(new LibraryTotals(mine.Length, 1), await db.Entries.GetLibraryTotalsAsync(own), $"{kind} di un account"); Assert.AreEqual(mine.Length, (await db.Entries.CountLibraryByDayAsync(own, TestZones.Rome)).Sum(d => d.Count)); CollectionAssert.AreEquivalent(mine, (await db.Entries.QueryLibraryAsync(new LibraryQuery { Filter = own })).Tiles.Select(t => t.EntryId).ToArray()); } } private static string Key(LibraryTile tile) => $"{tile.EntryId}.{tile.FilePosition}"; /// The files of «Tutte le foto» of a filter: the sum of its days, as /api/library/days gives it. private static async Task FileTotalAsync(FebeDatabase db, LibraryFilter filter) => (await db.Entries.CountLibraryByDayAsync(filter, TestZones.Rome, MosaicMode.Files)).Sum(d => d.Count); /// /// A story (13 Oct, 12:00), a carousel of photo, video, photo (13 Oct, 10:00), a post with its second file still to /// download (13 Oct, 9:00) and a single photo (12 Oct, 9:00), all of one account. /// private static async Task<(long Carousel, long Single, long Partial, long Story)> FillAsync(FebeDatabase db) { var account = await db.Accounts.AddAsync(Make.Account("marta.demo")); var day = new DateTimeOffset(2026, 10, 13, 0, 0, 0, TimeSpan.FromHours(2)); var single = await db.Entries.AddAsync(Make.Entry(account.Id, "s", day.AddHours(-15)), [Make.Stored(Make.Photo(), Make.Sha(1), "p/s.jpg")]); var partial = await db.Entries.AddAsync(Make.Entry(account.Id, "p", day.AddHours(9)), [ Make.Stored(Make.Photo(1), Make.Sha(21), "p/p1.jpg"), Make.Photo(2), Make.Stored(Make.Photo(3), Make.Sha(23), "p/p3.jpg"), ]); var carousel = await db.Entries.AddAsync(Make.Entry(account.Id, "c", day.AddHours(10)), [ Make.Stored(Make.Photo(1), Make.Sha(11), "p/c1.jpg"), Make.Stored(Make.Video(2), Make.Sha(12), "p/c2.mp4"), Make.Stored(Make.Photo(3), Make.Sha(13), "p/c3.jpg"), ]); var story = await db.Entries.AddAsync(Make.Entry(account.Id, "st", day.AddHours(12), EntryKind.Story), [Make.Stored(Make.Photo(), Make.Sha(31), "s/st.jpg")]); Assert.AreEqual(EntryState.Partial, partial.Stored.Entry.State); return (carousel.Stored.Entry.Id, single.Stored.Entry.Id, partial.Stored.Entry.Id, story.Stored.Entry.Id); } }