using Febe.Core.Domain; using Febe.Core.Library; using Febe.Tests.Support; namespace Febe.Tests.Library; [TestClass] public sealed class NamingSchemeTests { private static readonly TimeZoneInfo Rome = TestZones.Rome; /// A local time in Rome as an instant. private static DateTimeOffset RomeTime(int y, int mo, int d, int h, int mi, int s) => LocalCalendar.FromLocal(new DateTime(y, mo, d, h, mi, s), Rome); [TestMethod] public void DefaultSchemesProduceTheExamplesOfTheSpecification() { // Spec §4.7: marta.demo\post\2026\2026-10-10_18-22-05_DBx3kQ2_2.jpg (the account folder is the root). var post = NamingScheme.General.RenderRelativePath(new NamingValues("marta.demo", EntryKind.Post, RomeTime(2026, 10, 10, 18, 22, 5), "DBx3kQ2", 2, ".jpg"), Rome); Assert.AreEqual("post/2026/2026-10-10_18-22-05_DBx3kQ2_2.jpg", post); var story = NamingScheme.General.RenderRelativePath(new NamingValues("marta.demo", EntryKind.Story, RomeTime(2026, 10, 13, 9, 41, 7), "3742118", 1, "mp4"), Rome); Assert.AreEqual("storie/2026/2026-10-13_09-41-07_3742118_1.mp4", story); var highlight = NamingScheme.Highlight.RenderRelativePath(new NamingValues("marta.demo", EntryKind.Highlight, RomeTime(2025, 8, 2, 10, 11, 12), "3391207", 1, ".JPG", "Estate 2025"), Rome); Assert.AreEqual("evidenza/Estate 2025/2025-08-02_10-11-12_3391207_1.jpg", highlight); // Profile pictures (#38): their own folder, no year folder, the start of the SHA-256 as the code. var picture = NamingScheme.ProfilePicture.RenderRelativePath(new NamingValues("marta.demo", EntryKind.ProfilePicture, RomeTime(2026, 9, 30, 21, 4, 33), "a3f9c2e1", 1, ".jpg"), Rome); Assert.AreEqual("profilo/2026-09-30_21-04-33_a3f9c2e1_1.jpg", picture); } [TestMethod] public void ProfilePicturesKeepTheirFolderWhateverTheGeneralSchemeSays() { // Alberto's decision on #38: the pictures stay in \profilo, apart from posts and stories, even when the // general scheme puts everything in one folder. var flat = NamingScheme.Parse(@"{account}\{data}_{codice}_{n}", NamingSchemeUse.General); var naming = LibraryNaming.Default with { General = flat }; var post = new Entry { AccountId = 1, Kind = EntryKind.Post, InstagramId = "321", Code = "DBx3kQ2", PublishedAt = Make.T0, DiscoveredAt = Make.T0 }; var picture = post with { Kind = EntryKind.ProfilePicture }; Assert.AreSame(flat, naming.For(EntryKind.Post)); Assert.AreSame(flat, naming.For(EntryKind.Story)); Assert.AreSame(flat, naming.For(EntryKind.Tagged)); Assert.AreSame(NamingScheme.Highlight, naming.For(EntryKind.Highlight)); Assert.AreSame(NamingScheme.ProfilePicture, naming.For(EntryKind.ProfilePicture)); Assert.AreEqual("2026-10-13_DBx3kQ2_1.jpg", naming.For(post.Kind).RenderRelativePath(NamingValues.For("marta.demo", post, 1, ".jpg"), Rome)); Assert.AreEqual( "profilo/2026-10-13_10-00-00_00abcdef_1.jpg", naming.For(picture.Kind).RenderRelativePath(NamingValues.For("marta.demo", picture, 1, ".jpg", sha256: "00abcdef" + new string('0', 56)), Rome)); } [TestMethod] public void TheSchemeOfProfilePicturesHasItsOwnRules() { Assert.IsEmpty(NamingScheme.Validate(NamingScheme.DefaultProfilePicture, NamingSchemeUse.ProfilePicture).Warnings); Assert.IsEmpty(NamingScheme.Validate(@"{account}\profilo\{data}_{codice}", NamingSchemeUse.ProfilePicture).Warnings, "{n} is always 1 for a profile picture."); var withoutCode = NamingScheme.Validate(@"{account}\profilo\{data}_{n}", NamingSchemeUse.ProfilePicture); Assert.IsTrue(withoutCode.IsValid); StringAssert.Contains(withoutCode.Warnings.Single(), "Senza {codice} due foto del profilo"); var collection = NamingScheme.Validate(@"{account}\{raccolta}\{codice}", NamingSchemeUse.ProfilePicture); Assert.IsFalse(collection.IsValid); StringAssert.Contains(collection.Errors.Single(), "solo nello schema per le raccolte"); Assert.AreEqual(NamingSchemeUse.ProfilePicture, NamingScheme.ProfilePicture.Use); Assert.IsFalse(NamingScheme.ProfilePicture.ForHighlights); Assert.IsTrue(NamingScheme.Highlight.ForHighlights); Assert.AreSame(NamingScheme.ProfilePicture, NamingScheme.Default(NamingSchemeUse.ProfilePicture)); } [TestMethod] public void DatesAndTimesAreInAlbertosZone() { // 22:30 UTC on 31 December 2025 is already 2026 in Rome (CET, +1): year folder and date follow the local time. var instant = new DateTimeOffset(2025, 12, 31, 23, 30, 0, TimeSpan.Zero); var path = NamingScheme.General.RenderRelativePath(new NamingValues("a", EntryKind.Post, instant, "X", 1, ".jpg"), Rome); Assert.AreEqual("post/2026/2026-01-01_00-30-00_X_1.jpg", path); // Summer time: +2. var summer = new DateTimeOffset(2026, 7, 1, 10, 0, 0, TimeSpan.Zero); Assert.AreEqual("post/2026/2026-07-01_12-00-00_X_1.jpg", NamingScheme.General.RenderRelativePath(new NamingValues("a", EntryKind.Post, summer, "X", 1, ".jpg"), Rome)); // Same instant, UTC zone. Assert.AreEqual("post/2026/2026-07-01_10-00-00_X_1.jpg", NamingScheme.General.RenderRelativePath(new NamingValues("a", EntryKind.Post, summer, "X", 1, ".jpg"), TimeZoneInfo.Utc)); } [TestMethod] public void BackslashAndSlashSeparateFoldersOnEveryPlatform() { var values = new NamingValues("marta.demo", EntryKind.Tagged, RomeTime(2026, 3, 4, 5, 6, 7), "Cd3", 1, ".jpg"); var backslash = NamingScheme.Parse(@"{account}\{tipo}\{anno}-{mese}\{data}_{codice}", false); var slash = NamingScheme.Parse("{account}/{tipo}/{anno}-{mese}/{data}_{codice}", false); Assert.AreEqual("taggati/2026-03/2026-03-04_Cd3.jpg", backslash.RenderRelativePath(values, Rome)); Assert.AreEqual(backslash.RenderRelativePath(values, Rome), slash.RenderRelativePath(values, Rome)); Assert.HasCount(3, backslash.Render(values, Rome)); } [TestMethod] public void SchemesWithoutTheAccountSegmentAreRelativeToTheAccountFolder() { var values = new NamingValues("marta.demo", EntryKind.Post, RomeTime(2026, 3, 4, 5, 6, 7), "Cd3", 2, ".jpg"); Assert.AreEqual("2026/marta.demo_Cd3_2.jpg", NamingScheme.Parse("{anno}/{account}_{codice}_{n}", false).RenderRelativePath(values, Rome)); } [TestMethod] public void PlaceholderValuesAreSanitized() { var values = new NamingValues("marta.demo", EntryKind.Highlight, RomeTime(2026, 3, 4, 5, 6, 7), "1", 1, ".jpg", "Ore 10:30 / sera?"); Assert.AreEqual("evidenza/Ore 10_30 _ sera_/2026-03-04_05-06-07_1_1.jpg", NamingScheme.Highlight.RenderRelativePath(values, Rome)); var reserved = new NamingValues("con", EntryKind.Highlight, RomeTime(2026, 3, 4, 5, 6, 7), "1", 1, ".jpg", "NUL"); var scheme = NamingScheme.Parse(@"{raccolta}\{account}", forHighlights: true); Assert.AreEqual("NUL_/con_.jpg", scheme.RenderRelativePath(reserved, Rome)); var empty = values with { Collection = " " }; Assert.AreEqual("evidenza/senza titolo/2026-03-04_05-06-07_1_1.jpg", NamingScheme.Highlight.RenderRelativePath(empty, Rome)); } [TestMethod] [DataRow("", "vuoto")] [DataRow("{account}\\{tipo}\\", "manca il nome")] [DataRow("{account}\\\\{tipo}", "due barre")] [DataRow("{account}\\{foto}", "Segnaposto sconosciuto: {foto}")] [DataRow("{account}\\{tipo", "non è chiusa")] [DataRow("{account}\\tipo}", "non ha la sua apertura")] [DataRow("{account}\\{}", "vuoto")] [DataRow("{account}\\{raccolta}\\{n}", "solo nello schema per le raccolte")] public void InvalidSchemesAreExplained(string text, string message) { var validation = NamingScheme.Validate(text, forHighlights: false); Assert.IsFalse(validation.IsValid); Assert.IsTrue(validation.Errors.Any(e => e.Contains(message, StringComparison.Ordinal)), string.Join(" | ", validation.Errors)); Assert.ThrowsExactly(() => NamingScheme.Parse(text, false)); } [TestMethod] public void RiskySchemesGetWarnings() { Assert.IsEmpty(NamingScheme.Validate(NamingScheme.DefaultGeneral, false).Warnings); Assert.IsEmpty(NamingScheme.Validate(NamingScheme.DefaultHighlight, true).Warnings); Assert.IsNotEmpty(NamingScheme.Validate(@"{account}\{data}", false).Warnings); Assert.IsNotEmpty(NamingScheme.Validate(@"{account}\{data}_{codice}", false).Warnings); Assert.IsNotEmpty(NamingScheme.Validate(@"{account}\evidenza\{codice}_{n}", true).Warnings); Assert.IsTrue(NamingScheme.Validate(@"{Account}\{TIPO}\{n}", false).IsValid, "I segnaposto non distinguono maiuscole."); } [TestMethod] public void CodeDependsOnTheKind() { var post = new Entry { AccountId = 1, Kind = EntryKind.Post, InstagramId = "321", Code = "DBx3kQ2", PublishedAt = Make.T0, DiscoveredAt = Make.T0 }; Assert.AreEqual("DBx3kQ2", post.NameCode()); Assert.AreEqual("321", (post with { Kind = EntryKind.Story }).NameCode()); Assert.AreEqual("abcdef01", (post with { Kind = EntryKind.ProfilePicture }).NameCode("abcdef0123456789")); Assert.AreEqual("DBx3kQ2", (post with { Kind = EntryKind.Tagged }).NameCode()); var values = NamingValues.For("marta.demo", post with { Kind = EntryKind.ProfilePicture }, 1, ".jpg", sha256: Make.Sha(0xABCDEF)); Assert.AreEqual("profilo/2026/2026-10-13_10-00-00_00000000_1.jpg", NamingScheme.General.RenderRelativePath(values, Rome)); } [TestMethod] public void LayoutResolvesAccountFoldersAndFitsPaths() { using var temp = new TempFolder(); var layout = new LibraryLayout(temp.Combine("libreria")); var account = Make.Account("marta.demo"); Assert.AreEqual(Path.Combine(layout.Root, "marta.demo"), layout.AccountFolder(account)); Assert.AreEqual(Path.Combine(layout.Root, "vecchio.nome"), layout.AccountFolder(account with { Folder = "vecchio.nome" })); var outside = temp.Combine("altrove", "marta"); Assert.AreEqual(outside, layout.AccountFolder(account with { Folder = outside })); Assert.IsNull(layout.FolderSetting(Path.Combine(layout.Root, "marta.demo"), "marta.demo")); Assert.AreEqual("vecchio.nome", layout.FolderSetting(Path.Combine(layout.Root, "vecchio.nome"), "marta.demo")); Assert.AreEqual(outside, layout.FolderSetting(outside, "marta.demo")); Assert.ThrowsExactly(() => LibraryLayout.FullPath(layout.AccountFolder(account), "../fuori.jpg")); var relative = layout.RelativePathFor(layout.AccountFolder(account), NamingScheme.General, new NamingValues("marta.demo", EntryKind.Post, Make.T0, "X", 1, ".jpg"), Rome); Assert.AreEqual("post/2026/2026-10-13_10-00-00_X_1.jpg", relative); Assert.AreEqual(Path.Combine(layout.Root, "marta.demo", "post", "2026", "2026-10-13_10-00-00_X_1.jpg"), layout.FullPath(account, relative)); } [TestMethod] public void PreviewShowsTheTreeOfMockup08() { var preview = NamingPreview.Build(NamingScheme.DefaultGeneral, NamingScheme.DefaultHighlight, NamingScheme.DefaultProfilePicture, Rome, "marta.demo"); Assert.IsTrue(preview.IsValid); Assert.AreEqual("marta.demo", preview.AccountFolder); Assert.HasCount(4, preview.Folders); CollectionAssert.AreEqual(new[] { "post", "2026" }, preview.Folders[0].Path.ToArray()); Assert.AreEqual(new NamingPreviewFile("2026-10-10_18-22-05_DBx3kQ2_2.jpg", MediaKind.Photo), preview.Folders[0].Files.Single()); CollectionAssert.AreEqual(new[] { "storie", "2026" }, preview.Folders[1].Path.ToArray()); Assert.AreEqual(new NamingPreviewFile("2026-10-13_09-41-07_3742118_1.mp4", MediaKind.Video), preview.Folders[1].Files.Single()); CollectionAssert.AreEqual(new[] { "evidenza", "Estate 2025" }, preview.Folders[2].Path.ToArray()); Assert.AreEqual("2025-08-02_10-11-12_3391207_1.jpg", preview.Folders[2].Files.Single().Name); CollectionAssert.AreEqual(new[] { "profilo" }, preview.Folders[3].Path.ToArray()); Assert.AreEqual(new NamingPreviewFile("2026-09-30_21-04-33_a3f9c2e1_1.jpg", MediaKind.Photo), preview.Folders[3].Files.Single()); // The preview reads the same in any zone: sample times are local. var utc = NamingPreview.Build(NamingScheme.DefaultGeneral, NamingScheme.DefaultHighlight, NamingScheme.DefaultProfilePicture, TimeZoneInfo.Utc, "marta.demo"); Assert.AreEqual(preview.Folders[0].Files[0], utc.Folders[0].Files[0]); var flat = NamingPreview.Build(@"{account}\{data}_{codice}_{n}", @"{account}\{raccolta}\{n}", @"{account}\foto-{codice}", Rome); Assert.HasCount(2, flat.Folders); Assert.HasCount(3, flat.Folders[0].Files, "Post, story and profile picture all in the account folder."); var broken = NamingPreview.Build("{account}\\{x}", NamingScheme.DefaultHighlight, NamingScheme.DefaultProfilePicture, Rome); Assert.IsFalse(broken.IsValid); Assert.StartsWith("Schema: ", broken.Errors[0]); Assert.HasCount(2, broken.Folders); // Every problem says which scheme it is about: the settings screen marks that field. var wrongProfile = NamingPreview.Build(NamingScheme.DefaultGeneral, NamingScheme.DefaultHighlight, @"{account}\{raccolta}", Rome); Assert.StartsWith(NamingPreview.Prefix(NamingSchemeUse.ProfilePicture), wrongProfile.Errors.Single()); Assert.AreEqual("Schema per le foto del profilo: ", NamingPreview.Prefix(NamingSchemeUse.ProfilePicture)); var noCode = NamingPreview.Build(NamingScheme.DefaultGeneral, NamingScheme.DefaultHighlight, @"{account}\profilo\{data}", Rome); Assert.StartsWith("Schema per le foto del profilo: Senza {codice}", noCode.Warnings.Single()); } }