Files
Febe/tests/Febe.Tests/Export/ExportFrameNamesTests.cs
T
Alby96andClaude Opus 5.5 2c0cfcccf2 E4: copie del fotogramma con il loro nome, eventi che non si perdono, visualizzatore che si chiude
«Copia di nuovo» di un _fotogramma lo rifaceva come _t…, e un lavoro in attesa prendeva un istante del file vecchio.
Un'analisi copriva l'evento di una copia finita; il visualizzatore restava sopra la pagina nuova e poteva mostrare un
esito superato; la scheda Esportazione perdeva il fuoco a ogni rilettura.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 18:57:58 +01:00

139 lines
7.1 KiB
C#

using System.Security.Cryptography;
using Febe.Core.Codecs.Images;
using Febe.Core.Domain;
using Febe.Core.Export;
using Febe.Core.Library;
using Febe.Core.Library.Conversion;
using Febe.Tests.Codecs.Containers;
namespace Febe.Tests.Export;
/// <summary>
/// Instants, suffixes and labels of frames (tappa E3, D12 and D13), the protected suffix of the shortening and of the
/// numbered names (WP1.8), and the environment-free media conversion (WP1.6b).
/// </summary>
[TestClass]
public sealed class ExportFrameNamesTests
{
[TestMethod]
public void EveryKSecondsStartsAtZeroAndStopsBeforeTheEnd()
{
var instants = ExportFrames.Every(TimeSpan.FromSeconds(2), TimeSpan.FromMilliseconds(500));
CollectionAssert.AreEqual(
new[] { 0, 500, 1000, 1500 }.Select(ms => TimeSpan.FromMilliseconds(ms)).ToArray(),
instants.ToArray());
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => ExportFrames.Every(TimeSpan.FromSeconds(2), TimeSpan.FromMilliseconds(50)));
var over = Assert.ThrowsExactly<ArgumentException>(() => ExportFrames.Every(TimeSpan.FromHours(1), TimeSpan.FromSeconds(1)));
StringAssert.StartsWith(over.Message, ExportFrames.OverCapMessage);
}
[TestMethod]
public void NInAllAreCentred()
{
var instants = ExportFrames.Spread(TimeSpan.FromSeconds(4), 4);
CollectionAssert.AreEqual(
new[] { 500, 1500, 2500, 3500 }.Select(ms => TimeSpan.FromMilliseconds(ms)).ToArray(),
instants.ToArray());
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => ExportFrames.Spread(TimeSpan.FromSeconds(4), 1001));
}
[TestMethod]
public void SuffixesAndLabels()
{
var actual = TimeSpan.FromTicks((12_340 * TimeSpan.TicksPerMillisecond) - 3); // rounded up to 12.340 s
Assert.AreEqual("_t0m12s340", ExportFrames.Suffix(FrameMode.At, actual, 0, 1));
Assert.AreEqual("_t1m05s000", ExportFrames.Suffix(FrameMode.Every, TimeSpan.FromSeconds(65), 3, 9));
Assert.AreEqual("_f001", ExportFrames.Suffix(FrameMode.Count, actual, 0, 12));
Assert.AreEqual("_f0042", ExportFrames.Suffix(FrameMode.Count, actual, 41, 1000));
Assert.AreEqual("_fotogramma", ExportFrames.Suffix(FrameMode.Still, actual, 0, 1), "the same whatever the instant (tappa E4)");
Assert.AreEqual("0:01", ExportFrames.Label(TimeSpan.FromSeconds(1)));
Assert.AreEqual("0:01,25", ExportFrames.Label(TimeSpan.FromMilliseconds(1250)));
Assert.AreEqual("2:03,007", ExportFrames.Label(TimeSpan.FromMilliseconds(123_007)));
}
[TestMethod]
public void AKeptSuffixIsNotCutAndNumberingComesAfterIt()
{
var root = "/destinazione/" + new string('d', 120);
var name = new string('n', 200) + "_t0m12s340.png";
var fitted = WindowsFileNames.FitPath(root, ["post", name], keepSuffix: "_t0m12s340");
Assert.EndsWith("_t0m12s340.png", fitted[^1]);
Assert.IsLessThanOrEqualTo(WindowsFileNames.MaxPathLength, root.Length + fitted.Sum(s => s.Length + 1));
CollectionAssert.AreEqual(
WindowsFileNames.FitPath(root, ["post", name]).ToArray(),
WindowsFileNames.FitPath(root, ["post", name], keepSuffix: null).ToArray(),
"Senza suffisso nulla cambia.");
var numbered = ExportPaths.NumberedName(root, "post/" + name, 2, keepSuffix: "_t0m12s340");
Assert.EndsWith("_t0m12s340 (2).png", numbered);
Assert.AreEqual("post/a_fotogramma (2).png", ExportPaths.NumberedName("/d", "post/a_fotogramma.png", 2, keepSuffix: ExportFrames.SingleSuffix));
Assert.EndsWith("_fotogramma.png", WindowsFileNames.FitPath(root, ["post", new string('n', 200) + "_fotogramma.png"], keepSuffix: ExportFrames.SingleSuffix)[^1]);
Assert.AreEqual("post/a_f001 (3).png", ExportPaths.NumberedName("/d", "post/a_f001.png", 3, keepSuffix: "_f001"));
}
[TestMethod]
public async Task APhotoIsConvertedWithItsShaAndAChangedSourceIsRefused()
{
using var picture = Imaging.TestImages.Gradient(40, 30, seed: 5);
var png = ImageConverter.Encode(picture, ImageFormat.Png, null);
var sha = Convert.ToHexStringLower(SHA256.HashData(png));
using var jpg = await MediaConverter.PrepareAsync(() => new MemoryStream(png), "a/b.png", MediaKind.Photo,
ExportFormat.Jpg, sha, new ConversionThrottle(), CancellationToken.None);
using var output = new MemoryStream();
await jpg.Produce(output, CancellationToken.None);
Assert.AreEqual(ExportVariant.Converted, jpg.Variant);
Assert.AreEqual(".jpg", jpg.Extension);
Assert.AreEqual(jpg.Length, output.Length);
Assert.AreEqual(jpg.Sha256, Convert.ToHexStringLower(SHA256.HashData(output.ToArray())));
CollectionAssert.Contains(jpg.Notes.ToList(), ImageConversionLosses.TransparencyWhite);
using var same = await MediaConverter.PrepareAsync(() => new MemoryStream(png), "a/b.png", MediaKind.Photo,
ExportFormat.Png, sha, ConversionThrottle.Shared, CancellationToken.None);
Assert.AreEqual((ExportVariant.Whole, ExportFormat.Original, sha, ".png"), (same.Variant, same.Format, same.Sha256, same.Extension));
await Assert.ThrowsExactlyAsync<ContentChangedException>(() => MediaConverter.PrepareAsync(() => new MemoryStream(png),
"a/b.png", MediaKind.Photo, ExportFormat.Jpg, new string('0', 64), ConversionThrottle.Shared, CancellationToken.None));
}
[TestMethod]
public async Task AVideoIsRemuxedOrCopiedWithTheReason()
{
var es = ElementaryStream.Load("BA1_Sony_D");
var mkv = TestVideos.Matroska(es);
var sha = Convert.ToHexStringLower(SHA256.HashData(mkv));
using var remux = await MediaConverter.PrepareAsync(() => new MemoryStream(mkv), "v.mkv", MediaKind.Video,
ExportFormat.Mp4, sha, ConversionThrottle.Shared, CancellationToken.None);
using var output = new MemoryStream();
await remux.Produce(output, CancellationToken.None);
Assert.AreEqual((ExportVariant.Converted, ".mp4", (string?)null), (remux.Variant, remux.Extension, remux.Sha256));
Assert.AreEqual(remux.Length, output.Length);
var garbage = new byte[2048];
using var asIs = await MediaConverter.PrepareAsync(() => new MemoryStream(garbage), "v.mov", MediaKind.Video,
ExportFormat.Mp4, sha, ConversionThrottle.Shared, CancellationToken.None);
Assert.AreEqual(ExportVariant.Whole, asIs.Variant);
Assert.HasCount(1, asIs.Notes);
StringAssert.StartsWith(asIs.Notes[0], MediaConversionResult.AsIsNotRemuxable + ":");
}
[TestMethod]
[DataRow("marta/post/a_fotogramma.png", true)]
[DataRow("a_fotogramma (2).webp", true)]
[DataRow("a_fotogramma (12).jpg", true)]
[DataRow("a_t0m01s125.png", false)]
[DataRow("a_fotogramma_t0m01s125.png", false)]
[DataRow("a_fotogramma_f001.png", false)]
[DataRow("a fotogramma.png", false)]
[DataRow("a_fotogramma (x).png", false)]
[DataRow(null, false)]
public void TheNameOfSalvaIlFotogrammaIsKnownAgain(string? path, bool single) =>
Assert.AreEqual(single, ExportFrames.IsSingleName(path));
}