Per-file states (queued, hashing, uploading, verified, skipped, failed with reason), batches with their target (auto sessions, sequence, new sequence), uploader-queue.json persistence with interrupted transfers resuming as queued, photo scanning of files/folders/card roots, BRIEF §13 session keys (45 min gap, test shots merged), throughput meter and culture-aware queue/progress lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
125 lines
5.1 KiB
C#
125 lines
5.1 KiB
C#
using System.Globalization;
|
|
using Titano.Testing;
|
|
using Titano.Uploader.Text;
|
|
using Titano.Uploader.Uploads;
|
|
|
|
namespace Titano.App.Tests.Uploader;
|
|
|
|
public sealed class UploaderTextTests
|
|
{
|
|
private static readonly CultureInfo En = CultureInfo.GetCultureInfo("en");
|
|
private static readonly CultureInfo It = CultureInfo.GetCultureInfo("it");
|
|
|
|
private static readonly QueueSummary Mockup = new(4_812, 96_200_000_000, 2_214, 2_201, 0, 0, 44_000_000_000, "2026-09-28_18-02-11_0413.CR2")
|
|
{
|
|
RemainingBytes = 93_480_000_000,
|
|
};
|
|
|
|
[Test]
|
|
public void QueueLineMatchesTheMockup()
|
|
{
|
|
var left = RateMeter.TimeLeft(Mockup.RemainingBytes, 38_000_000);
|
|
Assert.Equal("4,812 files · 96.2 GB · 38 MB/s · 41 min left", UploaderText.QueueLine(Mockup, QueueActivity.Running, 38_000_000, left, En));
|
|
}
|
|
|
|
[Test]
|
|
public void QueueLineFollowsTheCulture()
|
|
{
|
|
Assert.Equal("4.812 file · 96,2 GB · 38 MB/s · mancano 41 min", UploaderText.QueueLine(Mockup, QueueActivity.Running, 38_000_000, TimeSpan.FromMinutes(41), It));
|
|
}
|
|
|
|
[Test]
|
|
public void QueueLineNamesTheActivityWhenNotRunning()
|
|
{
|
|
Assert.Equal("4,812 files · 96.2 GB · Paused", UploaderText.QueueLine(Mockup, QueueActivity.Paused, 0, null, En));
|
|
Assert.Equal("4,812 files · 96.2 GB · Waiting for server", UploaderText.QueueLine(Mockup, QueueActivity.WaitingForServer, 0, null, En));
|
|
Assert.Equal("1 file · 25 MB · Done", UploaderText.QueueLine(Mockup with { Files = 1, Bytes = 25_000_000 }, QueueActivity.Done, 0, null, En));
|
|
Assert.Equal("Nothing queued", UploaderText.QueueLine(default, QueueActivity.Idle, 0, null, En));
|
|
}
|
|
|
|
[Test]
|
|
public void RunningLineOmitsRateAndTimeUntilKnown()
|
|
{
|
|
Assert.Equal("4,812 files · 96.2 GB", UploaderText.QueueLine(Mockup, QueueActivity.Running, 0, null, En));
|
|
}
|
|
|
|
[Test]
|
|
public void ProgressLineMatchesTheMockup()
|
|
{
|
|
Assert.Equal("2,214 of 4,812 sent · 2,201 verified by the server · 0 retries", UploaderText.ProgressLine(Mockup, En));
|
|
Assert.Equal("2,214 of 4,812 sent · 2,201 verified by the server · 1 retry · 3 failed", UploaderText.ProgressLine(Mockup with { Retries = 1, Failed = 3 }, En));
|
|
}
|
|
|
|
[Theory]
|
|
[Case(0.0, "0 B/s")]
|
|
[Case(950_000.0, "950 KB/s")]
|
|
[Case(4_240_000.0, "4.2 MB/s")]
|
|
[Case(38_400_000.0, "38 MB/s")]
|
|
[Case(1_260_000_000.0, "1.3 GB/s")]
|
|
public void RatesUseDecimalUnits(double bytesPerSecond, string expected) =>
|
|
Assert.Equal(expected, UploaderText.Rate(bytesPerSecond, En));
|
|
|
|
[Theory]
|
|
[Case(40.0, "40 s")]
|
|
[Case(299.0, "5 min")]
|
|
[Case(3_590.0, "1 h 00 m")]
|
|
[Case(7_500.0, "2 h 05 m")]
|
|
[Case(93_600.0, "1 d 2 h")]
|
|
public void DurationsAreCompact(double seconds, string expected) =>
|
|
Assert.Equal(expected, UploaderText.Duration(TimeSpan.FromSeconds(seconds), En));
|
|
|
|
[Theory]
|
|
[Case(999L, "999 B")]
|
|
[Case(1_536L, "1.5 KB")]
|
|
[Case(96_200_000_000L, "96.2 GB")]
|
|
[Case(1_900_000_000_000L, "1.9 TB")]
|
|
public void SizesUseDecimalUnits(long bytes, string expected) => Assert.Equal(expected, UploaderText.Bytes(bytes, En));
|
|
|
|
[Test]
|
|
public void TimeLeftNeedsAUsableRate()
|
|
{
|
|
Assert.Equal(TimeSpan.FromMinutes(41), RateMeter.TimeLeft(93_480_000_000, 38_000_000));
|
|
Assert.Equal(TimeSpan.Zero, RateMeter.TimeLeft(0, 0));
|
|
Assert.Null(RateMeter.TimeLeft(1_000, 10));
|
|
}
|
|
|
|
[Test]
|
|
public void RateMeterAveragesTheWindowAndDecaysWhenStalled()
|
|
{
|
|
var meter = new RateMeter(TimeSpan.FromSeconds(10));
|
|
Assert.Equal(0.0, meter.BytesPerSecond(TimeSpan.Zero));
|
|
for (var s = 0; s <= 20; s++) meter.Add(4_000_000, TimeSpan.FromSeconds(s));
|
|
Assert.Approximately(4_000_000, meter.BytesPerSecond(TimeSpan.FromSeconds(20)), 1);
|
|
// Nothing sent for 20 s: the window holds no transfer any more.
|
|
Assert.Equal(0.0, meter.BytesPerSecond(TimeSpan.FromSeconds(40)));
|
|
meter.Reset();
|
|
Assert.Equal(0.0, meter.BytesPerSecond(TimeSpan.FromSeconds(40)));
|
|
}
|
|
|
|
[Test]
|
|
public void SummaryCountsStatesAndBytes()
|
|
{
|
|
var queue = new UploadQueue();
|
|
queue.Add(new UploadBatch { Id = Guid.CreateVersion7(), Mode = UploadTargetMode.Sessions },
|
|
Enumerable.Range(0, 5).Select(i => new QueueEntry($"{i}.jpg", 100, DateTime.UtcNow, null, null, null)));
|
|
queue.Items[0].State = UploadItemState.Verified;
|
|
queue.Items[1].State = UploadItemState.Skipped;
|
|
queue.Items[2].State = UploadItemState.Uploading;
|
|
queue.Items[2].BytesSent = 40;
|
|
queue.Items[3].State = UploadItemState.Failed;
|
|
queue.Items[3].Retries = 1;
|
|
|
|
var s = QueueSummary.From(queue.Items);
|
|
Assert.Equal(5, s.Files);
|
|
Assert.Equal(500L, s.Bytes);
|
|
Assert.Equal(2, s.Sent);
|
|
Assert.Equal(2, s.Verified);
|
|
Assert.Equal(1, s.Failed);
|
|
Assert.Equal(1, s.Retries);
|
|
Assert.Equal(240L, s.DoneBytes);
|
|
Assert.Equal(160L, s.RemainingBytes);
|
|
Assert.Equal("2.jpg", s.CurrentFile);
|
|
Assert.Approximately(0.48, s.Progress, 1e-9);
|
|
}
|
|
}
|