using Titano.Core.Model; using Titano.Data.Archiving; using Titano.Data.Jobs; using Titano.Data.Sync; using Titano.Data.Tests.Archiving; using Titano.Testing; namespace Titano.Data.Tests.Sync; /// A local archive and index, the sync engine over them and an in-memory server. internal sealed class SyncFixture : IAsyncDisposable { public const string Window41 = """{"exposure":{"deflickerWindow":{"isAuto":false,"manual":41}}}"""; public const string Window33 = """{"exposure":{"deflickerWindow":{"isAuto":false,"manual":33}}}"""; private readonly List _owned = []; private int _seed; private SyncFixture(ArchiveFixture archive, SyncService sync) { Archive = archive; Sync = sync; _owned.Add(archive); } public ArchiveFixture Archive { get; } public SyncService Sync { get; } public FakeSyncRemote Remote { get; } = new(); public TitanoDatabase Db => Archive.Db; public string Server => Remote.Server; public CancellationToken Ct => Archive.Ct; public static async Task CreateAsync(TestContext context) { var archive = await ArchiveFixture.CreateAsync(context); var sync = new SyncService(archive.Db, archive.Service, new SyncOptions { Archive = new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) }, ProxyCacheDirectory = context.CreateTempDirectory("proxies"), }); return new SyncFixture(archive, sync); } public Task RefreshAsync(SyncPolicy? policy = null) => Sync.RefreshAsync(Remote, policy ?? new SyncPolicy(), Ct); public async Task EntryAsync(string title) => (await Sync.ListAsync(Server, Ct)).Single(e => e.Title == title); /// Distinct photos (the same bytes on both sides make the same original). public List NewPhotos(int count) { var photos = new List(count); for (var i = 0; i < count; i++, _seed++) photos.Add(Photos.Jpeg(ArchiveFixture.Start.AddSeconds(10 * _seed), 1000 + _seed)); return photos; } /// A local sequence made through the archive ingest (hashed frames, a real sidecar). public async Task LocalAsync(string title, IReadOnlyList originals, string? settingsJson = null) { Guid? id = null; var staging = Path.Combine(Archive.Paths.StagingDirectory, "test"); Directory.CreateDirectory(staging); for (var i = 0; i < originals.Count; i++) { var path = Path.Combine(staging, $"{Guid.NewGuid():N}.part"); await File.WriteAllBytesAsync(path, originals[i], Ct); var sha = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(originals[i])); var result = await Archive.Service.Ingest.CommitAsync(new IngestRequest(Archive.Archive.Id, id, id is null ? title : null, null, path, $"IMG_{i + 1:0000}.JPG", originals[i].Length, sha, ArchiveFixture.Start.AddSeconds(10 * i), null), Ct); id = result.SequenceId; } var sequence = (await Db.Sequences.GetAsync(id!.Value, Ct))!; if (settingsJson is not null) sequence = await EditLocalSettingsAsync(sequence.Id, settingsJson); return sequence; } /// An edit on this PC, as the editor makes it (project file and index, settings version + 1). public async Task EditLocalSettingsAsync(Guid id, string settingsJson) { var sequence = (await Db.Sequences.GetAsync(id, Ct))!; var folder = Archive.Paths.Sequence(sequence.Folder); await File.WriteAllTextAsync(folder.ProjectPath, settingsJson, Ct); var result = await Db.Sequences.EditAsync(id, new Titano.Data.Repositories.SequenceEdit { SettingsJson = settingsJson }, sequence.SettingsVersion, Ct); return result.Sequence!; } /// A started engine with the archive and sync handlers; jobs reach the server through (the fake by default). public async Task StartEngineAsync(ISyncConnector? connector = null) { var engine = new JobEngine(Db, [.. Archive.Service.CreateJobHandlers(), .. Sync.CreateJobHandlers(connector ?? new Connector(Remote))], new JobEngineOptions { Workers = 2, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) }); _owned.Insert(0, engine); await engine.StartAsync(Ct); return engine; } public async ValueTask DisposeAsync() { foreach (var owned in _owned) await owned.DisposeAsync(); } /// Hands out for its own server; null for any other, or always when is null. public sealed class Connector(ISyncRemote? remote) : ISyncConnector { public ValueTask ConnectAsync(string server, CancellationToken cancellationToken) => ValueTask.FromResult(remote is not null && remote.Server == server ? remote : null); } }