Second fix round of owner review 2, phase 4a (planner findings).
- A sequence keeps a folder that differs from its own in case only while no
other folder holds that exact name (RenamePlanner.ExistsExactly, every level
compared exactly). On a case-sensitive file system a stray "Trip (2)" beside
the sequence at "trip (2)" no longer gives a case-only step that MoveFolder
refuses at every archive open: the plan gives "Trip", and a plan whose
target appeared since is refused under the step's leases before anything
moves (or dropped when it resumes). A change of case in an outer level no
longer looks like two folders on Windows.
- A file name longer than 200 characters is shortened before the ".{ext}" the
template ends with, never through it, so a long title never strips the
photo's extension.
- The archive engine's own settings check applies the template rules the
server and the desktop apply (a folder scans skip, a lost extension, names
that repeat), so archive.json can no longer take such a template.
- The server's invalid_template answer names the rule after the field
("fileTemplate:extension") with the desktop's sentence as its message; both
admin panels show it on the field.
- FrameNaming keeps only the naming overloads production uses.
- Comments about Unreadable say the whole sequence stays as it is.
- Tests pin filing against Reorganize: an upload without a capture time, a
card's second session with {session}, a duplicate skipped mid-session, and a
whole folder of two cameras named with the first one's camera.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 50fe59aec1e180daa1733bcb951892a79dceba6c)
270 lines
16 KiB
C#
270 lines
16 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json.Nodes;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Data.Jobs;
|
|
using Chronos.Protocol;
|
|
using Chronos.Server.Configuration;
|
|
using Chronos.Server.Hosting;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Server.Tests.Api;
|
|
|
|
[Category("server")]
|
|
public sealed class AdminTests(TestContext context)
|
|
{
|
|
[Test]
|
|
public async Task TheDashboardSummarisesTheServer()
|
|
{
|
|
await using var server = await TestServer.StartAsync(context, o => o.ServerName = "Studio NAS");
|
|
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
|
await Uploads.UploadAsync(editor, Uploads.Photo(4000, 61));
|
|
var admin = await server.AdminAsync();
|
|
using var response = await admin.GetAsync("/api/v1/server/dashboard");
|
|
await response.ExpectAsync(HttpStatusCode.OK);
|
|
var dashboard = await response.ReadAsync(ProtocolJson.Default.DashboardDto);
|
|
Assert.Equal(("Studio NAS", 2, 1L, 1L), (dashboard.ServerName, dashboard.Users, dashboard.Archive.Sequences, dashboard.Archive.Photos));
|
|
Assert.True(dashboard.Database.StartsWith("SQLite ", StringComparison.Ordinal), dashboard.Database);
|
|
Assert.Equal(1L, dashboard.Uploads.Single().Frames);
|
|
Assert.Contains(dashboard.Activity, a => a.Action == "user_created");
|
|
Assert.True(dashboard.Archive.CapacityBytes > 0);
|
|
}
|
|
|
|
[Test]
|
|
public async Task SettingsAreSavedAndRestartOnlyValuesFlagged()
|
|
{
|
|
await using var server = await TestServer.StartAsync(context);
|
|
var admin = await server.AdminAsync();
|
|
using var read = await admin.GetAsync("/api/v1/server/settings");
|
|
var settings = await read.ReadAsync(ProtocolJson.Default.ServerSettingsDto);
|
|
Assert.False(settings.RestartRequired);
|
|
|
|
var edited = settings with { ServerName = "Rooftop", Discovery = true, HttpPort = 18080, HttpsPort = 18443 };
|
|
using (var saved = await admin.PutAsJsonAsync("/api/v1/server/settings", edited, ProtocolJson.Default.ServerSettingsDto))
|
|
Assert.True((await saved.ReadAsync(ProtocolJson.Default.ServerSettingsDto)).RestartRequired);
|
|
// Name and discovery apply at once; ports wait for the restart.
|
|
Assert.Equal(("Rooftop", true, 0), (server.Options.ServerName, server.Options.Discovery, server.Options.HttpPort));
|
|
using (var again = await admin.GetAsync("/api/v1/server/settings"))
|
|
Assert.Equal(18080, (await again.ReadAsync(ProtocolJson.Default.ServerSettingsDto)).HttpPort);
|
|
var file = JsonNode.Parse(await File.ReadAllTextAsync(server.Options.SettingsFile))!.AsObject();
|
|
Assert.Equal(("Rooftop", 18080), (file["ServerName"]!.GetValue<string>(), file["HttpPort"]!.GetValue<int>()));
|
|
Assert.False(file.ContainsKey("AdminPassword"));
|
|
|
|
using var invalid = await admin.PutAsJsonAsync("/api/v1/server/settings", edited with { Language = "tlh" }, ProtocolJson.Default.ServerSettingsDto);
|
|
Assert.Equal("Language", (await invalid.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_setting")).Details);
|
|
}
|
|
|
|
[Test]
|
|
public async Task TheParallelismAndThePriorityQueueApplyAtOnce()
|
|
{
|
|
// Owner review 1 (R2): jobs run one at a time; the parallelism (photos at once) and the priority queue need no restart.
|
|
await using var server = await TestServer.StartAsync(context);
|
|
var admin = await server.AdminAsync();
|
|
using var read = await admin.GetAsync("/api/v1/server/settings");
|
|
var settings = await read.ReadAsync(ProtocolJson.Default.ServerSettingsDto);
|
|
Assert.Equal((2, false), (settings.Parallelism, settings.PriorityQueue));
|
|
Assert.Equal(ServerSettingsStore.Kinds.Count, settings.JobPriorities.Count);
|
|
Assert.Equal(("render", "high"), (settings.JobPriorities[0].Kind, settings.JobPriorities[0].Level));
|
|
Assert.Equal("low", settings.JobPriorities.Single(p => p.Kind == "verify").Level);
|
|
Assert.DoesNotContain("download", settings.JobPriorities.Select(p => p.Kind));
|
|
|
|
// The test server listens on ports the system chose (0), which no request may set.
|
|
var edited = settings with
|
|
{
|
|
HttpPort = 18080,
|
|
HttpsPort = 18443,
|
|
Parallelism = 3,
|
|
PriorityQueue = true,
|
|
JobPriorities = [.. settings.JobPriorities.Select(p => p.Kind == "scan" ? p with { Level = "high" } : p)],
|
|
};
|
|
using (var saved = await admin.PutAsJsonAsync("/api/v1/server/settings", edited, ProtocolJson.Default.ServerSettingsDto))
|
|
await saved.ExpectAsync(HttpStatusCode.OK);
|
|
// Applied at once, while the ports wait for the restart.
|
|
Assert.Equal((3, true, 0), (server.Options.Parallelism, server.Options.PriorityQueue, server.Options.HttpPort));
|
|
var queue = ServerSettingsStore.PriorityQueue(server.Options);
|
|
Assert.True(queue.Enabled);
|
|
Assert.Equal((JobKindPriority.High, JobKindPriority.High), (queue.LevelOf(JobKind.Scan), queue.LevelOf(JobKind.Render)));
|
|
// Only the level that differs from the defaults is stored.
|
|
var file = JsonNode.Parse(await File.ReadAllTextAsync(server.Options.SettingsFile))!.AsObject();
|
|
Assert.Equal((3, true), (file["Parallelism"]!.GetValue<int>(), file["PriorityQueue"]!.GetValue<bool>()));
|
|
Assert.Equal("""{"scan":"high"}""", file["JobPriorities"]!.ToJsonString());
|
|
Assert.False(file.ContainsKey("Workers"));
|
|
|
|
using (var dashboard = await admin.GetAsync("/api/v1/server/dashboard"))
|
|
{
|
|
var d = await dashboard.ReadAsync(ProtocolJson.Default.DashboardDto);
|
|
Assert.Equal((3, true), (d.Parallelism, d.PriorityQueue));
|
|
}
|
|
foreach (var (invalid, setting) in new (ServerSettingsDto, string)[]
|
|
{
|
|
(edited with { Parallelism = 0 }, "Parallelism"),
|
|
(edited with { JobPriorities = [new JobPriorityDto("download", "high")] }, "JobPriorities"),
|
|
(edited with { JobPriorities = [new JobPriorityDto("scan", "urgent")] }, "JobPriorities"),
|
|
})
|
|
{
|
|
using var refused = await admin.PutAsJsonAsync("/api/v1/server/settings", invalid, ProtocolJson.Default.ServerSettingsDto);
|
|
Assert.Equal(setting, (await refused.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_setting")).Details);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task LogsAreReadableButOnlyByName()
|
|
{
|
|
await using var server = await TestServer.StartAsync(context, o => o.LogLevel = Microsoft.Extensions.Logging.LogLevel.Information);
|
|
var admin = await server.AdminAsync();
|
|
using var tail = await admin.GetAsync("/api/v1/logs?lines=20");
|
|
var lines = await tail.ReadAsync(ProtocolJson.Default.LogTailDto);
|
|
Assert.NotEmpty(lines.Lines);
|
|
Assert.True(lines.Lines.Count <= 20);
|
|
using var files = await admin.GetAsync("/api/v1/logs/files");
|
|
var name = (await files.ReadAsync(ProtocolJson.Default.ListLogFileDto)).First().Name;
|
|
using (var download = await admin.GetAsync($"/api/v1/logs/files/{name}")) await download.ExpectAsync(HttpStatusCode.OK);
|
|
using var traversal = await admin.GetAsync("/api/v1/logs/files/..%2Fserver.json");
|
|
await traversal.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
|
}
|
|
|
|
[Test]
|
|
public async Task TheCurrentLogDownloadsWhileTheServerKeepsLogging()
|
|
{
|
|
await using var server = await TestServer.StartAsync(context, o => o.LogLevel = Microsoft.Extensions.Logging.LogLevel.Information);
|
|
var admin = await server.AdminAsync();
|
|
using var files = await admin.GetAsync("/api/v1/logs/files");
|
|
var name = (await files.ReadAsync(ProtocolJson.Default.ListLogFileDto)).First().Name;
|
|
// Jobs, uploads and sign-ins keep writing to the file an administrator downloads: a log of a few megabytes takes
|
|
// a while to send, and a line lands about every millisecond meanwhile. Only to the file and never in a tight loop:
|
|
// engine code of every test server in this process logs through the last one started, so this server's console
|
|
// and file lock are shared with the tests running alongside.
|
|
var logger = server.Services.GetServices<ILoggerProvider>().OfType<FileLoggerProvider>().Single().CreateLogger("Chronos.Server.Tests");
|
|
var block = new string('x', 64 * 1024);
|
|
for (var i = 0; i < 32; i++) logger.LogInformation("{Block}", block);
|
|
using var stop = new CancellationTokenSource();
|
|
var logging = Task.Factory.StartNew(() =>
|
|
{
|
|
while (!stop.IsCancellationRequested)
|
|
{
|
|
logger.LogInformation("Busy");
|
|
Thread.Sleep(1);
|
|
}
|
|
}, TaskCreationOptions.LongRunning);
|
|
try
|
|
{
|
|
for (var i = 0; i < 5; i++)
|
|
{
|
|
using var download = await admin.GetAsync($"/api/v1/logs/files/{name}");
|
|
await download.ExpectAsync(HttpStatusCode.OK);
|
|
Assert.Equal(download.Content.Headers.ContentLength, (await download.Content.ReadAsByteArrayAsync()).LongLength);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
await stop.CancelAsync();
|
|
await logging;
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task ArchiveSettingsValidateTemplatesAndJobsNeedHandlers()
|
|
{
|
|
await using var server = await TestServer.StartAsync(context);
|
|
var admin = await server.AdminAsync();
|
|
using var read = await admin.GetAsync("/api/v1/archive/settings");
|
|
var archive = await read.ReadAsync(ProtocolJson.Default.ArchiveSettingsDto);
|
|
Assert.Equal(Path.GetFullPath(server.Options.ArchivePath), archive.RootPath);
|
|
|
|
using (var bad = await admin.PutAsJsonAsync("/api/v1/archive/settings", archive with { FolderTemplate = "{year/{date" }, ProtocolJson.Default.ArchiveSettingsDto))
|
|
Assert.Equal("folderTemplate", (await bad.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_template")).Details);
|
|
// Templates that would hide sequences from scans, drop extensions or repeat names: refused as Reorganize refuses them,
|
|
// with the rule after the field and the message saying why, as the desktop does.
|
|
foreach (var (refused, details, rule) in new[]
|
|
{
|
|
(archive with { FolderTemplate = "Originals/{year}" }, "folderTemplate:reserved_folder", "ReservedFolder"),
|
|
(archive with { FileTemplate = "{n}{ext}" }, "fileTemplate:extension", "Extension"),
|
|
(archive with { FileTemplate = "{date}" }, "fileTemplate:not_unique", "NotUnique"),
|
|
(archive with { FileTemplate = " " }, "fileTemplate", (string?)null),
|
|
})
|
|
{
|
|
using var response = await admin.PutAsJsonAsync("/api/v1/archive/settings", refused, ProtocolJson.Default.ArchiveSettingsDto);
|
|
var error = await response.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_template");
|
|
Assert.Equal(details, error.Details);
|
|
Assert.Equal(Chronos.Core.Localization.Loc.Get(rule is null ? "Server.Error.InvalidTemplate" : "Errors.Template." + rule,
|
|
System.Globalization.CultureInfo.GetCultureInfo("en")), error.Message);
|
|
}
|
|
using (var good = await admin.PutAsJsonAsync("/api/v1/archive/settings", archive with { Name = "Studio", ProxyLongEdge = 2560 }, ProtocolJson.Default.ArchiveSettingsDto))
|
|
{
|
|
var saved = await good.ReadAsync(ProtocolJson.Default.ArchiveSettingsDto);
|
|
Assert.Equal(("Studio", 2560), (saved.Name, saved.ProxyLongEdge));
|
|
}
|
|
using var scan = await admin.PostAsync("/api/v1/archive/scan", null);
|
|
await scan.ExpectErrorAsync(HttpStatusCode.Conflict, "job_unavailable");
|
|
using var stats = await admin.GetAsync("/api/v1/archive/stats");
|
|
await stats.ExpectAsync(HttpStatusCode.OK);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ArchiveSettingsAreWrittenToArchiveJsonAndSurviveARestart()
|
|
{
|
|
await using var server = await TestServer.StartAsync(context);
|
|
var admin = await server.AdminAsync();
|
|
using var read = await admin.GetAsync("/api/v1/archive/settings");
|
|
var archive = await read.ReadAsync(ProtocolJson.Default.ArchiveSettingsDto);
|
|
|
|
var changed = archive with { Name = "Studio", FolderTemplate = "{year}/{camera}", FileTemplate = "{name}", ProxyLongEdge = -1 };
|
|
using (var put = await admin.PutAsJsonAsync("/api/v1/archive/settings", changed, ProtocolJson.Default.ArchiveSettingsDto))
|
|
await put.ExpectAsync(HttpStatusCode.OK);
|
|
|
|
// archive.json is the truth (BRIEF §7): the proxy jobs read it, and the next start registers the archive from it.
|
|
var manifest = Chronos.Core.Archiving.ManifestStore.ReadArchive(new Chronos.Core.Archiving.ArchivePaths(server.Options.ArchivePath).ManifestPath);
|
|
Assert.Equal(("Studio", "{year}/{camera}", "{name}", -1), (manifest.Name, manifest.LayoutTemplates.Folder, manifest.LayoutTemplates.File, manifest.ProxyPolicy.LongEdge));
|
|
var restarted = await Chronos.Server.Storage.ServerArchive.OpenAsync(server.Database, server.Options, context.Cancellation);
|
|
Assert.Equal(("Studio", "{name}", -1), (restarted.Current.Name, restarted.Current.FileTemplate, restarted.Current.ProxyLongEdge));
|
|
using var again = await admin.GetAsync("/api/v1/archive/settings");
|
|
Assert.Equal(changed, await again.ReadAsync(ProtocolJson.Default.ArchiveSettingsDto));
|
|
|
|
// The archive drive is gone: nothing is written and the admin hears why.
|
|
var path = new Chronos.Core.Archiving.ArchivePaths(server.Options.ArchivePath).ManifestPath;
|
|
File.Move(path, path + ".away");
|
|
try
|
|
{
|
|
using var offline = await admin.PutAsJsonAsync("/api/v1/archive/settings", changed with { ProxyLongEdge = 1280 }, ProtocolJson.Default.ArchiveSettingsDto);
|
|
await offline.ExpectErrorAsync(HttpStatusCode.ServiceUnavailable, "archive_offline");
|
|
}
|
|
finally
|
|
{
|
|
File.Move(path + ".away", path);
|
|
}
|
|
Assert.Equal(-1, Chronos.Core.Archiving.ManifestStore.ReadArchive(path).ProxyPolicy.LongEdge);
|
|
|
|
// A damaged archive.json is reported as such (not as an internal error) and left as it is.
|
|
var saved = await File.ReadAllBytesAsync(path, context.Cancellation);
|
|
await File.WriteAllTextAsync(path, "{ not json", context.Cancellation);
|
|
try
|
|
{
|
|
using var damaged = await admin.PutAsJsonAsync("/api/v1/archive/settings", changed with { ProxyLongEdge = 1280 }, ProtocolJson.Default.ArchiveSettingsDto);
|
|
await damaged.ExpectErrorAsync(HttpStatusCode.Conflict, "archive_manifest");
|
|
Assert.Equal("{ not json", await File.ReadAllTextAsync(path, context.Cancellation));
|
|
}
|
|
finally
|
|
{
|
|
await File.WriteAllBytesAsync(path, saved, context.Cancellation);
|
|
}
|
|
|
|
// A policy this form cannot set, written out of range by another tool, is named so the admin can correct the file.
|
|
var current = Chronos.Core.Archiving.ManifestStore.ReadArchive(path);
|
|
foreach (var (odd, property) in new[]
|
|
{
|
|
(current with { ThumbnailPolicy = new Chronos.Core.Archiving.ThumbnailPolicy { LongEdge = 2048 } }, "thumbnailPolicy"),
|
|
(current with { ProxyPolicy = current.ProxyPolicy with { Quality = 0 } }, "proxyPolicy"),
|
|
})
|
|
{
|
|
Chronos.Core.Archiving.ManifestStore.WriteArchive(path, odd);
|
|
var before = await File.ReadAllBytesAsync(path, context.Cancellation);
|
|
using var refused = await admin.PutAsJsonAsync("/api/v1/archive/settings", changed with { ProxyLongEdge = 1280 }, ProtocolJson.Default.ArchiveSettingsDto);
|
|
Assert.Equal(property, (await refused.ExpectErrorAsync(HttpStatusCode.Conflict, "archive_policy")).Details);
|
|
Assert.SequenceEqual(before, await File.ReadAllBytesAsync(path, context.Cancellation));
|
|
}
|
|
await File.WriteAllBytesAsync(path, saved, context.Cancellation);
|
|
}
|
|
}
|