Files
Febe/tests/Febe.Tests/Export/ExportPathsTests.cs
T
Alby96andClaude Opus 5.5 f5e12c5d74 E3: prove di codec, rimuxaggio, fotogrammi, copie e conversione
Le prove del nucleo della tappa E3 (WP6a), con i media costruiti dalle
prove: il controllo delle foto (stesso formato, HEIC/AVIF/raw, animate,
CMYK e oltre 8 bit, budget di pixel), metadati nei blocchi giusti e
perdite annotate, pixel identici in WEBP senza perdita; il rimuxaggio
da MOV (nuovo TestVideos.Mov) e Matroska a MP4 con campioni, date,
rotazione, lingua ed edit; i fotogrammi H.264 e H.265 all'istante, ogni
k secondi e N in tutto; i suffissi protetti dal taglio a 240; le copie
convertite, com'è con nota, adozione, «già copiato» in ogni forma,
«Copia di nuovo» esatta, spazio sul convertito, fotogrammi nella
cartella delle copie (FrameOnly, una copia per campione, lotti su più
pagine, tetto); la conversione della libreria (perdite, rimuxaggio,
doppioni, importazione delle copie convertite).

Servono a fissare il comportamento di E3 prima dell'unione. Nessun
codice del prodotto cambia; un limite trovato (istanti a meno di 1 ms
in «N in tutto») è annotato per Alberto.

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

231 lines
14 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 TheTargetExtensionReplacesTheSourceOneAndAFrameSuffixFollowsTheStem()
{
// Tappa E3 (WP1.8): a converted copy takes the extension of its format, a frame adds its suffix before it.
var asJpg = new ExportNameOptions(".jpg", null);
var png = Photo(1) with { RelativePath = "post/2026/vecchio_1.PNG" };
CollectionAssert.AreEqual(
new[] { "marta.demo", "post", "2026", "2026-10-13_10-00-00_C500_1.jpg" },
ExportPaths.Segments(General(), LibraryNaming.Default, Marta, Post(), png, null, false, TestZones.Rome, asJpg).ToArray());
var frame = new ExportNameOptions(".png", "_t0m01s250");
CollectionAssert.AreEqual(
new[] { "marta.demo", "post", "2026", "2026-10-13_10-00-00_C500_1_t0m01s250.png" },
ExportPaths.Segments(General(), LibraryNaming.Default, Marta, Post(), png, null, false, TestZones.Rome, frame).ToArray());
// A file of «importati/» keeps its name, with the extension swapped too.
var imported = Photo(1) with { RelativePath = "importati/vacanze/IMG 0001.HEIC.mov" };
CollectionAssert.AreEqual(
new[] { "marta.demo", "importati", "vacanze", "IMG 0001.HEIC.mp4" },
ExportPaths.Segments(General(), LibraryNaming.Default, Marta, Post(), imported, null, false, TestZones.Rome, new ExportNameOptions(".mp4", null)).ToArray());
CollectionAssert.AreEqual(
new[] { "marta.demo", "importati", "vacanze", "IMG 0001.HEIC_f001.png" },
ExportPaths.Segments(General(), LibraryNaming.Default, Marta, Post(), imported, null, false, TestZones.Rome, new ExportNameOptions(".png", "_f001")).ToArray());
// No options: nothing changes.
CollectionAssert.AreEqual(
ExportPaths.Segments(General(), LibraryNaming.Default, Marta, Post(), png, null, false, TestZones.Rome).ToArray(),
ExportPaths.Segments(General(), LibraryNaming.Default, Marta, Post(), png, null, false, TestZones.Rome, new ExportNameOptions(null, null)).ToArray());
}
[TestMethod]
public void TheFrameSuffixSurvivesTheShorteningTo240()
{
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', 80) + "_t12m05s340.png" };
var fitted = ExportPaths.RelativePathFor(deep, segments, keepSuffix: "_t12m05s340");
Assert.IsLessThanOrEqualTo(WindowsFileNames.MaxPathLength, Path.TrimEndingDirectorySeparator(root).Length + 1 + fitted.Length);
StringAssert.EndsWith(fitted, "_t12m05s340.png");
// The numbered names of the safe writer keep it and put the number after it.
var numbered = ExportPaths.NumberedName(root, fitted, 2, keepSuffix: "_t12m05s340");
StringAssert.EndsWith(numbered, "_t12m05s340 (2).png");
Assert.IsLessThanOrEqualTo(WindowsFileNames.MaxPathLength, Path.TrimEndingDirectorySeparator(root).Length + 1 + numbered.Length);
Assert.AreEqual("marta.demo/x_f001.png", ExportPaths.RelativePathFor(deep, ["marta.demo", "x_f001.png"], keepSuffix: "_f001"), "short paths are not touched");
}
[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");
}