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

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

856 lines
45 KiB
C#

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;
}