Files
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

226 lines
9.0 KiB
C#

using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using Chronos.Protocol;
using Chronos.Protocol.Client;
using Chronos.Testing;
using Chronos.Uploader.Uploads;
namespace Chronos.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 ChronosApiException(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);
}
}
}