Files
Febe/tests/Febe.Tests/Export/ExportSamePostTests.cs
T
Alby96andClaude Opus 5.5 736d241a38 E2: prove del motore, dell'archivio e del servizio delle copie
Le prove della tappa E2 (WP5a) coprono il montaggio di /esporta, la
radice, il controllo delle cartelle, le destinazioni, i percorsi, la
copia, gli stati «Tolto» e «Superato», lo stesso post in due account,
il lavoro in sottofondo (interrompi, riprendi, annulla, riavvio, porta
condivisa con importazione e ricostruzione, sessione del motore,
interruttore dell'account, Docker senza montaggio) e il filtro «Copie».
Si appoggiano a un banco comune con falsi per disco, sessione, montaggi
e file system, senza permessi dei file, così girano anche da root.

Le prove hanno trovato due difetti, corretti:
- MarkCopiesRemovedAsync riusava il comando in cache del primo blocco
  di 900 id: «Controlla le copie» falliva con più di 900 file mancanti
  in una pagina.
- ReplacedStatesSql senza statistiche leggeva tutte le copie tolte o
  superate della destinazione: «+state» la tiene sul suo indice.

Il controllo delle cartelle accetta una scrittura di prova sostituibile,
per provare il guasto della sonda senza permessi.

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

128 lines
7.1 KiB
C#

using Febe.Core.Domain;
using Febe.Core.Export;
using Febe.Core.Library;
using Febe.Tests.Export.Support;
using Febe.Tests.Support;
namespace Febe.Tests.Export;
/// <summary>
/// The same file in two places (spec §4.10 l.311-312): «Una copia per account» or «Una copia sola» for a post seen by two
/// accounts, and a story that is also in a highlight, which is one file and one copy.
/// </summary>
[TestClass]
public sealed class ExportSamePostTests
{
public TestContext TestContext { get; set; } = null!;
[TestMethod]
public async Task OneCopyPerAccountWritesTwoFilesUnderTwoSegments()
{
var ct = TestContext.CancellationToken;
await using var bed = await ExportTestBed.CreateAsync();
var marta = await bed.PostAsync(bed.Marta, "980", 60);
var luca = await bed.PostAsync(bed.Luca, "980", 60);
Assert.AreEqual(marta.Media[0].Sha256, luca.Media[0].Sha256);
var usb = await bed.GeneralAsync(ct: ct);
var started = await bed.CopyAsync(new ExportJobRequest(new ExportSelection.Entries([marta.Entry.Id, luca.Entry.Id])), ct);
Assert.AreEqual((2, 0), (started.Plan.Files, started.Plan.AlreadyCopied));
Assert.IsTrue(File.Exists(bed.GeneralCopy(bed.Marta, marta.Media[0])));
Assert.IsTrue(File.Exists(bed.GeneralCopy(bed.Luca, luca.Media[0])));
Assert.HasCount(2, Fingerprint.Of(bed.Usb));
var copies = await bed.Db.Exports.ListCopiesAsync(usb.Id, ct: ct);
CollectionAssert.AreEquivalent(new long?[] { bed.Marta.Id, bed.Luca.Id }, copies.Select(c => c.AccountId).ToArray());
Assert.IsTrue(copies.All(c => c.State == ExportCopyState.Copied));
}
[TestMethod]
public async Task OneCopyOnlyWritesOnceAndTheOtherShowsCopied()
{
var ct = TestContext.CancellationToken;
await using var bed = await ExportTestBed.CreateAsync();
var marta = await bed.PostAsync(bed.Marta, "981", 61);
var luca = await bed.PostAsync(bed.Luca, "981", 61);
await bed.SetSamePostAsync(SamePostCopies.OneOnly, ct);
var usb = await bed.GeneralAsync(ct: ct);
// In one job: the plan counts it once and one file is written.
var both = new ExportJobRequest(new ExportSelection.Entries([marta.Entry.Id, luca.Entry.Id]));
var plan = await bed.Service.PlanAsync(both, ct);
Assert.AreEqual((1, 1), (plan.Files, plan.AlreadyCopied));
var started = await bed.CopyAsync(both, ct);
Assert.HasCount(1, Fingerprint.Of(bed.Usb));
var items = await bed.ItemsAsync(started.Runs.Single().Id, ct);
Assert.AreEqual(1, items.Count(i => i.State == ExportItemState.Copied));
var other = items.Single(i => i.State != ExportItemState.Copied);
Assert.AreEqual((ExportItemState.AlreadyCopied, ExportText.Notes.CopiedForAnotherAccount), (other.State, other.Note));
var copy = (await bed.Db.Exports.ListCopiesAsync(usb.Id, ct: ct)).Single();
Assert.AreEqual(copy.Id, other.CopyId);
// Later, the other account alone: still «Già copiato», nothing written.
var otherAccount = copy.AccountId == bed.Marta.Id ? bed.Luca : bed.Marta;
var again = await bed.CopyAccountAsync(otherAccount, ct: ct);
Assert.AreEqual((0, 1), (again.Plan.Files, again.Plan.AlreadyCopied));
Assert.AreEqual(ExportItemState.AlreadyCopied, (await bed.ItemsAsync(again.Runs.Single().Id, ct)).Single().State);
Assert.HasCount(1, Fingerprint.Of(bed.Usb));
// Back to «Una copia per account»: the other account would get its own.
await bed.SetSamePostAsync(SamePostCopies.OnePerAccount, ct);
Assert.AreEqual(1, (await bed.Service.PlanAsync(new ExportJobRequest(new ExportSelection.Account(otherAccount.Id)), ct)).Files);
await bed.SetSamePostAsync(SamePostCopies.OneOnly, ct);
// The one copy goes from the disk: the copier checks the file and writes the other account's copy after all.
var copiedAccount = otherAccount == bed.Marta ? bed.Luca : bed.Marta;
File.Delete(bed.GeneralCopy(copiedAccount, (copiedAccount == bed.Marta ? marta : luca).Media[0]));
var written = await bed.CopyAccountAsync(otherAccount, ct: ct);
Assert.AreEqual(ExportItemState.Copied, (await bed.ItemsAsync(written.Runs.Single().Id, ct)).Single().State);
Assert.IsTrue(File.Exists(bed.GeneralCopy(otherAccount, (otherAccount == bed.Marta ? marta : luca).Media[0])));
}
[TestMethod]
public async Task AStoryAndItsHighlightAreOneCopy()
{
var ct = TestContext.CancellationToken;
await using var bed = await ExportTestBed.CreateAsync();
var story = await StoryInHighlightAsync(bed, "982", 62, ct);
var usb = await bed.GeneralAsync(ContentTypes.Stories | ContentTypes.Highlights, ct: ct);
var started = await bed.CopyAccountAsync(bed.Marta, ct: ct);
Assert.AreEqual(1, started.Plan.Files);
var item = (await bed.ItemsAsync(started.Runs.Single().Id, ct)).Single();
Assert.AreEqual(ExportItemState.Copied, item.State);
Assert.StartsWith("marta.demo/storie/", item.RelativePath!, "with «Storie» chosen it stays a story");
Assert.HasCount(1, Fingerprint.Of(bed.Usb));
Assert.HasCount(1, await bed.Db.Exports.ListCopiesAsync(usb.Id, ct: ct));
// Asked again through the entry: already copied.
Assert.AreEqual(1, (await bed.Service.PlanAsync(new ExportJobRequest(new ExportSelection.Entries([story.Entry.Id])), ct)).AlreadyCopied);
}
[TestMethod]
public async Task AHighlightOnlyDestinationCopiesStoriesInHighlights()
{
var ct = TestContext.CancellationToken;
await using var bed = await ExportTestBed.CreateAsync();
var inHighlight = await StoryInHighlightAsync(bed, "983", 63, ct);
await bed.Library.SaveAsync(bed.Marta, Make.Entry(bed.Marta.Id, "984", Make.T0.AddHours(-2), EntryKind.Story), [ExportTestBed.Photo(1, 64)]);
await bed.GeneralAsync(ContentTypes.Highlights, ct: ct);
var started = await bed.CopyAccountAsync(bed.Marta, ct: ct);
Assert.AreEqual((1, 1), (started.Plan.Files, started.Plan.NotExported));
var item = (await bed.ItemsAsync(started.Runs.Single().Id, ct)).Single(i => i.State == ExportItemState.Copied);
Assert.AreEqual(inHighlight.Media[0].Id, item.MediaId);
Assert.StartsWith("marta.demo/evidenza/Estate/", item.RelativePath!);
var files = Fingerprint.Of(bed.Usb);
Assert.HasCount(1, files);
Assert.IsFalse(Directory.Exists(Path.Combine(bed.Usb, "marta.demo", "storie")), "a story outside highlights is not due");
}
private static async Task<EntryWithMedia> StoryInHighlightAsync(ExportTestBed bed, string code, int scene, CancellationToken ct)
{
var highlight = await bed.Db.Highlights.UpsertAsync(new Highlight { AccountId = bed.Marta.Id, InstagramId = "h" + code, Title = "Estate", DiscoveredAt = Make.T0 }, ct);
var story = await bed.Library.SaveAsync(bed.Marta, Make.Entry(bed.Marta.Id, code, Make.T0.AddHours(-1), EntryKind.Story), [ExportTestBed.Photo(1, scene)]);
await bed.Db.Highlights.AddItemAsync(highlight.Id, story.Entry.Id, ct);
return story;
}
}