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(); 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 DeleteAfterUploadRemovesOnlyConfirmedUnchangedFiles() => 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)); Assert.False(File.Exists(existing.Path)); 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 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 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 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 CreateSequenceAsync(string? title, CancellationToken ct) { SequencesCreated++; return Task.FromResult(CreatedSequence); } } }