Files
Febe/tests/Febe.Tests/Export/ExportPathsTests.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

184 lines
11 KiB
C#

using Febe.Core.Domain;
using Febe.Core.Export;
using Febe.Core.Library;
using Febe.Core.Storage;
using Febe.Tests.Export.Support;
using Febe.Tests.Support;
namespace Febe.Tests.Export;
/// <summary>
/// Where a copy goes below its destination (spec §4.10 «Nomi dei file», §4.7; decision D18 of tappa E2). The default
/// library scheme names a post of 13 October 2026 08:00 UTC «post/2026/2026-10-13_10-00-00_C&lt;id&gt;_&lt;n&gt;» in Rome.
/// </summary>
[TestClass]
public sealed class ExportPathsTests
{
private static readonly Account Marta = Make.Account("marta.demo") with { Id = 1 };
public TestContext TestContext { get; set; } = null!;
[TestMethod]
public void TheGeneralFolderStartsWithTheAccountSegment()
{
var segments = ExportPaths.Segments(General(), LibraryNaming.Default, Marta, Post(), Photo(2), null, false, TestZones.Rome);
CollectionAssert.AreEqual(new[] { "marta.demo", "post", "2026", "2026-10-13_10-00-00_C500_2.jpg" }, segments.ToArray());
// The account segment is sanitized as a folder name (a reserved name on Windows gets changed).
var aux = Marta with { Username = "aux" };
var first = ExportPaths.Segments(General(), LibraryNaming.Default, aux, Post(), Photo(1), null, false, TestZones.Rome)[0];
Assert.AreEqual(WindowsFileNames.SanitizeSegment("aux"), first);
Assert.AreNotEqual("aux", first);
}
[TestMethod]
public void TheOwnFolderHasNoAccountSegment()
{
var own = General() with { Scope = ExportScope.Account, AccountId = 1 };
var segments = ExportPaths.Segments(own, LibraryNaming.Default, Marta, Post(), Photo(1), null, false, TestZones.Rome);
CollectionAssert.AreEqual(new[] { "post", "2026", "2026-10-13_10-00-00_C500_1.jpg" }, segments.ToArray());
Assert.AreEqual("post/2026/2026-10-13_10-00-00_C500_1.jpg", ExportPaths.RelativePathFor(own, segments));
}
[TestMethod]
public void ComeLaLibreriaFollowsTheCurrentLibrarySchemes()
{
// The library file was named with an older scheme: the copy follows the scheme of now, not the stored path.
var naming = LibraryNaming.Default with { General = NamingScheme.Parse(@"{account}\{anno}-{mese}\{codice}_{n}", NamingSchemeUse.General) };
var old = Photo(1) with { RelativePath = "post/2019/vecchio-nome.JPG" };
CollectionAssert.AreEqual(
new[] { "marta.demo", "2026-10", "C500_1.jpg" },
ExportPaths.Segments(General(), naming, Marta, Post(), old, null, false, TestZones.Rome).ToArray());
// A scheme of the destination wins over the library's; the highlight and profile schemes stay «Come la libreria».
var own = General() with { NamingScheme = @"{tipo}\{data}_{n}" };
CollectionAssert.AreEqual(new[] { "marta.demo", "post", "2026-10-13_1.jpg" }, ExportPaths.Segments(own, naming, Marta, Post(), old, null, false, TestZones.Rome).ToArray());
var highlight = Make.Entry(1, "700", Make.T0, EntryKind.Highlight);
CollectionAssert.AreEqual(
new[] { "marta.demo", "evidenza", "Mare", "2026-10-13_10-00-00_700_1.jpg" },
ExportPaths.Segments(own, naming, Marta, highlight, old, "Mare", true, TestZones.Rome).ToArray());
}
[TestMethod]
public async Task ComeLaLibreriaFollowsTheSchemeSavedInTheSettings()
{
var ct = TestContext.CancellationToken;
await using var bed = await ExportTestBed.CreateAsync();
var post = await bed.PostAsync(bed.Marta, "930", 1);
await bed.Db.Settings.SetAsync(SettingKeys.NamingScheme, @"{account}\{anno}\{codice}_{n}", ct);
await bed.GeneralAsync(ct: ct);
await bed.CopyAccountAsync(bed.Marta, ct: ct);
Assert.IsTrue(File.Exists(Path.Combine(bed.Usb, "marta.demo", "2026", "C930_1.jpg")));
Assert.IsFalse(File.Exists(bed.GeneralCopy(bed.Marta, post.Media[0])));
}
[TestMethod]
public void ImportedFilesWithoutDateKeepTheirName()
{
var imported = Photo(1) with { RelativePath = "importati/vacanze/IMG 0001.JPG" };
CollectionAssert.AreEqual(
new[] { "marta.demo", "importati", "vacanze", "IMG 0001.JPG" },
ExportPaths.Segments(General(), LibraryNaming.Default, Marta, Post(), imported, null, false, TestZones.Rome).ToArray());
var own = General() with { Scope = ExportScope.Account, AccountId = 1 };
CollectionAssert.AreEqual(
new[] { "importati", "vacanze", "IMG 0001.JPG" },
ExportPaths.Segments(own, LibraryNaming.Default, Marta, Post(), imported, null, false, TestZones.Rome).ToArray());
// Only a file the import left in «importati/» keeps its path; a scheme of the destination renames it.
var renamed = ExportPaths.Segments(own with { NamingScheme = @"{anno}\{codice}_{n}" }, LibraryNaming.Default, Marta, Post(), imported, null, false, TestZones.Rome);
CollectionAssert.AreEqual(new[] { "2026", "C500_1.jpg" }, renamed.ToArray());
Assert.ThrowsExactly<ArgumentException>(() => ExportPaths.Segments(General(), LibraryNaming.Default, Marta, Post(), Photo(1) with { RelativePath = null }, null, false, TestZones.Rome));
}
[TestMethod]
public void AStoryInAHighlightRendersAsHighlightOnlyWithoutStories()
{
var story = Make.Entry(1, "800", Make.T0, EntryKind.Story);
var media = Photo(1) with { RelativePath = "storie/2026/x.jpg" };
string[] Render(ContentTypes types, bool inHighlight) =>
[.. ExportPaths.Segments(General() with { ContentTypes = types }, LibraryNaming.Default, Marta, story, media, "Estate", inHighlight, TestZones.Rome)];
CollectionAssert.AreEqual(new[] { "marta.demo", "evidenza", "Estate", "2026-10-13_10-00-00_800_1.jpg" }, Render(ContentTypes.Highlights, inHighlight: true));
CollectionAssert.AreEqual(new[] { "marta.demo", "storie", "2026", "2026-10-13_10-00-00_800_1.jpg" }, Render(ContentTypes.Highlights | ContentTypes.Stories, inHighlight: true));
CollectionAssert.AreEqual(new[] { "marta.demo", "storie", "2026", "2026-10-13_10-00-00_800_1.jpg" }, Render(ContentTypes.Stories, inHighlight: true));
CollectionAssert.AreEqual(new[] { "marta.demo", "storie", "2026", "2026-10-13_10-00-00_800_1.jpg" }, Render(ContentTypes.Highlights, inHighlight: false));
// The content types of D4.
Assert.AreEqual(ContentTypes.Stories | ContentTypes.Highlights, ExportPaths.TypeOf(EntryKind.Story, MediaKind.Video, inHighlight: true));
Assert.AreEqual(ContentTypes.Stories, ExportPaths.TypeOf(EntryKind.Story, MediaKind.Photo, inHighlight: false));
Assert.AreEqual(ContentTypes.PostVideos, ExportPaths.TypeOf(EntryKind.Post, MediaKind.Video, false));
Assert.AreEqual(ContentTypes.Tagged, ExportPaths.TypeOf(EntryKind.Tagged, MediaKind.Video, false));
Assert.AreEqual(ContentTypes.ProfilePicture, ExportPaths.TypeOf(EntryKind.ProfilePicture, MediaKind.Photo, false));
}
[TestMethod]
public void ProfilePicturesUseTheFirstEightDigitsOfTheSha()
{
var profile = Make.Entry(1, "900", Make.T0, EntryKind.ProfilePicture);
var sha = "abcdef0123456789" + new string('0', 48);
var media = Photo(1) with { Sha256 = sha, RelativePath = "profilo/vecchio.jpg" };
CollectionAssert.AreEqual(
new[] { "marta.demo", "profilo", "2026-10-13_10-00-00_abcdef01_1.jpg" },
ExportPaths.Segments(General(), LibraryNaming.Default, Marta, profile, media, null, false, TestZones.Rome).ToArray());
var own = General() with { Scope = ExportScope.Account, AccountId = 1, ProfileNamingScheme = @"foto\{codice}" };
CollectionAssert.AreEqual(new[] { "foto", "abcdef01.jpg" }, ExportPaths.Segments(own, LibraryNaming.Default, Marta, profile, media, null, false, TestZones.Rome).ToArray());
}
[TestMethod]
public void LongPathsAreShortenedTo240WithTheRoot()
{
var root = Path.Combine(Path.GetTempPath(), new string('r', 120));
var deep = General() with { RootPath = root };
var segments = new[] { "marta.demo", new string('a', 60), new string('b', 60) + ".jpg" };
var fitted = ExportPaths.RelativePathFor(deep, segments);
Assert.IsLessThanOrEqualTo(WindowsFileNames.MaxPathLength, Path.TrimEndingDirectorySeparator(root).Length + 1 + fitted.Length);
StringAssert.EndsWith(fitted, ".jpg");
StringAssert.StartsWith(fitted, "marta.demo/");
Assert.AreEqual("marta.demo/x.jpg", ExportPaths.RelativePathFor(deep, ["marta.demo", "x.jpg"])); // short paths are not touched
// A root that leaves no room for a name at all.
var hopeless = General() with { RootPath = Path.Combine(Path.GetTempPath(), new string('r', 235)) };
Assert.ThrowsExactly<PathTooLongException>(() => ExportPaths.RelativePathFor(hopeless, segments));
// The numbered names of the safe writer fit too, keeping the extension (at the limit the number goes into
// the shortened name, as the safe writer does).
Assert.AreEqual("a/b/foto.jpg", ExportPaths.NumberedName(root, "a/b/foto.jpg", 1));
Assert.AreEqual("a/b/foto (2).jpg", ExportPaths.NumberedName(root, "a/b/foto.jpg", 2));
var numbered = ExportPaths.NumberedName(root, fitted, 3);
StringAssert.EndsWith(numbered, ".jpg");
CollectionAssert.AllItemsAreUnique(new[] { fitted, ExportPaths.NumberedName(root, fitted, 2), numbered });
Assert.IsLessThanOrEqualTo(WindowsFileNames.MaxPathLength, Path.TrimEndingDirectorySeparator(root).Length + 1 + numbered.Length);
}
[TestMethod]
public void TheNamingPreviewOfADestinationTakesTheLibrarySchemesWhenNull()
{
var naming = LibraryNaming.Default with { General = NamingScheme.Parse(@"{account}\{anno}\{codice}_{n}", NamingSchemeUse.General) };
var library = NamingPreview.Build(naming.General.Text, naming.Highlight.Text, naming.ProfilePicture.Text, TestZones.Rome, "marta.demo");
var own = ExportNamingPreview.Build(ExportScope.Account, null, null, null, naming, TestZones.Rome, "marta.demo");
Assert.AreEqual(Describe(library), Describe(own));
var general = ExportNamingPreview.Build(ExportScope.General, @"{tipo}\{codice}_{n}", null, null, naming, TestZones.Rome, "marta.demo");
Assert.IsNotEmpty(general.Folders);
Assert.IsTrue(general.Folders.All(f => f.Path[0] == "marta.demo"), Describe(general));
Assert.IsTrue(general.Folders.Any(f => f.Path.Count > 1 && f.Path[1] == "post"), Describe(general));
Assert.IsTrue(general.Folders.Any(f => f.Path.Contains("evidenza")), Describe(general)); // the highlight scheme of the library
}
private static string Describe(NamingPreview preview) =>
string.Join(" | ", preview.Folders.Select(f => string.Join('/', f.Path) + ": " + string.Join(", ", f.Files)));
private static ExportDestination General() => new()
{
Id = 1,
Scope = ExportScope.General,
RootPath = Path.Combine(Path.GetTempPath(), "copie"),
ContentTypes = ContentTypes.All,
CreatedAt = Make.T0,
};
private static Entry Post() => Make.Entry(1, "500", Make.T0);
private static Media Photo(int position) => Make.Stored(Make.Photo(position), Make.Sha(position), $"post/2026/vecchio_{position}.jpg");
}