using System.Security.Cryptography; using Febe.Core.Domain; using Febe.Core.Export; using Febe.Core.Library; using Febe.Core.Storage; using Febe.Tests.Codecs.Containers; using Febe.Tests.Export.Support; using Febe.Tests.Support; namespace Febe.Tests.Export; /// /// The copy job of tappa E3 on real folders (WP1.7): a converted copy and a frame copy. The full set of cases is WP6a's; /// the media are built by the tests. /// [TestClass] public sealed class ExportConversionTests { private static readonly byte[] PngSignature = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]; public TestContext TestContext { get; set; } = null!; [TestMethod] public async Task APhotoIsConvertedAndThenCountsAsCopiedInAnyForm() { var ct = TestContext.CancellationToken; await using var bed = await ExportTestBed.CreateAsync(); var post = await bed.PostAsync(bed.Marta, "960", 31); var media = post.Media[0]; await bed.GeneralAsync(ct: ct); var start = await bed.CopyAsync( new ExportJobRequest(new ExportSelection.Media([media.Id]), Formats: new ExportFormatChoice(ExportFormat.Png, null)), ct); var item = (await bed.ItemsAsync(start.Runs.Single().Id, ct)).Single(); Assert.AreEqual((ExportItemState.Copied, ExportVariant.Converted, ExportFormat.Png), (item.State, item.Variant, item.Format)); var copy = (await bed.Db.Exports.GetCopyAsync(item.CopyId!.Value, ct))!; Assert.AreEqual((ExportVariant.Converted, ExportFormat.Png, media.Sha256), (copy.Variant, copy.Format, copy.SourceSha256)); Assert.AreEqual("marta.demo/" + Path.ChangeExtension(media.RelativePath!, ".png"), copy.RelativePath); var file = Path.Combine(bed.Usb, LibraryLayout.ToPlatform(copy.RelativePath!)); var bytes = await File.ReadAllBytesAsync(file, ct); CollectionAssert.AreEqual(PngSignature, bytes[..8]); Assert.AreEqual(Convert.ToHexStringLower(SHA256.HashData(bytes)), copy.CopySha256); Assert.AreNotEqual(media.Sha256, copy.CopySha256); Assert.AreEqual(bytes.Length, copy.Bytes); Assert.IsFalse(File.Exists(bed.GeneralCopy(bed.Marta, media)), "no byte copy next to it"); // «Già copiato» is any form but a frame (D3): a copy of the account in Originale writes nothing more. var again = await bed.CopyAccountAsync(bed.Marta, ct: ct); var second = (await bed.ItemsAsync(again.Runs.Single().Id, ct)).Single(); Assert.AreEqual((ExportItemState.AlreadyCopied, copy.Id, ExportText.Notes.CopiedInAnotherFormat), (second.State, second.CopyId, second.Note)); Assert.AreEqual("Già copiato in un altro formato", ExportText.Note(second.Note)); Assert.HasCount(1, Fingerprint.Of(bed.Usb)); } [TestMethod] public async Task AFrameIsWrittenWithItsInstantAndLeavesTheVideoToCopy() { var ct = TestContext.CancellationToken; await using var bed = await ExportTestBed.CreateAsync(); var mp4 = TestVideos.Mp4(ElementaryStream.Load("BA1_Sony_D")); var post = await bed.Library.SaveAsync(bed.Marta, Make.Entry(bed.Marta.Id, "961", Make.T0.AddDays(-2)), [new TestFile(Make.Video(1), mp4, ".mp4")]); var video = post.Media[0]; await bed.GeneralAsync(ct: ct); var frames = new ExportFramesRequest(FrameMode.At, TimeSpan.Zero, null, null, ExportFormat.Png); var start = await bed.CopyAsync(new ExportJobRequest(new ExportSelection.Media([video.Id]), Frames: frames), ct); var run = await bed.RunAsync(start.Runs.Single().Id, ct); Assert.AreEqual(ExportRunState.Completed, run.State); var item = (await bed.ItemsAsync(run.Id, ct)).Single(); Assert.AreEqual((ExportItemState.Copied, ExportVariant.Frame, ExportFormat.Png, (TimeSpan?)TimeSpan.Zero), (item.State, item.Variant, item.Format, item.FrameAt)); var copy = (await bed.Db.Exports.GetCopyAsync(item.CopyId!.Value, ct))!; Assert.AreEqual((ExportVariant.Frame, ExportFormat.Png, video.Sha256), (copy.Variant, copy.Format, copy.SourceSha256)); Assert.IsNotNull(copy.FrameAt); StringAssert.Contains(copy.RelativePath, "_t0m00s"); StringAssert.EndsWith(copy.RelativePath, ".png"); var bytes = await File.ReadAllBytesAsync(Path.Combine(bed.Usb, LibraryLayout.ToPlatform(copy.RelativePath!)), ct); CollectionAssert.AreEqual(PngSignature, bytes[..8]); Assert.AreEqual(Convert.ToHexStringLower(SHA256.HashData(bytes)), copy.CopySha256); // A frame never makes the file copied (D4): the mark says so, and «Copia in…» copies the video itself. Assert.AreEqual(ExportMark.FrameOnly, (await bed.Db.Exports.GetFileMarksAsync([video.Id], ct))[video.Id]); var whole = await bed.CopyAccountAsync(bed.Marta, ct: ct); var copied = (await bed.ItemsAsync(whole.Runs.Single().Id, ct)).Single(); Assert.AreEqual((ExportItemState.Copied, ExportVariant.Whole), (copied.State, copied.Variant)); Assert.AreEqual(ExportMark.Copied, (await bed.Db.Exports.GetFileMarksAsync([video.Id], ct))[video.Id]); // The same instant asked again is «Già copiato» (its exact identity, D12). var repeat = await bed.CopyAsync(new ExportJobRequest(new ExportSelection.Media([video.Id]), Frames: frames), ct); var same = (await bed.ItemsAsync(repeat.Runs.Single().Id, ct)).Single(); Assert.AreEqual((ExportItemState.AlreadyCopied, copy.Id), (same.State, same.CopyId)); Assert.HasCount(2, Fingerprint.Of(bed.Usb)); } }