Files
Febe/tests/Febe.Tests/Library/WindowsFileNamesTests.cs
Claude 3aeaa17da7 M1 Dati e libreria: modello, archivi SQLite con scrittore unico, nomi dei file, scrittura sicura, miniature, impronte, _febe.jsonl, copie, eliminazione e ricostruzione
Modello del dominio e interfacce degli archivi in Febe.Core; implementazione SQLite in Febe.Data (WAL, chiavi esterne, migrazioni numerate in transazione, coda di scrittura su un solo thread); logica della libreria in Febe.Core/Library; 164 prove MSTest, compresa la ricostruzione del database da una libreria di prova e la scala a 200.000 elementi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
2026-10-13 16:06:15 +00:00

125 lines
4.9 KiB
C#

using Febe.Core.Library;
namespace Febe.Tests.Library;
[TestClass]
public sealed class WindowsFileNamesTests
{
[TestMethod]
[DataRow("a<b>c:d\"e/f\\g|h?i*j", "a_b_c_d_e_f_g_h_i_j")]
[DataRow("riga\nnuova\ttab\u0001", "riga_nuova_tab_")]
[DataRow("Estate 2025", "Estate 2025")]
[DataRow("caffè & più", "caffè & più")]
[DataRow("nome...", "nome")]
[DataRow("nome . . ", "nome")]
[DataRow(".", "_")]
[DataRow("..", "_")]
[DataRow(" ", "_")]
[DataRow("", "_")]
[DataRow(".nascosto", ".nascosto")]
public void ForbiddenCharactersAndTrailingDotsAreCleaned(string input, string expected)
{
Assert.AreEqual(expected, WindowsFileNames.SanitizeSegment(input));
}
[TestMethod]
[DataRow("CON", "CON_")]
[DataRow("con", "con_")]
[DataRow("Nul", "Nul_")]
[DataRow("PRN.jpg", "PRN_.jpg")]
[DataRow("aux.tar.gz", "aux_.tar.gz")]
[DataRow("COM1", "COM1_")]
[DataRow("com9.mp4", "com9_.mp4")]
[DataRow("LPT5", "LPT5_")]
[DataRow("COM\u00B9", "COM\u00B9_")]
[DataRow("LPT\u00B3.txt", "LPT\u00B3_.txt")]
[DataRow("CONIN$", "CONIN$_")]
[DataRow("CON.", "CON_")]
[DataRow("CON .txt", "CON _.txt")]
[DataRow("CONSOLE", "CONSOLE")]
[DataRow("COM10", "COM10")]
[DataRow("icon", "icon")]
public void ReservedNamesAreAvoided(string input, string expected)
{
var result = WindowsFileNames.SanitizeSegment(input);
Assert.AreEqual(expected, result);
Assert.IsFalse(WindowsFileNames.IsReservedName(result));
}
[TestMethod]
public void SanitizingIsIdempotent()
{
foreach (var input in new[] { "a:b", "CON", "x. ", "lpt1.jpg", new string('é', 300) + ".jpg", "Ore 10:30 / sera" })
{
var once = WindowsFileNames.SanitizeSegment(input);
Assert.AreEqual(once, WindowsFileNames.SanitizeSegment(once), input);
}
}
[TestMethod]
public void LongSegmentsKeepTheirExtension()
{
var name = new string('a', 400) + ".jpg";
var result = WindowsFileNames.SanitizeSegment(name);
Assert.IsLessThanOrEqualTo(WindowsFileNames.MaxSegmentLength, result.Length);
Assert.EndsWith(".jpg", result);
Assert.Contains("~", result);
}
[TestMethod]
public void ShortPathsAreLeftAlone()
{
var segments = new[] { "post", "2026", "2026-10-10_18-22-05_DBx3kQ2_2.jpg" };
CollectionAssert.AreEqual(segments, WindowsFileNames.FitPath(@"D:\Foto\Febe\marta.demo", segments).ToArray());
}
[TestMethod]
public void LongPathsAreShortenedKeepingExtensionAndUniqueness()
{
var baseFolder = "/libreria/" + new string('b', 120);
var stem = "2026-10-10_18-22-05_" + new string('C', 120);
var first = WindowsFileNames.FitPath(baseFolder, ["post", "2026", stem + "_1.jpg"]);
var second = WindowsFileNames.FitPath(baseFolder, ["post", "2026", stem + "_2.jpg"]);
foreach (var fitted in new[] { first, second })
{
var total = baseFolder.Length + fitted.Sum(s => s.Length + 1);
Assert.IsLessThanOrEqualTo(WindowsFileNames.MaxPathLength, total);
Assert.EndsWith(".jpg", fitted[^1]);
Assert.AreEqual("post", fitted[0]);
Assert.StartsWith("2026-10-10_18-22-05_", fitted[^1]);
}
Assert.AreNotEqual(first[^1], second[^1], "Due file diversi restano diversi anche accorciati.");
CollectionAssert.AreEqual(first.ToArray(), WindowsFileNames.FitPath(baseFolder, ["post", "2026", stem + "_1.jpg"]).ToArray(), "Accorciare è deterministico.");
}
[TestMethod]
public void LongFolderNamesAreShortenedWhenTheFileNameIsNotEnough()
{
var baseFolder = "/libreria/marta.demo";
var title = "Una raccolta in evidenza con un titolo davvero molto lungo " + new string('x', 200);
var fitted = WindowsFileNames.FitPath(baseFolder, ["evidenza", WindowsFileNames.SanitizeSegment(title), "2025-08-02_10-11-12_3391207_1.jpg"]);
Assert.IsLessThanOrEqualTo(WindowsFileNames.MaxPathLength, baseFolder.Length + fitted.Sum(s => s.Length + 1));
Assert.StartsWith("Una raccolta", fitted[1]);
Assert.EndsWith(".jpg", fitted[2]);
}
[TestMethod]
public void ABaseFolderThatIsTooLongIsReported()
{
var baseFolder = "/" + new string('z', 236);
var error = Assert.ThrowsExactly<PathTooLongException>(() => WindowsFileNames.FitPath(baseFolder, ["post", "a.jpg"]));
Assert.Contains("troppo lunga", error.Message);
}
[TestMethod]
public void SplitsExtensions()
{
Assert.AreEqual(("foto", ".jpg"), WindowsFileNames.SplitExtension("foto.jpg"));
Assert.AreEqual((".nascosto", string.Empty), WindowsFileNames.SplitExtension(".nascosto"));
Assert.AreEqual(("senza", string.Empty), WindowsFileNames.SplitExtension("senza"));
Assert.AreEqual(("a.b", ".c"), WindowsFileNames.SplitExtension("a.b.c"));
}
}