Files
Chronos/tests/Chronos.Data.Tests/Projects/ProjectServiceTests.cs
T
Alby96andClaude Opus 5.5 8dbdf8710e
CI / Build and test (pull_request) Successful in 10m29s
CI / Windows tests and installer (pull_request) Skipped
CI / YAML check (pull_request) Successful in 13s
CI / PostgreSQL 16 integration (pull_request) Successful in 10m19s
CI / PostgreSQL 17 integration (pull_request) Successful in 11m54s
CI / Docker image (pull_request) Successful in 54s
test(projects): compare Main's mirrored settings as JSON, not as text
PostgreSQL stores sequences.settings_json as jsonb, which reorders keys
and drops whitespace, so the exact text match with project.json failed
on the PostgreSQL integration jobs. The other mirror tests already
compare with JsonNode.DeepEquals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 16:49:28 +01:00

470 lines
29 KiB
C#

using Chronos.Core.Archiving;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Archiving;
using Chronos.Data.Projects;
using Chronos.Data.Repositories;
using Chronos.Data.Tests.Archiving;
using Chronos.Data.Tests.Infrastructure;
using Chronos.Testing;
using static Chronos.Data.Tests.Archiving.ArchiveFixture;
namespace Chronos.Data.Tests.Projects;
/// <summary>Projects of a sequence with this PC's archive as their home (owner review 2, R8): sidecars, index, Main mirror, push.</summary>
[Category("projects")]
public abstract class ProjectServiceTests(TestContext context) : DatabaseTest(context)
{
internal async Task<ArchiveFixture> FixtureAsync() => Own(await CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true)));
/// <summary>A sequence of <paramref name="count"/> uploaded photos (hashed), ten seconds apart.</summary>
internal static async Task<(Sequence Sequence, List<Frame> Frames)> UploadedAsync(ArchiveFixture fx, int count, int seed = 100)
{
Guid? id = null;
for (var i = 0; i < count; i++)
{
var (path, size, sha) = fx.Stage(Start.AddSeconds(10 * i), seed + i);
var result = await fx.Service.Ingest.CommitAsync(new IngestRequest(fx.Archive.Id, id, null, id is null ? $"upload-{seed}" : null, path,
$"IMG_{i + 1:0000}.JPG", size, sha, null, null), fx.Ct);
id = result.SequenceId;
}
return ((await fx.Db.Sequences.GetAsync(id!.Value))!, [.. await fx.Db.Frames.ListBySequenceAsync(id.Value)]);
}
internal static FramePhotos Photos(IEnumerable<Frame> frames) => new(frames.Select(f => (f.Index, new FramePhoto(f.Sha256, f.Size, f.CapturedAt))));
/// <summary>
/// The files of the frames at <paramref name="indices"/> replaced by other encodings, as a Replace conversion does (owner
/// review 2, R7, <see cref="FrameTrash.ReplaceAsync"/>): the same frames, other files and SHA-256.
/// </summary>
internal static async Task ConvertAsync(ArchiveFixture fx, Guid sequenceId, IReadOnlyList<int> indices, int seed)
{
var sequence = (await fx.Db.Sequences.GetAsync(sequenceId))!;
var manifest = ManifestStore.ReadSequence(fx.Paths.Sequence(sequence.Folder).ManifestPath);
var staging = Path.Combine(fx.Paths.StagingDirectory, "convert-test");
Directory.CreateDirectory(staging);
var swaps = new List<FrameSwap>();
foreach (var i in indices)
{
var frame = manifest.Frames[i];
var bytes = Chronos.Data.Tests.Archiving.Photos.Jpeg(frame.CapturedAt, seed + i);
var path = Path.Combine(staging, $"{Guid.NewGuid():N}.jpeg");
File.WriteAllBytes(path, bytes);
var sha = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(bytes));
swaps.Add(new FrameSwap(frame.Id, fx.Paths.ToRelative(path), [], (current, _) => Path.ChangeExtension(current.File, ".jpeg"), current => current with
{
Sha256 = sha,
Size = bytes.Length,
Source = new FrameSource { Sha256 = current.Sha256, Format = current.Format, File = current.File, Size = current.Size },
}));
}
Assert.NotNull(await fx.Service.FrameTrash.ReplaceAsync(fx.Archive, sequenceId, null, swaps, null, null, null, null, fx.Ct));
}
/// <summary>The sequence's settings written as an older code path writes them (the editor, an analysis): index first, with its version check.</summary>
internal static async Task<Sequence> WriteSettingsAsync(ArchiveFixture fx, Guid sequenceId, ProjectSettings settings)
{
var row = (await fx.Db.Sequences.GetAsync(sequenceId))!;
var result = await fx.Db.Sequences.EditAsync(sequenceId, new SequenceEdit { SettingsJson = settings.ToJson() }, row.SettingsVersion, fx.Ct);
Assert.Equal(SequenceEditOutcome.Updated, result.Outcome);
return result.Sequence!;
}
/// <summary>A version made from <paramref name="parent"/> with <paramref name="settings"/> (positions on <paramref name="frames"/>), as a workspace sends it.</summary>
internal static ProjectVersion Draft(ProjectVersion parent, ProjectSettings settings, IEnumerable<Frame> frames, string? note = null)
{
var portable = ProjectContent.ToPortable(settings, Photos(frames));
return new ProjectVersion
{
Id = Guid.CreateVersion7(),
ProjectId = parent.ProjectId,
ParentId = parent.Id,
Number = parent.Number + 1,
Kind = ProjectVersionKind.Commit,
Author = "alberto",
ClientName = "test",
Note = note,
SettingsJson = portable,
ContentHash = ProjectContent.Hash(portable),
FramesVersion = parent.FramesVersion,
CreatedAt = DateTimeOffset.UtcNow,
};
}
[Test]
public async Task MainIsMadeFromTheSequenceSettingsWithItsPositionsParkedByPhoto()
{
var fx = await FixtureAsync();
var (sequence, frames) = await UploadedAsync(fx, 5);
await WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { ExcludedFrames = [1], HeroFrame = 3 });
var projects = fx.Service.Projects;
var main = await projects.MainAsync(sequence.Id, fx.Ct);
Assert.True(main.IsMain);
Assert.Equal((ProjectService.MainName, 1), (main.Name, main.HeadNumber));
var head = await projects.HeadAsync(main.Id, fx.Ct);
Assert.Equal(ProjectVersionKind.Imported, head.Kind);
Assert.True(ProjectContent.Verifies(head.SettingsJson, head.ContentHash));
var portable = ProjectSettings.FromJson(head.SettingsJson);
Assert.Empty(portable.ExcludedFrames);
Assert.Null(portable.HeroFrame);
Assert.Equal(2, portable.PendingFrames!.Count);
var placed = ProjectSettings.FromJson(ProjectContent.Place(head.SettingsJson, Photos(frames)));
Assert.SequenceEqual([1], placed.ExcludedFrames);
Assert.Equal(3, placed.HeroFrame);
// The sidecars hold it: the record and an immutable version file.
var folder = fx.Paths.Sequence(sequence.Folder);
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id));
Assert.Equal((main.Id, sequence.Id, true, head.Id, 1), (record.Id, record.SequenceId, record.IsMain, record.Head!.Id, record.Head.Number));
Assert.Equal(head.SettingsJson, ProjectSidecars.ReadVersion(folder.VersionPath(main.Id, 1, head.Id)).SettingsJson);
// Asking again makes nothing new.
Assert.Equal(main.Id, (await projects.MainAsync(sequence.Id, fx.Ct)).Id);
Assert.Count(1, await projects.ListAsync(sequence.Id, fx.Ct));
Assert.Count(1, await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct));
}
[Test]
public async Task APushFastForwardsAndMirrorsMainIntoTheSequence()
{
var fx = await FixtureAsync();
var (sequence, frames) = await UploadedAsync(fx, 5);
var projects = fx.Service.Projects;
var main = await projects.MainAsync(sequence.Id, fx.Ct);
var head = await projects.HeadAsync(main.Id, fx.Ct);
var before = (await fx.Db.Sequences.GetAsync(sequence.Id))!;
var draft = Draft(head, new ProjectSettings { ExcludedFrames = [0, 4], HeroFrame = 2, Exposure = new ExposureSettings { ExposureOffsetEv = 0.7 } }, frames,
"first look");
var result = await projects.PushAsync(main.Id, draft, fx.Ct);
Assert.Equal(PushOutcome.Stored, result.Outcome);
var stored = Assert.NotNull(result.Version);
Assert.Equal((draft.Id, 2, "first look"), (stored.Id, stored.Number, stored.Note));
Assert.NotNull(stored.StoredAt);
Assert.Equal(stored.Id, (await projects.HeadAsync(main.Id, fx.Ct)).Id);
Assert.SequenceEqual([2, 1], (await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)).Select(v => v.Number));
Assert.FileExists(fx.Paths.Sequence(sequence.Folder).VersionPath(main.Id, 2, draft.Id));
// Main's head is the sequence's settings, positions on its frames, through the settings version check.
var after = (await fx.Db.Sequences.GetAsync(sequence.Id))!;
Assert.Greater(after.SettingsVersion, before.SettingsVersion);
var mirrored = ProjectSettings.FromJson(after.SettingsJson!);
Assert.SequenceEqual([0, 4], mirrored.ExcludedFrames);
Assert.Equal(0.7, mirrored.Exposure.ExposureOffsetEv);
Assert.Equal(frames[2].Id, after.HeroFrameId);
// PostgreSQL stores the settings as jsonb, which reorders keys: compare the JSON, not the text.
Assert.True(System.Text.Json.Nodes.JsonNode.DeepEquals(System.Text.Json.Nodes.JsonNode.Parse(after.SettingsJson!),
System.Text.Json.Nodes.JsonNode.Parse(File.ReadAllText(fx.Paths.Sequence(sequence.Folder).ProjectPath))), "project.json mirrors Main's head");
var row = (await fx.Db.Projects.GetAsync(main.Id))!;
Assert.Equal((stored.Id, after.SettingsVersion), (row.MirroredVersionId, row.MirroredSettingsVersion));
// The mirror is no outside change: no version is added for it.
Assert.Equal(2, (await projects.HeadAsync(main.Id, fx.Ct)).Number);
}
[Test]
public async Task APushIsIdempotentDivergesFromAnOldParentAndChecksItsHash()
{
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 draft = Draft(first, new ProjectSettings { Stack = new StackSettings { Mode = StackSetting.Median } }, frames);
Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(main.Id, draft, fx.Ct)).Outcome);
var again = await projects.PushAsync(main.Id, draft, fx.Ct);
Assert.Equal((PushOutcome.AlreadyStored, 2), (again.Outcome, again.Version!.Number));
Assert.Equal(2, (await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)).Count);
var late = Draft(first, new ProjectSettings { Stack = new StackSettings { Mode = StackSetting.Mean } }, frames);
var diverged = await projects.PushAsync(main.Id, late, fx.Ct);
Assert.Equal(PushOutcome.Diverged, diverged.Outcome);
Assert.Equal(draft.Id, diverged.Head!.Id);
var head = diverged.Head;
var damaged = Draft(head, new ProjectSettings(), frames) with { ContentHash = new string('f', 64) };
Assert.Equal(PushOutcome.HashMismatch, (await projects.PushAsync(main.Id, damaged, fx.Ct)).Outcome);
var positional = new ProjectSettings { ExcludedFrames = [1] }.ToJson();
var notPortable = Draft(head, new ProjectSettings(), frames) with { SettingsJson = positional, ContentHash = ProjectContent.Hash(positional) };
Assert.Equal(PushOutcome.Invalid, (await projects.PushAsync(main.Id, notPortable, fx.Ct)).Outcome);
var newer = """{ "version": 9 }""";
Assert.Equal(PushOutcome.NewerFormat,
(await projects.PushAsync(main.Id, Draft(head, new ProjectSettings(), frames) with { SettingsJson = newer, ContentHash = ProjectContent.Hash(newer) }, fx.Ct)).Outcome);
Assert.Equal(2, (await projects.HeadAsync(main.Id, fx.Ct)).Number);
}
[Test]
public async Task SettingsAnotherPathWritesBecomeAnExternalVersionOfMain()
{
var fx = await FixtureAsync();
var (sequence, _) = await UploadedAsync(fx, 3);
var projects = fx.Service.Projects;
var main = await projects.MainAsync(sequence.Id, fx.Ct);
// The editor's old path (or an analysis, or the server's API) writes the settings.
await WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { ExcludedFrames = [2], Exposure = new ExposureSettings { ExposureOffsetEv = -0.3 } });
var history = await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
Assert.Equal((2, ProjectVersionKind.External), (history[0].Number, history[0].Kind));
Assert.Equal(history[1].Id, history[0].ParentId);
Assert.Equal(-0.3, ProjectSettings.FromJson(history[0].SettingsJson).Exposure.ExposureOffsetEv);
// A title or tags edit moves the settings version too, but not the settings: nothing is added.
await fx.Db.Sequences.SetTagsAsync(sequence.Id, ["harbour"], fx.Ct);
Assert.Equal(2, (await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)).Count);
}
[Test]
public async Task AMirrorACrashCutShortIsCompletedAndOneRacedByAnEditIsMerged()
{
var fx = await FixtureAsync();
var (sequence, frames) = await UploadedAsync(fx, 4);
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!;
// As if the push had stopped before its mirror: the sequence still holds version 1, mirrored at its settings version.
var reverted = await WriteSettingsAsync(fx, sequence.Id, new ProjectSettings());
await fx.Db.Projects.SetMirroredAsync(main.Id, first.Id, reverted.SettingsVersion, fx.Ct);
Assert.Equal(pushed.Id, (await projects.HeadAsync(main.Id, fx.Ct)).Id);
var completed = (await fx.Db.Sequences.GetAsync(sequence.Id))!;
Assert.Equal(StackSetting.Median, ProjectSettings.FromJson(completed.SettingsJson!).Stack.Mode);
// Again, but someone wrote other settings meanwhile: both changes are kept in a merge version.
await WriteSettingsAsync(fx, sequence.Id, new ProjectSettings());
await fx.Db.Projects.SetMirroredAsync(main.Id, first.Id, completed.SettingsVersion, fx.Ct);
await WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 1.25 } });
var merge = await projects.HeadAsync(main.Id, fx.Ct);
Assert.Equal((ProjectVersionKind.Merge, pushed.Id), (merge.Kind, merge.ParentId));
var merged = ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!);
Assert.Equal((StackSetting.Median, 1.25), (merged.Stack.Mode, merged.Exposure.ExposureOffsetEv));
}
[Test]
public async Task ProjectsAreNamedOnceDeletedIntoTheirTrashAndMainStays()
{
var fx = await FixtureAsync();
var (sequence, _) = await UploadedAsync(fx, 3);
var projects = fx.Service.Projects;
var main = await projects.MainAsync(sequence.Id, fx.Ct);
var mainHead = await projects.HeadAsync(main.Id, fx.Ct);
var vertical = await projects.CreateAsync(sequence.Id, " Vertical social ", author: "alberto", cancellationToken: fx.Ct);
Assert.Equal(("Vertical social", false, 1), (vertical.Name, vertical.IsMain, vertical.HeadNumber));
var copy = await projects.HeadAsync(vertical.Id, fx.Ct);
Assert.Equal((ProjectVersionKind.Imported, mainHead.ContentHash), (copy.Kind, copy.ContentHash));
Assert.Equal(ProjectError.NameTaken,
(await Assert.ThrowsAsync<ProjectException>(() => projects.CreateAsync(sequence.Id, "VERTICAL SOCIAL", cancellationToken: fx.Ct))).Reason);
Assert.Equal(ProjectError.InvalidName, (await Assert.ThrowsAsync<ProjectException>(() => projects.CreateAsync(sequence.Id, " ", cancellationToken: fx.Ct))).Reason);
var landscape = await projects.CreateAsync(sequence.Id, "4K landscape", copy.Id, cancellationToken: fx.Ct);
Assert.Equal(ProjectError.NameTaken, (await Assert.ThrowsAsync<ProjectException>(() => projects.RenameAsync(landscape.Id, "Main", fx.Ct))).Reason);
landscape = await projects.RenameAsync(landscape.Id, "4K wide", fx.Ct);
Assert.Equal("4K wide", ProjectSidecars.ReadRecord(fx.Paths.Sequence(sequence.Folder).ProjectRecordPath(landscape.Id)).Name);
Assert.SequenceEqual(["Main", "Vertical social", "4K wide"], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Name));
Assert.Equal(ProjectError.MainCannotBeDeleted, (await Assert.ThrowsAsync<ProjectException>(() => projects.DeleteAsync(main.Id, fx.Ct))).Reason);
await projects.DeleteAsync(vertical.Id, fx.Ct);
var folder = fx.Paths.Sequence(sequence.Folder);
Assert.False(Directory.Exists(folder.ProjectDirectory(vertical.Id)));
var trashed = Directory.GetDirectories(folder.ProjectsTrashDirectory).Single();
Assert.True(Path.GetFileName(trashed).StartsWith(vertical.Id.ToString("N"), StringComparison.Ordinal));
Assert.NotNull(ProjectSidecars.ReadRecord(Path.Combine(trashed, "project.json")).Deleted);
Assert.SequenceEqual(["Main", "4K wide"], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Name));
// Its name is free again.
await projects.CreateAsync(sequence.Id, "Vertical social", cancellationToken: fx.Ct);
}
[Test]
public async Task AnIndexRebuiltFromTheSidecarsHasEveryProjectAndVersionBack()
{
var fx = await FixtureAsync();
var (sequence, frames) = await UploadedAsync(fx, 4);
var projects = fx.Service.Projects;
var main = await projects.MainAsync(sequence.Id, fx.Ct);
var mainPush = (await projects.PushAsync(main.Id, Draft(await projects.HeadAsync(main.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [3] }, frames),
fx.Ct)).Version!;
var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct);
var verticalPush = (await projects.PushAsync(vertical.Id,
Draft(await projects.HeadAsync(vertical.Id, fx.Ct), new ProjectSettings { Camera = new CameraSettings { Aspect = new Auto<string>().WithManual("9:16") } }, frames),
fx.Ct)).Version!;
var gone = await projects.CreateAsync(sequence.Id, "Old", cancellationToken: fx.Ct);
await projects.DeleteAsync(gone.Id, fx.Ct);
await fx.ReopenWithEmptyIndexAsync();
await fx.ScanAsync(ScanMode.Rebuild);
var rebuilt = fx.Service.Projects;
var listed = await rebuilt.ListAsync(sequence.Id, fx.Ct);
Assert.SequenceEqual([main.Id, vertical.Id], listed.Select(p => p.Id));
Assert.Equal((mainPush.Id, 2), (listed[0].HeadVersionId, listed[0].HeadNumber));
Assert.Equal((verticalPush.Id, 2), (listed[1].HeadVersionId, listed[1].HeadNumber));
Assert.Equal(verticalPush.SettingsJson, (await rebuilt.GetVersionAsync(verticalPush.Id, fx.Ct))!.SettingsJson);
Assert.NotNull((await fx.Db.Projects.GetAsync(gone.Id))!.DeletedAt);
// The sequence's settings are Main's head: the rebuilt index recognises the mirror and adds no version.
Assert.SequenceEqual([2, 1], (await rebuilt.HistoryAsync(main.Id, cancellationToken: fx.Ct)).Select(v => v.Number));
Assert.Equal(mainPush.Id, (await fx.Db.Projects.GetAsync(main.Id))!.MirroredVersionId);
}
[Test]
public async Task AProjectTheIndexDoesNotKnowYetIsFoundInItsSidecarsThroughItsHome()
{
var fx = await FixtureAsync();
var (sequence, frames) = await UploadedAsync(fx, 3);
var vertical = await fx.Service.Projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct);
var head = (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!;
// An index taken back from an older backup (or a sequence back from the trash) holds the sequence, not its projects yet:
// a workspace that has one checked out still finds it, and its versions.
await fx.ReopenWithEmptyIndexAsync();
await fx.ScanAsync();
Assert.Null(await fx.Db.Projects.GetAsync(vertical.Id));
var version = await new ArchiveProjectHome(fx.Db, fx.Service.Projects, sequence.Id).GetVersionAsync(vertical.Id, head.Id, fx.Ct);
Assert.Equal(head.ContentHash, Assert.NotNull(version).ContentHash);
await fx.ReopenWithEmptyIndexAsync();
await fx.ScanAsync();
var home = new ArchiveProjectHome(fx.Db, fx.Service.Projects, sequence.Id);
Assert.Equal(head.Id, (await home.HeadAsync(vertical.Id, fx.Ct)).Id);
// A project of another sequence is still none of this home's.
var other = (await UploadedAsync(fx, 1, seed: 300)).Sequence;
var square = await fx.Service.Projects.CreateAsync(other.Id, "Square", cancellationToken: fx.Ct);
Assert.Equal(ProjectError.NotFound, (await Assert.ThrowsAsync<ProjectException>(() => home.HeadAsync(square.Id, fx.Ct))).Reason);
Assert.Null(await home.GetVersionAsync(square.Id, head.Id, fx.Ct));
}
[Test]
public async Task AVersionFileAPushLeftBehindItsRecordIsAdoptedAndAStrayOneSetAside()
{
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 head = await projects.HeadAsync(vertical.Id, fx.Ct);
var folder = fx.Paths.Sequence(sequence.Folder);
// A push that wrote its version file and stopped before the record: the file follows the head.
var follower = Draft(head, new ProjectSettings { Stack = new StackSettings { Mode = StackSetting.StarTrails } }, frames) with { StoredAt = DateTimeOffset.UtcNow };
ProjectSidecars.WriteVersion(folder.VersionPath(vertical.Id, 2, follower.Id), follower);
// And a file past the head that follows nothing.
var stray = Draft(head, new ProjectSettings(), frames) with { Number = 5, ParentId = Guid.CreateVersion7(), StoredAt = DateTimeOffset.UtcNow };
ProjectSidecars.WriteVersion(folder.VersionPath(vertical.Id, 5, stray.Id), stray);
Assert.Equal(follower.Id, (await projects.HeadAsync(vertical.Id, fx.Ct)).Id);
Assert.Equal(follower.Id, ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id)).Head!.Id);
Assert.False(File.Exists(folder.VersionPath(vertical.Id, 5, stray.Id)));
Assert.Count(1, Directory.GetFiles(Path.Combine(folder.ProjectVersionsDirectory(vertical.Id), ".stray")));
// A version file is immutable: another version at its name is refused.
Assert.Throws<IOException>(() => ProjectSidecars.WriteVersion(folder.VersionPath(vertical.Id, 2, follower.Id), follower with { ContentHash = stray.ContentHash }));
}
[Test]
public async Task AReadWhileAnotherWriterHoldsTheSequenceAnswersFromTheIndex()
{
var fx = await FixtureAsync();
var (sequence, _) = await UploadedAsync(fx, 2);
var projects = fx.Service.Projects;
var main = await projects.MainAsync(sequence.Id, fx.Ct);
using (Assert.NotNull(LeaseFile.TryAcquire(fx.Paths.LocksDirectory, $"seq-{sequence.Id:N}", "another process")))
{
var watch = System.Diagnostics.Stopwatch.StartNew();
Assert.Equal(main.Id, (await projects.ListAsync(sequence.Id, fx.Ct)).Single().Id);
Assert.Equal(main.HeadVersionId, (await projects.HeadAsync(main.Id, fx.Ct)).Id);
// Each read waits a moment for the lease, never the writers' whole wait.
Assert.Less(watch.Elapsed.TotalSeconds, TestTiming.Budget(15));
}
}
[Test]
public async Task AReorganizedSequenceKeepsItsProjects()
{
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);
// A Reorganize (phase 4a) moves the whole folder under the sequence's lease: the projects' sidecars go with it.
var plan = await fx.Service.Renamer.PlanAsync(fx.Archive, [sequence.Id], "Moved/{title}", null, fx.Ct);
await fx.Service.Renamer.ApplyAsync(fx.Archive, plan, cancellationToken: fx.Ct);
var reorganized = (await fx.Db.Sequences.GetAsync(sequence.Id))!;
Assert.NotEqual(sequence.Folder, reorganized.Folder);
Assert.FileExists(fx.Paths.Sequence(reorganized.Folder).ProjectRecordPath(vertical.Id));
Assert.SequenceEqual([main.Id, vertical.Id], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
var pushed = await projects.PushAsync(main.Id, Draft(await projects.HeadAsync(main.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct);
Assert.Equal(PushOutcome.Stored, pushed.Outcome);
Assert.FileExists(fx.Paths.Sequence(reorganized.Folder).VersionPath(main.Id, 2, pushed.Version!.Id));
// Another index of the archive moved it away: until this index finds it there, its projects are read from the index (none
// taken for gone) and nothing is written where it was, which is never made again. A workspace hears its home is out of
// reach for now, and reads its head from the index meanwhile.
var away = "2026/Away " + Guid.NewGuid().ToString("N")[..6];
Directory.CreateDirectory(Path.GetDirectoryName(fx.Paths.Sequence(away).Folder)!);
Directory.Move(fx.Paths.Sequence(reorganized.Folder).Folder, fx.Paths.Sequence(away).Folder);
Assert.SequenceEqual([main.Id, vertical.Id], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
var head = await projects.HeadAsync(main.Id, fx.Ct);
await Assert.ThrowsAsync<SequenceFolderMovedException>(() => projects.PushAsync(main.Id, Draft(head, new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct));
var home = new ArchiveProjectHome(fx.Db, projects, sequence.Id);
await Assert.ThrowsAsync<HomeUnavailableException>(() => home.PushAsync(main.Id, Draft(head, new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct));
await Assert.ThrowsAsync<HomeUnavailableException>(() => home.CreateAsync("Square", null, null, fx.Ct));
Assert.Equal(head.Id, (await home.HeadAsync(main.Id, fx.Ct)).Id);
Assert.False(Directory.Exists(fx.Paths.Sequence(reorganized.Folder).Folder));
// Another sequence moved into the folder it left is no home of its projects either: none is read from it or written there.
var other = (await UploadedAsync(fx, 1, seed: 300)).Sequence;
Directory.Move(fx.Paths.Sequence(other.Folder).Folder, fx.Paths.Sequence(reorganized.Folder).Folder);
await Assert.ThrowsAsync<SequenceFolderMovedException>(() => projects.RenameAsync(vertical.Id, "Vertical 2", fx.Ct));
Assert.False(Directory.Exists(fx.Paths.Sequence(reorganized.Folder).ProjectsDirectory));
Assert.SequenceEqual([main.Id, vertical.Id], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
await fx.Db.Sequences.RelocateAsync(sequence.Id, away, reorganized.Title, fx.Ct);
Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(main.Id, Draft(head, new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct)).Outcome);
Assert.Null((await projects.GetAsync(vertical.Id, fx.Ct))!.DeletedAt);
}
[Test]
public async Task AVersionKeepsItsPositionsWhenItsPhotosAreConverted()
{
var fx = await FixtureAsync();
var (sequence, frames) = await UploadedAsync(fx, 4);
var projects = fx.Service.Projects;
await WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { ExcludedFrames = [1], HeroFrame = 2 });
var main = await projects.MainAsync(sequence.Id, fx.Ct);
var head = await projects.HeadAsync(main.Id, fx.Ct);
// A Replace conversion (phase 4b) gives frames 1 and 2 other files: the same frames, other SHA-256.
await ConvertAsync(fx, sequence.Id, [1, 2], 700);
var home = await projects.FramesAsync(sequence.Id, fx.Ct);
Assert.NotEqual(frames[1].Sha256, home.Frames[1].Sha256);
Assert.Equal(frames[1].Id, home.Frames[1].Id);
// The version parked the photos as they were: it still names the converted frames.
var placed = ProjectSettings.FromJson(ProjectContent.Place(head.SettingsJson, home.Photos));
Assert.SequenceEqual([1], placed.ExcludedFrames);
Assert.Equal((int?)2, placed.HeroFrame);
// The sequence's settings written again with the same choices are no outside change: Main keeps its head.
var row = (await fx.Db.Sequences.GetAsync(sequence.Id))!;
await WriteSettingsAsync(fx, sequence.Id, ProjectSettings.FromJson(row.SettingsJson!));
Assert.Equal(head.Id, (await projects.HeadAsync(main.Id, fx.Ct)).Id);
// A version pushed on top of it is mirrored onto the converted frames.
var pushed = await projects.PushAsync(main.Id, Draft(head, new ProjectSettings { ExcludedFrames = [1, 3], HeroFrame = 2 }, home.Frames), fx.Ct);
Assert.Equal(PushOutcome.Stored, pushed.Outcome);
var mirrored = ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!);
Assert.SequenceEqual([1, 3], mirrored.ExcludedFrames);
Assert.Equal((int?)2, mirrored.HeroFrame);
}
}
public sealed class SqliteProjectServiceTests(TestContext context) : ProjectServiceTests(context)
{
protected override Backend Backend => Backend.SqliteFile;
}
[RequiresEnvironment(PgTestServer.Variable)]
public sealed class PostgresProjectServiceTests(TestContext context) : ProjectServiceTests(context)
{
protected override Backend Backend => Backend.Postgres;
}