using Jellyfin.Plugin.JRay.Services;
using Xunit;
namespace Jellyfin.Plugin.JRay.Tests;
///
/// JR-022 — an earlier JRay injected its overlay script into index.html on
/// disk. Those users must not be left with a stale injection pointing at
/// endpoints that have since changed, so removal survives even though JR-021
/// deleted the injection that created it.
///
/// Removal keys on JRay's own marker. The cases that matter are the ones where
/// it could reach too far: another plugin's injection, or a script tag that
/// looks like JRay's but carries no marker.
///
/// TRACES: UT-001, UT-002, UT-003, UT-004, UT-005 | JR-022
///
public class WebClientPatchServiceTests
{
private const string Marker = "";
private const string ScriptTag = "";
// UT-001
[Fact]
public void RemoveInjection_WithMarkedTagAndNewline_RestoresOriginalBytes()
{
var original = "
x
\n";
var patched = "x
\n" + ScriptTag + Marker + "\n";
// The trailing newline goes with the tag. If it did not, every
// install/uninstall cycle would leave another blank line behind.
Assert.Equal(original, WebClientPatchService.RemoveInjection(patched));
}
// UT-002
[Fact]
public void RemoveInjection_WithMarkedTagAndNoNewline_RemovesTag()
{
var patched = "" + ScriptTag + Marker + "";
Assert.Equal("", WebClientPatchService.RemoveInjection(patched));
}
// UT-003
[Fact]
public void RemoveInjection_WithNoMarker_LeavesDocumentUnchanged()
{
var clean = "x
\n";
Assert.Equal(clean, WebClientPatchService.RemoveInjection(clean));
}
// UT-004
[Fact]
public void RemoveInjection_IsIdempotent()
{
var patched = "" + ScriptTag + Marker + "\n";
var once = WebClientPatchService.RemoveInjection(patched);
var twice = WebClientPatchService.RemoveInjection(once);
// Startup calls this unconditionally, so it runs on every boot forever
// after the patch is gone.
Assert.Equal(once, twice);
}
// UT-005
[Fact]
public void RemoveInjection_LeavesAnotherPluginsInjectionIntact()
{
var foreign = "";
var patched = "" + foreign + ScriptTag + Marker + "\n";
var cleaned = WebClientPatchService.RemoveInjection(patched);
// The marker is what makes removal unambiguous. Removing anything we did
// not add is the failure this guards: it is another plugin's file too.
Assert.Contains(foreign, cleaned, System.StringComparison.Ordinal);
Assert.DoesNotContain(Marker, cleaned, System.StringComparison.Ordinal);
}
// UT-006
[Fact]
public void RemoveInjection_WithUnmarkedLookalikeTag_LeavesItAlone()
{
// Same script tag, no marker: JRay did not write this, so JRay does not
// remove it.
var patched = "" + ScriptTag + "\n";
Assert.Equal(patched, WebClientPatchService.RemoveInjection(patched));
}
}