merge: server archive fixes (V-5, V-8, V-11, V-16, V-35, V-36)

Merge branch fix2/server-archive into the verification branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 16:54:04 +00:00
commit 61c7e9e7e3
19 files changed
+671 -39

No files matched your search

@@ -177,5 +177,21 @@ public sealed class AdminTests(TestContext context)
{
await File.WriteAllBytesAsync(path, saved, context.Cancellation);
}
// A policy this form cannot set, written out of range by another tool, is named so the admin can correct the file.
var current = Titano.Core.Archiving.ManifestStore.ReadArchive(path);
foreach (var (odd, property) in new[]
{
(current with { ThumbnailPolicy = new Titano.Core.Archiving.ThumbnailPolicy { LongEdge = 2048 } }, "thumbnailPolicy"),
(current with { ProxyPolicy = current.ProxyPolicy with { Quality = 0 } }, "proxyPolicy"),
})
{
Titano.Core.Archiving.ManifestStore.WriteArchive(path, odd);
var before = await File.ReadAllBytesAsync(path, context.Cancellation);
using var refused = await admin.PutAsJsonAsync("/api/v1/archive/settings", changed with { ProxyLongEdge = 1280 }, ProtocolJson.Default.ArchiveSettingsDto);
Assert.Equal(property, (await refused.ExpectErrorAsync(HttpStatusCode.Conflict, "archive_policy")).Details);
Assert.SequenceEqual(before, await File.ReadAllBytesAsync(path, context.Cancellation));
}
await File.WriteAllBytesAsync(path, saved, context.Cancellation);
}
}
@@ -68,7 +68,7 @@ public sealed class ArchiveModuleTests(TestContext context)
}
[Test]
public async Task PhotosAlreadyInTheArchiveAndAddedLaterAreIndexedByPolling()
public async Task PhotosAlreadyInTheArchiveAndAddedLaterAreIndexedByPollingWithThumbnailsAndProxies()
{
var photo = Photo();
// An adopted folder that already holds photos.
@@ -78,8 +78,11 @@ public sealed class ArchiveModuleTests(TestContext context)
File.WriteAllBytes(Path.Combine(options.ArchivePath, "roof", "IMG_0001.JPG"), photo);
}, ArchiveModule.Register);
var archive = server.Services.GetRequiredService<ServerArchive>();
var (editor, _, _) = await server.UserAsync("alberto", "editor");
await UntilIndexedAsync(server, archive.Id, "roof");
await UntilScannedAsync(server);
// The scan queues the thumbnails and proxies clients load from the server, as an upload does.
await UntilImagesAsync(server, editor, archive.Id, "roof");
// 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"));
@@ -100,6 +103,7 @@ public sealed class ArchiveModuleTests(TestContext context)
var scan = Assert.NotNull(await polling.PollAsync(context.Cancellation));
Assert.Equal(JobPriority.Background, scan.Priority);
await UntilIndexedAsync(server, archive.Id, "garden");
await UntilImagesAsync(server, editor, archive.Id, "garden");
}
private static async Task UntilIndexedAsync(TestServer server, Guid archiveId, string folder)
@@ -112,6 +116,30 @@ public sealed class ArchiveModuleTests(TestContext context)
}
}
/// <summary>Waits until the first frame of the sequence in <paramref name="folder"/> has its thumbnail and proxy served.</summary>
private static async Task UntilImagesAsync(TestServer server, HttpClient client, Guid archiveId, string folder)
{
var sequence = (await server.Database.Sequences.ListLocationsAsync(archiveId)).Single(l => l.Folder == folder);
var frame = (await server.Database.Frames.ListBySequenceAsync(sequence.Id)).First();
var until = DateTime.UtcNow + TimeSpan.FromSeconds(30);
foreach (var asset in new[] { "thumb", "proxy" })
{
while (true)
{
using var response = await client.GetAsync($"/api/v1/frames/{frame.Id}/{asset}");
if (response.StatusCode == HttpStatusCode.OK)
{
Assert.True(await response.Content.ReadAsByteArrayAsync() is [0xFF, 0xD8, ..], $"a JPEG {asset} of {folder}");
break;
}
Assert.True(DateTime.UtcNow < until, $"no {asset} of {folder}: {(int)response.StatusCode}");
await Task.Delay(100);
}
}
var built = Assert.NotNull(await server.Database.Sequences.GetAsync(sequence.Id)).Status;
Assert.True(built.HasFlag(SequenceStatus.ThumbsBuilt) && built.HasFlag(SequenceStatus.ProxiesBuilt), $"{folder}: {built}");
}
/// <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)
{
@@ -170,6 +170,35 @@ public sealed class UploadTests(TestContext context)
await oversized.ExpectErrorAsync(HttpStatusCode.BadRequest, "chunk_size");
}
[Test]
public async Task AnswersThatLeaveALargeChunkUnreadReachTheClient()
{
// Chunks far above the 1 MiB other requests may send: more than the connection buffers while the server reads nothing.
const int large = 32 * 1024 * 1024;
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = large);
var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader");
var data = Uploads.Photo(24 * 1024 * 1024, 16);
var id = (await Uploads.StartAsync(uploader, data)).UploadId!.Value;
using (var first = await Uploads.PutChunkAsync(uploader, id, 0, data)) await first.ExpectAsync(HttpStatusCode.OK);
// The client lost the answer and sends the chunk again: the same answer, not a closed connection.
using (var again = await Uploads.PutChunkAsync(uploader, id, 0, data))
{
await again.ExpectAsync(HttpStatusCode.OK);
Assert.SequenceEqual([0], (await again.ReadAsync(ProtocolJson.Default.ChunkReceived)).Received);
}
using (var unknown = await Uploads.PutChunkAsync(uploader, Guid.CreateVersion7(), 0, data)) await unknown.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
using (var complete = await uploader.PostAsync($"/api/v1/uploads/{id}/complete", null)) await complete.ExpectAsync(HttpStatusCode.OK);
using (var late = await Uploads.PutChunkAsync(uploader, id, 0, data)) await late.ExpectErrorAsync(HttpStatusCode.Conflict, "upload_state");
// Authentication answers before the endpoint runs: a user who may not upload, and a session signed out meanwhile.
var (viewer, _, _) = await server.UserAsync("marco", "viewer");
using (var forbidden = await Uploads.PutChunkAsync(viewer, id, 0, data)) await forbidden.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
using (var logout = await uploader.PostAsync("/api/v1/auth/logout", null)) await logout.ExpectAsync(HttpStatusCode.NoContent);
using var revoked = await Uploads.PutChunkAsync(uploader, id, 0, data);
await revoked.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized");
}
[Test]
public async Task RenderUploadsNeedTheEditPermission()
{