The server indexed only what arrived through its own uploads, so photos already in an adopted folder and sequences another writer added stayed unknown until an administrator asked for a rescan. ArchivePolling, registered by the archive module, queues an incremental scan of the online archive at start and every ten minutes at background priority, unless a scan or rebuild of it is waiting, running or paused (ENG-13). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
115 lines
5.2 KiB
C#
115 lines
5.2 KiB
C#
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;
|
|
|
|
/// <summary>The production composition: uploads committed by the real archive engine, thumbnails built by its job.</summary>
|
|
[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 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<ServerArchive>();
|
|
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<ArchivePolling>();
|
|
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<JobEngineHost>().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);
|
|
}
|
|
}
|
|
|
|
/// <summary>Waits for the scan queued at start to finish: its sequences are indexed before it ends.</summary>
|
|
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 });
|
|
}
|
|
}
|