using System.IO.Compression;
using Febe.Core.Codecs.H264;
using Febe.Core.Domain;
using Febe.Core.Export;
using Febe.Tests.Support;
using Febe.Web.Export;
namespace Febe.Tests.Web;
///
/// The zip of «Scarica» of frames (tappa E3, D14): written while it is sent, through asynchronous writes only (a
/// response body refuses synchronous ones), one entry per frame under the account folder, names made unique.
///
[TestClass]
public sealed class FrameServiceTests
{
public TestContext TestContext { get; set; } = null!;
[TestMethod]
public async Task TheZipIsWrittenThroughAsynchronousWritesOnly()
{
using var temp = new TempFolder("fotogrammi-zip");
var path = temp.Combine("video.mp4");
await File.WriteAllBytesAsync(path, H264PcmWriter.DemoVideo(), TestContext.CancellationToken);
var clock = new ManualClock(Make.T0);
var service = new FrameService(clock);
var instants = new[] { TimeSpan.Zero, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1.5) };
var plan = new FrameDownloadPlan(FrameMode.Count, ExportFormat.Png, [
new FrameDownloadVideo("marta.demo", path, "post", instants, Make.T0.AddDays(-3)),
new FrameDownloadVideo("marta.demo", path, "post", instants[..1], Make.T0.AddDays(-3)),
]);
using var sent = new MemoryStream();
await service.WriteZipAsync(new AsyncOnlyStream(sent), plan, publicationTime: true, TestContext.CancellationToken);
using var archive = new ZipArchive(new MemoryStream(sent.ToArray()), ZipArchiveMode.Read);
CollectionAssert.AreEqual(
new[] { "marta.demo/post_f001.png", "marta.demo/post_f002.png", "marta.demo/post_f003.png", "marta.demo/post_f001 (2).png" },
archive.Entries.Select(e => e.FullName).ToArray());
var published = TimeZoneInfo.ConvertTime(Make.T0.AddDays(-3), clock.LocalZone).DateTime;
foreach (var entry in archive.Entries)
{
Assert.AreEqual(entry.Length, entry.CompressedLength, "frames are stored, not compressed");
// The zip keeps local date and time fields at 2 s steps: the time of publication where Febe runs (D18).
Assert.IsLessThanOrEqualTo(2.0, Math.Abs((entry.LastWriteTime.DateTime - published).TotalSeconds), $"{entry.LastWriteTime} vs {published}");
}
}
/// A response body: asynchronous writes only.
private sealed class AsyncOnlyStream(Stream inner) : Stream
{
public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length => throw new NotSupportedException();
public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
public override void Flush() => throw new InvalidOperationException("synchronous flush");
public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new InvalidOperationException("synchronous write");
public override void Write(ReadOnlySpan buffer) => throw new InvalidOperationException("synchronous write");
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) =>
inner.WriteAsync(buffer, offset, count, cancellationToken);
public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) =>
inner.WriteAsync(buffer, cancellationToken);
}
}