using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Jobs;
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;
///
/// 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.
///
[Category("workspace")]
public abstract class WorkspaceSafetyTests(TestContext context) : DatabaseTest(context)
{
private async Task 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 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 WorkingOffsetAsync(WorkspaceSync sync, Guid projectId, CancellationToken ct) =>
(await sync.OpenAsync(projectId, ct)).Working.Exposure.ExposureOffsetEv;
private static List 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));
/// Polls until it holds, failing after the budget.
private static async Task UntilAsync(Func 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);
}
}
///
/// 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.
///
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 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(() =>
WorkspaceFixture.Cutting(store, "replay.base").ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: shown, cancellationToken: fx.Ct));
// 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(() =>
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");
}
/// A conflict where this side saved "bright" (offset 2) and has nothing unsaved: the working state is that version.
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(() =>
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(() =>
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);
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.Equal(listed, (await fx.Home.ListAsync(fx.Ct)).Count);
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 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 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;
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 { 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 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 = Path.Combine(aside, $"1-{Guid.CreateVersion7():N}.json");
File.WriteAllText(old, "{}");
File.SetLastWriteTimeUtc(old, DateTime.UtcNow - TimeSpan.FromDays(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);
}
}
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;
}