using System.Net; using System.Runtime.CompilerServices; using System.Security.Cryptography; using System.Threading.Channels; using Titano.Core.Model; using Titano.Data.Sync; using Titano.Protocol; using Titano.Protocol.Client; namespace Titano.Data.Tests.Sync; /// An in-memory Titano 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 = []; public string Server => "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; } /// The next original download of this frame returns damaged bytes. public Guid? CorruptOnce { get; set; } 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; } } public void Publish(ServerEvent e) => _events.Writer.TryWrite(e); public Task> ListSequencesAsync(CancellationToken cancellationToken) { lock (_gate) { if (FailingListings > 0) { FailingListings--; throw new HttpRequestException("connection refused"); } return Task.FromResult>([.. Sequences.Values.Select(s => s.Dto())]); } } public Task GetSequenceAsync(Guid id, CancellationToken cancellationToken) { lock (_gate) return Task.FromResult(Find(id).Dto()); } public Task GetManifestAsync(Guid id, CancellationToken cancellationToken) { lock (_gate) { ManifestCalls++; return Task.FromResult(Find(id).Manifest()); } } public Task> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken) { lock (_gate) return Task.FromResult>([.. Find(sequenceId).Frames.Select((f, i) => f.Dto(sequenceId, i))]); } 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 TitanoApiException(HttpStatusCode.Conflict, new ApiError("settings_conflict", "changed")); 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 TitanoApiException(HttpStatusCode.BadRequest, new ApiError("hash_mismatch", "hash")); lock (_gate) { if (Sequences.Values.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" 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); } public async IAsyncEnumerable EventsAsync([EnumeratorCancellation] CancellationToken cancellationToken) { while (await _events.Reader.WaitToReadAsync(cancellationToken)) { while (_events.Reader.TryRead(out var e)) yield return e; } } public void Dispose() { } private FakeSequence Find(Guid id) => Sequences.TryGetValue(id, out var s) ? s : throw NotFound(); private static TitanoApiException 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; } public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow; public List Frames { get; } = []; public FakeFrame AddFrame(byte[] bytes, string? name = null, DateTime? capturedAt = null) { var frame = new FakeFrame(Guid.CreateVersion7(), name ?? $"IMG_{Frames.Count + 1:0000}.JPG", bytes, capturedAt); Frames.Add(frame); FramesVersion++; UpdatedAt = DateTimeOffset.UtcNow; return frame; } public SequenceDto Dto() => new(Id, Title, Title ?? "Untitled", "2026/" + Id.ToString("N"), "Canon", "Canon EOS R5", null, null, null, Frames.Count, 10, Frames.Sum(f => (long)f.Bytes.Length), (long)Status, 0, null, Tags, Rating, Notes, SettingsVersion, FramesVersion, 0, null, UpdatedAt); public SequenceManifest Manifest() => new(Id, FramesVersion, SettingsVersion, SettingsJson, Tags, [.. Frames.Select((f, i) => new ManifestFrame(f.Id, i, f.FileName, f.Bytes.Length, f.Sha256, (Status & SequenceStatus.ProxiesBuilt) != 0, (Status & SequenceStatus.ThumbsBuilt) != 0))], []); } 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) => new(Id, sequenceId, index, FileName, Bytes.Length, Sha256, CapturedAt, null, 1.0 / 250, 8, 100, 24, 1, 48, 32, "jpeg", 0); }