Files
Alby96andClaude Opus 5.5 efa87f68ce
CI / Build and test (push) Failing after 3m1s
CI / PostgreSQL 16 integration (push) Skipped
CI / PostgreSQL 17 integration (push) Skipped
CI / Docker image (push) Skipped
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 10s
Release / Build, test and publish (push) Successful in 15m31s
Release / Server image (push) Successful in 1m13s
feat: owner review 2 phase 4b: mass conversion after import (#13)
Convert photos already in the archive to JPEG, TIFF-16 or Linear DNG, from the panel's Library and the desktop's Archive and Library. Each run asks Replace (same frames, originals to the trash, recoverable) or New sequence. A dry run lists what converts, what is skipped and why, size before and after, and free space; the job converts exactly the confirmed photos, resumes, and commits all or nothing per sequence. A conversion never changes what Chronos reads from a photo. Replaced originals are erased only on the archive's own word that their conversion is gone. Desktop TIFFs that WIC showed black or as noise now go to the managed reader; the Edit picker no longer spins a core.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-11-03 10:41:50 +01:00

680 lines
39 KiB
C#

using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Chronos.Protocol;
using Chronos.Server.Hosting;
using Chronos.Testing;
namespace Chronos.Server.Tests.Hosting;
[Category("server")]
public sealed partial class SystemEndpointTests(TestContext context)
{
[Test]
public async Task HealthReportsOk()
{
await using var server = await TestServer.StartAsync(context);
var health = await server.Client.GetFromJsonAsync("/health", ProtocolJson.Default.HealthStatus);
Assert.Equal("ok", Assert.NotNull(health).Status);
Assert.Equal("ok", health.Checks["archive"]);
}
[Test]
public async Task ServerInfoCarriesFingerprintAndApiHeader()
{
await using var server = await TestServer.StartAsync(context);
using var response = await server.Client.GetAsync("/api/v1/server/info");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("1", response.Headers.GetValues(ApiVersion.Header).Single());
var info = await response.Content.ReadFromJsonAsync(ProtocolJson.Default.ServerInfo);
Assert.Equal(1, Assert.NotNull(info).ApiVersion);
Assert.Equal(95, Assert.NotNull(info.TlsFingerprint).Length); // 32 bytes as XX: pairs
}
[Test]
public async Task HttpsServesTheAdvertisedCertificate()
{
await using var server = await TestServer.StartAsync(context);
var info = await server.Client.GetFromJsonAsync("/api/v1/server/info", ProtocolJson.Default.ServerInfo);
string? seen = null;
using var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (_, cert, _, _) =>
{
seen = cert is null ? null : string.Join(':', cert.GetCertHash(System.Security.Cryptography.HashAlgorithmName.SHA256).Select(b => b.ToString("X2", System.Globalization.CultureInfo.InvariantCulture)));
return true;
},
};
using var https = new HttpClient(handler) { BaseAddress = Assert.NotNull(server.HttpsBase) };
using var response = await https.GetAsync("/health");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(Assert.NotNull(info).TlsFingerprint, seen);
}
[Test]
public async Task CertificateIsReusedAcrossRestarts()
{
var root = context.CreateTempDirectory("restart");
string? first;
await using (var a = await TestServer.StartAsync(context, o => o.DataPath = Path.Combine(root, "data")))
first = (await a.Client.GetFromJsonAsync("/api/v1/server/info", ProtocolJson.Default.ServerInfo))!.TlsFingerprint;
await using var b = await TestServer.StartAsync(context, o => o.DataPath = Path.Combine(root, "data"));
var second = (await b.Client.GetFromJsonAsync("/api/v1/server/info", ProtocolJson.Default.ServerInfo))!.TlsFingerprint;
Assert.Equal(first, second);
}
[Test]
public async Task UnknownApiRouteReturnsJsonError()
{
await using var server = await TestServer.StartAsync(context);
using var response = await server.Client.GetAsync("/api/v1/does-not-exist");
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
var error = await response.Content.ReadFromJsonAsync(ProtocolJson.Default.ApiError);
Assert.Equal("not_found", Assert.NotNull(error).Code);
}
[Test]
public async Task AdminPanelIsServedWithEtags()
{
await using var server = await TestServer.StartAsync(context);
using var redirect = await new HttpClient(new HttpClientHandler { AllowAutoRedirect = false }) { BaseAddress = server.HttpBase }.GetAsync("/admin");
Assert.Equal(HttpStatusCode.Redirect, redirect.StatusCode);
using var page = await server.Client.GetAsync("/admin/");
Assert.Equal(HttpStatusCode.OK, page.StatusCode);
Assert.Contains("app.js", await page.Content.ReadAsStringAsync());
var etag = page.Headers.ETag;
using var request = new HttpRequestMessage(HttpMethod.Get, "/admin/");
request.Headers.IfNoneMatch.Add(Assert.NotNull(etag));
using var cached = await server.Client.SendAsync(request);
Assert.Equal(HttpStatusCode.NotModified, cached.StatusCode);
}
[Test]
public async Task AdminPanelFilesAreRevalidatedSoAnUpgradeNeverMixesOldAndNewModules()
{
// A browser that kept app.js from before an upgrade must not run it next to a new ui.js: every file is kept but
// asked for again before each use, and its ETag follows its content, so only what the upgrade changed is sent.
await using var server = await TestServer.StartAsync(context);
foreach (var path in new[] { "/admin/", "/admin/app.js", "/admin/ui.js", "/admin/admin.css", "/admin/icons.js", "/admin/pages/library.js", "/admin/upload.js" })
{
using var response = await server.Client.GetAsync(path);
Assert.Equal(HttpStatusCode.OK, response.StatusCode, path);
var cache = Assert.NotNull(response.Headers.CacheControl);
Assert.True(cache.NoCache && cache.MaxAge is null && !cache.Public, $"{path}: {cache}");
var etag = Assert.NotNull(response.Headers.ETag);
Assert.False(etag.IsWeak, path);
var bytes = await response.Content.ReadAsByteArrayAsync();
Assert.Equal($"\"{Convert.ToHexString(SHA256.HashData(bytes).AsSpan(0, 12))}\"", etag.Tag, path);
foreach (var sent in new[] { etag, new EntityTagHeaderValue(etag.Tag, isWeak: true), EntityTagHeaderValue.Any })
{
using var again = new HttpRequestMessage(HttpMethod.Get, path);
again.Headers.IfNoneMatch.Add(new EntityTagHeaderValue("\"0123\""));
again.Headers.IfNoneMatch.Add(sent);
using var notModified = await server.Client.SendAsync(again);
Assert.Equal(HttpStatusCode.NotModified, notModified.StatusCode, $"{path} with {sent}");
Assert.Equal(etag, notModified.Headers.ETag, path);
Assert.True(Assert.NotNull(notModified.Headers.CacheControl).NoCache, path);
}
// A file the browser holds from another build has another ETag: the new content comes back.
using var stale = new HttpRequestMessage(HttpMethod.Get, path);
stale.Headers.IfNoneMatch.Add(new EntityTagHeaderValue("\"0123456789ABCDEF01234567\""));
using var fresh = await server.Client.SendAsync(stale);
Assert.Equal(HttpStatusCode.OK, fresh.StatusCode, path);
var resent = await fresh.Content.ReadAsByteArrayAsync();
Assert.BytesEqual(bytes, resent, path);
}
}
[Test]
public async Task PlainHttpFromAnotherMachineGoesToThePublicHttpsPort()
{
// The container listens on 8443, published on the host as 18443 (BRIEF §19.6: the template's ports are editable).
await using var server = await TestServer.StartAsync(context, o => o.PublicHttpsPort = 18443);
var kestrel = server.Services.GetRequiredService<IServer>();
var request = AdminRequest(IPAddress.Parse("10.0.0.7"));
Assert.Equal("https://10.0.0.5:18443/admin/?page=jobs", AdminPanel.HttpsLocation(request, server.Options, kestrel));
server.Options.PublicHttpsPort = null;
Assert.Equal($"https://10.0.0.5:{Assert.NotNull(server.HttpsBase).Port}/admin/?page=jobs", AdminPanel.HttpsLocation(request, server.Options, kestrel));
Assert.Null(AdminPanel.HttpsLocation(AdminRequest(IPAddress.Loopback), server.Options, kestrel));
}
private static DefaultHttpContext AdminRequest(IPAddress from)
{
var request = new DefaultHttpContext();
request.Connection.RemoteIpAddress = from;
request.Request.Scheme = "http";
request.Request.Host = new HostString("10.0.0.5", 18080);
request.Request.Path = "/admin/";
request.Request.QueryString = new QueryString("?page=jobs");
return request;
}
[Test]
public async Task AdminPanelRunsUnderAStrictPolicyAndIsNeverFramed()
{
await using var server = await TestServer.StartAsync(context);
foreach (var path in new[] { "/admin/", "/admin/app.js", "/admin/admin.css" })
{
using var response = await server.Client.GetAsync(path);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var policy = string.Join(' ', response.Headers.GetValues("Content-Security-Policy"));
foreach (var directive in new[] { "default-src 'self'", "script-src 'self'", "style-src 'self'", "frame-ancestors 'none'" })
Assert.Contains(directive, policy);
Assert.True(!policy.Contains("unsafe-inline", StringComparison.Ordinal), policy);
Assert.Equal("DENY", response.Headers.GetValues("X-Frame-Options").Single());
}
}
[Test]
public async Task AdminPanelModulesAllResolve()
{
await using var server = await TestServer.StartAsync(context);
var index = await server.Client.GetStringAsync("/admin/");
// The page container is not a live region: only toasts and form errors announce themselves.
Assert.True(!index.Contains("aria-live", StringComparison.Ordinal), index);
var seen = await ModulesAsync(server);
Assert.True(seen.Count >= 20, string.Join(", ", seen.Keys));
// The Library's views (owner review 2, R6) are modules of their own under pages/library; the web upload (R3) is the
// dialog, its hash worker and the in-house SHA-256 the worker imports; Reorganize (R7) is the dialog the Library and
// the Archive page import, Convert (R7) the one the Library imports.
foreach (var module in new[] { "pages/library.js", "pages/library/sequence.js", "pages/library/trash.js", "pages/library/shared.js", "upload.js", "hash-worker.js", "sha256.js", "reorganize.js", "convert.js" })
Assert.True(seen.ContainsKey(module), module);
}
[Test]
public async Task AdminPanelListsTheLibraryAndImportsTheWebUploadOnlyDynamically()
{
// Owner review 2, R6: the Library comes right after the Dashboard. The web upload (R3) is a module of its own that
// only the Library list and a sequence import, and only dynamically (never statically): the other pages never load
// it, and the Library still works, without Upload and Add photos, when the module does not load.
await using var server = await TestServer.StartAsync(context);
var app = await server.Client.GetStringAsync("/admin/app.js");
var top = MenuPages(app)[0];
Assert.SequenceEqual(["dashboard", "library"], top.Take(2));
var icons = await server.Client.GetStringAsync("/admin/icons.js");
var strings = Assert.NotNull(await server.Client.GetFromJsonAsync("/api/v1/i18n/it", ProtocolJson.Default.DictionaryStringString));
foreach (var page in MenuPages(app).SelectMany(group => group))
{
Assert.Contains($" {page}: `<svg", icons);
var name = $"Admin.Nav.{char.ToUpperInvariant(page[0])}{page[1..]}";
Assert.True(strings.ContainsKey(name) && strings.ContainsKey(name + ".Tip"), name);
}
var modules = await ModulesAsync(server);
Assert.Contains(@"import(""../upload.js"")", modules["pages/library.js"]);
foreach (var (module, source) in modules)
{
var folder = module.Contains('/', StringComparison.Ordinal) ? module[..(module.LastIndexOf('/') + 1)] : string.Empty;
foreach (Match import in StaticImport().Matches(source))
Assert.True(Resolve(folder, import.Groups[1].Value) != "upload.js", $"{module} imports upload.js statically");
}
// The module the dynamic import names is there (no 404 in the browser) and exports what the Library calls.
Assert.Equal("upload.js", Resolve("pages/", "../upload.js"));
Assert.True(Regex.IsMatch(modules["upload.js"], @"export (?:async )?function openUpload\(\{ api, sequence, onDone \}"), "upload.js exports openUpload({ api, sequence, onDone })");
Assert.Contains("openUpload({ api, onDone: ", modules["pages/library.js"]);
Assert.Contains("openUpload({ api, sequence: seq, onDone: ", modules["pages/library/sequence.js"]);
}
[Test]
public async Task TheTrashPageSeesAnEmptyingThatEndedBeforeItsPostAnswered()
{
// The worker claims an emptying as soon as it is queued and can end it before the browser has read the 202: the page
// keeps the last state of the jobs that ended and looks its own job up there before it starts following it.
await using var server = await TestServer.StartAsync(context);
var trash = await server.Client.GetStringAsync("/admin/pages/library/trash.js");
Assert.Contains("ended.set(e.jobId, e)", trash);
var lookedUp = trash.IndexOf("ended.get(job.id)", StringComparison.Ordinal);
var followed = trash.IndexOf("emptyJob = job.id", StringComparison.Ordinal);
Assert.True(lookedUp > 0 && followed > lookedUp, "the queued job is looked up among the ended ones before it is followed");
// An emptying started elsewhere sends no sequenceChanged: any job that ends shows the trash again, and a restore
// of an entry that is gone (404) does too.
Assert.True(Regex.IsMatch(trash, @"e\.type === ""jobState"" && JOB_ENDED\.has\(e\.state\)"), "every ended job is seen");
Assert.True(Regex.IsMatch(trash, @"if \(err\.status === 404\) await load\(\);"), "a restore of a gone entry reloads the trash");
}
[Test]
public async Task ASequencePageRefreshesAfterJobsAndAsksAgainForMissingThumbnails()
{
// Thumbnail, proxy, verify and analysis jobs send no sequenceChanged: when any job ends, the sequence page reads the
// sequence again (facts, status chips) and asks again, past the browser's first answer, for the images that failed.
await using var server = await TestServer.StartAsync(context);
var sequence = await server.Client.GetStringAsync("/admin/pages/library/sequence.js");
var shared = await server.Client.GetStringAsync("/admin/pages/library/shared.js");
Assert.True(Regex.IsMatch(sequence, @"e\.type === ""jobState""\) \{\s+(//[^\n]*\s+)*if \(JOB_ENDED\.has\(e\.state\) && jobTimer === null\) jobTimer = setTimeout\(afterJob"),
"an ended job schedules one refresh, not pushed back by the next");
Assert.True(Regex.IsMatch(sequence, @"async function afterJob\(\) \{[^}]*const changed = await reload\(false\);[^}]*retryImages\(root, changed\);", RegexOptions.Singleline),
"the refresh reads the sequence again, then retries the cover and the photos, all of them again once the sequence changed");
Assert.Contains(@"export const JOB_ENDED = new Set([""done"", ""failed"", ""cancelled""]);", shared);
// A thumbnail that never comes (its original is missing) is not asked for after every job for as long as the page
// stays open: each retry waits twice as long as the last, and an image stops after RETRY_TRIES.
Assert.True(Regex.IsMatch(shared, @"export function retryImages\(root, fresh = false\) \{[^}]*root\.querySelectorAll\(""img\.failed""\)[^}]*" +
@"if \(tries >= RETRY_TRIES \|\| \(!fresh && now < Number\(img\.dataset\.retryAt \?\? 0\)\)\) return;[^}]*" +
@"Math\.min\(RETRY_FIRST_MS \* 2 \*\* tries, RETRY_LONGEST_MS\)[^}]*searchParams\.set\(""retry"",", RegexOptions.Singleline),
"a retried image waits longer each time, stops, and gets an address the browser has not tried");
Assert.True(Regex.IsMatch(shared, @"const RETRY_TRIES = \d+;"), "RETRY_TRIES");
// The Library list also counts a cancelled job as ended.
var library = await server.Client.GetStringAsync("/admin/pages/library.js");
Assert.Contains(@"e.type === ""jobState"" && JOB_ENDED.has(e.state)", library);
}
[Test]
public async Task ASequencePageRefreshDrawsOnlyWhatChangedAndKeepsTheFocus()
{
// The refresh after every ended job must not drop a keyboard user on the body: the header is drawn again only when
// the sequence read differs from the one shown, the renders only when their list does, and every redraw of the
// stars, the tags and the renders puts the focus back on the same control.
await using var server = await TestServer.StartAsync(context);
var sequence = await server.Client.GetStringAsync("/admin/pages/library/sequence.js");
Assert.True(Regex.IsMatch(sequence, @"async function reload\(withFrames\) \{[^}]*const changed = JSON\.stringify\(seq\) !== JSON\.stringify\(shown\);\s+if \(changed\) drawHeader\(\);", RegexOptions.Singleline),
"the header is drawn again only when the sequence changed");
foreach (var host in new[] { "tags", "rating" })
{
Assert.True(Regex.IsMatch(sequence, $@"redraw\({host}, \(\) => \{{\s+{host}\.innerHTML = "), $"#{host} is drawn keeping the focus");
Assert.Count(1, Regex.Matches(sequence, $@"\b{host}\.innerHTML = "));
}
Assert.True(Regex.IsMatch(sequence, @"if \(key === rendersShown \|\| !root\.isConnected\) return;\s+rendersShown = key;\s+renders = list;\s+const host = root\.querySelector\(""#renders""\);\s+redraw\(host, \(\) => drawRenders\(host\)\);"),
"the renders are drawn again only when they changed, keeping the focus");
Assert.Count(2, Regex.Matches(sequence, @"\bdrawRenders\("));
// A control without a name (a tag's remove button) is found again by its place, not as the first unnamed one.
var ui = await server.Client.GetStringAsync("/admin/ui.js");
Assert.Contains("const same = place.control ? controls.find((c) => controlKey(c) === place.control) : undefined;", ui);
}
[Test]
public async Task TheTrashPageReadsItsEmptyingAgainAndKeepsARestoreDisabledUntilItAnswers()
{
// The end of an emptying can be lost while the events are not connected: whenever the page reads the trash (and
// after the socket connects again) it reads the job it follows, so Empty never stays disabled.
await using var server = await TestServer.StartAsync(context);
var trash = await server.Client.GetStringAsync("/admin/pages/library/trash.js");
Assert.True(Regex.IsMatch(trash, @"async function load\(\) \{\s+if \(emptyJob !== null\) await checkEmptyJob\(\);"), "load reads the emptying first");
Assert.True(Regex.IsMatch(trash, @"async function checkEmptyJob\(\) \{[^}]*api\.get\(`/api/v1/jobs/\$\{encodeURIComponent\(id\)\}`\);.*?if \(err\.status === 404 && emptyJob === id\) emptyJob = null;.*?" +
@"if \(emptyJob === id && JOB_ENDED\.has\(job\.state\)\) emptyEnded\(job\);", RegexOptions.Singleline), "the job read ends the emptying, and a job gone too");
Assert.True(Regex.IsMatch(trash, @"e\.type === ""reconnected""\) \{\s+(//[^\n]*\s+)*if \(root\.isConnected\) load\(\);"), "a reconnect reads the trash again");
var api = await server.Client.GetStringAsync("/admin/api.js");
Assert.True(Regex.IsMatch(api, @"socket\.onopen = \(\) => \{\s+delay = 1000;\s+if \(opened\) \{\s+try \{\s+onEvent\(\{ type: ""reconnected"" \}\);"), "the socket says when it connected again");
// A redraw while a restore is on its way (a job ended, a sequence changed) keeps its button disabled, so the same
// entry is not sent twice.
Assert.Contains(@"${restoring.has(e.name) ? ""disabled"" : """"}", trash);
Assert.True(Regex.IsMatch(trash, @"if \(!entry \|\| restoring\.has\(entry\.name\)\) return;\s+restoring\.add\(entry\.name\);"), "a restore marks its entry");
Assert.True(Regex.IsMatch(trash, @"catch \(err\) \{\s+restoring\.delete\(entry\.name\);"), "a refused restore frees its button");
Assert.True(Regex.IsMatch(trash, @"await load\(\);\s+restoring\.delete\(entry\.name\);\s+\}"), "a restore frees its entry once the trash is read again");
}
[Test]
public async Task AdminModulesCallTheApiOnlyWhereTheyHaveIt()
{
// upload.js once called api from a top-level helper while api was only openUpload's parameter: every completion
// threw a ReferenceError that string checks never saw. A module that neither declares api (api.js) nor imports it
// (app.js) takes it as a parameter in each top-level function that uses it, and uses it nowhere else.
await using var server = await TestServer.StartAsync(context);
var modules = await ModulesAsync(server);
var checkedUses = 0;
foreach (var (module, source) in modules)
{
var code = JsCode(source);
if (HasApi().IsMatch(code)) continue;
foreach (var (name, parameters, body) in TopLevel(code))
{
if (!ApiName().IsMatch(body)) continue;
checkedUses++;
Assert.True(ApiName().IsMatch(parameters), $"{module}: {name} uses api but does not take it");
}
}
Assert.Greater(checkedUses, 5);
Assert.Contains("complete(api, item.uploadId, restore, signal)", modules["upload.js"]);
// The check sees the mistake it is here for, and is not fooled by strings, comments, templates or regular expressions.
const string broken = """
import { ApiFailure } from "./api.js";
// api.get in a comment
export function openUpload({ api, sequence } = {}) {
return complete(1, /api\//.test("x"));
}
async function complete(id) {
const url = `/api/v1/uploads/${id}/complete${id ? `?api=${"api"}` : ""}`;
return api.post(url);
}
function retry(call) {
return call("api");
}
""";
var flagged = TopLevel(JsCode(broken)).Where(s => ApiName().IsMatch(s.Body) && !ApiName().IsMatch(s.Parameters)).Select(s => s.Name);
Assert.SequenceEqual(["complete"], flagged);
}
/// <summary>
/// A module's code with its comments, string and regular expression literals and the text of its template literals
/// blanked (their expressions kept), each character where it was: what is left are names, keywords and punctuation.
/// </summary>
private static string JsCode(string source)
{
var code = source.ToCharArray();
// The brace depth at which each template expression open ends and its template's text goes on.
var templates = new Stack<int>();
var depth = 0;
var i = 0;
void Blank(int from, int to)
{
for (var k = from; k < Math.Min(to, code.Length); k++)
if (code[k] != '\n') code[k] = ' ';
}
// From the text of a template literal: true at its closing backtick, false at an expression; i past either.
bool TemplateText()
{
var start = i;
while (i < code.Length)
{
if (code[i] == '\\') i += 2;
else if (code[i] == '`')
{
Blank(start, i++);
return true;
}
else if (code[i] == '$' && i + 1 < code.Length && code[i + 1] == '{')
{
Blank(start, i);
i += 2;
return false;
}
else i++;
}
Blank(start, i);
return true;
}
while (i < code.Length)
{
var c = code[i];
var next = i + 1 < code.Length ? code[i + 1] : '\0';
if (c == '/' && next is '/' or '*')
{
var end = next == '/' ? source.IndexOf('\n', i) : source.IndexOf("*/", i + 2, StringComparison.Ordinal);
end = end < 0 ? code.Length : next == '/' ? end : end + 2;
Blank(i, end);
i = end;
}
else if (c is '"' or '\'' || (c == '/' && RegexMayStart(code, i)))
{
var start = i++;
var inClass = false;
while (i < code.Length && code[i] != '\n' && (code[i] != c || inClass))
{
if (code[i] == '\\') i++;
else if (c == '/' && code[i] == '[') inClass = true;
else if (c == '/' && code[i] == ']') inClass = false;
i++;
}
Blank(start + 1, i++);
}
else if (c == '`')
{
i++;
if (!TemplateText()) templates.Push(++depth);
}
else if (c == '}' && templates.Count > 0 && templates.Peek() == depth)
{
templates.Pop();
depth--;
i++;
if (!TemplateText()) templates.Push(++depth);
}
else
{
if (c == '{') depth++;
else if (c == '}') depth--;
i++;
}
}
return new string(code);
}
/// <summary>Whether the slash at <paramref name="at"/> starts a regular expression (after an operator or a keyword), not a division.</summary>
private static bool RegexMayStart(char[] code, int at)
{
var p = at - 1;
while (p >= 0 && char.IsWhiteSpace(code[p])) p--;
if (p < 0 || "(,=:[!&|?{};+-*%<>~^".Contains(code[p], StringComparison.Ordinal)) return true;
if (!(char.IsLetterOrDigit(code[p]) || code[p] is '_' or '$')) return false;
var end = p + 1;
while (p >= 0 && (char.IsLetterOrDigit(code[p]) || code[p] is '_' or '$')) p--;
return new string(code, p + 1, end - p - 1) is "return" or "typeof" or "case" or "in" or "of" or "delete" or "void" or "throw" or "new";
}
/// <summary>
/// The top-level statements of blanked code (each starts at the beginning of a line): a function's name, parameters and
/// body, or another statement's first line, no parameters and its text.
/// </summary>
private static IEnumerable<(string Name, string Parameters, string Body)> TopLevel(string code)
{
var starts = TopLevelStart().Matches(code).Select(m => m.Index).Append(code.Length).ToList();
for (var s = 0; s + 1 < starts.Count; s++)
{
var text = code[starts[s]..starts[s + 1]];
var head = FunctionHead().Match(text);
if (!head.Success)
{
yield return (text.Split('\n')[0].Trim(), string.Empty, text);
continue;
}
var open = head.Index + head.Length - 1;
var close = open;
for (var level = 0; close < text.Length; close++)
{
if (text[close] == '(') level++;
else if (text[close] == ')' && --level == 0) break;
}
yield return (head.Groups[1].Value, text[(open + 1)..close], text[(close + 1)..]);
}
}
[GeneratedRegex(@"^[A-Za-z_$]", RegexOptions.Multiline)]
private static partial Regex TopLevelStart();
[GeneratedRegex(@"^(?:export\s+)?(?:async\s+)?function\s*\*?\s*([\w$]*)\s*\(")]
private static partial Regex FunctionHead();
/// <summary>The name api, not a member (x.api) nor part of a longer name.</summary>
[GeneratedRegex(@"(?<![\w$.])api(?![\w$])")]
private static partial Regex ApiName();
/// <summary>A module that imports api or declares it at its top level.</summary>
[GeneratedRegex(@"^(?:import \{[^}]*(?<![\w$])api(?![\w$])[^}]*\} from|(?:export )?(?:const|let|var) api(?![\w$]))", RegexOptions.Multiline)]
private static partial Regex HasApi();
[Test]
public async Task TheTrashsRestoreTooltipsSayWhatIsChecked()
{
// A photo entry is hashed against its record before it goes back; a sequence entry is moved back as a folder and
// nothing is hashed (SequenceTrash.RestoreAsync), so its tooltip must not promise a check.
await using var server = await TestServer.StartAsync(context);
foreach (var (culture, verify) in new[] { ("en", "Verify"), ("it", "Verifica") })
{
var strings = Assert.NotNull(await server.Client.GetFromJsonAsync($"/api/v1/i18n/{culture}", ProtocolJson.Default.DictionaryStringString));
Assert.Contains("SHA-256", strings["Admin.Trash.RestorePhotos.Tip"]);
var sequence = strings["Admin.Trash.RestoreSequence.Tip"];
Assert.False(sequence.Contains("SHA-256", StringComparison.Ordinal), $"{culture}: {sequence}");
// The check it does not make is named, with the job that makes it.
Assert.Contains(verify, sequence);
}
}
[Test]
public async Task TheLibraryListDropsSelectAllWhenNoUnloadedSequenceStaysSelectedAndCapsItsDeleteList()
{
await using var server = await TestServer.StartAsync(context);
var library = await server.Client.GetStringAsync("/admin/pages/library.js");
var sequence = await server.Client.GetStringAsync("/admin/pages/library/sequence.js");
var shared = await server.Client.GetStringAsync("/admin/pages/library/shared.js");
// Esc and Ctrl+A in the grid change the selection through onChange: once no selected sequence lies beyond the loaded
// rows, "Select all" no longer holds, so its button comes back and a live refresh prunes the selection again.
Assert.True(Regex.IsMatch(library, @"onChange: \(\) => \{.*?if \(matching\) \{.*?\[\.\.\.selection\.ids\]\.every\(\(id\) => loaded\.has\(id\)\)\) matching = null;.*?syncSelection\(\);", RegexOptions.Singleline),
"the grid's onChange drops matching");
Assert.True(Regex.IsMatch(library, @"function clearFilters\(\) \{(?:(?!\n \}).)*selection\.clear\(\);\s+matching = null;", RegexOptions.Singleline),
"clearing the filters clears the selection as setFilter does");
// A delete of thousands of sequences or photos lists at most LISTED names (the dialog's title counts them all); the
// sequences that stay come first, with their reasons.
Assert.True(Regex.IsMatch(shared, @"export const LISTED = 500;"), "LISTED");
Assert.True(Regex.IsMatch(library, @"list: listed\(\[\.\.\.blocked\.map\("), "the sequences' delete list is capped, blocked first");
Assert.Contains("list: listed(names),", sequence);
Assert.False(Regex.IsMatch(library + sequence, @"list: \[\.\.\."), "an uncapped delete list");
}
/// <summary>The admin panel's modules from app.js and the menu's pages along their static imports, with their source.</summary>
private static async Task<Dictionary<string, string>> ModulesAsync(TestServer server)
{
var app = await server.Client.GetStringAsync("/admin/app.js");
// The upload dialog is a module the Library imports dynamically (owner review 2, R3); its hash worker is named by a URL.
var pending = new Queue<string>(MenuPages(app).SelectMany(group => group).Select(p => $"pages/{p}.js").Prepend("upload.js").Prepend("app.js"));
var seen = new Dictionary<string, string>(StringComparer.Ordinal);
while (pending.TryDequeue(out var module))
{
if (seen.ContainsKey(module)) continue;
using var response = await server.Client.GetAsync("/admin/" + module);
Assert.Equal(HttpStatusCode.OK, response.StatusCode, module);
Assert.Equal("text/javascript", response.Content.Headers.ContentType?.MediaType, module);
var source = await response.Content.ReadAsStringAsync();
seen[module] = source;
var folder = module.Contains('/', StringComparison.Ordinal) ? module[..(module.LastIndexOf('/') + 1)] : string.Empty;
foreach (Match import in ModuleReference().Matches(source))
pending.Enqueue(Resolve(folder, import.Groups[1].Value));
}
return seen;
}
[GeneratedRegex(@"from ""(\.{1,2}/[^""]+)""")]
private static partial Regex StaticImport();
/// <summary>A module's static imports and the module workers it starts (<c>new URL("./…", import.meta.url)</c>).</summary>
[GeneratedRegex(@"(?:from |new URL\()""(\.{1,2}/[^""]+)""")]
private static partial Regex ModuleReference();
[Test]
public async Task TheUploadDialogsHashWorkerRunsUnderThePanelPolicy()
{
await using var server = await TestServer.StartAsync(context);
using var page = await server.Client.GetAsync("/admin/");
// CSP 3: a worker is allowed by worker-src, else child-src, else script-src, else default-src.
var policy = Directives(page);
var workers = new[] { "worker-src", "child-src", "script-src", "default-src" }.Select(d => policy.GetValueOrDefault(d)).First(v => v is not null);
Assert.SequenceEqual(["'self'"], Assert.NotNull(workers));
using var worker = await server.Client.GetAsync("/admin/hash-worker.js");
Assert.Equal(HttpStatusCode.OK, worker.StatusCode);
Assert.Equal("text/javascript", worker.Content.Headers.ContentType?.MediaType);
Assert.Equal("nosniff", worker.Headers.GetValues("X-Content-Type-Options").First());
// The worker runs under its own response's policy, which lets it import the hash from the same origin.
Assert.SequenceEqual(["'self'"], Assert.NotNull(Directives(worker).GetValueOrDefault("script-src")));
Assert.Contains(@"from ""./sha256.js""", await worker.Content.ReadAsStringAsync());
var upload = await server.Client.GetStringAsync("/admin/upload.js");
Assert.Contains(@"new Worker(new URL(""./hash-worker.js"", import.meta.url), { type: ""module"" })", upload);
Assert.Contains("api.putBytes(", upload);
// No folder picking on the web: importing a whole folder is a desktop feature (owner review 2, R9).
foreach (var picker in new[] { "webkitdirectory", "showDirectoryPicker", "mozdirectory" })
Assert.True(!upload.Contains(picker, StringComparison.OrdinalIgnoreCase), picker);
// The panel has no inline handlers for the policy to block (and none to allow).
Assert.True(!Regex.IsMatch(upload, @"\son[a-z]+="""), "inline handler");
}
/// <summary>The directives of a response's Content-Security-Policy, each with its sources.</summary>
private static Dictionary<string, string[]> Directives(HttpResponseMessage response) =>
string.Join(';', response.Headers.GetValues("Content-Security-Policy"))
.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Select(d => d.Split(' ', StringSplitOptions.RemoveEmptyEntries))
.ToDictionary(d => d[0], d => d[1..], StringComparer.OrdinalIgnoreCase);
[Test]
public async Task AdminPanelDocksSettingsAtTheFootAndFoldsAboutIntoIt()
{
// Owner review 2: Settings below the other pages, About its last group; old #/about bookmarks open it there.
await using var server = await TestServer.StartAsync(context);
var app = await server.Client.GetStringAsync("/admin/app.js");
var groups = MenuPages(app);
Assert.Count(2, groups);
Assert.SequenceEqual(new[] { "settings" }, groups[1]);
Assert.DoesNotContain("about", groups[0]);
Assert.Contains(@"about: ""settings?group=about""", app);
using var about = await server.Client.GetAsync("/admin/pages/about.js");
Assert.Equal(HttpStatusCode.NotFound, about.StatusCode);
var settings = await server.Client.GetStringAsync("/admin/pages/settings.js");
Assert.Contains(@"id=""about-title""", settings);
// The page draws before the dashboard aggregate answers; these two rows fill in afterwards.
Assert.Contains(@"id=""about-database""", settings);
Assert.Contains(@"id=""about-uptime""", settings);
}
/// <summary>The menu's page groups as app.js lists them: the top pages, then the ones docked at the foot.</summary>
private static List<string[]> MenuPages(string app) =>
[.. Regex.Matches(app, @"const (?:top|bottom)Pages = \[([^\]]*)\]")
.Select(m => m.Groups[1].Value.Split(',').Select(p => p.Trim().Trim('"')).ToArray())];
private static string Resolve(string folder, string relative)
{
var parts = (folder + relative).Split('/').ToList();
for (var i = 0; i < parts.Count; i++)
{
if (parts[i] == ".") parts.RemoveAt(i--);
else if (parts[i] == ".." && i > 0) parts.RemoveRange(--i, 2);
}
return string.Join('/', parts.Where(p => p.Length > 0));
}
[Test]
public async Task I18nEndpointFallsBackToEnglish()
{
await using var server = await TestServer.StartAsync(context);
var it = await server.Client.GetFromJsonAsync("/api/v1/i18n/it-IT", ProtocolJson.Default.DictionaryStringString);
Assert.Equal("Accedi", Assert.NotNull(it)["Admin.Login.SignIn"]);
var unknown = await server.Client.GetFromJsonAsync("/api/v1/i18n/xx", ProtocolJson.Default.DictionaryStringString);
Assert.Equal("Sign in", Assert.NotNull(unknown)["Admin.Login.SignIn"]);
}
[Test]
public async Task I18nEndpointMarksRightToLeftLanguages()
{
await using var server = await TestServer.StartAsync(context);
foreach (var (culture, rtl) in new[] { ("ar", "true"), ("he", "true"), ("it", "false"), ("en", "false") })
{
var strings = Assert.NotNull(await server.Client.GetFromJsonAsync($"/api/v1/i18n/{culture}", ProtocolJson.Default.DictionaryStringString));
Assert.Equal((culture, rtl), (strings["$culture"], strings["$rtl"]));
// Count strings reach the panel in their plural forms.
Assert.True(strings.ContainsKey("Admin.Users.Accounts.one") && strings.ContainsKey("Admin.Users.Accounts.other"), culture);
}
}
[Test]
public void LinuxRefusesSqlite()
{
if (OperatingSystem.IsWindows()) TestContext.Skip("SQLite is allowed on Windows");
var options = new Chronos.Server.Configuration.ServerOptions { Database = Chronos.Server.Configuration.DatabaseKind.Sqlite };
Assert.Throws<InvalidOperationException>(options.Validate);
}
[Test]
public void EnvironmentOverridesDefaults()
{
var root = context.CreateTempDirectory("env");
var env = new Dictionary<string, string?>
{
["CHRONOS_DATA_PATH"] = root,
["CHRONOS_HTTP_PORT"] = "9090",
["CHRONOS_TLS_MODE"] = "proxy",
["CHRONOS_DB"] = "postgres",
["CHRONOS_SERVER_NAME"] = "Studio NAS",
};
var options = Chronos.Server.Configuration.ServerOptions.Load(env);
Assert.Equal(9090, options.HttpPort);
Assert.Equal(Chronos.Server.Configuration.TlsMode.Proxy, options.TlsMode);
Assert.Equal("Studio NAS", options.ServerName);
}
}