Owner review 2, R7 asks for renaming and moving after import, on the web panel and the desktop. This is the engine and the server API both UIs build on. - FrameNaming (Core/Naming) is the one naming context and unique-name rule of a frame: imports, uploads and Reorganize now produce identical names (behaviour of the ingest and the import planner unchanged). - RenamePlanner takes a file template: each step counts its renamed frames, shows the first three and the frames version they were planned at, and lists what keeps its name (photos in place outside originals/, never hashed, missing, unreadable sidecar). Names are given in index order with _2, _3 as filing does; XMP companions follow their photo. - MassRenamer renames a step's files under the same leases after its folder moved, checks the frames version, journals .chronos/rename-files.json and moves files in two phases through temporary names so chains and swaps never meet; then sidecar, index (FrameRepository.RenameFilesAsync, both dialects) and journal. A failure puts every file back; a crash is completed by the next writer or the plan's resume, the index following. Plans a paused or cancelled job holds are left to that job. - JobKind.Move gets MoveJobHandler: it plans again and applies only the confirmed plan (fingerprint and frames versions), with progress and checkpoints per sequence. - POST /api/v1/archive/reorganize: dry run with the plan and its fingerprint, otherwise a 202 move job; validated templates, audited, sequenceChanged reorganized events. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit aae1c3eb18ea932b4a6251c5e2a282b9295a5785)
188 lines
11 KiB
C#
188 lines
11 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Chronos.Core.Codecs.Jpeg;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Protocol;
|
|
using Chronos.Server.Events;
|
|
using Chronos.Server.Storage;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Server.Tests.Api;
|
|
|
|
/// <summary>
|
|
/// Reorganize on the server (owner review 2, R7): <c>POST /api/v1/archive/reorganize</c>, its dry run, the <c>move</c> job that
|
|
/// applies the confirmed plan, and what the request may not do, against the real archive engine.
|
|
/// </summary>
|
|
[Category("server")]
|
|
public sealed class ReorganizeTests(TestContext context)
|
|
{
|
|
private const string Url = "/api/v1/archive/reorganize";
|
|
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(TestTiming.Budget(30));
|
|
|
|
[Test]
|
|
public async Task AReorganizeIsDryRunThenAppliedByAJob()
|
|
{
|
|
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
|
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
|
var admin = await server.AdminAsync();
|
|
Guid id = default;
|
|
for (var i = 0; i < 3; i++) id = (await Uploads.UploadAsync(editor, Photo(i), $"IMG_{i + 1:0000}.JPG", sessionKey: "roof")).SequenceId;
|
|
await UntilIdleAsync(server);
|
|
var before = await server.Database.Frames.ListBySequenceAsync(id);
|
|
|
|
var request = new ReorganizeRequest([id], false, "reorganized/{title}", "{title}_{n:000}", "Roof", DryRun: true);
|
|
ReorganizePlanDto plan;
|
|
using (var dry = await admin.PostAsJsonAsync(Url, request, ProtocolJson.Default.ReorganizeRequest))
|
|
plan = await dry.ReadAsync(ProtocolJson.Default.ReorganizePlanDto);
|
|
Assert.Equal((1, 3, 0), (plan.Sequences, plan.Files, plan.Kept));
|
|
var step = plan.Steps.Single();
|
|
Assert.Equal((id, "reorganized/Roof", "Roof", 3), (step.SequenceId, step.ToFolder, step.ToTitle, step.FileChanges));
|
|
Assert.SequenceEqual(["originals/Roof_001.JPG", "originals/Roof_002.JPG", "originals/Roof_003.JPG"], step.FileSamples.Select(f => f.To));
|
|
Assert.Equal(64, plan.Fingerprint.Length);
|
|
Assert.SequenceEqual(before.Select(f => f.RelativePath), (await server.Database.Frames.ListBySequenceAsync(id)).Select(f => f.RelativePath));
|
|
|
|
using (var missing = await admin.PostAsJsonAsync(Url, request with { DryRun = false }, ProtocolJson.Default.ReorganizeRequest))
|
|
Assert.Equal("fingerprint", (await missing.ExpectErrorAsync(HttpStatusCode.BadRequest, "bad_request")).Details);
|
|
using (var wrong = await admin.PostAsJsonAsync(Url, request with { DryRun = false, Fingerprint = "0123" }, ProtocolJson.Default.ReorganizeRequest))
|
|
Assert.Equal(plan.Fingerprint, (await wrong.ExpectErrorAsync(HttpStatusCode.Conflict, "plan_changed")).Details);
|
|
|
|
using var events = server.Services.GetRequiredService<EventHub>().Subscribe();
|
|
JobDto job;
|
|
using (var applied = await admin.PostAsJsonAsync(Url, request with { DryRun = false, Fingerprint = plan.Fingerprint }, ProtocolJson.Default.ReorganizeRequest))
|
|
{
|
|
await applied.ExpectAsync(HttpStatusCode.Accepted);
|
|
job = await applied.ReadAsync(ProtocolJson.Default.JobDto);
|
|
}
|
|
Assert.Equal("move", job.Kind);
|
|
await UntilJobAsync(admin, job.Id, "done");
|
|
|
|
var sequence = (await server.Database.Sequences.GetAsync(id))!;
|
|
Assert.Equal(("reorganized/Roof", "Roof"), (sequence.Folder, sequence.Title));
|
|
var after = await server.Database.Frames.ListBySequenceAsync(id);
|
|
Assert.SequenceEqual(before.Select(f => f.Id), after.Select(f => f.Id));
|
|
Assert.SequenceEqual(["reorganized/Roof/originals/Roof_001.JPG", "reorganized/Roof/originals/Roof_002.JPG", "reorganized/Roof/originals/Roof_003.JPG"],
|
|
after.Select(f => f.RelativePath));
|
|
foreach (var (was, now) in before.Zip(after))
|
|
Assert.Equal(was.Sha256, Uploads.Sha(File.ReadAllBytes(Path.Combine(server.Options.ArchivePath, now.RelativePath))));
|
|
Assert.NotEmpty((await server.Database.Audit.ListAsync(null, "archive_reorganized", 10, null)).Items);
|
|
|
|
var until = DateTime.UtcNow + Wait;
|
|
while (true)
|
|
{
|
|
using var timeout = new CancellationTokenSource(until - DateTime.UtcNow);
|
|
var e = await events.Reader.ReadAsync(timeout.Token);
|
|
if (e is SequenceChangedEvent { Change: "reorganized" } changed && changed.SequenceId == id)
|
|
{
|
|
Assert.Equal(sequence.FramesVersion, changed.FramesVersion);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task AReorganizeAsksForTheArchiveAndAValidRequest()
|
|
{
|
|
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
|
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
|
var admin = await server.AdminAsync();
|
|
var a = (await Uploads.UploadAsync(editor, Photo(1), "a.jpg", sessionKey: "a")).SequenceId;
|
|
var b = (await Uploads.UploadAsync(editor, Photo(2), "b.jpg", sessionKey: "b")).SequenceId;
|
|
await UntilIdleAsync(server);
|
|
var valid = new ReorganizeRequest([a], false, null, "{n:000}", null, DryRun: true);
|
|
|
|
using (var forbidden = await editor.PostAsJsonAsync(Url, valid, ProtocolJson.Default.ReorganizeRequest))
|
|
await forbidden.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
|
foreach (var (bad, details) in new[]
|
|
{
|
|
(valid with { All = true }, "sequenceIds"),
|
|
(valid with { SequenceIds = null }, "sequenceIds"),
|
|
(valid with { SequenceIds = [] }, "sequenceIds"),
|
|
(valid with { FileTemplate = null }, "templates"),
|
|
})
|
|
{
|
|
using var response = await admin.PostAsJsonAsync(Url, bad, ProtocolJson.Default.ReorganizeRequest);
|
|
Assert.Equal(details, (await response.ExpectErrorAsync(HttpStatusCode.BadRequest, "bad_request")).Details);
|
|
}
|
|
using (var template = await admin.PostAsJsonAsync(Url, valid with { FileTemplate = "{nope}" }, ProtocolJson.Default.ReorganizeRequest))
|
|
Assert.Equal("fileTemplate", (await template.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_template")).Details);
|
|
using (var blank = await admin.PostAsJsonAsync(Url, valid with { FolderTemplate = " " }, ProtocolJson.Default.ReorganizeRequest))
|
|
Assert.Equal("folderTemplate", (await blank.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_template")).Details);
|
|
using (var unknown = await admin.PostAsJsonAsync(Url, valid with { SequenceIds = [Guid.NewGuid()] }, ProtocolJson.Default.ReorganizeRequest))
|
|
await unknown.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
|
|
|
// One sequence's new folder inside the other's: refused, nothing planned.
|
|
var host = (await server.Database.Sequences.GetAsync(a))!.Folder;
|
|
using (var nested = await admin.PostAsJsonAsync(Url, new ReorganizeRequest([b], false, host + "/inside", null, null, DryRun: true),
|
|
ProtocolJson.Default.ReorganizeRequest))
|
|
Assert.Equal(host + "/inside", (await nested.ExpectErrorAsync(HttpStatusCode.Conflict, "nested_target")).Details);
|
|
|
|
using (var all = await admin.PostAsJsonAsync(Url, valid with { SequenceIds = null, All = true }, ProtocolJson.Default.ReorganizeRequest))
|
|
{
|
|
var plan = await all.ReadAsync(ProtocolJson.Default.ReorganizePlanDto);
|
|
Assert.Equal((2, 2), (plan.Sequences, plan.Files));
|
|
Assert.SequenceEqual(new[] { a, b }.Order(), plan.Steps.Select(s => s.SequenceId).Order());
|
|
}
|
|
Assert.Empty((await server.Database.Jobs.ListAsync(JobState.Queued, JobKind.Move)).Items);
|
|
}
|
|
|
|
[Test]
|
|
public async Task PhotosThatKeepTheirNamesAreToldWhy()
|
|
{
|
|
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
|
var admin = await server.AdminAsync();
|
|
// Photos copied into the archive by hand and found by a scan stay where they are.
|
|
var folder = Path.Combine(server.Options.ArchivePath, "trips");
|
|
Directory.CreateDirectory(folder);
|
|
for (var i = 0; i < 2; i++) await File.WriteAllBytesAsync(Path.Combine(folder, $"IMG_{i:0000}.JPG"), Photo(i));
|
|
using (var scan = await admin.PostAsync("/api/v1/archive/scan", null)) await scan.ExpectAsync(HttpStatusCode.Accepted);
|
|
await UntilIdleAsync(server);
|
|
var id = (await server.Database.Sequences.ListLocationsAsync((await server.Database.Archives.ListAsync()).Single().Id)).Single().Id;
|
|
|
|
var request = new ReorganizeRequest([id], false, null, "{n:000}", null, DryRun: true);
|
|
admin.DefaultRequestHeaders.AcceptLanguage.ParseAdd("it");
|
|
using var dry = await admin.PostAsJsonAsync(Url, request, ProtocolJson.Default.ReorganizeRequest);
|
|
var plan = await dry.ReadAsync(ProtocolJson.Default.ReorganizePlanDto);
|
|
Assert.Equal((0, 0, 2), (plan.Sequences, plan.Files, plan.Kept));
|
|
var blocked = plan.Steps.Single().Blocked.Single();
|
|
Assert.Equal(("in_place", 2), (blocked.Reason, blocked.Photos));
|
|
Assert.Equal(Chronos.Core.Localization.Loc.Get("Archive.Reorganize.Blocked.InPlace", System.Globalization.CultureInfo.GetCultureInfo("it")), blocked.Message);
|
|
}
|
|
|
|
private static async Task UntilJobAsync(HttpClient client, Guid id, string state)
|
|
{
|
|
var until = DateTime.UtcNow + Wait;
|
|
while (true)
|
|
{
|
|
using var response = await client.GetAsync($"/api/v1/jobs/{id}");
|
|
var job = await response.ReadAsync(ProtocolJson.Default.JobDto);
|
|
if (job.State == state) 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 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 });
|
|
}
|
|
}
|