Files
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

117 lines
5.5 KiB
C#

using System.Text;
using Febe.Core.Domain;
using Febe.Core.Library;
using Febe.Tests.Support;
namespace Febe.Tests.Library;
[TestClass]
public sealed class AccountJournalTests
{
[TestMethod]
public async Task SaveAndDeleteLinesAreAppendedAndReadBack()
{
using var temp = new TempFolder();
var clock = new ManualClock(Make.T0);
var journal = new AccountJournal(clock);
var account = Make.Account("marta.demo", "100") with { DisplayName = "Marta B." };
var entry = Make.Entry(1, "3742118", Make.T0.AddHours(-2), EntryKind.Highlight, "Però che bella giornata") with { Code = null };
var media = new[]
{
Make.Stored(Make.Photo(1), Make.Sha(1), "evidenza/Estate 2025/a_1.jpg"),
Make.Video(2), // not downloaded: not in the line
};
await journal.AppendSavedAsync(temp.Path, account, entry, media, [new JournalHighlight("h1", "Estate 2025", 0)]);
clock.Now = Make.T0.AddDays(1);
await journal.AppendDeletedAsync(temp.Path, account, entry, ["evidenza/Estate 2025/a_1.jpg"], remember: true);
var text = await File.ReadAllTextAsync(AccountJournal.PathFor(temp.Path));
Assert.Contains("Però", text, "Gli accenti restano leggibili.");
Assert.AreEqual(2, text.Count(c => c == '\n'));
var content = await AccountJournal.ReadAsync(temp.Path);
Assert.AreEqual(0, content.InvalidLines);
Assert.HasCount(2, content.Lines);
var save = content.Lines[0];
Assert.AreEqual(JournalOps.Save, save.Op);
Assert.AreEqual(1, save.V);
Assert.AreEqual(Make.T0, save.At);
Assert.AreEqual("100", save.Account.Id);
Assert.AreEqual("marta.demo", save.Account.Username);
Assert.AreEqual("evidenza", save.Entry.Kind);
Assert.AreEqual(EntryKind.Highlight, save.Kind);
Assert.AreEqual("3742118", save.Entry.Id);
Assert.AreEqual("Però che bella giornata", save.Entry.Caption);
Assert.AreEqual(Make.T0.AddHours(-2), save.Entry.PublishedAt);
Assert.AreEqual("Estate 2025", save.Entry.Highlights!.Single().Title);
var file = save.Files!.Single();
Assert.AreEqual(1, file.N);
Assert.AreEqual("photo", file.Type);
Assert.AreEqual("evidenza/Estate 2025/a_1.jpg", file.Path);
Assert.AreEqual(Make.Sha(1), file.Sha256);
var delete = content.Lines[1];
Assert.AreEqual(JournalOps.Delete, delete.Op);
Assert.IsTrue(delete.Remember);
Assert.IsNull(delete.Entry.Caption, "Una riga di eliminazione non ripete la didascalia.");
Assert.AreEqual("evidenza/Estate 2025/a_1.jpg", delete.Files!.Single().Path);
}
[TestMethod]
public async Task TheJournalIsOnlyAppendedNeverRewritten()
{
using var temp = new TempFolder();
var journal = new AccountJournal(new ManualClock(Make.T0));
var account = Make.Account("marta.demo");
await journal.AppendSavedAsync(temp.Path, account, Make.Entry(1, "1", Make.T0), [Make.Stored(Make.Photo(), Make.Sha(1), "post/a.jpg")]);
var before = await File.ReadAllBytesAsync(AccountJournal.PathFor(temp.Path));
await journal.AppendSavedAsync(temp.Path, account, Make.Entry(1, "2", Make.T0), [Make.Stored(Make.Photo(), Make.Sha(2), "post/b.jpg")]);
await journal.AppendDeletedAsync(temp.Path, account, Make.Entry(1, "1", Make.T0), ["post/a.jpg"], remember: false);
var after = await File.ReadAllBytesAsync(AccountJournal.PathFor(temp.Path));
Assert.IsGreaterThan(before.Length, after.Length);
CollectionAssert.AreEqual(before, after.Take(before.Length).ToArray());
}
[TestMethod]
public async Task ALineCutByACrashDoesNotSpoilTheNextOnes()
{
using var temp = new TempFolder();
var journal = new AccountJournal(new ManualClock(Make.T0));
var account = Make.Account("marta.demo");
await journal.AppendSavedAsync(temp.Path, account, Make.Entry(1, "1", Make.T0), [Make.Stored(Make.Photo(), Make.Sha(1), "post/a.jpg")]);
await File.AppendAllTextAsync(AccountJournal.PathFor(temp.Path), "{\"v\":1,\"op\":\"save\",\"at\":\"2026-", Encoding.UTF8);
await journal.AppendSavedAsync(temp.Path, account, Make.Entry(1, "2", Make.T0), [Make.Stored(Make.Photo(), Make.Sha(2), "post/b.jpg")]);
var content = await AccountJournal.ReadAsync(temp.Path);
Assert.AreEqual(1, content.InvalidLines);
Assert.IsTrue(content.Lines.Select(l => l.Entry.Id).SequenceEqual(["1", "2"]));
}
[TestMethod]
public async Task ParallelAppendsKeepEveryLineWhole()
{
using var temp = new TempFolder();
var journal = new AccountJournal(new ManualClock(Make.T0));
var account = Make.Account("marta.demo");
await Task.WhenAll(Enumerable.Range(0, 100).Select(i => Task.Run(() =>
journal.AppendSavedAsync(temp.Path, account, Make.Entry(1, $"e{i}", Make.T0, caption: new string('x', 3000)), [Make.Stored(Make.Photo(), Make.Sha(i), $"post/{i}.jpg")]))));
var content = await AccountJournal.ReadAsync(temp.Path);
Assert.AreEqual(0, content.InvalidLines);
Assert.AreEqual(100, content.Lines.Select(l => l.Entry.Id).Distinct().Count());
}
[TestMethod]
public async Task AMissingJournalIsEmpty()
{
using var temp = new TempFolder();
var content = await AccountJournal.ReadAsync(temp.Combine("nessuno"));
Assert.IsEmpty(content.Lines);
Assert.AreEqual(0, content.InvalidLines);
}
}