Files
Chronos/tests/Chronos.Data.Tests/Workspaces/WorkspaceConflictTests.cs
T
Alby96andClaude Opus 5.5 64fc5521c0 fix(workspace): settle conflicts in one answer and give refused folders a way on
Phase 5a fix round 2 of owner review 2 (desktop):

- Take library's no longer commits the working state first, so a take that
  leaves only the library's values sends no empty version; the working state
  it replaces is kept aside in versions/.replaced, outside the chain.
- Conflicts list this side's value as the working state holds it, so one
  answer settles them instead of asking again for the newer value.
- A conflict of format (library saved by a newer Chronos) offers only Save
  both and Later, says why in its tooltip and shows no raw format numbers.
- Conflict lines count the photos a setting differs on ("Excluded frames
  (3)") instead of dropping the duplicates.
- A read-only library is named as such, not as offline; checkouts from it
  stay editable and their versions wait in the workspace.
- A workspace folder refused inside a library offers Choose folder, and
  Settings > Workspace gets Change...: a plan (projects left, projects found)
  is confirmed before the folder changes; nothing is moved.
- Open in workspace shows the copy's size and the drive's free space, and
  warns when the copy would not fit with its 0.5 GB reserve.
- A cancelled first copy removes the half media it wrote.
- A copy whose project.chronos cannot be read is listed as damaged instead of
  hidden, and goes only through Remove local copy > Discard, after a plan.
- Pruning keeps a version whose archive file is damaged (test added).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 11:52:49 +01:00

263 lines
16 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);
Assert.Equal("warmer", two.Unpushed(id).Single().Note, "the saved version is replayed, nothing more");
}
[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");
}
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;
}