Files
Chronos/tests/Chronos.Data.Tests/Workspaces/WorkspaceConflictTests.cs
T
Alby96andClaude Opus 5.5 9b24cf74fc
CI / Build and test (push) Successful in 9m43s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 27s
CI / PostgreSQL 16 integration (push) Failing after 8m41s
CI / PostgreSQL 17 integration (push) Successful in 12m37s
CI / Docker image (push) Successful in 56s
Release / Build, test and publish (push) Successful in 14m59s
Release / Server image (push) Successful in 1m51s
feat: owner review 2 phase 5a: workspace and projects with a local home (#14)
Edit opens a sequence as a project of a workspace folder outside the library. Projects have linear version histories kept as sidecars in .chronos/projects/, Main mirrors the sequence's settings, and sync merges setting by setting. Migration 0009, JobKind.WorkspaceCopy = 20, Settings › Workspace, and 241 new strings in 38 languages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-11-04 09:22:08 +01:00

326 lines
20 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Projects;
using Chronos.Data.Tests.Archiving;
using Chronos.Data.Tests.Infrastructure;
using Chronos.Data.Workspaces;
using Chronos.Testing;
namespace Chronos.Data.Tests.Workspaces;
/// <summary>
/// Settling a workspace conflict and restoring a version (owner review 2, R8): a choice applies only to the conflicts the
/// user was shown, what a choice replaces stays in a version, a restore loses nothing while a conflict waits, and only a
/// restore the working state still shows makes a "restore" version.
/// </summary>
[Category("workspace")]
public abstract class WorkspaceConflictTests(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));
}
/// <summary>The working state edited as the editor does: read, changed, stored.</summary>
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);
}
private static ProjectSettings Offset(ProjectSettings s, double ev) => s with { Exposure = s.Exposure with { ExposureOffsetEv = ev } };
private static ProjectSettings Stack(ProjectSettings s, StackSetting mode) => s with { Stack = new StackSettings { Mode = mode } };
/// <summary>Two workspaces of Main; the first has sent an offset of 0.5 and a Median stack, the second changed the offset to 1 and was asked.</summary>
private static async Task<(WorkspaceFixture Fx, WorkspaceSync One, WorkspaceSync Two, Guid Id, WorkspaceConflict Shown)> AskedAsync(WorkspaceFixture fx)
{
var (_, one) = fx.Workspace("one");
var (_, two) = fx.Workspace("two");
var id = (await fx.CheckoutAsync(one)).ProjectId;
await fx.CheckoutAsync(two, id);
await EditAsync(one, id, s => Stack(Offset(s, 0.5), StackSetting.Median), fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
await EditAsync(two, id, s => Offset(s, 1.0), fx.Ct);
var pulled = await two.PullAsync(id, fx.Home, fx.Ct);
var shown = Assert.NotNull(pulled.Status.Conflict);
Assert.Equal("exposure.exposureOffsetEv", shown.Differences.Single().Path, "the stack only one side changed merges");
return (fx, one, two, id, shown);
}
[Test]
public async Task AChoiceSettlesOnlyTheConflictsTheUserWasShown()
{
var (fx, _, two, id, shown) = await AskedAsync(await FixtureAsync());
// Later: editing goes on, and the stack, which the library changed too, is set here. Nobody was shown that one.
await EditAsync(two, id, s => Stack(s, StackSetting.StarTrails), fx.Ct);
var asked = await two.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: shown, cancellationToken: fx.Ct);
Assert.Equal(WorkspaceState.Conflict, asked.Status.State);
var again = Assert.NotNull(asked.Status.Conflict);
Assert.SequenceEqual(["exposure.exposureOffsetEv", "stack.mode"], again.Differences.Select(d => d.Path).Order(StringComparer.Ordinal));
var working = (await two.OpenAsync(id, fx.Ct)).Working;
Assert.Equal((1.0, StackSetting.StarTrails), (working.Exposure.ExposureOffsetEv, working.Stack.Mode), "nothing was settled");
// Asked again with both, take the library's: its values; this side's are kept in a version all the same.
Assert.Null(asked.Kept, "nothing settled, nothing to take back");
var settled = await two.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: again, cancellationToken: fx.Ct);
Assert.Null(settled.Status.Conflict);
working = (await two.OpenAsync(id, fx.Ct)).Working;
Assert.Equal((0.5, StackSetting.Median), (working.Exposure.ExposureOffsetEv, working.Stack.Mode));
// What keeping this side's values would have left, for the editor's undo of the take.
var kept = Assert.NotNull(settled.Kept);
Assert.Equal((1.0, StackSetting.StarTrails), (kept.Exposure.ExposureOffsetEv, kept.Stack.Mode));
var replaced = Path.Combine(two.Store.Find(id)!.VersionsDirectory, WorkspaceProject.ReplacedFolder);
Assert.True(Directory.GetFiles(replaced).Select(ProjectSidecars.ReadVersion).Any(v =>
ProjectSettings.FromJson(v.SettingsJson) is { Stack.Mode: StackSetting.StarTrails, Exposure.ExposureOffsetEv: 1.0 }), "the values taken over stay in a version");
}
[Test]
public async Task TakingTheLibrarysValuesOverUnsavedChangesSendsNoVersion()
{
var (fx, _, two, id, shown) = await AskedAsync(await FixtureAsync());
// Unsaved too: a setting only this side changed.
await EditAsync(two, id, s => s with { Motion = s.Motion with { TargetShutterAngle = s.Motion.TargetShutterAngle.WithManual(90) } }, fx.Ct);
var history = await fx.Home.HistoryAsync(id, null, 10, fx.Ct);
var settled = await two.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: shown, cancellationToken: fx.Ct);
Assert.Null(settled.Status.Conflict);
Assert.Empty(two.Unpushed(id), "no version of the working state is made, so none repeating the head is sent");
Assert.Equal(WorkspaceState.Modified, settled.Status.State, "the change only this side made waits, unsaved as it was");
var working = (await two.OpenAsync(id, fx.Ct)).Working;
Assert.Equal((0.5, StackSetting.Median, 90.0), (working.Exposure.ExposureOffsetEv, working.Stack.Mode, working.Motion.TargetShutterAngle.Resolve(0)));
Assert.Equal(1.0, Assert.NotNull(settled.Kept).Exposure.ExposureOffsetEv);
Assert.Empty((await two.PushAsync(id, fx.Home, fx.Ct)).Pushed);
Assert.Equal(history.Count, (await fx.Home.HistoryAsync(id, null, 10, fx.Ct)).Count, "the library's history holds no empty version");
// The value taken over stays in a version file set aside, outside the versions to send.
var replaced = Path.Combine(two.Store.Find(id)!.VersionsDirectory, WorkspaceProject.ReplacedFolder);
Assert.Contains(Directory.GetFiles(replaced).Select(ProjectSidecars.ReadVersion), v => ProjectSettings.FromJson(v.SettingsJson).Exposure.ExposureOffsetEv == 1.0);
}
[Test]
public async Task AConflictListsTheWorkingStatesValueAndOneAnswerSettlesIt()
{
var fx = await FixtureAsync();
var (_, one) = fx.Workspace("one");
var (_, two) = fx.Workspace("two");
var id = (await fx.CheckoutAsync(one)).ProjectId;
await fx.CheckoutAsync(two, id);
await EditAsync(one, id, s => Offset(s, 0.5), fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
// This side saved 0.8 as a version, then went on to 1 without saving.
await EditAsync(two, id, s => Offset(s, 0.8), fx.Ct);
await two.CommitAsync(id, "warmer", cancellationToken: fx.Ct);
await EditAsync(two, id, s => Offset(s, 1.0), fx.Ct);
var shown = Assert.NotNull((await two.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
var difference = shown.Differences.Single();
Assert.Equal(("exposure.exposureOffsetEv", 1.0, 0.5), (difference.Path, Number(difference.Mine), Number(difference.Theirs)),
"this side's value is the one the editor shows");
var settled = await two.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: shown, cancellationToken: fx.Ct);
Assert.Null(settled.Status.Conflict, "one answer settles what was shown");
Assert.Equal(0.5, (await two.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv);
Assert.Equal(1.0, Assert.NotNull(settled.Kept).Exposure.ExposureOffsetEv);
// The saved version changes nothing once the library's offset is taken: it is not sent, its file kept aside.
Assert.Empty(two.Unpushed(id));
var replaced = Path.Combine(two.Store.Find(id)!.VersionsDirectory, WorkspaceProject.ReplacedFolder);
Assert.Contains(Directory.GetFiles(replaced).Select(ProjectSidecars.ReadVersion), v => v.Note == "warmer");
}
[Test]
public async Task KeepMineKeepsTheWorkingStatesValueItListed()
{
var fx = await FixtureAsync();
var (_, one) = fx.Workspace("one");
var (_, two) = fx.Workspace("two");
var id = (await fx.CheckoutAsync(one)).ProjectId;
await fx.CheckoutAsync(two, id);
await EditAsync(one, id, s => Offset(s, 0.5), fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
await EditAsync(two, id, s => Offset(s, 0.8), fx.Ct);
await two.CommitAsync(id, cancellationToken: fx.Ct);
await EditAsync(two, id, s => Offset(s, 1.0), fx.Ct);
var shown = Assert.NotNull((await two.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
var settled = await two.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, shown: shown, cancellationToken: fx.Ct);
Assert.Null(settled.Status.Conflict);
Assert.Equal(1.0, (await two.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv, "the value listed as this side's is kept");
}
[Test]
public async Task AWorkingStateBackAtTheBaseValueIsListedAndKeptAsItIs()
{
var fx = await FixtureAsync();
var (_, one) = fx.Workspace("one");
var (_, two) = fx.Workspace("two");
var id = (await fx.CheckoutAsync(one)).ProjectId;
await fx.CheckoutAsync(two, id);
var basis = (await two.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv;
await EditAsync(one, id, s => Offset(s, 0.5), fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
// This side saved 0.8 as a version, then went back to the base value without saving.
await EditAsync(two, id, s => Offset(s, 0.8), fx.Ct);
await two.CommitAsync(id, "warmer", cancellationToken: fx.Ct);
await EditAsync(two, id, s => Offset(s, basis), fx.Ct);
var shown = Assert.NotNull((await two.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
var difference = shown.Differences.Single();
Assert.Equal(("exposure.exposureOffsetEv", basis, 0.5), (difference.Path, Number(difference.Mine) ?? 0, Number(difference.Theirs)),
"this side's value is the working state's, not the version's 0.8");
var settled = await two.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, shown: shown, cancellationToken: fx.Ct);
Assert.Null(settled.Status.Conflict, "one answer settles what was shown");
Assert.Equal(basis, (await two.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv, "the value listed as this side's is the one kept");
Assert.True(settled.Status.Modified, "the saved version keeps its own value; the working state's is not saved yet");
Assert.Equal("warmer", two.Unpushed(id).Single().Note);
await two.CommitAsync(id, cancellationToken: fx.Ct);
await two.PushAsync(id, fx.Home, fx.Ct);
Assert.Equal(basis, ProjectSettings.FromJson((await fx.Home.HeadAsync(id, fx.Ct)).SettingsJson).Exposure.ExposureOffsetEv, "the library ends with the value kept");
}
[Test]
public async Task AWorkingStateHoldingTheLibrarysValueIsNoConflictAndItsVersionAgrees()
{
var fx = await FixtureAsync();
var (_, one) = fx.Workspace("one");
var (_, two) = fx.Workspace("two");
var id = (await fx.CheckoutAsync(one)).ProjectId;
await fx.CheckoutAsync(two, id);
await EditAsync(one, id, s => Offset(s, 0.5), fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
// This side saved 0.8 and a stack as a version, then set the offset to the library's own 0.5 without saving.
await EditAsync(two, id, s => Stack(Offset(s, 0.8), StackSetting.Mean), fx.Ct);
await two.CommitAsync(id, "local", cancellationToken: fx.Ct);
await EditAsync(two, id, s => Offset(s, 0.5), fx.Ct);
var pulled = await two.PullAsync(id, fx.Home, fx.Ct);
Assert.Null(pulled.Status.Conflict, "nothing to choose: the working state holds the library's value");
var working = (await two.OpenAsync(id, fx.Ct)).Working;
Assert.Equal((0.5, StackSetting.Mean), (working.Exposure.ExposureOffsetEv, working.Stack.Mode), "the working state is unchanged");
var replayed = ProjectSettings.FromJson(two.Unpushed(id).Single().SettingsJson);
Assert.Equal((0.5, StackSetting.Mean), (replayed.Exposure.ExposureOffsetEv, replayed.Stack.Mode), "the replayed version agrees with the working state");
Assert.True(pulled.Status is { State: WorkspaceState.Ahead, Modified: false }, "nothing left unsaved");
await two.PushAsync(id, fx.Home, fx.Ct);
var head = ProjectSettings.FromJson((await fx.Home.HeadAsync(id, fx.Ct)).SettingsJson);
Assert.Equal((0.5, StackSetting.Mean), (head.Exposure.ExposureOffsetEv, head.Stack.Mode));
}
private static double? Number(string? json) => json is null ? null : double.Parse(json, System.Globalization.CultureInfo.InvariantCulture);
[Test]
public async Task KeepMineIsAskedAgainWhenTheLibraryMovedOnASettingThisSideChanged()
{
var (fx, one, two, id, shown) = await AskedAsync(await FixtureAsync());
await EditAsync(two, id, s => s with { Motion = s.Motion with { TargetShutterAngle = s.Motion.TargetShutterAngle.WithManual(90) } }, fx.Ct);
// Meanwhile the other workspace sends a change of that same setting.
await one.PullAsync(id, fx.Home, fx.Ct);
await EditAsync(one, id, s => s with { Motion = s.Motion with { TargetShutterAngle = s.Motion.TargetShutterAngle.WithManual(270) } }, fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
var asked = await two.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, shown: shown, cancellationToken: fx.Ct);
var again = Assert.NotNull(asked.Status.Conflict, "the library's newer change is asked about, not overwritten");
Assert.Equal(2, again.Differences.Count);
Assert.Contains(again.Differences, d => d.Path.StartsWith("motion.targetShutterAngle", StringComparison.Ordinal));
Assert.Equal(270.0, ProjectSettings.FromJson((await fx.Home.HeadAsync(id, fx.Ct)).SettingsJson).Motion.TargetShutterAngle.Resolve(0));
var settled = await two.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, shown: again, cancellationToken: fx.Ct);
Assert.Equal(WorkspaceState.Modified, settled.Status.State, "this side's values are the working state, on the library's head");
Assert.Null(settled.Kept, "keeping mine replaces nothing of this side's");
await two.CommitAsync(id, cancellationToken: fx.Ct);
await two.PushAsync(id, fx.Home, fx.Ct);
var head = ProjectSettings.FromJson((await fx.Home.HeadAsync(id, fx.Ct)).SettingsJson);
Assert.Equal((1.0, 90.0, StackSetting.Median), (head.Exposure.ExposureOffsetEv, head.Motion.TargetShutterAngle.Resolve(0), head.Stack.Mode));
}
[Test]
public async Task SyncAsksAboutWhatDiffersNowWhileAConflictWaits()
{
var (fx, _, two, id, _) = await AskedAsync(await FixtureAsync());
// Edited during Later: the next pull lists the stack too.
await EditAsync(two, id, s => Stack(s, StackSetting.Mean), fx.Ct);
var refreshed = Assert.NotNull((await two.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
Assert.SequenceEqual(["exposure.exposureOffsetEv", "stack.mode"], refreshed.Differences.Select(d => d.Path).Order(StringComparer.Ordinal));
// This side takes the library's values by hand: nothing conflicts any more, and the pull settles it.
await EditAsync(two, id, s => Stack(Offset(s, 0.5), StackSetting.Median), fx.Ct);
var pulled = await two.PullAsync(id, fx.Home, fx.Ct);
Assert.Null(pulled.Status.Conflict);
Assert.Equal(WorkspaceState.InSync, pulled.Status.State);
Assert.Null(await two.CommitAsync(id, cancellationToken: fx.Ct), "nothing new to save, and no conflict refuses a version");
}
[Test]
public async Task ARestoreWhileAConflictWaitsKeepsTheUnsavedChangesAsAVersion()
{
var (fx, _, two, id, _) = await AskedAsync(await FixtureAsync());
var first = (await fx.Home.HistoryAsync(id, null, 10, fx.Ct)).Single(v => v.Number == 1);
// Later, then an edit stored but saved as no version, then History › Restore.
await EditAsync(two, id, s => Stack(s, StackSetting.Mean), fx.Ct);
var restored = await two.RestoreAsync(id, first.Id, fx.Home, fx.Ct);
Assert.Equal((0.0, StackSetting.None), (restored.Exposure.ExposureOffsetEv, restored.Stack.Mode));
var kept = Assert.Contains(two.Unpushed(id), v => ProjectSettings.FromJson(v.SettingsJson) is { Stack.Mode: StackSetting.Mean, Exposure.ExposureOffsetEv: 1.0 });
Assert.Equal(ProjectVersionKind.Commit, kept.Kind);
Assert.NotNull((await two.StatusAsync(id, fx.Home, fx.Ct)).Conflict, "the conflict still waits");
// Settled for this side, the saved edit reaches the library under the restore.
var asked = Assert.NotNull((await two.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
await two.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, shown: asked, cancellationToken: fx.Ct);
await two.PushAsync(id, fx.Home, fx.Ct);
Assert.Contains(await fx.Home.HistoryAsync(id, null, 10, fx.Ct), v => ProjectSettings.FromJson(v.SettingsJson).Stack.Mode == StackSetting.Mean);
}
[Test]
public async Task OnlyARestoreTheWorkingStateStillShowsMakesARestoreVersion()
{
var fx = await FixtureAsync();
var (_, sync) = fx.Workspace();
var id = (await fx.CheckoutAsync(sync)).ProjectId;
var original = await fx.Home.HeadAsync(id, fx.Ct);
await EditAsync(sync, id, s => Offset(s, 0.3), fx.Ct);
var warm = Assert.NotNull(await sync.CommitAsync(id, cancellationToken: fx.Ct));
// Restoring the version the project is changes nothing: the next version is a plain one.
await sync.RestoreAsync(id, warm.Id, fx.Home, fx.Ct);
await EditAsync(sync, id, s => Stack(s, StackSetting.Mean), fx.Ct);
var next = Assert.NotNull(await sync.CommitAsync(id, cancellationToken: fx.Ct));
Assert.Equal((ProjectVersionKind.Commit, (Guid?)null), (next.Kind, next.RestoredFrom));
// A restore taken back (the editor's Undo writes the state from before it again), then another edit: no restore either.
var before = (await sync.OpenAsync(id, fx.Ct)).Working;
await sync.RestoreAsync(id, original.Id, fx.Home, fx.Ct);
await sync.SaveAsync(id, before, fx.Ct);
await EditAsync(sync, id, s => s with { ExcludedFrames = [1] }, fx.Ct);
var after = Assert.NotNull(await sync.CommitAsync(id, cancellationToken: fx.Ct));
Assert.Equal((ProjectVersionKind.Commit, (Guid?)null), (after.Kind, after.RestoredFrom));
// A restore the working state still shows, edited further: a restore of it.
await sync.RestoreAsync(id, original.Id, fx.Home, fx.Ct);
await EditAsync(sync, id, s => s with { HeroFrame = 2 }, fx.Ct);
var restore = Assert.NotNull(await sync.CommitAsync(id, cancellationToken: fx.Ct));
Assert.Equal((ProjectVersionKind.Restore, (Guid?)original.Id), (restore.Kind, restore.RestoredFrom));
}
}
public sealed class SqliteWorkspaceConflictTests(TestContext context) : WorkspaceConflictTests(context)
{
protected override Backend Backend => Backend.SqliteFile;
}
[RequiresEnvironment(PgTestServer.Variable)]
public sealed class PostgresWorkspaceConflictTests(TestContext context) : WorkspaceConflictTests(context)
{
protected override Backend Backend => Backend.Postgres;
}