|
| 1 | +using System.ComponentModel.DataAnnotations; |
| 2 | +using Coder.Desktop.App.Models; |
| 3 | +using Coder.Desktop.App.Services; |
| 4 | +using Coder.Desktop.CoderSdk.Coder; |
| 5 | +using Microsoft.Extensions.Hosting; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using Moq; |
| 8 | +using Serilog; |
| 9 | + |
| 10 | +namespace Coder.Desktop.Tests.App.Services; |
| 11 | + |
| 12 | +[TestFixture] |
| 13 | +public class HostnameSuffixGetterTest |
| 14 | +{ |
| 15 | + const string coderUrl = "https://coder.test/"; |
| 16 | + |
| 17 | + [SetUp] |
| 18 | + public void SetupMocks() |
| 19 | + { |
| 20 | + Log.Logger = new LoggerConfiguration().MinimumLevel.Debug().WriteTo.NUnitOutput().CreateLogger(); |
| 21 | + var builder = Host.CreateApplicationBuilder(); |
| 22 | + builder.Services.AddSerilog(); |
| 23 | + _logger = (ILogger<HostnameSuffixGetter>)builder.Build().Services |
| 24 | + .GetService(typeof(ILogger<HostnameSuffixGetter>))!; |
| 25 | + |
| 26 | + _mCoderApiClientFactory = new Mock<ICoderApiClientFactory>(MockBehavior.Strict); |
| 27 | + _mCredentialManager = new Mock<ICredentialManager>(MockBehavior.Strict); |
| 28 | + _mCoderApiClient = new Mock<ICoderApiClient>(MockBehavior.Strict); |
| 29 | + _mCoderApiClientFactory.Setup(m => m.Create(coderUrl)).Returns(_mCoderApiClient.Object); |
| 30 | + } |
| 31 | + |
| 32 | + private Mock<ICoderApiClientFactory> _mCoderApiClientFactory; |
| 33 | + private Mock<ICredentialManager> _mCredentialManager; |
| 34 | + private Mock<ICoderApiClient> _mCoderApiClient; |
| 35 | + private ILogger<HostnameSuffixGetter> _logger; |
| 36 | + |
| 37 | + [Test(Description = "Mainline no errors")] |
| 38 | + [CancelAfter(10_000)] |
| 39 | + public async Task Mainline(CancellationToken ct) |
| 40 | + { |
| 41 | + _mCredentialManager.Setup(m => m.GetCachedCredentials()) |
| 42 | + .Returns(new CredentialModel() { State = CredentialState.Invalid }); |
| 43 | + var hostnameSuffixGetter = |
| 44 | + new HostnameSuffixGetter(_mCredentialManager.Object, _mCoderApiClientFactory.Object, _logger); |
| 45 | + |
| 46 | + // initially, we return the default |
| 47 | + Assert.That(hostnameSuffixGetter.GetCachedSuffix(), Is.EqualTo(".coder")); |
| 48 | + |
| 49 | + // subscribed to suffix changes |
| 50 | + var suffixCompletion = new TaskCompletionSource<string>(); |
| 51 | + hostnameSuffixGetter.SuffixChanged += (_, suffix) => suffixCompletion.SetResult(suffix); |
| 52 | + |
| 53 | + // set the client to return "test" as the suffix |
| 54 | + _mCoderApiClient.Setup(m => m.SetSessionToken("test-token")); |
| 55 | + _mCoderApiClient.Setup(m => m.GetAgentConnectionInfoGeneric(It.IsAny<CancellationToken>())) |
| 56 | + .Returns(Task.FromResult(new AgentConnectionInfo() { HostnameSuffix = "test" })); |
| 57 | + |
| 58 | + _mCredentialManager.Raise(m => m.CredentialsChanged += null, _mCredentialManager.Object, new CredentialModel |
| 59 | + { |
| 60 | + State = CredentialState.Valid, |
| 61 | + CoderUrl = new Uri(coderUrl), |
| 62 | + ApiToken = "test-token", |
| 63 | + }); |
| 64 | + var gotSuffix = await TaskOrCancellation(suffixCompletion.Task, ct); |
| 65 | + Assert.That(gotSuffix, Is.EqualTo(".test")); |
| 66 | + |
| 67 | + // now, we should return the .test domain going forward |
| 68 | + Assert.That(hostnameSuffixGetter.GetCachedSuffix(), Is.EqualTo(".test")); |
| 69 | + } |
| 70 | + |
| 71 | + [Test(Description = "Retries if error")] |
| 72 | + [CancelAfter(30_000)] |
| 73 | + // TODO: make this test not have to actually wait for the retry. |
| 74 | + public async Task RetryError(CancellationToken ct) |
| 75 | + { |
| 76 | + _mCredentialManager.Setup(m => m.GetCachedCredentials()) |
| 77 | + .Returns(new CredentialModel() { State = CredentialState.Invalid }); |
| 78 | + var hostnameSuffixGetter = |
| 79 | + new HostnameSuffixGetter(_mCredentialManager.Object, _mCoderApiClientFactory.Object, _logger); |
| 80 | + |
| 81 | + // subscribed to suffix changes |
| 82 | + var suffixCompletion = new TaskCompletionSource<string>(); |
| 83 | + hostnameSuffixGetter.SuffixChanged += (_, suffix) => suffixCompletion.SetResult(suffix); |
| 84 | + |
| 85 | + // set the client to fail once, then return successfully |
| 86 | + _mCoderApiClient.Setup(m => m.SetSessionToken("test-token")); |
| 87 | + var connectionInfoCompletion = new TaskCompletionSource<AgentConnectionInfo>(); |
| 88 | + _mCoderApiClient.SetupSequence(m => m.GetAgentConnectionInfoGeneric(It.IsAny<CancellationToken>())) |
| 89 | + .Returns(Task.FromException<AgentConnectionInfo>(new Exception("a bad thing happened"))) |
| 90 | + .Returns(Task.FromResult(new AgentConnectionInfo() { HostnameSuffix = "test" })); |
| 91 | + |
| 92 | + _mCredentialManager.Raise(m => m.CredentialsChanged += null, _mCredentialManager.Object, new CredentialModel |
| 93 | + { |
| 94 | + State = CredentialState.Valid, |
| 95 | + CoderUrl = new Uri(coderUrl), |
| 96 | + ApiToken = "test-token", |
| 97 | + }); |
| 98 | + var gotSuffix = await TaskOrCancellation(suffixCompletion.Task, ct); |
| 99 | + Assert.That(gotSuffix, Is.EqualTo(".test")); |
| 100 | + |
| 101 | + // now, we should return the .test domain going forward |
| 102 | + Assert.That(hostnameSuffixGetter.GetCachedSuffix(), Is.EqualTo(".test")); |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// TaskOrCancellation waits for either the task to complete, or the given token to be canceled. |
| 107 | + /// </summary> |
| 108 | + internal static async Task<TResult> TaskOrCancellation<TResult>(Task<TResult> task, |
| 109 | + CancellationToken cancellationToken) |
| 110 | + { |
| 111 | + var cancellationTask = new TaskCompletionSource<TResult>(); |
| 112 | + await using (cancellationToken.Register(() => cancellationTask.TrySetCanceled())) |
| 113 | + { |
| 114 | + // Wait for either the task or the cancellation |
| 115 | + var completedTask = await Task.WhenAny(task, cancellationTask.Task); |
| 116 | + // Await to propagate exceptions, if any |
| 117 | + return await completedTask; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments