feat: owner review 2 phase 5a: workspace and projects with a local home (#14)
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
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
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>
This commit was merged in pull request #14.
This commit is contained in:
1 parent
efa87f68ce
commit
9b24cf74fc
370 files changed
+29237
-287
No files matched your search
@@ -0,0 +1,325 @@
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Projects;
|
||||
using Chronos.Data.Tests.Archiving;
|
||||
using Chronos.Data.Tests.Projects;
|
||||
using Chronos.Data.Workspaces;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Data.Tests.Workspaces;
|
||||
|
||||
/// <summary>An archive with an uploaded sequence as the projects' home, and workspaces that edit it (owner review 2, R8).</summary>
|
||||
internal sealed class WorkspaceFixture : IAsyncDisposable
|
||||
{
|
||||
private readonly TestContext _context;
|
||||
private JobEngine? _engine;
|
||||
|
||||
private WorkspaceFixture(TestContext context, ArchiveFixture archive, Sequence sequence, List<Frame> frames)
|
||||
{
|
||||
_context = context;
|
||||
Archive = archive;
|
||||
Sequence = sequence;
|
||||
Frames = frames;
|
||||
Home = new ArchiveProjectHome(archive.Db, archive.Service.Projects, sequence.Id);
|
||||
}
|
||||
|
||||
public ArchiveFixture Archive { get; }
|
||||
public Sequence Sequence { get; set; }
|
||||
public List<Frame> Frames { get; }
|
||||
public ArchiveProjectHome Home { get; }
|
||||
public ChronosDatabase Db => Archive.Db;
|
||||
public CancellationToken Ct => Archive.Ct;
|
||||
|
||||
public static async Task<WorkspaceFixture> CreateAsync(TestContext context, ArchiveFixture archive, int frames = 5)
|
||||
{
|
||||
var (sequence, list) = await ProjectServiceTests.UploadedAsync(archive, frames);
|
||||
return new WorkspaceFixture(context, archive, sequence, list);
|
||||
}
|
||||
|
||||
/// <summary>A workspace of its own (another PC, or another folder of this one) and its sync.</summary>
|
||||
public (WorkspaceStore Store, WorkspaceSync Sync) Workspace(string name = "workspace")
|
||||
{
|
||||
var store = new WorkspaceStore(Path.Combine(_context.CreateTempDirectory(name), "workspace"));
|
||||
return (store, new WorkspaceSync(store, "Chronos on " + name) { LockWait = TimeSpan.FromSeconds(TestTiming.Budget(10)) });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Another sync of <paramref name="store"/> that stops, as a crash would, at the <paramref name="occurrence"/>-th time it
|
||||
/// reaches step <paramref name="step"/> (<see cref="WorkspaceSync.Cut"/>), throwing <see cref="CutException"/>.
|
||||
/// </summary>
|
||||
public static WorkspaceSync Cutting(WorkspaceStore store, string step, int occurrence = 1)
|
||||
{
|
||||
var seen = 0;
|
||||
return new WorkspaceSync(store, "Chronos cut")
|
||||
{
|
||||
LockWait = TimeSpan.FromSeconds(TestTiming.Budget(10)),
|
||||
Cut = name =>
|
||||
{
|
||||
if (name == step && ++seen == occurrence) throw new CutException(step);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Main of the sequence checked out into <paramref name="sync"/>'s workspace, its media copied.</summary>
|
||||
public async Task<WorkspaceProject> CheckoutAsync(WorkspaceSync sync, Guid? projectId = null)
|
||||
{
|
||||
var id = projectId ?? (await Archive.Service.Projects.MainAsync(Sequence.Id, Ct)).Id;
|
||||
var project = await sync.CheckoutAsync(Home, id, cancellationToken: Ct);
|
||||
await CopyAsync(sync.Store, id);
|
||||
return project;
|
||||
}
|
||||
|
||||
/// <summary>Runs a workspace copy job to its end.</summary>
|
||||
public Task<Job> CopyAsync(WorkspaceStore store, Guid projectId, JobState expected = JobState.Done, string? checkpoint = null, string media = "proxies") =>
|
||||
CopyAsync(store.Root, projectId, expected, checkpoint, media);
|
||||
|
||||
/// <summary>Runs a workspace copy job into the workspace at <paramref name="root"/> to its end.</summary>
|
||||
public async Task<Job> CopyAsync(string root, Guid projectId, JobState expected = JobState.Done, string? checkpoint = null, string media = "proxies")
|
||||
{
|
||||
var engine = await EngineAsync();
|
||||
var request = WorkspaceJobs.Copy(Sequence, root, projectId, media);
|
||||
var job = checkpoint is null ? await engine.EnqueueAsync(request, cancellationToken: Ct) : await Archive.EnqueueWithCheckpointAsync(engine, request, checkpoint);
|
||||
return await Archive.WaitForAsync(job.Id, expected, (int)TestTiming.Budget(60));
|
||||
}
|
||||
|
||||
/// <summary>Queues a workspace copy job and returns at once, while it runs.</summary>
|
||||
public async Task<Job> StartCopyAsync(WorkspaceStore store, Guid projectId)
|
||||
{
|
||||
var engine = await EngineAsync();
|
||||
return await engine.EnqueueAsync(WorkspaceJobs.Copy(Sequence, store.Root, projectId), cancellationToken: Ct);
|
||||
}
|
||||
|
||||
/// <summary>Cancels a job, as the user does from the jobs list.</summary>
|
||||
public async Task CancelAsync(Guid jobId) => await (await EngineAsync()).CancelAsync(jobId, Ct);
|
||||
|
||||
/// <summary>Pauses a job, as the user does from the jobs list.</summary>
|
||||
public async Task PauseAsync(Guid jobId) => await (await EngineAsync()).PauseAsync(jobId, Ct);
|
||||
|
||||
private async Task<JobEngine> EngineAsync()
|
||||
{
|
||||
if (_engine is not null) return _engine;
|
||||
_engine = new JobEngine(Db, [.. Archive.Service.CreateJobHandlers(), .. WorkspaceJobs.CreateHandlers(Db, Archive.Service)],
|
||||
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
|
||||
await _engine.StartAsync(Ct);
|
||||
return _engine;
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_engine is not null) await _engine.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A crash between two writes of a sync (<see cref="WorkspaceFixture.Cutting"/>).</summary>
|
||||
internal sealed class CutException(string step) : Exception($"Cut at {step}.");
|
||||
|
||||
/// <summary>
|
||||
/// A home whose pushes can be cut: one refused as out of reach before the home sees it, or one stored whose answer is lost
|
||||
/// on the way back (the program killed, the network dropped).
|
||||
/// </summary>
|
||||
internal sealed class CuttingHome(IProjectHome home) : IProjectHome
|
||||
{
|
||||
public int Pushes { get; private set; }
|
||||
|
||||
/// <summary>The push (counted from 1) refused as out of reach.</summary>
|
||||
public int? Refuse { get; set; }
|
||||
|
||||
/// <summary>The push (counted from 1) the home stores and whose answer is lost.</summary>
|
||||
public int? LoseAnswer { get; set; }
|
||||
|
||||
/// <summary>What the home answers every push with, storing nothing (a home with a smaller limit, say); null to store.</summary>
|
||||
public PushOutcome? Answer { get; set; }
|
||||
|
||||
/// <summary>The home answers it holds no versions after any number (an index taken back from an older backup).</summary>
|
||||
public bool Forgets { get; set; }
|
||||
|
||||
public async Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken)
|
||||
{
|
||||
var push = ++Pushes;
|
||||
if (push == Refuse) throw new HomeUnavailableException("The home cannot be reached.");
|
||||
if (Answer is { } answer) return new PushResult(answer, null, await home.HeadAsync(projectId, cancellationToken));
|
||||
var result = await home.PushAsync(projectId, version, cancellationToken);
|
||||
if (push == LoseAnswer) throw new HomeUnavailableException("The answer was lost.");
|
||||
return result;
|
||||
}
|
||||
|
||||
public Task<HomeSequence> SequenceAsync(CancellationToken cancellationToken) => home.SequenceAsync(cancellationToken);
|
||||
public Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken) => home.ListAsync(cancellationToken);
|
||||
|
||||
public Task<SequenceProject> CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) =>
|
||||
home.CreateAsync(name, fromVersionId, author, cancellationToken);
|
||||
|
||||
public Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken) => home.HeadAsync(projectId, cancellationToken);
|
||||
|
||||
public Task<ProjectVersion?> GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) =>
|
||||
home.GetVersionAsync(projectId, versionId, cancellationToken);
|
||||
|
||||
public Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
|
||||
Forgets ? Task.FromResult<IReadOnlyList<ProjectVersion>>([]) : home.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken);
|
||||
|
||||
public Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) =>
|
||||
home.HistoryAsync(projectId, beforeNumber, limit, cancellationToken);
|
||||
|
||||
public Task<HomeFrames> FramesAsync(CancellationToken cancellationToken) => home.FramesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>A home that counts its reads of the frames, its stamp of them passed through.</summary>
|
||||
internal sealed class CountingHome(IProjectHome home) : IProjectHome
|
||||
{
|
||||
public int FramesReads { get; private set; }
|
||||
|
||||
public Task<HomeFrames> FramesAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
FramesReads++;
|
||||
return home.FramesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public Task<string?> FramesStampAsync(CancellationToken cancellationToken) => home.FramesStampAsync(cancellationToken);
|
||||
|
||||
public Task<HomeSequence> SequenceAsync(CancellationToken cancellationToken) => home.SequenceAsync(cancellationToken);
|
||||
public Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken) => home.ListAsync(cancellationToken);
|
||||
|
||||
public Task<SequenceProject> CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) =>
|
||||
home.CreateAsync(name, fromVersionId, author, cancellationToken);
|
||||
|
||||
public Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken) => home.HeadAsync(projectId, cancellationToken);
|
||||
|
||||
public Task<ProjectVersion?> GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) =>
|
||||
home.GetVersionAsync(projectId, versionId, cancellationToken);
|
||||
|
||||
public Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
|
||||
home.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken);
|
||||
|
||||
public Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) =>
|
||||
home.HistoryAsync(projectId, beforeNumber, limit, cancellationToken);
|
||||
|
||||
public Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) => home.PushAsync(projectId, version, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>A home that cannot be reached, standing for a server out of reach or an archive offline.</summary>
|
||||
internal sealed class UnreachableHome(IProjectHome home) : IProjectHome
|
||||
{
|
||||
public bool Reachable { get; set; }
|
||||
|
||||
private T Reach<T>(Func<T> call) => Reachable ? call() : throw new HomeUnavailableException("The home cannot be reached.");
|
||||
|
||||
public Task<HomeSequence> SequenceAsync(CancellationToken cancellationToken) => Reach(() => home.SequenceAsync(cancellationToken));
|
||||
public Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken) => Reach(() => home.ListAsync(cancellationToken));
|
||||
|
||||
public Task<SequenceProject> CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) =>
|
||||
Reach(() => home.CreateAsync(name, fromVersionId, author, cancellationToken));
|
||||
|
||||
public Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken) => Reach(() => home.HeadAsync(projectId, cancellationToken));
|
||||
|
||||
public Task<ProjectVersion?> GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) =>
|
||||
Reach(() => home.GetVersionAsync(projectId, versionId, cancellationToken));
|
||||
|
||||
public Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
|
||||
Reach(() => home.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken));
|
||||
|
||||
public Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) =>
|
||||
Reach(() => home.HistoryAsync(projectId, beforeNumber, limit, cancellationToken));
|
||||
|
||||
public Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) =>
|
||||
Reach(() => home.PushAsync(projectId, version, cancellationToken));
|
||||
|
||||
public Task<HomeFrames> FramesAsync(CancellationToken cancellationToken) => Reach(() => home.FramesAsync(cancellationToken));
|
||||
}
|
||||
@@ -0,0 +1,407 @@
|
||||
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 ABaseNamingAPhotoByItsHashReadsAsTheWorkingStateOnMediaThatHaveNotLearntIt()
|
||||
{
|
||||
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()!;
|
||||
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 } });
|
||||
Assert.Equal(WorkspaceState.InSync, (await sync.PullAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
var hashed = (await fx.Db.Frames.ListBySequenceAsync(fx.Sequence.Id))[2].Sha256!;
|
||||
Assert.Contains(hashed, (await fx.Home.HeadAsync(id, fx.Ct)).SettingsJson);
|
||||
|
||||
// Media that have not learnt the hash (copied so, the home out of reach since): the base still holds the working state.
|
||||
project.WriteMedia(copied);
|
||||
var status = await sync.StatusAsync(id, cancellationToken: fx.Ct);
|
||||
Assert.Equal((WorkspaceState.InSync, false), (status.State, status.Modified));
|
||||
Assert.Null(await sync.CommitAsync(id, cancellationToken: fx.Ct));
|
||||
Assert.Empty(sync.Unpushed(id));
|
||||
|
||||
// A change of the user's own still reads as one, and its version keeps the exclusion.
|
||||
await EditAsync(sync, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.9 } }, fx.Ct);
|
||||
Assert.True((await sync.StatusAsync(id, cancellationToken: fx.Ct)).Modified);
|
||||
var saved = Assert.NotNull(await sync.CommitAsync(id, cancellationToken: fx.Ct));
|
||||
Assert.SequenceEqual([2], ProjectSettings.FromJson(ProjectContent.Place(saved.SettingsJson, copied.Photos)).ExcludedFrames);
|
||||
Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
var settings = await SequenceSettingsAsync(fx);
|
||||
Assert.Equal((0.9, 2), (settings.Exposure.ExposureOffsetEv, settings.ExcludedFrames.Single()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APhotoHashedAtTheHomeIsOutdatedMediaUntilTheMediaLearnItsHash()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
await UnhashAsync(fx, 2);
|
||||
var (_, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var id = project.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);
|
||||
Assert.False((await sync.StatusAsync(id, fx.Home, fx.Ct)).MediaOutdated);
|
||||
await VerifyAsync(fx);
|
||||
await WriteAtHomeAsync(fx, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.7 } });
|
||||
var hashed = (await fx.Db.Frames.ListBySequenceAsync(fx.Sequence.Id))[2].Sha256!;
|
||||
|
||||
// Asked while another operation holds the project: outdated, the media as they were, nothing to save here.
|
||||
WorkspaceStatus busy;
|
||||
using (await project.LockAsync(TimeSpan.FromSeconds(TestTiming.Budget(10)), fx.Ct))
|
||||
busy = await sync.StatusAsync(id, fx.Home, fx.Ct);
|
||||
Assert.True(busy.MediaOutdated);
|
||||
Assert.Equal((WorkspaceState.Behind, false), (busy.State, busy.Modified));
|
||||
Assert.Null(project.ReadMedia()!.Frames[2].Sha256);
|
||||
Assert.False((await sync.StatusAsync(id, cancellationToken: fx.Ct)).MediaOutdated, "without a home nothing is known of it");
|
||||
|
||||
// Asked again with the project free: the media learn the hash, the frames and their files as copied.
|
||||
var learnt = await sync.StatusAsync(id, fx.Home, fx.Ct);
|
||||
Assert.False(learnt.MediaOutdated);
|
||||
Assert.Equal((WorkspaceState.Behind, false), (learnt.State, learnt.Modified));
|
||||
Assert.Equal(hashed, project.ReadMedia()!.Frames[2].Sha256);
|
||||
var pulled = await sync.PullAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal((WorkspaceState.InSync, false), (pulled.Status.State, pulled.Status.MediaOutdated));
|
||||
Assert.SequenceEqual([2], (await sync.OpenAsync(id, fx.Ct)).Working.ExcludedFrames);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProjectHeldForAMomentStillLetsTheMediaLearnTheHomesNames()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
await UnhashAsync(fx, 2);
|
||||
var (_, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var id = project.ProjectId;
|
||||
await VerifyAsync(fx);
|
||||
var hashed = (await fx.Db.Frames.ListBySequenceAsync(fx.Sequence.Id))[2].Sha256!;
|
||||
|
||||
// Another read of the state holds the project for a moment as this one asks: it waits for it, once.
|
||||
var lease = await project.LockAsync(TimeSpan.FromSeconds(TestTiming.Budget(10)), fx.Ct);
|
||||
var release = Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(150, fx.Ct);
|
||||
lease.Dispose();
|
||||
}, fx.Ct);
|
||||
var status = await sync.StatusAsync(id, fx.Home, fx.Ct);
|
||||
await release;
|
||||
Assert.False(status.MediaOutdated);
|
||||
Assert.Equal(hashed, project.ReadMedia()!.Frames[2].Sha256);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APhotoConvertedAtTheHomeIsOutdatedMediaAndTheMediaLearnItsEncodings()
|
||||
{
|
||||
var fx = await FixtureAsync(4);
|
||||
var (store, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var id = project.ProjectId;
|
||||
var copied = project.ReadMedia()!;
|
||||
Assert.Null(copied.Aliases);
|
||||
await ProjectServiceTests.ConvertAsync(fx.Archive, fx.Sequence.Id, [1], 700);
|
||||
var converted = (await fx.Db.Frames.ListBySequenceAsync(fx.Sequence.Id))[1].Sha256!;
|
||||
|
||||
// Opened again: the media learn the encodings the conversion joined, but still hold the photo as copied.
|
||||
await sync.CheckoutAsync(fx.Home, id, cancellationToken: fx.Ct);
|
||||
var link = Assert.NotNull(project.ReadMedia()!.Aliases).Single();
|
||||
Assert.Equal((copied.Frames[1].Sha256!.ToLowerInvariant(), converted.ToLowerInvariant()), (link.Sha256.ToLowerInvariant(), link.Replacement.ToLowerInvariant()));
|
||||
Assert.Equal(copied.Frames[1].Sha256, project.ReadMedia()!.Frames[1].Sha256);
|
||||
var status = await sync.StatusAsync(id, fx.Home, fx.Ct);
|
||||
Assert.True(status.MediaOutdated, "the file is not the library's any more: Update media copies it again");
|
||||
Assert.False(status.Modified);
|
||||
|
||||
await fx.CopyAsync(store, id);
|
||||
Assert.False((await sync.StatusAsync(id, fx.Home, fx.Ct)).MediaOutdated);
|
||||
Assert.Equal(converted, project.ReadMedia()!.Frames[1].Sha256);
|
||||
}
|
||||
|
||||
[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;
|
||||
}
|
||||
@@ -0,0 +1,588 @@
|
||||
using System.Security.Cryptography;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Localization;
|
||||
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>
|
||||
/// The workspace survives a cut at every step (owner review 2, R8): a push, a replay and a save-both stopped between two
|
||||
/// writes lose no version and make no false conflict, and the next sync finishes them; the files it writes are whole.
|
||||
/// </summary>
|
||||
[Category("workspace")]
|
||||
public abstract class WorkspaceRecoveryTests(TestContext context) : DatabaseTest(context)
|
||||
{
|
||||
private async Task<WorkspaceFixture> FixtureAsync(int frames = 5, Func<ArchiveServiceOptions, ArchiveServiceOptions>? configure = null)
|
||||
{
|
||||
var archive = Own(await ArchiveFixture.CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true), configure: configure));
|
||||
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);
|
||||
}
|
||||
|
||||
private static ProjectSettings Offset(ProjectSettings s, double ev) => s with { Exposure = s.Exposure with { ExposureOffsetEv = ev } };
|
||||
|
||||
private static ProjectSettings Settings(ProjectVersion version) => ProjectSettings.FromJson(version.SettingsJson);
|
||||
|
||||
private static async Task<List<string?>> NotesAsync(WorkspaceFixture fx, Guid projectId) =>
|
||||
[.. (await fx.Home.HistoryAsync(projectId, null, 50, fx.Ct)).OrderBy(v => v.Number).Select(v => v.Note)];
|
||||
|
||||
[Test]
|
||||
public async Task APushCutAfterTheBaseMovedLosesNoVersionAndMakesNoFalseConflict()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (store, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var id = project.ProjectId;
|
||||
await EditAsync(sync, id, s => Offset(s, 0.5), fx.Ct);
|
||||
var first = (await sync.CommitAsync(id, "first", cancellationToken: fx.Ct))!;
|
||||
await EditAsync(sync, id, s => Offset(s, 0.8), fx.Ct);
|
||||
var second = (await sync.CommitAsync(id, "second", cancellationToken: fx.Ct))!;
|
||||
|
||||
// Stored at the home, the base moved onto it, then the program died: 'second' still waits after it.
|
||||
await Assert.ThrowsAsync<CutException>(() => WorkspaceFixture.Cutting(store, "push.base").PushAsync(id, fx.Home, fx.Ct));
|
||||
var status = await sync.StatusAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal((WorkspaceState.Ahead, 1, 0, first.Id), (status.State, status.Ahead, status.Behind, status.Base!.VersionId));
|
||||
Assert.Equal(second.Id, sync.Unpushed(id).Single().Id);
|
||||
|
||||
// The next sync, as the app runs it (pull, then push): no conflict, both versions at the home, the local file adopted.
|
||||
Assert.Null((await sync.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
|
||||
Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
Assert.SequenceEqual(["first", "second"], (await NotesAsync(fx, id)).Skip(1));
|
||||
Assert.Equal(0.8, Settings(await fx.Home.HeadAsync(id, fx.Ct)).Exposure.ExposureOffsetEv);
|
||||
Assert.True(project.Versions().Where(v => v.Id == first.Id).All(v => v.StoredAt is not null), "the local file reads as sent");
|
||||
Assert.Empty(project.Versions().Where(v => v.StoredAt is null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APushWhoseAnswerWasLostIsFinishedByTheNextSync()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (_, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var id = project.ProjectId;
|
||||
await EditAsync(sync, id, s => Offset(s, 0.5), fx.Ct);
|
||||
await sync.CommitAsync(id, "first", cancellationToken: fx.Ct);
|
||||
await EditAsync(sync, id, s => Offset(s, 0.8), fx.Ct);
|
||||
await sync.CommitAsync(id, "second", cancellationToken: fx.Ct);
|
||||
|
||||
await Assert.ThrowsAsync<HomeUnavailableException>(() => sync.PushAsync(id, new CuttingHome(fx.Home) { LoseAnswer = 1 }, fx.Ct));
|
||||
Assert.Equal(2, sync.Unpushed(id).Count);
|
||||
Assert.Null((await sync.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
|
||||
Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
Assert.SequenceEqual(["first", "second"], (await NotesAsync(fx, id)).Skip(1));
|
||||
Assert.Equal(0.8, Settings(await fx.Home.HeadAsync(id, fx.Ct)).Exposure.ExposureOffsetEv);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AReplayCutAtAnyStepLosesNoVersionEvenWhenTheHomeMovesAgain()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var cuts = new[] { ("replay.journal", 1), ("replay.version", 1), ("replay.version", 2), ("replay.base", 1) };
|
||||
foreach (var (step, occurrence) in cuts)
|
||||
{
|
||||
var label = $"{step} #{occurrence}";
|
||||
var id = (await fx.Archive.Service.Projects.CreateAsync(fx.Sequence.Id, $"Cut {step} {occurrence}", cancellationToken: fx.Ct)).Id;
|
||||
var (store, one) = fx.Workspace("one");
|
||||
var (_, two) = fx.Workspace("two");
|
||||
var project = await fx.CheckoutAsync(one, id);
|
||||
await fx.CheckoutAsync(two, id);
|
||||
await EditAsync(two, id, s => s with { Exposure = s.Exposure with { DeflickerStrength = new Auto<double>().WithManual(0.6) } }, fx.Ct);
|
||||
await two.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
await two.PushAsync(id, fx.Home, fx.Ct);
|
||||
await EditAsync(one, id, s => Offset(s, 0.3), fx.Ct);
|
||||
await one.CommitAsync(id, "offset", cancellationToken: fx.Ct);
|
||||
await EditAsync(one, id, s => s with { ExcludedFrames = [3], Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
|
||||
await one.CommitAsync(id, "stack", cancellationToken: fx.Ct);
|
||||
|
||||
// The push meets the home moved: the replay is cut, then the home moves again before this PC syncs.
|
||||
await Assert.ThrowsAsync<CutException>(() => WorkspaceFixture.Cutting(store, step, occurrence).PushAsync(id, fx.Home, fx.Ct));
|
||||
Assert.Equal(2, one.Unpushed(id).Count, label);
|
||||
await two.PullAsync(id, fx.Home, fx.Ct);
|
||||
await EditAsync(two, id, s => s with { Exposure = s.Exposure with { DeflickerStrength = new Auto<double>().WithManual(0.7) } }, fx.Ct);
|
||||
await two.CommitAsync(id, "again", cancellationToken: fx.Ct);
|
||||
await two.PushAsync(id, fx.Home, fx.Ct);
|
||||
|
||||
Assert.Null((await one.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict, label);
|
||||
Assert.Equal(WorkspaceState.InSync, (await one.PushAsync(id, fx.Home, fx.Ct)).Status.State, label);
|
||||
var notes = await NotesAsync(fx, id);
|
||||
Assert.Equal((1, 1), (notes.Count(n => n == "offset"), notes.Count(n => n == "stack")), label);
|
||||
var head = ProjectSettings.FromJson(ProjectContent.Place((await fx.Home.HeadAsync(id, fx.Ct)).SettingsJson, ProjectServiceTests.Photos(fx.Frames)));
|
||||
Assert.Equal((0.7, 0.3, StackSetting.Mean), (head.Exposure.DeflickerStrength.Resolve(1), head.Exposure.ExposureOffsetEv, head.Stack.Mode), label);
|
||||
Assert.SequenceEqual([3], head.ExcludedFrames, label);
|
||||
Assert.Null(project.ReadSync().Replay, label);
|
||||
Assert.Empty(project.Versions().Where(v => v.StoredAt is null));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AConflictMetPastAVersionTheHomeHeldKeepsTheVersionsAfterIt()
|
||||
{
|
||||
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);
|
||||
var first = (await one.CommitAsync(id, "first", cancellationToken: fx.Ct))!;
|
||||
await EditAsync(one, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
|
||||
var second = (await one.CommitAsync(id, "second", cancellationToken: fx.Ct))!;
|
||||
// 'first' is stored, its answer lost; another PC builds on it.
|
||||
await Assert.ThrowsAsync<HomeUnavailableException>(() => one.PushAsync(id, new CuttingHome(fx.Home) { LoseAnswer = 1 }, fx.Ct));
|
||||
await two.PullAsync(id, fx.Home, fx.Ct);
|
||||
await EditAsync(two, id, s => Offset(s, 1.0), fx.Ct);
|
||||
await two.CommitAsync(id, "other", cancellationToken: fx.Ct);
|
||||
await two.PushAsync(id, fx.Home, fx.Ct);
|
||||
|
||||
// This PC changed the offset too, without saving a version: a real conflict, met past 'first'.
|
||||
await EditAsync(one, id, s => Offset(s, 2.0), fx.Ct);
|
||||
var stopped = await one.PullAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Conflict, stopped.Status.State);
|
||||
Assert.Equal((first.Id, 1), (stopped.Status.Base!.VersionId, stopped.Status.Ahead));
|
||||
Assert.Equal(second.Id, one.Unpushed(id).Single().Id);
|
||||
Assert.Equal("exposure.exposureOffsetEv", stopped.Status.Conflict!.Differences.Single().Path);
|
||||
|
||||
// The server's offset; 'second''s own change, which nobody else made, reaches the home.
|
||||
await one.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, cancellationToken: fx.Ct);
|
||||
Assert.Equal(WorkspaceState.InSync, (await one.PushAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
var head = Settings(await fx.Home.HeadAsync(id, fx.Ct));
|
||||
Assert.Equal((1.0, StackSetting.Mean), (head.Exposure.ExposureOffsetEv, head.Stack.Mode));
|
||||
// The unsaved offset taken over is kept aside, never sent: the home holds exactly these versions, in order.
|
||||
Assert.SequenceEqual(["first", "other", "second"], (await NotesAsync(fx, id)).Skip(1));
|
||||
Assert.Contains(Directory.GetFiles(Path.Combine(one.Store.Project(id).VersionsDirectory, WorkspaceProject.ReplacedFolder)),
|
||||
f => ProjectSidecars.ReadVersion(f) is { Note: null } saved && Settings(saved).Exposure.ExposureOffsetEv == 2.0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A conflict this workspace stopped at: Main moved at the home (offset 0.5), and <c>three</c> saved "bright" (offset 2)
|
||||
/// and then changed the stack without saving a version.
|
||||
/// </summary>
|
||||
private static async Task<(WorkspaceSync Three, WorkspaceStore Store, WorkspaceProject Project, Guid Id)> ConflictAsync(WorkspaceFixture fx)
|
||||
{
|
||||
var (_, one) = fx.Workspace("one");
|
||||
var (store, three) = fx.Workspace("three");
|
||||
var id = (await fx.CheckoutAsync(one)).ProjectId;
|
||||
var project = await fx.CheckoutAsync(three, 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(three, id, s => Offset(s, 2), fx.Ct);
|
||||
await three.CommitAsync(id, "bright", cancellationToken: fx.Ct);
|
||||
await EditAsync(three, id, s => s with { Stack = new StackSettings { Mode = StackSetting.StarTrails } }, fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Conflict, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
return (three, store, project, id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASaveBothRetryMakesANewCopyWhenSomebodySentAVersionToTheHalfMadeOne()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (three, _, project, id) = await ConflictAsync(fx);
|
||||
await Assert.ThrowsAsync<HomeUnavailableException>(() =>
|
||||
three.ResolveAsync(id, new CuttingHome(fx.Home) { Refuse = 2 }, ConflictChoice.SaveBoth, saveBothName: "Main (PC)", cancellationToken: fx.Ct));
|
||||
var partial = (await fx.Home.ListAsync(fx.Ct)).Single(p => p.Name == "Main (PC)");
|
||||
|
||||
// Another PC opens the half-made copy and sends a version to it: it is theirs now.
|
||||
var (_, two) = fx.Workspace("two");
|
||||
await fx.CheckoutAsync(two, partial.Id);
|
||||
await EditAsync(two, partial.Id, s => Offset(s, 4), fx.Ct);
|
||||
await two.CommitAsync(partial.Id, "theirs", cancellationToken: fx.Ct);
|
||||
await two.PushAsync(partial.Id, fx.Home, fx.Ct);
|
||||
var taken = await fx.Home.HeadAsync(partial.Id, fx.Ct);
|
||||
|
||||
// The retry leaves it as it is and sends every version of this workspace to a new copy.
|
||||
var both = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, saveBothName: "Main (other)", cancellationToken: fx.Ct);
|
||||
var copy = Assert.NotNull(both.SavedAs);
|
||||
Assert.NotEqual(partial.Id, copy.Id);
|
||||
Assert.Equal("Main (other)", copy.Name);
|
||||
var kept = Settings(await fx.Home.HeadAsync(copy.Id, fx.Ct));
|
||||
Assert.Equal((2, StackSetting.StarTrails), (kept.Exposure.ExposureOffsetEv, kept.Stack.Mode));
|
||||
Assert.SequenceEqual(["bright", null], (await NotesAsync(fx, copy.Id)).Skip(1));
|
||||
Assert.Equal(taken.Id, (await fx.Home.HeadAsync(partial.Id, fx.Ct)).Id);
|
||||
Assert.Equal(WorkspaceState.InSync, both.Status.State);
|
||||
Assert.Equal(WorkspaceSyncState.None, project.ReadSync());
|
||||
Assert.Empty(project.Versions().Where(v => v.StoredAt is null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task KeepMineAfterASaveBothCutShortFinishesItsCopy()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (three, _, project, id) = await ConflictAsync(fx);
|
||||
await Assert.ThrowsAsync<HomeUnavailableException>(() =>
|
||||
three.ResolveAsync(id, new CuttingHome(fx.Home) { Refuse = 2 }, ConflictChoice.SaveBoth, saveBothName: "Main (PC)", cancellationToken: fx.Ct));
|
||||
var partial = (await fx.Home.ListAsync(fx.Ct)).Single(p => p.Name == "Main (PC)");
|
||||
Assert.Equal(2, (await fx.Home.HistoryAsync(partial.Id, null, 10, fx.Ct)).Count);
|
||||
|
||||
// The user keeps theirs instead: the copy the cut left half-made gets the rest, and this project goes on.
|
||||
var resolved = await three.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, cancellationToken: fx.Ct);
|
||||
Assert.Null(resolved.Status.Conflict);
|
||||
Assert.Null(project.ReadSync().SaveBoth);
|
||||
var copied = Settings(await fx.Home.HeadAsync(partial.Id, fx.Ct));
|
||||
Assert.Equal((2, StackSetting.StarTrails), (copied.Exposure.ExposureOffsetEv, copied.Stack.Mode));
|
||||
Assert.Equal(3, (await fx.Home.HistoryAsync(partial.Id, null, 10, fx.Ct)).Count);
|
||||
Assert.Equal(WorkspaceState.InSync, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
var main = Settings(await fx.Home.HeadAsync(id, fx.Ct));
|
||||
Assert.Equal((2, StackSetting.StarTrails), (main.Exposure.ExposureOffsetEv, main.Stack.Mode));
|
||||
Assert.Equal(2, (await fx.Home.ListAsync(fx.Ct)).Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AConflictStaysWhileTheHomeHoldsNoVersionAfterTheBase()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (three, _, project, id) = await ConflictAsync(fx);
|
||||
var recorded = project.ReadSync().Conflict!;
|
||||
|
||||
// The home answers it has nothing after the base (its index taken back from an older backup): nothing settles the conflict.
|
||||
var forgetful = new CuttingHome(fx.Home) { Forgets = true };
|
||||
Assert.Equal(WorkspaceState.Conflict, (await three.PullAsync(id, forgetful, fx.Ct)).Status.State);
|
||||
Assert.Equal(WorkspaceState.Conflict, (await three.ResolveAsync(id, forgetful, ConflictChoice.KeepMine, cancellationToken: fx.Ct)).Status.State);
|
||||
Assert.Equal(WorkspaceState.Conflict, (await three.ResolveAsync(id, forgetful, ConflictChoice.TakeServer, cancellationToken: fx.Ct)).Status.State);
|
||||
Assert.Equal(recorded.TheirsVersionId, project.ReadSync().Conflict!.TheirsVersionId);
|
||||
// Taking the server's saves nothing first, so the refused take left the versions as they were.
|
||||
Assert.Equal("bright", three.Unpushed(id).Single().Note);
|
||||
|
||||
// Back as it was, the home settles it as the user chooses; the unsaved stack change stays in the working state.
|
||||
Assert.True((await three.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, cancellationToken: fx.Ct)).Status.Modified);
|
||||
await three.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
Assert.Equal(WorkspaceState.InSync, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
var head = Settings(await fx.Home.HeadAsync(id, fx.Ct));
|
||||
Assert.Equal((2, StackSetting.StarTrails), (head.Exposure.ExposureOffsetEv, head.Stack.Mode));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASaveBothAfterAConflictMetPastAVersionTheHomeHeldSendsEveryVersion()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (store, one) = fx.Workspace("one");
|
||||
var (_, two) = fx.Workspace("two");
|
||||
var project = await fx.CheckoutAsync(one);
|
||||
var id = project.ProjectId;
|
||||
await fx.CheckoutAsync(two, id);
|
||||
await EditAsync(one, id, s => Offset(s, 0.5), fx.Ct);
|
||||
var first = (await one.CommitAsync(id, "first", cancellationToken: fx.Ct))!;
|
||||
await EditAsync(one, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
|
||||
await one.CommitAsync(id, "second", cancellationToken: fx.Ct);
|
||||
await Assert.ThrowsAsync<HomeUnavailableException>(() => one.PushAsync(id, new CuttingHome(fx.Home) { LoseAnswer = 1 }, fx.Ct));
|
||||
await two.PullAsync(id, fx.Home, fx.Ct);
|
||||
await EditAsync(two, id, s => Offset(s, 1.0), fx.Ct);
|
||||
await two.CommitAsync(id, "other", cancellationToken: fx.Ct);
|
||||
await two.PushAsync(id, fx.Home, fx.Ct);
|
||||
await EditAsync(one, id, s => Offset(s, 2.0), fx.Ct);
|
||||
var stopped = await one.PullAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal((WorkspaceState.Conflict, first.Id), (stopped.Status.State, stopped.Status.Conflict!.BaseVersionId));
|
||||
Assert.Equal(first.Id, stopped.Status.Base!.VersionId);
|
||||
|
||||
// Save both, cut once this project moved onto the head: the next sync finishes it and nothing of this workspace's is lost.
|
||||
await Assert.ThrowsAsync<CutException>(() => WorkspaceFixture.Cutting(store, "saveboth.base").ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth,
|
||||
cancellationToken: fx.Ct));
|
||||
var settled = await one.PushAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal((WorkspaceState.InSync, 0), (settled.Status.State, settled.Status.Ahead));
|
||||
var copy = (await fx.Home.ListAsync(fx.Ct)).Single(p => p.Name == "Main (2)");
|
||||
Assert.SequenceEqual(["second", null], (await NotesAsync(fx, copy.Id)).Skip(1));
|
||||
var kept = Settings(await fx.Home.HeadAsync(copy.Id, fx.Ct));
|
||||
Assert.Equal((2.0, StackSetting.Mean), (kept.Exposure.ExposureOffsetEv, kept.Stack.Mode));
|
||||
Assert.Equal(1.0, (await one.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv);
|
||||
Assert.Equal(WorkspaceSyncState.None, project.ReadSync());
|
||||
Assert.Empty(project.Versions().Where(v => v.StoredAt is null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AConflictOfFormatMetPastAVersionTheHomeHeldMovesTheBaseOntoIt()
|
||||
{
|
||||
var fx = await FixtureAsync(3);
|
||||
var (_, one) = fx.Workspace("one");
|
||||
var project = await fx.CheckoutAsync(one);
|
||||
var id = project.ProjectId;
|
||||
await EditAsync(one, id, s => Offset(s, 0.5), fx.Ct);
|
||||
var first = (await one.CommitAsync(id, "first", cancellationToken: fx.Ct))!;
|
||||
await EditAsync(one, id, s => s with { ExcludedFrames = [1] }, fx.Ct);
|
||||
await one.CommitAsync(id, "second", cancellationToken: fx.Ct);
|
||||
await Assert.ThrowsAsync<HomeUnavailableException>(() => one.PushAsync(id, new CuttingHome(fx.Home) { LoseAnswer = 1 }, fx.Ct));
|
||||
var newer = NewerChronos.Push(fx.Archive, fx.Sequence, await fx.Home.HeadAsync(id, fx.Ct));
|
||||
|
||||
// The conflict, the base and the chain name one version: the one the home held.
|
||||
var stopped = await one.PullAsync(id, fx.Home, fx.Ct);
|
||||
Assert.True(stopped.Status.Conflict!.NewerFormat);
|
||||
Assert.Equal((first.Id, first.Id, 1), (stopped.Status.Base!.VersionId, stopped.Status.Conflict.BaseVersionId, stopped.Status.Ahead));
|
||||
Assert.True(project.Versions().Single(v => v.Id == first.Id).StoredAt is not null, "its file took the home's copy");
|
||||
|
||||
var both = await one.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct);
|
||||
var copy = Assert.NotNull(both.SavedAs);
|
||||
Assert.SequenceEqual(["second"], (await NotesAsync(fx, copy.Id)).Skip(1));
|
||||
var kept = ProjectSettings.FromJson(ProjectContent.Place((await fx.Home.HeadAsync(copy.Id, fx.Ct)).SettingsJson, ProjectServiceTests.Photos(fx.Frames)));
|
||||
Assert.Equal(0.5, kept.Exposure.ExposureOffsetEv);
|
||||
Assert.SequenceEqual([1], kept.ExcludedFrames);
|
||||
Assert.Equal((newer.Id, true), (both.Status.Base!.VersionId, both.Status.ReadOnly));
|
||||
Assert.Empty(project.Versions().Where(v => v.StoredAt is null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AVersionFileThatCannotBeReadStopsTheSyncAndTakesTheHomesCopyWhenItHasOne()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (_, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var id = project.ProjectId;
|
||||
await EditAsync(sync, id, s => Offset(s, 0.5), fx.Ct);
|
||||
var first = (await sync.CommitAsync(id, "first", cancellationToken: fx.Ct))!;
|
||||
await EditAsync(sync, id, s => Offset(s, 0.8), fx.Ct);
|
||||
var second = (await sync.CommitAsync(id, "second", cancellationToken: fx.Ct))!;
|
||||
await EditAsync(sync, id, s => Offset(s, 0.9), fx.Ct);
|
||||
|
||||
// A version saved here and never sent cannot be read: shown, and nothing is saved, sent or taken past it.
|
||||
var path = project.VersionPath(second);
|
||||
var bytes = File.ReadAllBytes(path);
|
||||
File.WriteAllText(path, "{ damaged");
|
||||
var status = await sync.StatusAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal((WorkspaceState.Damaged, 1), (status.State, status.Damaged));
|
||||
await Assert.ThrowsAsync<InvalidDataException>(() => sync.CommitAsync(id, cancellationToken: fx.Ct));
|
||||
await Assert.ThrowsAsync<InvalidDataException>(() => sync.PushAsync(id, fx.Home, fx.Ct));
|
||||
await Assert.ThrowsAsync<InvalidDataException>(() => sync.PullAsync(id, fx.Home, fx.Ct));
|
||||
Assert.Equal(1, (await fx.Home.HistoryAsync(id, null, 10, fx.Ct)).Count);
|
||||
Assert.True(File.Exists(path));
|
||||
|
||||
// Readable again (it was held open, say), it goes on as if nothing happened.
|
||||
File.WriteAllBytes(path, bytes);
|
||||
Assert.Equal(WorkspaceState.Ahead, (await sync.StatusAsync(id, fx.Home, fx.Ct)).State);
|
||||
|
||||
// One the home stored (its answer lost) takes the home's copy at the next sync; the damaged file is kept aside.
|
||||
await Assert.ThrowsAsync<HomeUnavailableException>(() => sync.PushAsync(id, new CuttingHome(fx.Home) { LoseAnswer = 1 }, fx.Ct));
|
||||
File.WriteAllText(project.VersionPath(first), "{ damaged");
|
||||
Assert.Equal((WorkspaceState.Damaged, 2), ((await sync.StatusAsync(id, fx.Home, fx.Ct)).State, (await sync.StatusAsync(id, null, fx.Ct)).Damaged));
|
||||
Assert.Null((await sync.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
|
||||
await sync.CommitAsync(id, "third", cancellationToken: fx.Ct);
|
||||
Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
Assert.SequenceEqual(["first", "second", "third"], (await NotesAsync(fx, id)).Skip(1));
|
||||
Assert.Contains(Directory.GetFiles(Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedFolder)),
|
||||
f => File.ReadAllText(f) == "{ damaged");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AVersionTheHomeFindsTooLargeIsRefusedAsSuchAndWaits()
|
||||
{
|
||||
var fx = await FixtureAsync(3);
|
||||
var (_, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var id = project.ProjectId;
|
||||
await EditAsync(sync, id, s => Offset(s, 0.5), fx.Ct);
|
||||
await sync.CommitAsync(id, "large", cancellationToken: fx.Ct);
|
||||
var refused = await Assert.ThrowsAsync<WorkspaceSyncException>(() => sync.PushAsync(id, new CuttingHome(fx.Home) { Answer = PushOutcome.TooLarge }, fx.Ct));
|
||||
Assert.Equal(PushOutcome.TooLarge, refused.Outcome);
|
||||
Assert.Equal("large", sync.Unpushed(id).Single().Note);
|
||||
Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ACopyThatFailsLeavesTheEditorsPhotosInPlace()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (store, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var before = project.ReadMedia()!;
|
||||
var frames = before.Frames.OrderBy(f => f.Index).ToList();
|
||||
var editors = project.MediaFile(frames[0].File!);
|
||||
// The first frame's original is gone at the home, and the copy then fails on the second.
|
||||
File.Delete(fx.Archive.Paths.FromRelative(fx.Frames.Single(f => f.Id == frames[0].Id).RelativePath));
|
||||
var blocked = project.MediaFile(frames[1].File!);
|
||||
File.Delete(blocked);
|
||||
Directory.CreateDirectory(blocked);
|
||||
|
||||
await fx.CopyAsync(store, project.ProjectId, JobState.Failed);
|
||||
Assert.True(File.Exists(editors), "the editor's photo of the first frame stays while the copy has not committed");
|
||||
Assert.Equal(frames[0].File, project.ReadMedia()!.Frames.Single(f => f.Id == frames[0].Id).File);
|
||||
|
||||
// A copy that commits without it removes it then.
|
||||
Directory.Delete(blocked);
|
||||
await fx.CopyAsync(store, project.ProjectId);
|
||||
Assert.Null(project.ReadMedia()!.Frames.Single(f => f.Id == frames[0].Id).File);
|
||||
Assert.False(File.Exists(editors));
|
||||
Assert.True(File.Exists(blocked));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASaveBothCutShortIsFinishedIntoTheSameCopy()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (_, one) = fx.Workspace("one");
|
||||
var (store, three) = fx.Workspace("three");
|
||||
var id = (await fx.CheckoutAsync(one)).ProjectId;
|
||||
var project = await fx.CheckoutAsync(three, 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(three, id, s => Offset(s, 2), fx.Ct);
|
||||
await three.CommitAsync(id, "bright", cancellationToken: fx.Ct);
|
||||
await EditAsync(three, id, s => s with { Stack = new StackSettings { Mode = StackSetting.StarTrails } }, fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Conflict, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
|
||||
// The copy is made and the first version sent, then the home goes out of reach.
|
||||
var cut = new CuttingHome(fx.Home) { Refuse = 2 };
|
||||
await Assert.ThrowsAsync<HomeUnavailableException>(() => three.ResolveAsync(id, cut, ConflictChoice.SaveBoth, saveBothName: "Main (PC)", cancellationToken: fx.Ct));
|
||||
var partial = (await fx.Home.ListAsync(fx.Ct)).Single(p => p.Name == "Main (PC)");
|
||||
Assert.Equal(WorkspaceState.Conflict, (await three.StatusAsync(id, fx.Home, fx.Ct)).State);
|
||||
|
||||
// The retry, asked another name as the app asks once the first is taken, sends the rest to the same copy.
|
||||
var both = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, saveBothName: "Main (other)", cancellationToken: fx.Ct);
|
||||
Assert.Equal(partial.Id, both.SavedAs!.Id);
|
||||
Assert.Equal(2, (await fx.Home.ListAsync(fx.Ct)).Count);
|
||||
var kept = Settings(await fx.Home.HeadAsync(partial.Id, fx.Ct));
|
||||
Assert.Equal((2, StackSetting.StarTrails), (kept.Exposure.ExposureOffsetEv, kept.Stack.Mode));
|
||||
Assert.Equal(3, (await fx.Home.HistoryAsync(partial.Id, null, 10, fx.Ct)).Count);
|
||||
Assert.Equal(WorkspaceState.InSync, both.Status.State);
|
||||
Assert.Equal(WorkspaceSyncState.None, project.ReadSync());
|
||||
|
||||
// Cut once this project moved onto the home's head: the next sync finishes it, the conflict settled.
|
||||
await EditAsync(one, id, s => Offset(s, 0.7), fx.Ct);
|
||||
await one.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
await one.PushAsync(id, fx.Home, fx.Ct);
|
||||
await EditAsync(three, id, s => Offset(s, 3), fx.Ct);
|
||||
await three.CommitAsync(id, "brighter", cancellationToken: fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Conflict, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
await Assert.ThrowsAsync<CutException>(() => WorkspaceFixture.Cutting(store, "saveboth.base").ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct));
|
||||
var settled = await three.PushAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal((WorkspaceState.InSync, 0), (settled.Status.State, settled.Status.Ahead));
|
||||
Assert.Equal(0.7, (await three.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv);
|
||||
var second = (await fx.Home.ListAsync(fx.Ct)).Single(p => p.Name == "Main (2)");
|
||||
Assert.Equal(3, Settings(await fx.Home.HeadAsync(second.Id, fx.Ct)).Exposure.ExposureOffsetEv);
|
||||
Assert.Equal(WorkspaceSyncState.None, project.ReadSync());
|
||||
Assert.Empty(project.Versions().Where(v => v.StoredAt is null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProjectWithTwentyThousandParkedExclusionsIsCheckedOutEditedAndSent()
|
||||
{
|
||||
var fx = await FixtureAsync(3);
|
||||
var projects = fx.Archive.Service.Projects;
|
||||
var large = await projects.CreateAsync(fx.Sequence.Id, "Large", cancellationToken: fx.Ct);
|
||||
var head = await projects.HeadAsync(large.Id, fx.Ct);
|
||||
// Exclusions of photos this sequence does not hold (taken from another copy): they wait, parked by photo.
|
||||
var pending = Enumerable.Range(0, 20_000).Select(i => new PendingFrame
|
||||
{
|
||||
Sha256 = Convert.ToHexStringLower(SHA256.HashData(BitConverter.GetBytes(i))),
|
||||
Size = 1_000_000 + i,
|
||||
CapturedAt = new DateTime(2024, 1, 1).AddMinutes(i),
|
||||
Excluded = true,
|
||||
}).ToList();
|
||||
var portable = ProjectContent.Canonical((Settings(head) with { PendingFrames = pending }).ToJson());
|
||||
var draft = head with
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
ParentId = head.Id,
|
||||
Number = head.Number + 1,
|
||||
Kind = ProjectVersionKind.Commit,
|
||||
SettingsJson = portable,
|
||||
ContentHash = ProjectContent.Hash(portable),
|
||||
StoredAt = null,
|
||||
};
|
||||
Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(large.Id, draft, fx.Ct)).Outcome);
|
||||
|
||||
var (_, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync, large.Id);
|
||||
Assert.True(new FileInfo(project.BundlePath).Length > 4 << 20, "larger than a project file could be before");
|
||||
var session = await sync.OpenAsync(large.Id, fx.Ct);
|
||||
Assert.Equal(20_000, session.Working.PendingFrames!.Count);
|
||||
await sync.SaveAsync(large.Id, Offset(session.Working, 0.2), fx.Ct);
|
||||
Assert.NotNull(await sync.CommitAsync(large.Id, cancellationToken: fx.Ct));
|
||||
Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(large.Id, fx.Home, fx.Ct)).Status.State);
|
||||
Assert.Equal(20_000, Settings(await fx.Home.HeadAsync(large.Id, fx.Ct)).PendingFrames!.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheCopyStopsBeforeItFillsTheWorkspaceDrive()
|
||||
{
|
||||
var fx = await FixtureAsync(3, options => options with { FreeSpace = _ => 1 << 20 });
|
||||
var (store, sync) = fx.Workspace();
|
||||
var main = await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct);
|
||||
var project = await sync.CheckoutAsync(fx.Home, main.Id, cancellationToken: fx.Ct);
|
||||
var job = await fx.CopyAsync(store, main.Id, JobState.Failed);
|
||||
var parts = Loc.L("Errors.Workspace.DiskFull", "\u0001").Split('\u0001');
|
||||
Assert.True(job.Error!.StartsWith(parts[0], StringComparison.Ordinal) && job.Error.EndsWith(parts[1], StringComparison.Ordinal), job.Error);
|
||||
Assert.Null(project.ReadMedia());
|
||||
Assert.Empty(Directory.GetFiles(project.ProxiesDirectory));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ACopyResumedAfterTheSequenceMovedChecksEveryFileAgain()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (store, sync) = fx.Workspace();
|
||||
var main = await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct);
|
||||
var project = await sync.CheckoutAsync(fx.Home, main.Id, cancellationToken: fx.Ct);
|
||||
await fx.Archive.Service.Images.BuildProxiesAsync(fx.Archive.Archive, fx.Sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
await fx.CopyAsync(store, main.Id);
|
||||
var copied = project.ReadMedia()!;
|
||||
var damaged = project.MediaFile(copied.Frames[0].File!);
|
||||
|
||||
// A run of the same frames in the same folder keeps what it had copied.
|
||||
File.WriteAllText(damaged, "not a photo");
|
||||
var job = await fx.CopyAsync(store, main.Id, checkpoint: $"copy:{copied.Frames.Count}:{fx.Sequence.FramesVersion}:{fx.Sequence.Folder}");
|
||||
Assert.Contains(Loc.L("Archive.Log.WorkspaceCopied", fx.Sequence.DisplayTitle, 0, 0, 5, 0), job.LogTail!);
|
||||
// One read in another folder (the sequence moved since, another may hold its old one) checks every file again.
|
||||
job = await fx.CopyAsync(store, main.Id, checkpoint: $"copy:{copied.Frames.Count}:{fx.Sequence.FramesVersion}:elsewhere/folder");
|
||||
Assert.Contains(Loc.L("Archive.Log.WorkspaceCopied", fx.Sequence.DisplayTitle, 1, 0, 4, 0), job.LogTail!);
|
||||
// Under a name of its own: the media named the file kept before, which the editor reads until the commit.
|
||||
var mended = project.MediaFile(project.ReadMedia()!.Frames[0].File!);
|
||||
Assert.Equal(copied.Frames[0].FileSha256, Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(mended))));
|
||||
Assert.False(File.Exists(damaged));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheEditorReadsTheWorkingStateAndTheMediaUnderTheLease()
|
||||
{
|
||||
var fx = await FixtureAsync(2);
|
||||
var (store, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var impatient = new WorkspaceSync(store, "Chronos test") { LockWait = TimeSpan.FromMilliseconds(200) };
|
||||
using (await project.LockAsync(TimeSpan.FromSeconds(TestTiming.Budget(5)), fx.Ct))
|
||||
await Assert.ThrowsAsync<LeaseUnavailableException>(() => impatient.OpenAsync(project.ProjectId, fx.Ct));
|
||||
Assert.NotNull((await impatient.OpenAsync(project.ProjectId, fx.Ct)).Sequence);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AManifestLeftUnfinishedIsMadeAgainOnlyWhileTheFolderHoldsNoProject()
|
||||
{
|
||||
var root = Path.Combine(Context.CreateTempDirectory("ws"), "workspace");
|
||||
Directory.CreateDirectory(root);
|
||||
var manifest = Path.Combine(root, WorkspaceStore.ManifestFile);
|
||||
File.WriteAllBytes(manifest, []);
|
||||
var store = new WorkspaceStore(root);
|
||||
Assert.Equal(WorkspaceStore.FormatName, store.Manifest.Format);
|
||||
Assert.Empty(Directory.GetFiles(root, "*.tmp"));
|
||||
Assert.Equal(store.Id, new WorkspaceStore(root).Id);
|
||||
|
||||
File.WriteAllText(manifest, """{ "format": "chronos-wor""");
|
||||
Assert.NotEqual(store.Id, new WorkspaceStore(root).Id);
|
||||
|
||||
// With a project in the folder, a damaged manifest is never written over: it is reported.
|
||||
var project = Path.Combine(root, Guid.CreateVersion7().ToString("N"));
|
||||
Directory.CreateDirectory(project);
|
||||
File.WriteAllText(Path.Combine(project, WorkspaceProject.BundleFile), "{}");
|
||||
File.WriteAllText(manifest, """{ "format": "chronos-wor""");
|
||||
Assert.Throws<InvalidDataException>(() => new WorkspaceStore(root));
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SqliteWorkspaceRecoveryTests(TestContext context) : WorkspaceRecoveryTests(context)
|
||||
{
|
||||
protected override Backend Backend => Backend.SqliteFile;
|
||||
}
|
||||
|
||||
[RequiresEnvironment(PgTestServer.Variable)]
|
||||
public sealed class PostgresWorkspaceRecoveryTests(TestContext context) : WorkspaceRecoveryTests(context)
|
||||
{
|
||||
protected override Backend Backend => Backend.Postgres;
|
||||
}
|
||||
@@ -0,0 +1,855 @@
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Projects;
|
||||
using Chronos.Data.Repositories;
|
||||
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 the workspace's sync and copy never lose or leave behind (owner review 2, R8, fix round 3): a choice cut short is
|
||||
/// applied or asked again, a version a take leaves changing nothing is not sent, a save-both cut short makes one copy, a
|
||||
/// project deleted at the home stops the sync once, the copy never writes over the editor's photo nor leaves half media.
|
||||
/// </summary>
|
||||
[Category("workspace")]
|
||||
public abstract class WorkspaceSafetyTests(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);
|
||||
}
|
||||
|
||||
private static ProjectSettings Offset(ProjectSettings s, double ev) => s with { Exposure = s.Exposure with { ExposureOffsetEv = ev } };
|
||||
|
||||
private static ProjectSettings Settings(ProjectVersion version) => ProjectSettings.FromJson(version.SettingsJson);
|
||||
|
||||
private static async Task<double> WorkingOffsetAsync(WorkspaceSync sync, Guid projectId, CancellationToken ct) =>
|
||||
(await sync.OpenAsync(projectId, ct)).Working.Exposure.ExposureOffsetEv;
|
||||
|
||||
private static List<ProjectVersion> Replaced(WorkspaceProject project)
|
||||
{
|
||||
var aside = Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedFolder);
|
||||
return Directory.Exists(aside) ? [.. Directory.GetFiles(aside).Select(ProjectSidecars.ReadVersion)] : [];
|
||||
}
|
||||
|
||||
private static TimeSpan Wait => TimeSpan.FromSeconds(TestTiming.Budget(10));
|
||||
|
||||
/// <summary>Polls <paramref name="done"/> until it holds, failing after the budget.</summary>
|
||||
private static async Task UntilAsync(Func<bool> done, string what, CancellationToken ct)
|
||||
{
|
||||
var until = DateTime.UtcNow + TimeSpan.FromSeconds(TestTiming.Budget(30));
|
||||
while (!done())
|
||||
{
|
||||
if (DateTime.UtcNow > until) Assert.Fail($"Timed out waiting: {what}");
|
||||
await Task.Delay(20, ct);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A project of its own checked out into two workspaces: the first sent an offset of 0.5, the second changed it to 1
|
||||
/// without saving and was asked. The second's store, sync and project, and the conflict shown.
|
||||
/// </summary>
|
||||
private static async Task<(WorkspaceStore Store, WorkspaceSync Two, WorkspaceProject Project, WorkspaceConflict Shown)> AskedAsync(WorkspaceFixture fx, Guid id)
|
||||
{
|
||||
var (_, one) = fx.Workspace("one");
|
||||
var (store, two) = fx.Workspace("two");
|
||||
await fx.CheckoutAsync(one, id);
|
||||
var project = 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, 1.0), fx.Ct);
|
||||
var shown = Assert.NotNull((await two.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
|
||||
return (store, two, project, shown);
|
||||
}
|
||||
|
||||
private static async Task<Guid> ProjectAsync(WorkspaceFixture fx, string name) =>
|
||||
(await fx.Archive.Service.Projects.CreateAsync(fx.Sequence.Id, name, cancellationToken: fx.Ct)).Id;
|
||||
|
||||
[Test]
|
||||
public async Task ATakeCutPastItsCommitPointIsAppliedAndALaterChoiceFindsNothingToSettle()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var id = await ProjectAsync(fx, "Cut late");
|
||||
var (store, two, project, shown) = await AskedAsync(fx, id);
|
||||
await Assert.ThrowsAsync<CutException>(() =>
|
||||
WorkspaceFixture.Cutting(store, "replay.base").ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: shown, cancellationToken: fx.Ct));
|
||||
// sync.json still records the conflict, but it waits for no choice: what counts it reads it as the status does.
|
||||
Assert.NotNull(project.ReadSync().Conflict);
|
||||
Assert.Null(project.OpenConflict(), "settled past its commit point");
|
||||
|
||||
// The working state is on the library's head with its value: the choice was made, nothing asks it again.
|
||||
var status = await two.StatusAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Null(status.Conflict, "a take past its commit point was applied");
|
||||
Assert.Equal(WorkspaceState.InSync, status.State);
|
||||
Assert.Equal(0.5, await WorkingOffsetAsync(two, id, fx.Ct));
|
||||
Assert.Contains(Replaced(project), v => Settings(v).Exposure.ExposureOffsetEv == 1.0);
|
||||
|
||||
// The dialog still open answers Keep mine: there is nothing left to settle, and nothing is cleared without applying it.
|
||||
var later = await two.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, shown: shown, cancellationToken: fx.Ct);
|
||||
Assert.Null(later.Status.Conflict);
|
||||
Assert.Equal(0.5, await WorkingOffsetAsync(two, id, fx.Ct));
|
||||
Assert.Equal(WorkspaceSyncState.None, project.ReadSync());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ATakeCutBeforeItsCommitPointKeepsTheConflictAndALaterChoiceIsApplied()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var id = await ProjectAsync(fx, "Cut early");
|
||||
var (store, two, project, shown) = await AskedAsync(fx, id);
|
||||
await Assert.ThrowsAsync<CutException>(() =>
|
||||
WorkspaceFixture.Cutting(store, "replay.journal").ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: shown, cancellationToken: fx.Ct));
|
||||
|
||||
var status = await two.StatusAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Conflict, status.State);
|
||||
Assert.Equal(1.0, await WorkingOffsetAsync(two, id, fx.Ct));
|
||||
|
||||
// Keep mine, asked again: applied, and sent.
|
||||
var kept = await two.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, shown: Assert.NotNull(status.Conflict), cancellationToken: fx.Ct);
|
||||
Assert.Null(kept.Status.Conflict);
|
||||
Assert.Equal(1.0, await WorkingOffsetAsync(two, id, fx.Ct));
|
||||
Assert.Null(project.ReadSync().Conflict);
|
||||
await two.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
await two.PushAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal(1.0, Settings(await fx.Home.HeadAsync(id, fx.Ct)).Exposure.ExposureOffsetEv);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AVersionTheTakeLeavesChangingNothingIsNotSentAndStaysAside()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (_, one) = fx.Workspace("one");
|
||||
var (_, two) = fx.Workspace("two");
|
||||
var id = (await fx.CheckoutAsync(one)).ProjectId;
|
||||
var project = 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);
|
||||
// Saved here: an offset and a stack, then the offset alone again.
|
||||
await EditAsync(two, id, s => Offset(s, 0.8) with { Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
|
||||
await two.CommitAsync(id, "stacked", cancellationToken: fx.Ct);
|
||||
await EditAsync(two, id, s => Offset(s, 0.9), fx.Ct);
|
||||
await two.CommitAsync(id, "again", cancellationToken: fx.Ct);
|
||||
var shown = Assert.NotNull((await two.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
|
||||
|
||||
// The library's offset taken: "again" changes nothing on "stacked" replayed, so only that one waits to be sent.
|
||||
var settled = await two.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: shown, cancellationToken: fx.Ct);
|
||||
Assert.Null(settled.Status.Conflict);
|
||||
Assert.Equal("stacked", two.Unpushed(id).Single().Note);
|
||||
Assert.Contains(Replaced(project), v => v.Note == "again");
|
||||
var history = (await fx.Home.HistoryAsync(id, null, 10, fx.Ct)).Count;
|
||||
Assert.Count(1, (await two.PushAsync(id, fx.Home, fx.Ct)).Pushed);
|
||||
Assert.Equal(history + 1, (await fx.Home.HistoryAsync(id, null, 10, fx.Ct)).Count);
|
||||
var head = Settings(await fx.Home.HeadAsync(id, fx.Ct));
|
||||
Assert.Equal((0.5, StackSetting.Mean), (head.Exposure.ExposureOffsetEv, head.Stack.Mode));
|
||||
|
||||
// A take whose every version changes nothing sends none, merge or not.
|
||||
var (_, three) = fx.Workspace("three");
|
||||
await fx.CheckoutAsync(three, id);
|
||||
await EditAsync(one, id, s => Offset(s, 0.6), fx.Ct);
|
||||
await one.PullAsync(id, fx.Home, fx.Ct);
|
||||
await one.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
await one.PushAsync(id, fx.Home, fx.Ct);
|
||||
await EditAsync(three, id, s => Offset(s, 2.0), fx.Ct);
|
||||
await three.CommitAsync(id, "bright", cancellationToken: fx.Ct);
|
||||
var asked = Assert.NotNull((await three.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
|
||||
var taken = await three.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: asked, cancellationToken: fx.Ct);
|
||||
Assert.Equal(WorkspaceState.InSync, taken.Status.State);
|
||||
Assert.Empty(three.Unpushed(id));
|
||||
Assert.Contains(Replaced(three.Store.Project(id)), v => v.Note == "bright");
|
||||
}
|
||||
|
||||
/// <summary>A conflict where this side saved "bright" (offset 2) and has nothing unsaved: the working state is that version.</summary>
|
||||
private static async Task<(WorkspaceStore Store, WorkspaceSync Three, WorkspaceProject Project, Guid Id)> SavedConflictAsync(WorkspaceFixture fx, Guid? projectId = null)
|
||||
{
|
||||
var (_, one) = fx.Workspace("one");
|
||||
var (store, three) = fx.Workspace("three");
|
||||
var id = (await fx.CheckoutAsync(one, projectId)).ProjectId;
|
||||
var project = await fx.CheckoutAsync(three, 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(three, id, s => Offset(s, 2), fx.Ct);
|
||||
await three.CommitAsync(id, "bright", cancellationToken: fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Conflict, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
return (store, three, project, id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASaveBothCutOnceTheHomeMadeTheCopyUsesThatCopyOnRetry()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (store, three, project, id) = await SavedConflictAsync(fx);
|
||||
await Assert.ThrowsAsync<CutException>(() =>
|
||||
WorkspaceFixture.Cutting(store, "saveboth.created").ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, saveBothName: "Main (PC)", cancellationToken: fx.Ct));
|
||||
var made = (await fx.Home.ListAsync(fx.Ct)).Single(p => p.Name == "Main (PC)");
|
||||
Assert.True(Assert.NotNull(project.ReadSync().SaveBoth).Intended, "the copy was asked for under a journal");
|
||||
|
||||
// The retry, asked another name, finds the copy it asked for and makes no other.
|
||||
var both = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, saveBothName: "Main (other)", cancellationToken: fx.Ct);
|
||||
Assert.Equal(made.Id, Assert.NotNull(both.SavedAs).Id);
|
||||
Assert.Equal(2, (await fx.Home.ListAsync(fx.Ct)).Count);
|
||||
Assert.Equal(2.0, Settings(await fx.Home.HeadAsync(made.Id, fx.Ct)).Exposure.ExposureOffsetEv);
|
||||
Assert.Equal(2, (await fx.Home.HistoryAsync(made.Id, null, 10, fx.Ct)).Count);
|
||||
Assert.Equal(WorkspaceState.InSync, both.Status.State);
|
||||
Assert.Equal(WorkspaceSyncState.None, project.ReadSync());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASaveBothCutBeforeTheHomeWasAskedMakesOneCopyOnRetry()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (store, three, project, id) = await SavedConflictAsync(fx);
|
||||
await Assert.ThrowsAsync<CutException>(() =>
|
||||
WorkspaceFixture.Cutting(store, "saveboth.intent").ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, saveBothName: "Main (PC)", cancellationToken: fx.Ct));
|
||||
Assert.Count(1, await fx.Home.ListAsync(fx.Ct));
|
||||
|
||||
var both = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, saveBothName: "Main (PC)", cancellationToken: fx.Ct);
|
||||
Assert.Equal("Main (PC)", Assert.NotNull(both.SavedAs).Name);
|
||||
Assert.Equal(2, (await fx.Home.ListAsync(fx.Ct)).Count);
|
||||
Assert.Equal(WorkspaceSyncState.None, project.ReadSync());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASaveBothKeepsTheRestoreRecorded()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (_, three, project, id) = await SavedConflictAsync(fx);
|
||||
var first = (await fx.Home.HistoryAsync(id, null, 10, fx.Ct)).Single(v => v.Number == 1);
|
||||
project.WriteSync(project.ReadSync() with { RestoredFrom = first.Id });
|
||||
|
||||
var both = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct);
|
||||
Assert.NotNull(both.SavedAs);
|
||||
Assert.Equal(new WorkspaceSyncState(first.Id, null), project.ReadSync());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProjectDeletedAtTheHomeIsGoneAndKeepsEverythingHere()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var id = await ProjectAsync(fx, "Side");
|
||||
var (_, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync, id);
|
||||
await EditAsync(sync, id, s => Offset(s, 0.5), fx.Ct);
|
||||
await sync.CommitAsync(id, "unsent", cancellationToken: fx.Ct);
|
||||
await EditAsync(sync, id, s => Offset(s, 0.9), fx.Ct);
|
||||
await fx.Archive.Service.Projects.DeleteAsync(id, fx.Ct);
|
||||
|
||||
// Every sync answers Gone, without throwing, and moves nothing.
|
||||
var pulled = await sync.PullAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Gone, pulled.Status.State);
|
||||
Assert.Empty(pulled.Pulled);
|
||||
var pushed = await sync.PushAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Gone, pushed.Status.State);
|
||||
Assert.Empty(pushed.Pushed);
|
||||
Assert.Equal(WorkspaceState.Gone, (await sync.StatusAsync(id, fx.Home, fx.Ct)).State);
|
||||
Assert.Equal("unsent", sync.Unpushed(id).Single().Note);
|
||||
Assert.Equal(0.9, await WorkingOffsetAsync(sync, id, fx.Ct));
|
||||
Assert.Equal(WorkspaceSyncState.None, project.ReadSync());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASaveBothOfAProjectDeletedAtTheHomeIsGoneAndARetryMakesNoOtherCopy()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var side = await ProjectAsync(fx, "Side");
|
||||
var (_, three, project, id) = await SavedConflictAsync(fx, side);
|
||||
await EditAsync(three, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
|
||||
await fx.Archive.Service.Projects.DeleteAsync(id, fx.Ct);
|
||||
|
||||
var both = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Gone, both.Status.State);
|
||||
// Gone holds no conflict to answer: the editor says the project is gone, never asks the conflict again.
|
||||
Assert.Null(both.Status.Conflict, "a project the home no longer has asks nothing");
|
||||
var copy = Assert.NotNull(both.SavedAs, "the copy holds this side's work at the home");
|
||||
Assert.NotNull(project.ReadSync().Conflict, "the conflict stays recorded");
|
||||
Assert.Null((await three.StatusAsync(id, fx.Home, fx.Ct)).Conflict);
|
||||
var listed = (await fx.Home.ListAsync(fx.Ct)).Count;
|
||||
var again = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Gone, again.Status.State);
|
||||
Assert.Null(again.Status.Conflict);
|
||||
Assert.Equal(listed, (await fx.Home.ListAsync(fx.Ct)).Count);
|
||||
Assert.Contains(await fx.Home.ListAsync(fx.Ct), p => p.Id == copy.Id);
|
||||
Assert.Equal(WorkspaceState.Gone, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
|
||||
// Nothing of this side's is lost: its versions wait here, the working state as it was.
|
||||
var working = (await three.OpenAsync(id, fx.Ct)).Working;
|
||||
Assert.Equal((2.0, StackSetting.Mean), (working.Exposure.ExposureOffsetEv, working.Stack.Mode));
|
||||
Assert.Contains(project.Versions(), v => v.Note == "bright");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task KeepMineOrTakeTheLibrarysOnAProjectDeletedAtTheHomeIsGoneAndChangesNothing()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var side = await ProjectAsync(fx, "Side");
|
||||
var (_, three, project, id) = await SavedConflictAsync(fx, side);
|
||||
await EditAsync(three, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
|
||||
var shown = Assert.NotNull(project.ReadSync().Conflict);
|
||||
var versions = project.Versions().Select(v => (v.Id, v.ContentHash)).ToList();
|
||||
await fx.Archive.Service.Projects.DeleteAsync(id, fx.Ct);
|
||||
|
||||
// Neither choice is merged onto the last version the home had: Gone, the working state and the unsent version as they were.
|
||||
foreach (var choice in new[] { ConflictChoice.KeepMine, ConflictChoice.TakeServer })
|
||||
{
|
||||
var result = await three.ResolveAsync(id, fx.Home, choice, shown: shown, cancellationToken: fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Gone, result.Status.State, $"{choice}");
|
||||
Assert.Null(result.Status.Conflict);
|
||||
Assert.Null(result.Kept, "nothing was taken over");
|
||||
Assert.Empty(result.Pulled);
|
||||
var working = (await three.OpenAsync(id, fx.Ct)).Working;
|
||||
Assert.Equal((2.0, StackSetting.Mean), (working.Exposure.ExposureOffsetEv, working.Stack.Mode));
|
||||
Assert.Equal("bright", three.Unpushed(id).Single().Note);
|
||||
Assert.SequenceEqual(versions, project.Versions().Select(v => (v.Id, v.ContentHash)));
|
||||
Assert.Empty(Replaced(project));
|
||||
Assert.Equal(shown.TheirsVersionId, Assert.NotNull(project.ReadSync().Conflict).TheirsVersionId, "the conflict stays recorded");
|
||||
}
|
||||
Assert.Equal(WorkspaceState.Gone, (await three.PullAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
Assert.Equal("bright", three.Unpushed(id).Single().Note);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APushStoppedAtAConflictLeavesTheReplacedVersionsAndOneThatEndsWellPrunesThem()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (_, one) = fx.Workspace("one");
|
||||
var (_, three) = fx.Workspace("three");
|
||||
var id = (await fx.CheckoutAsync(one)).ProjectId;
|
||||
var project = await fx.CheckoutAsync(three, 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(three, id, s => Offset(s, 2), fx.Ct);
|
||||
await three.CommitAsync(id, "bright", cancellationToken: fx.Ct);
|
||||
var old = OldReplaced(project, 40);
|
||||
|
||||
var stopped = await three.PushAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Conflict, stopped.Status.State);
|
||||
Assert.FileExists(old);
|
||||
|
||||
await three.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, shown: stopped.Status.Conflict, cancellationToken: fx.Ct);
|
||||
Assert.Count(1, (await three.PushAsync(id, fx.Home, fx.Ct)).Pushed);
|
||||
Assert.False(File.Exists(old), "a push that ended well prunes");
|
||||
}
|
||||
|
||||
/// <summary>A file in <c>versions/.replaced</c> set aside <paramref name="days"/> days ago, by this Chronos (the folder known since then).</summary>
|
||||
private static string OldReplaced(WorkspaceProject project, int days)
|
||||
{
|
||||
var aside = Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedFolder);
|
||||
Directory.CreateDirectory(aside);
|
||||
var then = DateTime.UtcNow - TimeSpan.FromDays(days);
|
||||
File.WriteAllText(Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedSinceFile), then.ToString("O", System.Globalization.CultureInfo.InvariantCulture));
|
||||
var old = Path.Combine(aside, $"1-{Guid.CreateVersion7():N}.json");
|
||||
File.WriteAllText(old, "{}");
|
||||
File.SetLastWriteTimeUtc(old, then);
|
||||
return old;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AMergeIsMarkedOnTheVersionThatCarriesItNeverOnOneTheWorkingStateAloneConflicted()
|
||||
{
|
||||
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);
|
||||
// Saved here: a stack the library never touched; then, not saved, the offset the library changed too.
|
||||
await EditAsync(two, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
|
||||
await two.CommitAsync(id, "stacked", 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);
|
||||
Assert.Equal("exposure.exposureOffsetEv", shown.Differences.Single().Path);
|
||||
|
||||
// Keep mine: the choice lives in the working state; "stacked" replayed merges nothing and stays a commit.
|
||||
var kept = await two.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, shown: shown, cancellationToken: fx.Ct);
|
||||
Assert.Null(kept.Status.Conflict);
|
||||
var stacked = two.Unpushed(id).Single();
|
||||
Assert.Equal(("stacked", ProjectVersionKind.Commit), (stacked.Note, stacked.Kind));
|
||||
Assert.Equal(1.0, await WorkingOffsetAsync(two, id, fx.Ct));
|
||||
|
||||
// A version whose own change met the library's: marked as the merge.
|
||||
var (_, three) = fx.Workspace("three");
|
||||
await fx.CheckoutAsync(three, id);
|
||||
await two.PushAsync(id, fx.Home, fx.Ct);
|
||||
await EditAsync(three, id, s => Offset(s, 2.0), fx.Ct);
|
||||
await three.CommitAsync(id, "bright", cancellationToken: fx.Ct);
|
||||
await EditAsync(three, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Median } }, fx.Ct);
|
||||
await three.CommitAsync(id, "median", cancellationToken: fx.Ct);
|
||||
await two.CommitAsync(id, "sent", cancellationToken: fx.Ct);
|
||||
await two.PushAsync(id, fx.Home, fx.Ct);
|
||||
var asked = Assert.NotNull((await three.PushAsync(id, fx.Home, fx.Ct)).Status.Conflict);
|
||||
await three.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, shown: asked, cancellationToken: fx.Ct);
|
||||
var replayed = three.Unpushed(id);
|
||||
Assert.SequenceEqual(["bright", "median"], replayed.Select(v => v.Note));
|
||||
Assert.Equal(ProjectVersionKind.Merge, replayed[^1].Kind, "the last version carrying the choice");
|
||||
Assert.Equal(ProjectVersionKind.Commit, replayed[0].Kind);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ACopyWritesAConvertedPhotoUnderANameOfItsOwnAndSwitchesAtItsCommit()
|
||||
{
|
||||
var fx = await FixtureAsync(4);
|
||||
var (store, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var id = project.ProjectId;
|
||||
var before = project.ReadMedia()!.Frames[1];
|
||||
var editors = project.MediaFile(before.File!);
|
||||
var bytes = File.ReadAllBytes(editors);
|
||||
await ProjectServiceTests.ConvertAsync(fx.Archive, fx.Sequence.Id, [1], 700);
|
||||
// The archive's proxy of the frame is made again from the converted photo.
|
||||
var row = (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!;
|
||||
var proxy = fx.Archive.Paths.Sequence(row.Folder).Proxy(1);
|
||||
if (File.Exists(proxy)) File.Delete(proxy);
|
||||
|
||||
Job job;
|
||||
string? written = null;
|
||||
using (await project.LockAsync(Wait, fx.Ct))
|
||||
{
|
||||
// The copy waits for the lease at its commit, its files written.
|
||||
job = await fx.StartCopyAsync(store, id);
|
||||
await UntilAsync(() => (written = Directory.GetFiles(project.ProxiesDirectory, $"{before.Id:N}-*.jpg").FirstOrDefault()) is not null,
|
||||
"the converted photo's proxy", fx.Ct);
|
||||
await fx.Archive.WaitForAsync(job.Id, JobState.Running, (int)TestTiming.Budget(30));
|
||||
Assert.SequenceEqual(bytes, File.ReadAllBytes(editors), "the editor's file is never written over before the commit");
|
||||
Assert.Equal(before.File, project.ReadMedia()!.Frames[1].File);
|
||||
}
|
||||
await fx.Archive.WaitForAsync(job.Id, JobState.Done, (int)TestTiming.Budget(60));
|
||||
var after = project.ReadMedia()!.Frames[1];
|
||||
Assert.Equal(Path.GetFileName(written), Path.GetFileName(after.File));
|
||||
Assert.NotEqual(before.FileSha256, after.FileSha256);
|
||||
Assert.False(File.Exists(editors), "the file the media no longer name goes at the commit");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ACancelledFirstCopyRemovesTheMediaItWroteOnceItStopped()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (store, sync) = fx.Workspace();
|
||||
var id = (await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct)).Id;
|
||||
var project = await sync.CheckoutAsync(fx.Home, id, cancellationToken: fx.Ct);
|
||||
var lease = await project.LockAsync(Wait, fx.Ct);
|
||||
Job job;
|
||||
try
|
||||
{
|
||||
job = await fx.StartCopyAsync(store, id);
|
||||
await UntilAsync(() => Directory.Exists(project.ProxiesDirectory) && Directory.GetFiles(project.ProxiesDirectory, "*.jpg").Length == fx.Frames.Count,
|
||||
"the copy's files", fx.Ct);
|
||||
await fx.CancelAsync(job.Id);
|
||||
await fx.Archive.WaitForAsync(job.Id, JobState.Cancelled, (int)TestTiming.Budget(30));
|
||||
Assert.True(Directory.Exists(project.MediaDirectory), "nothing is removed while another holds the project");
|
||||
}
|
||||
finally
|
||||
{
|
||||
lease.Dispose();
|
||||
}
|
||||
await UntilAsync(() => !Directory.Exists(project.MediaDirectory), "the cancelled copy's media removed", fx.Ct);
|
||||
Assert.Equal(id, project.ReadBundle().Workspace!.ProjectId);
|
||||
|
||||
// Copied again, then a second copy cancelled: media a copy finished stay.
|
||||
await fx.CopyAsync(store, id);
|
||||
var media = project.ReadMedia()!;
|
||||
using (await project.LockAsync(Wait, fx.Ct))
|
||||
{
|
||||
job = await fx.StartCopyAsync(store, id);
|
||||
await fx.Archive.WaitForAsync(job.Id, JobState.Running, (int)TestTiming.Budget(30));
|
||||
await fx.CancelAsync(job.Id);
|
||||
await fx.Archive.WaitForAsync(job.Id, JobState.Cancelled, (int)TestTiming.Budget(30));
|
||||
}
|
||||
using (await project.LockAsync(Wait, fx.Ct))
|
||||
Assert.Equal(media.Frames.Count, project.ReadMedia()!.Frames.Count);
|
||||
foreach (var frame in media.Frames) Assert.FileExists(project.MediaFile(frame.File!));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APausedFirstCopyKeepsWhatItWrote()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (store, sync) = fx.Workspace();
|
||||
var id = (await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct)).Id;
|
||||
var project = await sync.CheckoutAsync(fx.Home, id, cancellationToken: fx.Ct);
|
||||
using (await project.LockAsync(Wait, fx.Ct))
|
||||
{
|
||||
var job = await fx.StartCopyAsync(store, id);
|
||||
await UntilAsync(() => Directory.Exists(project.ProxiesDirectory) && Directory.GetFiles(project.ProxiesDirectory, "*.jpg").Length == fx.Frames.Count,
|
||||
"the copy's files", fx.Ct);
|
||||
await fx.PauseAsync(job.Id);
|
||||
await fx.Archive.WaitForAsync(job.Id, JobState.Paused, (int)TestTiming.Budget(30));
|
||||
}
|
||||
// Taken again by this test: nothing waited for it to remove the files.
|
||||
using (await project.LockAsync(Wait, fx.Ct))
|
||||
Assert.Equal(fx.Frames.Count, Directory.GetFiles(project.ProxiesDirectory, "*.jpg").Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AFirstCopyCancelledWhilePausedRemovesTheMediaItWroteUnderTheLease()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (store, sync) = fx.Workspace();
|
||||
var id = (await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct)).Id;
|
||||
var project = await sync.CheckoutAsync(fx.Home, id, cancellationToken: fx.Ct);
|
||||
Job job;
|
||||
using (await project.LockAsync(Wait, fx.Ct))
|
||||
{
|
||||
job = await fx.StartCopyAsync(store, id);
|
||||
await UntilAsync(() => Directory.Exists(project.ProxiesDirectory) && Directory.GetFiles(project.ProxiesDirectory, "*.jpg").Length == fx.Frames.Count,
|
||||
"the copy's files", fx.Ct);
|
||||
await fx.PauseAsync(job.Id);
|
||||
await fx.Archive.WaitForAsync(job.Id, JobState.Paused, (int)TestTiming.Budget(30));
|
||||
}
|
||||
|
||||
// Cancelled while paused (no handler runs it) and while another holds the project: removed once that one let go.
|
||||
var lease = await project.LockAsync(Wait, fx.Ct);
|
||||
try
|
||||
{
|
||||
await fx.CancelAsync(job.Id);
|
||||
await fx.Archive.WaitForAsync(job.Id, JobState.Cancelled, (int)TestTiming.Budget(30));
|
||||
await Task.Delay(200, fx.Ct);
|
||||
Assert.True(Directory.Exists(project.MediaDirectory), "nothing is removed while another holds the project");
|
||||
}
|
||||
finally
|
||||
{
|
||||
lease.Dispose();
|
||||
}
|
||||
await UntilAsync(() => !Directory.Exists(project.MediaDirectory), "the cancelled copy's media removed", fx.Ct);
|
||||
Assert.Equal(id, project.ReadBundle().Workspace!.ProjectId);
|
||||
|
||||
// Copied again, then a copy paused and cancelled: media a copy finished stay.
|
||||
await fx.CopyAsync(store, id);
|
||||
var media = project.ReadMedia()!;
|
||||
using (await project.LockAsync(Wait, fx.Ct))
|
||||
{
|
||||
job = await fx.StartCopyAsync(store, id);
|
||||
await fx.Archive.WaitForAsync(job.Id, JobState.Running, (int)TestTiming.Budget(30));
|
||||
await fx.PauseAsync(job.Id);
|
||||
await fx.Archive.WaitForAsync(job.Id, JobState.Paused, (int)TestTiming.Budget(30));
|
||||
await fx.CancelAsync(job.Id);
|
||||
}
|
||||
await Task.Delay(200, fx.Ct);
|
||||
using (await project.LockAsync(Wait, fx.Ct))
|
||||
Assert.Equal(media.Frames.Count, project.ReadMedia()!.Frames.Count);
|
||||
foreach (var frame in media.Frames) Assert.FileExists(project.MediaFile(frame.File!));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APausedFirstCopyCancelledWhileASecondCopyOfTheProjectRunsLeavesItsMedia()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (store, sync) = fx.Workspace();
|
||||
var id = (await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct)).Id;
|
||||
var project = await sync.CheckoutAsync(fx.Home, id, cancellationToken: fx.Ct);
|
||||
Job second;
|
||||
// Held so that neither copy commits before the first is cancelled.
|
||||
var lease = await project.LockAsync(Wait, fx.Ct);
|
||||
try
|
||||
{
|
||||
var first = await fx.StartCopyAsync(store, id);
|
||||
await UntilAsync(() => Directory.Exists(project.ProxiesDirectory) && Directory.GetFiles(project.ProxiesDirectory, "*.jpg").Length == fx.Frames.Count,
|
||||
"the first copy's files", fx.Ct);
|
||||
await fx.PauseAsync(first.Id);
|
||||
await fx.Archive.WaitForAsync(first.Id, JobState.Paused, (int)TestTiming.Budget(30));
|
||||
// The sequence opened again queues another copy (the first is paused), then the first is cancelled in Jobs.
|
||||
second = await fx.StartCopyAsync(store, id);
|
||||
await fx.Archive.WaitForAsync(second.Id, JobState.Running, (int)TestTiming.Budget(30));
|
||||
await fx.CancelAsync(first.Id);
|
||||
await fx.Archive.WaitForAsync(first.Id, JobState.Cancelled, (int)TestTiming.Budget(30));
|
||||
}
|
||||
finally
|
||||
{
|
||||
lease.Dispose();
|
||||
}
|
||||
await fx.Archive.WaitForAsync(second.Id, JobState.Done, (int)TestTiming.Budget(60));
|
||||
// The clean-up had its chance at the lease: it found the second copy and left the media.
|
||||
await Task.Delay(500, fx.Ct);
|
||||
using (await project.LockAsync(Wait, fx.Ct))
|
||||
{
|
||||
var media = Assert.NotNull(project.ReadMedia());
|
||||
Assert.Equal(fx.Frames.Count, media.Frames.Count);
|
||||
foreach (var frame in media.Frames) Assert.FileExists(project.MediaFile(frame.File!));
|
||||
}
|
||||
var status = await sync.StatusAsync(id, fx.Home, fx.Ct);
|
||||
Assert.True(status is { State: WorkspaceState.InSync, MediaRemoved: false });
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ACancelledCopysCleanUpLeavesAFolderNoLongerAProjectAndNeverMakesOneAgain()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (store, sync) = fx.Workspace();
|
||||
var main = (await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct)).Id;
|
||||
var side = (await fx.Archive.Service.Projects.CreateAsync(fx.Sequence.Id, "Side", cancellationToken: fx.Ct)).Id;
|
||||
|
||||
// The working state moved aside while the clean-up waited for the lease (Remove local copy): its folder is left as it is.
|
||||
var moving = await PausedFirstCopyAsync(fx, store, sync, main);
|
||||
var lease = await moving.Project.LockAsync(Wait, fx.Ct);
|
||||
var aside = Path.Combine(Context.CreateTempDirectory("aside"), WorkspaceProject.BundleFile);
|
||||
try
|
||||
{
|
||||
await fx.CancelAsync(moving.Copy.Id);
|
||||
// The clean-up waits for the lease meanwhile.
|
||||
await Task.Delay(300, fx.Ct);
|
||||
File.Move(moving.Project.BundlePath, aside);
|
||||
}
|
||||
finally
|
||||
{
|
||||
lease.Dispose();
|
||||
}
|
||||
await Task.Delay(1000, fx.Ct);
|
||||
Assert.True(Directory.Exists(moving.Project.ProxiesDirectory), "a folder that is no project is not the clean-up's");
|
||||
|
||||
// The folder removed while the clean-up waited: it is never made again.
|
||||
var removed = await PausedFirstCopyAsync(fx, store, sync, side);
|
||||
lease = await removed.Project.LockAsync(Wait, fx.Ct);
|
||||
try
|
||||
{
|
||||
await fx.CancelAsync(removed.Copy.Id);
|
||||
await Task.Delay(300, fx.Ct);
|
||||
Directory.Delete(removed.Project.Folder, recursive: true);
|
||||
}
|
||||
finally
|
||||
{
|
||||
lease.Dispose();
|
||||
}
|
||||
await Task.Delay(1000, fx.Ct);
|
||||
Assert.False(Directory.Exists(removed.Project.Folder), "no folder made again for a lease");
|
||||
}
|
||||
|
||||
/// <summary>A first copy of <paramref name="projectId"/>, checked out, that wrote every file and is paused before it committed.</summary>
|
||||
private async Task<(WorkspaceProject Project, Job Copy)> PausedFirstCopyAsync(WorkspaceFixture fx, WorkspaceStore store, WorkspaceSync sync, Guid projectId)
|
||||
{
|
||||
var project = await sync.CheckoutAsync(fx.Home, projectId, cancellationToken: fx.Ct);
|
||||
using (await project.LockAsync(Wait, fx.Ct))
|
||||
{
|
||||
var copy = await fx.StartCopyAsync(store, projectId);
|
||||
await UntilAsync(() => Directory.Exists(project.ProxiesDirectory) && Directory.GetFiles(project.ProxiesDirectory, "*.jpg").Length == fx.Frames.Count,
|
||||
"the copy's files", fx.Ct);
|
||||
await fx.PauseAsync(copy.Id);
|
||||
await fx.Archive.WaitForAsync(copy.Id, JobState.Paused, (int)TestTiming.Budget(30));
|
||||
return (project, copy);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProjectFolderHoldingOnlyALeaseNoOneHoldsGoesOnTheNextList()
|
||||
{
|
||||
var store = new WorkspaceStore(Path.Combine(Context.CreateTempDirectory("leftovers"), "workspace"));
|
||||
var old = DateTime.UtcNow - TimeSpan.FromHours(1);
|
||||
string Folder(params string[] files)
|
||||
{
|
||||
var folder = Path.Combine(store.Root, Guid.CreateVersion7().ToString("N"));
|
||||
Directory.CreateDirectory(folder);
|
||||
foreach (var file in files)
|
||||
{
|
||||
var path = Path.Combine(folder, file);
|
||||
if (!File.Exists(path)) File.WriteAllText(path, string.Empty);
|
||||
File.SetLastWriteTimeUtc(path, old);
|
||||
}
|
||||
Directory.SetLastWriteTimeUtc(folder, old);
|
||||
return folder;
|
||||
}
|
||||
|
||||
var leftover = Folder(".lock.lease", ".lock.lease.0a1b2c3d.stale");
|
||||
var empty = Folder();
|
||||
var other = Folder(".lock.lease", "notes.txt");
|
||||
var fresh = Path.Combine(store.Root, Guid.CreateVersion7().ToString("N"));
|
||||
Directory.CreateDirectory(fresh);
|
||||
File.WriteAllText(Path.Combine(fresh, ".lock.lease"), string.Empty);
|
||||
var heldId = Guid.CreateVersion7();
|
||||
using var held = await store.Project(heldId).LockAsync(TimeSpan.Zero);
|
||||
foreach (var file in Directory.GetFiles(store.Project(heldId).Folder)) File.SetLastWriteTimeUtc(file, old);
|
||||
Directory.SetLastWriteTimeUtc(store.Project(heldId).Folder, old);
|
||||
|
||||
Assert.Empty(store.List());
|
||||
Assert.False(Directory.Exists(leftover), "only an old lease and its stale copy: removed");
|
||||
Assert.False(Directory.Exists(empty), "nothing in it for a while: removed");
|
||||
Assert.True(Directory.Exists(other), "a file that is no lease stays");
|
||||
Assert.True(Directory.Exists(fresh), "a lease just written stays");
|
||||
Assert.True(Directory.Exists(store.Project(heldId).Folder), "a lease held stays");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheSpaceACopyNeedsCountsAPhotoHeldUnderEitherName()
|
||||
{
|
||||
var a = Guid.CreateVersion7();
|
||||
var b = Guid.CreateVersion7();
|
||||
var held = WorkspaceCopyJobHandler.HeldFrames([
|
||||
Path.Combine("p", $"{a:N}.jpg"),
|
||||
Path.Combine("p", $"{b:N}-0123456789abcdef.jpg"),
|
||||
Path.Combine("p", $"{Guid.CreateVersion7():N}x.jpg"),
|
||||
Path.Combine("p", "notes.jpg"),
|
||||
]);
|
||||
Assert.SequenceEqual(new[] { a, b }.Order(), held.Order());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AVersionReadAsDamagedWhileAnotherHoldsTheProjectIsReportedOnlyOnceConfirmed()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (_, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var id = project.ProjectId;
|
||||
await EditAsync(sync, id, s => Offset(s, 0.5), fx.Ct);
|
||||
var version = Assert.NotNull(await sync.CommitAsync(id, "first", cancellationToken: fx.Ct));
|
||||
File.WriteAllText(project.VersionPath(version), "{ half written");
|
||||
|
||||
WorkspaceStatus busy;
|
||||
using (await project.LockAsync(Wait, fx.Ct))
|
||||
busy = await sync.StatusAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal(0, busy.Damaged);
|
||||
Assert.NotEqual(WorkspaceState.Damaged, busy.State);
|
||||
|
||||
var free = await sync.StatusAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal((WorkspaceState.Damaged, 1), (free.State, free.Damaged));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheReplacedVersionsArePrunedByAgeAndCountButNeverOneStillNamed()
|
||||
{
|
||||
var store = new WorkspaceStore(Path.Combine(Context.CreateTempDirectory("prune"), "workspace"));
|
||||
var project = store.Project(Guid.CreateVersion7());
|
||||
var aside = Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedFolder);
|
||||
Directory.CreateDirectory(aside);
|
||||
var now = DateTime.UtcNow;
|
||||
// This Chronos has known the folder for longer than any of its files.
|
||||
File.WriteAllText(Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedSinceFile),
|
||||
(now - TimeSpan.FromDays(400)).ToString("O", System.Globalization.CultureInfo.InvariantCulture));
|
||||
var files = new List<(string Path, Guid Id)>();
|
||||
for (var i = 0; i < 25; i++)
|
||||
{
|
||||
var id = Guid.CreateVersion7();
|
||||
var path = Path.Combine(aside, $"{i + 1}-{id:N}.json" + (i == 24 ? ".638000000000000000" : ""));
|
||||
File.WriteAllText(path, "{}");
|
||||
File.SetLastWriteTimeUtc(path, now - (i < 22 ? TimeSpan.FromHours(i + 1) : TimeSpan.FromDays(31 + i)));
|
||||
files.Add((path, id));
|
||||
}
|
||||
|
||||
// The newest 20 stay; of the rest, only those a journal still names.
|
||||
var removed = project.PruneReplaced(new HashSet<Guid> { files[21].Id, files[24].Id }, now);
|
||||
Assert.Equal(3, removed);
|
||||
var left = Directory.GetFiles(aside).ToHashSet(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var (path, _) in files.Take(20)) Assert.Contains(path, left);
|
||||
Assert.Contains(files[21].Path, left);
|
||||
Assert.Contains(files[24].Path, left);
|
||||
Assert.DoesNotContain(files[20].Path, left);
|
||||
Assert.DoesNotContain(files[22].Path, left);
|
||||
Assert.DoesNotContain(files[23].Path, left);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AFileAnEarlierBuildSetAsideIsKeptAMonthFromWhenThisOneFirstKnewTheFolder()
|
||||
{
|
||||
var store = new WorkspaceStore(Path.Combine(Context.CreateTempDirectory("prune-old"), "workspace"));
|
||||
var project = store.Project(Guid.CreateVersion7());
|
||||
var aside = Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedFolder);
|
||||
Directory.CreateDirectory(aside);
|
||||
var now = DateTime.UtcNow;
|
||||
// Set aside by an earlier build, which kept the time its version was written: a year ago.
|
||||
var old = Path.Combine(aside, $"1-{Guid.CreateVersion7():N}.json");
|
||||
File.WriteAllText(old, "{}");
|
||||
File.SetLastWriteTimeUtc(old, now - TimeSpan.FromDays(365));
|
||||
|
||||
Assert.Equal(0, project.PruneReplaced(new HashSet<Guid>(), now));
|
||||
Assert.FileExists(old);
|
||||
var since = Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedSinceFile);
|
||||
Assert.FileExists(since);
|
||||
Assert.Equal(0, project.PruneReplaced(new HashSet<Guid>(), now + TimeSpan.FromDays(29)));
|
||||
Assert.FileExists(old);
|
||||
Assert.Equal(1, project.PruneReplaced(new HashSet<Guid>(), now + TimeSpan.FromDays(31)));
|
||||
Assert.False(File.Exists(old), "a month after this Chronos first knew it");
|
||||
|
||||
// A marker that cannot be read is written again as now: nothing goes by an age it cannot tell.
|
||||
var older = Path.Combine(aside, $"2-{Guid.CreateVersion7():N}.json");
|
||||
File.WriteAllText(older, "{}");
|
||||
File.SetLastWriteTimeUtc(older, now - TimeSpan.FromDays(365));
|
||||
File.WriteAllText(since, "not a time");
|
||||
Assert.Equal(0, project.PruneReplaced(new HashSet<Guid>(), now));
|
||||
Assert.FileExists(older);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASyncThatEndsWellPrunesTheReplacedVersions()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var id = await ProjectAsync(fx, "Pruned");
|
||||
var (_, two, project, shown) = await AskedAsync(fx, id);
|
||||
await two.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: shown, cancellationToken: fx.Ct);
|
||||
var aside = Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedFolder);
|
||||
var files = Directory.GetFiles(aside);
|
||||
Assert.Count(1, files);
|
||||
var taken = files[0];
|
||||
Assert.True(DateTime.UtcNow - File.GetLastWriteTimeUtc(taken) < TimeSpan.FromHours(1), "a file set aside is dated then");
|
||||
var old = OldReplaced(project, 40);
|
||||
|
||||
await two.PushAsync(id, fx.Home, fx.Ct);
|
||||
Assert.False(File.Exists(old));
|
||||
Assert.True(File.Exists(taken), "what the take replaced stays a month");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheHomesFramesAreReadAgainOnlyWhenItsStampMoved()
|
||||
{
|
||||
var fx = await FixtureAsync(4);
|
||||
var (_, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var id = project.ProjectId;
|
||||
var home = new CountingHome(fx.Home);
|
||||
Assert.False((await sync.StatusAsync(id, home, fx.Ct)).MediaOutdated);
|
||||
var read = home.FramesReads;
|
||||
Assert.False((await sync.StatusAsync(id, home, fx.Ct)).MediaOutdated);
|
||||
Assert.Equal(read, home.FramesReads, "the same stamp: the frames read before");
|
||||
|
||||
await ProjectServiceTests.ConvertAsync(fx.Archive, fx.Sequence.Id, [1], 700);
|
||||
Assert.True((await sync.StatusAsync(id, home, fx.Ct)).MediaOutdated);
|
||||
Assert.Equal(read + 1, home.FramesReads);
|
||||
Assert.True((await sync.StatusAsync(id, home, fx.Ct)).MediaOutdated);
|
||||
Assert.Equal(read + 1, home.FramesReads);
|
||||
|
||||
// A hash replaced in place (a Verify that found other bytes) moves the stamp, with no other frames version.
|
||||
var frame = (await fx.Db.Frames.ListBySequenceAsync(fx.Sequence.Id, cancellationToken: fx.Ct))[2];
|
||||
var before = await fx.Home.FramesStampAsync(fx.Ct);
|
||||
var sha = frame.Sha256 ?? new string('0', 64);
|
||||
var other = (sha[0] == 'f' ? "e" : "f") + sha[1..];
|
||||
Assert.Equal(1, await fx.Db.Frames.UpdateIntegrityAsync([new FrameIntegrity(frame.Id, frame.Flags, other)], fx.Ct));
|
||||
Assert.NotEqual(before, await fx.Home.FramesStampAsync(fx.Ct));
|
||||
await sync.StatusAsync(id, home, fx.Ct);
|
||||
Assert.Equal(read + 2, home.FramesReads, "the frames read again");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheFramesStampMovesWhenTwoPhotosSwapTheirHashes()
|
||||
{
|
||||
var fx = await FixtureAsync(4);
|
||||
var frames = await fx.Db.Frames.ListBySequenceAsync(fx.Sequence.Id, cancellationToken: fx.Ct);
|
||||
var (a, b) = (new string('a', 64), new string('b', 64));
|
||||
await fx.Db.Frames.UpdateIntegrityAsync([new FrameIntegrity(frames[0].Id, frames[0].Flags, a), new FrameIntegrity(frames[1].Id, frames[1].Flags, b)], fx.Ct);
|
||||
var before = await fx.Home.FramesStampAsync(fx.Ct);
|
||||
await fx.Db.Frames.UpdateIntegrityAsync([new FrameIntegrity(frames[0].Id, frames[0].Flags, b), new FrameIntegrity(frames[1].Id, frames[1].Flags, a)], fx.Ct);
|
||||
Assert.NotEqual(before, await fx.Home.FramesStampAsync(fx.Ct));
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SqliteWorkspaceSafetyTests(TestContext context) : WorkspaceSafetyTests(context)
|
||||
{
|
||||
protected override Backend Backend => Backend.SqliteFile;
|
||||
}
|
||||
|
||||
[RequiresEnvironment(PgTestServer.Variable)]
|
||||
public sealed class PostgresWorkspaceSafetyTests(TestContext context) : WorkspaceSafetyTests(context)
|
||||
{
|
||||
protected override Backend Backend => Backend.Postgres;
|
||||
}
|
||||
@@ -0,0 +1,451 @@
|
||||
using Chronos.Data.Tests.Projects;
|
||||
using System.Security.Cryptography;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Localization;
|
||||
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>The workspace (owner review 2, R8): its folder, the copy of a sequence's proxies, and sync with the archive as home.</summary>
|
||||
[Category("workspace")]
|
||||
public abstract class WorkspaceTests(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 string Sha(string path) => Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(path)));
|
||||
|
||||
/// <summary>The working state edited as the editor does: read, changed, stored.</summary>
|
||||
private static async Task<ProjectSettings> EditAsync(WorkspaceSync sync, Guid projectId, Func<ProjectSettings, ProjectSettings> edit, CancellationToken ct)
|
||||
{
|
||||
var session = await sync.OpenAsync(projectId, ct);
|
||||
var edited = edit(session.Working);
|
||||
await sync.SaveAsync(projectId, edited, ct);
|
||||
return edited;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheWorkspaceFolderHasItsManifestAndProjectFolders()
|
||||
{
|
||||
var root = Path.Combine(Context.CreateTempDirectory("ws"), "workspace");
|
||||
var store = new WorkspaceStore(root);
|
||||
Assert.FileExists(Path.Combine(root, WorkspaceStore.ManifestFile));
|
||||
Assert.Equal((WorkspaceStore.FormatName, 1), (store.Manifest.Format, store.Manifest.FormatVersion));
|
||||
Assert.Equal(store.Id, new WorkspaceStore(root).Id);
|
||||
Assert.Empty(store.List());
|
||||
|
||||
// Only folders named by a project id that hold a working state are projects.
|
||||
Directory.CreateDirectory(Path.Combine(root, "notes"));
|
||||
var empty = Guid.CreateVersion7();
|
||||
Directory.CreateDirectory(Path.Combine(root, empty.ToString("N")));
|
||||
Assert.Empty(store.List());
|
||||
Assert.Null(store.Find(empty));
|
||||
var project = store.Project(empty);
|
||||
Assert.Equal(Path.Combine(root, empty.ToString("N"), "media", "proxies"), project.ProxiesDirectory);
|
||||
Assert.Equal("proxies/" + empty.ToString("N") + ".jpg", WorkspaceProject.ProxyName(empty));
|
||||
|
||||
File.WriteAllText(Path.Combine(root, WorkspaceStore.ManifestFile), """{ "format": "chronos-workspace", "formatVersion": 7, "id": "0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01" }""");
|
||||
Assert.Throws<InvalidDataException>(() => new WorkspaceStore(root));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProjectIsHeldByOneWriterAtATime()
|
||||
{
|
||||
var store = new WorkspaceStore(Path.Combine(Context.CreateTempDirectory("ws"), "workspace"));
|
||||
var project = store.Project(Guid.CreateVersion7());
|
||||
using (await project.LockAsync(TimeSpan.FromSeconds(1), Ct))
|
||||
await Assert.ThrowsAsync<LeaseUnavailableException>(() => project.LockAsync(TimeSpan.FromMilliseconds(200), Ct));
|
||||
using (await project.LockAsync(TimeSpan.FromSeconds(TestTiming.Budget(5)), Ct))
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheCopyBringsVerifiedProxiesAndTheWorkingStateFollowsItsFrames()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (store, sync) = fx.Workspace();
|
||||
var main = await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct);
|
||||
var project = await sync.CheckoutAsync(fx.Home, main.Id, cancellationToken: fx.Ct);
|
||||
Assert.Equal(WorkspaceState.MediaRemoved, (await sync.StatusAsync(main.Id, fx.Home, fx.Ct)).State);
|
||||
Assert.Null((await sync.OpenAsync(main.Id, fx.Ct)).Sequence);
|
||||
|
||||
// The archive has no proxies yet: the copy makes them in the workspace and writes nothing into the archive.
|
||||
var folder = fx.Archive.Paths.Sequence(fx.Sequence.Folder);
|
||||
var job = await fx.CopyAsync(store, main.Id);
|
||||
Assert.Contains(Loc.L("Archive.Log.WorkspaceCopied", fx.Sequence.DisplayTitle, 0, 5, 0, 0), job.LogTail!);
|
||||
Assert.False(fx.Frames.Any(f => File.Exists(folder.Proxy(f.Index))));
|
||||
var media = Assert.NotNull(project.ReadMedia());
|
||||
Assert.Equal((BundleMedia.Proxies, fx.Sequence.Id, fx.Frames.Count), (media.Kind, media.SequenceId, media.Frames.Count));
|
||||
foreach (var (frame, entry) in fx.Frames.Zip(media.Frames))
|
||||
{
|
||||
Assert.Equal((frame.Id, frame.Sha256), (entry.Id, entry.Sha256));
|
||||
Assert.Equal(entry.FileSha256, Sha(project.MediaFile(entry.File!)));
|
||||
}
|
||||
|
||||
// Once the archive's own pass made its proxies, those are what the workspace holds, byte for byte.
|
||||
await fx.Archive.Service.Images.BuildProxiesAsync(fx.Archive.Archive, fx.Sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
await fx.CopyAsync(store, main.Id);
|
||||
media = Assert.NotNull(project.ReadMedia());
|
||||
foreach (var (frame, entry) in fx.Frames.Zip(media.Frames))
|
||||
{
|
||||
Assert.Equal(entry.FileSha256, Sha(project.MediaFile(entry.File!)));
|
||||
Assert.Equal(Sha(folder.Proxy(frame.Index)), entry.FileSha256);
|
||||
}
|
||||
var session = await sync.OpenAsync(main.Id, fx.Ct);
|
||||
var opened = Assert.NotNull(session.Sequence);
|
||||
Assert.Equal(5, opened.Files.Count(f => f is not null));
|
||||
Assert.Equal((WorkspaceState.InSync, 0), ((await sync.StatusAsync(main.Id, fx.Home, fx.Ct)).State, (await sync.StatusAsync(main.Id, fx.Home, fx.Ct)).Ahead));
|
||||
|
||||
// Positions on the copied frames follow their photos when the home's frames change and the copy runs again.
|
||||
await EditAsync(sync, main.Id, s => s with { ExcludedFrames = [2] }, fx.Ct);
|
||||
var (path, size, sha) = fx.Archive.Stage(ArchiveFixture.Start.AddSeconds(-30), 999);
|
||||
await fx.Archive.Service.Ingest.CommitAsync(new Chronos.Data.Archiving.IngestRequest(fx.Archive.Archive.Id, fx.Sequence.Id, null, null, path, "IMG_0000.JPG",
|
||||
size, sha, null, null), fx.Ct);
|
||||
fx.Sequence = (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!;
|
||||
await fx.CopyAsync(store, main.Id);
|
||||
var moved = await sync.OpenAsync(main.Id, fx.Ct);
|
||||
Assert.Equal(6, moved.Media!.Frames.Count);
|
||||
Assert.SequenceEqual([3], moved.Working.ExcludedFrames);
|
||||
Assert.Equal(fx.Frames[2].Id, moved.Media.Frames[3].Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ACopyResumesFromItsCheckpointAndRewritesWhatDoesNotMatch()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (store, sync) = fx.Workspace();
|
||||
var main = await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct);
|
||||
var project = await sync.CheckoutAsync(fx.Home, main.Id, cancellationToken: fx.Ct);
|
||||
// The archive's own proxies, which the copy takes as they are.
|
||||
await fx.Archive.Service.Images.BuildProxiesAsync(fx.Archive.Archive, fx.Sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
await fx.CopyAsync(store, main.Id);
|
||||
var copied = project.ReadMedia()!;
|
||||
|
||||
// An earlier run copied the first two before it stopped; a later file was left damaged.
|
||||
File.WriteAllText(project.MediaFile(copied.Frames[3].File!), "not a photo");
|
||||
File.Delete(project.MediaIndexPath);
|
||||
var job = await fx.CopyAsync(store, main.Id, checkpoint: "copy:2");
|
||||
Assert.Contains(Loc.L("Archive.Log.WorkspaceCopied", fx.Sequence.DisplayTitle, 1, 0, 4, 0), job.LogTail!);
|
||||
var media = Assert.NotNull(project.ReadMedia());
|
||||
Assert.SequenceEqual(copied.Frames.Select(f => f.FileSha256), media.Frames.Select(f => f.FileSha256));
|
||||
Assert.Equal(copied.Frames[3].FileSha256, Sha(project.MediaFile(media.Frames[3].File!)));
|
||||
|
||||
// A run stopped on other frames (a conversion keeps a frame's id, not its photo) leaves no file unchecked.
|
||||
File.WriteAllText(project.MediaFile(copied.Frames[0].File!), "not a photo either");
|
||||
job = await fx.CopyAsync(store, main.Id, checkpoint: $"copy:{copied.Frames.Count}:{fx.Sequence.FramesVersion - 1}");
|
||||
Assert.Contains(Loc.L("Archive.Log.WorkspaceCopied", fx.Sequence.DisplayTitle, 1, 0, 4, 0), job.LogTail!);
|
||||
Assert.Equal(copied.Frames[0].FileSha256, Sha(project.MediaFile(Assert.NotNull(project.ReadMedia()).Frames[0].File!)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AWorkspaceInsideALibraryGetsNothingCopied()
|
||||
{
|
||||
var fx = await FixtureAsync(2);
|
||||
var main = await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct);
|
||||
var inside = Path.Combine(fx.Archive.Paths.Root, "workspace");
|
||||
var job = await fx.CopyAsync(inside, main.Id, JobState.Failed);
|
||||
Assert.Equal(Loc.L("Errors.Workspace.InsideLibrary"), job.Error);
|
||||
Assert.False(Directory.Exists(inside));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PhotosConvertedAtTheHomeKeepTheirPositionsInTheWorkspace()
|
||||
{
|
||||
var fx = await FixtureAsync(4);
|
||||
var (store, sync) = fx.Workspace();
|
||||
var id = (await fx.CheckoutAsync(sync)).ProjectId;
|
||||
await EditAsync(sync, id, s => s with { ExcludedFrames = [1], HeroFrame = 2 }, fx.Ct);
|
||||
var saved = Assert.NotNull(await sync.CommitAsync(id, cancellationToken: fx.Ct));
|
||||
await sync.PushAsync(id, fx.Home, fx.Ct);
|
||||
await EditAsync(sync, id, s => s with { ExcludedFrames = [] }, fx.Ct);
|
||||
await sync.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
await sync.PushAsync(id, fx.Home, fx.Ct);
|
||||
await EditAsync(sync, id, s => s with { ExcludedFrames = [1] }, fx.Ct);
|
||||
|
||||
// A Replace conversion gives frames 1 and 2 other files (the same frames); Update media copies them again.
|
||||
await ProjectServiceTests.ConvertAsync(fx.Archive, fx.Sequence.Id, [1, 2], 700);
|
||||
fx.Sequence = (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!;
|
||||
await fx.CopyAsync(store, id);
|
||||
var session = await sync.OpenAsync(id, fx.Ct);
|
||||
Assert.NotEqual(fx.Frames[1].Sha256, session.Media!.Frames[1].Sha256);
|
||||
Assert.SequenceEqual([1], session.Working.ExcludedFrames);
|
||||
Assert.Equal((int?)2, session.Working.HeroFrame);
|
||||
|
||||
// The versions saved before name the photos as they were: still the same choices, and they restore onto the frames.
|
||||
Assert.Equal(WorkspaceState.Modified, (await sync.StatusAsync(id, fx.Home, fx.Ct)).State);
|
||||
await EditAsync(sync, id, s => s with { ExcludedFrames = [] }, fx.Ct);
|
||||
Assert.Equal(WorkspaceState.InSync, (await sync.StatusAsync(id, fx.Home, fx.Ct)).State);
|
||||
Assert.Null(await sync.CommitAsync(id, cancellationToken: fx.Ct));
|
||||
var restored = await sync.RestoreAsync(id, saved.Id, fx.Home, fx.Ct);
|
||||
Assert.SequenceEqual([1], restored.ExcludedFrames);
|
||||
Assert.Equal((int?)2, restored.HeroFrame);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CopyingTheOriginalsIsRefusedForNowWithItsReason()
|
||||
{
|
||||
var fx = await FixtureAsync(2);
|
||||
var (store, sync) = fx.Workspace();
|
||||
var main = await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct);
|
||||
await sync.CheckoutAsync(fx.Home, main.Id, BundleMedia.Originals, fx.Ct);
|
||||
var job = await fx.CopyAsync(store, main.Id, JobState.Failed, media: BundleMedia.Originals);
|
||||
Assert.Equal(Loc.L("Errors.Workspace.OriginalsLater"), job.Error);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AVersionSavedHereIsPushedAndBecomesTheSequenceSettings()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (_, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var id = project.ProjectId;
|
||||
|
||||
Assert.Null(await sync.CommitAsync(id, cancellationToken: fx.Ct));
|
||||
await EditAsync(sync, id, s => s with { ExcludedFrames = [1, 4], Exposure = s.Exposure with { ExposureOffsetEv = 0.4 } }, fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Modified, (await sync.StatusAsync(id, fx.Home, fx.Ct)).State);
|
||||
var version = Assert.NotNull(await sync.CommitAsync(id, "warmer", "alberto", fx.Ct));
|
||||
Assert.Equal((ProjectVersionKind.Commit, 2, "warmer", (DateTimeOffset?)null), (version.Kind, version.Number, version.Note, version.StoredAt));
|
||||
var status = await sync.StatusAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal((WorkspaceState.Ahead, 1, false), (status.State, status.Ahead, status.Modified));
|
||||
|
||||
var pushed = await sync.PushAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal(version.Id, pushed.Pushed.Single().Id);
|
||||
Assert.Equal(WorkspaceState.InSync, pushed.Status.State);
|
||||
Assert.NotNull(project.Versions().Single(v => v.Id == version.Id).StoredAt);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!;
|
||||
var settings = ProjectSettings.FromJson(sequence.SettingsJson!);
|
||||
Assert.SequenceEqual([1, 4], settings.ExcludedFrames);
|
||||
Assert.Equal(0.4, settings.Exposure.ExposureOffsetEv);
|
||||
|
||||
// Nothing else to push; the home's head is ours.
|
||||
Assert.Empty((await sync.PushAsync(id, fx.Home, fx.Ct)).Pushed);
|
||||
Assert.Equal(version.Id, (await fx.Home.HeadAsync(id, fx.Ct)).Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnotherWorkspacesPushIsPulledIntoTheWorkingState()
|
||||
{
|
||||
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 => s with { Stack = new StackSettings { Mode = StackSetting.Median, Length = 5 } }, fx.Ct);
|
||||
await one.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
await one.PushAsync(id, fx.Home, fx.Ct);
|
||||
|
||||
// Two has a change of its own not saved as a version yet: it stays, on top of what one pushed.
|
||||
await EditAsync(two, id, s => s with { ExcludedFrames = [0] }, fx.Ct);
|
||||
Assert.Equal((WorkspaceState.Behind, 1), ((await two.StatusAsync(id, fx.Home, fx.Ct)).State, (await two.StatusAsync(id, fx.Home, fx.Ct)).Behind));
|
||||
var pulled = await two.PullAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal(1, pulled.Pulled.Count);
|
||||
Assert.Equal(WorkspaceState.Modified, pulled.Status.State);
|
||||
var working = (await two.OpenAsync(id, fx.Ct)).Working;
|
||||
Assert.Equal((StackSetting.Median, 5), (working.Stack.Mode, working.Stack.Length));
|
||||
Assert.SequenceEqual([0], working.ExcludedFrames);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SeveralLocalVersionsAreReplayedOnTopOfTheHomesWhenItMovedMeanwhile()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (_, one) = fx.Workspace("one");
|
||||
var (_, two) = fx.Workspace("two");
|
||||
var project = await fx.CheckoutAsync(one);
|
||||
var id = project.ProjectId;
|
||||
await fx.CheckoutAsync(two, id);
|
||||
|
||||
await EditAsync(two, id, s => s with { Exposure = s.Exposure with { DeflickerStrength = new Auto<double>().WithManual(0.6) } }, fx.Ct);
|
||||
await two.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
await two.PushAsync(id, fx.Home, fx.Ct);
|
||||
|
||||
// One saved two versions meanwhile, excluding another photo than two would have and changing other settings.
|
||||
await EditAsync(one, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.3 } }, fx.Ct);
|
||||
var first = (await one.CommitAsync(id, "offset", cancellationToken: fx.Ct))!;
|
||||
await EditAsync(one, id, s => s with { ExcludedFrames = [3], Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
|
||||
var second = (await one.CommitAsync(id, "stack", cancellationToken: fx.Ct))!;
|
||||
Assert.Equal(2, (await one.StatusAsync(id, cancellationToken: fx.Ct)).Ahead);
|
||||
|
||||
var pushed = await one.PushAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal(1, pushed.Pulled.Count);
|
||||
Assert.SequenceEqual([3, 4], pushed.Pushed.Select(v => v.Number));
|
||||
Assert.SequenceEqual(["offset", "stack"], pushed.Pushed.Select(v => v.Note));
|
||||
Assert.False(pushed.Pushed.Any(v => v.Id == first.Id || v.Id == second.Id), "replayed versions are new versions");
|
||||
Assert.Equal(WorkspaceState.InSync, pushed.Status.State);
|
||||
var head = await fx.Home.HeadAsync(id, fx.Ct);
|
||||
var settings = ProjectSettings.FromJson(ProjectContent.Place(head.SettingsJson, ProjectServiceTests.Photos(fx.Frames)));
|
||||
Assert.Equal((0.6, 0.3, StackSetting.Mean), (settings.Exposure.DeflickerStrength.Resolve(1), settings.Exposure.ExposureOffsetEv, settings.Stack.Mode));
|
||||
Assert.SequenceEqual([3], settings.ExcludedFrames);
|
||||
// The first replayed version holds one's first change on top of two's, without the later ones.
|
||||
var replayed = ProjectSettings.FromJson(pushed.Pushed[0].SettingsJson);
|
||||
Assert.Equal((0.6, 0.3, StackSetting.None), (replayed.Exposure.DeflickerStrength.Resolve(1), replayed.Exposure.ExposureOffsetEv, replayed.Stack.Mode));
|
||||
// The versions as they were saved are kept.
|
||||
Assert.Equal(2, Directory.GetFiles(Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedFolder)).Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ExcludingDifferentPhotosOnEachSideMergesWithoutAConflict()
|
||||
{
|
||||
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 => s with { ExcludedFrames = [1] }, fx.Ct);
|
||||
await one.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
await one.PushAsync(id, fx.Home, fx.Ct);
|
||||
await EditAsync(two, id, s => s with { ExcludedFrames = [3] }, fx.Ct);
|
||||
await two.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
|
||||
var pushed = await two.PushAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal(WorkspaceState.InSync, pushed.Status.State);
|
||||
Assert.SequenceEqual([1, 3], ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!.SettingsJson!).ExcludedFrames);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheSameSettingChangedOnBothSidesIsAConflictToSettle()
|
||||
{
|
||||
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 => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.5 } }, fx.Ct);
|
||||
await one.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
await one.PushAsync(id, fx.Home, fx.Ct);
|
||||
|
||||
await EditAsync(two, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 1.0 }, Stack = new StackSettings { Mode = StackSetting.Median } }, fx.Ct);
|
||||
await two.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
var stopped = await two.PushAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Empty(stopped.Pushed);
|
||||
Assert.Equal(WorkspaceState.Conflict, stopped.Status.State);
|
||||
var difference = stopped.Status.Conflict!.Differences.Single();
|
||||
Assert.Equal(("exposure.exposureOffsetEv", "1", "0.5"), (difference.Path, difference.Mine, difference.Theirs));
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => two.CommitAsync(id, cancellationToken: fx.Ct));
|
||||
|
||||
// Take the server's value there: the other change of this workspace stays, and is pushed.
|
||||
var settled = await two.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, cancellationToken: fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Ahead, settled.Status.State);
|
||||
Assert.Equal(ProjectVersionKind.Merge, two.Unpushed(id).Single().Kind);
|
||||
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.Median), (head.Exposure.ExposureOffsetEv, head.Stack.Mode));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task KeepMineWinsWhereBothChangedAndSaveBothKeepsEachSide()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (_, one) = fx.Workspace("one");
|
||||
var (_, two) = fx.Workspace("two");
|
||||
var (_, three) = fx.Workspace("three");
|
||||
var id = (await fx.CheckoutAsync(one)).ProjectId;
|
||||
await fx.CheckoutAsync(two, id);
|
||||
await fx.CheckoutAsync(three, id);
|
||||
await EditAsync(one, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.5 } }, fx.Ct);
|
||||
await one.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
await one.PushAsync(id, fx.Home, fx.Ct);
|
||||
|
||||
await EditAsync(two, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = -1 } }, fx.Ct);
|
||||
await two.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Conflict, (await two.PullAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
await two.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, cancellationToken: fx.Ct);
|
||||
await two.PushAsync(id, fx.Home, fx.Ct);
|
||||
Assert.Equal(-1, ProjectSettings.FromJson((await fx.Home.HeadAsync(id, fx.Ct)).SettingsJson).Exposure.ExposureOffsetEv);
|
||||
|
||||
await EditAsync(three, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 2 } }, fx.Ct);
|
||||
await three.CommitAsync(id, "bright", cancellationToken: fx.Ct);
|
||||
await EditAsync(three, id, s => s with { Stack = new StackSettings { Mode = StackSetting.StarTrails } }, fx.Ct);
|
||||
Assert.Equal(WorkspaceState.Conflict, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State);
|
||||
var both = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct);
|
||||
|
||||
// Three's versions (its unsaved change saved first) live on in a new project; this project is the home's again.
|
||||
var copy = Assert.NotNull(both.SavedAs);
|
||||
Assert.Equal("Main (2)", copy.Name);
|
||||
Assert.Equal(2, both.Pushed.Count);
|
||||
var kept = ProjectSettings.FromJson((await fx.Home.HeadAsync(copy.Id, fx.Ct)).SettingsJson);
|
||||
Assert.Equal((2, StackSetting.StarTrails), (kept.Exposure.ExposureOffsetEv, kept.Stack.Mode));
|
||||
Assert.Equal(WorkspaceState.InSync, both.Status.State);
|
||||
Assert.Equal(-1, (await three.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv);
|
||||
Assert.Equal(2, (await fx.Home.ListAsync(fx.Ct)).Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ARestoredVersionIsTheWorkingStateAndTheNextVersionSaysSo()
|
||||
{
|
||||
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 => s with { ExcludedFrames = [2] }, fx.Ct);
|
||||
await sync.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
// An unsaved change before the restore is saved as a version first.
|
||||
await EditAsync(sync, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
|
||||
|
||||
var restored = await sync.RestoreAsync(id, original.Id, fx.Home, fx.Ct);
|
||||
Assert.Empty(restored.ExcludedFrames);
|
||||
Assert.Equal(2, sync.Unpushed(id).Count);
|
||||
await EditAsync(sync, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.1 } }, fx.Ct);
|
||||
var version = Assert.NotNull(await sync.CommitAsync(id, cancellationToken: fx.Ct));
|
||||
Assert.Equal((ProjectVersionKind.Restore, original.Id), (version.Kind, version.RestoredFrom));
|
||||
await EditAsync(sync, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.2 } }, fx.Ct);
|
||||
var next = Assert.NotNull(await sync.CommitAsync(id, cancellationToken: fx.Ct));
|
||||
Assert.Equal((ProjectVersionKind.Commit, (Guid?)null), (next.Kind, next.RestoredFrom));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AHomeOutOfReachIsOfflineAndTheWorkWaits()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (_, sync) = fx.Workspace();
|
||||
var id = (await fx.CheckoutAsync(sync)).ProjectId;
|
||||
var away = new UnreachableHome(fx.Home);
|
||||
await EditAsync(sync, id, s => s with { ExcludedFrames = [0] }, fx.Ct);
|
||||
await sync.CommitAsync(id, cancellationToken: fx.Ct);
|
||||
|
||||
var status = await sync.StatusAsync(id, away, fx.Ct);
|
||||
Assert.Equal((WorkspaceState.Offline, 1, true), (status.State, status.Ahead, status.Offline));
|
||||
await Assert.ThrowsAsync<HomeUnavailableException>(() => sync.PushAsync(id, away, fx.Ct));
|
||||
away.Reachable = true;
|
||||
Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, away, fx.Ct)).Status.State);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SettingsOfANewerChronosOpenReadOnly()
|
||||
{
|
||||
var fx = await FixtureAsync(2);
|
||||
var (_, sync) = fx.Workspace();
|
||||
var project = await fx.CheckoutAsync(sync);
|
||||
var bundle = project.ReadBundle();
|
||||
File.WriteAllText(project.BundlePath, bundle.ToJson().Replace("\"version\": 1", "\"version\": 2", StringComparison.Ordinal));
|
||||
|
||||
var session = await sync.OpenAsync(project.ProjectId, fx.Ct);
|
||||
Assert.True(session.IsReadOnly);
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => sync.SaveAsync(project.ProjectId, new ProjectSettings(), fx.Ct));
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => sync.CommitAsync(project.ProjectId, cancellationToken: fx.Ct));
|
||||
Assert.True((await sync.StatusAsync(project.ProjectId, cancellationToken: fx.Ct)).ReadOnly);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SqliteWorkspaceTests(TestContext context) : WorkspaceTests(context)
|
||||
{
|
||||
protected override Backend Backend => Backend.SqliteFile;
|
||||
}
|
||||
|
||||
[RequiresEnvironment(PgTestServer.Variable)]
|
||||
public sealed class PostgresWorkspaceTests(TestContext context) : WorkspaceTests(context)
|
||||
{
|
||||
protected override Backend Backend => Backend.Postgres;
|
||||
}
|
||||
Reference in new issue
Block a user