Merge branch 'review2-p5a-fix2-workspace' into review2-p5a-int2

# Conflicts:
#	src/Chronos.Data/Workspaces/WorkspaceSync.cs
This commit is contained in:
Alby96 committed 2026-11-03 12:12:19 +01:00
commit 775deda0f4
18 files changed
+641 -86

No files matched your search

@@ -1,7 +1,9 @@
using System.IO;
using Chronos.App.Editor;
using Chronos.App.Infrastructure;
using Chronos.App.Workspace;
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
@@ -64,6 +66,26 @@ public sealed class WorkspaceEditorTests(TestContext context)
}
}
[Test]
public async Task AProjectASyncKeepsBusyOpensOnceItIsFree()
{
await using var bed = await WorkspaceTestBed.CreateAsync(context, frames: 3);
var host = bed.Host();
var projectId = await bed.CheckoutAsync(host);
var impatient = new WorkspaceSync(host.Store, "Chronos test") { LockWait = TimeSpan.FromMilliseconds(200) };
var source = new WorkspaceSequenceSource(impatient, projectId, bed.Db, DecoderFactory.CreateClient(null), bed.Engine);
await using (source)
{
// A long sync or copy holds the project: the editor says so in words, and nothing was read.
using (await host.Store.Find(projectId)!.LockAsync(TimeSpan.FromSeconds(TestTiming.Budget(5)), bed.Ct))
{
var busy = await Assert.ThrowsAsync<EditorException>(() => source.LoadAsync(bed.Ct));
Assert.Equal(Loc.L("Errors.Workspace.Busy"), busy.Message);
}
Assert.Equal(3, (await source.LoadAsync(bed.Ct)).Frames.Count);
}
}
[Test]
public async Task SaveVersionSendsItAndTheLibraryTakesMain()
{
@@ -5,7 +5,9 @@ using Chronos.App.Workspace;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Projects;
using Chronos.Data.Sync;
using Chronos.Ui.Controls;
using Chronos.Data.Workspaces;
using Chronos.Testing;
@@ -258,6 +260,25 @@ public sealed class WorkspaceMenuTests(TestContext context)
Assert.Equal((WorkspaceState.Behind, 2), (ProjectMenuViewModel.WithLocal(behind, edited).State, ProjectMenuViewModel.WithLocal(behind, edited).Behind));
Assert.Equal(WorkspaceState.Modified, ProjectMenuViewModel.WithLocal(behind, edited with { Base = basis with { VersionId = Guid.NewGuid() } }).State,
"a base that moved since says nothing of how far the library is ahead");
// A version saved here that cannot be read outranks what the library said.
Assert.Equal(WorkspaceState.Damaged, ProjectMenuViewModel.WithLocal(behind, edited with { Damaged = 1 }).State);
}
[Test]
public void ADamagedVersionAndOneTooLargeReadAsSuch()
{
var damaged = new WorkspaceStatus(WorkspaceState.Damaged, false, 1, 0, false, false, false, null, null, null) { Damaged = 1 };
Assert.Equal((Loc.L("Editor.Workspace.State.Damaged"), StatusKind.Error), (WorkspaceText.State(damaged), WorkspaceText.Kind(damaged)));
Assert.Equal(Loc.L("Tooltips.Editor.Workspace.State.Damaged"), WorkspaceText.StateToolTip(damaged));
Assert.Equal(Loc.L("Errors.Workspace.Damaged"), WorkspaceText.Failure(new InvalidDataException("a version file cannot be read")));
// Too large: a version saved here (refused before it was written) and one the library refused say the same, with the limit.
var tooLarge = Loc.L("Errors.Workspace.VersionTooLarge", ProjectSidecars.MaxVersionBytes >> 20);
Assert.Contains("64", tooLarge);
Assert.Equal(tooLarge, WorkspaceText.Failure(new ProjectVersionTooLargeException("over the limit")));
Assert.Equal(tooLarge, WorkspaceText.Failure(new WorkspaceSyncException(PushOutcome.TooLarge, "over the limit")));
Assert.Equal(tooLarge, Chronos.App.Shell.ErrorText.For(new ProjectVersionTooLargeException("over the limit")));
Assert.Equal(Loc.L("Errors.Workspace.Refused"), WorkspaceText.Failure(new WorkspaceSyncException(PushOutcome.Invalid, "not this project's")));
}
[Test]
@@ -115,10 +115,17 @@ internal sealed class CuttingHome(IProjectHome home) : IProjectHome
/// <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;
@@ -136,7 +143,7 @@ internal sealed class CuttingHome(IProjectHome home) : IProjectHome
home.GetVersionAsync(projectId, versionId, cancellationToken);
public Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
home.VersionsAfterAsync(projectId, afterNumber, limit, 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);
@@ -159,8 +159,257 @@ public abstract class WorkspaceRecoveryTests(TestContext context) : DatabaseTest
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));
// Taking the server's first saves the unsaved offset as a version (kept for its undo); the versions before it reach the home in order.
Assert.SequenceEqual(["first", "other", "second"], (await NotesAsync(fx, id)).Skip(1).Take(3));
// Taking the server's first saves the unsaved offset as a version, kept aside for its undo: replayed it holds the
// server's values only, so it is not sent.
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);
Assert.Equal("bright", three.Unpushed(id)[0].Note);
// Back as it was, the home settles it as the user chooses.
await three.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, 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]