Files
Febe/tests/Febe.Tests/Support/TestLibrary.cs
T
Alby96andClaude Opus 5.5 e66e322d35 E5: il lavoratore delle copie automatiche, all'avvio e ogni ora
AutoExportWorker copia da solo nelle cartelle con «Automatica» accesa:
il giro di recupero all'avvio e ogni ora (IClock), una passata due
minuti dopo l'ultima riga di _febe.jsonl (il segnale parte da
LibraryIngest, anche per le righe scritte in ritardo), subito con un
segnale urgente. Mai durante una sessione del motore: aspetta e dice
perché; i lavori di Alberto vengono prima e, finiti, il lavoro
automatico spostato riprende da solo (risposta 49). Al giro riprova le
cartelle in pausa (una copia manuale «In pausa» resta di Alberto,
risposta 48) e tutti i «Non riuscito» (risposta 50b); «Interrompi»
lo ferma fino alla fine della sosta (risposta 53). Un lavoro finito
non ne fa partire un altro da solo, così un file che non cambia
torna solo al giro. La composizione lo avvia solo nell'avvio normale
(mai nella dimostrazione) e lo ferma per primo alla chiusura.

Prove: AutoExportWorkerTests (le prove di accettazione di §15:
«Tolto» e «Superato» non si ricopiano, una cartella irraggiungibile
va in pausa e un giro dopo riprende, nessuna copia durante una
sessione), il segnale del giornale, la composizione.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 15:49:29 +01:00

124 lines
4.8 KiB
C#

using Febe.Core.Domain;
using Febe.Core.Export;
using Febe.Core.Library;
using Febe.Data;
namespace Febe.Tests.Support;
/// <summary>A library with its Data folder, database and services, in a temporary folder.</summary>
internal sealed class TestLibrary : IAsyncDisposable
{
private TestLibrary(TempFolder temp, FebeDatabase db, ManualClock clock)
{
Temp = temp;
Db = db;
Clock = clock;
Data = new DataFolderLayout(temp.Combine("dati"));
Layout = new LibraryLayout(temp.Combine("libreria"));
Thumbnails = new ThumbnailStore(Data.Thumbnails);
Index = new PerceptualHashIndex();
Journal = new AccountJournal(clock);
Ingest = new LibraryIngest(db, Layout, LibraryNaming.Default, Journal, clock, Thumbnails, Index, autoExport: AutoExport);
}
public TempFolder Temp { get; }
public FebeDatabase Db { get; private set; }
public ManualClock Clock { get; }
public DataFolderLayout Data { get; }
public LibraryLayout Layout { get; }
public ThumbnailStore Thumbnails { get; }
public PerceptualHashIndex Index { get; }
public AccountJournal Journal { get; }
public LibraryIngest Ingest { get; private set; }
/// <summary>The signal of the automatic export that this library's journal lines raise (its own: tests never wake each other).</summary>
public AutoExportSignal AutoExport { get; } = new();
public static async Task<TestLibrary> CreateAsync(TempFolder temp)
{
var db = await FebeDatabase.OpenAsync(temp.Combine("dati"));
return new TestLibrary(temp, db, new ManualClock(Make.T0));
}
/// <summary>Records an entry and stores its files as the collection pipeline would, then writes the journal line.</summary>
public async Task<EntryWithMedia> SaveAsync(Account account, Entry entry, IReadOnlyList<TestFile> files, string? highlightTitle = null)
{
var added = await Db.Entries.AddAsync(entry, files.Select(f => f.Media).ToList());
foreach (var (media, file) in added.Stored.Media.Zip(files.OrderBy(f => f.Media.Position)))
{
await Ingest.SaveMediaAsync(account, added.Stored.Entry, media, new MediaContent { Content = new MemoryStream(file.Bytes), Extension = file.Extension }, highlightTitle);
}
await Ingest.AppendJournalAsync(added.Stored.Entry.Id);
return (await Db.Entries.GetWithMediaAsync(added.Stored.Entry.Id))!;
}
/// <summary>Simulates the loss of the database: closes it, deletes the file and opens an empty one.</summary>
public async Task LoseDatabaseAsync()
{
await Db.DisposeAsync();
foreach (var suffix in new[] { string.Empty, "-wal", "-shm" })
{
await DeleteClosedFileAsync(Data.Database + suffix);
}
Db = await FebeDatabase.OpenAsync(Data.Root);
Ingest = new LibraryIngest(Db, Layout, LibraryNaming.Default, Journal, Clock, Thumbnails, Index, autoExport: AutoExport);
}
/// <summary>
/// Deletes a file Febe has already closed. On Windows a file still held open elsewhere cannot be deleted, and on one PC a
/// database just closed was now and then held for a few milliseconds by something outside Febe (Restart Manager listed
/// no process; probably the real-time antivirus, which was active, but the cause was never shown): that sharing
/// violation is waited out (at most 5 s). On Linux a delete never meets one, so the first attempt is the only one.
/// </summary>
private static async Task DeleteClosedFileAsync(string path)
{
const int SharingViolation = unchecked((int)0x80070020);
for (var attempt = 1; ; attempt++)
{
try
{
File.Delete(path);
return;
}
catch (IOException ex) when (OperatingSystem.IsWindows() && ex.HResult == SharingViolation && attempt < 100)
{
await Task.Delay(50);
}
}
}
public ValueTask DisposeAsync() => Db.DisposeAsync();
}
/// <summary>A file to store: its record and its bytes.</summary>
internal sealed record TestFile(Media Media, byte[] Bytes, string Extension)
{
private static int videoSeed;
public static TestFile Photo(int position, int scene, int width = 1080, int height = 1350) =>
new(Make.Photo(position), PhotoBytes(scene, width, height), ".jpg");
public static TestFile Video(int position, int size = 50_000)
{
var bytes = new byte[size];
new Random(Interlocked.Increment(ref videoSeed)).NextBytes(bytes);
return new TestFile(Make.Video(position), bytes, ".mp4");
}
public static byte[] PhotoBytes(int scene, int width = 1080, int height = 1350)
{
using var bitmap = TestImages.Scene(scene, width, height);
return TestImages.Jpeg(bitmap, 88);
}
}