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;
///
/// 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.
///
[Category("workspace")]
public abstract class WorkspaceRecoveryTests(TestContext context) : DatabaseTest(context)
{
private async Task FixtureAsync(int frames = 5, Func? 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 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> 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(() => 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(() => 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().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(() => 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().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(() => 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);
}
///
/// A conflict this workspace stopped at: Main moved at the home (offset 0.5), and three saved "bright" (offset 2)
/// and then changed the stack without saving a version.
///
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(() =>
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(() =>
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(() => 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(() => 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(() => 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(() => sync.CommitAsync(id, cancellationToken: fx.Ct));
await Assert.ThrowsAsync(() => sync.PushAsync(id, fx.Home, fx.Ct));
await Assert.ThrowsAsync(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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;
}