Files
Chronos/tests/Chronos.Server.Tests/Hosting/AdminConvertTests.cs
T
Alby96 341fb77507 fix: conversions name, lock and settle as phase 4a's writers do
Phase 4b was built on phase 4a before its review fixes. Integrated on the reviewed 4a, the
conversion now follows the same rules as filing and the other writers.

- Names: a converted file is named as filing names a photo of its sequence (FrameNaming: the
  archive's file template, the frame's context in the sequence with its camera, lens, number
  and title, {name} the card's, {ext} the target's, a long name keeping its extension).
  Replace through ConvertOptions.FileName; New sequence through the ingest itself
  (StagedFrame.NamedAs), which numbers and names the copies in the new sequence. Names stay
  unique against every file present, those leaving included, so a Replace never moves onto a
  file; a test pins a stray file that keeps its bytes.
- Leases: stage A starts from the sidecar settled under the sequence's lease where the index
  has it then (SequenceWriter.ReadLocked), and FrameTrash.ReplaceAsync reads the row again
  under the lease, so a folder a Reorganize moved meanwhile is followed, never made again.
  A Reorganize the archive waits for keeps the conversion off its sequences (pinned).
- Journals: the replace journal is settled with the others. MassRenamer steps, restores into
  a sequence and sequence deletions wait for an unsettled replacement as they wait for a
  restore, and so do photo deletions, whose frames version the recovery reads
  (FrameTrash.EnsureReplaced); a scan settles it and reconciles nothing meanwhile; a restore
  settles its sequence's replace journal first, at the folder the index has under the lease.
- The convert job tells each sequence converted or made as it completes, as a Reorganize
  tells each step, so an editor open on it reads it again while the rest of the job runs.
- Desktop: JobKind.Convert gets its row in the settings' job priority list. A click on Apply
  that commits the quality no longer starts the same dry run again under the click (as 4a
  fixed for Reorganize).
- Panel: a dry run answered during the confirmation with the same plan no longer drops the
  confirmed Apply; a different plan is shown with Admin.Convert.PlanChanged (en, it).
- Tests: TrashDialogTests.EmptyQueuesTheJobOfTheConfirmedPlanOnly waits for the command to
  end before running it again (a running AsyncRelayCommand ignored the second Execute).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 2e0d1d6dcc10575734970bb9c77749f00777c859)
2026-10-27 22:03:07 +01:00

260 lines
16 KiB
C#

using System.Globalization;
using System.Net;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Imaging;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Protocol;
using Chronos.Server.Storage;
using Chronos.Testing;
namespace Chronos.Server.Tests.Hosting;
/// <summary>
/// Convert on the admin panel (owner review 2, R7; <c>Admin/web/convert.js</c>): the Library's mass bar offers it, the dialog
/// module and its strings, the mode it never preselects and the typed confirmation of Replace, the audit entry read as a
/// sentence, and the requests the dialog writes, sent through the cookie session as <c>api.js</c> sends them and read back
/// by the names the dialog reads, through the trash's undo of a Replace.
/// </summary>
[Category("server")]
public sealed partial class AdminConvertTests(TestContext context)
{
private const string Url = "/api/v1/archive/convert";
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(TestTiming.Budget(60));
[Test]
public async Task TheLibraryMassBarOpensTheConvertDialog()
{
await using var server = await TestServer.StartAsync(context);
var library = await server.Client.GetStringAsync("/admin/pages/library.js");
Assert.Contains(@"from ""../convert.js""", library);
Assert.Contains(@"data-mass=""convert"" data-i18n=""Admin.Convert.Action"" data-i18n-title=""Admin.Convert.Action.Tip""", library);
Assert.Contains(@"if (action === ""convert"") return convert();", library);
// The selection: every sequence after Select all without a filter, else the ids (one request takes a thousand).
Assert.Contains(@"const target = planTarget(""Admin.Convert.TooMany"");", library);
Assert.Contains("openConvert({ api, target, names })", library);
using var response = await server.Client.GetAsync("/admin/convert.js");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("text/javascript", response.Content.Headers.ContentType?.MediaType);
var dialog = await response.Content.ReadAsStringAsync();
Assert.True(Regex.IsMatch(dialog, @"export function openConvert\(\{ api, target, "), "convert.js exports openConvert({ api, target, … })");
var imports = StaticImport().Matches(dialog).Select(m => m.Groups[1].Value).ToList();
Assert.SequenceEqual(["./i18n.js", "./ui.js", "./pages/library/shared.js"], imports);
foreach (var module in imports)
{
using var imported = await server.Client.GetAsync("/admin/" + module[2..]);
Assert.Equal(HttpStatusCode.OK, imported.StatusCode, module);
}
// Linear DNG, the archival choice, is the format chosen first; the mode is chosen at every run, never preselected.
Assert.Contains(@"TARGETS.map((o) => radio(""target"", o, o.value === ARCHIVAL))", dialog);
Assert.Contains(@"MODES.map((o) => radio(""mode"", o, false))", dialog);
Assert.Contains(@"const ARCHIVAL = ""linear-dng"";", dialog);
Assert.Contains(@"mode === null ? ""Admin.Convert.ChooseMode.Tip""", dialog);
// A dry run at every change; Replace has the word typed, New sequence a plain confirmation; Apply sends the shown
// plan's fingerprint back and links the job it queued.
Assert.Contains("api.post(ENDPOINT, { ...body, dryRun: true })", dialog);
Assert.Contains("api.post(ENDPOINT, { ...body, dryRun: false, fingerprint: shownPlan.fingerprint })", dialog);
Assert.True(Regex.IsMatch(dialog, @"confirmDialog\(replace \? \{[^}]*typed: t\(""Admin\.Convert\.ReplaceWord""\),\s+typedKind: ""word"",\s+\} : \{[^}]*\}\)", RegexOptions.Singleline),
"Replace asks for the word, New sequence does not");
Assert.Contains("#/jobs?job=", dialog);
// A dry run answered during the confirmation: the same plan again is applied, a different one shown.
Assert.Contains("if (plan !== shownPlan && (plan?.key !== shownPlan.key || plan.fingerprint !== shownPlan.fingerprint)) {", dialog);
Assert.Contains(@"showError(t(""Admin.Convert.PlanChanged""))", dialog);
Assert.True(!dialog.Contains("plan !== shownPlan) return;", StringComparison.Ordinal), "a confirmed apply is dropped when the same plan is answered again");
// The panel runs under a strict CSP: no inline handlers or styles.
Assert.True(!Regex.IsMatch(dialog, @"\son[a-z]+="""), "inline handler");
Assert.True(!dialog.Contains(" style=", StringComparison.Ordinal), "inline style");
var audit = await server.Client.GetStringAsync("/admin/audit.js");
Assert.Contains(@"case ""archive_converted"":", audit);
var trash = await server.Client.GetStringAsync("/admin/pages/library/trash.js");
Assert.Contains(@"result.kind === ""replaced"" ? plural(""Admin.Trash.RestoredOriginals""", trash);
var en = Assert.NotNull(await server.Client.GetFromJsonAsync("/api/v1/i18n/en", ProtocolJson.Default.DictionaryStringString));
var it = Assert.NotNull(await server.Client.GetFromJsonAsync("/api/v1/i18n/it", ProtocolJson.Default.DictionaryStringString));
// Every whole key in quotes (not the prefixes audit.js completes with a wire name).
var keys = Regex.Matches(dialog + library + audit + trash, @"""(Admin\.(?:Convert\.[A-Za-z]+|Trash\.Restore[A-Za-z]*)(?:\.[A-Za-z]+)*)""")
.Select(m => m.Groups[1].Value).Distinct().ToList();
Assert.Greater(keys.Count, 40);
foreach (var key in keys)
{
var name = en.ContainsKey(key) ? key : key + ".other";
Assert.True(en.ContainsKey(name) && it.ContainsKey(name), key);
}
// The audit entry's parts by the keys audit.js builds from the wire names.
foreach (var key in new[] { "Admin.Convert.Format.Jpeg", "Admin.Convert.Format.Tiff16", "Admin.Convert.Format.LinearDng", "Admin.Convert.Mode.Replace",
"Admin.Convert.Mode.NewSequence", "Admin.Logs.Action.ArchiveConverted", "Admin.Activity.ArchiveConverted" })
Assert.True(en.ContainsKey(key) && it.ContainsKey(key), key);
Assert.Equal("Converti…", it["Admin.Convert.Action"]);
Assert.Equal("DNG lineare", it["Admin.Convert.Format.LinearDng"]);
}
[Test]
public async Task TheDialogsRequestsPlanQueueAndUndoAConversion()
{
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
var (editor, _, _) = await server.UserAsync("alberto", "editor");
Guid id = default;
for (var i = 0; i < 2; i++) id = (await Uploads.UploadAsync(editor, Photo(i), $"IMG_{i + 1:0000}.JPG", sessionKey: "roof")).SequenceId;
await UntilIdleAsync(server);
var browser = await Browser.SignInAsync(server);
var italian = CultureInfo.GetCultureInfo("it");
// The body the dialog writes: the selection's ids, the format, the quality for a JPEG only, the mode (the preview's
// while none is chosen), then dryRun.
StringContent Body(string target, string quality, string mode, string tail) =>
Json($$"""{"all":false,"sequenceIds":["{{id}}"],"target":"{{target}}","quality":{{quality}},"mode":"{{mode}}",{{tail}}}""");
string preview;
using (var dry = await browser.SendAsync(HttpMethod.Post, Url, Body("linear-dng", "null", "new-sequence", "\"dryRun\":true")))
{
await dry.ExpectAsync(HttpStatusCode.OK);
using var plan = JsonDocument.Parse(await dry.Content.ReadAsStringAsync());
var root = plan.RootElement;
Assert.Equal(("linear-dng", "new-sequence"), (root.GetProperty("target").GetString(), root.GetProperty("mode").GetString()));
Assert.Equal((1, 2, 0), (root.GetProperty("sequences").GetInt32(), root.GetProperty("photos").GetInt32(), root.GetProperty("skipped").GetInt32()));
Assert.True(root.GetProperty("bytesBefore").GetInt64() > 0 && root.GetProperty("bytesAfter").GetInt64() > 0, "sizes");
var space = root.GetProperty("space");
Assert.True(space.GetProperty("enough").GetBoolean(), "enough");
Assert.False(string.IsNullOrWhiteSpace(space.GetProperty("message").GetString()));
preview = root.GetProperty("fingerprint").GetString()!;
Assert.Equal(64, preview.Length);
var step = root.GetProperty("steps").EnumerateArray().Single();
Assert.Equal(id, step.GetProperty("sequenceId").GetGuid());
Assert.True(step.GetProperty("folder").GetString()!.Length > 0, "folder");
Assert.Equal(JsonValueKind.String, step.GetProperty("title").ValueKind);
Assert.Equal(2, step.GetProperty("photos").GetInt32());
Assert.True(step.GetProperty("bytesAfter").GetInt64() > 0, "bytesAfter");
Assert.Equal((0, 0), (step.GetProperty("skipped").GetArrayLength(), step.GetProperty("blocked").GetArrayLength()));
}
// Replace chosen: the same photos, another fingerprint, so the dialog plans again before Apply.
string fingerprint;
using (var dry = await browser.SendAsync(HttpMethod.Post, Url, Body("linear-dng", "null", "replace", "\"dryRun\":true")))
{
using var plan = JsonDocument.Parse(await dry.Content.ReadAsStringAsync());
Assert.Equal(2, plan.RootElement.GetProperty("photos").GetInt32());
fingerprint = plan.RootElement.GetProperty("fingerprint").GetString()!;
Assert.NotEqual(preview, fingerprint);
}
// A quality the server refuses names its field: the dialog marks Quality.
using (var bad = await browser.SendAsync(HttpMethod.Post, Url, Body("jpeg", "0", "replace", "\"dryRun\":true")))
Assert.Equal("quality", (await bad.ExpectErrorAsync(HttpStatusCode.BadRequest, "bad_request")).Details);
// JPEG to JPEG converts nothing: the table says why, in the page's language.
using (var same = await browser.SendAsync(HttpMethod.Post, Url, Body("jpeg", "90", "replace", "\"dryRun\":true")))
{
using var plan = JsonDocument.Parse(await same.Content.ReadAsStringAsync());
Assert.Equal(0, plan.RootElement.GetProperty("photos").GetInt32());
var skipped = plan.RootElement.GetProperty("steps")[0].GetProperty("skipped").EnumerateArray().Single();
Assert.Equal(("same_format", 2), (skipped.GetProperty("reason").GetString(), skipped.GetProperty("photos").GetInt32()));
Assert.Equal(Loc.Get("Archive.Convert.Skipped.SameFormat", italian), skipped.GetProperty("message").GetString());
}
// A plan that no longer matches is refused with the one there is now: the dialog plans again.
using (var stale = await browser.SendAsync(HttpMethod.Post, Url, Body("linear-dng", "null", "replace", $"\"dryRun\":false,\"fingerprint\":\"{new string('0', 64)}\"")))
Assert.Equal(fingerprint, (await stale.ExpectErrorAsync(HttpStatusCode.Conflict, "plan_changed")).Details);
Guid job;
using (var applied = await browser.SendAsync(HttpMethod.Post, Url, Body("linear-dng", "null", "replace", $"\"dryRun\":false,\"fingerprint\":\"{fingerprint}\"")))
{
await applied.ExpectAsync(HttpStatusCode.Accepted);
using var queued = JsonDocument.Parse(await applied.Content.ReadAsStringAsync());
Assert.Equal("convert", queued.RootElement.GetProperty("kind").GetString());
job = queued.RootElement.GetProperty("id").GetGuid();
}
await UntilJobAsync(browser, job);
Assert.True((await server.Database.Frames.ListBySequenceAsync(id)).All(f => f.Format == ImageFileFormat.Dng), "converted");
// The audit entry audit.js reads: sequences · photos · target · mode.
var entry = (await server.Database.Audit.ListAsync(null, "archive_converted", 10, null)).Items.Single();
Assert.Equal("1 · 2 · linear-dng · replace", entry.Details);
// The originals wait in the trash as a replaced entry; its Restore swaps them back (the toast the page shows).
string name;
using (var list = await browser.SendAsync(HttpMethod.Get, "/api/v1/archive/trash"))
{
using var trash = JsonDocument.Parse(await list.Content.ReadAsStringAsync());
var replaced = trash.RootElement.GetProperty("entries").EnumerateArray().Single(e => e.GetProperty("kind").GetString() == "replaced");
Assert.Equal(2, replaced.GetProperty("photos").GetInt32());
name = replaced.GetProperty("name").GetString()!;
}
using (var restore = await browser.SendAsync(HttpMethod.Post, $"/api/v1/archive/trash/{Uri.EscapeDataString(name)}/restore"))
{
await restore.ExpectAsync(HttpStatusCode.OK);
using var result = JsonDocument.Parse(await restore.Content.ReadAsStringAsync());
Assert.Equal(("replaced", false, 2), (result.RootElement.GetProperty("kind").GetString(), result.RootElement.GetProperty("newSequence").GetBoolean(),
result.RootElement.GetProperty("photos").GetInt32()));
}
Assert.True((await server.Database.Frames.ListBySequenceAsync(id)).All(f => f.Format == ImageFileFormat.Jpeg), "swapped back");
}
[GeneratedRegex(@"from ""(\.{1,2}/[^""]+)""")]
private static partial Regex StaticImport();
private static async Task UntilJobAsync(Browser browser, Guid id)
{
var until = DateTime.UtcNow + Wait;
while (true)
{
using var response = await browser.SendAsync(HttpMethod.Get, $"/api/v1/jobs/{id}");
var job = await response.ReadAsync(ProtocolJson.Default.JobDto);
if (job.State == "done") return;
if (job.State is "failed" or "cancelled") Assert.Fail($"job {job.Kind} {job.State}: {job.Error}\n{job.LogTail}");
Assert.True(DateTime.UtcNow < until, $"job {job.Kind} still {job.State}");
await Task.Delay(50);
}
}
/// <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);
}
}
}
private static StringContent Json(string json) => new(json, Encoding.UTF8, "application/json");
private static byte[] Photo(int seed)
{
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 * 31) % 251);
return JpegEncoder.Encode(pixels, width, height, 3, new JpegEncodeOptions { Quality = 90 });
}
/// <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)
{
using var request = new HttpRequestMessage(method, url) { Content = content };
request.Headers.Accept.ParseAdd("application/json");
request.Headers.AcceptLanguage.ParseAdd("it");
if (method != HttpMethod.Get) request.Headers.Add("X-CSRF-Token", csrfToken);
return await client.SendAsync(request);
}
}
}