using System.Net; using Microsoft.Extensions.DependencyInjection; using Titano.Core.Archiving; using Titano.Core.Codecs.Jpeg; using Titano.Core.Model; using Titano.Server.Jobs; 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); } } [Test] public async Task AServerOnSqliteBacksUpItsIndexOnStart() { await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); var backups = Path.Combine(server.Options.DataPath, "backups"); // No copy yet, so the first one is made at once (BRIEF ยง6.2), named after the index it copies. var until = DateTime.UtcNow + TimeSpan.FromSeconds(30); while (!Directory.Exists(backups) || Directory.GetFiles(backups, "*.db").Length == 0) { Assert.True(DateTime.UtcNow < until, "no backup of the index"); await Task.Delay(50); } var path = Assert.NotNull(((Titano.Data.Sqlite.SqliteDatabase)server.Database.Database).Path); var backup = Titano.Data.DatabaseBackups.List(backups, path).Single(); var summary = await Titano.Data.DatabaseBackups.SummarizeBackupAsync(backup.Path, context.CreateTempDirectory("scratch"), context.Cancellation); Assert.Equal(server.Database.SchemaVersion, summary.SchemaVersion); } [Test] public async Task PhotosAlreadyInTheArchiveAndAddedLaterAreIndexedByPolling() { var photo = Photo(); // An adopted folder that already holds photos. await using var server = await TestServer.StartAsync(context, options => { Directory.CreateDirectory(Path.Combine(options.ArchivePath, "roof")); File.WriteAllBytes(Path.Combine(options.ArchivePath, "roof", "IMG_0001.JPG"), photo); }, ArchiveModule.Register); var archive = server.Services.GetRequiredService(); await UntilIndexedAsync(server, archive.Id, "roof"); await UntilScannedAsync(server); // Another writer adds a folder: the next poll queues a scan, but not while one is pending. Directory.CreateDirectory(Path.Combine(server.Options.ArchivePath, "garden")); File.WriteAllBytes(Path.Combine(server.Options.ArchivePath, "garden", "IMG_0002.JPG"), Photo(seed: 3)); var polling = server.Services.GetRequiredService(); var paused = new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Scan, TargetIds = [archive.Id], State = JobState.Paused, Created = DateTimeOffset.UtcNow, }; await server.Database.Jobs.EnqueueAsync(paused, context.Cancellation); Assert.Null(await polling.PollAsync(context.Cancellation)); await server.Services.GetRequiredService().Engine.CancelAsync(paused.Id, context.Cancellation); var scan = Assert.NotNull(await polling.PollAsync(context.Cancellation)); Assert.Equal(JobPriority.Background, scan.Priority); await UntilIndexedAsync(server, archive.Id, "garden"); } private static async Task UntilIndexedAsync(TestServer server, Guid archiveId, string folder) { var until = DateTime.UtcNow + TimeSpan.FromSeconds(30); while (!(await server.Database.Sequences.ListLocationsAsync(archiveId)).Any(l => l.Folder == folder)) { Assert.True(DateTime.UtcNow < until, $"{folder} was not indexed"); await Task.Delay(50); } } /// Waits for the scan queued at start to finish: its sequences are indexed before it ends. private static async Task UntilScannedAsync(TestServer server) { var until = DateTime.UtcNow + TimeSpan.FromSeconds(30); foreach (var state in new[] { JobState.Queued, JobState.Running }) { while ((await server.Database.Jobs.ListAsync(state, JobKind.Scan)).Items.Count > 0) { Assert.True(DateTime.UtcNow < until, "the scan at start did not finish"); await Task.Delay(50); } } } private static byte[] Photo(int seed = 0) { 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 + seed) % 251); return JpegEncoder.Encode(pixels, width, height, 3, new JpegEncodeOptions { Quality = 90 }); } }