diff --git a/src/Chronos.Data/Archiving/SequenceWriter.cs b/src/Chronos.Data/Archiving/SequenceWriter.cs index f93e3ff7..042066fb 100644 --- a/src/Chronos.Data/Archiving/SequenceWriter.cs +++ b/src/Chronos.Data/Archiving/SequenceWriter.cs @@ -26,6 +26,16 @@ public sealed record ArchiveServiceOptions /// public bool ImagesAfterScan { get; init; } + /// + /// The index may hold settings newer than a sequence's project.json: the server, whose PATCH wrote the + /// settings a client sent into the index alone before owner review 2 phase 5b. When the projects meet a sequence whose two + /// copies differ, the index's is never dropped: Main is born with project.json's as version 1 and the index's as + /// version 2, in force; a Main the sidecars already hold keeps the index's as a version below the settings in force + /// (). The desktop, whose every writer writes the file, takes the file alone: its index + /// can be an old copy (a second PC, a backup). + /// + public bool IndexSettingsMayLead { get; init; } + /// /// Photos a pass works on at once, asked at the start of each sequence (owner review 1, R2: one job runs at a time and /// the host's parallelism decides how many photos it processes at once; Settings › Rendering › Parallelism on the diff --git a/src/Chronos.Data/Projects/ProjectService.cs b/src/Chronos.Data/Projects/ProjectService.cs index bcd173be..39603fa2 100644 --- a/src/Chronos.Data/Projects/ProjectService.cs +++ b/src/Chronos.Data/Projects/ProjectService.cs @@ -799,9 +799,15 @@ public sealed class ProjectService await _db.Projects.SetMirroredAsync(main.Id, head.Id, context.Row.SettingsVersion, cancellationToken).ConfigureAwait(false); return; } + if (LeadingCopy(context, shared, CurrentPortable(context, photos), photos) is { } copy && !ProjectContent.SameChoices(head.SettingsJson, copy, photos) + && !await MirroredBeforeAsync(main, head, null, copy, photos, cancellationToken).ConfigureAwait(false)) + { + await KeepCopyAsync(context, main, head, shared, copy, photos, cancellationToken).ConfigureAwait(false); + return; + } if (ProjectContent.SameChoices(head.SettingsJson, shared, photos)) { - if (CurrentPortable(context, photos) is { } copy && ProjectContent.SameChoices(head.SettingsJson, copy, photos)) + if (CurrentPortable(context, photos) is { } current && ProjectContent.SameChoices(head.SettingsJson, current, photos)) await MarkMirroredAsync(context, main, head, photos, cancellationToken).ConfigureAwait(false); else await MirrorAsync(context, main, head, cancellationToken).ConfigureAwait(false); @@ -826,6 +832,58 @@ public sealed class ProjectService await MirrorAsync(context, main, external, cancellationToken).ConfigureAwait(false); } + /// + /// Main met with no mirror known on a host whose index may hold newer settings than project.json + /// (), with this index's copy () none of + /// Main's versions: which of the two is newer cannot be told (an earlier server's PATCH wrote the index alone, an + /// index taken back from a backup is old), so both are kept. The copy becomes an External version on the head, and on + /// it the settings the meeting puts in force as it does elsewhere, from project.json: the head (a mirror cut + /// short, brought back as a Restore), a merge with the versions no mirror wrote, or project.json's as External. + /// That is mirrored. + /// + private async Task KeepCopyAsync(LeaseContext context, SequenceProject main, ProjectVersion head, string shared, string copy, FramePhotos photos, + CancellationToken cancellationToken) + { + var recorded = await RecordedMirrorAsync(context, main, head, cancellationToken).ConfigureAwait(false); + var headMirrored = context.Reconcile.Live.TryGetValue(main.Id, out var record) && record.Mirrored?.Id == head.Id; + var (inForce, kind) = ProjectContent.SameChoices(head.SettingsJson, shared, photos) + || (!headMirrored && await MirroredBeforeAsync(main, head, recorded, shared, photos, cancellationToken).ConfigureAwait(false)) + ? (head.SettingsJson, ProjectVersionKind.Restore) + : recorded is not null && Merged(recorded, head, shared, photos) is { } merged + ? (merged, ProjectVersionKind.Merge) + : (shared, ProjectVersionKind.External); + Log.Warning("projects", $"The settings of sequence {context.Row.Id} in the index differ from its project.json and from Main's versions: " + + $"they are kept as version {head.Number + 1} of Main, and the settings project.json gives on top of it"); + var kept = await AppendDerivedAsync(context, main, head, copy, ProjectVersionKind.External, cancellationToken).ConfigureAwait(false); + main = (await _db.Projects.GetAsync(main.Id, cancellationToken).ConfigureAwait(false))!; + var top = kept; + if (!ProjectContent.SameChoices(copy, inForce, photos)) + { + var restoredFrom = kind == ProjectVersionKind.Restore ? head.Id : (Guid?)null; + top = NewVersion(main.Id, kept.Id, main.HeadNumber + 1, kind, inForce, context.Row.FramesVersion, DateTimeOffset.UtcNow) with { RestoredFrom = restoredFrom }; + main = await AppendAsync(context.Folder, main, top, cancellationToken).ConfigureAwait(false); + } + // project.json's settings are taken in (kept or in force): the mirror writes over them. + await MirrorAsync(context, main, top, cancellationToken, shared).ConfigureAwait(false); + } + + /// + /// merged with over as + /// merges them ( wins where both changed); null when either is of a newer Chronos. + /// + private static string? Merged(ProjectVersion basis, ProjectVersion head, string theirs, FramePhotos photos) + { + try + { + return ProjectContent.Canonical(ProjectMerge.MergeWithConflicts(ProjectMerge.Read(ProjectContent.Rebind(basis.SettingsJson, photos)), + ProjectMerge.Read(ProjectContent.Rebind(theirs, photos)), ProjectMerge.Read(ProjectContent.Rebind(head.SettingsJson, photos))).Merged.ToJson()); + } + catch (NewerSettingsException) + { + return null; + } + } + /// /// The version Main's record names as mirrored last, when it is one the head descends from (not the head itself) and of /// a format this Chronos reads; null otherwise (an older record names none). @@ -998,9 +1056,11 @@ public sealed class ProjectService /// /// The Main project of a sequence that has none: version 1 from the sequence's settings as its project.json holds - /// them (this index's copy when there is none, else defaults), this index's copy brought up to them. With the archive - /// read-only it is made in the index only, and nothing is written to the archive: its sidecars are written once the - /// archive takes writes (). + /// them (this index's copy when there is none, else defaults), this index's copy brought up to them. On a host whose index + /// may hold newer settings than the file (: the server) and when + /// the two differ, the index's copy is version 2 (External) on top, in force and mirrored: neither is lost. With the + /// archive read-only it is made in the index only, and nothing is written to the archive: its sidecars are written once + /// the archive takes writes (). /// private async Task MakeMainAsync(LeaseContext context, bool writable, CancellationToken cancellationToken) { @@ -1019,20 +1079,39 @@ public sealed class ProjectService UpdatedAt = now, }; var first = NewVersion(project.Id, null, 1, ProjectVersionKind.Imported, portable, context.Row.FramesVersion, now); + var versions = new List { first }; + if (LeadingCopy(context, shared, copy, frames.Photos) is { } leading) + { + Log.Warning("projects", $"The settings of sequence {context.Row.Id} in the index differ from its project.json: Main is made with project.json's as " + + "version 1 and the index's as version 2, in force"); + versions.Add(NewVersion(project.Id, first.Id, 2, ProjectVersionKind.External, leading, context.Row.FramesVersion, now)); + } // Settings this Chronos cannot read (of a newer one) are never written over: the index's copy then stays as it is too. - var inStep = shared is null || copy is null || ProjectContent.SameChoices(portable, copy, frames.Photos); + var inStep = versions.Count == 1 && (shared is null || copy is null || ProjectContent.SameChoices(portable, copy, frames.Photos)); if (inStep) project = project with { MirroredSettingsVersion = context.Row.SettingsVersion, MirroredVersionId = first.Id }; if (!writable) { - await AddProjectAsync(null, project, first, null, cancellationToken).ConfigureAwait(false); + await AddProjectAsync(null, project, versions, null, cancellationToken).ConfigureAwait(false); return; } var onDisk = shared is not null && File.Exists(context.Folder.ProjectPath); - var made = await AddProjectAsync(context.Folder, project, first, onDisk ? new ProjectHead(first.Id, first.Number) : null, cancellationToken) + var made = await AddProjectAsync(context.Folder, project, versions, onDisk ? new ProjectHead(first.Id, first.Number) : null, cancellationToken) .ConfigureAwait(false); - if (!inStep) await MirrorAsync(context, made, first, cancellationToken).ConfigureAwait(false); + // project.json holds version 1: what the mirror of version 2 writes over is taken in already. + if (!inStep) await MirrorAsync(context, made, versions[^1], cancellationToken, versions.Count > 1 ? shared : null).ConfigureAwait(false); } + /// + /// This index's copy of the sequence's settings when it differs from its project.json on a host whose index may hold + /// the newer ones (); null otherwise (the desktop, the two the same, + /// either unreadable, or no file: then is the copy). + /// + private string? LeadingCopy(LeaseContext context, string? shared, string? copy, FramePhotos photos) => + _writer.Options.IndexSettingsMayLead && shared is not null && copy is not null && File.Exists(context.Folder.ProjectPath) + && !ProjectContent.SameChoices(shared, copy, photos) + ? copy + : null; + /// "Main", or "Main (2)"… when a project of that name exists already (one made before the sequence had its Main). private async Task FreeMainNameAsync(Guid sequenceId, CancellationToken cancellationToken) { @@ -1102,18 +1181,24 @@ public sealed class ProjectService /// A new project with its first version: sidecars (version, then the record, naming ), then /// both rows in one transaction; the rows only when is null (a read-only archive). /// - private async Task AddProjectAsync(SequencePaths? folder, SequenceProject project, ProjectVersion first, ProjectHead? mirrored, + private Task AddProjectAsync(SequencePaths? folder, SequenceProject project, ProjectVersion first, ProjectHead? mirrored, + CancellationToken cancellationToken) => + AddProjectAsync(folder, project, [first], mirrored, cancellationToken); + + /// A new project with its first versions, in order (each the next one's parent): the last is its head. + private async Task AddProjectAsync(SequencePaths? folder, SequenceProject project, IReadOnlyList versions, ProjectHead? mirrored, CancellationToken cancellationToken) { - var withHead = project with { HeadVersionId = first.Id, HeadNumber = first.Number }; + var head = versions[^1]; + var withHead = project with { HeadVersionId = head.Id, HeadNumber = head.Number }; if (folder is not null) { - ProjectSidecars.WriteVersion(folder.VersionPath(project.Id, first.Number, first.Id), first); + foreach (var version in versions) ProjectSidecars.WriteVersion(folder.VersionPath(project.Id, version.Number, version.Id), version); ProjectSidecars.WriteRecord(folder.ProjectRecordPath(project.Id), ProjectRecord.Of(withHead, mirrored)); } await using var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false); await tx.Projects.InsertAsync(withHead with { HeadVersionId = null, HeadNumber = 0 }, cancellationToken).ConfigureAwait(false); - await tx.Projects.InsertVersionAsync(first, cancellationToken).ConfigureAwait(false); + foreach (var version in versions) await tx.Projects.InsertVersionAsync(version, cancellationToken).ConfigureAwait(false); await tx.Projects.UpdateAsync(withHead, cancellationToken).ConfigureAwait(false); await tx.CommitAsync(cancellationToken).ConfigureAwait(false); return withHead; diff --git a/src/Chronos.Server/Api/ArchiveEndpoints.cs b/src/Chronos.Server/Api/ArchiveEndpoints.cs index 425e706d..54592867 100644 --- a/src/Chronos.Server/Api/ArchiveEndpoints.cs +++ b/src/Chronos.Server/Api/ArchiveEndpoints.cs @@ -98,7 +98,7 @@ public static class ArchiveEndpoints return InvalidTemplate(context, problem); if (!ProxyPolicy.IsValidLongEdge(request.ProxyLongEdge)) return ApiErrors.BadRequest(context, "bad_request", "proxyLongEdge"); - var engine = archives ?? new ArchiveService(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner }); + var engine = archives ?? new ArchiveService(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner, IndexSettingsMayLead = true }); Archive updated; try { diff --git a/src/Chronos.Server/Api/FileEndpoints.cs b/src/Chronos.Server/Api/FileEndpoints.cs index f6eed718..00e28ea9 100644 --- a/src/Chronos.Server/Api/FileEndpoints.cs +++ b/src/Chronos.Server/Api/FileEndpoints.cs @@ -80,7 +80,7 @@ public static class FileEndpoints using var body = new MemoryStream(); await context.Request.Body.CopyToAsync(body, ct); - var images = (archives ?? new ArchiveService(db, new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner })).Images; + var images = (archives ?? new ArchiveService(db, new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner, IndexSettingsMayLead = true })).Images; ProxyStoreResult result; try { diff --git a/src/Chronos.Server/Api/LibraryEndpoints.cs b/src/Chronos.Server/Api/LibraryEndpoints.cs index b51b9592..f4af700c 100644 --- a/src/Chronos.Server/Api/LibraryEndpoints.cs +++ b/src/Chronos.Server/Api/LibraryEndpoints.cs @@ -56,7 +56,13 @@ public static class LibraryEndpoints /// The archive engine behind these operations: the host's, or one of its own (a test host that replaces the archive module). internal static ArchiveService Engine(ArchiveService? archives, ChronosDatabase db) => - archives ?? new ArchiveService(db, new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner, ImagesAfterScan = true }); + archives ?? new ArchiveService(db, new ArchiveServiceOptions + { + Decoder = DecoderFactory.CreateServer(), + Owner = ArchiveModule.Owner, + ImagesAfterScan = true, + IndexSettingsMayLead = true + }); /// /// What the Library's filters offer (the panel's Year, Camera and Tag menus): the years of first capture, the camera diff --git a/src/Chronos.Server/Api/SequenceEndpoints.cs b/src/Chronos.Server/Api/SequenceEndpoints.cs index e85a2973..3fb2f2c0 100644 --- a/src/Chronos.Server/Api/SequenceEndpoints.cs +++ b/src/Chronos.Server/Api/SequenceEndpoints.cs @@ -228,7 +228,7 @@ public static class SequenceEndpoints var ct = context.RequestAborted; if (await db.Sequences.GetAsync(id, ct) is not { } sequence) return ApiErrors.NotFound(context, "sequence"); if (!caller.IsAdmin && sequence.OwnerUserId != caller.Id) return ApiErrors.Forbidden(context); - var trash = archives?.Trash ?? new SequenceTrash(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner }); + var trash = archives?.Trash ?? new SequenceTrash(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner, IndexSettingsMayLead = true }); DeletePlan plan; try { diff --git a/src/Chronos.Server/Storage/ArchiveModule.cs b/src/Chronos.Server/Storage/ArchiveModule.cs index c7e8e180..d6ca1922 100644 --- a/src/Chronos.Server/Storage/ArchiveModule.cs +++ b/src/Chronos.Server/Storage/ArchiveModule.cs @@ -64,6 +64,8 @@ public static class ArchiveModule FreeSpace = volumes is null ? null : volumes.WritableFree, Owner = Owner, ImagesAfterScan = true, + // An earlier release's PATCH wrote settings into the index alone: they are never reverted to project.json's. + IndexSettingsMayLead = true, Parallelism = () => options.Parallelism, CleanupSteps = [.. sp.GetServices()], // Next to the index in the data folder, where the daily backups always went. diff --git a/src/Chronos.Server/Storage/ProcessingModule.cs b/src/Chronos.Server/Storage/ProcessingModule.cs index b1d6c424..5314e46b 100644 --- a/src/Chronos.Server/Storage/ProcessingModule.cs +++ b/src/Chronos.Server/Storage/ProcessingModule.cs @@ -33,7 +33,7 @@ public static class ProcessingModule var options = sp.GetRequiredService(); return new ProcessingService(db, new ProcessingOptions { - Archive = new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner, Parallelism = () => options.Parallelism }, + Archive = new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner, IndexSettingsMayLead = true, Parallelism = () => options.Parallelism }, // Analyses and renders take the parallelism as it is when they start, and the one job that runs has the host's // memory budget for its frame window, as on the desktop (owner review 1). Tune = basis => diff --git a/src/Chronos.Server/Storage/RenderStore.cs b/src/Chronos.Server/Storage/RenderStore.cs index b2cfb67f..8bce7b1d 100644 --- a/src/Chronos.Server/Storage/RenderStore.cs +++ b/src/Chronos.Server/Storage/RenderStore.cs @@ -12,7 +12,7 @@ namespace Chronos.Server.Storage; /// The archive engine whose sequence lease the filing takes; one of its own when none is registered. public sealed class RenderStore(ChronosDatabase db, ServerArchive archive, TimeProvider time, ArchiveService? archives = null) { - private readonly ArchiveService _archives = archives ?? new ArchiveService(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner }); + private readonly ArchiveService _archives = archives ?? new ArchiveService(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner, IndexSettingsMayLead = true }); /// /// Moves (on the archive volume) to <sequence folder>/exports/, never diff --git a/tests/Chronos.Data.Tests/Projects/ProjectRecoveryTests.cs b/tests/Chronos.Data.Tests/Projects/ProjectRecoveryTests.cs index f0da3458..74acf18b 100644 --- a/tests/Chronos.Data.Tests/Projects/ProjectRecoveryTests.cs +++ b/tests/Chronos.Data.Tests/Projects/ProjectRecoveryTests.cs @@ -177,6 +177,98 @@ public abstract class ProjectRecoveryTests(TestContext context) : DatabaseTest(c Assert.SequenceEqual([0], Settings(fx, sequence).ExcludedFrames); } + /// The settings of an index that may hold newer ones than project.json, as the server's does (an earlier release's PATCH wrote the index alone). + private async Task ServerFixtureAsync() => + Own(await CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true), + configure: _ => new ArchiveServiceOptions { Owner = "server", LeaseWait = TimeSpan.FromSeconds(20), IndexSettingsMayLead = true })); + + private static ProjectSettings Exposure(double ev) => new() { Exposure = new ExposureSettings { ExposureOffsetEv = ev } }; + + private static double Ev(string json) => ProjectSettings.FromJson(json).Exposure.ExposureOffsetEv; + + [Test] + public async Task OnThisPcMainIsBornFromProjectJsonAloneWhenTheIndexDiffers() + { + var fx = await FixtureAsync(); + var (sequence, _) = await UploadedAsync(fx, 3); + var folder = fx.Paths.Sequence(sequence.Folder); + Directory.CreateDirectory(folder.MetaDirectory); + File.WriteAllText(folder.ProjectPath, Exposure(0.2).ToJson()); + await WriteSettingsAsync(fx, sequence.Id, Exposure(0.7)); + + // The desktop's every writer writes the file: an index that differs is an old copy, brought up to it. + var main = await fx.Service.Projects.MainAsync(sequence.Id, fx.Ct); + var history = await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct); + Assert.Equal((1, ProjectVersionKind.Imported), (history.Single().Number, history.Single().Kind)); + Assert.Equal(0.2, Ev(history.Single().SettingsJson)); + Assert.Equal(0.2, Ev((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!)); + Assert.Equal(0.2, Settings(fx, sequence).Exposure.ExposureOffsetEv); + } + + [Test] + public async Task OnAServerMainIsBornWithTheIndexsNewerSettingsInForceAndProjectJsonsKept() + { + foreach (var readOnly in new[] { false, true }) + { + var fx = await ServerFixtureAsync(); + var (sequence, _) = await UploadedAsync(fx, 3); + var folder = fx.Paths.Sequence(sequence.Folder); + Directory.CreateDirectory(folder.MetaDirectory); + File.WriteAllText(folder.ProjectPath, Exposure(0.2).ToJson()); + // A settings PATCH of an earlier release: the index alone. + await WriteSettingsAsync(fx, sequence.Id, Exposure(0.7)); + + var projects = fx.Service.Projects; + if (readOnly) + { + // On a read-only archive Main is made in the index only, both versions in it, nothing written; its sidecars + // and the mirror follow once the archive takes writes. + projects.Availability = _ => ArchiveAvailability.ReadOnly; + Assert.Equal(2, (await projects.ListAsync(sequence.Id, fx.Ct)).Single().HeadNumber); + Assert.False(Directory.Exists(folder.ProjectsDirectory)); + Assert.Equal(0.2, Settings(fx, sequence).Exposure.ExposureOffsetEv); + projects.Availability = ArchiveVolume.Probe; + } + var main = await projects.MainAsync(sequence.Id, fx.Ct); + var history = await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct); + Assert.SequenceEqual([(2, ProjectVersionKind.External), (1, ProjectVersionKind.Imported)], history.Select(v => (v.Number, v.Kind))); + Assert.Equal((0.7, 0.2), (Ev(history[0].SettingsJson), Ev(history[1].SettingsJson))); + Assert.Equal(0.7, Ev((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!)); + Assert.Equal(0.7, Settings(fx, sequence).Exposure.ExposureOffsetEv); + // In step now: read again, nothing is added. + await projects.ListAsync(sequence.Id, fx.Ct); + Assert.Count(2, await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)); + Assert.Equal(main.Id, ProjectFolders(folder).Select(d => Guid.ParseExact(Path.GetFileName(d), "N")).Single()); + } + } + + [Test] + public async Task OnAServerAnIndexMeetingMainKeepsItsOwnSettingsAsAVersion() + { + var fx = await ServerFixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 3); + var projects = fx.Service.Projects; + var main = await projects.MainAsync(sequence.Id, fx.Ct); + var pushed = (await projects.PushAsync(main.Id, Draft(await projects.HeadAsync(main.Id, fx.Ct), Exposure(0.4), frames), fx.Ct)).Version!; + + // The index meets Main with no mirror known and its own settings, which no version holds: which is newer cannot be + // told, so both are kept, the index's as a version and project.json's (the pushed head) in force on top of it. + var row = await WriteSettingsAsync(fx, sequence.Id, Exposure(0.7)); + await fx.Db.Projects.SetMirroredAsync(main.Id, null, row.SettingsVersion, fx.Ct); + var history = await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct); + Assert.SequenceEqual([(4, ProjectVersionKind.Restore), (3, ProjectVersionKind.External), (2, ProjectVersionKind.Commit)], + history.Take(3).Select(v => (v.Number, v.Kind))); + Assert.Equal((0.4, 0.7, pushed.Id), (Ev(history[0].SettingsJson), Ev(history[1].SettingsJson), history[0].RestoredFrom)); + Assert.Equal(0.4, Ev((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!)); + Assert.Equal(0.4, Settings(fx, sequence).Exposure.ExposureOffsetEv); + + // An index copy a version holds already (a backup's) adds nothing: the head is mirrored. + row = await WriteSettingsAsync(fx, sequence.Id, Exposure(0.7)); + await fx.Db.Projects.SetMirroredAsync(main.Id, null, row.SettingsVersion, fx.Ct); + Assert.Count(4, await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)); + Assert.Equal(0.4, Ev((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!)); + } + [Test] public async Task AMirrorACrashCutShortBeforeProjectJsonIsCompletedAfterARebuild() { diff --git a/tests/Chronos.Server.Tests/Api/ProjectTests.cs b/tests/Chronos.Server.Tests/Api/ProjectTests.cs index 7cc6ec94..67648ae8 100644 --- a/tests/Chronos.Server.Tests/Api/ProjectTests.cs +++ b/tests/Chronos.Server.Tests/Api/ProjectTests.cs @@ -9,6 +9,7 @@ using Chronos.Core.Project; using Chronos.Data; using Chronos.Data.Archiving; using Chronos.Data.Projects; +using Chronos.Data.Repositories; using Chronos.Protocol; using Chronos.Protocol.Client; using Chronos.Server.Api; @@ -430,6 +431,37 @@ public sealed class ProjectTests(TestContext context) Assert.Equal(0.7, ProjectSettings.FromJson(File.ReadAllText(folder.ProjectPath)).Exposure.ExposureOffsetEv); } + [Test] + public async Task ASequencePatchedBeforeTheServerKeptProjectsKeepsItsSettingsWhenMainIsBorn() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (_, _, token) = await server.UserAsync("alberto", "editor"); + using var client = Client(server, token); + var sid = await SequenceAsync(server, token, 2); + var row = (await server.Database.Sequences.GetAsync(sid))!; + var folder = new ArchivePaths((await server.Database.Archives.ListAsync()).Single().RootPath).Sequence(row.Folder); + Directory.CreateDirectory(Path.GetDirectoryName(folder.ProjectPath)!); + File.WriteAllText(folder.ProjectPath, new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 0.2 } }.ToJson()); + // An earlier release's PATCH (the desktop's sync of a linked copy) wrote the index alone, after project.json. + var patched = new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 0.7 } }.ToJson(); + Assert.Equal(SequenceEditOutcome.Updated, + (await server.Database.Sequences.EditAsync(sid, new SequenceEdit { SettingsJson = patched }, row.SettingsVersion, context.Cancellation)).Outcome); + + // Upgraded, the first list makes Main: the index's settings stay in force, project.json's are version 1. + var main = (await client.ListProjectsAsync(sid, context.Cancellation)).Single(); + Assert.Equal((2, "external"), (main.HeadNumber, main.HeadKind)); + var versions = (await client.ListProjectVersionsAsync(sid, main.Id, ct: context.Cancellation)).Items; + Assert.SequenceEqual([2, 1], versions.Select(v => v.Number)); + Assert.Equal(0.7, ProjectSettings.FromJson((await client.GetProjectHeadAsync(sid, main.Id, context.Cancellation)).SettingsJson).Exposure.ExposureOffsetEv); + var first = Assert.NotNull(await client.GetProjectVersionAsync(sid, main.Id, versions[1].Id, context.Cancellation)); + Assert.Equal("imported", first.Kind); + Assert.Equal(0.2, ProjectSettings.FromJson(first.SettingsJson).Exposure.ExposureOffsetEv); + Assert.Equal(0.7, ProjectSettings.FromJson((await server.Database.Sequences.GetAsync(sid))!.SettingsJson!).Exposure.ExposureOffsetEv); + Assert.Equal(0.7, ProjectSettings.FromJson(File.ReadAllText(folder.ProjectPath)).Exposure.ExposureOffsetEv); + // Read again, nothing moves. + Assert.Equal(2, (await client.ListProjectsAsync(sid, context.Cancellation)).Single().HeadNumber); + } + [Test] public async Task AServerHomeHoldsOnlyWhatItsFilesHold() {