Files
Febe/tests/Febe.Tests/Library/LibraryRebuildTests.cs
T
Alby96andClaude Opus 5.5 72deb212a7
verifica / controlli (push) Successful in 17s
verifica / prove (push) Successful in 12m21s
verifica / installatore (push) Successful in 3m45s
verifica / immagine (push) Successful in 16m5s
Foto del profilo: uno schema dei nomi proprio (#38)
Alberto ha deciso che le foto del profilo stanno dentro ogni account, in
<account>\profilo. Con un solo schema generale bastava cambiarlo perché
finissero insieme ai post: ora hanno il loro schema, come le raccolte in
evidenza, predefinito {account}\profilo\{data}_{ora}_{codice}_{n}, senza la
cartella dell'anno.

Impostazioni > Generali mostra il terzo campo con la cartella nel risultato
e colora solo il campo sbagliato. Un salvataggio che non manda il terzo
schema tiene quello salvato. Importazione e ricostruzione del database
riconoscono la cartella; prove nuove per nomi, impostazioni, salvataggio,
importazione e ricostruzione. Schermata confrontata con il mockup 08.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 11:39:58 +01:00

206 lines
12 KiB
C#

using Febe.Core.Domain;
using Febe.Core.Library;
using Febe.Core.Storage;
using Febe.Tests.Support;
namespace Febe.Tests.Library;
[TestClass]
public sealed class LibraryRebuildTests
{
/// <summary>
/// «Fatto quando» of M1: the database is rebuilt from a test library after it was lost, without downloading
/// anything again, honouring deletions.
/// </summary>
[TestMethod]
public async Task TheDatabaseIsRebuiltFromTheLibraryAfterItIsLost()
{
using var temp = new TempFolder();
await using var library = await TestLibrary.CreateAsync(temp);
var outsideFolder = temp.Combine("altro-disco", "luca");
var marta = await library.Db.Accounts.AddAsync(Make.Account("marta.demo", "100") with { DisplayName = "Marta B.", IsFollowed = true });
var luca = await library.Db.Accounts.AddAsync(Make.Account("luca.demo", "200") with { Folder = outsideFolder });
var me = await library.Db.Accounts.AddAsync(Make.Account("tuo.profilo", "300", mine: true));
var post = await library.SaveAsync(marta, Make.Entry(marta.Id, "p1", Make.T0.AddDays(-10), caption: "Però che vista") with { Code = "AbC1" }, [TestFile.Photo(1, 1), TestFile.Photo(2, 2), TestFile.Video(3)]);
var story = await library.SaveAsync(marta, Make.Entry(marta.Id, "s1", Make.T0.AddHours(-3), EntryKind.Story) with { ExpiresAt = Make.T0.AddHours(21) }, [TestFile.Video(1)]);
var collection = await library.Db.Highlights.UpsertAsync(new Highlight { AccountId = marta.Id, InstagramId = "h1", Title = "Estate 2025", Position = 2, DiscoveredAt = Make.T0 });
var highlightEntry = await library.Db.Entries.AddAsync(Make.Entry(marta.Id, "e1", Make.T0.AddDays(-60), EntryKind.Highlight), [Make.Photo()]);
await library.Db.Highlights.AddItemAsync(collection.Id, highlightEntry.Stored.Entry.Id);
await library.Ingest.SaveMediaAsync(marta, highlightEntry.Stored.Entry, highlightEntry.Stored.Media[0], new MediaContent { Content = new MemoryStream(TestFile.PhotoBytes(3)), Extension = ".jpg" }, collection.Title);
await library.Ingest.AppendJournalAsync(highlightEntry.Stored.Entry.Id);
var forgotten = await library.SaveAsync(marta, Make.Entry(marta.Id, "gone", Make.T0.AddDays(-5)), [TestFile.Photo(1, 4)]);
var comesBack = await library.SaveAsync(marta, Make.Entry(marta.Id, "back", Make.T0.AddDays(-6)), [TestFile.Photo(1, 5)]);
var broken = await library.SaveAsync(marta, Make.Entry(marta.Id, "broken", Make.T0.AddDays(-7)), [TestFile.Photo(1, 6), TestFile.Photo(2, 7)]);
var imported = await library.SaveAsync(marta, Make.Entry(marta.Id, "imp", Make.T0.AddDays(-400)) with { InstagramId = null, Code = null, Provenance = Provenance.Imported }, [TestFile.Photo(1, 8)]);
var lucaPost = await library.SaveAsync(luca, Make.Entry(luca.Id, "l1", Make.T0.AddDays(-1)), [TestFile.Photo(1, 9)]);
var tagged = await library.SaveAsync(me, Make.Entry(me.Id, "p1", Make.T0.AddDays(-10), EntryKind.Tagged) with { AuthorUsername = "marta.demo" }, [TestFile.Photo(1, 1)]);
var deletion = new LibraryDeletion(library.Db, library.Layout, library.Journal, library.Clock, library.Thumbnails, library.Index);
await deletion.DeleteAsync(forgotten.Entry.Id, DeletionMode.Remember);
await deletion.DeleteAsync(comesBack.Entry.Id, DeletionMode.AllowReturn);
File.Delete(library.Layout.FullPath(marta, broken.Media[1].RelativePath!));
await File.WriteAllTextAsync(Path.Combine(library.Layout.AccountFolder(marta), "post", "aggiunto-a-mano.jpg"), "x");
Directory.CreateDirectory(Path.Combine(library.Layout.Root, "cartella-senza-registro"));
await File.WriteAllTextAsync(Path.Combine(library.Layout.Root, "cartella-senza-registro", "foto.jpg"), "x");
var before = await Snapshot(library);
var filesBefore = Directory.GetFiles(temp.Path, "*", SearchOption.AllDirectories).Where(f => !f.StartsWith(library.Data.Root, StringComparison.Ordinal)).Order().ToList();
await library.LoseDatabaseAsync();
Assert.IsEmpty(await library.Db.Accounts.ListAsync());
var index = new PerceptualHashIndex();
var rebuilder = new LibraryRebuilder(library.Db, library.Layout, library.Clock, library.Thumbnails, null, index);
var progress = new List<RebuildProgress>();
var report = await rebuilder.RebuildAsync(new RebuildOptions { ExtraAccountFolders = [outsideFolder] }, new SyncProgress<RebuildProgress>(progress.Add));
Assert.AreEqual(3, report.AccountFolders);
Assert.AreEqual(3, report.AccountsCreated);
CollectionAssert.AreEqual(new[] { "cartella-senza-registro" }, report.FoldersWithoutJournal.ToArray());
Assert.AreEqual(1, report.FilesMissing);
Assert.AreEqual(1, report.UntrackedFiles);
Assert.AreEqual(1, report.EntriesDeleted);
Assert.AreEqual(0, report.HashMismatches);
Assert.AreEqual(0, report.InvalidLines);
var after = await Snapshot(library);
foreach (var (key, expected) in before)
{
if (key.StartsWith("marta.demo|Post|broken", StringComparison.Ordinal))
{
continue;
}
Assert.IsTrue(after.TryGetValue(key, out var actual), $"Manca dopo la ricostruzione: {key}");
Assert.AreEqual(expected, actual, key);
}
Assert.IsFalse(after.Keys.Any(k => k.Contains("|back", StringComparison.Ordinal)), "«Lascia che torni» non ricompare.");
Assert.IsTrue(await library.Db.Entries.IsKnownAsync((await library.Db.Accounts.FindByInstagramIdAsync("100"))!.Id, EntryKind.Post, "gone"));
// Accounts come back paused, with their identity and folders.
var accounts = await library.Db.Accounts.ListAsync();
Assert.IsTrue(accounts.All(a => a.State == AccountState.Paused));
var martaAgain = accounts.Single(a => a.InstagramId == "100");
Assert.AreEqual("Marta B.", martaAgain.DisplayName);
Assert.IsNull(martaAgain.Folder);
Assert.AreEqual(outsideFolder, accounts.Single(a => a.InstagramId == "200").Folder);
Assert.IsTrue(accounts.Single(a => a.InstagramId == "300").IsMine);
// The file that disappeared is reported, not downloaded again.
var brokenAgain = (await library.Db.Entries.FindAsync(martaAgain.Id, EntryKind.Post, "broken"))!;
Assert.AreEqual(EntryState.Partial, brokenAgain.State);
// Highlights, thumbnails and perceptual hashes are back.
var collections = await library.Db.Highlights.ListAsync(martaAgain.Id);
Assert.AreEqual("Estate 2025", collections.Single().Title);
Assert.HasCount(1, await library.Db.Highlights.ListItemsAsync(collections.Single().Id));
Assert.IsTrue(library.Thumbnails.Exists(post.Media[0].Sha256!));
Assert.AreEqual(7, index.Count, "Impronte delle foto presenti: due del post, evidenza, foto rimasta, importata, luca, taggato.");
Assert.HasCount(7, await library.Db.Media.LoadPerceptualHashesAsync());
// Nothing on disk changed.
var filesAfter = Directory.GetFiles(temp.Path, "*", SearchOption.AllDirectories).Where(f => !f.StartsWith(library.Data.Root, StringComparison.Ordinal)).Order().ToList();
CollectionAssert.AreEqual(filesBefore, filesAfter);
Assert.IsNotEmpty(progress);
// A second run adds nothing.
var again = await rebuilder.RebuildAsync(new RebuildOptions { ExtraAccountFolders = [outsideFolder] });
Assert.AreEqual(0, again.EntriesCreated);
Assert.AreEqual(0, again.AccountsCreated);
Assert.AreEqual(report.EntriesCreated, again.EntriesAlreadyPresent);
CollectionAssert.AreEquivalent(after.ToList(), (await Snapshot(library)).ToList());
_ = (story, imported, lucaPost, tagged);
}
[TestMethod]
public async Task ADeletedEntryThatCameBackIsRestored()
{
using var temp = new TempFolder();
await using var library = await TestLibrary.CreateAsync(temp);
var marta = await library.Db.Accounts.AddAsync(Make.Account("marta.demo", "100"));
var entry = await library.SaveAsync(marta, Make.Entry(marta.Id, "x", Make.T0), [TestFile.Photo(1, 1)]);
await new LibraryDeletion(library.Db, library.Layout, library.Journal, library.Clock).DeleteAsync(entry.Entry.Id, DeletionMode.AllowReturn);
var again = await library.SaveAsync(marta, Make.Entry(marta.Id, "x", Make.T0), [TestFile.Photo(1, 1)]);
await library.LoseDatabaseAsync();
var report = await new LibraryRebuilder(library.Db, library.Layout, library.Clock).RebuildAsync();
Assert.AreEqual(1, report.EntriesCreated);
var restored = (await library.Db.Entries.FindAsync((await library.Db.Accounts.FindByInstagramIdAsync("100"))!.Id, EntryKind.Post, "x"))!;
Assert.AreEqual(EntryState.Downloaded, restored.State);
Assert.AreEqual(again.Media[0].RelativePath, (await library.Db.Media.ListByEntryAsync(restored.Id))[0].RelativePath);
}
[TestMethod]
public async Task ProfilePicturesComeBackFromTheirOwnFolder()
{
// #38: the pictures have their own scheme (no year folder, outside the general scheme); the rebuild reads them
// from the journal wherever that scheme put them.
using var temp = new TempFolder();
await using var library = await TestLibrary.CreateAsync(temp);
library.Ingest.Apply(LibraryNaming.Default with { ProfilePicture = NamingScheme.Parse(@"{account}\foto\{data}_{codice}", NamingSchemeUse.ProfilePicture) }, true);
var marta = await library.Db.Accounts.AddAsync(Make.Account("marta.demo", "100"));
var first = await library.SaveAsync(marta, Make.Entry(marta.Id, "pp1", Make.T0.AddDays(-30), EntryKind.ProfilePicture), [TestFile.Photo(1, 1)]);
var second = await library.SaveAsync(marta, Make.Entry(marta.Id, "pp2", Make.T0, EntryKind.ProfilePicture), [TestFile.Photo(1, 2)]);
StringAssert.StartsWith(first.Media[0].RelativePath, "foto/");
await library.LoseDatabaseAsync();
var report = await new LibraryRebuilder(library.Db, library.Layout, library.Clock).RebuildAsync();
Assert.AreEqual(2, report.EntriesCreated);
Assert.AreEqual(0, report.UntrackedFiles);
var account = (await library.Db.Accounts.FindByInstagramIdAsync("100"))!;
var pictures = await library.Db.Entries.ListByAccountAsync(account.Id, new EntryListOptions { Kind = EntryKind.ProfilePicture });
Assert.HasCount(2, pictures);
var paths = new List<string?>();
foreach (var picture in pictures)
{
paths.AddRange((await library.Db.Media.ListByEntryAsync(picture.Id)).Select(m => m.RelativePath));
}
CollectionAssert.AreEquivalent(new[] { first.Media[0].RelativePath, second.Media[0].RelativePath }, paths);
}
[TestMethod]
public async Task AnEmptyLibraryRebuildsNothing()
{
using var temp = new TempFolder();
await using var library = await TestLibrary.CreateAsync(temp);
var report = await new LibraryRebuilder(library.Db, library.Layout, library.Clock).RebuildAsync();
Assert.AreEqual(0, report.AccountFolders);
Assert.AreEqual(0, report.EntriesCreated);
}
private sealed class SyncProgress<T>(Action<T> report) : IProgress<T>
{
public void Report(T value) => report(value);
}
/// <summary>Everything that identifies the library content, keyed by account and entry identity.</summary>
private static async Task<Dictionary<string, string>> Snapshot(TestLibrary library)
{
var result = new Dictionary<string, string>(StringComparer.Ordinal);
foreach (var account in await library.Db.Accounts.ListAsync())
{
var entries = await library.Db.Entries.ListByAccountAsync(account.Id, new EntryListOptions { Limit = 1000 });
foreach (var entry in entries)
{
var media = await library.Db.Media.ListByEntryAsync(entry.Id);
var id = entry.InstagramId ?? $"t{entry.PublishedAt.ToUnixTimeMilliseconds()}";
result[$"{account.Username}|{entry.Kind}|{id}"] = $"{entry.State}|{entry.Code}|{entry.Caption}|{entry.PublishedAt:O}|{entry.Provenance}|{entry.AuthorUsername}";
foreach (var m in media)
{
result[$"{account.Username}|{entry.Kind}|{id}|{m.Position}|{m.Kind}|"] =
$"{m.State}|{m.RelativePath}|{m.Sha256}|{m.PerceptualHash}|{m.Bytes}|{m.HasThumbnail}|{m.Width}x{m.Height}";
}
}
}
return result;
}
}