using System.Net;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Threading.Channels;
using Chronos.Core.Metadata;
using Chronos.Core.Model;
using Chronos.Data.Sync;
using Chronos.Protocol;
using Chronos.Protocol.Client;
namespace Chronos.Data.Tests.Sync;
/// An in-memory Chronos Server for the sync engine: sequences with versions, originals, proxies, thumbnails and events.
internal sealed class FakeSyncRemote : ISyncRemote
{
private readonly Lock _gate = new();
private readonly Channel _events = Channel.CreateUnbounded();
private readonly Dictionary _sessions = [];
/// The server's key: an address key unless a test names one by id (id:<N>).
public string Server { get; init; } = "https://nas.test:8443";
public Dictionary Sequences { get; } = [];
/// Manifest requests so far (comparisons skip them while frame counters stand still).
public int ManifestCalls { get; private set; }
/// Listings still to fail with a network error.
public int FailingListings { get; set; }
/// Event streams still to be lost right after they opened.
public int FailingEvents { get; set; }
/// The next original download of this frame returns damaged bytes.
public Guid? CorruptOnce { get; set; }
/// Sequences the listing skips (a page boundary moved while it was read); they are still there.
public HashSet Unlisted { get; } = [];
/// Sequences whose manifest the server refuses (403), as for an account that lost a right.
public HashSet Refused { get; } = [];
/// Proxy and thumbnail downloads fail with a server error.
public bool FailAssets { get; set; }
///
/// Assets (frame id, kind) the sequence's flags offer that the server has not drawn yet: frames added since its last
/// thumbnail or proxy pass answer 404 until that pass ends, as the real endpoints do.
///
public HashSet<(Guid Frame, string Kind)> Undrawn { get; } = [];
/// Proxies clients sent, by frame id (for sequences the server cannot decode).
public Dictionary SentProxies { get; } = [];
/// Edits the server accepted, in order.
public List Updates { get; } = [];
/// The settings format the server announces (an older server's 1).
public int SettingsFormat { get; set; } = Chronos.Core.Project.ProjectSettings.CurrentVersion;
public Task SettingsFormatAsync(CancellationToken cancellationToken) => Task.FromResult(SettingsFormat);
///
/// The capabilities the server announces (): none by default, an older server's path
/// (owner review 6, #73); tests of the lookup and of uploads into a target opt in.
///
public HashSet Capabilities { get; } = new(StringComparer.Ordinal);
public Task> CapabilitiesAsync(CancellationToken cancellationToken)
{
lock (_gate) return Task.FromResult>(new HashSet(Capabilities, StringComparer.Ordinal));
}
/// Lookup requests so far, as received.
public List Lookups { get; } = [];
/// Uploads into a target so far (), and how many the server filed from a copy it held elsewhere.
public int IntoUploads { get; private set; }
public int CopiedInto { get; private set; }
/// Frame deletions refused with 403, as for an account that is neither the owner nor an admin.
public bool ForbidFrameDeletes { get; set; }
/// Frames moved to the server's trash so far, by sequence.
public List<(Guid SequenceId, FakeFrame Frame)> Trashed { get; } = [];
/// Files under , as an upload with that key would.
public void FileUnder(string sessionKey, FakeSequence sequence)
{
lock (_gate) _sessions[sessionKey] = sequence;
}
///
/// Like the server (owner review 6, #73): per probe, the sequences holding its hashes in a frame (a sequence it never
/// hashed lists none), tombstoning them (by the photo's or its replacement's SHA-256), holding a frame of its unhashed keys
/// by size and capture millisecond, or filed under its session key. 404 without ,
/// 413 beyond the limits.
///
public Task LookupPhotosAsync(PhotoLookupRequest request, CancellationToken cancellationToken)
{
lock (_gate)
{
if (!Capabilities.Contains(ServerCapabilities.PhotoLookup)) throw new ChronosApiException(HttpStatusCode.NotFound, new ApiError("not_found", "route"));
if (request.Probes.Count > PhotoLookupRequest.MaxProbes || request.CountItems() > PhotoLookupRequest.MaxItems)
throw new ChronosApiException(HttpStatusCode.RequestEntityTooLarge, new ApiError("too_many", "probes"));
Lookups.Add(request);
var results = new List();
foreach (var probe in request.Probes)
{
var hashes = probe.Sha256.Select(h => h.ToLowerInvariant()).ToHashSet(StringComparer.Ordinal);
var keys = (probe.Unhashed ?? []).Select(k => (k.Size, Ms(k.CapturedAt))).ToHashSet();
var filed = probe.SessionKey is { } key && _sessions.TryGetValue(key, out var held) ? held : null;
var hits = new List();
foreach (var sequence in Sequences.Values)
{
var inFrames = sequence.Unhashed ? 0 : sequence.Frames.Select(f => f.Sha256).Where(hashes.Contains).Distinct(StringComparer.Ordinal).Count();
var removed = hashes.Count(h => sequence.Removed.Any(r => r.Sha256 == h || r.Replacement == h));
var unhashed = keys.Count(k => sequence.Frames.Any(f => f.CapturedAt is { } at && f.Bytes.Length == k.Size && Ms(at) == k.Item2));
var session = ReferenceEquals(filed, sequence);
if (inFrames + removed + unhashed > 0 || session) hits.Add(new PhotoLookupHit(sequence.Id, inFrames, removed, unhashed, session));
}
results.Add(new ProbeResult(probe.Key, hits));
}
return Task.FromResult(new PhotoLookupResponse(results));
}
static long Ms(DateTime at) => at.Ticks / TimeSpan.TicksPerMillisecond;
}
///
/// Like the server with : the photo is filed into the target even when another
/// sequence holds it (a server-side copy, counted in ); one the target holds answers its frame, one
/// it tombstoned answers none. Without the capability the server ignores the flag: an upload as before.
///
public async Task UploadFileIntoAsync(string path, Guid sequenceId, string sha256, DateTime? capturedAt,
CancellationToken cancellationToken)
{
bool capable;
lock (_gate) capable = Capabilities.Contains(ServerCapabilities.UploadInto);
if (!capable) return await UploadFileAsync(path, sequenceId, null, sha256, capturedAt, cancellationToken);
var bytes = await File.ReadAllBytesAsync(path, cancellationToken);
if (Convert.ToHexStringLower(SHA256.HashData(bytes)) != sha256)
throw new ChronosApiException(HttpStatusCode.BadRequest, new ApiError("hash_mismatch", "hash"));
lock (_gate)
{
IntoUploads++;
var target = Find(sequenceId);
if (target.Removed.Any(r => r.Sha256 == sha256)) return null;
var index = target.Frames.FindIndex(f => f.Sha256 == sha256);
if (index >= 0) return new CompleteUploadResponse(target.Frames[index].Id, target.Id, index);
if (Sequences.Values.Where(s => !s.Unhashed && s != target).SelectMany(s => s.Frames).Any(f => f.Sha256 == sha256)) CopiedInto++;
var frame = target.AddFrame(bytes, Path.GetFileName(path), capturedAt);
return new CompleteUploadResponse(frame.Id, target.Id, target.Frames.IndexOf(frame));
}
}
///
/// Like the server's frames/delete: 404 for an unknown sequence, 403 with , 409
/// frames_conflict when a frame is not there or the frames version moved, 409 would_empty; a dry run answers
/// the plan; else the frames go to the trash, each leaving a tombstone, and the frames version moves.
///
public Task DeleteFramesAsync(Guid sequenceId, FrameDeleteRequest request, CancellationToken cancellationToken)
{
lock (_gate)
{
var sequence = Find(sequenceId);
if (ForbidFrameDeletes) throw new ChronosApiException(HttpStatusCode.Forbidden, new ApiError("forbidden", "delete"));
var wanted = request.FrameIds.ToHashSet();
var frames = sequence.Frames.Where(f => wanted.Contains(f.Id)).ToList();
if (frames.Count != wanted.Count || (request.ExpectedFramesVersion is { } expected && expected != sequence.FramesVersion))
throw new ChronosApiException(HttpStatusCode.Conflict, new ApiError("frames_conflict", "changed"));
if (frames.Count == sequence.Frames.Count) throw new ChronosApiException(HttpStatusCode.Conflict, new ApiError("would_empty", "empty"));
var bytes = frames.Sum(f => (long)f.Bytes.Length);
if (request.DryRun)
return Task.FromResult(new FrameDeleteResultDto(sequenceId, frames.Count, bytes, sequence.FramesVersion, sequence.Frames.Count, null, true));
var at = DateTimeOffset.UtcNow;
foreach (var frame in frames)
{
sequence.Frames.Remove(frame);
Trashed.Add((sequenceId, frame));
if (!sequence.Removed.Any(r => r.Sha256 == frame.Sha256)) sequence.Removed.Add(new RemovedPhotoDto(frame.Sha256, null, "deleted", at));
}
sequence.FramesVersion++;
sequence.UpdatedAt = at;
return Task.FromResult(new FrameDeleteResultDto(sequenceId, frames.Count, bytes, sequence.FramesVersion, sequence.Frames.Count,
$"trash/photos-{Guid.NewGuid():N}", false));
}
}
public FakeSequence Add(string? title, IEnumerable originals, string? settingsJson = null,
SequenceStatus status = SequenceStatus.ProxiesBuilt | SequenceStatus.ThumbsBuilt)
{
var sequence = new FakeSequence { Id = Guid.CreateVersion7(), Title = title, SettingsJson = settingsJson, Status = status };
foreach (var bytes in originals) sequence.AddFrame(bytes);
lock (_gate) Sequences[sequence.Id] = sequence;
return sequence;
}
/// Someone else edits the settings on the server.
public void EditSettings(Guid id, string settingsJson)
{
lock (_gate)
{
var s = Sequences[id];
s.SettingsJson = settingsJson;
s.SettingsVersion++;
s.UpdatedAt = DateTimeOffset.UtcNow;
}
}
///
/// Sends an event to the stream open now. With , one sent while no stream is open is lost,
/// as a real server's is.
///
public void Publish(ServerEvent e)
{
if (DropWhileClosed && !Subscribed) return;
_events.Writer.TryWrite(e);
}
/// Events sent while no stream is open are lost ().
public bool DropWhileClosed { get; set; }
/// How long a stream takes to open after a lost one (a reconnection).
public TimeSpan ReconnectDelay { get; set; }
/// Whether an event stream is open now.
public bool Subscribed => Volatile.Read(ref _subscribed) > 0;
private int _subscribed;
private int _opened;
public Task> ListSequencesAsync(CancellationToken cancellationToken)
{
lock (_gate)
{
if (FailingListings > 0)
{
FailingListings--;
throw new HttpRequestException("connection refused");
}
return Task.FromResult>([.. Sequences.Values.Where(s => !Unlisted.Contains(s.Id)).Select(s => s.Dto())]);
}
}
/// The next request for this sequence times out (HttpClient reports a timeout as a cancellation).
public Guid? TimeoutOnce { get; set; }
public Task GetSequenceAsync(Guid id, CancellationToken cancellationToken)
{
lock (_gate)
{
if (TimeoutOnce == id)
{
TimeoutOnce = null;
throw new TaskCanceledException("The request timed out.");
}
return Task.FromResult(Find(id).Dto());
}
}
/// Runs once, before the next manifest is answered: the archive on this PC changing while the sync compares.
public Func? OnceBeforeManifest { get; set; }
public async Task GetManifestAsync(Guid id, CancellationToken cancellationToken)
{
if (OnceBeforeManifest is { } before)
{
OnceBeforeManifest = null;
await before();
}
lock (_gate)
{
ManifestCalls++;
if (Refused.Contains(id)) throw new ChronosApiException(HttpStatusCode.Forbidden, new ApiError("forbidden", "refused"));
return Find(id).Manifest(SentProxies.ContainsKey);
}
}
/// Runs once, before the next frame listing is answered (frame positions translated against the server's frames).
public Func? OnceBeforeFrames { get; set; }
public async Task> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken)
{
if (OnceBeforeFrames is { } before)
{
OnceBeforeFrames = null;
await before();
}
lock (_gate)
{
var sequence = Find(sequenceId);
return [.. sequence.Frames.Select(sequence.FrameDto)];
}
}
public Task UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken)
{
lock (_gate)
{
var s = Find(id);
if (request.ExpectedSettingsVersion is { } expected && expected != s.SettingsVersion)
throw new ChronosApiException(HttpStatusCode.Conflict, new ApiError("settings_conflict", "changed"));
if (request.ExpectedFramesVersion is { } frames && frames != s.FramesVersion)
throw new ChronosApiException(HttpStatusCode.Conflict, new ApiError("frames_conflict", "changed"));
Updates.Add(request);
if (request.Title is not null) s.Title = request.Title.Length == 0 ? null : request.Title;
if (request.Tags is not null) s.Tags = [.. request.Tags];
if (request.Rating is { } rating) s.Rating = rating;
if (request.Notes is not null) s.Notes = request.Notes.Length == 0 ? null : request.Notes;
if (request.SettingsJson is not null) s.SettingsJson = request.SettingsJson;
s.SettingsVersion++;
s.UpdatedAt = DateTimeOffset.UtcNow;
return Task.FromResult(s.Dto());
}
}
/// Like the server: content it already holds anywhere is not stored again; a session key files the first original into a new sequence.
public async Task UploadFileAsync(string path, Guid? sequenceId, string? sessionKey, string sha256, DateTime? capturedAt,
CancellationToken cancellationToken)
{
var bytes = await File.ReadAllBytesAsync(path, cancellationToken);
if (Convert.ToHexStringLower(SHA256.HashData(bytes)) != sha256)
throw new ChronosApiException(HttpStatusCode.BadRequest, new ApiError("hash_mismatch", "hash"));
lock (_gate)
{
// Originals it never hashed are not recognised by content.
if (Sequences.Values.Where(s => !s.Unhashed).SelectMany(s => s.Frames).FirstOrDefault(f => f.Sha256 == sha256) is { } existing)
return new CompleteUploadResponse(existing.Id, sequenceId ?? Guid.Empty, -1);
FakeSequence target;
if (sequenceId is { } id) target = Find(id);
else if (!_sessions.TryGetValue(sessionKey!, out target!))
{
target = new FakeSequence { Id = Guid.CreateVersion7() };
Sequences[target.Id] = target;
_sessions[sessionKey!] = target;
}
var frame = target.AddFrame(bytes, Path.GetFileName(path), capturedAt);
return new CompleteUploadResponse(frame.Id, target.Id, target.Frames.Count - 1);
}
}
public async Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken)
{
byte[] bytes;
lock (_gate)
{
var (sequence, frame) = Sequences.Values.SelectMany(s => s.Frames.Select(f => (s, f))).FirstOrDefault(p => p.f.Id == frameId);
if (frame is null) throw NotFound();
bytes = kind switch
{
"original" when CorruptOnce == frameId => [.. frame.Bytes.Select(b => (byte)~b)],
"original" => frame.Bytes,
"proxy" or "thumb" when FailAssets => throw new ChronosApiException(HttpStatusCode.InternalServerError, new ApiError("internal", "failed")),
"proxy" or "thumb" when Undrawn.Contains((frameId, kind)) => throw NotFound(),
// A proxy a client or render node sent is served as soon as it is filed, like the manifest says.
"proxy" when SentProxies.TryGetValue(frameId, out var sent) => sent,
"proxy" when (sequence.Status & SequenceStatus.ProxiesBuilt) != 0 => frame.Proxy,
"thumb" when (sequence.Status & SequenceStatus.ThumbsBuilt) != 0 => frame.Proxy[..Math.Min(frame.Proxy.Length, 64)],
_ => throw NotFound(),
};
if (kind == "original" && CorruptOnce == frameId) CorruptOnce = null;
}
// Range: continue after what the destination already holds, like the real endpoint.
var offset = destination.CanSeek ? (int)Math.Min(destination.Length, bytes.Length) : 0;
if (destination.CanSeek) destination.Seek(0, SeekOrigin.End);
await destination.WriteAsync(bytes.AsMemory(offset), cancellationToken);
}
/// Like the server: only a frame whose proxy it asks for takes one, and only a JPEG.
public Task UploadProxyAsync(Guid frameId, byte[] jpeg, CancellationToken cancellationToken)
{
lock (_gate)
{
if (!Sequences.Values.Any(s => s.Undecodable && s.Frames.Any(f => f.Id == frameId))) throw NotFound();
if (jpeg is not [0xFF, 0xD8, ..]) throw new ChronosApiException(HttpStatusCode.UnprocessableEntity, new ApiError("invalid_proxy", "not a JPEG"));
SentProxies[frameId] = jpeg;
}
return Task.CompletedTask;
}
public async Task DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken)
{
FakeRender render;
lock (_gate) render = Sequences.Values.SelectMany(s => s.Renders).FirstOrDefault(r => r.Id == renderId) ?? throw NotFound();
var offset = destination.CanSeek ? (int)Math.Min(destination.Length, render.Bytes.Length) : 0;
if (destination.CanSeek) destination.Seek(0, SeekOrigin.End);
await destination.WriteAsync(render.Bytes.AsMemory(offset), cancellationToken);
return render.FileName;
}
/// Like the server: the SHA-256 is checked and a render keeps its id; one it already has is not sent again.
public async Task UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, CancellationToken cancellationToken)
{
var bytes = await File.ReadAllBytesAsync(path, cancellationToken);
if (Convert.ToHexStringLower(SHA256.HashData(bytes)) != request.Sha256 || bytes.Length != request.Size)
throw new ChronosApiException(HttpStatusCode.UnprocessableEntity, new ApiError("hash_mismatch", "hash"));
lock (_gate)
{
var sequence = Find(sequenceId);
if (sequence.Renders.Any(r => r.Id == request.RenderId)) return null;
var render = new FakeRender(request.RenderId, request.FileName, request.PresetName, bytes, request.Kind);
sequence.Renders.Add(render);
return render.Dto();
}
}
public IAsyncEnumerable EventsAsync(CancellationToken cancellationToken) => EventsAsync(() => { }, cancellationToken);
public async IAsyncEnumerable EventsAsync(Action connected, [EnumeratorCancellation] CancellationToken cancellationToken)
{
if (Interlocked.Increment(ref _opened) > 1 && ReconnectDelay > TimeSpan.Zero) await Task.Delay(ReconnectDelay, cancellationToken);
lock (_gate)
{
if (FailingEvents > 0)
{
FailingEvents--;
throw new HttpRequestException("socket closed");
}
}
Interlocked.Increment(ref _subscribed);
try
{
connected();
while (await _events.Reader.WaitToReadAsync(cancellationToken))
{
while (_events.Reader.TryRead(out var e)) yield return e;
}
}
finally
{
Interlocked.Decrement(ref _subscribed);
}
}
public void Dispose()
{
}
private FakeSequence Find(Guid id) => Sequences.TryGetValue(id, out var s) ? s : throw NotFound();
private static ChronosApiException NotFound() => new(HttpStatusCode.NotFound, new ApiError("not_found", "sequence"));
}
internal sealed class FakeSequence
{
public required Guid Id { get; init; }
public string? Title { get; set; }
public List Tags { get; set; } = [];
public int Rating { get; set; }
public string? Notes { get; set; }
public string? SettingsJson { get; set; }
public int SettingsVersion { get; set; } = 1;
public int FramesVersion { get; set; }
public SequenceStatus Status { get; set; }
/// The server cannot decode these originals (HEIF): its manifest asks for proxies of them.
public bool Undecodable { get; set; }
/// The server adopted these originals and never verified them: it lists no SHA-256 for them.
public bool Unhashed { get; set; }
public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow;
public DateTime? FirstCapture { get; set; }
public string? CameraModel { get; set; } = "Canon EOS R5";
public List Frames { get; } = [];
public List Renders { get; } = [];
/// Tombstones of photos removed on the server (owner review 2, R6), as its manifest lists them.
public List Removed { get; } = [];
/// Like the server, the capture time comes from the photo's Exif data when none is given.
public FakeFrame AddFrame(byte[] bytes, string? name = null, DateTime? capturedAt = null) => InsertFrame(Frames.Count, bytes, name, capturedAt);
/// A photo filed at (captured before the frames after it): those move up by one.
public FakeFrame InsertFrame(int index, byte[] bytes, string? name = null, DateTime? capturedAt = null)
{
name ??= $"IMG_{Frames.Count + 1:0000}.JPG";
using var stream = new MemoryStream(bytes);
var frame = new FakeFrame(Guid.CreateVersion7(), name, bytes, capturedAt ?? MetadataReader.Read(stream, name).CapturedAt);
Frames.Insert(index, frame);
FramesVersion++;
UpdatedAt = DateTimeOffset.UtcNow;
return frame;
}
public SequenceDto Dto() => new(Id, Title, Title ?? "Untitled", "2026/" + Id.ToString("N"), "Canon", CameraModel, null, FirstCapture, null, Frames.Count, 10,
Frames.Sum(f => (long)f.Bytes.Length), (long)Status, Renders.Count, null, Tags, Rating, Notes, SettingsVersion, FramesVersion, 0, null, UpdatedAt);
public SequenceManifest Manifest(Func proxySent) => new(Id, FramesVersion, SettingsVersion, SettingsJson, Tags,
[.. Frames.Select((f, i) => new ManifestFrame(f.Id, i, f.FileName, f.Bytes.Length, Unhashed ? string.Empty : f.Sha256,
(Status & SequenceStatus.ProxiesBuilt) != 0 || proxySent(f.Id), (Status & SequenceStatus.ThumbsBuilt) != 0, WantsProxy: Undecodable && !proxySent(f.Id)))],
[.. Renders.Select(r => r.Dto())], Undecodable ? new ProxySpec(1280, 90) : null, Removed.Count == 0 ? null : [.. Removed]);
public FrameDto FrameDto(FakeFrame frame) => frame.Dto(Id, Frames.IndexOf(frame), Unhashed);
}
/// The SHA-256 the server records (its file's when null; empty: none, as before migration 0010).
internal sealed record FakeRender(Guid Id, string FileName, string PresetName, byte[] Bytes, string? Kind = null, string? Recorded = null)
{
public string Sha256 => Recorded ?? Convert.ToHexStringLower(SHA256.HashData(Bytes));
public RenderDto Dto() => new(Id, PresetName, "hevc", 3840, 2160, 30, 12, Bytes.Length, new DateTimeOffset(2026, 9, 20, 21, 0, 0, TimeSpan.Zero), "Studio NAS",
Sha256: Sha256.Length == 0 ? null : Sha256, Kind: Kind);
}
internal sealed record FakeFrame(Guid Id, string FileName, byte[] Bytes, DateTime? CapturedAt)
{
public string Sha256 { get; } = Convert.ToHexStringLower(SHA256.HashData(Bytes));
/// The original stands in for its proxy: a real JPEG the decoders read.
public byte[] Proxy => Bytes;
public FrameDto Dto(Guid sequenceId, int index, bool unhashed = false) =>
new(Id, sequenceId, index, FileName, Bytes.Length, unhashed ? null : Sha256, CapturedAt, null, 1.0 / 250, 8, 100, 24, 1, 48, 32, "jpeg", 0);
}