using System.Net; using Titano.Core.Archiving; using Titano.Core.Codecs.Jpeg; using Titano.Server.Storage; using Titano.Testing; namespace Titano.Server.Tests.Storage; /// The production composition: uploads committed by the real archive engine, thumbnails built by its job. [Category("server")] public sealed class ArchiveModuleTests(TestContext context) { [Test] public async Task UploadsLandInTheArchiveLayoutAndGetThumbnails() { await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); var (editor, _, _) = await server.UserAsync("alberto", "editor"); var photo = Photo(); var frame = await Uploads.UploadAsync(editor, photo, "IMG_0001.JPG", sessionKey: "roof"); var row = Assert.NotNull(await server.Database.Frames.GetAsync(frame.FrameId)); var sequence = Assert.NotNull(await server.Database.Sequences.GetAsync(frame.SequenceId)); var paths = new ArchivePaths(server.Options.ArchivePath).Sequence(sequence.Folder); Assert.True(File.Exists(paths.ManifestPath), "sequence.json"); Assert.SequenceEqual(photo, await File.ReadAllBytesAsync(Path.Combine(server.Options.ArchivePath, row.RelativePath))); Assert.Count(0, Directory.GetFiles(Path.Combine(server.Options.ArchivePath, ".titano", "staging"))); // The commit queues a thumbnail job; its handler comes from the archive module. var until = DateTime.UtcNow + TimeSpan.FromSeconds(30); while (true) { using var thumb = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/thumb"); if (thumb.StatusCode == HttpStatusCode.OK) { var jpeg = await thumb.Content.ReadAsByteArrayAsync(); Assert.True(jpeg is [0xFF, 0xD8, ..], "a JPEG thumbnail"); break; } Assert.True(DateTime.UtcNow < until, $"thumbnail not built: {(int)thumb.StatusCode}"); await Task.Delay(100); } } private static byte[] Photo() { const int width = 96, height = 64; var pixels = new byte[width * height * 3]; for (var i = 0; i < pixels.Length; i++) pixels[i] = (byte)(i * 7 % 251); return JpegEncoder.Encode(pixels, width, height, 3, new JpegEncodeOptions { Quality = 90 }); } }