using Jellyfin.Plugin.Multilang.Services.Injection; using Microsoft.Extensions.Logging.Abstractions; namespace Jellyfin.Plugin.Multilang.Tests; public sealed class WebScriptInjectorTests : IDisposable { private const string Shell = "
"; private const string OtherBlock = "\n\n\n"; private readonly string _root = Path.Combine(Path.GetTempPath(), "web-inject-test-" + Guid.NewGuid().ToString("N")); private string Index => Path.Combine(_root, "index.html"); public WebScriptInjectorTests() { Directory.CreateDirectory(_root); File.WriteAllText(Index, Shell); } [Fact] public void ReplacingShorterScriptPreservesOtherPluginAndRepeatedInjectionDoesNotWrite() { File.WriteAllText(Index, Shell.Replace("", OtherBlock + "")); Inject("a-long-old-script-version.js"); Inject("v2.js"); var content = File.ReadAllText(Index); Assert.DoesNotContain("a-long-old", content); Assert.Contains(OtherBlock, content); Assert.Contains("v2.js", content); Assert.EndsWith("", content.TrimEnd()); Assert.Equal(1, content.Split("").Length - 1); var timestamp = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); File.SetLastWriteTimeUtc(Index, timestamp); Inject("v2.js"); Assert.Equal(timestamp, File.GetLastWriteTimeUtc(Index)); Assert.Single(Directory.GetFiles(_root)); } [Fact] public void RemovalPreservesOtherPlugin() { var original = Shell.Replace("", OtherBlock + ""); File.WriteAllText(Index, original); Inject("plugin.js"); WebScriptInjector.RemoveInjected(_root); var content = File.ReadAllText(Index); Assert.DoesNotContain("", content); Assert.Contains(OtherBlock, content); Assert.EndsWith("", content.TrimEnd()); WebScriptInjector.RemoveInjected(_root); Assert.Equal(content, File.ReadAllText(Index)); } [Fact] public void MissingBodyEndLeavesFileUntouched() { File.WriteAllText(Index, "incomplete"); Inject("plugin.js"); Assert.Equal("incomplete", File.ReadAllText(Index)); Assert.Single(Directory.GetFiles(_root)); } [Theory] [InlineData(false)] [InlineData(true)] public async Task SharedLockProtectsWholeReadModifyWriteIncludingRemoval(bool remove) { Inject("plugin.js"); using var entered = new ManualResetEventSlim(); using var release = new ManualResetEventSlim(); var other = Task.Run(() => { using var mutex = new Mutex(false, "Jellyfin.Web.IndexHtml"); Assert.True(mutex.WaitOne(TimeSpan.FromSeconds(10))); try { var content = File.ReadAllText(Index); entered.Set(); Assert.True(release.Wait(TimeSpan.FromSeconds(10))); File.WriteAllText(Index, content.Replace("", OtherBlock + "")); } finally { mutex.ReleaseMutex(); } }); Task ours = Task.CompletedTask; try { Assert.True(entered.Wait(TimeSpan.FromSeconds(10))); using var calling = new ManualResetEventSlim(); ours = Task.Run(() => { calling.Set(); if (remove) WebScriptInjector.RemoveInjected(_root); else Inject("updated.js"); }); Assert.True(calling.Wait(TimeSpan.FromSeconds(10))); await Task.Delay(100); Assert.False(ours.IsCompleted); } finally { release.Set(); await Task.WhenAll(other, ours).WaitAsync(TimeSpan.FromSeconds(10)); } var final = File.ReadAllText(Index); Assert.Contains(OtherBlock, final); Assert.Equal(!remove, final.Contains("")); if (!remove) Assert.Contains("updated.js", final); Assert.EndsWith("", final.TrimEnd()); } [Fact] public async Task ReadersNeverSeeTruncatedHtmlDuringUpdates() { using var stop = new CancellationTokenSource(); using var reading = new ManualResetEventSlim(); var reader = Task.Run(() => { reading.Set(); while (!stop.IsCancellationRequested) { var content = File.ReadAllText(Index); Assert.StartsWith("", content); Assert.EndsWith("", content.TrimEnd()); } }); try { Assert.True(reading.Wait(TimeSpan.FromSeconds(10))); for (var i = 0; i < 30; i++) Inject(new string('x', i % 2 == 0 ? 131072 : 1) + i + ".js"); } finally { stop.Cancel(); await reader.WaitAsync(TimeSpan.FromSeconds(10)); } Assert.Single(Directory.GetFiles(_root)); } private void Inject(string source) => WebScriptInjector.EnsureInjected(_root, $"", NullLogger.Instance); public void Dispose() => Directory.Delete(_root, recursive: true); }