Files
Chronos/tests/Chronos.Server.Tests/Storage/BrowserUploadTests.cs
T
Alby96 316c182b19 fix(sync,upload): keep tombstones honest and let an explicit upload bring a photo back
Sync:
- A server frame that has not been hashed is known by content only once it is
  downloaded: one this PC deleted (or holds as its replacement) is now deleted
  from staging instead of filed again, which also dropped its tombstone.
- An unhashed local original that turns out to be a photo the server deleted
  is not uploaded, and the archive verify then writes the hashes into the
  index, so the row settles instead of offering Upload for good.
- A tombstone leaves a photo out of the comparison only when its side no longer
  holds that content, so a tombstone left beside a held copy does not keep the
  other side's copy missing forever.

Uploads:
- StartUploadRequest.Restore (optional, never sent by sync or older clients)
  lets an explicit user upload file a photo deleted from the sequence again,
  also after the trash was emptied; the completion repeats it with
  ?restore=true and the ingest drops the tombstone.
- The completion checks the tombstone again: a photo deleted while its bytes
  were on the way is not filed (409 photo_removed, upload cancelled); the
  protocol client reports that as not sent.
- A tombstone of content a frame of the target still holds refuses no repair.
- Hidden dot names (AppleDouble "._" files) are refused, as the archive
  scanner leaves them out.

Web upload dialog:
- Upload again on rows marked Deleted before; the tooltip no longer promises
  a trash restore that may be impossible.
- Sends a capture time: the Exif DateTimeOriginal of a JPEG or TIFF-based file
  (minimal in-house reader, capture-time.js), else the file's time, so photos
  without Exif keep their order.
- Esc is stopped at keydown during a run (Chromium allows one prevented cancel
  per activation); hidden dot files are left out; a completion refused while
  another one files the upload waits for it; the same photo picked under two
  names waits for its first copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit d051bcfc351f5a08500ca864a6508576a95492a4)
2026-10-27 12:45:33 +01:00

438 lines
27 KiB
C#

using System.Globalization;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Extensions.DependencyInjection;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Protocol;
using Chronos.Server.Storage;
using Chronos.Testing;
namespace Chronos.Server.Tests.Storage;
/// <summary>
/// The admin panel's upload dialog (owner review 2, R3; <c>Admin/web/upload.js</c>) speaks the upload API through the
/// cookie session: requests shaped like <c>api.js</c> sends them (JSON bodies written as the page writes them, the
/// page's language, the CSRF header on every unsafe call, chunks as octet-stream), against the real archive engine.
/// </summary>
[Category("server")]
public sealed class BrowserUploadTests(TestContext context)
{
private const int Chunk = 64 * 1024;
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(TestTiming.Budget(30));
[Test]
public async Task TheUploadDialogFilesPhotosThroughTheCookieSession()
{
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register);
var browser = await Browser.SignInAsync(server);
// What the dialog offers and checks before it hashes anything.
ServerInfo info;
using (var response = await browser.SendAsync(HttpMethod.Get, "/api/v1/server/info"))
info = await response.ReadAsync(ProtocolJson.Default.ServerInfo);
var extensions = Assert.NotNull(info.Extensions);
foreach (var extension in new[] { "jpg", "jpeg", "tif", "dng", "cr3", "nef", "heic" }) Assert.Contains(extension, extensions);
Assert.True(extensions.All(e => e.Length > 0 && e == e.ToLowerInvariant() && !e.StartsWith('.')), string.Join(",", extensions));
// New sequence: the title as typed, no camera hint.
SequenceDto sequence;
using (var created = await browser.SendAsync(HttpMethod.Post, "/api/v1/sequences", Json("""{"title":"Roof","cameraHint":null}""")))
{
await created.ExpectAsync(HttpStatusCode.Created);
sequence = await created.ReadAsync(ProtocolJson.Default.SequenceDto);
}
var photos = new[] { Photo(1), Photo(2) };
var frames = new List<CompleteUploadResponse>();
for (var i = 0; i < photos.Length; i++)
{
var photo = photos[i];
var start = await StartAsync(browser, sequence.Id, $"IMG_{i + 1:0000}.JPG", photo, HttpStatusCode.Created);
var id = Assert.NotNull(start.UploadId);
Assert.Equal(Chunk, start.ChunkSize);
var count = (photo.Length + Chunk - 1) / Chunk;
Assert.True(count >= 2, $"{photo.Length} bytes make one chunk");
// putBytes sends the session's headers: a chunk without the CSRF header is refused like any other change.
using (var refused = await browser.SendAsync(HttpMethod.Put, $"/api/v1/uploads/{id}/chunks/0", Bytes(photo, 0), csrf: false))
await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "csrf_failed");
// Two chunks in flight, finishing in any order.
for (var last = count - 1; last >= 0; last -= 2)
{
var pair = new[] { last, last - 1 }.Where(n => n >= 0).Select(async n =>
{
using var put = await browser.SendAsync(HttpMethod.Put, $"/api/v1/uploads/{id}/chunks/{n}", Bytes(photo, n));
return await put.ReadAsync(ProtocolJson.Default.ChunkReceived);
});
foreach (var received in await Task.WhenAll(pair)) Assert.Contains(received.Index, received.Received);
}
using var complete = await browser.SendAsync(HttpMethod.Post, $"/api/v1/uploads/{id}/complete", Json("{}"));
var frame = await complete.ReadAsync(ProtocolJson.Default.CompleteUploadResponse);
Assert.Equal(sequence.Id, frame.SequenceId);
frames.Add(frame);
}
var stored = await server.Database.Frames.ListBySequenceAsync(sequence.Id);
Assert.SequenceEqual(frames.Select(f => f.FrameId).Order(), stored.Select(f => f.Id).Order());
for (var i = 0; i < photos.Length; i++)
{
var file = Path.Combine(server.Options.ArchivePath, stored.Single(f => f.Id == frames[i].FrameId).RelativePath);
await Http.BytesEqualAsync(photos[i], File.ReadAllBytesAsync(file));
}
// Picked again: already there, with its frame.
var again = await StartAsync(browser, sequence.Id, "IMG_0001.JPG", photos[0], HttpStatusCode.OK);
Assert.True(again.AlreadyExists);
Assert.Equal(frames[0].FrameId, again.FrameId);
// Deleted from the sequence: also answered as there, without a frame ("deleted before": restore it from the trash, or
// upload it again).
await DeleteAsync(server, browser, sequence.Id, frames[1].FrameId);
var deleted = await StartAsync(browser, sequence.Id, "IMG_0002.JPG", photos[1], HttpStatusCode.OK);
Assert.True(deleted.AlreadyExists);
Assert.Null(deleted.UploadId);
Assert.Null(deleted.FrameId);
// Cancel: an upload the dialog started is discarded on the server.
var cancelled = Photo(3);
var started = await StartAsync(browser, sequence.Id, "IMG_0003.JPG", cancelled, HttpStatusCode.Created);
var cancelledId = Assert.NotNull(started.UploadId);
using (var put = await browser.SendAsync(HttpMethod.Put, $"/api/v1/uploads/{cancelledId}/chunks/0", Bytes(cancelled, 0)))
await put.ExpectAsync(HttpStatusCode.OK);
using (var discard = await browser.SendAsync(HttpMethod.Delete, $"/api/v1/uploads/{cancelledId}"))
await discard.ExpectAsync(HttpStatusCode.NoContent);
using (var state = await browser.SendAsync(HttpMethod.Get, $"/api/v1/uploads/{cancelledId}"))
Assert.Equal("cancelled", (await state.ReadAsync(ProtocolJson.Default.UploadState)).State);
using (var late = await browser.SendAsync(HttpMethod.Put, $"/api/v1/uploads/{cancelledId}/chunks/1", Bytes(cancelled, 1)))
await late.ExpectErrorAsync(HttpStatusCode.Conflict, "upload_state");
Assert.False(server.Services.GetRequiredService<UploadStore>().Exists(cancelledId), "the partial file is gone");
}
[Test]
public async Task AQuotaRefusalCarriesTheMessageTheDialogShows()
{
// A quota or space refusal stops the dialog's run with the server's message, in the page's language.
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk);
var browser = await Browser.SignInAsync(server);
Guid me;
using (var response = await browser.SendAsync(HttpMethod.Get, "/api/v1/auth/me")) me = (await response.ReadAsync(ProtocolJson.Default.UserDto)).Id;
using (var quota = await browser.SendAsync(HttpMethod.Patch, $"/api/v1/users/{me}", Json("""{"quotaBytes":1000}""")))
await quota.ExpectAsync(HttpStatusCode.OK);
using var start = await browser.SendAsync(HttpMethod.Post, "/api/v1/uploads", StartBody(null, "IMG_0001.JPG", Uploads.Photo(Chunk + 10, 7)));
var error = await start.ExpectErrorAsync(HttpStatusCode.Forbidden, "quota_exceeded");
Assert.Equal(Loc.Get("Server.Error.QuotaExceeded", CultureInfo.GetCultureInfo("it")), error.Message);
}
[Test]
public async Task UploadAgainBringsBackAPhotoDeletedBeforeEvenAfterTheTrashIsEmptied()
{
// Owner review 2, R6 and R3: a tombstone outlives the trash and keeps sync and old clients from filing the photo
// again, but the user who picks it in the dialog and chooses Upload again gets it back into the sequence.
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register);
var browser = await Browser.SignInAsync(server);
var sequence = await CreateAsync(browser, "Roof");
var photo = Photo(11);
var sha = Uploads.Sha(photo);
// A sequence keeps one photo at least: the other is the one deleted.
var keeper = await UploadAsync(browser, sequence.Id, "IMG_0000.JPG", Photo(10));
var first = await UploadAsync(browser, sequence.Id, "IMG_0001.JPG", photo);
await DeleteAsync(server, browser, sequence.Id, first.FrameId);
await EmptyTrashAsync(server, browser);
Assert.True(await server.Database.FrameRemovals.IsRemovedAsync(sequence.Id, sha), "the tombstone outlives the trash");
// Picked again: deleted before, nothing started.
var refused = await StartAsync(browser, sequence.Id, "IMG_0001.JPG", photo, HttpStatusCode.OK);
Assert.True(refused is { AlreadyExists: true, UploadId: null, FrameId: null }, "answered as deleted before");
// Upload again whose completion does not repeat the restore (an older page): refused at the end, the upload dropped.
var started = await StartAsync(browser, sequence.Id, "IMG_0001.JPG", photo, HttpStatusCode.Created, restore: true);
var dropped = Assert.NotNull(started.UploadId);
await SendChunksAsync(browser, dropped, photo);
using (var complete = await CompleteAsync(browser, dropped))
{
var error = await complete.ExpectErrorAsync(HttpStatusCode.Conflict, UploadErrors.PhotoRemoved);
Assert.Equal(Loc.Get("Server.Error.PhotoRemoved", CultureInfo.GetCultureInfo("it")), error.Message);
}
using (var state = await browser.SendAsync(HttpMethod.Get, $"/api/v1/uploads/{dropped}"))
Assert.Equal("cancelled", (await state.ReadAsync(ProtocolJson.Default.UploadState)).State);
Assert.False(server.Services.GetRequiredService<UploadStore>().Exists(dropped), "the partial file is gone");
Assert.True(await server.Database.FrameRemovals.IsRemovedAsync(sequence.Id, sha), "still deleted");
// Upload again, start and completion: filed into the sequence, and its tombstone is gone.
var back = await UploadAsync(browser, sequence.Id, "IMG_0001.JPG", photo, restore: true);
Assert.Equal(sequence.Id, back.SequenceId);
var frames = await server.Database.Frames.ListBySequenceAsync(sequence.Id);
Assert.SequenceEqual([keeper.FrameId, back.FrameId], frames.Select(f => f.Id).Order());
Assert.Equal(sha, frames.Single(f => f.Id == back.FrameId).Sha256);
Assert.False(await server.Database.FrameRemovals.IsRemovedAsync(sequence.Id, sha), "the tombstone went with the upload");
var again = await StartAsync(browser, sequence.Id, "IMG_0001.JPG", photo, HttpStatusCode.OK);
Assert.Equal(back.FrameId, again.FrameId);
}
[Test]
public async Task UploadAgainFilesThePhotoIntoItsSequenceWhateverOtherSequencesHold()
{
// A copy in another sequence stands for an ordinary upload; one brought back on purpose goes where the user put it.
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register);
var browser = await Browser.SignInAsync(server);
var roof = await CreateAsync(browser, "Roof");
var yard = await CreateAsync(browser, "Yard");
var photo = Photo(12);
await UploadAsync(browser, roof.Id, "IMG_0000.JPG", Photo(10));
var first = await UploadAsync(browser, roof.Id, "IMG_0001.JPG", photo);
await DeleteAsync(server, browser, roof.Id, first.FrameId);
var elsewhere = await UploadAsync(browser, yard.Id, "IMG_0001.JPG", photo);
var deleted = await StartAsync(browser, roof.Id, "IMG_0001.JPG", photo, HttpStatusCode.OK);
Assert.Null(deleted.FrameId);
var back = await UploadAsync(browser, roof.Id, "IMG_0001.JPG", photo, restore: true);
Assert.Equal(roof.Id, back.SequenceId);
Assert.NotEqual(elsewhere.FrameId, back.FrameId);
Assert.Count(1, await server.Database.Frames.ListBySequenceAsync(yard.Id));
}
[Test]
public async Task AnUploadCompletedAfterItsPhotoWasDeletedDoesNotBringItBack()
{
// Owner review 2, R6: the start checked the tombstones, but the photo can be deleted while its bytes are on the way
// (here a repair of a copy whose original is gone, deleted from the Library meanwhile).
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register);
var browser = await Browser.SignInAsync(server);
var sequence = await CreateAsync(browser, "Roof");
var photo = Photo(13);
var sha = Uploads.Sha(photo);
var keeper = await UploadAsync(browser, sequence.Id, "IMG_0000.JPG", Photo(10));
var first = await UploadAsync(browser, sequence.Id, "IMG_0001.JPG", photo);
await UntilIdleAsync(server);
var frame = Assert.NotNull(await server.Database.Frames.GetAsync(first.FrameId));
File.Delete(Path.Combine(server.Options.ArchivePath, frame.RelativePath));
var repair = await StartAsync(browser, sequence.Id, "IMG_0001.JPG", photo, HttpStatusCode.Created);
var id = Assert.NotNull(repair.UploadId);
await SendChunksAsync(browser, id, photo);
await DeleteAsync(server, browser, sequence.Id, first.FrameId);
using (var complete = await CompleteAsync(browser, id))
await complete.ExpectErrorAsync(HttpStatusCode.Conflict, UploadErrors.PhotoRemoved);
Assert.SequenceEqual([keeper.FrameId], (await server.Database.Frames.ListBySequenceAsync(sequence.Id)).Select(f => f.Id));
Assert.True(await server.Database.FrameRemovals.IsRemovedAsync(sequence.Id, sha), "the tombstone stays");
Assert.False(server.Services.GetRequiredService<UploadStore>().Exists(id), "the partial file is gone");
// Completing again changes nothing: the upload is over.
using (var late = await CompleteAsync(browser, id))
await late.ExpectErrorAsync(HttpStatusCode.Conflict, "upload_state");
Assert.SequenceEqual([keeper.FrameId], (await server.Database.Frames.ListBySequenceAsync(sequence.Id)).Select(f => f.Id));
}
[Test]
public async Task AStaleTombstoneBesideAFrameOfTheSameContentRefusesNoRepair()
{
// A tombstone left beside a frame that still holds the content (the other copy of a duplicate, a file put back by
// hand) does not stand for a deletion: the frame's original can still be repaired by uploading it.
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register);
var browser = await Browser.SignInAsync(server);
var sequence = await CreateAsync(browser, "Roof");
var photo = Photo(14);
var sha = Uploads.Sha(photo);
var first = await UploadAsync(browser, sequence.Id, "IMG_0001.JPG", photo);
await UntilIdleAsync(server);
await server.Database.FrameRemovals.SyncSequenceAsync(sequence.Id, [new FrameRemoval(sequence.Id, sha, null, RemovalReason.Deleted, DateTimeOffset.UtcNow)]);
var frame = Assert.NotNull(await server.Database.Frames.GetAsync(first.FrameId));
var original = Path.Combine(server.Options.ArchivePath, frame.RelativePath);
File.Delete(original);
var repaired = await UploadAsync(browser, sequence.Id, "IMG_0001.JPG", photo);
Assert.Equal(first.FrameId, repaired.FrameId);
await Http.BytesEqualAsync(photo, File.ReadAllBytesAsync(original));
}
[Test]
public async Task TheDialogsCaptureTimeDatesAPhotoThatRecordsNone()
{
// Owner review 2, R3: a photo without Exif (a PNG, a JPEG stripped on export) takes the time the dialog sends, as local
// wall time, not the moment the server staged it; frames sent out of order still end up in capture order.
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register);
var browser = await Browser.SignInAsync(server);
var sequence = await CreateAsync(browser, "Timelapse");
var later = await UploadAsync(browser, sequence.Id, "frame_0002.jpg", Photo(16), "2025-05-01T06:31:00.000");
var earlier = await UploadAsync(browser, sequence.Id, "frame_0001.jpg", Photo(15), "2025-05-01T06:30:00");
var frames = await server.Database.Frames.ListBySequenceAsync(sequence.Id);
Assert.SequenceEqual([earlier.FrameId, later.FrameId], frames.OrderBy(f => f.Index).Select(f => f.Id));
Assert.Equal(new DateTime(2025, 5, 1, 6, 30, 0), frames.Single(f => f.Id == earlier.FrameId).CapturedAt);
Assert.Equal(new DateTime(2025, 5, 1, 6, 31, 0), frames.Single(f => f.Id == later.FrameId).CapturedAt);
}
[Test]
public async Task HiddenDotFilesAreRefusedLikeTheScannerLeavesThemOut()
{
// The AppleDouble "._" companion a Mac leaves on an exFAT card, and any hidden dot file, is no photo for the scanner:
// the dialog leaves them out, and the server refuses them from any client.
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register);
var browser = await Browser.SignInAsync(server);
var sequence = await CreateAsync(browser, "Card");
foreach (var name in new[] { "._IMG_0001.JPG", ".IMG_0002.JPG" })
{
using var start = await browser.SendAsync(HttpMethod.Post, "/api/v1/uploads", StartBody(sequence.Id, name, Uploads.Photo(4096, 21)));
await start.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_file_name");
}
var upload = await server.Client.GetStringAsync("/admin/upload.js");
Assert.Contains(@"file.name.startsWith(""."")", upload);
}
[Test]
public async Task TheDialogKeepsItselfOpenAndSettlesWhatAnotherCompletionFiles()
{
// Owner review 2, R3 (upload.js): Esc is stopped at its keydown during a run, since Chromium lets a dialog's cancel be
// prevented only once per user activation; a completion refused while another one files the upload (409
// upload_state) waits for it instead of failing the photo; the same photo picked twice waits for its first copy.
await using var server = await TestServer.StartAsync(context);
var upload = await server.Client.GetStringAsync("/admin/upload.js");
Assert.True(Regex.IsMatch(upload, @"addEventListener\(""keydown"", \(e\) => \{\s*if \(running && e\.key === ""Escape""\) e\.preventDefault\(\);"), "Esc at keydown");
Assert.Contains(@"err.code === ""upload_state""", upload);
Assert.Contains(@"api.get(`/api/v1/uploads/${uploadId}`)", upload);
Assert.Contains("sameContent.get(sha256)", upload);
Assert.Contains(@"?restore=true", upload);
Assert.Contains(@"err.code === ""photo_removed""", upload);
Assert.Contains(@"import { captureTime } from ""./capture-time.js"";", upload);
var capture = await server.Client.GetStringAsync("/admin/capture-time.js");
Assert.Contains("export function exifCaptureTime(bytes)", capture);
}
/// <summary>The bootstrap administrator signed in to the panel (a cookie session over HTTPS), calling as <c>api.js</c> does.</summary>
private sealed class Browser(HttpClient client, string csrfToken)
{
public static async Task<Browser> SignInAsync(TestServer server)
{
await server.AdminAsync();
var cookies = new CookieContainer();
var client = server.NewCookieClient(cookies);
using var login = await client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", TestServer.Password, "admin-web", Cookie: true),
ProtocolJson.Default.LoginRequest);
await login.ExpectAsync(HttpStatusCode.OK);
return new Browser(client, Assert.NotNull(cookies.GetCookies(Assert.NotNull(server.HttpsBase))["chronos_csrf"]).Value);
}
/// <summary>JSON accepted, the page's language (Italian here), and the CSRF header on every call but a GET.</summary>
public async Task<HttpResponseMessage> SendAsync(HttpMethod method, string url, HttpContent? content = null, bool csrf = true)
{
using var request = new HttpRequestMessage(method, url) { Content = content };
request.Headers.Accept.ParseAdd("application/json");
request.Headers.AcceptLanguage.ParseAdd("it");
if (method != HttpMethod.Get && csrf) request.Headers.Add("X-CSRF-Token", csrfToken);
return await client.SendAsync(request);
}
}
private static StringContent Json(string json) => new(json, Encoding.UTF8, "application/json");
/// <summary>A chunk as <c>putBytes</c> sends a <c>File.slice()</c>: its bytes, octet-stream.</summary>
private static ByteArrayContent Bytes(byte[] data, int index)
{
var content = new ByteArrayContent(Uploads.Chunk(data, Chunk, index).ToArray());
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return content;
}
/// <summary>
/// The start body upload.js writes: the capture time capture-time.js read (local wall time without an offset, or null),
/// no session key, and restore true only after the user chose Upload again on the photo's row.
/// </summary>
private static StringContent StartBody(Guid? sequenceId, string fileName, byte[] data, string? capturedAt = null, bool restore = false) =>
Json($$"""{"sequenceId":{{(sequenceId is { } id ? $"\"{id}\"" : "null")}},"fileName":"{{fileName}}","size":{{data.Length}},"sha256":"{{Uploads.Sha(data)}}","capturedAt":{{(capturedAt is null ? "null" : $"\"{capturedAt}\"")}},"sessionKey":null,"restore":{{(restore ? "true" : "false")}}}""");
private static async Task<StartUploadResponse> StartAsync(Browser browser, Guid sequenceId, string fileName, byte[] data, HttpStatusCode expected,
string? capturedAt = null, bool restore = false)
{
using var response = await browser.SendAsync(HttpMethod.Post, "/api/v1/uploads", StartBody(sequenceId, fileName, data, capturedAt, restore));
await response.ExpectAsync(expected);
return await response.ReadAsync(ProtocolJson.Default.StartUploadResponse);
}
/// <summary>A sequence made as the dialog's New sequence makes it.</summary>
private static async Task<SequenceDto> CreateAsync(Browser browser, string title)
{
using var created = await browser.SendAsync(HttpMethod.Post, "/api/v1/sequences", Json($$"""{"title":"{{title}}","cameraHint":null}"""));
await created.ExpectAsync(HttpStatusCode.Created);
return await created.ReadAsync(ProtocolJson.Default.SequenceDto);
}
/// <summary>Every chunk of an upload the dialog started, one after the other.</summary>
private static async Task SendChunksAsync(Browser browser, Guid id, byte[] data)
{
for (var n = 0; n < (data.Length + Chunk - 1) / Chunk; n++)
{
using var put = await browser.SendAsync(HttpMethod.Put, $"/api/v1/uploads/{id}/chunks/{n}", Bytes(data, n));
await put.ExpectAsync(HttpStatusCode.OK);
}
}
/// <summary>The dialog's completion: a body of <c>{}</c> (api.post), <c>?restore=true</c> after Upload again.</summary>
private static Task<HttpResponseMessage> CompleteAsync(Browser browser, Guid id, bool restore = false) =>
browser.SendAsync(HttpMethod.Post, $"/api/v1/uploads/{id}/complete{(restore ? "?restore=true" : string.Empty)}", Json("{}"));
/// <summary>Started, sent and completed as the dialog does it; returns the frame.</summary>
private static async Task<CompleteUploadResponse> UploadAsync(Browser browser, Guid sequenceId, string fileName, byte[] data, string? capturedAt = null,
bool restore = false)
{
var start = await StartAsync(browser, sequenceId, fileName, data, HttpStatusCode.Created, capturedAt, restore);
var id = Assert.NotNull(start.UploadId);
await SendChunksAsync(browser, id, data);
using var complete = await CompleteAsync(browser, id, restore);
await complete.ExpectAsync(HttpStatusCode.OK);
return await complete.ReadAsync(ProtocolJson.Default.CompleteUploadResponse);
}
/// <summary>Deletes a photo from its sequence as the Library does, once no job is working on it.</summary>
private static async Task DeleteAsync(TestServer server, Browser browser, Guid sequenceId, Guid frameId)
{
await UntilIdleAsync(server);
var current = Assert.NotNull(await server.Database.Sequences.GetAsync(sequenceId));
using var delete = await browser.SendAsync(HttpMethod.Post, $"/api/v1/sequences/{sequenceId}/frames/delete",
Json($$"""{"frameIds":["{{frameId}}"],"expectedFramesVersion":{{current.FramesVersion}},"dryRun":false}"""));
Assert.NotNull((await delete.ReadAsync(ProtocolJson.Default.FrameDeleteResultDto)).TrashEntry);
}
/// <summary>Empties the whole trash as the Trash page does: the dry run, then the job with its fingerprint.</summary>
private static async Task EmptyTrashAsync(TestServer server, Browser browser)
{
TrashEmptyPlanDto plan;
using (var dry = await browser.SendAsync(HttpMethod.Post, "/api/v1/archive/trash/empty", Json("""{"names":null,"dryRun":true,"fingerprint":null}""")))
plan = await dry.ReadAsync(ProtocolJson.Default.TrashEmptyPlanDto);
Assert.True(plan.Entries.Count > 0, "the trash holds the deleted photo");
using (var empty = await browser.SendAsync(HttpMethod.Post, "/api/v1/archive/trash/empty",
Json($$"""{"names":null,"dryRun":false,"fingerprint":"{{plan.Fingerprint}}"}""")))
await empty.ExpectAsync(HttpStatusCode.Accepted);
await UntilIdleAsync(server);
using var listed = await browser.SendAsync(HttpMethod.Get, "/api/v1/archive/trash");
Assert.Empty((await listed.ReadAsync(ProtocolJson.Default.TrashListDto)).Entries);
}
/// <summary>Waits until no job is queued or running (the thumbnails, proxies and analyses uploads queue).</summary>
private static async Task UntilIdleAsync(TestServer server)
{
var until = DateTime.UtcNow + Wait;
foreach (var state in new[] { JobState.Queued, JobState.Running, JobState.Queued, JobState.Running })
{
while ((await server.Database.Jobs.ListAsync(state)).Items.Count > 0)
{
Assert.True(DateTime.UtcNow < until, $"jobs still {state}");
await Task.Delay(50);
}
}
}
/// <summary>A JPEG of a few chunks: noise does not compress below the chunk size.</summary>
private static byte[] Photo(int seed)
{
const int width = 512, height = 384;
var pixels = new byte[width * height * 3];
new Random(seed).NextBytes(pixels);
return JpegEncoder.Encode(pixels, width, height, 3, new JpegEncodeOptions { Quality = 95 });
}
}