Files
Febe/tests/Febe.Tests/Support/TestLibrary.cs
T
Claude 3aeaa17da7 M1 Dati e libreria: modello, archivi SQLite con scrittore unico, nomi dei file, scrittura sicura, miniature, impronte, _febe.jsonl, copie, eliminazione e ricostruzione
Modello del dominio e interfacce degli archivi in Febe.Core; implementazione SQLite in Febe.Data (WAL, chiavi esterne, migrazioni numerate in transazione, coda di scrittura su un solo thread); logica della libreria in Febe.Core/Library; 164 prove MSTest, compresa la ricostruzione del database da una libreria di prova e la scala a 200.000 elementi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
2026-10-13 16:06:15 +00:00

97 lines
3.5 KiB
C#

using Febe.Core.Domain;
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);
}
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; }
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" })
{
File.Delete(Data.Database + suffix);
}
Db = await FebeDatabase.OpenAsync(Data.Root);
Ingest = new LibraryIngest(Db, Layout, LibraryNaming.Default, Journal, Clock, Thumbnails, Index);
}
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);
}
}