CI / Build and test (push) Successful in 9m15s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 15m35s
CI / PostgreSQL 16 integration (push) Successful in 7m26s
CI / PostgreSQL 17 integration (push) Successful in 6m47s
CI / Docker image (push) Successful in 59s
Release / Server image (push) Successful in 1m24s
- Reorganize… on the web panel and the desktop: folder, file and title templates applied to existing sequences, with a dry run and an explicit Apply that runs as a job. - One naming rule (FrameNaming) for import, upload and Reorganize. - Crash-safe plan and file journals, held plans, stale-plan refusal; exports move with their folder. - Writers refuse a folder another program moved or gave to another sequence. - Server: POST /api/v1/archive/reorganize with a bounded plan cache; template rules. - Phase 3 docs and translations merged; strings in all 38 languages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
266 lines
17 KiB
C#
266 lines
17 KiB
C#
using System.Text.Json.Nodes;
|
||
using Chronos.Core.Archiving;
|
||
using Chronos.Core.Codecs.Jpeg;
|
||
using Chronos.Core.Imaging;
|
||
using Chronos.Core.Localization;
|
||
using Chronos.Core.Metadata;
|
||
using Chronos.Core.Model;
|
||
using Chronos.Data.Archiving;
|
||
using Chronos.Testing;
|
||
|
||
namespace Chronos.Data.Tests.Archiving;
|
||
|
||
/// <summary>
|
||
/// An archive's settings live in its archive.json (BRIEF §7): changing them rewrites the file atomically and the index
|
||
/// row, survives a reopen (which registers the archive from the file again), and the proxy and thumbnail jobs use them.
|
||
/// </summary>
|
||
[Category("archive")]
|
||
public sealed class ArchiveSettingsTests(TestContext context)
|
||
{
|
||
[Test]
|
||
public async Task ChangedSettingsReachArchiveJsonTheIndexAndAReopen()
|
||
{
|
||
await using var fx = await ArchiveFixture.CreateAsync(context);
|
||
var updated = await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with
|
||
{
|
||
Name = " Studio ",
|
||
Templates = s.Templates with { Folder = "{year}/{camera}", File = "{name}" },
|
||
Proxies = s.Proxies with { LongEdge = 2560 },
|
||
Thumbnails = s.Thumbnails with { LongEdge = 480 },
|
||
}, fx.Ct);
|
||
|
||
Assert.Equal(("Studio", "{year}/{camera}", "{name}", 2560), (updated.Name, updated.FolderTemplate, updated.FileTemplate, updated.ProxyLongEdge));
|
||
var manifest = ManifestStore.ReadArchive(fx.Paths.ManifestPath);
|
||
Assert.Equal(fx.Archive.Id, manifest.Id);
|
||
Assert.Equal(("Studio", "{year}/{camera}", "{name}"), (manifest.Name, manifest.LayoutTemplates.Folder, manifest.LayoutTemplates.File));
|
||
Assert.Equal((2560, 480), (manifest.ProxyPolicy.LongEdge, manifest.ThumbnailPolicy.LongEdge));
|
||
var row = (await fx.Db.Archives.GetAsync(fx.Archive.Id, fx.Ct))!;
|
||
Assert.Equal(("Studio", "{year}/{camera}", 2560), (row.Name, row.FolderTemplate, row.ProxyLongEdge));
|
||
Assert.Empty(Directory.GetFiles(fx.Paths.MetaDirectory, "*.tmp"));
|
||
|
||
// A restart registers the archive from archive.json again: the change stays.
|
||
var reopened = await fx.Service.OpenAsync(fx.Archive.RootPath, fx.Ct);
|
||
Assert.Equal(("Studio", "{name}", 2560), (reopened.Name, reopened.FileTemplate, reopened.ProxyLongEdge));
|
||
var settings = await fx.Service.GetSettingsAsync(fx.Archive.Id, fx.Ct);
|
||
Assert.Equal(480, settings.Thumbnails.LongEdge);
|
||
}
|
||
|
||
[Test]
|
||
public async Task AnEditKeepsWhatAnotherProcessChangedAndUnknownFields()
|
||
{
|
||
await using var fx = await ArchiveFixture.CreateAsync(context);
|
||
// A newer Chronos (or the server) wrote a field this build does not know, and changed the proxy size.
|
||
var json = JsonNode.Parse(await File.ReadAllTextAsync(fx.Paths.ManifestPath, fx.Ct))!.AsObject();
|
||
json["futureOption"] = 42;
|
||
json["proxyPolicy"]!["longEdge"] = 1280;
|
||
await File.WriteAllTextAsync(fx.Paths.ManifestPath, json.ToJsonString(), fx.Ct);
|
||
|
||
await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Templates = s.Templates with { File = "{date}_{n:0000}" } }, fx.Ct);
|
||
|
||
var written = JsonNode.Parse(await File.ReadAllTextAsync(fx.Paths.ManifestPath, fx.Ct))!;
|
||
Assert.Equal(42, (int)written["futureOption"]!);
|
||
Assert.Equal(1280, (int)written["proxyPolicy"]!["longEdge"]!);
|
||
Assert.Equal("{date}_{n:0000}", (string)written["layoutTemplates"]!["file"]!);
|
||
}
|
||
|
||
[Test]
|
||
public async Task AnArchiveWhoseFileHasNoUsableNameCanStillBeEdited()
|
||
{
|
||
await using var fx = await ArchiveFixture.CreateAsync(context);
|
||
var folder = Path.GetFileName(fx.Paths.Root);
|
||
// Archives made at a drive root before names were checked carry an empty name; others one over the limit.
|
||
foreach (var (stored, expected) in new[] { (string.Empty, folder), (new string('n', ArchiveSettings.MaxNameLength + 20), new string('n', ArchiveSettings.MaxNameLength)) })
|
||
{
|
||
var json = JsonNode.Parse(await File.ReadAllTextAsync(fx.Paths.ManifestPath, fx.Ct))!.AsObject();
|
||
json["name"] = stored;
|
||
await File.WriteAllTextAsync(fx.Paths.ManifestPath, json.ToJsonString(), fx.Ct);
|
||
|
||
var updated = await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Templates = s.Templates with { File = "{date}_{n:0000}" } }, fx.Ct);
|
||
|
||
Assert.Equal((expected, "{date}_{n:0000}"), (updated.Name, updated.FileTemplate));
|
||
Assert.Equal(expected, ManifestStore.ReadArchive(fx.Paths.ManifestPath).Name);
|
||
}
|
||
}
|
||
|
||
[Test]
|
||
public async Task InvalidSettingsAreRefusedAndNothingIsWritten()
|
||
{
|
||
await using var fx = await ArchiveFixture.CreateAsync(context);
|
||
var before = await File.ReadAllTextAsync(fx.Paths.ManifestPath, fx.Ct);
|
||
|
||
var template = await Assert.ThrowsAsync<ArgumentException>(() =>
|
||
fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Templates = s.Templates with { Folder = "{year/{date" } }, fx.Ct));
|
||
Assert.Equal("folderTemplate", template.ParamName);
|
||
// The rules every template is held to wherever it is typed: a folder scans never enter, a lost extension, names that repeat.
|
||
foreach (var (folder, file, field) in new[]
|
||
{
|
||
("exports/{date}", "{n}", "folderTemplate"), ("{date}", "{n}{ext}", "fileTemplate"), ("{date}", "{title}", "fileTemplate"),
|
||
})
|
||
{
|
||
var rule = await Assert.ThrowsAsync<ArgumentException>(() =>
|
||
fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Templates = s.Templates with { Folder = folder, File = file } }, fx.Ct));
|
||
Assert.Equal(field, rule.ParamName, $"{folder} · {file}");
|
||
}
|
||
var created = await Assert.ThrowsAsync<ArgumentException>(() =>
|
||
fx.Service.CreateAsync(context.CreateTempDirectory("other"), "Other", new LayoutTemplates { Folder = "{date}", File = "{n}{ext}" }, null, fx.Ct));
|
||
Assert.Equal("fileTemplate", created.ParamName);
|
||
var proxies = await Assert.ThrowsAsync<ArgumentException>(() =>
|
||
fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Proxies = s.Proxies with { LongEdge = 100 } }, fx.Ct));
|
||
Assert.Equal("proxyLongEdge", proxies.ParamName);
|
||
var thumbnails = await Assert.ThrowsAsync<ArgumentException>(() =>
|
||
fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Thumbnails = s.Thumbnails with { LongEdge = 5000 } }, fx.Ct));
|
||
Assert.Equal("thumbnailLongEdge", thumbnails.ParamName);
|
||
var name = await Assert.ThrowsAsync<ArgumentException>(() => fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Name = " " }, fx.Ct));
|
||
Assert.Equal("name", name.ParamName);
|
||
|
||
Assert.Equal(before, await File.ReadAllTextAsync(fx.Paths.ManifestPath, fx.Ct));
|
||
}
|
||
|
||
[Test]
|
||
public async Task TemplatesSavedBeforeTheRulesNeverFailAnEditOfAnotherSetting()
|
||
{
|
||
await using var fx = await ArchiveFixture.CreateAsync(context);
|
||
// An archive.json of 0.1.0-rc.5 or earlier: both templates break a rule new templates are held to.
|
||
var manifest = ManifestStore.ReadArchive(fx.Paths.ManifestPath);
|
||
ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with { LayoutTemplates = new LayoutTemplates { Folder = "exports/{date}", File = "{title}" } });
|
||
|
||
// The desktop saves one row at a time: each edit keeps the templates it does not change.
|
||
var proxies = await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Proxies = s.Proxies with { LongEdge = 2560 } }, fx.Ct);
|
||
Assert.Equal(("exports/{date}", "{title}", 2560), (proxies.FolderTemplate, proxies.FileTemplate, proxies.ProxyLongEdge));
|
||
await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Name = "Studio" }, fx.Ct);
|
||
var folder = await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Templates = s.Templates with { Folder = "{year}/{date}" } }, fx.Ct);
|
||
Assert.Equal(("{year}/{date}", "{title}"), (folder.FolderTemplate, folder.FileTemplate));
|
||
|
||
// A template given anew is held to every rule, and the refusal says which.
|
||
var before = await File.ReadAllTextAsync(fx.Paths.ManifestPath, fx.Ct);
|
||
var refused = await Assert.ThrowsAsync<TemplateRuleException>(() =>
|
||
fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Templates = s.Templates with { File = "{n}{ext}" } }, fx.Ct));
|
||
Assert.Equal(("fileTemplate", "Extension", Loc.L("Errors.Template.Extension")), (refused.ParamName, refused.Rule, refused.Message));
|
||
Assert.Equal(before, await File.ReadAllTextAsync(fx.Paths.ManifestPath, fx.Ct));
|
||
|
||
var file = await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Templates = s.Templates with { File = "{date}_{n:0000}" } }, fx.Ct);
|
||
Assert.Equal(("Studio", "{year}/{date}", "{date}_{n:0000}", 2560), (file.Name, file.FolderTemplate, file.FileTemplate, file.ProxyLongEdge));
|
||
|
||
// A new archive has nothing to keep: its templates meet every rule.
|
||
var created = await Assert.ThrowsAsync<TemplateRuleException>(() => fx.Service.CreateAsync(context.CreateTempDirectory("other"), "Other",
|
||
new LayoutTemplates { Folder = "exports/{date}", File = "{date}_{n:0000}" }, null, fx.Ct));
|
||
Assert.Equal(("folderTemplate", "ReservedFolder"), (created.ParamName, created.Rule));
|
||
}
|
||
|
||
[Test]
|
||
public async Task AnOfflineArchiveIsNotChangedAndItsIndexSettingsAreShown()
|
||
{
|
||
await using var fx = await ArchiveFixture.CreateAsync(context);
|
||
await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Proxies = s.Proxies with { LongEdge = 1280 } }, fx.Ct);
|
||
// The drive is unplugged: archive.json is out of reach.
|
||
File.Move(fx.Paths.ManifestPath, fx.Paths.ManifestPath + ".away");
|
||
|
||
await Assert.ThrowsAsync<ArchiveOfflineException>(() =>
|
||
fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Proxies = s.Proxies with { LongEdge = 2560 } }, fx.Ct));
|
||
var shown = await fx.Service.GetSettingsAsync(fx.Archive.Id, fx.Ct);
|
||
Assert.Equal((1280, FrameImageRenderer.ThumbnailLongEdge), (shown.Proxies.LongEdge, shown.Thumbnails.LongEdge));
|
||
// A refresh reports that only the index's stand-ins are known: the thumbnail size above is not the archive's.
|
||
Assert.Null(await fx.Service.RefreshSettingsAsync(fx.Archive.Id, fx.Ct));
|
||
}
|
||
|
||
[Test]
|
||
public async Task ARefreshBringsTheIndexUpToSettingsChangedElsewhere()
|
||
{
|
||
await using var fx = await ArchiveFixture.CreateAsync(context);
|
||
// Another process sharing the archive (a server's admin panel) changed archive.json: this index still has the old row.
|
||
var manifest = ManifestStore.ReadArchive(fx.Paths.ManifestPath);
|
||
ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with
|
||
{
|
||
Name = "Shared",
|
||
LayoutTemplates = manifest.LayoutTemplates with { Folder = "{year}/{camera}" },
|
||
ProxyPolicy = manifest.ProxyPolicy with { LongEdge = 2560 },
|
||
ThumbnailPolicy = manifest.ThumbnailPolicy with { LongEdge = 480 },
|
||
});
|
||
Assert.Equal(fx.Archive.FolderTemplate, (await fx.Db.Archives.GetAsync(fx.Archive.Id, fx.Ct))!.FolderTemplate);
|
||
|
||
var settings = Assert.NotNull(await fx.Service.RefreshSettingsAsync(fx.Archive.Id, fx.Ct));
|
||
Assert.Equal(("{year}/{camera}", 2560, 480), (settings.Templates.Folder, settings.Proxies.LongEdge, settings.Thumbnails.LongEdge));
|
||
var row = (await fx.Db.Archives.GetAsync(fx.Archive.Id, fx.Ct))!;
|
||
Assert.Equal(("Shared", "{year}/{camera}", 2560), (row.Name, row.FolderTemplate, row.ProxyLongEdge));
|
||
|
||
// The drive is unplugged: nothing is known beyond the index, which stays as it is.
|
||
File.Move(fx.Paths.ManifestPath, fx.Paths.ManifestPath + ".away");
|
||
Assert.Null(await fx.Service.RefreshSettingsAsync(fx.Archive.Id, fx.Ct));
|
||
Assert.Equal("{year}/{camera}", (await fx.Db.Archives.GetAsync(fx.Archive.Id, fx.Ct))!.FolderTemplate);
|
||
|
||
// A newer Chronos rewrote the file: what this one reads of it is neither shown nor registered.
|
||
ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with { Name = "Newer", LayoutTemplates = manifest.LayoutTemplates with { Folder = "{camera}" } });
|
||
var newer = JsonNode.Parse(await File.ReadAllTextAsync(fx.Paths.ManifestPath, fx.Ct))!;
|
||
newer["schemaVersion"] = ArchiveManifest.CurrentSchemaVersion + 1;
|
||
await File.WriteAllTextAsync(fx.Paths.ManifestPath, newer.ToJsonString(), fx.Ct);
|
||
Assert.Null(await fx.Service.RefreshSettingsAsync(fx.Archive.Id, fx.Ct));
|
||
var kept = (await fx.Db.Archives.GetAsync(fx.Archive.Id, fx.Ct))!;
|
||
Assert.Equal(("Shared", "{year}/{camera}"), (kept.Name, kept.FolderTemplate));
|
||
|
||
// The folder now holds another archive: its settings are neither shown nor registered as this one's.
|
||
ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with { Id = Guid.CreateVersion7(), Name = "Other" });
|
||
Assert.Null(await fx.Service.RefreshSettingsAsync(fx.Archive.Id, fx.Ct));
|
||
Assert.Equal("Shared", (await fx.Db.Archives.GetAsync(fx.Archive.Id, fx.Ct))!.Name);
|
||
}
|
||
|
||
[Test]
|
||
public async Task ANewArchiveTakesTheDefaultsAndAnExistingOneKeepsItsOwn()
|
||
{
|
||
await using var fx = await ArchiveFixture.CreateAsync(context);
|
||
var defaults = new ArchiveSettings(string.Empty, new LayoutTemplates { Folder = "{camera}/{date}", File = "{time}_{n}" },
|
||
new ProxyPolicy { LongEdge = ProxyPolicy.Disabled }, new ThumbnailPolicy { LongEdge = 640 });
|
||
|
||
var root = Path.Combine(context.CreateTempDirectory("new"), "Mountains");
|
||
Directory.CreateDirectory(root);
|
||
var created = await fx.Service.AdoptAsync(root, defaults, fx.Ct);
|
||
var manifest = ManifestStore.ReadArchive(new ArchivePaths(root).ManifestPath);
|
||
Assert.Equal(("Mountains", "{camera}/{date}", "{time}_{n}"), (created.Name, manifest.LayoutTemplates.Folder, manifest.LayoutTemplates.File));
|
||
Assert.Equal((ProxyPolicy.Disabled, 640), (manifest.ProxyPolicy.LongEdge, manifest.ThumbnailPolicy.LongEdge));
|
||
|
||
// The fixture's archive already exists: adopting it again opens it with its own settings.
|
||
var existing = await fx.Service.AdoptAsync(fx.Archive.RootPath, defaults, fx.Ct);
|
||
Assert.Equal((fx.Archive.Id, fx.Archive.FolderTemplate), (existing.Id, existing.FolderTemplate));
|
||
Assert.Equal(1920, ManifestStore.ReadArchive(fx.Paths.ManifestPath).ProxyPolicy.LongEdge);
|
||
}
|
||
|
||
[Test]
|
||
public async Task ProxyAndThumbnailJobsFollowTheArchiveSettings()
|
||
{
|
||
await using var fx = await ArchiveFixture.CreateAsync(context);
|
||
var folder = Path.Combine(fx.Paths.Root, "Harbour");
|
||
Directory.CreateDirectory(folder);
|
||
for (var i = 0; i < 3; i++)
|
||
await File.WriteAllBytesAsync(Path.Combine(folder, $"IMG_{i + 1:0000}.JPG"), Photo(ArchiveFixture.Start.AddSeconds(2 * i), i), fx.Ct);
|
||
await fx.ScanAsync(ScanMode.Full);
|
||
var sequence = (await fx.Db.Sequences.GetAsync((await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id, fx.Ct)).Single().Id, fx.Ct))!;
|
||
var paths = fx.Paths.Sequence(sequence.Folder);
|
||
|
||
// No proxies for this archive: the proxy job builds none.
|
||
await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Proxies = s.Proxies with { LongEdge = ProxyPolicy.Disabled } }, fx.Ct);
|
||
var proxies = await fx.Service.Images.BuildProxiesAsync(fx.Archive, sequence, force: true, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||
Assert.Equal((0, 0), (proxies.Built, proxies.Reused));
|
||
Assert.False(Directory.Exists(paths.Proxies) && Directory.EnumerateFiles(paths.Proxies).Any(), "no proxy written");
|
||
|
||
// Small thumbnails: every one fits the archive's long edge.
|
||
await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Thumbnails = s.Thumbnails with { LongEdge = ThumbnailPolicy.MinLongEdge } }, fx.Ct);
|
||
var thumbnails = await fx.Service.Images.BuildThumbnailsAsync(fx.Archive, sequence, force: true, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||
Assert.Equal(3, thumbnails.Built);
|
||
using var pack = ThumbPackReader.Open(paths.ThumbsPackPath);
|
||
Assert.Count(3, pack.Entries);
|
||
Assert.True(pack.Entries.Values.All(e => int.Max(e.Width, e.Height) == ThumbnailPolicy.MinLongEdge), "thumbnails at the archive's size");
|
||
}
|
||
|
||
/// <summary>A 480×320 JPEG with its capture time: larger than every thumbnail size.</summary>
|
||
private static byte[] Photo(DateTime captured, int seed)
|
||
{
|
||
using var image = new ImageBuffer(480, 320);
|
||
for (var c = 0; c < 3; c++)
|
||
{
|
||
var plane = image.Plane(c);
|
||
for (var i = 0; i < plane.Length; i++) plane[i] = (seed + 1) * 0.2f * (i % 480) / 480f;
|
||
}
|
||
var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { Make = "Canon", Model = "Canon EOS R5", CapturedAt = captured, Iso = 100 });
|
||
return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] });
|
||
}
|
||
}
|