using Jellyfin.Plugin.Multilang.Services; namespace Jellyfin.Plugin.Multilang.Tests; public sealed class ItemsProxyCacheTests { [Fact] public void StoreRetrievesAndInvalidatesByItemId() { var cache = new ItemsProxyCache(); Assert.True(Store(cache, "key", ["A"])); Assert.True(cache.TryGet("key", TimeSpan.FromMinutes(1), out var response)); Assert.Equal("body-key", response.Body); Assert.Equal(1, response.ItemCount); cache.InvalidateItems(["a"]); Assert.False(cache.TryGet("key", TimeSpan.FromMinutes(1), out _)); } [Fact] public void MicroCacheServesFastResponsesAndIsInvalidatedWithTheMainCache() { var cache = new ItemsProxyCache(); Assert.True(cache.StoreMicro("key", "user", "/Items", ["A"], "micro", "application/json", 1, 1024)); Assert.True(cache.TryGet("key", TimeSpan.FromMinutes(1), out var response)); Assert.Equal("micro", response.Body); cache.InvalidateItems(["A"]); Assert.False(cache.TryGet("key", TimeSpan.FromMinutes(1), out _)); } [Fact] public void StoreEvictsTheOldestEntryWhenCapacityIsExceeded() { var cache = new ItemsProxyCache(); Assert.True(Store(cache, "first", ["A"], body: "1111", maxBytes: 6)); Thread.Sleep(10); Assert.True(Store(cache, "second", ["B"], body: "2222", maxBytes: 6)); Assert.False(cache.TryGet("first", TimeSpan.FromMinutes(1), out _)); Assert.True(cache.TryGet("second", TimeSpan.FromMinutes(1), out _)); } [Fact] public void StoreEntriesExpireAtTheConfiguredTtl() { var cache = new ItemsProxyCache(); Assert.True(Store(cache, "key", ["A"], ttl: TimeSpan.FromMilliseconds(1))); Thread.Sleep(20); Assert.False(cache.TryGet("key", TimeSpan.FromMilliseconds(1), out _)); } [Fact] public void StoreRejectsResponsesLargerThanTheConfiguredLimit() { var cache = new ItemsProxyCache(); Assert.False(Store(cache, "key", ["A"], body: "12345", maxBytes: 4)); Assert.False(cache.TryGet("key", TimeSpan.FromMinutes(1), out _)); } private static bool Store( ItemsProxyCache cache, string key, string[] itemIds, string? body = null, long maxBytes = 1024, TimeSpan? ttl = null) => cache.Store( key, "user", "/Items", itemIds, body ?? "body-" + key, "application/json", 100, maxBytes, ttl ?? TimeSpan.FromMinutes(1)); }