Files
Chronos/tests/Chronos.Data.Tests/Workspaces/WorkspaceTests.cs
T
Alby96andClaude Opus 5.5 0c51d11080 feat(workspace): copy originals into the workspace and choose the project Edit opens
Phase 5c P1. A workspace copy of kind "originals" now copies each frame's
original beside its proxy, so a later local render has full-quality input:
streamed into a .part file, hashed as written, checked against the home's
SHA-256, renamed and read back. Archive and server homes both work, a stopped
copy resumes (originals: checkpoint), and the space check counts the
originals. A frame whose original is gone, damaged or refused keeps its proxy
and the job says how many; the copy does not fail. Stale originals are
deleted only when the home holds the same SHA-256, otherwise moved to the
workspace's trash.

Proxies only... plans first (every original hashed against the home's
frames), asks for an explicit confirmation, rewrites the media to proxies
before it touches a file, refuses a plan that changed since, and trashes what
the home no longer holds instead of deleting it.

Edit: Open in workspace offers Originals (the settings' default, disabled
when the drive lacks room) with the originals' size for archive and server
homes; Settings > Workspace > Open project picks Last used, Main or asks
which; each open is recorded for the clean-up. The project menu gains Copy
originals... and Proxies only..., refused while changes are unsaved or jobs
of the sequence run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 15:43:42 +01:00

478 lines
27 KiB
C#

using Chronos.Data.Tests.Projects;
using System.Security.Cryptography;
using Chronos.Core.Archiving;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Projects;
using Chronos.Data.Tests.Archiving;
using Chronos.Data.Tests.Infrastructure;
using Chronos.Data.Workspaces;
using Chronos.Testing;
namespace Chronos.Data.Tests.Workspaces;
/// <summary>The workspace (owner review 2, R8): its folder, the copy of a sequence's proxies, and sync with the archive as home.</summary>
[Category("workspace")]
public abstract class WorkspaceTests(TestContext context) : DatabaseTest(context)
{
private async Task<WorkspaceFixture> FixtureAsync(int frames = 5)
{
var archive = Own(await ArchiveFixture.CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true)));
return Own(await WorkspaceFixture.CreateAsync(Context, archive, frames));
}
private static string Sha(string path) => Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(path)));
/// <summary>The working state edited as the editor does: read, changed, stored.</summary>
private static async Task<ProjectSettings> EditAsync(WorkspaceSync sync, Guid projectId, Func<ProjectSettings, ProjectSettings> edit, CancellationToken ct)
{
var session = await sync.OpenAsync(projectId, ct);
var edited = edit(session.Working);
await sync.SaveAsync(projectId, edited, ct);
return edited;
}
[Test]
public void TheWorkspaceFolderHasItsManifestAndProjectFolders()
{
var root = Path.Combine(Context.CreateTempDirectory("ws"), "workspace");
var store = new WorkspaceStore(root);
Assert.FileExists(Path.Combine(root, WorkspaceStore.ManifestFile));
Assert.Equal((WorkspaceStore.FormatName, 1), (store.Manifest.Format, store.Manifest.FormatVersion));
Assert.Equal(store.Id, new WorkspaceStore(root).Id);
Assert.Empty(store.List());
// Only folders named by a project id that hold a working state are projects.
Directory.CreateDirectory(Path.Combine(root, "notes"));
var empty = Guid.CreateVersion7();
Directory.CreateDirectory(Path.Combine(root, empty.ToString("N")));
Assert.Empty(store.List());
Assert.Null(store.Find(empty));
var project = store.Project(empty);
Assert.Equal(Path.Combine(root, empty.ToString("N"), "media", "proxies"), project.ProxiesDirectory);
Assert.Equal("proxies/" + empty.ToString("N") + ".jpg", WorkspaceProject.ProxyName(empty));
File.WriteAllText(Path.Combine(root, WorkspaceStore.ManifestFile), """{ "format": "chronos-workspace", "formatVersion": 7, "id": "0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01" }""");
Assert.Throws<InvalidDataException>(() => new WorkspaceStore(root));
}
[Test]
public async Task AProjectIsHeldByOneWriterAtATime()
{
var store = new WorkspaceStore(Path.Combine(Context.CreateTempDirectory("ws"), "workspace"));
var project = store.Project(Guid.CreateVersion7());
using (await project.LockAsync(TimeSpan.FromSeconds(1), Ct))
await Assert.ThrowsAsync<LeaseUnavailableException>(() => project.LockAsync(TimeSpan.FromMilliseconds(200), Ct));
using (await project.LockAsync(TimeSpan.FromSeconds(TestTiming.Budget(5)), Ct))
{
}
}
[Test]
public async Task TheCopyBringsVerifiedProxiesAndTheWorkingStateFollowsItsFrames()
{
var fx = await FixtureAsync();
var (store, sync) = fx.Workspace();
var main = await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct);
var project = await sync.CheckoutAsync(fx.Home, main.Id, cancellationToken: fx.Ct);
Assert.Equal(WorkspaceState.MediaRemoved, (await sync.StatusAsync(main.Id, fx.Home, fx.Ct)).State);
Assert.Null((await sync.OpenAsync(main.Id, fx.Ct)).Sequence);
// The archive has no proxies yet: the copy makes them in the workspace and writes nothing into the archive.
var folder = fx.Archive.Paths.Sequence(fx.Sequence.Folder);
var job = await fx.CopyAsync(store, main.Id);
Assert.Contains(Loc.L("Archive.Log.WorkspaceCopied", fx.Sequence.DisplayTitle, 0, 5, 0, 0), job.LogTail!);
Assert.False(fx.Frames.Any(f => File.Exists(folder.Proxy(f.Index))));
var media = Assert.NotNull(project.ReadMedia());
Assert.Equal((BundleMedia.Proxies, fx.Sequence.Id, fx.Frames.Count), (media.Kind, media.SequenceId, media.Frames.Count));
foreach (var (frame, entry) in fx.Frames.Zip(media.Frames))
{
Assert.Equal((frame.Id, frame.Sha256), (entry.Id, entry.Sha256));
Assert.Equal(entry.FileSha256, Sha(project.MediaFile(entry.File!)));
}
// Once the archive's own pass made its proxies, those are what the workspace holds, byte for byte.
await fx.Archive.Service.Images.BuildProxiesAsync(fx.Archive.Archive, fx.Sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
await fx.CopyAsync(store, main.Id);
media = Assert.NotNull(project.ReadMedia());
foreach (var (frame, entry) in fx.Frames.Zip(media.Frames))
{
Assert.Equal(entry.FileSha256, Sha(project.MediaFile(entry.File!)));
Assert.Equal(Sha(folder.Proxy(frame.Index)), entry.FileSha256);
}
var session = await sync.OpenAsync(main.Id, fx.Ct);
var opened = Assert.NotNull(session.Sequence);
Assert.Equal(5, opened.Files.Count(f => f is not null));
Assert.Equal((WorkspaceState.InSync, 0), ((await sync.StatusAsync(main.Id, fx.Home, fx.Ct)).State, (await sync.StatusAsync(main.Id, fx.Home, fx.Ct)).Ahead));
// Positions on the copied frames follow their photos when the home's frames change and the copy runs again.
await EditAsync(sync, main.Id, s => s with { ExcludedFrames = [2] }, fx.Ct);
var (path, size, sha) = fx.Archive.Stage(ArchiveFixture.Start.AddSeconds(-30), 999);
await fx.Archive.Service.Ingest.CommitAsync(new Chronos.Data.Archiving.IngestRequest(fx.Archive.Archive.Id, fx.Sequence.Id, null, null, path, "IMG_0000.JPG",
size, sha, null, null), fx.Ct);
fx.Sequence = (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!;
await fx.CopyAsync(store, main.Id);
var moved = await sync.OpenAsync(main.Id, fx.Ct);
Assert.Equal(6, moved.Media!.Frames.Count);
Assert.SequenceEqual([3], moved.Working.ExcludedFrames);
Assert.Equal(fx.Frames[2].Id, moved.Media.Frames[3].Id);
}
[Test]
public async Task ACopyResumesFromItsCheckpointAndRewritesWhatDoesNotMatch()
{
var fx = await FixtureAsync();
var (store, sync) = fx.Workspace();
var main = await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct);
var project = await sync.CheckoutAsync(fx.Home, main.Id, cancellationToken: fx.Ct);
// The archive's own proxies, which the copy takes as they are.
await fx.Archive.Service.Images.BuildProxiesAsync(fx.Archive.Archive, fx.Sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
await fx.CopyAsync(store, main.Id);
var copied = project.ReadMedia()!;
// An earlier run copied the first two before it stopped; a later file was left damaged.
File.WriteAllText(project.MediaFile(copied.Frames[3].File!), "not a photo");
File.Delete(project.MediaIndexPath);
var job = await fx.CopyAsync(store, main.Id, checkpoint: "copy:2");
Assert.Contains(Loc.L("Archive.Log.WorkspaceCopied", fx.Sequence.DisplayTitle, 1, 0, 4, 0), job.LogTail!);
var media = Assert.NotNull(project.ReadMedia());
Assert.SequenceEqual(copied.Frames.Select(f => f.FileSha256), media.Frames.Select(f => f.FileSha256));
Assert.Equal(copied.Frames[3].FileSha256, Sha(project.MediaFile(media.Frames[3].File!)));
// A run stopped on other frames (a conversion keeps a frame's id, not its photo) leaves no file unchecked.
File.WriteAllText(project.MediaFile(copied.Frames[0].File!), "not a photo either");
job = await fx.CopyAsync(store, main.Id, checkpoint: $"copy:{copied.Frames.Count}:{fx.Sequence.FramesVersion - 1}");
Assert.Contains(Loc.L("Archive.Log.WorkspaceCopied", fx.Sequence.DisplayTitle, 1, 0, 4, 0), job.LogTail!);
Assert.Equal(copied.Frames[0].FileSha256, Sha(project.MediaFile(Assert.NotNull(project.ReadMedia()).Frames[0].File!)));
}
[Test]
public async Task AWorkspaceInsideALibraryGetsNothingCopied()
{
var fx = await FixtureAsync(2);
var main = await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct);
var inside = Path.Combine(fx.Archive.Paths.Root, "workspace");
var job = await fx.CopyAsync(inside, main.Id, JobState.Failed);
Assert.Equal(Loc.L("Errors.Workspace.InsideLibrary"), job.Error);
Assert.False(Directory.Exists(inside));
}
[Test]
public async Task PhotosConvertedAtTheHomeKeepTheirPositionsInTheWorkspace()
{
var fx = await FixtureAsync(4);
var (store, sync) = fx.Workspace();
var id = (await fx.CheckoutAsync(sync)).ProjectId;
await EditAsync(sync, id, s => s with { ExcludedFrames = [1], HeroFrame = 2 }, fx.Ct);
var saved = Assert.NotNull(await sync.CommitAsync(id, cancellationToken: fx.Ct));
await sync.PushAsync(id, fx.Home, fx.Ct);
await EditAsync(sync, id, s => s with { ExcludedFrames = [] }, fx.Ct);
await sync.CommitAsync(id, cancellationToken: fx.Ct);
await sync.PushAsync(id, fx.Home, fx.Ct);
await EditAsync(sync, id, s => s with { ExcludedFrames = [1] }, fx.Ct);
// A Replace conversion gives frames 1 and 2 other files (the same frames); Update media copies them again.
await ProjectServiceTests.ConvertAsync(fx.Archive, fx.Sequence.Id, [1, 2], 700);
fx.Sequence = (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!;
await fx.CopyAsync(store, id);
var session = await sync.OpenAsync(id, fx.Ct);
Assert.NotEqual(fx.Frames[1].Sha256, session.Media!.Frames[1].Sha256);
Assert.SequenceEqual([1], session.Working.ExcludedFrames);
Assert.Equal((int?)2, session.Working.HeroFrame);
// The versions saved before name the photos as they were: still the same choices, and they restore onto the frames.
Assert.Equal(WorkspaceState.Modified, (await sync.StatusAsync(id, fx.Home, fx.Ct)).State);
await EditAsync(sync, id, s => s with { ExcludedFrames = [] }, fx.Ct);
Assert.Equal(WorkspaceState.InSync, (await sync.StatusAsync(id, fx.Home, fx.Ct)).State);
Assert.Null(await sync.CommitAsync(id, cancellationToken: fx.Ct));
var restored = await sync.RestoreAsync(id, saved.Id, fx.Home, fx.Ct);
Assert.SequenceEqual([1], restored.ExcludedFrames);
Assert.Equal((int?)2, restored.HeroFrame);
}
[Test]
public async Task CopyingTheOriginalsCopiesTheProxiesToo()
{
var fx = await FixtureAsync(2);
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, BundleMedia.Originals, fx.Ct);
await fx.CopyAsync(store, main.Id, media: BundleMedia.Originals);
var media = project.ReadMedia()!;
Assert.True(media.HoldsOriginals);
Assert.True(media.Frames.All(f => f.HasOriginal), "every frame has its original");
Assert.Equal(media.Frames.Count, (await sync.OpenAsync(main.Id, fx.Ct)).Media!.Frames.Count);
}
[Test]
public async Task AVersionSavedHereIsPushedAndBecomesTheSequenceSettings()
{
var fx = await FixtureAsync();
var (_, sync) = fx.Workspace();
var project = await fx.CheckoutAsync(sync);
var id = project.ProjectId;
Assert.Null(await sync.CommitAsync(id, cancellationToken: fx.Ct));
await EditAsync(sync, id, s => s with { ExcludedFrames = [1, 4], Exposure = s.Exposure with { ExposureOffsetEv = 0.4 } }, fx.Ct);
Assert.Equal(WorkspaceState.Modified, (await sync.StatusAsync(id, fx.Home, fx.Ct)).State);
var version = Assert.NotNull(await sync.CommitAsync(id, "warmer", "alberto", fx.Ct));
Assert.Equal((ProjectVersionKind.Commit, 2, "warmer", (DateTimeOffset?)null), (version.Kind, version.Number, version.Note, version.StoredAt));
var status = await sync.StatusAsync(id, fx.Home, fx.Ct);
Assert.Equal((WorkspaceState.Ahead, 1, false), (status.State, status.Ahead, status.Modified));
var pushed = await sync.PushAsync(id, fx.Home, fx.Ct);
Assert.Equal(version.Id, pushed.Pushed.Single().Id);
Assert.Equal(WorkspaceState.InSync, pushed.Status.State);
Assert.NotNull(project.Versions().Single(v => v.Id == version.Id).StoredAt);
var sequence = (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!;
var settings = ProjectSettings.FromJson(sequence.SettingsJson!);
Assert.SequenceEqual([1, 4], settings.ExcludedFrames);
Assert.Equal(0.4, settings.Exposure.ExposureOffsetEv);
// Nothing else to push; the home's head is ours.
Assert.Empty((await sync.PushAsync(id, fx.Home, fx.Ct)).Pushed);
Assert.Equal(version.Id, (await fx.Home.HeadAsync(id, fx.Ct)).Id);
}
[Test]
public async Task AnotherWorkspacesPushIsPulledIntoTheWorkingState()
{
var fx = await FixtureAsync();
var (_, one) = fx.Workspace("one");
var (_, two) = fx.Workspace("two");
var id = (await fx.CheckoutAsync(one)).ProjectId;
await fx.CheckoutAsync(two, id);
await EditAsync(one, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Median, Length = 5 } }, fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
// Two has a change of its own not saved as a version yet: it stays, on top of what one pushed.
await EditAsync(two, id, s => s with { ExcludedFrames = [0] }, fx.Ct);
Assert.Equal((WorkspaceState.Behind, 1), ((await two.StatusAsync(id, fx.Home, fx.Ct)).State, (await two.StatusAsync(id, fx.Home, fx.Ct)).Behind));
var pulled = await two.PullAsync(id, fx.Home, fx.Ct);
Assert.Equal(1, pulled.Pulled.Count);
Assert.Equal(WorkspaceState.Modified, pulled.Status.State);
var working = (await two.OpenAsync(id, fx.Ct)).Working;
Assert.Equal((StackSetting.Median, 5), (working.Stack.Mode, working.Stack.Length));
Assert.SequenceEqual([0], working.ExcludedFrames);
}
[Test]
public async Task APullThatSucceededNeverFailsOnTheNameItFollows()
{
var fx = await FixtureAsync();
var (_, one) = fx.Workspace("one");
var (_, two) = fx.Workspace("two");
var id = (await fx.CheckoutAsync(one)).ProjectId;
await fx.CheckoutAsync(two, id);
await EditAsync(one, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Median } }, fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
// The home refuses its list (a server's 403) when the pull, done, follows the project's name: the pull stands.
var home = new FailingListHome(fx.Home);
var name = two.Store.Find(id)!.ReadBundle().Workspace!.Name;
var pulled = await two.PullAsync(id, home, fx.Ct);
Assert.Equal(1, home.Lists);
Assert.Equal((1, WorkspaceState.InSync), (pulled.Pulled.Count, pulled.Status.State));
Assert.Equal(StackSetting.Median, (await two.OpenAsync(id, fx.Ct)).Working.Stack.Mode);
Assert.Equal(name, two.Store.Find(id)!.ReadBundle().Workspace!.Name);
Assert.False(await two.FollowNameAsync(id, home, fx.Ct));
}
[Test]
public async Task SeveralLocalVersionsAreReplayedOnTopOfTheHomesWhenItMovedMeanwhile()
{
var fx = await FixtureAsync();
var (_, one) = fx.Workspace("one");
var (_, two) = fx.Workspace("two");
var project = await fx.CheckoutAsync(one);
var id = project.ProjectId;
await fx.CheckoutAsync(two, id);
await EditAsync(two, id, s => s with { Exposure = s.Exposure with { DeflickerStrength = new Auto<double>().WithManual(0.6) } }, fx.Ct);
await two.CommitAsync(id, cancellationToken: fx.Ct);
await two.PushAsync(id, fx.Home, fx.Ct);
// One saved two versions meanwhile, excluding another photo than two would have and changing other settings.
await EditAsync(one, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.3 } }, fx.Ct);
var first = (await one.CommitAsync(id, "offset", cancellationToken: fx.Ct))!;
await EditAsync(one, id, s => s with { ExcludedFrames = [3], Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
var second = (await one.CommitAsync(id, "stack", cancellationToken: fx.Ct))!;
Assert.Equal(2, (await one.StatusAsync(id, cancellationToken: fx.Ct)).Ahead);
var pushed = await one.PushAsync(id, fx.Home, fx.Ct);
Assert.Equal(1, pushed.Pulled.Count);
Assert.SequenceEqual([3, 4], pushed.Pushed.Select(v => v.Number));
Assert.SequenceEqual(["offset", "stack"], pushed.Pushed.Select(v => v.Note));
Assert.False(pushed.Pushed.Any(v => v.Id == first.Id || v.Id == second.Id), "replayed versions are new versions");
Assert.Equal(WorkspaceState.InSync, pushed.Status.State);
var head = await fx.Home.HeadAsync(id, fx.Ct);
var settings = ProjectSettings.FromJson(ProjectContent.Place(head.SettingsJson, ProjectServiceTests.Photos(fx.Frames)));
Assert.Equal((0.6, 0.3, StackSetting.Mean), (settings.Exposure.DeflickerStrength.Resolve(1), settings.Exposure.ExposureOffsetEv, settings.Stack.Mode));
Assert.SequenceEqual([3], settings.ExcludedFrames);
// The first replayed version holds one's first change on top of two's, without the later ones.
var replayed = ProjectSettings.FromJson(pushed.Pushed[0].SettingsJson);
Assert.Equal((0.6, 0.3, StackSetting.None), (replayed.Exposure.DeflickerStrength.Resolve(1), replayed.Exposure.ExposureOffsetEv, replayed.Stack.Mode));
// The versions as they were saved are kept.
Assert.Equal(2, Directory.GetFiles(Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedFolder)).Length);
}
[Test]
public async Task ExcludingDifferentPhotosOnEachSideMergesWithoutAConflict()
{
var fx = await FixtureAsync();
var (_, one) = fx.Workspace("one");
var (_, two) = fx.Workspace("two");
var id = (await fx.CheckoutAsync(one)).ProjectId;
await fx.CheckoutAsync(two, id);
await EditAsync(one, id, s => s with { ExcludedFrames = [1] }, fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
await EditAsync(two, id, s => s with { ExcludedFrames = [3] }, fx.Ct);
await two.CommitAsync(id, cancellationToken: fx.Ct);
var pushed = await two.PushAsync(id, fx.Home, fx.Ct);
Assert.Equal(WorkspaceState.InSync, pushed.Status.State);
Assert.SequenceEqual([1, 3], ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!.SettingsJson!).ExcludedFrames);
}
[Test]
public async Task TheSameSettingChangedOnBothSidesIsAConflictToSettle()
{
var fx = await FixtureAsync();
var (_, one) = fx.Workspace("one");
var (_, two) = fx.Workspace("two");
var id = (await fx.CheckoutAsync(one)).ProjectId;
await fx.CheckoutAsync(two, id);
await EditAsync(one, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.5 } }, fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
await EditAsync(two, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 1.0 }, Stack = new StackSettings { Mode = StackSetting.Median } }, fx.Ct);
await two.CommitAsync(id, cancellationToken: fx.Ct);
var stopped = await two.PushAsync(id, fx.Home, fx.Ct);
Assert.Empty(stopped.Pushed);
Assert.Equal(WorkspaceState.Conflict, stopped.Status.State);
var difference = stopped.Status.Conflict!.Differences.Single();
Assert.Equal(("exposure.exposureOffsetEv", "1", "0.5"), (difference.Path, difference.Mine, difference.Theirs));
await Assert.ThrowsAsync<InvalidOperationException>(() => two.CommitAsync(id, cancellationToken: fx.Ct));
// Take the server's value there: the other change of this workspace stays, and is pushed.
var settled = await two.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, cancellationToken: fx.Ct);
Assert.Equal(WorkspaceState.Ahead, settled.Status.State);
Assert.Equal(ProjectVersionKind.Merge, two.Unpushed(id).Single().Kind);
await two.PushAsync(id, fx.Home, fx.Ct);
var head = ProjectSettings.FromJson((await fx.Home.HeadAsync(id, fx.Ct)).SettingsJson);
Assert.Equal((0.5, StackSetting.Median), (head.Exposure.ExposureOffsetEv, head.Stack.Mode));
}
[Test]
public async Task KeepMineWinsWhereBothChangedAndSaveBothKeepsEachSide()
{
var fx = await FixtureAsync();
var (_, one) = fx.Workspace("one");
var (_, two) = fx.Workspace("two");
var (_, three) = fx.Workspace("three");
var id = (await fx.CheckoutAsync(one)).ProjectId;
await fx.CheckoutAsync(two, id);
await fx.CheckoutAsync(three, id);
await EditAsync(one, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.5 } }, fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
await EditAsync(two, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = -1 } }, fx.Ct);
await two.CommitAsync(id, cancellationToken: fx.Ct);
Assert.Equal(WorkspaceState.Conflict, (await two.PullAsync(id, fx.Home, fx.Ct)).Status.State);
await two.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, cancellationToken: fx.Ct);
await two.PushAsync(id, fx.Home, fx.Ct);
Assert.Equal(-1, ProjectSettings.FromJson((await fx.Home.HeadAsync(id, fx.Ct)).SettingsJson).Exposure.ExposureOffsetEv);
await EditAsync(three, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 2 } }, fx.Ct);
await three.CommitAsync(id, "bright", cancellationToken: fx.Ct);
await EditAsync(three, id, s => s with { Stack = new StackSettings { Mode = StackSetting.StarTrails } }, fx.Ct);
Assert.Equal(WorkspaceState.Conflict, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State);
var both = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct);
// Three's versions (its unsaved change saved first) live on in a new project; this project is the home's again.
var copy = Assert.NotNull(both.SavedAs);
Assert.Equal("Main (2)", copy.Name);
Assert.Equal(2, both.Pushed.Count);
var kept = ProjectSettings.FromJson((await fx.Home.HeadAsync(copy.Id, fx.Ct)).SettingsJson);
Assert.Equal((2, StackSetting.StarTrails), (kept.Exposure.ExposureOffsetEv, kept.Stack.Mode));
Assert.Equal(WorkspaceState.InSync, both.Status.State);
Assert.Equal(-1, (await three.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv);
Assert.Equal(2, (await fx.Home.ListAsync(fx.Ct)).Count);
}
[Test]
public async Task ARestoredVersionIsTheWorkingStateAndTheNextVersionSaysSo()
{
var fx = await FixtureAsync();
var (_, sync) = fx.Workspace();
var id = (await fx.CheckoutAsync(sync)).ProjectId;
var original = await fx.Home.HeadAsync(id, fx.Ct);
await EditAsync(sync, id, s => s with { ExcludedFrames = [2] }, fx.Ct);
await sync.CommitAsync(id, cancellationToken: fx.Ct);
// An unsaved change before the restore is saved as a version first.
await EditAsync(sync, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
var restored = await sync.RestoreAsync(id, original.Id, fx.Home, fx.Ct);
Assert.Empty(restored.ExcludedFrames);
Assert.Equal(2, sync.Unpushed(id).Count);
await EditAsync(sync, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.1 } }, fx.Ct);
var version = Assert.NotNull(await sync.CommitAsync(id, cancellationToken: fx.Ct));
Assert.Equal((ProjectVersionKind.Restore, original.Id), (version.Kind, version.RestoredFrom));
await EditAsync(sync, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.2 } }, fx.Ct);
var next = Assert.NotNull(await sync.CommitAsync(id, cancellationToken: fx.Ct));
Assert.Equal((ProjectVersionKind.Commit, (Guid?)null), (next.Kind, next.RestoredFrom));
}
[Test]
public async Task AHomeOutOfReachIsOfflineAndTheWorkWaits()
{
var fx = await FixtureAsync();
var (_, sync) = fx.Workspace();
var id = (await fx.CheckoutAsync(sync)).ProjectId;
var away = new UnreachableHome(fx.Home);
await EditAsync(sync, id, s => s with { ExcludedFrames = [0] }, fx.Ct);
await sync.CommitAsync(id, cancellationToken: fx.Ct);
var status = await sync.StatusAsync(id, away, fx.Ct);
Assert.Equal((WorkspaceState.Offline, 1, true), (status.State, status.Ahead, status.Offline));
await Assert.ThrowsAsync<HomeUnavailableException>(() => sync.PushAsync(id, away, fx.Ct));
away.Reachable = true;
Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, away, fx.Ct)).Status.State);
}
[Test]
public async Task SettingsOfANewerChronosOpenReadOnly()
{
var fx = await FixtureAsync(2);
var (_, sync) = fx.Workspace();
var project = await fx.CheckoutAsync(sync);
var bundle = project.ReadBundle();
File.WriteAllText(project.BundlePath, bundle.ToJson().Replace("\"version\": 1", "\"version\": 2", StringComparison.Ordinal));
var session = await sync.OpenAsync(project.ProjectId, fx.Ct);
Assert.True(session.IsReadOnly);
await Assert.ThrowsAsync<InvalidOperationException>(() => sync.SaveAsync(project.ProjectId, new ProjectSettings(), fx.Ct));
await Assert.ThrowsAsync<InvalidOperationException>(() => sync.CommitAsync(project.ProjectId, cancellationToken: fx.Ct));
Assert.True((await sync.StatusAsync(project.ProjectId, cancellationToken: fx.Ct)).ReadOnly);
}
}
public sealed class SqliteWorkspaceTests(TestContext context) : WorkspaceTests(context)
{
protected override Backend Backend => Backend.SqliteFile;
}
[RequiresEnvironment(PgTestServer.Variable)]
public sealed class PostgresWorkspaceTests(TestContext context) : WorkspaceTests(context)
{
protected override Backend Backend => Backend.Postgres;
}