test: cover watch-time tracking, notices and storage
This commit is contained in:
@@ -0,0 +1,201 @@
|
||||
using Jellyfin.Plugin.Herald.Notices;
|
||||
using Jellyfin.Plugin.Herald.Storage;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace Jellyfin.Plugin.Herald.Tests;
|
||||
|
||||
public sealed class NoticeServiceTests : IDisposable
|
||||
{
|
||||
private static readonly Guid _alice = Guid.NewGuid();
|
||||
private static readonly Guid _bob = Guid.NewGuid();
|
||||
private static readonly Dictionary<Guid, string> _userNames = new() { [_alice] = "alice", [_bob] = "bob" };
|
||||
|
||||
private readonly TempDatabase _temp = new();
|
||||
private readonly ManualTimeProvider _clock = new(new DateTimeOffset(2026, 3, 10, 12, 0, 0, TimeSpan.Zero));
|
||||
private readonly NoticeService _service;
|
||||
|
||||
public NoticeServiceTests()
|
||||
{
|
||||
_service = new NoticeService(new SqliteNoticeStore(_temp.Database), _clock, NullLogger<NoticeService>.Instance);
|
||||
}
|
||||
|
||||
public void Dispose() => _temp.Dispose();
|
||||
|
||||
private Notice Create(string title, Action<NoticeDraft>? configure = null)
|
||||
{
|
||||
var draft = new NoticeDraft { Title = title, Body = "Body" };
|
||||
configure?.Invoke(draft);
|
||||
var notice = _service.Create(draft);
|
||||
_clock.Advance(TimeSpan.FromMinutes(1));
|
||||
return notice;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_RoundTripsThroughTheDatabase()
|
||||
{
|
||||
var created = Create(" Hello ", d =>
|
||||
{
|
||||
d.Body = "Line one\r\nLine two";
|
||||
d.RequireAccept = true;
|
||||
d.Target = new NoticeTarget { AllUsers = false, UserIds = [_alice, _alice, _bob] };
|
||||
});
|
||||
|
||||
var loaded = _service.Get(created.Id);
|
||||
|
||||
Assert.NotNull(loaded);
|
||||
Assert.Equal("Hello", loaded.Title);
|
||||
Assert.Equal("Line one\nLine two", loaded.Body);
|
||||
Assert.True(loaded.RequireAccept);
|
||||
Assert.True(loaded.Enabled);
|
||||
Assert.False(loaded.Target.AllUsers);
|
||||
Assert.Equal([_alice, _bob], loaded.Target.UserIds);
|
||||
Assert.Equal(1, loaded.Revision);
|
||||
Assert.Equal(new DateTime(2026, 3, 10, 12, 0, 0, DateTimeKind.Utc), loaded.CreatedAt);
|
||||
Assert.Equal(DateTimeKind.Utc, loaded.CreatedAt.Kind);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Pending_ShowsEachNoticeOnceOldestFirst()
|
||||
{
|
||||
var first = Create("First");
|
||||
var second = Create("Second");
|
||||
|
||||
Assert.Equal([first.Id, second.Id], _service.GetPending(_alice).Select(n => n.Id));
|
||||
|
||||
Assert.Equal(AckResult.Ok, _service.Acknowledge(_alice, first.Id, 1, AckAction.Dismissed));
|
||||
|
||||
Assert.Equal([second.Id], _service.GetPending(_alice).Select(n => n.Id));
|
||||
Assert.Equal(2, _service.GetPending(_bob).Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reshow_BringsTheNoticeBackForEveryone()
|
||||
{
|
||||
var notice = Create("Rules");
|
||||
_service.Acknowledge(_alice, notice.Id, 1, AckAction.Accepted);
|
||||
Assert.Empty(_service.GetPending(_alice));
|
||||
|
||||
var bumped = _service.Reshow(notice.Id);
|
||||
|
||||
Assert.Equal(2, bumped!.Revision);
|
||||
Assert.Equal(2, Assert.Single(_service.GetPending(_alice)).Revision);
|
||||
|
||||
// The old revision can no longer be answered.
|
||||
Assert.Equal(AckResult.RevisionMismatch, _service.Acknowledge(_alice, notice.Id, 1, AckAction.Accepted));
|
||||
Assert.Equal(AckResult.Ok, _service.Acknowledge(_alice, notice.Id, 2, AckAction.Accepted));
|
||||
Assert.Empty(_service.GetPending(_alice));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_KeepsRevisionAndAcknowledgements()
|
||||
{
|
||||
var notice = Create("Typo");
|
||||
_service.Acknowledge(_alice, notice.Id, 1, AckAction.Dismissed);
|
||||
|
||||
var updated = _service.Update(notice.Id, new NoticeDraft { Title = "Fixed", Body = "Body" });
|
||||
|
||||
Assert.Equal(1, updated!.Revision);
|
||||
Assert.Equal(notice.CreatedAt, updated.CreatedAt);
|
||||
Assert.True(updated.UpdatedAt > updated.CreatedAt);
|
||||
Assert.Empty(_service.GetPending(_alice));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Disabled_NoticeIsNotPendingAndCannotBeAnswered()
|
||||
{
|
||||
var notice = Create("Off", d => d.Enabled = false);
|
||||
|
||||
Assert.Empty(_service.GetPending(_alice));
|
||||
Assert.Equal(AckResult.NotFound, _service.Acknowledge(_alice, notice.Id, 1, AckAction.Accepted));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Targeted_NoticeIsOnlyPendingForItsUsers()
|
||||
{
|
||||
var notice = Create("Just Alice", d => d.Target = new NoticeTarget { AllUsers = false, UserIds = [_alice] });
|
||||
|
||||
Assert.Single(_service.GetPending(_alice));
|
||||
Assert.Empty(_service.GetPending(_bob));
|
||||
Assert.Equal(AckResult.NotFound, _service.Acknowledge(_bob, notice.Id, 1, AckAction.Dismissed));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequireAccept_DismissIsRejectedAndNothingIsStored()
|
||||
{
|
||||
var notice = Create("Terms", d => d.RequireAccept = true);
|
||||
|
||||
Assert.Equal(AckResult.AcceptRequired, _service.Acknowledge(_alice, notice.Id, 1, AckAction.Dismissed));
|
||||
Assert.Single(_service.GetPending(_alice));
|
||||
|
||||
Assert.Equal(AckResult.Ok, _service.Acknowledge(_alice, notice.Id, 1, AckAction.Accepted));
|
||||
Assert.Empty(_service.GetPending(_alice));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Acknowledge_TwiceKeepsTheFirstResponse()
|
||||
{
|
||||
var notice = Create("Once");
|
||||
_service.Acknowledge(_alice, notice.Id, 1, AckAction.Dismissed);
|
||||
_clock.Advance(TimeSpan.FromHours(1));
|
||||
_service.Acknowledge(_alice, notice.Id, 1, AckAction.Accepted);
|
||||
|
||||
var response = Assert.Single(_service.GetAckReport(notice.Id, _userNames)!.Responses);
|
||||
|
||||
Assert.Equal(AckAction.Dismissed, response.Action);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AckReport_ListsResponsesAndWhoIsStillPending()
|
||||
{
|
||||
var carol = Guid.NewGuid();
|
||||
var notice = Create("Report", d => d.Target = new NoticeTarget { AllUsers = false, UserIds = [_alice, _bob, carol] });
|
||||
var answeredAt = _clock.GetUtcNow().UtcDateTime;
|
||||
_service.Acknowledge(_alice, notice.Id, 1, AckAction.Accepted);
|
||||
|
||||
// carol was deleted in the meantime and is not part of the user list.
|
||||
var report = _service.GetAckReport(notice.Id, _userNames)!;
|
||||
|
||||
var response = Assert.Single(report.Responses);
|
||||
Assert.Equal("alice", response.UserName);
|
||||
Assert.Equal(AckAction.Accepted, response.Action);
|
||||
Assert.Equal(answeredAt, response.Timestamp);
|
||||
Assert.Equal("bob", Assert.Single(report.Pending).UserName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AckReport_OnlyCoversTheCurrentRevision()
|
||||
{
|
||||
var notice = Create("Again");
|
||||
_service.Acknowledge(_alice, notice.Id, 1, AckAction.Dismissed);
|
||||
_service.Reshow(notice.Id);
|
||||
|
||||
var report = _service.GetAckReport(notice.Id, _userNames)!;
|
||||
|
||||
Assert.Equal(2, report.Revision);
|
||||
Assert.Empty(report.Responses);
|
||||
Assert.Equal(["alice", "bob"], report.Pending.Select(p => p.UserName));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Delete_RemovesNoticeAndAcknowledgements()
|
||||
{
|
||||
var notice = Create("Gone");
|
||||
_service.Acknowledge(_alice, notice.Id, 1, AckAction.Dismissed);
|
||||
|
||||
Assert.True(_service.Delete(notice.Id));
|
||||
Assert.False(_service.Delete(notice.Id));
|
||||
Assert.Null(_service.Get(notice.Id));
|
||||
Assert.Null(_service.GetAckReport(notice.Id, _userNames));
|
||||
Assert.Empty(new SqliteNoticeStore(_temp.Database).GetAcksByUser(_alice));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Missing_NoticeOperationsReturnNull()
|
||||
{
|
||||
var id = Guid.NewGuid();
|
||||
|
||||
Assert.Null(_service.Update(id, new NoticeDraft { Title = "x" }));
|
||||
Assert.Null(_service.Reshow(id));
|
||||
Assert.Equal(AckResult.NotFound, _service.Acknowledge(_alice, id, 1, AckAction.Accepted));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user