using System.IO; using Titano.App.Infrastructure; using Titano.Testing; namespace Titano.App.Tests.Server; public sealed class RenderNodeHostTests { private static readonly RenderNodeSession Nas = new(new Uri("https://nas.local:8443/"), "token-a", ["AA:BB", "CC:DD"], Hardware: true); /// Records the nodes it starts; fails the first starts. private sealed class Nodes { public List<(RenderNodeSession Session, Node Node)> Started { get; } = []; public int Failures { get; set; } public IRenderNode Start(RenderNodeSession session) { if (Failures-- > 0) throw new IOException("cache folder unavailable"); var node = new Node(); Started.Add((session, node)); return node; } } private sealed class Node : IRenderNode { private readonly TaskCompletionSource _completion = new(); public bool Stopped { get; private set; } public Task Completion => _completion.Task; /// The server refused the node (signed out, no render-node right): it ends by itself. public void Refuse() => _completion.TrySetResult(); public ValueTask DisposeAsync() { Stopped = true; _completion.TrySetResult(); return ValueTask.CompletedTask; } } [Test] public async Task TheNodeFollowsTheWantedSession() { var nodes = new Nodes(); await using var host = new RenderNodeHost(nodes.Start); host.Update(Nas); host.Update(Nas); await host.Switching; Assert.Equal(Nas, host.Session); Assert.Count(1, nodes.Started); // An equal session (fingerprints in another order) keeps the node. host.Update(Nas with { TrustedFingerprints = ["CC:DD", "AA:BB"] }); await host.Switching; Assert.Count(1, nodes.Started); // A new token (signed in again) is another session: the old node stops before the new one starts. var renewed = Nas with { Token = "token-b" }; host.Update(renewed); await host.Switching; Assert.True(nodes.Started[0].Node.Stopped, "old node stopped"); Assert.Equal((renewed, false), (nodes.Started[1].Session, nodes.Started[1].Node.Stopped)); // A forgotten certificate or another hardware choice also needs a new node. host.Update(renewed with { TrustedFingerprints = [] }); await host.Switching; host.Update(renewed with { TrustedFingerprints = [], Hardware = false }); await host.Switching; Assert.Count(4, nodes.Started); Assert.True(nodes.Started[2].Node.Stopped, "replaced again"); host.Update(null); await host.Switching; Assert.Null(host.Session); Assert.True(nodes.Started[3].Node.Stopped, "stopped when no longer wanted"); } [Test] public async Task ANodeThatStoppedItselfIsStartedAgainByTheNextUpdate() { var nodes = new Nodes(); await using var host = new RenderNodeHost(nodes.Start); host.Update(Nas); await host.Switching; nodes.Started[0].Node.Refuse(); host.Update(Nas); await host.Switching; Assert.Count(2, nodes.Started); Assert.Equal((Nas, false), (host.Session, nodes.Started[1].Node.Stopped)); } [Test] public void TheNodeIsWantedOnlyWhenOfferedAndSignedIn() { var address = new Uri("https://nas.local:8443/"); Assert.Equal(Nas, RenderNodeSession.For(offered: true, connected: true, address, "token-a", ["AA:BB", "CC:DD"], hardware: true)); Assert.Null(RenderNodeSession.For(offered: false, connected: true, address, "token-a", [], hardware: true)); Assert.Null(RenderNodeSession.For(offered: true, connected: false, address, "token-a", [], hardware: true)); Assert.Null(RenderNodeSession.For(offered: true, connected: true, address, null, [], hardware: true)); // signed out Assert.Null(RenderNodeSession.For(offered: true, connected: true, null, "token-a", [], hardware: true)); } [Test] public void HardwarePreferenceDecidesPerPreset() { Assert.True(AppServices.AllowsHardware(HardwarePreference.PreferHardware, "4k-hevc")); Assert.False(AppServices.AllowsHardware(HardwarePreference.SoftwareOnly, "720p-preview")); Assert.True(AppServices.AllowsHardware(HardwarePreference.HardwareForPreviews, "720p-preview")); Assert.False(AppServices.AllowsHardware(HardwarePreference.HardwareForPreviews, "4k-hevc10-archive")); } [Test] public async Task ANodeThatCannotStartIsRetriedByTheNextUpdate() { var nodes = new Nodes { Failures = 1 }; await using var host = new RenderNodeHost(nodes.Start); host.Update(Nas); await host.Switching; Assert.Null(host.Session); host.Update(Nas); await host.Switching; Assert.Equal(Nas, host.Session); } [Test] public async Task DisposingStopsTheNodeAndIgnoresLaterUpdates() { var nodes = new Nodes(); var host = new RenderNodeHost(nodes.Start); host.Update(Nas); await host.DisposeAsync(); Assert.True(nodes.Started.Single().Node.Stopped, "stopped on exit"); host.Update(Nas); await host.Switching; Assert.Count(1, nodes.Started); } }