fix(projects): one photo merges as one, newer settings are never merged

A workspace copied before the home hashed or converted a photo named it
differently from the versions the home made since, so a pull merged the
same photo as two: frame edits were silently undone and Keep mine placed
the library's value. Before replaying, the workspace media now learn the
home's hashes (same frame id, size and capture time) and conversion
aliases, so every side names each photo alike. Rebind also gives a
renamed entry the occurrence a placement looks for, so positions on
burst photos keep applying once hashed, and folds entries of one photo
(in Rebind and in the merge's keyed form) instead of keeping one.

Settings of a newer Chronos are no longer merged member by member with
this one's: ProjectMerge refuses when both sides changed. A workspace
pull keeps its local versions and working state untouched and records a
conflict of format that only Save both settles (into a new project of
this format); with nothing local it takes the newer head read-only.
Main's catch-up leaves a newer head and settings written meanwhile as
they are.

When Main's catch-up merge overrides values another writer stored, that
writer's settings are kept as an External version before the Merge one,
so nothing is lost from the history.

Unknown members inside Auto values, camera keyframes and curve points,
and on pending entries once placed, are kept like ProjectSettings' Extra.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-03 10:05:27 +01:00
1 parent 50e28fd63d
commit eac4a36a7b
12 files changed
+905 -38

No files matched your search

@@ -0,0 +1,192 @@
using System.Globalization;
using System.Text.Json.Nodes;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Projects;
using Chronos.Data.Tests.Archiving;
using Chronos.Data.Tests.Infrastructure;
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 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;
}
@@ -0,0 +1,287 @@
using Chronos.Core.Archiving;
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.Projects;
using Chronos.Data.Workspaces;
using Chronos.Testing;
namespace Chronos.Data.Tests.Workspaces;
/// <summary>
/// What a workspace's sync merges (owner review 2, R8): one photo named alike on every side whatever the home did to it since
/// the copy (hashed, converted), and settings of a newer Chronos never merged with this one's.
/// </summary>
[Category("workspace")]
public abstract class WorkspaceMergeTests(TestContext context) : DatabaseTest(context)
{
private async Task<WorkspaceFixture> FixtureAsync(int frames = 5)
{
var archive = Own(await ArchiveFixture.CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true)));
return Own(await WorkspaceFixture.CreateAsync(Context, archive, frames));
}
private static async Task EditAsync(WorkspaceSync sync, Guid projectId, Func<ProjectSettings, ProjectSettings> edit, CancellationToken ct)
{
var session = await sync.OpenAsync(projectId, ct);
await sync.SaveAsync(projectId, edit(session.Working), ct);
}
/// <summary>The photo at <paramref name="index"/> not hashed at the home, as in a folder adopted and not verified yet: its sidecar, then a full scan.</summary>
private static async Task UnhashAsync(WorkspaceFixture fx, int index)
{
var folder = fx.Archive.Paths.Sequence(fx.Sequence.Folder);
var manifest = ManifestStore.ReadSequence(folder.ManifestPath);
ManifestStore.WriteSequence(folder.ManifestPath, manifest with { Frames = [.. manifest.Frames.Select((f, i) => i == index ? f with { Sha256 = null } : f)] });
await fx.Archive.ScanAsync(ScanMode.Full);
Assert.Null((await fx.Db.Frames.ListBySequenceAsync(fx.Sequence.Id))[index].Sha256);
}
/// <summary>The archive verified: the photos not hashed yet are hashed (the frames version stays, so no Update media is offered).</summary>
private static async Task VerifyAsync(WorkspaceFixture fx)
{
var before = (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!.FramesVersion;
await fx.Archive.Service.Verifier.VerifyAsync(fx.Archive.Archive, null, default, (_, _, _) => ValueTask.CompletedTask, null, fx.Ct);
Assert.Equal(before, (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!.FramesVersion);
}
/// <summary>The sequence's settings changed at the home by another path (an analysis): Main takes them as an external version, its photos named as the home's frames hold them.</summary>
private static async Task WriteAtHomeAsync(WorkspaceFixture fx, Func<ProjectSettings, ProjectSettings> edit)
{
var row = (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!;
await ProjectServiceTests.WriteSettingsAsync(fx.Archive, fx.Sequence.Id, edit(ProjectSettings.FromJson(row.SettingsJson!)));
}
private static async Task<ProjectSettings> SequenceSettingsAsync(WorkspaceFixture fx) =>
ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!.SettingsJson!);
[Test]
public async Task AnEditSurvivesAPullOnceTheHomeHashedAPhotoTheCopyHadNot()
{
var fx = await FixtureAsync();
await UnhashAsync(fx, 2);
var (_, sync) = fx.Workspace();
var project = await fx.CheckoutAsync(sync);
var id = project.ProjectId;
var copied = project.ReadMedia()!;
Assert.Null(copied.Frames[2].Sha256);
await EditAsync(sync, id, s => s with { ExcludedFrames = [2] }, fx.Ct);
await sync.CommitAsync(id, cancellationToken: fx.Ct);
await sync.PushAsync(id, fx.Home, fx.Ct);
// The home hashes the photo, then an analysis writes the settings: Main's new version names the photo by its hash.
await VerifyAsync(fx);
var hashed = (await fx.Db.Frames.ListBySequenceAsync(fx.Sequence.Id))[2].Sha256!;
await WriteAtHomeAsync(fx, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.7 } });
Assert.Contains(hashed, (await fx.Home.HeadAsync(id, fx.Ct)).SettingsJson);
// Photo 2 brought back here: the pull takes the analysis's value and keeps the photo in, with nothing to settle.
await EditAsync(sync, id, s => s with { ExcludedFrames = [] }, fx.Ct);
var pulled = await sync.PullAsync(id, fx.Home, fx.Ct);
Assert.Null(pulled.Status.Conflict);
Assert.Equal(WorkspaceState.Modified, pulled.Status.State);
var working = (await sync.OpenAsync(id, fx.Ct)).Working;
Assert.Equal(0.7, working.Exposure.ExposureOffsetEv);
Assert.Empty(working.ExcludedFrames);
// The media learnt the hash: the frames and their files are as they were copied.
var media = project.ReadMedia()!;
Assert.Equal(hashed, media.Frames[2].Sha256);
Assert.Equal(copied.FramesVersion, media.FramesVersion);
Assert.SequenceEqual(copied.Frames.Select(f => f.FileSha256), media.Frames.Select(f => f.FileSha256));
Assert.NotNull(await sync.CommitAsync(id, cancellationToken: fx.Ct));
Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, fx.Home, fx.Ct)).Status.State);
var settings = await SequenceSettingsAsync(fx);
Assert.Empty(settings.ExcludedFrames);
Assert.Equal(0.7, settings.Exposure.ExposureOffsetEv);
}
[Test]
public async Task APullWithNothingEditedHereIsInSyncOnceTheHomeHashedAPhoto()
{
var fx = await FixtureAsync();
await UnhashAsync(fx, 2);
var (_, sync) = fx.Workspace();
var id = (await fx.CheckoutAsync(sync)).ProjectId;
await EditAsync(sync, id, s => s with { ExcludedFrames = [2] }, fx.Ct);
await sync.CommitAsync(id, cancellationToken: fx.Ct);
await sync.PushAsync(id, fx.Home, fx.Ct);
await VerifyAsync(fx);
await WriteAtHomeAsync(fx, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.7 } });
var pulled = await sync.PullAsync(id, fx.Home, fx.Ct);
Assert.Equal(WorkspaceState.InSync, pulled.Status.State);
Assert.SequenceEqual([2], (await sync.OpenAsync(id, fx.Ct)).Working.ExcludedFrames);
Assert.Null(await sync.CommitAsync(id, cancellationToken: fx.Ct));
}
[Test]
public async Task AVersionSavedBeforeAnotherWorkspaceNamedThePhotoByItsHashKeepsItsEdit()
{
var fx = await FixtureAsync();
await UnhashAsync(fx, 2);
var (_, one) = fx.Workspace("one");
var id = (await fx.CheckoutAsync(one)).ProjectId;
await EditAsync(one, id, s => s with { ExcludedFrames = [2] }, fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
await VerifyAsync(fx);
// Two copies the media once hashed and changes the stack.
var (_, two) = fx.Workspace("two");
await fx.CheckoutAsync(two, id);
await EditAsync(two, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
await two.CommitAsync(id, cancellationToken: fx.Ct);
await two.PushAsync(id, fx.Home, fx.Ct);
// One brings photo 2 back as a version of its own, then syncs: the version replayed still brings it back.
await EditAsync(one, id, s => s with { ExcludedFrames = [] }, fx.Ct);
await one.CommitAsync(id, "bring 2 back", cancellationToken: fx.Ct);
var pushed = await one.PushAsync(id, fx.Home, fx.Ct);
Assert.Equal(WorkspaceState.InSync, pushed.Status.State);
Assert.Equal("bring 2 back", pushed.Pushed.Single().Note);
var settings = await SequenceSettingsAsync(fx);
Assert.Empty(settings.ExcludedFrames);
Assert.Equal(StackSetting.Mean, settings.Stack.Mode);
}
[Test]
public async Task APhotoConvertedAtTheHomeBeforeUpdateMediaMergesAsOnePhotoAndKeepMineKeepsMine()
{
var fx = await FixtureAsync(4);
var (_, sync) = fx.Workspace();
var project = await fx.CheckoutAsync(sync);
var id = project.ProjectId;
await EditAsync(sync, id, s => s with { ExcludedFrames = [1], Camera = s.Camera with { Keyframes = [new CameraKey(1, 0.2, 0.5, 1.2, 0)] } }, fx.Ct);
await sync.CommitAsync(id, cancellationToken: fx.Ct);
await sync.PushAsync(id, fx.Home, fx.Ct);
// Photo 1 converted at the home (the media still hold the encoding copied), then the home moves its keyframe.
await ProjectServiceTests.ConvertAsync(fx.Archive, fx.Sequence.Id, [1], 700);
await WriteAtHomeAsync(fx, s => s with
{
Exposure = s.Exposure with { ExposureOffsetEv = 0.7 },
Camera = s.Camera with { Keyframes = [new CameraKey(1, 0.5, 0.5, 1.2, 0)] },
});
// Here photo 1 is brought back and its keyframe moved elsewhere: the keyframe is the one thing both changed.
await EditAsync(sync, id, s => s with { ExcludedFrames = [], Camera = s.Camera with { Keyframes = [new CameraKey(1, 0.8, 0.5, 1.2, 0)] } }, fx.Ct);
var pulled = await sync.PullAsync(id, fx.Home, fx.Ct);
var difference = Assert.NotNull(pulled.Status.Conflict).Differences.Single();
Assert.True(difference.Path.EndsWith(".keyframes", StringComparison.Ordinal), difference.Path);
Assert.Contains(project.ReadMedia()!.Frames[1].Sha256!, difference.Path);
await sync.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, cancellationToken: fx.Ct);
var working = (await sync.OpenAsync(id, fx.Ct)).Working;
Assert.Empty(working.ExcludedFrames);
Assert.Equal((1, 0.8), (working.Camera.Keyframes.Single().Frame, working.Camera.Keyframes.Single().CenterX));
Assert.Equal(0.7, working.Exposure.ExposureOffsetEv);
await sync.CommitAsync(id, cancellationToken: fx.Ct);
Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, fx.Home, fx.Ct)).Status.State);
var settings = await SequenceSettingsAsync(fx);
Assert.Empty(settings.ExcludedFrames);
Assert.Equal((1, 0.8), (settings.Camera.Keyframes.Single().Frame, settings.Camera.Keyframes.Single().CenterX));
}
/// <summary>What a newer Chronos sharing the archive writes for a push: its version file after the head, then the record's head.</summary>
private static ProjectVersion NewerPush(WorkspaceFixture fx, ProjectVersion head) => NewerChronos.Push(fx.Archive, fx.Sequence, head);
[Test]
public async Task LocalVersionsMeetingANewerChronosHeadStayAsTheyAreAndSaveBothSendsThem()
{
var fx = await FixtureAsync(3);
var (_, sync) = fx.Workspace();
var named = await fx.Archive.Service.Projects.CreateAsync(fx.Sequence.Id, "Grade", null, null, fx.Ct);
var project = await fx.CheckoutAsync(sync, named.Id);
var id = project.ProjectId;
await EditAsync(sync, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.3 } }, fx.Ct);
var local = Assert.NotNull(await sync.CommitAsync(id, "offset", cancellationToken: fx.Ct));
await EditAsync(sync, id, s => s with { ExcludedFrames = [1] }, fx.Ct);
var newer = NewerPush(fx, await fx.Home.HeadAsync(id, fx.Ct));
Assert.Equal(newer.Id, (await fx.Home.HeadAsync(id, fx.Ct)).Id);
// Nothing is merged into the newer settings: the local version, the working state and the base stay; a conflict of format waits.
var stopped = await sync.PushAsync(id, fx.Home, fx.Ct);
Assert.Empty(stopped.Pushed);
var conflict = Assert.NotNull(stopped.Status.Conflict);
Assert.True(conflict.NewerFormat);
Assert.Equal((WorkspaceState.Conflict, 1, false), (stopped.Status.State, stopped.Status.Ahead, stopped.Status.ReadOnly));
Assert.Equal(local.Id, sync.Unpushed(id).Single().Id);
Assert.Equal(local.SettingsJson, sync.Unpushed(id).Single().SettingsJson);
var working = (await sync.OpenAsync(id, fx.Ct)).Working;
Assert.Equal((ProjectSettings.CurrentVersion, 0.3), (working.Version, working.Exposure.ExposureOffsetEv));
Assert.SequenceEqual([1], working.ExcludedFrames);
Assert.Equal(WorkspaceState.Conflict, (await sync.PullAsync(id, fx.Home, fx.Ct)).Status.State);
// Merging either way is refused and leaves everything as it was.
var refused = await Assert.ThrowsAsync<WorkspaceSyncException>(() => sync.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, cancellationToken: fx.Ct));
Assert.Equal(PushOutcome.NewerFormat, refused.Outcome);
await Assert.ThrowsAsync<WorkspaceSyncException>(() => sync.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, cancellationToken: fx.Ct));
Assert.True((await sync.StatusAsync(id, fx.Home, fx.Ct)).Conflict!.NewerFormat);
Assert.Equal(local.Id, sync.Unpushed(id).Single().Id);
// Save both: this workspace's work (the change not saved yet too) goes to a new project in this Chronos's format.
var both = await sync.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct);
var copy = Assert.NotNull(both.SavedAs);
Assert.Equal(2, both.Pushed.Count);
var kept = ProjectSettings.FromJson(ProjectContent.Place((await fx.Home.HeadAsync(copy.Id, fx.Ct)).SettingsJson, ProjectServiceTests.Photos(fx.Frames)));
Assert.Equal(0.3, kept.Exposure.ExposureOffsetEv);
Assert.SequenceEqual([1], kept.ExcludedFrames);
// This project shows the newer Chronos's head, read-only.
Assert.True(both.Status.ReadOnly);
Assert.Equal(newer.Id, both.Status.Base!.VersionId);
Assert.Equal(1.5, (await sync.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv);
}
[Test]
public async Task AChangeNotSavedYetMeetingANewerChronosHeadWaitsOnAConflict()
{
var fx = await FixtureAsync(3);
var (_, sync) = fx.Workspace();
var named = await fx.Archive.Service.Projects.CreateAsync(fx.Sequence.Id, "Grade", null, null, fx.Ct);
var id = (await fx.CheckoutAsync(sync, named.Id)).ProjectId;
await EditAsync(sync, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.7 } }, fx.Ct);
NewerPush(fx, await fx.Home.HeadAsync(id, fx.Ct));
var pulled = await sync.PullAsync(id, fx.Home, fx.Ct);
Assert.True(Assert.NotNull(pulled.Status.Conflict).NewerFormat);
Assert.Equal((WorkspaceState.Conflict, true, false), (pulled.Status.State, pulled.Status.Modified, pulled.Status.ReadOnly));
Assert.Equal(0.7, (await sync.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv);
var both = await sync.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct);
Assert.Equal(0.7, ProjectSettings.FromJson(Assert.NotNull(both.Pushed.SingleOrDefault()).SettingsJson).Exposure.ExposureOffsetEv);
Assert.True(both.Status.ReadOnly);
}
[Test]
public async Task ANewerChronosHeadOverNothingOfThisWorkspacesIsTakenReadOnly()
{
var fx = await FixtureAsync(3);
var (_, sync) = fx.Workspace();
var named = await fx.Archive.Service.Projects.CreateAsync(fx.Sequence.Id, "Grade", null, null, fx.Ct);
var id = (await fx.CheckoutAsync(sync, named.Id)).ProjectId;
var newer = NewerPush(fx, await fx.Home.HeadAsync(id, fx.Ct));
var pulled = await sync.PullAsync(id, fx.Home, fx.Ct);
Assert.Null(pulled.Status.Conflict);
Assert.Equal((WorkspaceState.InSync, true), (pulled.Status.State, pulled.Status.ReadOnly));
Assert.Equal(newer.Id, pulled.Status.Base!.VersionId);
var session = await sync.OpenAsync(id, fx.Ct);
Assert.True(session.IsReadOnly);
Assert.Equal("film", session.Working.Extra!["grade"].GetProperty("look").GetString());
}
}
public sealed class SqliteWorkspaceMergeTests(TestContext context) : WorkspaceMergeTests(context)
{
protected override Backend Backend => Backend.SqliteFile;
}
[RequiresEnvironment(PgTestServer.Variable)]
public sealed class PostgresWorkspaceMergeTests(TestContext context) : WorkspaceMergeTests(context)
{
protected override Backend Backend => Backend.Postgres;
}