Merge branch 'fix/ingest-upload': uploads into API-made sequences, damaged frames repaired, positions follow inserts, cardEvent

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 11:44:09 +00:00
commit 3b1b743fcd
29 files changed
+1546 -80

No files matched your search

@@ -1,5 +1,10 @@
using System.Text.Json.Nodes;
using Titano.Core.Archiving;
using Titano.Core.IO;
using Titano.Core.Model;
using Titano.Core.Project;
using Titano.Data.Archiving;
using Titano.Data.Repositories;
using Titano.Testing;
using static Titano.Data.Tests.Archiving.ArchiveFixture;
@@ -121,4 +126,209 @@ public sealed class ArchiveIngestTests(TestContext context)
Assert.FileMissing(sequence.IngestJournalPath);
Assert.Equal(3, (await fx.Db.Frames.ListBySequenceAsync(first.SequenceId)).Count);
}
[Test]
public async Task AMissingFrameIsRepairedByTheSameContentInsteadOfCountingAsDuplicate()
{
await using var fx = await CreateAsync(context);
var original = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start, 7)), fx.Ct);
var row = (await fx.Db.Sequences.GetAsync(original.SequenceId))!;
var file = Path.Combine(fx.Archive.RootPath, original.RelativePath);
var bytes = await File.ReadAllBytesAsync(file, fx.Ct);
File.Delete(file);
await fx.Service.Indexer.ReconcileAsync(fx.Archive, row, fx.Ct);
Assert.Equal(FrameFlags.Missing, (await fx.Db.Frames.GetAsync(original.FrameId))!.Flags);
var again = fx.Stage(Start, 7);
var repaired = await fx.Service.Ingest.CommitAsync(Request(fx, again), fx.Ct);
Assert.False(repaired.Duplicate, "the server holds no intact copy: this one is filed");
Assert.Equal((false, true), (original.Repaired, repaired.Repaired));
Assert.Equal((original.FrameId, original.Index, original.RelativePath), (repaired.FrameId, repaired.Index, repaired.RelativePath));
Assert.SequenceEqual(bytes, await File.ReadAllBytesAsync(file, fx.Ct));
Assert.FileMissing(again.Path);
Assert.Equal(FrameFlags.None, (await fx.Db.Frames.GetAsync(original.FrameId))!.Flags);
var manifest = ManifestStore.ReadSequence(fx.Paths.Sequence(row.Folder).ManifestPath);
Assert.Equal(FrameFlags.None, manifest.Frames.Single().Flags);
Assert.False((await fx.Db.Sequences.GetAsync(original.SequenceId))!.Status.HasFlag(SequenceStatus.HasErrors));
// Intact again: the next copy is a duplicate and stays in staging.
var third = fx.Stage(Start, 7);
Assert.True((await fx.Service.Ingest.CommitAsync(Request(fx, third), fx.Ct)).Duplicate);
Assert.FileExists(third.Path);
}
[Test]
public async Task ACorruptFrameIsRepairedAndItsDamagedFileKeptAside()
{
await using var fx = await CreateAsync(context);
var original = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start, 7)), fx.Ct);
var file = Path.Combine(fx.Archive.RootPath, original.RelativePath);
var bytes = await File.ReadAllBytesAsync(file, fx.Ct);
var damaged = bytes.ToArray();
damaged[^10] ^= 0xFF;
await File.WriteAllBytesAsync(file, damaged, fx.Ct);
await fx.Service.Verifier.VerifyAsync(fx.Archive, null, default, (_, _, _) => ValueTask.CompletedTask, null, fx.Ct);
Assert.Equal(FrameFlags.Corrupt, (await fx.Db.Frames.GetAsync(original.FrameId))!.Flags);
var repaired = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start, 7)), fx.Ct);
Assert.Equal((false, true), (repaired.Duplicate, repaired.Repaired));
Assert.Equal(original.FrameId, repaired.FrameId);
Assert.SequenceEqual(bytes, await File.ReadAllBytesAsync(file, fx.Ct));
var aside = Directory.GetFiles(Path.GetDirectoryName(file)!, Path.GetFileName(file) + ".damaged-*");
Assert.Count(1, aside);
Assert.SequenceEqual(damaged, await File.ReadAllBytesAsync(aside[0], fx.Ct));
Assert.Equal(FrameFlags.None, (await fx.Db.Frames.GetAsync(original.FrameId))!.Flags);
// The kept file is no photo to a scan: the sequence still has one frame.
Assert.Equal(0, (await fx.ScanAsync(ScanMode.Full)).Failed.Count);
Assert.Equal(1, (await fx.Db.Sequences.GetAsync(original.SequenceId))!.FrameCount);
}
[Test]
public async Task ASequenceCreatedWithoutPhotosGetsItsFolderWithTheFirstFrame()
{
await using var fx = await CreateAsync(context);
// As POST /api/v1/sequences creates it: a row without a folder.
var created = new Sequence
{
Id = Guid.CreateVersion7(),
ArchiveId = fx.Archive.Id,
Title = "Harbour sunset",
Folder = string.Empty,
Tags = ["harbour"],
Rating = 4,
Origin = SequenceOrigin.Server,
OwnerUserId = Guid.CreateVersion7(),
UpdatedAt = DateTimeOffset.UtcNow,
};
await fx.Db.Sequences.UpsertAsync(created, fx.Ct);
var first = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start, 1), sequenceId: created.Id), fx.Ct);
var second = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddSeconds(10), 2), sequenceId: created.Id, name: "IMG_0002.JPG"), fx.Ct);
Assert.Equal((created.Id, created.Id), (first.SequenceId, second.SequenceId));
var row = (await fx.Db.Sequences.GetAsync(created.Id))!;
Assert.Equal("2026/2026-09-28 Canon EOS R5", row.Folder);
Assert.Equal(("Harbour sunset", 4, SequenceOrigin.Server, created.OwnerUserId, 2), (row.Title, row.Rating, row.Origin, row.OwnerUserId, row.FrameCount));
Assert.SequenceEqual(["harbour"], row.Tags);
Assert.Equal("2026/2026-09-28 Canon EOS R5/originals/2026-09-28_18-00-00_0001.JPG", first.RelativePath);
var manifest = ManifestStore.ReadSequence(fx.Paths.Sequence(row.Folder).ManifestPath);
Assert.Equal((created.Id, "Harbour sunset", created.OwnerUserId.ToString()), (manifest.Id, manifest.Title, manifest.CreatedBy));
Assert.SequenceEqual([first.FrameId, second.FrameId], manifest.Frames.Select(f => f.Id));
Assert.FileMissing(fx.Paths.Sequence(string.Empty).ManifestPath);
// The index can be rebuilt from the sidecar it now has.
Assert.Equal(0, (await fx.ScanAsync()).Indexed);
}
[Test]
public async Task ASequenceAdoptedAtTheRootIsNeverMovedWhenItsSidecarIsGone()
{
await using var fx = await CreateAsync(context);
Photos.WriteSeries(fx.Archive.RootPath, Start, 2);
await fx.ScanAsync();
var adopted = Assert.NotNull((await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id, fx.Ct)).SingleOrDefault());
Assert.Equal(string.Empty, adopted.Folder);
File.Delete(fx.Paths.Sequence(string.Empty).ManifestPath);
// It held photos, so it is no sequence waiting for a folder: the commit reports the missing sidecar.
var staged = fx.Stage(Start.AddMinutes(5), 3);
await Assert.ThrowsAsync<IOException>(() => fx.Service.Ingest.CommitAsync(Request(fx, staged, sequenceId: adopted.Id), fx.Ct));
var row = (await fx.Db.Sequences.GetAsync(adopted.Id))!;
Assert.Equal((string.Empty, 2), (row.Folder, row.FrameCount));
Assert.Count(2, await fx.Db.Frames.ListBySequenceAsync(adopted.Id));
Assert.False(Directory.Exists(Path.Combine(fx.Archive.RootPath, "2026")), "no folder was claimed for it");
Assert.FileExists(staged.Path);
}
[Test]
public async Task FramesInsertedBeforeOthersTakeTheProjectPositionsAlong()
{
await using var fx = await CreateAsync(context);
var results = new List<IngestResult>();
for (var i = 0; i < 5; i++) results.Add(await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddMinutes(1 + i), 10 + i)), fx.Ct));
var sequenceId = results[0].SequenceId;
var folder = fx.Paths.Sequence((await fx.Db.Sequences.GetAsync(sequenceId))!.Folder);
var project = new ProjectSettings
{
ExcludedFrames = [3],
HeroFrame = 1,
Camera = new CameraSettings { Keyframes = [new CameraKey(0, 0.5, 0.5, 1, 0), new CameraKey(4, 0.4, 0.5, 1.5, 0)] },
}.ToJson();
AtomicFile.WriteAllText(folder.ProjectPath, project);
var edited = await fx.Db.Sequences.EditAsync(sequenceId, new SequenceEdit { SettingsJson = project, HeroFrameId = results[1].FrameId }, null, fx.Ct);
var version = edited.Sequence!.SettingsVersion;
// Two photos captured before every other one, and one between the third and the fourth, arrive later.
await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start, 20)), fx.Ct);
await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddSeconds(30), 21)), fx.Ct);
await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddMinutes(3.5), 22)), fx.Ct);
var row = (await fx.Db.Sequences.GetAsync(sequenceId))!;
var frames = await fx.Db.Frames.ListBySequenceAsync(sequenceId);
var moved = ProjectSettings.FromJson(row.SettingsJson!);
Assert.SequenceEqual([results[3].FrameId], moved.ExcludedFrames.Select(i => frames[i].Id));
Assert.Equal(results[1].FrameId, frames[moved.HeroFrame!.Value].Id);
Assert.SequenceEqual([results[0].FrameId, results[4].FrameId], moved.Camera.Keyframes.Select(k => frames[k.Frame].Id));
Assert.Equal(version, row.SettingsVersion);
Assert.Equal(results[1].FrameId, row.HeroFrameId);
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(row.SettingsJson!), JsonNode.Parse(File.ReadAllText(folder.ProjectPath))), "project.json mirrors the index");
}
[Test]
public async Task PhotosAFolderScanFindsMoveTheProjectPositionsButUnknownMembersStay()
{
await using var fx = await CreateAsync(context);
var late = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddMinutes(1), 1)), fx.Ct);
await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddMinutes(2), 2)), fx.Ct);
var row = (await fx.Db.Sequences.GetAsync(late.SequenceId))!;
var folder = fx.Paths.Sequence(row.Folder);
// A project with a member from a newer Titano; only the index copy holds it (the API edited it).
const string project = "{ \"excludedFrames\": [1], \"heroFrame\": 0, \"futureOption\": { \"keep\": true } }";
await fx.Db.Sequences.EditAsync(late.SequenceId, new SequenceEdit { SettingsJson = project }, null, fx.Ct);
File.WriteAllBytes(folder.File("originals/early.JPG"), Photos.Jpeg(Start, 3));
await fx.Service.Indexer.ReconcileAsync(fx.Archive, (await fx.Db.Sequences.GetAsync(late.SequenceId))!, fx.Ct);
var settings = (await fx.Db.Sequences.GetAsync(late.SequenceId))!.SettingsJson!;
var moved = ProjectSettings.FromJson(settings);
Assert.SequenceEqual([2], moved.ExcludedFrames);
Assert.Equal(1, moved.HeroFrame);
Assert.Contains("futureOption", settings);
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(settings), JsonNode.Parse(File.ReadAllText(folder.ProjectPath))), "project.json mirrors the index");
}
[Test]
public async Task AScanMovesTheProjectPositionsOnlyBetweenProjectSaves()
{
await using var fx = await CreateAsync(context);
var late = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddMinutes(1), 1)), fx.Ct);
await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddMinutes(2), 2)), fx.Ct);
var id = late.SequenceId;
var folder = fx.Paths.Sequence((await fx.Db.Sequences.GetAsync(id))!.Folder);
var version = (await fx.Db.Sequences.EditAsync(id, new SequenceEdit { SettingsJson = "{ \"excludedFrames\": [1] }" }, null, fx.Ct)).Sequence!.SettingsVersion;
// Another process files a photo captured before both into the sidecar; this index learns of it from a scan.
var photos = ArchiveScanner.PhotoDirectory(folder);
var stamp = Directory.GetLastWriteTimeUtc(photos);
File.WriteAllBytes(folder.File("originals/early.JPG"), Photos.Jpeg(Start, 3));
ManifestStore.WriteSequence(folder.ManifestPath, SequenceAdopter.Reconcile(folder, ManifestStore.ReadSequence(folder.ManifestPath), fx.Ct)!);
Directory.SetLastWriteTimeUtc(photos, stamp);
// An editor saving the project holds the sequence lease (LocalSequenceSource.SaveProjectAsync).
var save = await LeaseFile.AcquireAsync(fx.Paths.LocksDirectory, $"seq-{id:N}", "editor", TimeSpan.FromSeconds(5), cancellationToken: fx.Ct);
var scan = Task.Run(() => fx.ScanAsync());
await Task.Delay(500, fx.Ct);
Assert.False(scan.IsCompleted, "the scan waits for the save");
// The save stores the positions its editor knew, as of the frames before the scan.
Assert.Equal(SequenceEditOutcome.Updated,
(await fx.Db.Sequences.EditAsync(id, new SequenceEdit { SettingsJson = "{ \"excludedFrames\": [0, 1] }" }, version, fx.Ct)).Outcome);
save.Dispose();
Assert.Equal(1, (await scan).Indexed);
var row = (await fx.Db.Sequences.GetAsync(id))!;
Assert.Equal(3, row.FrameCount);
Assert.SequenceEqual([1, 2], ProjectSettings.FromJson(row.SettingsJson!).ExcludedFrames);
}
}