D1, quarta parte: «Raggruppa i post» o «Tutte le foto» in Libreria e scheda account

Un post con più foto era una tessera sola: per vederle tutte bisognava aprirlo.
Ora un interruttore nella riga degli strumenti (Libreria e Contenuti
dell'account, non «In evidenza») passa a una tessera per file, video dei
caroselli compresi, con la posizione «2/4» sulla tessera; la scelta resta nel
browser, una per pagina, e il predefinito è raggruppato (§4.6).

- API: /api/library e /api/library/days accettano mode=files; il cursore
  diventa «ms.elemento.posizione», i giorni contano file e il totale dice anche
  quanti file. Le finestre restano a chiavi sull'indice di pubblicazione; per i
  conteggi un indice ix_media_entry_files (entry_id, state, kind) nella
  migrazione 0002 non ancora unita. Prova di scala a 200.000 elementi: finestre
  6-11 ms, conteggi 0,2-0,3 s.
- Visualizzatore: si apre sul file della tessera e va di file in file, anche da
  un elemento al successivo («Libreria, file 12 di 30.340»).
- Selezione di file; elenco CSV ed «Elimina» lavorano sugli elementi interi e
  lo dicono («3 file scelti appartengono a 2 elementi: elimino gli elementi
  interi, 9 file»). «Copia in…» non è in questo ramo.
- Dimostrazione: alcune serie hanno un video in seconda posizione, scelto
  dall'identificativo senza toccare la sequenza casuale.
- Prove di archivio, API, dimostrazione e interfaccia (queste ultime con
  Chromium, solo nella CI); guida e decisioni aggiornate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-25 16:40:07 +01:00
1 parent 34fe5e9073
commit 30e159cbd4
23 files changed
+1126 -130

No files matched your search

+157
View File
@@ -0,0 +1,157 @@
using Febe.Core.Domain;
using Febe.Core.Storage;
using Febe.Data;
using Febe.Tests.Support;
namespace Febe.Tests.Data;
/// <summary>
/// «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.
/// </summary>
[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<string>();
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<string>();
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 db.Entries.CountLibraryFilesAsync(new LibraryFilter()));
Assert.AreEqual(1, await db.Entries.CountLibraryFilesAsync(new LibraryFilter { Kind = LibraryKindFilter.Videos }));
Assert.AreEqual(5, await db.Entries.CountLibraryFilesAsync(new LibraryFilter { Kind = LibraryKindFilter.Photos }));
Assert.AreEqual(1, await db.Entries.CountLibraryFilesAsync(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 db.Entries.CountLibraryFilesAsync(mine));
Assert.AreEqual(8, await db.Entries.CountLibraryFilesAsync(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 db.Entries.CountLibraryFilesAsync(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));
}
private static string Key(LibraryTile tile) => $"{tile.EntryId}.{tile.FilePosition}";
/// <summary>
/// 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.
/// </summary>
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);
}
}
@@ -109,6 +109,34 @@ public sealed class LibraryScaleTests
Assert.AreEqual(new LibraryTotals(Entries, Accounts), totals);
var days = await Measure("conteggi per giorno", AggregateBoundMs, () => db.Entries.CountLibraryByDayAsync(new LibraryFilter(), TestZones.Rome));
Assert.AreEqual(Entries, days.Sum(d => d.Count));
// «Tutte le foto» (spec §4.6): one tile per file, in the same times as the grouped mosaic.
var files = await Measure("tutte le foto: prima finestra", IndexBoundMs, () => db.Entries.QueryLibraryAsync(new LibraryQuery { Limit = 200, Mode = MosaicMode.Files }));
Assert.HasCount(200, files.Tiles);
Assert.IsTrue(files.Tiles.All(t => t.FilePosition is not null));
var insidePost = files.Tiles.First(t => t.MediaCount == 2 && t.FilePosition == 1);
var resumed = await Measure("tutte le foto: finestra dentro un post", IndexBoundMs, () => db.Entries.QueryLibraryAsync(new LibraryQuery
{
Limit = 200,
Mode = MosaicMode.Files,
Cursor = new LibraryCursor(insidePost.PublishedAt, insidePost.EntryId, 1),
}));
Assert.AreEqual((insidePost.EntryId, (int?)2), (resumed.Tiles[0].EntryId, resumed.Tiles[0].FilePosition));
var filesMiddle = await Measure("tutte le foto: finestra a metà", IndexBoundMs, () => db.Entries.QueryLibraryAsync(new LibraryQuery { Limit = 200, Mode = MosaicMode.Files, Cursor = middleCursor }));
Assert.HasCount(200, filesMiddle.Tiles);
await Measure("tutte le foto: più recente da metà", IndexBoundMs, () => db.Entries.QueryLibraryAsync(new LibraryQuery { Limit = 200, Mode = MosaicMode.Files, Cursor = middleCursor, Direction = LibraryDirection.Newer }));
await Measure("tutte le foto: filtro Video", IndexBoundMs, () => db.Entries.QueryLibraryAsync(new LibraryQuery { Limit = 200, Mode = MosaicMode.Files, Filter = new LibraryFilter { Kind = LibraryKindFilter.Videos } }));
await Measure("tutte le foto: un account", IndexBoundMs, () => db.Entries.QueryLibraryAsync(new LibraryQuery { Limit = 200, Mode = MosaicMode.Files, Filter = new LibraryFilter { AccountIds = [accountIds[3]] } }));
await Measure("tutte le foto: ricerca comune", AggregateBoundMs, () => db.Entries.QueryLibraryAsync(new LibraryQuery { Limit = 200, Mode = MosaicMode.Files, Filter = new LibraryFilter { Search = "caffe" } }));
var fileCount = await Measure("tutte le foto: totale dei file", AggregateBoundMs, () => db.Entries.CountLibraryFilesAsync(new LibraryFilter()));
Assert.AreEqual(Entries + (Entries / 5), fileCount);
var fileDays = await Measure("tutte le foto: conteggi per giorno", AggregateBoundMs, () => db.Entries.CountLibraryByDayAsync(new LibraryFilter(), TestZones.Rome, MosaicMode.Files));
Assert.AreEqual(fileCount, fileDays.Sum(d => d.Count));
var accountFiles = await Measure("tutte le foto: conteggi di un account", AggregateBoundMs, () => db.Entries.CountLibraryByDayAsync(new LibraryFilter { AccountIds = [accountIds[3]] }, TestZones.Rome, MosaicMode.Files));
Assert.AreEqual(await db.Entries.CountLibraryFilesAsync(new LibraryFilter { AccountIds = [accountIds[3]] }, ct), accountFiles.Sum(d => d.Count));
await Measure("scheda account: Post", IndexBoundMs, () => db.Entries.QueryLibraryAsync(new LibraryQuery { Limit = 200, Filter = new LibraryFilter { AccountIds = [accountIds[3]], Kind = LibraryKindFilter.Posts } }));
await Measure("scheda account: Post, tutte le foto", IndexBoundMs, () => db.Entries.QueryLibraryAsync(new LibraryQuery { Limit = 200, Mode = MosaicMode.Files, Filter = new LibraryFilter { AccountIds = [accountIds[3]], Kind = LibraryKindFilter.Posts } }));
await Measure("scheda account: conteggi dei Post", AggregateBoundMs, () => db.Entries.CountLibraryByDayAsync(new LibraryFilter { AccountIds = [accountIds[3]], Kind = LibraryKindFilter.Posts }, TestZones.Rome));
await Measure("tutte le foto: conteggi del filtro Video", AggregateBoundMs, () => db.Entries.CountLibraryByDayAsync(new LibraryFilter { Kind = LibraryKindFilter.Videos }, TestZones.Rome, MosaicMode.Files));
await Measure("è già noto? (pagina di 12)", IndexBoundMs, async () => await db.Entries.GetKnownInstagramIdsAsync(accountIds[5], EntryKind.Post, Enumerable.Range(0, 12).Select(i => $"id{5 + (i * 40)}").ToList()));
await Measure("SHA-256 in tutta la libreria", IndexBoundMs, () => db.Media.FindBySha256Async(Make.Sha(150_001)));
var summaries = await Measure("elenco account con conteggi", AggregateBoundMs, () => db.Accounts.ListSummariesAsync(new DateTimeOffset(2026, 10, 1, 0, 0, 0, TimeSpan.Zero)));