Files
Chronos/tests/Titano.Data.Tests/Archiving/ArchiveIngestTests.cs
T
Claude 94b13cc064 fix(ingest): file uploads into API-made sequences and repair damaged frames
A sequence created with POST /api/v1/sequences had no folder, and the ingest
looked for its sidecar at the archive root: every upload into it failed with
503 commit_failed, so the Uploader's "New sequence" target never worked. Its
first frame now claims a folder by the rules of a sequence created by an
upload (folder template with its title, unique variant, sidecar with the
row's id, title, tags, rating, notes and project), and the row takes the
folder in one transaction (DOCKER-1).

A frame flagged missing or corrupt, or whose file is gone or has another
size, no longer stands for an upload: the start short-circuit and crash
recovery match only intact frames still on disk, and the ingest repairs such
a frame with the verified copy (same id and position, flags cleared, a damaged
file kept beside it as name.damaged-<time>) instead of calling it a
duplicate. The Uploader deletes a source only after the server received and
verified its bytes in that upload, never on "already on the server" (SRV-1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 09:40:27 +00:00

219 lines
13 KiB
C#

using Titano.Core.Archiving;
using Titano.Core.Model;
using Titano.Data.Archiving;
using Titano.Testing;
using static Titano.Data.Tests.Archiving.ArchiveFixture;
namespace Titano.Data.Tests.Archiving;
[Category("archive")]
public sealed class ArchiveIngestTests(TestContext context)
{
private static IngestRequest Request(ArchiveFixture fx, (string Path, long Size, string Sha256) staged, string? sessionKey = "upload-1",
Guid? sequenceId = null, string? title = null, string name = "IMG_0001.JPG") =>
new(fx.Archive.Id, sequenceId, title, sequenceId is null && title is null ? sessionKey : null, staged.Path, name, staged.Size, staged.Sha256, null, null);
[Test]
public async Task ASessionKeyCreatesOneSequenceNamedByTheTemplates()
{
await using var fx = await CreateAsync(context);
var first = fx.Stage(Start, 1);
var a = await fx.Service.Ingest.CommitAsync(Request(fx, first), fx.Ct);
var b = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddSeconds(10), 2), name: "IMG_0002.JPG"), fx.Ct);
Assert.Equal(a.SequenceId, b.SequenceId);
Assert.Equal("2026/2026-09-28 Canon EOS R5/originals/2026-09-28_18-00-00_0001.JPG", a.RelativePath);
Assert.Equal("2026/2026-09-28 Canon EOS R5/originals/2026-09-28_18-00-10_0002.JPG", b.RelativePath);
Assert.Equal((0, 1), (a.Index, b.Index));
Assert.False(a.Duplicate);
Assert.FileMissing(first.Path);
var manifest = ManifestStore.ReadSequence(fx.Paths.Sequence("2026/2026-09-28 Canon EOS R5").ManifestPath);
Assert.Equal(a.SequenceId, manifest.Id);
Assert.SequenceEqual(["upload-1"], manifest.SessionKeys);
Assert.Equal("IMG_0002.JPG", manifest.Frames[1].Original);
Assert.Equal(first.Sha256, manifest.Frames[0].Sha256);
Assert.Equal(2, (await fx.Db.Sequences.GetAsync(a.SequenceId))!.FrameCount);
Assert.Equal(0, (await fx.ScanAsync()).Indexed);
}
[Test]
public async Task TheSameContentIsReportedAsDuplicateAndNotCopiedTwice()
{
await using var fx = await CreateAsync(context);
var original = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start, 7)), fx.Ct);
var again = fx.Stage(Start, 7);
var duplicate = await fx.Service.Ingest.CommitAsync(Request(fx, again), fx.Ct);
Assert.True(duplicate.Duplicate);
Assert.Equal(original.FrameId, duplicate.FrameId);
Assert.Equal(original.RelativePath, duplicate.RelativePath);
Assert.FileExists(again.Path);
Assert.Equal(1, (await fx.Db.Sequences.GetAsync(original.SequenceId))!.FrameCount);
}
[Test]
public async Task ParallelCommitsToOneSequenceGiveGapFreeIndexesAndAValidSidecar()
{
await using var fx = await CreateAsync(context);
const int count = 40;
var staged = Enumerable.Range(0, count).Select(i => fx.Stage(Start.AddSeconds(10 * i), 1000 + i)).ToList();
// Arrival order differs from capture order, as with parallel chunked uploads.
var order = Enumerable.Range(0, count).OrderBy(i => (i * 17) % count).ToList();
var results = await Task.WhenAll(order.Select(i => Task.Run(() =>
fx.Service.Ingest.CommitAsync(Request(fx, staged[i], name: $"IMG_{i:0000}.JPG"), fx.Ct))));
Assert.Count(1, results.Select(r => r.SequenceId).Distinct());
Assert.False(results.Any(r => r.Duplicate));
var sequenceId = results[0].SequenceId;
var row = (await fx.Db.Sequences.GetAsync(sequenceId))!;
var manifest = ManifestStore.ReadSequence(fx.Paths.Sequence(row.Folder).ManifestPath);
Assert.SequenceEqual(Enumerable.Range(0, count), manifest.Frames.Select(f => f.I));
Assert.SequenceEqual(Enumerable.Range(0, count).Select(i => Start.AddSeconds(10 * i)), manifest.Frames.Select(f => f.CapturedAt!.Value));
Assert.Count(count, manifest.Frames.Select(f => f.File).Distinct());
Assert.True(manifest.Frames.All(f => File.Exists(fx.Paths.Sequence(row.Folder).File(f.File))));
var frames = await fx.Db.Frames.ListBySequenceAsync(sequenceId);
Assert.SequenceEqual(manifest.Frames.Select(f => (f.Id, f.I)), frames.Select(f => (f.Id, f.Index)));
Assert.Equal(count, row.FrameCount);
Assert.Empty(Directory.EnumerateFiles(Path.Combine(fx.Paths.StagingDirectory, "uploads")));
}
[Test]
public async Task ExplicitTargetsUseTheGivenSequenceOrTitle()
{
await using var fx = await CreateAsync(context);
var titled = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start, 1), title: "Harbour"), fx.Ct);
var sameTitle = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddSeconds(5), 2), title: "harbour "), fx.Ct);
var byId = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddSeconds(9), 3), sequenceId: titled.SequenceId), fx.Ct);
Assert.Equal(titled.SequenceId, sameTitle.SequenceId);
Assert.Equal(titled.SequenceId, byId.SequenceId);
Assert.Equal("Harbour", (await fx.Db.Sequences.GetAsync(titled.SequenceId))!.Title);
await Assert.ThrowsAsync<ArgumentException>(() => fx.Service.Ingest.CommitAsync(
new IngestRequest(fx.Archive.Id, titled.SequenceId, "Both", null, fx.Stage(Start, 4).Path, "x.jpg", 1, "00", null, null), fx.Ct));
}
[Test]
public async Task AFrameCapturedEarlierIsInsertedInOrderAndTheIndexFollows()
{
await using var fx = await CreateAsync(context);
var late = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddMinutes(1), 1)), fx.Ct);
var early = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start, 2)), fx.Ct);
Assert.Equal(0, early.Index);
var frames = await fx.Db.Frames.ListBySequenceAsync(late.SequenceId);
Assert.SequenceEqual([early.FrameId, late.FrameId], frames.Select(f => f.Id));
Assert.SequenceEqual([0, 1], frames.Select(f => f.Index));
}
[Test]
public async Task ABatchInterruptedBetweenMoveAndSidecarIsCompletedOnTheNextCommit()
{
await using var fx = await CreateAsync(context);
var first = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start, 1)), fx.Ct);
var row = (await fx.Db.Sequences.GetAsync(first.SequenceId))!;
var sequence = fx.Paths.Sequence(row.Folder);
// Simulated crash: the journal was written and the file moved, but sequence.json was not rewritten.
var bytes = Photos.Jpeg(Start.AddSeconds(10), 2);
File.WriteAllBytes(sequence.File("originals/moved.JPG"), bytes);
var orphan = new SidecarFrame { Id = Guid.CreateVersion7(), File = "originals/moved.JPG", Size = bytes.Length, CapturedAt = Start.AddSeconds(10) };
IngestJournal.Write(sequence, new IngestJournal([orphan]));
var next = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddSeconds(20), 3)), fx.Ct);
Assert.Equal(2, next.Index);
var manifest = ManifestStore.ReadSequence(sequence.ManifestPath);
Assert.SequenceEqual([first.FrameId, orphan.Id, next.FrameId], manifest.Frames.Select(f => f.Id));
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((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.False(repaired.Duplicate);
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);
}
}