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; /// /// 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. /// [Category("workspace")] public abstract class WorkspaceMergeTests(TestContext context) : DatabaseTest(context) { private async Task 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 edit, CancellationToken ct) { var session = await sync.OpenAsync(projectId, ct); await sync.SaveAsync(projectId, edit(session.Working), ct); } /// The photo at not hashed at the home, as in a folder adopted and not verified yet: its sidecar, then a full scan. 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); } /// The archive verified: the photos not hashed yet are hashed (the frames version stays, so no Update media is offered). 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); } /// 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. private static async Task WriteAtHomeAsync(WorkspaceFixture fx, Func 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 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)); } /// What a newer Chronos sharing the archive writes for a push: its version file after the head, then the record's head. 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(() => sync.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, cancellationToken: fx.Ct)); Assert.Equal(PushOutcome.NewerFormat, refused.Outcome); await Assert.ThrowsAsync(() => 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; }