using LehrerApp.Core.Models;
using LehrerApp.Sync.Crypto;
using Xunit;
namespace LehrerApp.Sync.Tests;
public sealed class SyncEventPublisherTests
{
private static readonly byte[] Key = SyncCrypto.GenerateKey();
[Fact]
public void Publish_DocumentationMitAnhang_ReihtIhnZumHochladenEin()
{
using var temp = new TempEventQueue();
var publisher = new SyncEventPublisher(temp.Queue, "desktop-1", Key);
var doc = new Documentation { StudentId = Guid.NewGuid(), Title = "Elternbrief" };
doc.Attachments.Add(new DocumentAttachment { StorageId = "abc", FileName = "brief.pdf" });
publisher.Publish(nameof(Documentation), doc.Id.ToString(), "Save", doc);
Assert.Equal(["abc"], temp.Queue.GetPendingAttachmentUploads());
}
/// Regressionstest für die Verallgemeinerung von "nur Documentation" auf
/// — Anhänge an einer müssen genauso in die
/// Upload-Warteliste eingereiht werden.
[Fact]
public void Publish_LessonMitAnhang_ReihtIhnZumHochladenEin()
{
using var temp = new TempEventQueue();
var publisher = new SyncEventPublisher(temp.Queue, "desktop-1", Key);
var lesson = new Lesson { UnitId = Guid.NewGuid(), GroupId = Guid.NewGuid(), Topic = "Brechung" };
lesson.Attachments.Add(new DocumentAttachment { StorageId = "gbu-1", FileName = "gbu.pdf" });
publisher.Publish(nameof(Lesson), lesson.Id.ToString(), "Save", lesson);
Assert.Equal(["gbu-1"], temp.Queue.GetPendingAttachmentUploads());
}
[Fact]
public void Publish_EntitaetOhneAnhaenge_ReihtNichtsZumHochladenEin()
{
using var temp = new TempEventQueue();
var publisher = new SyncEventPublisher(temp.Queue, "desktop-1", Key);
var student = new Student { FirstName = "Anna", LastName = "Beispiel" };
publisher.Publish(nameof(Student), student.Id.ToString(), "Save", student);
Assert.Empty(temp.Queue.GetPendingAttachmentUploads());
}
private sealed class TempEventQueue : IDisposable
{
private readonly string _directory = Path.Combine(
Path.GetTempPath(), $"lehrerapp-sync-tests-publisher-{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);
}
}
}