using LehrerApp.Sync.Models; using Xunit; namespace LehrerApp.Sync.Tests; public sealed class EventQueueTests { [Fact] public void MarkReviewed_EntferntKonfliktAusDerUnreviewedListe() { using var temp = new TempEventQueue(); var conflict = new ConflictEntry { LocalEvent = MakeEvent(), RemoteEvent = MakeEvent(), Resolution = "LocalWon", }; temp.Queue.AddConflict(conflict); temp.Queue.MarkReviewed(conflict.Id); Assert.Empty(temp.Queue.GetUnreviewed()); Assert.Equal(0, temp.Queue.ConflictCount()); } [Fact] public void MarkReviewed_UnbekannteId_TutNichtsUndWirftNicht() { using var temp = new TempEventQueue(); var exception = Record.Exception(() => temp.Queue.MarkReviewed(Guid.NewGuid())); Assert.Null(exception); } [Fact] public void GetKnownServerSeq_UnbekannteEntitaet_LiefertNull() { using var temp = new TempEventQueue(); Assert.Null(temp.Queue.GetKnownServerSeq("Lesson", Guid.NewGuid().ToString())); } [Fact] public void SetKnownServerSeq_GefolgtVonGet_LiefertDenGesetztenWert() { using var temp = new TempEventQueue(); var entityId = Guid.NewGuid().ToString(); temp.Queue.SetKnownServerSeq("Lesson", entityId, 42); Assert.Equal(42, temp.Queue.GetKnownServerSeq("Lesson", entityId)); } [Fact] public void SetKnownServerSeq_ErneutesSetzen_UeberschreibtDenAltenWert() { using var temp = new TempEventQueue(); var entityId = Guid.NewGuid().ToString(); temp.Queue.SetKnownServerSeq("Lesson", entityId, 42); temp.Queue.SetKnownServerSeq("Lesson", entityId, 43); Assert.Equal(43, temp.Queue.GetKnownServerSeq("Lesson", entityId)); } [Fact] public void SetKnownServerSeq_GleicheEntityIdAndererEntityType_BleibtGetrennt() { using var temp = new TempEventQueue(); var entityId = Guid.NewGuid().ToString(); temp.Queue.SetKnownServerSeq("Lesson", entityId, 42); temp.Queue.SetKnownServerSeq("Unit", entityId, 7); Assert.Equal(42, temp.Queue.GetKnownServerSeq("Lesson", entityId)); Assert.Equal(7, temp.Queue.GetKnownServerSeq("Unit", entityId)); } private static SyncEvent MakeEvent() => new() { DeviceId = "desktop-1", DeviceType = DeviceType.Desktop, EntityType = "Student", EntityId = Guid.NewGuid().ToString(), Operation = "Save", Payload = "{}", }; private sealed class TempEventQueue : IDisposable { private readonly string _directory = Path.Combine( Path.GetTempPath(), $"lehrerapp-sync-tests-eventqueue-{Guid.NewGuid():N}"); public EventQueue Queue { get; } public TempEventQueue() { Directory.CreateDirectory(_directory); Queue = new EventQueue(Path.Combine(_directory, "queue.db")); } public void Dispose() { Queue.Dispose(); if (Directory.Exists(_directory)) Directory.Delete(_directory, recursive: true); } } }