using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using Chronos.Core.Model;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Data.Jobs;
using Chronos.Server.Configuration;
using Chronos.Server.Storage;
namespace Chronos.Server.Tests;
///
/// Stand-in for the archive module's ingest: files staged uploads as <folder>/originals/<name>, one
/// sequence per session key, and records every request it received.
///
public sealed class FakeArchiveIngest(ChronosDatabase db) : IArchiveIngest
{
private readonly ConcurrentDictionary _sessions = new(StringComparer.Ordinal);
private readonly SemaphoreSlim _gate = new(1, 1);
public ConcurrentQueue Requests { get; } = new();
public async Task CommitAsync(IngestRequest request, CancellationToken cancellationToken = default)
{
Requests.Enqueue(request);
await _gate.WaitAsync(cancellationToken);
try
{
var archive = (await db.Archives.GetAsync(request.ArchiveId, cancellationToken))!;
var sequence = await SequenceForAsync(request, archive, cancellationToken);
var existing = (await db.Frames.FindBySha256Async(request.Sha256, cancellationToken)).FirstOrDefault(f => f.SequenceId == sequence.Id);
if (existing is not null)
{
File.Delete(request.StagedPath);
return new IngestResult(sequence.Id, existing.Id, existing.Index, existing.RelativePath, Duplicate: true);
}
var index = sequence.FrameCount;
var relative = $"{sequence.Folder}/originals/{index:D4}_{request.OriginalFileName}";
var target = Path.Combine(archive.RootPath, relative);
Directory.CreateDirectory(Path.GetDirectoryName(target)!);
File.Move(request.StagedPath, target);
var frame = new Frame
{
Id = Guid.CreateVersion7(),
SequenceId = sequence.Id,
Index = index,
RelativePath = relative,
FileName = Path.GetFileName(relative),
OriginalFileName = request.OriginalFileName,
Size = request.Size,
Sha256 = request.Sha256,
CapturedAt = request.ClientCapturedAt,
};
await db.Frames.InsertAsync(frame, cancellationToken);
await db.Sequences.RefreshStatsAsync(sequence.Id, cancellationToken);
return new IngestResult(sequence.Id, frame.Id, index, relative, Duplicate: false);
}
finally
{
_gate.Release();
}
}
private async Task SequenceForAsync(IngestRequest request, Archive archive, CancellationToken cancellationToken)
{
Sequence? sequence = null;
if (request.SequenceId is { } id) sequence = await db.Sequences.GetAsync(id, cancellationToken);
else if (request.SessionKey is { } key && _sessions.TryGetValue(key, out var known)) sequence = await db.Sequences.GetAsync(known, cancellationToken);
if (sequence is null)
{
sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = archive.Id, Folder = string.Empty, Title = request.NewSequenceTitle, OwnerUserId = request.UserId };
if (request.SessionKey is { } key) _sessions[key] = sequence.Id;
}
if (string.IsNullOrEmpty(sequence.Folder))
{
sequence = sequence with { Folder = $"2026/{sequence.Id:N}", UpdatedAt = DateTimeOffset.UtcNow };
await db.Sequences.UpsertAsync(sequence, cancellationToken);
}
return sequence;
}
}
/// Proxies at <folder>/proxies/NNNN.jpg; thumbnails as 64-byte slots of <folder>/.chronos/thumbs.pack.
public sealed class FakeAssetLocator : IFrameAssetLocator
{
public const int ThumbnailSize = 64;
/// When set, every lookup answers this location (path traversal tests).
public AssetLocation? Override { get; set; }
public ValueTask LocateAsync(Archive archive, Sequence sequence, Frame frame, FrameAsset asset, CancellationToken cancellationToken)
{
if (Override is not null) return ValueTask.FromResult(Override);
var folder = Path.Combine(archive.RootPath, sequence.Folder);
AssetLocation? location = asset == FrameAsset.Proxy
? new AssetLocation(Path.Combine(folder, "proxies", $"{frame.Index:D4}.jpg"), 0, -1, "image/jpeg")
: new AssetLocation(Path.Combine(folder, ".chronos", "thumbs.pack"), (long)frame.Index * ThumbnailSize, ThumbnailSize, "image/jpeg");
if (!File.Exists(location.Path)) return ValueTask.FromResult(null);
if (location.Length < 0) location = location with { Length = new FileInfo(location.Path).Length };
return ValueTask.FromResult(location);
}
}
/// Measures volumes for real, except the folders given figures of their own (a full archive, a separate array).
public sealed class FakeVolumeSpace(ServerOptions options, ILogger logger) : VolumeSpace(options, logger)
{
private readonly ConcurrentDictionary _figures = new(StringComparer.Ordinal);
/// Gives these figures; null measures it for real again.
public void Set(string path, VolumeFigures? figures)
{
if (figures is null) _figures.TryRemove(Key(path), out _);
else _figures[Key(path)] = figures;
}
public override VolumeFigures? Measure(string path) => _figures.TryGetValue(Key(path), out var figures) ? figures : base.Measure(path);
private static string Key(string path) => Path.TrimEndingDirectorySeparator(Path.GetFullPath(path));
}
/// A clock tests move by hand (token expiry, heartbeat timeouts).
public sealed class ManualTimeProvider : TimeProvider
{
private long _ticks = DateTimeOffset.UtcNow.UtcTicks;
public override DateTimeOffset GetUtcNow() => new(Interlocked.Read(ref _ticks), TimeSpan.Zero);
public void Advance(TimeSpan by) => Interlocked.Add(ref _ticks, by.Ticks);
}
///
/// A clock tests move by hand whose timers (, Task.Delay, timed cancellation) fire only
/// when reaches them, never in real time: the loops paced by it run exactly when a test says.
///
public sealed class SteppedTimeProvider : TimeProvider
{
private readonly Lock _gate = new();
private readonly List _timers = [];
private DateTimeOffset _now = DateTimeOffset.UtcNow;
public override DateTimeOffset GetUtcNow()
{
lock (_gate) return _now;
}
public override ITimer CreateTimer(TimerCallback callback, object? state, TimeSpan dueTime, TimeSpan period)
{
var timer = new SteppedTimer(this, callback, state);
timer.Change(dueTime, period);
return timer;
}
/// Moves the clock on, then fires each timer due by then once (a periodic one moves to its next period after now).
public void Advance(TimeSpan by)
{
List due;
lock (_gate)
{
_now += by;
due = [.. _timers.Where(t => t.Due <= _now)];
foreach (var timer in due)
{
if (timer.Period <= TimeSpan.Zero) _timers.Remove(timer);
else
{
while (timer.Due <= _now) timer.Due += timer.Period;
}
}
}
foreach (var timer in due) timer.Fire();
}
private void Schedule(SteppedTimer timer, TimeSpan dueTime, TimeSpan period)
{
lock (_gate)
{
_timers.Remove(timer);
if (dueTime == Timeout.InfiniteTimeSpan) return;
timer.Due = _now + dueTime;
timer.Period = period == Timeout.InfiniteTimeSpan ? TimeSpan.Zero : period;
_timers.Add(timer);
}
}
private void Remove(SteppedTimer timer)
{
lock (_gate) _timers.Remove(timer);
}
private sealed class SteppedTimer(SteppedTimeProvider clock, TimerCallback callback, object? state) : ITimer
{
public DateTimeOffset Due { get; set; }
public TimeSpan Period { get; set; }
public bool Change(TimeSpan dueTime, TimeSpan period)
{
clock.Schedule(this, dueTime, period);
return true;
}
public void Fire() => callback(state);
public void Dispose() => clock.Remove(this);
public ValueTask DisposeAsync()
{
Dispose();
return ValueTask.CompletedTask;
}
}
}
///
/// A job handler that reports progress and then waits until (or cancellation), so tests can
/// observe running, paused and cancelled states.
///
public sealed class BlockingJobHandler(JobKind kind) : IJobHandler
{
private TaskCompletionSource _release = new(TaskCreationOptions.RunContinuationsAsynchronously);
public JobKind Kind => kind;
public TaskCompletionSource Started { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
/// When set, the next run fails with this message.
public string? FailWith { get; set; }
public void Release() => _release.TrySetResult();
public async Task RunAsync(JobContext context)
{
await context.ReportAsync(1, 4, "started");
Started.TrySetResult();
if (FailWith is { } message)
{
FailWith = null;
throw new InvalidOperationException(message);
}
for (var i = 2; i <= 3; i++)
{
await Task.Delay(50, context.Cancellation);
await context.ReportAsync(i, 4);
}
await _release.Task.WaitAsync(context.Cancellation);
_release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
await context.ReportAsync(4, 4);
}
}