Files
Chronos/tests/Titano.App.Tests/Uploader/UploadEngineTests.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

226 lines
9.0 KiB
C#

using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using Titano.Protocol;
using Titano.Protocol.Client;
using Titano.Testing;
using Titano.Uploader.Uploads;
namespace Titano.App.Tests.Uploader;
public sealed class UploadEngineTests(TestContext context)
{
private readonly ManualTime _time = new();
[Test]
public void VerifiedUploadsKeepTheSourceByDefault() => SingleThreadedContext.Run(async () =>
{
var queue = Queue(UploadTargetMode.Sessions, 2);
var transport = new FakeTransport();
using var engine = new UploadEngine(queue, transport, () => false, _time);
var confirmed = new List<UploadItem>();
engine.ItemConfirmed += (_, item) => confirmed.Add(item);
engine.Pump();
await Until(() => queue.Items.All(i => i.IsDone));
Assert.True(queue.Items.All(i => i.State == UploadItemState.Verified && i.FrameId is not null));
Assert.True(queue.Items.All(i => File.Exists(i.Path)));
// Two workers hash in parallel, so calls arrive in either order.
foreach (var item in queue.Items) Assert.Equal(Sha256(item.Path), transport.Calls.Single(c => c.Path == item.Path).Sha256);
Assert.Equal(2, confirmed.Count);
});
[Test]
public void DeleteAfterUploadRemovesOnlyVerifiedUnchangedFiles() => SingleThreadedContext.Run(async () =>
{
var queue = Queue(UploadTargetMode.Sessions, 3);
var (verified, existing, changed) = (queue.Items[0], queue.Items[1], queue.Items[2]);
var transport = new FakeTransport
{
Respond = item =>
{
if (item == existing) return (UploadOutcome.AlreadyOnServer, null);
// Written to after it was hashed: the bytes on disk are no longer the verified ones.
if (item == changed) File.AppendAllText(item.Path, "more");
return (UploadOutcome.Verified, Guid.CreateVersion7());
},
};
using var engine = new UploadEngine(queue, transport, () => true, _time);
engine.Pump();
await Until(() => queue.Items.All(i => i.IsDone));
Assert.Equal(UploadItemState.Skipped, existing.State);
Assert.False(File.Exists(verified.Path));
// The server matched its hash without receiving these bytes: no verified copy, so the source stays.
Assert.True(File.Exists(existing.Path), "a file the server already held is kept");
Assert.True(File.Exists(changed.Path), "a file changed after verification is kept");
});
[Test]
public void RejectedFileIsRetriedAfterThirtySeconds() => SingleThreadedContext.Run(async () =>
{
var queue = Queue(UploadTargetMode.Sessions, 1);
var item = queue.Items[0];
var attempts = 0;
var transport = new FakeTransport
{
Respond = _ => ++attempts == 1
? throw new TitanoApiException(HttpStatusCode.UnprocessableEntity, new ApiError("hash_mismatch", "Hash mismatch"))
: (UploadOutcome.Verified, Guid.CreateVersion7()),
};
using var engine = new UploadEngine(queue, transport, () => false, _time);
engine.Pump();
await Until(() => item.State == UploadItemState.Failed);
Assert.Equal(UploadFailure.ServerRejected, item.Failure);
Assert.Equal("Hash mismatch", item.FailureDetail);
Assert.Equal(_time.UtcNow + UploadQueue.RetryDelay, item.NextRetryUtc);
_time.Advance(TimeSpan.FromSeconds(29));
engine.Pump();
Assert.Equal(UploadItemState.Failed, item.State);
_time.Advance(TimeSpan.FromSeconds(1));
engine.Pump();
await Until(() => item.IsDone);
Assert.Equal(1, item.Retries);
Assert.Equal(UploadFailure.None, item.Failure);
});
[Test]
public void UnreachableServerRequeuesWithoutSpendingARetry() => SingleThreadedContext.Run(async () =>
{
var queue = Queue(UploadTargetMode.Sessions, 1);
var item = queue.Items[0];
var transport = new FakeTransport();
transport.Respond = _ =>
{
transport.SetReady(false);
throw new ServerUnavailableException(ServerProblem.Unreachable);
};
using var engine = new UploadEngine(queue, transport, () => false, _time);
engine.Pump();
await Until(() => item.State == UploadItemState.Queued && transport.Calls.Count == 1);
Assert.Equal(0, item.Retries);
Assert.NotNull(item.Sha256);
Assert.Equal(EngineStatus.WaitingForServer, engine.Status);
transport.Respond = _ => (UploadOutcome.Verified, Guid.CreateVersion7());
transport.SetReady(true);
await Until(() => item.IsDone);
});
[Test]
public void NewSequenceIsCreatedOnceForTheBatch() => SingleThreadedContext.Run(async () =>
{
var queue = Queue(UploadTargetMode.NewSequence, 3);
var transport = new FakeTransport();
using var engine = new UploadEngine(queue, transport, () => false, _time);
engine.Pump();
await Until(() => queue.Items.All(i => i.IsDone));
Assert.Equal(1, transport.SequencesCreated);
Assert.True(transport.Calls.All(c => c.SequenceId == FakeTransport.CreatedSequence));
Assert.Equal(FakeTransport.CreatedSequence, queue.Batches.Single().SequenceId);
});
[Test]
public void AtMostTwoFilesAreInFlightAndPauseRequeuesThem() => SingleThreadedContext.Run(async () =>
{
var queue = Queue(UploadTargetMode.Sessions, 4);
var transport = new FakeTransport { Block = true };
using var engine = new UploadEngine(queue, transport, () => false, _time);
engine.Pump();
await Until(() => queue.Items.Count(i => i.State == UploadItemState.Uploading) == UploadEngine.Parallelism);
Assert.Equal(UploadEngine.Parallelism, queue.Items.Count(i => i.IsActive));
engine.Pause();
await Until(() => queue.Items.All(i => i.State == UploadItemState.Queued));
Assert.Equal(EngineStatus.Paused, engine.Status);
transport.Block = false;
engine.Resume();
await Until(() => queue.Items.All(i => i.IsDone));
Assert.False(queue.IsPaused);
});
private UploadQueue Queue(UploadTargetMode mode, int count)
{
var dir = context.CreateTempDirectory();
var queue = new UploadQueue();
var entries = Enumerable.Range(0, count).Select(i =>
{
var path = Path.Combine(dir, $"IMG_{i:0000}.JPG");
File.WriteAllBytes(path, RandomNumberGenerator.GetBytes(4096 + i));
var info = new FileInfo(path);
return new QueueEntry(path, info.Length, info.LastWriteTimeUtc, info.LastWriteTime, "k", null);
}).ToList();
queue.Add(new UploadBatch { Id = Guid.CreateVersion7(), Mode = mode, Title = mode == UploadTargetMode.NewSequence ? "Roof" : null }, entries);
return queue;
}
private static string Sha256(string path) => Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(path)));
private static async Task Until(Func<bool> condition)
{
var clock = Stopwatch.StartNew();
while (!condition())
{
if (clock.Elapsed > TimeSpan.FromSeconds(15)) throw new TimeoutException("Condition not reached");
await Task.Delay(5);
}
}
private sealed class ManualTime : TimeProvider
{
private DateTimeOffset _now = new(2026, 9, 28, 18, 0, 0, TimeSpan.Zero);
public DateTime UtcNow => _now.UtcDateTime;
public void Advance(TimeSpan by) => _now += by;
public override DateTimeOffset GetUtcNow() => _now;
}
private sealed class FakeTransport : IUploadTransport
{
public static readonly Guid CreatedSequence = Guid.CreateVersion7();
public event EventHandler? ReadyChanged;
public bool IsReady { get; private set; } = true;
public bool Block { get; set; }
public int SequencesCreated { get; private set; }
public List<(string Path, Guid? SequenceId, string Sha256)> Calls { get; } = [];
public Func<UploadItem, (UploadOutcome, Guid?)> Respond { get; set; } = _ => (UploadOutcome.Verified, Guid.CreateVersion7());
public void SetReady(bool ready)
{
IsReady = ready;
ReadyChanged?.Invoke(this, EventArgs.Empty);
}
public async Task<(UploadOutcome Outcome, Guid? FrameId)> UploadAsync(UploadItem item, Guid? sequenceId, string sha256, IProgress<UploadProgress> progress, CancellationToken ct)
{
Calls.Add((item.Path, sequenceId, sha256));
progress.Report(new UploadProgress(0, item.Size));
while (Block) await Task.Delay(5, ct);
await Task.Yield();
progress.Report(new UploadProgress(item.Size, item.Size));
return Respond(item);
}
public Task<Guid> CreateSequenceAsync(string? title, CancellationToken ct)
{
SequencesCreated++;
return Task.FromResult(CreatedSequence);
}
}
}