using Khaemhat.Application.Modules;
using Khaemhat.Application.Notifications;
using Khaemhat.Application.Security;
using Khaemhat.Core.Instance;
using Khaemhat.Core.Security;
using Khaemhat.Web.Tests.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace Khaemhat.Web.Tests.Shell;
/// The notification panel of mockup 010 (issue #32): what each user reads is kept per user.
[TestClass]
public sealed class NotificationInboxTests
{
private const string Category = "test";
[TestMethod]
public async Task ANotificationForEveryone_IsReadByEachUserSeparately()
{
var (app, _, company) = await TestInstance.StartAsync(CancellationToken.None);
await using (app)
{
var notifications = app.Services.GetRequiredService();
var users = app.Services.GetRequiredService();
var root = new Actor(TestInstance.SuperUser, UserLevel.SuperUser, 1, null, null);
var (anna, _) = await users.CreateAsync(root, company, new NewCompanyUser("anna", "Anna Verdi", null, UserKind.Human, [PredefinedRoles.ReadOnly], new UserFlags(false, false, false, false)));
var (luca, _) = await users.CreateAsync(root, company, new NewCompanyUser("luca", "Luca Neri", null, UserKind.Human, [PredefinedRoles.ReadOnly], new UserFlags(false, false, false, false)));
var annaActor = new Actor("anna", UserLevel.Standard, null, anna.Id, company);
var lucaActor = new Actor("luca", UserLevel.Standard, null, luca.Id, company);
await notifications.NotifyCompanyAsync(company, new NotificationMessage(Category, NotificationSeverity.Info, "Per tutti"));
await notifications.NotifyCompanyAsync(company, new NotificationMessage(Category, NotificationSeverity.Warning, "Solo per Anna", "Dettaglio", "/profilo"), anna.Id);
var annaInbox = await Inbox(annaActor);
CollectionAssert.AreEqual(new[] { "Solo per Anna", "Per tutti" }, annaInbox.Select(n => n.Title).ToList(), "Newest first.");
Assert.IsTrue(annaInbox[0].Personal);
Assert.AreEqual("/profilo", annaInbox[0].Link);
Assert.IsTrue(annaInbox.All(n => !n.Read));
CollectionAssert.AreEqual(new[] { "Per tutti" }, (await Inbox(lucaActor)).Select(n => n.Title).ToList(), "Personal notifications stay personal.");
// Anna reads the one for everyone: it stays unread for Luca; reading it again changes nothing.
var everyone = annaInbox[1].Id;
await notifications.MarkReadAsync(annaActor, everyone);
await notifications.MarkReadAsync(annaActor, everyone);
Assert.IsTrue((await Inbox(annaActor)).Single(n => n.Id == everyone).Read);
Assert.IsFalse((await Inbox(lucaActor)).Single(n => n.Id == everyone).Read);
// Nobody marks someone else's notification.
await notifications.MarkReadAsync(lucaActor, annaInbox[0].Id);
Assert.IsFalse((await Inbox(annaActor)).Single(n => n.Id == annaInbox[0].Id).Read);
// "Segna tutte come lette" is for the user only.
await notifications.MarkAllReadAsync(annaActor);
Assert.IsEmpty(await Inbox(annaActor, unreadOnly: true));
Assert.HasCount(1, await Inbox(lucaActor, unreadOnly: true));
// Instance accounts read the notifications of the system.
await notifications.NotifyInstanceAsync(new NotificationMessage(Category, NotificationSeverity.Error, "Backup non riuscito"));
var system = await Inbox(root, unreadOnly: true);
Assert.AreEqual("Backup non riuscito", system.Single().Title);
Assert.IsFalse((await Inbox(annaActor)).Any(n => n.Title == "Backup non riuscito"));
await notifications.MarkAllReadAsync(root);
Assert.IsEmpty(await Inbox(root, unreadOnly: true));
await app.StopAsync();
async Task> Inbox(Actor actor, bool unreadOnly = false) =>
[.. (await notifications.InboxAsync(actor, unreadOnly)).Where(n => n.Category == Category)];
}
}
}