152 lines
5.4 KiB
C#
152 lines
5.4 KiB
C#
using Jellyfin.Plugin.Herald.Stats;
|
|
using Jellyfin.Plugin.Herald.Storage;
|
|
|
|
namespace Jellyfin.Plugin.Herald.Tests;
|
|
|
|
public sealed class StatsStorageTests : IDisposable
|
|
{
|
|
private static readonly Guid _alice = Guid.NewGuid();
|
|
private static readonly Guid _bob = Guid.NewGuid();
|
|
private static readonly DateOnly _today = new(2026, 3, 10);
|
|
|
|
private readonly TempDatabase _temp = new();
|
|
private readonly ManualTimeProvider _clock = new(new DateTimeOffset(2026, 3, 10, 12, 0, 0, TimeSpan.Zero));
|
|
private readonly SqliteWatchStatsStore _store;
|
|
private readonly StatsService _service;
|
|
|
|
public StatsStorageTests()
|
|
{
|
|
_store = new SqliteWatchStatsStore(_temp.Database);
|
|
_service = new StatsService(_store, _clock);
|
|
}
|
|
|
|
public void Dispose() => _temp.Dispose();
|
|
|
|
private static WatchIncrement Watched(Guid user, DateOnly date, double seconds, int plays = 0, int hour = 10)
|
|
{
|
|
return new WatchIncrement(user, date, seconds, plays, date.ToDateTime(new TimeOnly(hour, 0), DateTimeKind.Utc));
|
|
}
|
|
|
|
[Fact]
|
|
public void Add_AccumulatesIntoOneRowPerUserAndDay()
|
|
{
|
|
_store.Add([Watched(_alice, _today, 0, plays: 1, hour: 9)]);
|
|
_store.Add([Watched(_alice, _today, 10, hour: 11), Watched(_alice, _today, 15.5, hour: 10)]);
|
|
_store.Add([Watched(_bob, _today, 7, plays: 1)]);
|
|
|
|
var rows = _store.GetSince(_today);
|
|
|
|
Assert.Equal(2, rows.Count);
|
|
var alice = Assert.Single(rows, r => r.UserId == _alice);
|
|
Assert.Equal(25.5, alice.WatchedSeconds, 6);
|
|
Assert.Equal(1, alice.PlayCount);
|
|
Assert.Equal(new DateTime(2026, 3, 10, 11, 0, 0, DateTimeKind.Utc), alice.LastActivity);
|
|
}
|
|
|
|
[Fact]
|
|
public void Summary_CoversTheRequestedDaysIncludingToday()
|
|
{
|
|
_store.Add(
|
|
[
|
|
Watched(_alice, _today, 3600, plays: 1),
|
|
Watched(_alice, _today.AddDays(-6), 1800, plays: 2),
|
|
Watched(_alice, _today.AddDays(-7), 7200, plays: 4),
|
|
Watched(_alice, _today.AddDays(-29), 3600, plays: 1),
|
|
Watched(_alice, _today.AddDays(-30), 36000, plays: 9)
|
|
]);
|
|
var names = new Dictionary<Guid, string> { [_alice] = "alice", [_bob] = "bob" };
|
|
|
|
var week = _service.GetSummary(7, names);
|
|
var month = _service.GetSummary(30, names);
|
|
|
|
var aliceWeek = Assert.Single(week, s => s.UserName == "alice");
|
|
Assert.Equal(1.5, aliceWeek.WatchedHours);
|
|
Assert.Equal(5400, aliceWeek.WatchedSeconds);
|
|
Assert.Equal(3, aliceWeek.PlayCount);
|
|
|
|
var aliceMonth = Assert.Single(month, s => s.UserName == "alice");
|
|
Assert.Equal(4.5, aliceMonth.WatchedHours);
|
|
Assert.Equal(8, aliceMonth.PlayCount);
|
|
Assert.Equal(new DateTime(2026, 3, 10, 10, 0, 0, DateTimeKind.Utc), aliceMonth.LastActivity);
|
|
|
|
var bob = Assert.Single(month, s => s.UserName == "bob");
|
|
Assert.Equal(0, bob.WatchedHours);
|
|
Assert.Equal(0, bob.PlayCount);
|
|
Assert.Null(bob.LastActivity);
|
|
}
|
|
|
|
[Fact]
|
|
public void Summary_KeepsActivityOfDeletedUsersWithoutAName()
|
|
{
|
|
_store.Add([Watched(_alice, _today, 600, plays: 1)]);
|
|
|
|
var summary = _service.GetSummary(30, new Dictionary<Guid, string> { [_bob] = "bob" });
|
|
|
|
Assert.Equal(["bob", null], summary.Select(s => s.UserName));
|
|
Assert.Equal(_alice, summary[1].UserId);
|
|
}
|
|
|
|
[Fact]
|
|
public void Summary_UsesTheServerLocalDay()
|
|
{
|
|
// 23:30 UTC on the 10th is already the 11th at UTC+2.
|
|
var zone = TimeZoneInfo.CreateCustomTimeZone("test+2", TimeSpan.FromHours(2), "test+2", "test+2");
|
|
var service = new StatsService(_store, new ManualTimeProvider(new DateTimeOffset(2026, 3, 10, 23, 30, 0, TimeSpan.Zero), zone));
|
|
|
|
Assert.Equal(new DateOnly(2026, 3, 11), service.Today);
|
|
}
|
|
|
|
[Fact]
|
|
public void Cleanup_DeletesRowsOlderThanNinetyDays()
|
|
{
|
|
_store.Add(
|
|
[
|
|
Watched(_alice, _today, 10),
|
|
Watched(_alice, _today.AddDays(-90), 10),
|
|
Watched(_alice, _today.AddDays(-91), 10),
|
|
Watched(_bob, _today.AddDays(-200), 10)
|
|
]);
|
|
|
|
var deleted = _service.Cleanup();
|
|
|
|
Assert.Equal(2, deleted);
|
|
Assert.Equal([_today.AddDays(-90), _today], _store.GetSince(DateOnly.MinValue).Select(r => r.Date));
|
|
}
|
|
|
|
[Fact]
|
|
public void Database_IsCreatedOnFirstUseAndMigrationsRunOnce()
|
|
{
|
|
_store.Add([Watched(_alice, _today, 10)]);
|
|
Assert.True(File.Exists(_temp.FilePath));
|
|
|
|
// A second instance on the same file, as after a server restart.
|
|
var reopened = new SqliteWatchStatsStore(_temp.Open());
|
|
|
|
Assert.Single(reopened.GetSince(_today));
|
|
Assert.Equal(HeraldDatabase.SchemaVersion, _temp.Database.Read(connection =>
|
|
{
|
|
using var command = connection.CreateCommand();
|
|
command.CommandText = "PRAGMA user_version;";
|
|
return Convert.ToInt32(command.ExecuteScalar(), System.Globalization.CultureInfo.InvariantCulture);
|
|
}));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Add_FromManyThreadsLosesNothing()
|
|
{
|
|
var tasks = Enumerable.Range(0, 8).Select(_ => Task.Run(() =>
|
|
{
|
|
for (var i = 0; i < 50; i++)
|
|
{
|
|
_store.Add([Watched(_alice, _today, 1, plays: 1)]);
|
|
}
|
|
}));
|
|
|
|
await Task.WhenAll(tasks);
|
|
|
|
var row = Assert.Single(_store.GetSince(_today));
|
|
Assert.Equal(400, row.WatchedSeconds, 6);
|
|
Assert.Equal(400, row.PlayCount);
|
|
}
|
|
}
|