The sidecars stay the truth of a sequence's projects in the cases round 1's checkers found still open, and no version or setting is lost on the way: - An index-only Main meeting a Main another index made is written back as another project, and Main passes to theirs in one transaction through a free temporary name, so ux_projects_name and ux_projects_main never fail (the SqliteException used to escape ListAsync and leave the index and the sidecars disagreeing). A refusal is logged; the next reconcile settles it. - The reconcile takes Main from the records, in step with the index, when another index passed it on: exactly one Main per sequence, names from the records where free. - A project folder with versions and no record, of a project no index row names (a creation cut before its record), no longer holds the sequence back forever: it is left as it is and indexed once its record arrives. - A record behind the index (a projects folder put back from an older backup) is made whole from the index: its missing versions and the record are written again, at every catch-up and before any append, rename or delete. Only a record ahead of the index or naming another version is refused, with a message that says so. - An index meeting Main walks the chain down to the version the record names as mirrored (a bound, since that record write may lag project.json), so a lagging record never makes an External that reverts a newer push. When the record names a mirrored ancestor, settings written since merge with the unmirrored versions, as for an index that knows its mirror. - The catch-up merge's External + Merge pair is resumable: an External head whose parent is a push no mirror wrote is finished into its Merge version next time, by this index or one rebuilt from the sidecars. - A project deleted at the home has no head there, so a workspace copy of it reads Gone, never In sync; Remove local copy without discard says the project was deleted in the library. - DatabaseExceptions are contained per sequence in the archive scan's project reconcile and in ProjectService reads. Tests: SQLite (PostgreSQL-ready) for each scenario, including a cut between the two catch-up versions, a 40,000-exclusion version through save, push and rebuild, and the App's Remove local copy of a project deleted at home. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
249 lines
15 KiB
C#
249 lines
15 KiB
C#
using System.Globalization;
|
|
using System.Text.Json.Nodes;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Core.Project;
|
|
using Chronos.Data.Archiving;
|
|
using Chronos.Data.Projects;
|
|
using Chronos.Data.Tests.Archiving;
|
|
using Chronos.Data.Tests.Infrastructure;
|
|
using Chronos.Data.Tests.Workspaces;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Data.Tests.Projects;
|
|
|
|
/// <summary>The merge of projects (owner review 2, R8): one photo under two names, and settings of a newer Chronos.</summary>
|
|
public sealed class ProjectMergeTests
|
|
{
|
|
private static readonly DateTime Start = new(2026, 9, 28, 18, 0, 0);
|
|
|
|
private static FramePhoto Photo(int n) => new(n.ToString("x64", CultureInfo.InvariantCulture), 1000 + n, Start.AddSeconds(10 * n));
|
|
|
|
private static PendingFrame Waiting(FramePhoto photo, bool excluded = false, bool hero = false) =>
|
|
new() { Sha256 = photo.Sha256, Size = photo.Size, CapturedAt = photo.CapturedAt, Excluded = excluded, Hero = hero };
|
|
|
|
[Test]
|
|
public void EntriesOfOnePhotoMergeAsOneWithThePositionsOfBoth()
|
|
{
|
|
var frames = new FramePhotos([(0, Photo(1)), (1, Photo(2)), (2, Photo(3))]);
|
|
var basis = new ProjectSettings();
|
|
// A version stored with an exclusion and the hero of one photo in two entries (renamed alike since they were saved).
|
|
var ours = new ProjectSettings { PendingFrames = [Waiting(Photo(2), excluded: true), Waiting(Photo(2), hero: true)] };
|
|
var theirs = basis with { Stack = new StackSettings { Mode = StackSetting.Median } };
|
|
|
|
var merged = ProjectMerge.MergeWithConflicts(basis, ours, theirs);
|
|
Assert.Empty(merged.Conflicts);
|
|
var entry = Assert.NotNull(merged.Merged.PendingFrames).Single();
|
|
Assert.True(entry.Excluded && entry.Hero);
|
|
Assert.Equal(StackSetting.Median, merged.Merged.Stack.Mode);
|
|
var placed = ProjectSettings.FromJson(ProjectContent.Place(merged.Merged.ToJson(), frames));
|
|
Assert.SequenceEqual([1], placed.ExcludedFrames);
|
|
Assert.Equal((int?)1, placed.HeroFrame);
|
|
|
|
// The same through the merge's other side, and when the other side changed the photo's flags.
|
|
Assert.True(ProjectMerge.Merge(basis, theirs, ours).PendingFrames!.Single() is { Excluded: true, Hero: true });
|
|
var unexcluded = new ProjectSettings { PendingFrames = [Waiting(Photo(2), hero: true)] };
|
|
var both = ProjectMerge.MergeWithConflicts(ours, ours, unexcluded);
|
|
Assert.Empty(both.Conflicts);
|
|
Assert.True(both.Merged.PendingFrames!.Single() is { Excluded: false, Hero: true });
|
|
}
|
|
|
|
[Test]
|
|
public void APhotoNamedAlikeOnEverySideMergesAsOnePhoto()
|
|
{
|
|
// Saved here before the photo was hashed (by size and capture time) and stored at the home after (by SHA-256): once each
|
|
// side names it as the frames hold it now, undoing the exclusion here is a change of this side only.
|
|
var unhashed = new FramePhoto(null, Photo(2).Size, Photo(2).CapturedAt);
|
|
var frames = new FramePhotos([(0, Photo(1)), (1, Photo(2)), (2, Photo(3))]);
|
|
var basis = new ProjectSettings { PendingFrames = [Waiting(unhashed, excluded: true)] };
|
|
var theirs = new ProjectSettings { PendingFrames = [Waiting(Photo(2), excluded: true)], Exposure = new ExposureSettings { ExposureOffsetEv = 0.7 } };
|
|
var ours = new ProjectSettings();
|
|
|
|
ProjectSettings Named(ProjectSettings settings) => ProjectSettings.FromJson(ProjectContent.Rebind(ProjectContent.Canonical(settings.ToJson()), frames));
|
|
var merged = ProjectMerge.MergeWithConflicts(Named(basis), Named(ours), Named(theirs));
|
|
Assert.Empty(merged.Conflicts);
|
|
Assert.Null(merged.Merged.PendingFrames);
|
|
Assert.Equal(0.7, merged.Merged.Exposure.ExposureOffsetEv);
|
|
}
|
|
|
|
[Test]
|
|
public void SettingsOfANewerChronosAreNeverMergedWithThisOnes()
|
|
{
|
|
var basis = new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 0.1 } };
|
|
var newer = ProjectSettings.FromJson("""{ "version": 2, "exposure": { "exposureOffsetEv": 0.5 }, "grade": { "look": "film" } }""", allowNewer: true);
|
|
var mine = basis with { Stack = new StackSettings { Mode = StackSetting.Median } };
|
|
|
|
// Both changed: refused, whichever side the newer one is, in a replay too.
|
|
Assert.Throws<NewerSettingsException>(() => ProjectMerge.MergeWithConflicts(basis, mine, newer));
|
|
Assert.Throws<NewerSettingsException>(() => ProjectMerge.Merge(basis, newer, mine));
|
|
Assert.Throws<NewerSettingsException>(() => ProjectMerge.Replay(basis, [mine], newer));
|
|
Assert.Throws<NewerSettingsException>(() => new ProjectMerge.Rebase(basis, newer).Apply(mine));
|
|
|
|
// A side that did not change takes the other's as they are: nothing is merged.
|
|
var taken = ProjectMerge.MergeWithConflicts(basis, basis, newer);
|
|
Assert.Empty(taken.Conflicts);
|
|
Assert.True(taken.Merged.IsNewerFormat);
|
|
Assert.Equal(newer.ToJson(), taken.Merged.ToJson());
|
|
Assert.Equal(mine.ToJson(), ProjectMerge.Merge(newer, mine, newer).ToJson());
|
|
Assert.Equal(basis.ToJson(), ProjectMerge.Merge(newer, newer, basis).ToJson());
|
|
Assert.Equal(newer.ToJson(), ProjectMerge.Replay(basis, [basis], newer).Commits.Single().ToJson());
|
|
}
|
|
}
|
|
|
|
/// <summary>A newer Chronos sharing the archive (owner review 2, R8): what it writes when it stores a version.</summary>
|
|
internal static class NewerChronos
|
|
{
|
|
/// <summary>
|
|
/// A version of settings format <see cref="ProjectSettings.CurrentVersion"/> + 1 on top of <paramref name="head"/>, written
|
|
/// as that Chronos's push writes it: the version file, then the record's head (the index adopts it from the sidecars).
|
|
/// It holds a member this Chronos does not know (a grade) and an exposure offset of 1.5.
|
|
/// </summary>
|
|
public static ProjectVersion Push(ArchiveFixture fx, Sequence sequence, ProjectVersion head)
|
|
{
|
|
var folder = fx.Paths.Sequence(sequence.Folder);
|
|
var settings = JsonNode.Parse(head.SettingsJson)!.AsObject();
|
|
settings["version"] = ProjectSettings.CurrentVersion + 1;
|
|
settings["grade"] = new JsonObject { ["look"] = "film" };
|
|
settings["exposure"] = new JsonObject { ["exposureOffsetEv"] = 1.5 };
|
|
var text = ProjectContent.Canonical(settings.ToJsonString());
|
|
var version = new ProjectVersion
|
|
{
|
|
Id = Guid.CreateVersion7(),
|
|
ProjectId = head.ProjectId,
|
|
ParentId = head.Id,
|
|
Number = head.Number + 1,
|
|
Kind = ProjectVersionKind.Commit,
|
|
ClientName = "a newer Chronos",
|
|
SettingsJson = text,
|
|
ContentHash = ProjectContent.Hash(text),
|
|
SettingsFormat = ProjectSettings.CurrentVersion + 1,
|
|
FramesVersion = head.FramesVersion,
|
|
CreatedAt = DateTimeOffset.UtcNow,
|
|
StoredAt = DateTimeOffset.UtcNow,
|
|
};
|
|
ProjectSidecars.WriteVersion(folder.VersionPath(head.ProjectId, version.Number, version.Id), version);
|
|
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(head.ProjectId));
|
|
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(head.ProjectId), record with { Head = new ProjectHead(version.Id, version.Number), Updated = DateTimeOffset.UtcNow });
|
|
return version;
|
|
}
|
|
}
|
|
|
|
/// <summary>Main's catch-up merge (owner review 2, R8): a mirror cut short meets settings written meanwhile.</summary>
|
|
[Category("projects")]
|
|
public abstract class MainMergeTests(TestContext context) : DatabaseTest(context)
|
|
{
|
|
private async Task<ArchiveFixture> FixtureAsync() => Own(await ArchiveFixture.CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true)));
|
|
|
|
[Test]
|
|
public async Task TheValuesTheMergeLeavesOutStayInMainsHistory()
|
|
{
|
|
var fx = await FixtureAsync();
|
|
var (sequence, frames) = await ProjectServiceTests.UploadedAsync(fx, 4);
|
|
var projects = fx.Service.Projects;
|
|
var main = await projects.MainAsync(sequence.Id, fx.Ct);
|
|
var first = await projects.HeadAsync(main.Id, fx.Ct);
|
|
var pushed = (await projects.PushAsync(main.Id, ProjectServiceTests.Draft(first, new ProjectSettings
|
|
{
|
|
Stack = new StackSettings { Mode = StackSetting.Median },
|
|
Exposure = new ExposureSettings { ExposureOffsetEv = 0.5 },
|
|
}, frames), fx.Ct)).Version!;
|
|
|
|
// The push's mirror cut short, and another path writes an offset of its own and an exclusion meanwhile.
|
|
var reverted = await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings());
|
|
await fx.Db.Projects.SetMirroredAsync(main.Id, first.Id, reverted.SettingsVersion, fx.Ct);
|
|
await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 1.25 }, ExcludedFrames = [3] });
|
|
|
|
// The pushed version wins where both changed; the other writer's settings are the version before the merge.
|
|
var history = await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
|
|
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.External, ProjectVersionKind.Commit, ProjectVersionKind.Imported], history.Select(v => v.Kind));
|
|
Assert.Equal((history[1].Id, pushed.Id), (history[0].ParentId, history[1].ParentId));
|
|
var external = ProjectSettings.FromJson(ProjectContent.Place(history[1].SettingsJson, ProjectServiceTests.Photos(frames)));
|
|
Assert.Equal((1.25, StackSetting.None), (external.Exposure.ExposureOffsetEv, external.Stack.Mode));
|
|
Assert.SequenceEqual([3], external.ExcludedFrames);
|
|
var settings = ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!);
|
|
Assert.Equal((0.5, StackSetting.Median), (settings.Exposure.ExposureOffsetEv, settings.Stack.Mode));
|
|
Assert.SequenceEqual([3], settings.ExcludedFrames);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AMergeACrashStopsBetweenItsTwoVersionsIsFinishedNextTime()
|
|
{
|
|
foreach (var rebuild in new[] { false, true })
|
|
{
|
|
var fx = await FixtureAsync();
|
|
var (sequence, frames) = await ProjectServiceTests.UploadedAsync(fx, 4);
|
|
var projects = fx.Service.Projects;
|
|
var main = await projects.MainAsync(sequence.Id, fx.Ct);
|
|
var first = await projects.HeadAsync(main.Id, fx.Ct);
|
|
var pushed = (await projects.PushAsync(main.Id, ProjectServiceTests.Draft(first, new ProjectSettings
|
|
{
|
|
Stack = new StackSettings { Mode = StackSetting.Median },
|
|
Exposure = new ExposureSettings { ExposureOffsetEv = 0.5 },
|
|
}, frames), fx.Ct)).Version!;
|
|
var folder = fx.Paths.Sequence(sequence.Folder);
|
|
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id));
|
|
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(main.Id), record with { Mirrored = new ProjectHead(first.Id, 1) });
|
|
|
|
// The push's mirror cut short, another path's settings meanwhile, and the catch-up's merge cut after its external version.
|
|
var reverted = await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings());
|
|
await fx.Db.Projects.SetMirroredAsync(main.Id, first.Id, reverted.SettingsVersion, fx.Ct);
|
|
var theirs = new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 1.25 }, ExcludedFrames = [3] };
|
|
await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, theirs);
|
|
File.WriteAllText(folder.ProjectPath, theirs.ToJson());
|
|
projects.Cut = step =>
|
|
{
|
|
if (step == "catchup.external") throw new CutException(step);
|
|
};
|
|
await Assert.ThrowsAsync<CutException>(() => projects.ListAsync(sequence.Id, fx.Ct));
|
|
projects.Cut = null;
|
|
Assert.SequenceEqual([ProjectVersionKind.External, ProjectVersionKind.Commit, ProjectVersionKind.Imported],
|
|
(await fx.Db.Projects.ListVersionsAsync(main.Id, null, 10, fx.Ct)).Select(v => v.Kind));
|
|
|
|
// The next catch-up (by this index, or by one rebuilt from the sidecars) makes the merge version: the push is in force.
|
|
if (rebuild)
|
|
{
|
|
await fx.ReopenWithEmptyIndexAsync();
|
|
await fx.ScanAsync(ScanMode.Rebuild);
|
|
}
|
|
var history = await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
|
|
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.External, ProjectVersionKind.Commit, ProjectVersionKind.Imported],
|
|
history.Select(v => v.Kind));
|
|
Assert.Equal((history[1].Id, pushed.Id), (history[0].ParentId, history[1].ParentId));
|
|
foreach (var settings in new[] { ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!),
|
|
ProjectSettings.FromJson(File.ReadAllText(folder.ProjectPath)) })
|
|
{
|
|
Assert.Equal((0.5, StackSetting.Median), (settings.Exposure.ExposureOffsetEv, settings.Stack.Mode));
|
|
Assert.SequenceEqual([3], settings.ExcludedFrames);
|
|
}
|
|
Assert.Count(4, await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task ANewerChronosHeadOfMainIsNotMergedWithSettingsWrittenMeanwhile()
|
|
{
|
|
var fx = await FixtureAsync();
|
|
var (sequence, _) = await ProjectServiceTests.UploadedAsync(fx, 3);
|
|
var projects = fx.Service.Projects;
|
|
var main = await projects.MainAsync(sequence.Id, fx.Ct);
|
|
var newer = NewerChronos.Push(fx, sequence, await projects.HeadAsync(main.Id, fx.Ct));
|
|
await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { Stack = new StackSettings { Mode = StackSetting.Mean } });
|
|
|
|
// Both are left as they are: the newer head stays the head, the settings stay those written.
|
|
Assert.Equal(newer.Id, (await projects.HeadAsync(main.Id, fx.Ct)).Id);
|
|
Assert.Count(2, await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct));
|
|
var settings = ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!);
|
|
Assert.Equal((StackSetting.Mean, 0.0), (settings.Stack.Mode, settings.Exposure.ExposureOffsetEv));
|
|
}
|
|
}
|
|
|
|
public sealed class SqliteMainMergeTests(TestContext context) : MainMergeTests(context)
|
|
{
|
|
protected override Backend Backend => Backend.SqliteFile;
|
|
}
|
|
|
|
[RequiresEnvironment(PgTestServer.Variable)]
|
|
public sealed class PostgresMainMergeTests(TestContext context) : MainMergeTests(context)
|
|
{
|
|
protected override Backend Backend => Backend.Postgres;
|
|
}
|