using System.Security.Cryptography; using Chronos.Core.Archiving; 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.Workspaces; using Chronos.Data.Workspaces; using Chronos.Testing; using static Chronos.Data.Tests.Archiving.ArchiveFixture; using static Chronos.Data.Tests.Projects.ProjectServiceTests; namespace Chronos.Data.Tests.Projects; /// /// The sidecars stay the truth of a sequence's projects when they cannot be read, are missing, are a newer Chronos's, or /// sit on a read-only archive, and an index that meets them late never takes its own old copy for the sequence's settings. /// [Category("projects")] public abstract class ProjectRecoveryTests(TestContext context) : DatabaseTest(context) { private async Task FixtureAsync() => Own(await CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true))); /// The live project folders of a sequence (one per project). private static string[] ProjectFolders(SequencePaths folder) => [.. Directory.GetDirectories(folder.ProjectsDirectory).Where(d => Guid.TryParseExact(Path.GetFileName(d), "N", out _))]; private static int MainRecords(SequencePaths folder) => ProjectFolders(folder).Count(d => ProjectSidecars.ReadRecord(Path.Combine(d, "project.json")).IsMain); private static ProjectSettings Settings(ArchiveFixture fx, Sequence sequence) => ProjectSettings.FromJson(File.ReadAllText(fx.Paths.Sequence(sequence.Folder).ProjectPath)); [Test] public async Task ARecordThatCannotBeReadNeverTakesItsProjectForGoneNorMakesASecondMain() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct); var folder = fx.Paths.Sequence(sequence.Folder); var mainRecord = File.ReadAllText(folder.ProjectRecordPath(main.Id)); var verticalRecord = File.ReadAllText(folder.ProjectRecordPath(vertical.Id)); // Main's record as a newer Chronos writes it, Vertical's damaged. var newer = mainRecord.Replace("\"formatVersion\": 1", "\"formatVersion\": 2", StringComparison.Ordinal); File.WriteAllText(folder.ProjectRecordPath(main.Id), newer); File.WriteAllText(folder.ProjectRecordPath(vertical.Id), "{ not json"); Assert.SequenceEqual([main.Id, vertical.Id], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id)); Assert.Equal(main.Id, (await projects.MainAsync(sequence.Id, fx.Ct)).Id); Assert.Count(2, ProjectFolders(folder)); Assert.Null((await fx.Db.Projects.GetAsync(vertical.Id))!.DeletedAt); // Nothing is written over a record that cannot be read: the versions sent wait. var head = await projects.HeadAsync(vertical.Id, fx.Ct); await Assert.ThrowsAsync(() => projects.PushAsync(vertical.Id, Draft(head, new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)); var home = new ArchiveProjectHome(fx.Db, projects, sequence.Id); await Assert.ThrowsAsync(() => home.PushAsync(vertical.Id, Draft(head, new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)); await Assert.ThrowsAsync(() => projects.RenameAsync(vertical.Id, "Square", fx.Ct)); await Assert.ThrowsAsync(() => projects.DeleteAsync(vertical.Id, fx.Ct)); Assert.Equal("{ not json", File.ReadAllText(folder.ProjectRecordPath(vertical.Id))); Assert.Equal(newer, File.ReadAllText(folder.ProjectRecordPath(main.Id))); // Readable again: everything is as it was, and goes on. File.WriteAllText(folder.ProjectRecordPath(main.Id), mainRecord); File.WriteAllText(folder.ProjectRecordPath(vertical.Id), verticalRecord); Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(vertical.Id, Draft(head, new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)).Outcome); Assert.SequenceEqual([main.Id, vertical.Id], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id)); // An index that meets Main's record unreadable makes no Main of its own either, and takes it once it reads. File.WriteAllText(folder.ProjectRecordPath(main.Id), newer); await fx.ReopenWithEmptyIndexAsync(); Assert.Empty((await fx.ScanAsync(ScanMode.Rebuild)).Failed); var rebuilt = fx.Service.Projects; Assert.SequenceEqual([vertical.Id], (await rebuilt.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id)); await Assert.ThrowsAsync(() => rebuilt.MainAsync(sequence.Id, fx.Ct)); await Assert.ThrowsAsync(() => new ArchiveProjectHome(fx.Db, rebuilt, sequence.Id).ListAsync(fx.Ct)); Assert.Count(2, ProjectFolders(folder)); File.WriteAllText(folder.ProjectRecordPath(main.Id), mainRecord); Assert.Equal(main.Id, (await rebuilt.MainAsync(sequence.Id, fx.Ct)).Id); Assert.Equal(1, MainRecords(folder)); } [Test] public async Task AMissingProjectsFolderIsWrittenAgainFromTheIndex() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct); var pushed = (await projects.PushAsync(vertical.Id, Draft(await projects.HeadAsync(vertical.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct)).Version!; var folder = fx.Paths.Sequence(sequence.Folder); // Moved away for a moment (a sync tool, a backup put back without it): no project is taken for gone, none made anew. Directory.Move(folder.ProjectsDirectory, folder.ProjectsDirectory + "-aside"); Assert.SequenceEqual([main.Id, vertical.Id], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id)); Assert.Equal(1, MainRecords(folder)); Assert.Equal(pushed.Id, ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id)).Head!.Id); Assert.Equal(pushed.ContentHash, ProjectSidecars.ReadVersion(folder.VersionPath(vertical.Id, 2, pushed.Id)).ContentHash); Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(vertical.Id, Draft(pushed, new ProjectSettings(), frames), fx.Ct)).Outcome); // The sidecars written again are the truth again: a rebuilt index has every project and version back. await fx.ReopenWithEmptyIndexAsync(); await fx.ScanAsync(ScanMode.Rebuild); var listed = await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct); Assert.SequenceEqual([main.Id, vertical.Id], listed.Select(p => p.Id)); Assert.Equal(3, listed[1].HeadNumber); } [Test] public async Task AMainAnOlderChronosTookForGoneIsListedAgain() { var fx = await FixtureAsync(); var (sequence, _) = await UploadedAsync(fx, 2); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var folder = fx.Paths.Sequence(sequence.Folder); // Its live record says it is not deleted: Main again, no second one. await fx.Db.Projects.UpdateAsync((await fx.Db.Projects.GetAsync(main.Id))! with { DeletedAt = DateTimeOffset.UtcNow }); Assert.SequenceEqual([main.Id], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id)); Assert.Equal(main.Id, (await projects.MainAsync(sequence.Id, fx.Ct)).Id); // And with its folder gone too: written again from the index. await fx.Db.Projects.UpdateAsync((await fx.Db.Projects.GetAsync(main.Id))! with { DeletedAt = DateTimeOffset.UtcNow }); Directory.Move(folder.ProjectDirectory(main.Id), folder.ProjectDirectory(main.Id) + "-aside"); Assert.SequenceEqual([main.Id], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id)); Assert.True(ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id)).IsMain); Assert.Equal(1, MainRecords(folder)); } [Test] public async Task ASecondIndexMeetingMainBetweenTwoPushesRevertsNothing() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var second = (await projects.PushAsync(main.Id, Draft(await projects.HeadAsync(main.Id, fx.Ct), new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 0.7 } }, frames), fx.Ct)).Version!; // Another PC indexes the archive (its copy of the settings: version 2's), then this one pushes version 3. var (other, _) = await fx.OtherWriterAsync(); var third = (await projects.PushAsync(main.Id, Draft(second, new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 1.4 } }, frames), fx.Ct)).Version!; // Its first look takes the settings from the archive, not from its old copy: no version is added. await other.Projects.ListAsync(sequence.Id, fx.Ct); Assert.SequenceEqual([3, 2, 1], (await other.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)).Select(v => v.Number)); Assert.Equal(1.4, Settings(fx, sequence).Exposure.ExposureOffsetEv); Assert.Equal(third.Id, (await projects.HeadAsync(main.Id, fx.Ct)).Id); Assert.Equal(1.4, ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!).Exposure.ExposureOffsetEv); } [Test] public async Task AnIndexWithAnOldCopyOfTheSettingsBringsItUpToMain() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var pushed = (await projects.PushAsync(main.Id, Draft(await projects.HeadAsync(main.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [0] }, frames), fx.Ct)).Version!; // An index taken back from a backup made before: it holds the sequence with the settings it had then, and no projects. await fx.ReopenWithEmptyIndexAsync(); await fx.ScanAsync(); await WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { ExcludedFrames = [1] }); var listed = await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct); Assert.Equal((pushed.Id, 2), (listed[0].HeadVersionId, listed[0].HeadNumber)); Assert.SequenceEqual([2, 1], (await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)).Select(v => v.Number)); Assert.SequenceEqual([0], ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!).ExcludedFrames); Assert.SequenceEqual([0], Settings(fx, sequence).ExcludedFrames); } [Test] public async Task AMirrorACrashCutShortBeforeProjectJsonIsCompletedAfterARebuild() { foreach (var olderRecord in new[] { false, true }) { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var first = await projects.HeadAsync(main.Id, fx.Ct); await projects.PushAsync(main.Id, Draft(first, new ProjectSettings { Stack = new StackSettings { Mode = StackSetting.Median } }, frames), fx.Ct); // As if the push had stopped before its mirror wrote project.json (an older record names no mirror at all). var folder = fx.Paths.Sequence(sequence.Folder); File.WriteAllText(folder.ProjectPath, ProjectSettings.FromJson(ProjectContent.Place(first.SettingsJson, Photos(frames))).ToJson()); var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id)); ProjectSidecars.WriteRecord(folder.ProjectRecordPath(main.Id), record with { Mirrored = olderRecord ? null : new ProjectHead(first.Id, 1) }); await fx.ReopenWithEmptyIndexAsync(); await fx.ScanAsync(ScanMode.Rebuild); Assert.SequenceEqual([2, 1], (await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)).Select(v => v.Number)); Assert.Equal(StackSetting.Median, Settings(fx, sequence).Stack.Mode); Assert.Equal(StackSetting.Median, ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!).Stack.Mode); } } [Test] public async Task SettingsAnOlderChronosWroteIntoProjectJsonBecomeAnExternalVersionForAnIndexMeetingMain() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); await projects.PushAsync(main.Id, Draft(await projects.HeadAsync(main.Id, fx.Ct), new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 0.7 } }, frames), fx.Ct); // An older Chronos on another PC edits the sequence (project.json); this PC's index is then rebuilt. File.WriteAllText(fx.Paths.Sequence(sequence.Folder).ProjectPath, new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = -1 } }.ToJson()); await fx.ReopenWithEmptyIndexAsync(); await fx.ScanAsync(ScanMode.Rebuild); var history = await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct); Assert.SequenceEqual([3, 2, 1], history.Select(v => v.Number)); Assert.Equal(ProjectVersionKind.External, history[0].Kind); Assert.Equal(-1, ProjectSettings.FromJson(history[0].SettingsJson).Exposure.ExposureOffsetEv); } [Test] public async Task AMainHeadOfANewerChronosIsListedAndPulledButNeverMirroredNorBuiltOn() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct); var head = await projects.HeadAsync(main.Id, fx.Ct); var folder = fx.Paths.Sequence(sequence.Folder); // A newer Chronos on another PC sharing the archive pushes version 2 in its settings format, and mirrors it. var settings = """{ "version": 2, "exposure": { "exposureOffsetEv": 0.5 }, "futureThing": true }"""; var now = DateTimeOffset.UtcNow; var future = new ProjectVersion { Id = Guid.CreateVersion7(), ProjectId = main.Id, ParentId = head.Id, Number = 2, Kind = ProjectVersionKind.Commit, ClientName = "newer", SettingsJson = settings, ContentHash = ProjectContent.Hash(settings), SettingsFormat = 2, FramesVersion = head.FramesVersion, CreatedAt = now, StoredAt = now, }; ProjectSidecars.WriteVersion(folder.VersionPath(main.Id, 2, future.Id), future); var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id)); ProjectSidecars.WriteRecord(folder.ProjectRecordPath(main.Id), record with { Head = new ProjectHead(future.Id, 2), Mirrored = new ProjectHead(future.Id, 2) }); File.WriteAllText(folder.ProjectPath, settings); var copy = (await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson; Assert.SequenceEqual([main.Id, vertical.Id], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id)); Assert.Equal(future.Id, (await projects.HeadAsync(main.Id, fx.Ct)).Id); Assert.Equal(future.Id, (await new ArchiveProjectHome(fx.Db, projects, sequence.Id).VersionsAfterAsync(main.Id, 1, 10, fx.Ct)).Single().Id); Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(vertical.Id, Draft(await projects.HeadAsync(vertical.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)).Outcome); Assert.Count(2, await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)); Assert.Equal(settings, File.ReadAllText(folder.ProjectPath)); Assert.Equal(copy, (await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson); // An older path writes the settings here meanwhile: nothing is merged into, or added over, the newer head. await WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { ExcludedFrames = [2] }); await projects.ListAsync(sequence.Id, fx.Ct); await projects.ListAsync(sequence.Id, fx.Ct); Assert.Count(2, await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)); Assert.Equal(settings, File.ReadAllText(folder.ProjectPath)); // Nor by an index meeting it for the first time. await fx.ReopenWithEmptyIndexAsync(); await fx.ScanAsync(ScanMode.Rebuild); Assert.Equal(future.Id, (await fx.Service.Projects.HeadAsync(main.Id, fx.Ct)).Id); Assert.Count(2, await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)); Assert.Equal(settings, File.ReadAllText(folder.ProjectPath)); } [Test] public async Task APushThisChronosCannotReadStrictlyIsRefused() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 2); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var head = await projects.HeadAsync(main.Id, fx.Ct); var wrong = """{ "exposure": 5 }"""; var result = await projects.PushAsync(main.Id, Draft(head, new ProjectSettings(), frames) with { SettingsJson = wrong, ContentHash = ProjectContent.Hash(wrong) }, fx.Ct); Assert.Equal(PushOutcome.Invalid, result.Outcome); Assert.Count(1, await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)); Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(main.Id, Draft(head, new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)).Outcome); } [Test] public async Task AHeadThisChronosCannotReadLeavesTheRecordAndTheHistoryUntouched() { foreach (var missing in new[] { false, true }) { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var first = await projects.HeadAsync(main.Id, fx.Ct); var second = (await projects.PushAsync(main.Id, Draft(first, new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)).Version!; var folder = fx.Paths.Sequence(sequence.Folder); // Version 2's file in a newer Chronos's format, or not here yet (a sync tool that brought the record first). var path = folder.VersionPath(main.Id, 2, second.Id); var text = File.ReadAllText(path); if (missing) File.Delete(path); else File.WriteAllText(path, text.Replace("\"formatVersion\": 1", "\"formatVersion\": 2", StringComparison.Ordinal)); var recordBytes = File.ReadAllBytes(folder.ProjectRecordPath(main.Id)); await fx.ReopenWithEmptyIndexAsync(); Assert.Empty((await fx.ScanAsync(ScanMode.Rebuild)).Failed); var rebuilt = fx.Service.Projects; var listed = await rebuilt.ListAsync(sequence.Id, fx.Ct); Assert.Equal((main.Id, first.Id), (listed.Single().Id, listed.Single().HeadVersionId)); Assert.Equal(first.Id, (await rebuilt.HeadAsync(main.Id, fx.Ct)).Id); await Assert.ThrowsAsync(() => rebuilt.PushAsync(main.Id, Draft(first, new ProjectSettings(), frames), fx.Ct)); Assert.SequenceEqual(recordBytes, File.ReadAllBytes(folder.ProjectRecordPath(main.Id))); Assert.False(Directory.Exists(Path.Combine(folder.ProjectVersionsDirectory(main.Id), ".stray"))); Assert.Count(missing ? 1 : 2, ProjectSidecars.ListVersions(folder.ProjectVersionsDirectory(main.Id))); Assert.Empty((await fx.ScanAsync(ScanMode.Rebuild)).Failed); // Once it reads, the history goes on from it. File.WriteAllText(path, text); Assert.Equal(second.Id, (await rebuilt.HeadAsync(main.Id, fx.Ct)).Id); Assert.Equal(PushOutcome.Stored, (await rebuilt.PushAsync(main.Id, Draft(second, new ProjectSettings(), frames), fx.Ct)).Outcome); } } [Test] public async Task VersionFilesOfOneNumberAndRecordsThatAllSayMainNeverStopARebuild() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var other = (await UploadedAsync(fx, 1, seed: 300)).Sequence; var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var otherMain = await projects.MainAsync(other.Id, fx.Ct); var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct); var first = await projects.HeadAsync(vertical.Id, fx.Ct); var second = (await projects.PushAsync(vertical.Id, Draft(first, new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct)).Version!; var folder = fx.Paths.Sequence(sequence.Folder); // Another file numbered 2, off the record's chain, and two more records that say they are Main and are named so. var twin = Draft(first, new ProjectSettings { ExcludedFrames = [0] }, frames) with { StoredAt = DateTimeOffset.UtcNow }; ProjectSidecars.WriteVersion(folder.VersionPath(vertical.Id, 2, twin.Id), twin); foreach (var _ in new[] { 1, 2 }) { var id = Guid.CreateVersion7(); ProjectSidecars.WriteRecord(folder.ProjectRecordPath(id), new ProjectRecord(id, sequence.Id, ProjectService.MainName, true, null, DateTimeOffset.UtcNow, null, DateTimeOffset.UtcNow)); } await fx.ReopenWithEmptyIndexAsync(); Assert.Empty((await fx.ScanAsync(ScanMode.Rebuild)).Failed); var listed = await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct); Assert.Equal(main.Id, listed.Single(p => p.IsMain).Id); Assert.SequenceEqual(["Main", "Main (2)", "Main (3)", "Vertical"], listed.Select(p => p.Name).Order(StringComparer.Ordinal)); Assert.Equal(second.Id, listed.Single(p => p.Id == vertical.Id).HeadVersionId); Assert.Count(1, Directory.GetFiles(Path.Combine(folder.ProjectVersionsDirectory(vertical.Id), ".stray"))); Assert.NotNull(await fx.Db.Projects.GetAsync(otherMain.Id)); } [Test] public async Task ALargeVersionIsStoredAndReadBackAndOneTooLargeIsNeverStored() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 2); var projects = fx.Service.Projects; var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct); var head = await projects.HeadAsync(vertical.Id, fx.Ct); var folder = fx.Paths.Sequence(sequence.Folder); // Some 30,000 photos parked by a long sequence (its nights excluded): past the old 8 MB read limit. var pending = Enumerable.Range(0, 30_000).Select(i => new PendingFrame { Sha256 = Convert.ToHexStringLower(SHA256.HashData(BitConverter.GetBytes(i))), Size = 1_000_000 + i, CapturedAt = Start.AddSeconds(i), Excluded = true, }).ToList(); var settings = ProjectContent.Canonical(new ProjectSettings { PendingFrames = pending }.ToJson()); var large = Draft(head, new ProjectSettings(), frames) with { SettingsJson = settings, ContentHash = ProjectContent.Hash(settings) }; Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(vertical.Id, large, fx.Ct)).Outcome); var path = folder.VersionPath(vertical.Id, 2, large.Id); Assert.Greater(new FileInfo(path).Length, 8L << 20); Assert.Equal(large.ContentHash, ProjectSidecars.ReadVersion(path).ContentHash); await fx.ReopenWithEmptyIndexAsync(); await fx.ScanAsync(ScanMode.Rebuild); var rebuilt = fx.Service.Projects; Assert.Equal(large.Id, (await rebuilt.HeadAsync(vertical.Id, fx.Ct)).Id); // One whose file would be past the limit is refused, and nothing is written. var huge = Draft(large, new ProjectSettings(), frames) with { Note = new string('x', ProjectSidecars.MaxVersionBytes) }; Assert.Equal(PushOutcome.TooLarge, (await rebuilt.PushAsync(vertical.Id, huge, fx.Ct)).Outcome); Assert.False(File.Exists(folder.VersionPath(vertical.Id, 3, huge.Id))); Assert.Throws(() => ProjectSidecars.WriteVersion(Path.Combine(Context.CreateTempDirectory("huge"), "v.json"), huge)); } [Test] public async Task TheHomeHoldsAVersionOnlyWhileItsFileReadsBack() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 2); var projects = fx.Service.Projects; var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct); var first = await projects.HeadAsync(vertical.Id, fx.Ct); var second = (await projects.PushAsync(vertical.Id, Draft(first, new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)).Version!; var folder = fx.Paths.Sequence(sequence.Folder); IProjectHome home = new ArchiveProjectHome(fx.Db, projects, sequence.Id); Assert.True(await home.HoldsAsync(vertical.Id, first, fx.Ct)); Assert.True(await home.HoldsAsync(vertical.Id, second, fx.Ct)); Assert.False(await home.HoldsAsync(vertical.Id, second with { ContentHash = new string('0', 64) }, fx.Ct)); // The index still has both rows, but the archive no longer holds them as they were. File.WriteAllText(folder.VersionPath(vertical.Id, 1, first.Id), "damaged"); File.Delete(folder.VersionPath(vertical.Id, 2, second.Id)); Assert.NotNull(await home.GetVersionAsync(vertical.Id, first.Id, fx.Ct)); Assert.False(await home.HoldsAsync(vertical.Id, first, fx.Ct)); Assert.False(await home.HoldsAsync(vertical.Id, second, fx.Ct)); } [Test] public async Task ADeleteThatCannotMoveTheFolderChangesNothing() { var fx = await FixtureAsync(); var (sequence, _) = await UploadedAsync(fx, 2); var projects = fx.Service.Projects; var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct); var folder = fx.Paths.Sequence(sequence.Folder); // The projects' trash cannot take the folder (a file in its way): the delete fails and says so, and nothing changed. File.WriteAllText(folder.ProjectsTrashDirectory, "in the way"); await Assert.ThrowsAsync(() => projects.DeleteAsync(vertical.Id, fx.Ct)); Assert.Null(ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id)).Deleted); Assert.SequenceEqual(["Main", "Vertical"], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Name)); File.Delete(folder.ProjectsTrashDirectory); await projects.DeleteAsync(vertical.Id, fx.Ct); Assert.False(Directory.Exists(folder.ProjectDirectory(vertical.Id))); Assert.NotNull(ProjectSidecars.ReadRecord(Path.Combine(Directory.GetDirectories(folder.ProjectsTrashDirectory).Single(), "project.json")).Deleted); Assert.SequenceEqual(["Main"], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Name)); } [Test] public async Task ASequenceOfAReadOnlyArchiveOpensFromItsSidecarsWithoutWriting() { var fx = await FixtureAsync(); var (fresh, freshFrames) = await UploadedAsync(fx, 2); var (edited, frames) = await UploadedAsync(fx, 2, seed: 300); var vertical = await fx.Service.Projects.CreateAsync(edited.Id, "Vertical", cancellationToken: fx.Ct); var pushed = (await fx.Service.Projects.PushAsync(vertical.Id, Draft(await fx.Service.Projects.HeadAsync(vertical.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)).Version!; await fx.ReopenWithEmptyIndexAsync(); await fx.ScanAsync(); var projects = fx.Service.Projects; projects.Availability = _ => ArchiveAvailability.ReadOnly; var locks = Directory.Exists(fx.Paths.LocksDirectory) ? Directory.GetFiles(fx.Paths.LocksDirectory).Length : 0; // A sequence with projects: read from its sidecars as they are. var home = new ArchiveProjectHome(fx.Db, projects, edited.Id); Assert.SequenceEqual(["Main", "Vertical"], (await home.ListAsync(fx.Ct)).Select(p => p.Name)); Assert.Equal(pushed.Id, (await home.HeadAsync(vertical.Id, fx.Ct)).Id); await Assert.ThrowsAsync(() => home.PushAsync(vertical.Id, Draft(pushed, new ProjectSettings(), frames), fx.Ct)); // A sequence with none yet: a Main in the index only, nothing written to the archive. var freshHome = new ArchiveProjectHome(fx.Db, projects, fresh.Id); var main = (await freshHome.ListAsync(fx.Ct)).Single(); Assert.True(main.IsMain); var head = await freshHome.HeadAsync(main.Id, fx.Ct); Assert.False(Directory.Exists(fx.Paths.Sequence(fresh.Folder).ProjectsDirectory)); Assert.Equal(locks, Directory.Exists(fx.Paths.LocksDirectory) ? Directory.GetFiles(fx.Paths.LocksDirectory).Length : 0); await Assert.ThrowsAsync(() => freshHome.PushAsync(main.Id, Draft(head, new ProjectSettings(), freshFrames), fx.Ct)); // Once the archive takes writes, that Main's sidecars are written, under the same id, and it goes on. projects.Availability = ArchiveVolume.Probe; Assert.Equal(main.Id, (await freshHome.ListAsync(fx.Ct)).Single().Id); var folder = fx.Paths.Sequence(fresh.Folder); Assert.Equal((main.Id, true), (ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id)).Id, ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id)).IsMain)); Assert.FileExists(folder.VersionPath(main.Id, 1, head.Id)); Assert.Equal(PushOutcome.Stored, (await freshHome.PushAsync(main.Id, Draft(head, new ProjectSettings { ExcludedFrames = [0] }, freshFrames), fx.Ct)).Outcome); } [Test] public async Task AnIndexOnlyMainMeetingAMainAnotherIndexMadeIsWrittenBackAsAnotherProject() { var fx = await FixtureAsync(); var (fresh, frames) = await UploadedAsync(fx, 2); var projects = fx.Service.Projects; projects.Availability = _ => ArchiveAvailability.ReadOnly; var ours = (await projects.ListAsync(fresh.Id, fx.Ct)).Single(); // Meanwhile another PC's index makes its own Main into the archive; then this archive takes writes again. var (other, _) = await fx.OtherWriterAsync(); var theirs = await other.Projects.MainAsync(fresh.Id, fx.Ct); projects.Availability = ArchiveVolume.Probe; // Theirs is Main, ours another project: in the index and in the sidecars alike, and it stays so. foreach (var _ in new[] { 1, 2 }) { Assert.SequenceEqual([(theirs.Id, "Main", true), (ours.Id, "Main (2)", false)], (await projects.ListAsync(fresh.Id, fx.Ct)).Select(p => (p.Id, p.Name, p.IsMain))); } var folder = fx.Paths.Sequence(fresh.Folder); Assert.Equal(1, MainRecords(folder)); var ourRecord = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(ours.Id)); Assert.Equal(("Main (2)", false), (ourRecord.Name, ourRecord.IsMain)); Assert.Equal(theirs.Id, (await other.Projects.MainAsync(fresh.Id, fx.Ct)).Id); Assert.SequenceEqual([theirs.Id, ours.Id], (await other.Projects.ListAsync(fresh.Id, fx.Ct)).Select(p => p.Id)); Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(theirs.Id, Draft(await projects.HeadAsync(theirs.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)).Outcome); Assert.SequenceEqual([1], Settings(fx, fresh).ExcludedFrames); } [Test] public async Task AMainAnotherIndexPassedOnIsTakenFromTheRecords() { var fx = await FixtureAsync(); var (sequence, _) = await UploadedAsync(fx, 2); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var second = await projects.CreateAsync(sequence.Id, "Main (2)", cancellationToken: fx.Ct); var folder = fx.Paths.Sequence(sequence.Folder); // Another index passed Main on, names and all: the records say the opposite of this index. var mainRecord = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id)); var secondRecord = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(second.Id)); ProjectSidecars.WriteRecord(folder.ProjectRecordPath(main.Id), mainRecord with { IsMain = false, Name = "Main (2)", Mirrored = null }); ProjectSidecars.WriteRecord(folder.ProjectRecordPath(second.Id), secondRecord with { IsMain = true, Name = "Main" }); Assert.SequenceEqual([(second.Id, "Main", true), (main.Id, "Main (2)", false)], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.Name, p.IsMain))); Assert.Equal(second.Id, (await projects.MainAsync(sequence.Id, fx.Ct)).Id); Assert.Count(1, (await fx.Db.Projects.ListBySequenceAsync(sequence.Id, includeDeleted: true, fx.Ct)).Where(p => p.IsMain)); Assert.Equal(1, MainRecords(folder)); } [Test] public async Task AProjectCreationCutBeforeItsRecordHoldsNothingBack() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 2); var folder = fx.Paths.Sequence(sequence.Folder); // As making Main leaves it when its record cannot be written after its version (a lost disk, a crash): a folder with // version 1 and no record, of a project no index row names. var id = Guid.CreateVersion7(); var settings = ProjectContent.ToPortable(new ProjectSettings(), Photos(frames)); var now = DateTimeOffset.UtcNow; var cut = new ProjectVersion { Id = Guid.CreateVersion7(), ProjectId = id, Number = 1, Kind = ProjectVersionKind.Imported, ClientName = "cut", SettingsJson = settings, ContentHash = ProjectContent.Hash(settings), SettingsFormat = ProjectContent.FormatOf(settings), FramesVersion = sequence.FramesVersion, CreatedAt = now, StoredAt = now, }; ProjectSidecars.WriteVersion(folder.VersionPath(id, 1, cut.Id), cut); var main = await fx.Service.Projects.MainAsync(sequence.Id, fx.Ct); Assert.NotEqual(id, main.Id); Assert.SequenceEqual([main.Id], (await new ArchiveProjectHome(fx.Db, fx.Service.Projects, sequence.Id).ListAsync(fx.Ct)).Select(p => p.Id)); Assert.FileExists(folder.VersionPath(id, 1, cut.Id)); Assert.Null(await fx.Db.Projects.GetAsync(id)); // Its record there at last (a sync tool bringing another PC's Main, made before this one): Main, as in every index; the // one made here meanwhile is another project, its version kept. ProjectSidecars.WriteRecord(folder.ProjectRecordPath(id), new ProjectRecord(id, sequence.Id, ProjectService.MainName, true, new ProjectHead(cut.Id, 1), now, null, now)); Assert.SequenceEqual([(id, "Main", true), (main.Id, "Main (2)", false)], (await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.Name, p.IsMain))); Assert.Equal(1, MainRecords(folder)); Assert.Count(1, await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)); } [Test] public async Task AMainMadeWhileAnotherMainsRecordWasNotThereYetGivesWayInEveryIndex() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var pushed = (await projects.PushAsync(main.Id, Draft(await projects.HeadAsync(main.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)) .Version!; var folder = fx.Paths.Sequence(sequence.Folder); // Another PC's index meets the sequence while a sync tool is still bringing Main's record: it makes a Main of its own. var record = folder.ProjectRecordPath(main.Id); File.Move(record, record + ".syncing"); var (other, _) = await fx.OtherWriterAsync(); var theirs = await other.Projects.MainAsync(sequence.Id, fx.Ct); Assert.NotEqual(main.Id, theirs.Id); File.Move(record + ".syncing", record); // The record there, both indexes take the older Main, in either order, and the other is an ordinary project with its version. foreach (var service in new[] { other, fx.Service, other }) { Assert.SequenceEqual([(main.Id, "Main", true), (theirs.Id, "Main (2)", false)], (await service.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.Name, p.IsMain))); } Assert.Equal(1, MainRecords(folder)); Assert.Equal(("Main (2)", false), (ProjectSidecars.ReadRecord(folder.ProjectRecordPath(theirs.Id)).Name, ProjectSidecars.ReadRecord(folder.ProjectRecordPath(theirs.Id)).IsMain)); Assert.Count(1, await other.Projects.HistoryAsync(theirs.Id, cancellationToken: fx.Ct)); Assert.Equal(pushed.Id, (await other.Projects.HeadAsync(main.Id, fx.Ct)).Id); Assert.Equal(PushOutcome.Stored, (await other.Projects.PushAsync(main.Id, Draft(pushed, new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct)).Outcome); Assert.SequenceEqual([2], Settings(fx, sequence).ExcludedFrames); Assert.Equal(main.Id, (await projects.MainAsync(sequence.Id, fx.Ct)).Id); } [Test] public async Task AProjectWhoseRecordIsLostIsWrittenAgainFromTheIndex() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct); var first = await projects.HeadAsync(vertical.Id, fx.Ct); var second = (await projects.PushAsync(vertical.Id, Draft(first, new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)).Version!; var mainSecond = (await projects.PushAsync(main.Id, Draft(await projects.HeadAsync(main.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct)) .Version!; var folder = fx.Paths.Sequence(sequence.Folder); // Version 3 of Vertical is in its folder (another PC's push), then both records and version 1's file are lost. var third = Draft(second, new ProjectSettings { ExcludedFrames = [0] }, frames) with { StoredAt = DateTimeOffset.UtcNow }; ProjectSidecars.WriteVersion(folder.VersionPath(vertical.Id, 3, third.Id), third); File.Delete(folder.ProjectRecordPath(vertical.Id)); File.Delete(folder.ProjectRecordPath(main.Id)); File.Delete(folder.VersionPath(vertical.Id, 1, first.Id)); // Nothing is held back: the records are written again from the index, the version on disk past its head adopted. Assert.SequenceEqual([(main.Id, true, 2), (vertical.Id, false, 3)], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.IsMain, p.HeadNumber))); var verticalRecord = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id)); Assert.Equal(("Vertical", third.Id), (verticalRecord.Name, verticalRecord.Head!.Id)); var mainRecord = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id)); Assert.Equal((true, mainSecond.Id, mainSecond.Id), (mainRecord.IsMain, mainRecord.Head!.Id, mainRecord.Mirrored?.Id)); Assert.FileExists(folder.VersionPath(vertical.Id, 1, first.Id)); Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(main.Id, Draft(mainSecond, new ProjectSettings { ExcludedFrames = [0] }, frames), fx.Ct)).Outcome); Assert.SequenceEqual([0], Settings(fx, sequence).ExcludedFrames); Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(vertical.Id, Draft(third, new ProjectSettings(), frames), fx.Ct)).Outcome); await fx.ReopenWithEmptyIndexAsync(); await fx.ScanAsync(ScanMode.Rebuild); Assert.SequenceEqual([4, 3, 2, 1], (await fx.Service.Projects.HistoryAsync(vertical.Id, cancellationToken: fx.Ct)).Select(v => v.Number)); Assert.SequenceEqual([3, 2, 1], (await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)).Select(v => v.Number)); } [Test] public async Task ARenameMadeAfterABackupStaysWhenTheBackupIsPutBack() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct); var folder = fx.Paths.Sequence(sequence.Folder); var backup = folder.ProjectsDirectory + "-backup"; CopyDirectory(folder.ProjectsDirectory, backup); await projects.PushAsync(vertical.Id, Draft(await projects.HeadAsync(vertical.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct); await projects.RenameAsync(vertical.Id, "Square", fx.Ct); // The backup made before is put back: its record is behind the index and names the old name. Directory.Delete(folder.ProjectsDirectory, recursive: true); CopyDirectory(backup, folder.ProjectsDirectory); Assert.Equal("Square", (await projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name); var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id)); Assert.Equal(("Square", 2), (record.Name, record.Head!.Number)); await fx.ReopenWithEmptyIndexAsync(); await fx.ScanAsync(ScanMode.Rebuild); Assert.Equal("Square", (await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name); } [Test] public async Task AProjectsFolderPutBackFromAnOlderBackupIsMadeWholeFromTheIndex() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct); var folder = fx.Paths.Sequence(sequence.Folder); var backup = folder.ProjectsDirectory + "-backup"; CopyDirectory(folder.ProjectsDirectory, backup); var v2 = (await projects.PushAsync(vertical.Id, Draft(await projects.HeadAsync(vertical.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)) .Version!; var m2 = (await projects.PushAsync(main.Id, Draft(await projects.HeadAsync(main.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct)).Version!; // The backup made before those pushes is put back: the records name version 1, the index holds version 2. Directory.Delete(folder.ProjectsDirectory, recursive: true); CopyDirectory(backup, folder.ProjectsDirectory); Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(vertical.Id, Draft(v2, new ProjectSettings { ExcludedFrames = [0] }, frames), fx.Ct)).Outcome); Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(main.Id, Draft(m2, new ProjectSettings { ExcludedFrames = [0] }, frames), fx.Ct)).Outcome); Assert.Equal(3, ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id)).Head!.Number); Assert.Equal(v2.ContentHash, ProjectSidecars.ReadVersion(folder.VersionPath(vertical.Id, 2, v2.Id)).ContentHash); Assert.Equal(m2.ContentHash, ProjectSidecars.ReadVersion(folder.VersionPath(main.Id, 2, m2.Id)).ContentHash); // The sidecars hold every version again: a rebuilt index has them all, and nothing was added. await fx.ReopenWithEmptyIndexAsync(); await fx.ScanAsync(ScanMode.Rebuild); Assert.SequenceEqual([3, 2, 1], (await fx.Service.Projects.HistoryAsync(vertical.Id, cancellationToken: fx.Ct)).Select(v => v.Number)); Assert.SequenceEqual([ProjectVersionKind.Commit, ProjectVersionKind.Commit, ProjectVersionKind.Imported], (await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)).Select(v => v.Kind)); Assert.SequenceEqual([0], Settings(fx, sequence).ExcludedFrames); // A read alone makes the records whole too. Directory.Delete(folder.ProjectsDirectory, recursive: true); CopyDirectory(backup, folder.ProjectsDirectory); await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct); Assert.Equal(3, ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id)).Head!.Number); Assert.Count(3, ProjectSidecars.ListVersions(folder.ProjectVersionsDirectory(main.Id))); } [Test] public async Task AMirrorRecordThatLagsProjectJsonNeverRevertsANewerPush() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var first = await projects.HeadAsync(main.Id, fx.Ct); var second = (await projects.PushAsync(main.Id, Draft(first, new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)).Version!; var folder = fx.Paths.Sequence(sequence.Folder); var afterSecond = File.ReadAllText(folder.ProjectPath); await projects.PushAsync(main.Id, Draft(second, new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct); // Version 2's mirror wrote project.json but not the record, and version 3's mirror was cut before project.json. File.WriteAllText(folder.ProjectPath, afterSecond); var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id)); ProjectSidecars.WriteRecord(folder.ProjectRecordPath(main.Id), record with { Mirrored = new ProjectHead(first.Id, 1) }); await fx.ReopenWithEmptyIndexAsync(); await fx.ScanAsync(ScanMode.Rebuild); // The mirror completes: no external version brings version 2 back over version 3. Assert.SequenceEqual([3, 2, 1], (await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)).Select(v => v.Number)); Assert.SequenceEqual([2], Settings(fx, sequence).ExcludedFrames); } [Test] public async Task AnIndexMeetingMainMergesSettingsWrittenSinceWithAPushNoMirrorWrote() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var first = await projects.HeadAsync(main.Id, fx.Ct); var pushed = (await projects.PushAsync(main.Id, Draft(first, new ProjectSettings { Stack = new StackSettings { Mode = StackSetting.Median } }, frames), fx.Ct)) .Version!; // Version 2's mirror cut short (the record names version 1), and an older Chronos then writes an offset into project.json. var folder = fx.Paths.Sequence(sequence.Folder); File.WriteAllText(folder.ProjectPath, new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = -1 } }.ToJson()); var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id)); ProjectSidecars.WriteRecord(folder.ProjectRecordPath(main.Id), record with { Mirrored = new ProjectHead(first.Id, 1) }); await fx.ReopenWithEmptyIndexAsync(); await fx.ScanAsync(ScanMode.Rebuild); // Both are in force, as an index that knew its mirror would have merged them. var history = await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct); Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.Commit, ProjectVersionKind.Imported], history.Select(v => v.Kind)); Assert.Equal(pushed.Id, history[0].ParentId); Assert.Equal((StackSetting.Median, -1.0), (Settings(fx, sequence).Stack.Mode, Settings(fx, sequence).Exposure.ExposureOffsetEv)); } [Test] public async Task ANewerChronosHeadOverAMirrorCutShortIsLeftAsItIs() { var fx = await FixtureAsync(); var (sequence, frames) = await UploadedAsync(fx, 3); var projects = fx.Service.Projects; var main = await projects.MainAsync(sequence.Id, fx.Ct); var first = await projects.HeadAsync(main.Id, fx.Ct); var pushed = (await projects.PushAsync(main.Id, Draft(first, new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)).Version!; // Version 2's mirror cut short, nobody writing the settings since; then a newer Chronos pushes version 3. var reverted = await WriteSettingsAsync(fx, sequence.Id, new ProjectSettings()); await fx.Db.Projects.SetMirroredAsync(main.Id, first.Id, reverted.SettingsVersion, fx.Ct); var newer = NewerChronos.Push(fx, sequence, pushed); Assert.SequenceEqual([main.Id], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id)); Assert.Equal(newer.Id, (await projects.HeadAsync(main.Id, fx.Ct)).Id); Assert.Count(3, await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)); Assert.Empty(ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!).ExcludedFrames); } [Test] public async Task AProjectDeletedAtItsHomeHasNoHeadThere() { var fx = await FixtureAsync(); var (sequence, _) = await UploadedAsync(fx, 2); var projects = fx.Service.Projects; var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct); var first = await projects.HeadAsync(vertical.Id, fx.Ct); IProjectHome home = new ArchiveProjectHome(fx.Db, projects, sequence.Id); // Deleted (by another PC, say): a workspace asking for its head learns it is gone, never that it is in step. await projects.DeleteAsync(vertical.Id, fx.Ct); Assert.Equal(ProjectError.NotFound, (await Assert.ThrowsAsync(() => home.HeadAsync(vertical.Id, fx.Ct))).Reason); Assert.False(await home.HoldsAsync(vertical.Id, first, fx.Ct)); Assert.Equal(first.Id, (await home.HistoryAsync(vertical.Id, null, 10, fx.Ct)).Single().Id); } [Test] public async Task AVersionParkingFortyThousandPhotosGoesFromAWorkspaceToTheArchiveAndBack() { var fx = await FixtureAsync(); var bed = Own(await WorkspaceFixture.CreateAsync(Context, fx, 3)); var vertical = await fx.Service.Projects.CreateAsync(bed.Sequence.Id, "Vertical", cancellationToken: fx.Ct); var (_, sync) = bed.Workspace(); var project = await bed.CheckoutAsync(sync, vertical.Id); // A long sequence's nights excluded: 40,000 photos parked by a version, past the old 8 MB limit of both files. var pending = Enumerable.Range(0, 40_000).Select(i => new PendingFrame { Sha256 = Convert.ToHexStringLower(SHA256.HashData(BitConverter.GetBytes(i))), Size = 1_000_000 + i, CapturedAt = Start.AddSeconds(i), Excluded = true, }).ToList(); var session = await sync.OpenAsync(vertical.Id, fx.Ct); await sync.SaveAsync(vertical.Id, session.Working with { PendingFrames = pending }, fx.Ct); var version = Assert.NotNull(await sync.CommitAsync(vertical.Id, "nights", cancellationToken: fx.Ct)); Assert.Greater(new FileInfo(project.VersionPath(version)).Length, 8L << 20); Assert.Equal(1, (await sync.StatusAsync(vertical.Id, bed.Home, fx.Ct)).Ahead); var pushed = await sync.PushAsync(vertical.Id, bed.Home, fx.Ct); Assert.Equal(version.Id, pushed.Pushed.Single().Id); Assert.Equal(WorkspaceState.InSync, pushed.Status.State); await fx.ReopenWithEmptyIndexAsync(); await fx.ScanAsync(ScanMode.Rebuild); var head = await fx.Service.Projects.HeadAsync(vertical.Id, fx.Ct); Assert.Equal((version.Id, "nights"), (head.Id, head.Note)); Assert.Count(40_000, ProjectSettings.FromJson(head.SettingsJson).PendingFrames ?? []); } private static void CopyDirectory(string from, string to) { Directory.CreateDirectory(to); foreach (var file in Directory.GetFiles(from)) File.Copy(file, Path.Combine(to, Path.GetFileName(file))); foreach (var directory in Directory.GetDirectories(from)) CopyDirectory(directory, Path.Combine(to, Path.GetFileName(directory))); } } public sealed class SqliteProjectRecoveryTests(TestContext context) : ProjectRecoveryTests(context) { protected override Backend Backend => Backend.SqliteFile; } [RequiresEnvironment(PgTestServer.Variable)] public sealed class PostgresProjectRecoveryTests(TestContext context) : ProjectRecoveryTests(context) { protected override Backend Backend => Backend.Postgres; }