using System.Net;
using System.Net.Http.Headers;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Imaging;
using Chronos.Core.Metadata;
using Chronos.Core.Model;
using Chronos.Protocol;
using Chronos.Server.Storage;
using Chronos.Testing;
namespace Chronos.Server.Tests.Storage;
///
/// Proxies of originals the server cannot decode (BRIEF §2.3: HEIF) arrive from clients and render nodes through
/// PUT /api/v1/frames/{id}/proxy and become the frame's proxy and thumbnail.
///
[Category("server")]
public sealed class ProxyUploadTests(TestContext context)
{
[Test]
public async Task AProxySentForAFrameTheServerCannotDecodeIsCheckedKeptAndThumbnailed()
{
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var (viewer, _, _) = await server.UserAsync("vera", "viewer");
var upload = await Uploads.UploadAsync(editor, Heif(1), "IMG_0001.HEIC", sessionKey: "roof");
var frame = Assert.NotNull(await server.Database.Frames.GetAsync(upload.FrameId));
Assert.Equal(ImageFileFormat.Heif, frame.Format);
var manifest = await ManifestAsync(editor, upload.SequenceId);
Assert.Equal((true, false), (manifest.Frames.Single().WantsProxy, manifest.Frames.Single().HasProxy));
Assert.Equal(new ProxySpec(1920, 92), manifest.Proxies);
using (var garbage = await PutAsync(editor, frame.Id, "not a photo"u8.ToArray()))
await garbage.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "invalid_proxy");
using (var other = await PutAsync(editor, frame.Id, Proxy(frame.CapturedAt!.Value.AddSeconds(1))))
await other.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "proxy_mismatch");
var proxy = Proxy(frame.CapturedAt!.Value);
using (var refused = await PutAsync(viewer, frame.Id, proxy))
await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
using (var missing = await PutAsync(editor, Guid.CreateVersion7(), proxy))
await missing.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
using (var stored = await PutAsync(editor, frame.Id, proxy))
await stored.ExpectAsync(HttpStatusCode.NoContent);
await Http.BytesEqualAsync(proxy, editor.GetByteArrayAsync($"/api/v1/frames/{frame.Id}/proxy"));
var after = (await ManifestAsync(editor, upload.SequenceId)).Frames.Single();
Assert.Equal((false, true), (after.WantsProxy, after.HasProxy));
// The thumbnail job the proxy queued draws the frame from it.
var until = DateTime.UtcNow + TimeSpan.FromSeconds(30);
while (true)
{
using var thumb = await editor.GetAsync($"/api/v1/frames/{frame.Id}/thumb");
if (thumb.StatusCode == HttpStatusCode.OK)
{
var jpeg = await thumb.Content.ReadAsByteArrayAsync();
Assert.Equal((320, 240), (JpegDecoder.ReadInfo(jpeg).Width, JpegDecoder.ReadInfo(jpeg).Height));
break;
}
Assert.True(DateTime.UtcNow < until, $"thumbnail not built: {(int)thumb.StatusCode}");
await Task.Delay(100);
}
}
[Test]
public async Task AnotherPhotosProxyInTheFramesPlaceIsAskedForAgainAndNeverServed()
{
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var upload = await Uploads.UploadAsync(editor, Heif(1), "IMG_0001.HEIC", sessionKey: "roof");
var frame = Assert.NotNull(await server.Database.Frames.GetAsync(upload.FrameId));
var sequence = Assert.NotNull(await server.Database.Sequences.GetAsync(upload.SequenceId));
// Proxies are named by frame index: after a frame was inserted before this one, its place holds another photo's.
var path = new Core.Archiving.ArchivePaths(server.Options.ArchivePath).Sequence(sequence.Folder).Proxy(frame.Index);
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
await File.WriteAllBytesAsync(path, Proxy(frame.CapturedAt!.Value.AddSeconds(-10)));
Assert.Equal((true, false), Wanted((await ManifestAsync(editor, upload.SequenceId)).Frames.Single()));
using (var served = await editor.GetAsync($"/api/v1/frames/{frame.Id}/proxy"))
Assert.Equal("asset_missing", (await served.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details);
// The frame's own proxy takes its place.
var proxy = Proxy(frame.CapturedAt!.Value);
using (var stored = await PutAsync(editor, frame.Id, proxy))
await stored.ExpectAsync(HttpStatusCode.NoContent);
Assert.Equal((false, true), Wanted((await ManifestAsync(editor, upload.SequenceId)).Frames.Single()));
await Http.BytesEqualAsync(proxy, editor.GetByteArrayAsync($"/api/v1/frames/{frame.Id}/proxy"));
}
[Test]
public async Task FramesAddedAfterEveryProxyArrivedAreAskedForTheirs()
{
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var first = await Uploads.UploadAsync(editor, Heif(1), "IMG_0001.HEIC", sessionKey: "roof");
var frame = Assert.NotNull(await server.Database.Frames.GetAsync(first.FrameId));
using (var stored = await PutAsync(editor, frame.Id, Proxy(frame.CapturedAt!.Value)))
await stored.ExpectAsync(HttpStatusCode.NoContent);
// A frame comes after the proxy pass found every proxy there: until the next pass the status still says so, yet
// the new frame is asked for its proxy and the first keeps its own.
var second = await Uploads.UploadAsync(editor, Heif(2), "IMG_0002.HEIC", sessionKey: "roof");
Assert.Equal(first.SequenceId, second.SequenceId);
await server.Database.Sequences.UpdateStatusAsync(first.SequenceId, SequenceStatus.ProxiesBuilt, SequenceStatus.None, context.Cancellation);
var frames = (await ManifestAsync(editor, first.SequenceId)).Frames;
Assert.Equal((false, true), Wanted(frames.Single(f => f.Id == first.FrameId)));
Assert.Equal((true, false), Wanted(frames.Single(f => f.Id == second.FrameId)));
}
[Test]
public async Task AProxyTheServerMadeIsServedForAFrameTimedToTheTick()
{
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
var (editor, _, _) = await server.UserAsync("alberto", "editor");
// Seven sub-second digits: the sidecar, and the proxy made from it, keep them all; the index keeps six.
var upload = await Uploads.UploadAsync(editor, Proxy(new DateTime(2026, 9, 28, 18, 2, 11).AddTicks(1_234_567)), "IMG_0001.JPG", sessionKey: "roof");
// The proxy pass the upload queued makes the frame's proxy, and the server serves it.
var until = DateTime.UtcNow + TimeSpan.FromSeconds(30);
while (true)
{
using var proxy = await editor.GetAsync($"/api/v1/frames/{upload.FrameId}/proxy");
if (proxy.StatusCode == HttpStatusCode.OK) break;
Assert.True(DateTime.UtcNow < until, $"proxy not served: {(int)proxy.StatusCode}");
await Task.Delay(100);
}
}
[Test]
public async Task AReportThatFoundAFrameUnreadableBeforeItsProxyCameIsNotServed()
{
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var upload = await Uploads.UploadAsync(editor, Heif(1), "IMG_0001.HEIC", sessionKey: "roof");
var frame = Assert.NotNull(await server.Database.Frames.GetAsync(upload.FrameId));
var sequence = Assert.NotNull(await server.Database.Sequences.GetAsync(upload.SequenceId));
// A deep analysis ran before the proxy came and found no pixels in the HEIF original.
var path = new Core.Archiving.ArchivePaths(server.Options.ArchivePath).Sequence(sequence.Folder).AnalysisPath;
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
Reports.Minimal(1, undecodable: 0).Save(path);
var url = $"/api/v1/sequences/{sequence.Id}/analysis/report";
using (var before = await editor.GetAsync(url))
await before.ExpectAsync(HttpStatusCode.OK);
// Once the frame is read from its proxy, that verdict and its advice to exclude the frame no longer hold, even
// before a light analysis has dropped the deep status.
using (var stored = await PutAsync(editor, frame.Id, Proxy(frame.CapturedAt!.Value)))
await stored.ExpectAsync(HttpStatusCode.NoContent);
using var after = await editor.GetAsync(url);
Assert.Equal("analysis", (await after.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details);
}
[Test]
public async Task FramesTheServerDecodesAskForNoProxy()
{
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var upload = await Uploads.UploadAsync(editor, Proxy(new DateTime(2026, 9, 28, 18, 2, 11)), "IMG_0001.JPG", sessionKey: "roof");
Assert.False((await ManifestAsync(editor, upload.SequenceId)).Frames.Single().WantsProxy);
// Nor takes one: renders and remote views keep the proxy the server makes itself.
using var refused = await PutAsync(editor, upload.FrameId, Proxy(new DateTime(2026, 9, 28, 18, 2, 11)));
await refused.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "proxy_not_wanted");
}
[Test]
public async Task AnswersThatLeaveALargeProxyUnreadReachTheClient()
{
// A full-size proxy is far above the 1 MiB other requests may send: more than the connection buffers while the
// server reads nothing.
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var heif = await Uploads.UploadAsync(editor, Heif(1), "IMG_0001.HEIC", sessionKey: "roof");
var jpeg = await Uploads.UploadAsync(editor, Proxy(new DateTime(2026, 9, 28, 18, 2, 12)), "IMG_0002.JPG", sessionKey: "roof");
var large = Uploads.Photo(24 * 1024 * 1024, 7);
// The handler answers before reading the body: a frame deleted since the manifest, and one the server decodes itself.
using (var missing = await PutAsync(editor, Guid.CreateVersion7(), large))
Assert.Equal("frame", (await missing.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details);
using (var unwanted = await PutAsync(editor, jpeg.FrameId, large))
await unwanted.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "proxy_not_wanted");
// A body that may be the proxy is read whole, past the 1 MiB, before it is judged.
using (var invalid = await PutAsync(editor, heif.FrameId, large))
await invalid.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "invalid_proxy");
// Authentication answers before the endpoint runs: an account that lost the Edit role, and a session signed out meanwhile.
var (viewer, _, _) = await server.UserAsync("vera", "viewer");
using (var forbidden = await PutAsync(viewer, heif.FrameId, large))
await forbidden.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
using (var logout = await editor.PostAsync("/api/v1/auth/logout", null))
await logout.ExpectAsync(HttpStatusCode.NoContent);
using var revoked = await PutAsync(editor, heif.FrameId, large);
await revoked.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized");
}
/// A HEIF file as far as signature sniffing goes (an ftyp box naming heic), with no pixels the server can read.
private static byte[] Heif(int seed) => [0, 0, 0, 24, .. "ftypheic"u8, 0, 0, 0, 0, .. "mif1heic"u8, .. Uploads.Photo(2000, seed)];
/// A 320 × 240 JPEG with in its Exif, as a client makes a proxy.
private static byte[] Proxy(DateTime captured)
{
using var image = new ImageBuffer(320, 240);
for (var c = 0; c < 3; c++) image.Plane(c).Fill(0.3f + c * 0.1f);
var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { CapturedAt = captured, Software = "Chronos" });
return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] });
}
private static (bool WantsProxy, bool HasProxy) Wanted(ManifestFrame frame) => (frame.WantsProxy, frame.HasProxy);
private static Task PutAsync(HttpClient client, Guid frameId, byte[] body)
{
var content = new ByteArrayContent(body);
content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
return client.PutAsync($"/api/v1/frames/{frameId}/proxy", content);
}
private static async Task ManifestAsync(HttpClient client, Guid sequenceId)
{
using var response = await client.GetAsync($"/api/v1/sequences/{sequenceId}/manifest");
return await response.ReadAsync(ProtocolJson.Default.SequenceManifest);
}
}