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"); /// 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 IAsyncDisposable 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 : IAsyncDisposable { public bool Stopped { get; private set; } public ValueTask DisposeAsync() { Stopped = true; 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); // 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)); host.Update(null); await host.Switching; Assert.Null(host.Session); Assert.True(nodes.Started[1].Node.Stopped, "stopped when no longer wanted"); } [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); } }