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; /// /// 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<id>_<n>» in Rome. /// [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(() => 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(() => 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"); }