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 ClearConflicts_EntferntAlleKonflikte() { using var temp = new TempEventQueue(); temp.Queue.AddConflict(new ConflictEntry { LocalEvent = MakeEvent(), RemoteEvent = MakeEvent(), Resolution = "LocalWon", }); temp.Queue.AddConflict(new ConflictEntry { LocalEvent = MakeEvent(), RemoteEvent = MakeEvent(), Resolution = "RemoteWon", }); temp.Queue.ClearConflicts(); Assert.Empty(temp.Queue.GetUnreviewed()); Assert.Equal(0, temp.Queue.ConflictCount()); } [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)); } [Fact] public void ClearKnownServerSeq_EntferntNurDieAngegebeneEntitaet() { using var temp = new TempEventQueue(); var entityId = Guid.NewGuid().ToString(); temp.Queue.SetKnownServerSeq("Lesson", entityId, 42); temp.Queue.SetKnownServerSeq("Unit", entityId, 7); temp.Queue.ClearKnownServerSeq("Lesson", entityId); Assert.Null(temp.Queue.GetKnownServerSeq("Lesson", entityId)); Assert.Equal(7, temp.Queue.GetKnownServerSeq("Unit", entityId)); } [Fact] public void ClearKnownServerSeq_UnbekannteEntitaet_TutNichtsUndWirftNicht() { using var temp = new TempEventQueue(); var exception = Record.Exception(() => temp.Queue.ClearKnownServerSeq("Lesson", "unbekannt")); Assert.Null(exception); } /// Regression: nach einem Kontowechsel (TODO 10.3.5, z.B. Login-Korrektur der Groß-/ /// Kleinschreibung, siehe TODO 10.2.5) sind alle lokal zwischengespeicherten ServerSeq-Werte /// bedeutungslos - sie beziehen sich auf das Event-Log eines ANDEREN Server-Kontos. [Fact] public void ResetKnownServerSeqs_EntferntAlleEintraege() { using var temp = new TempEventQueue(); temp.Queue.SetKnownServerSeq("Lesson", "a", 1); temp.Queue.SetKnownServerSeq("Unit", "b", 2); temp.Queue.ResetKnownServerSeqs(); Assert.Null(temp.Queue.GetKnownServerSeq("Lesson", "a")); Assert.Null(temp.Queue.GetKnownServerSeq("Unit", "b")); } 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); } } }