using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using Tgstation.Server.Api.Models; using Tgstation.Server.Host.Components.Repository; using Tgstation.Server.Host.Configuration; using Tgstation.Server.Host.IO; using Tgstation.Server.Host.Jobs; using Tgstation.Server.Host.System; using Tgstation.Server.Host.Utils; namespace Tgstation.Server.Host.Components.Engine.Tests { [TestClass] public sealed class TestOpenDreamInstaller { [TestMethod] public async Task TestDownloadsUseSameRepositoryIfItExists() { await RepoDownloadTest(false); } [TestMethod] public async Task TestDownloadsCloneRepositoryIfItDoesntExists() { await RepoDownloadTest(true); } static async Task RepoDownloadTest(bool needsClone) { var mockGeneralConfigOptions = new Mock>(); var generalConfig = new GeneralConfiguration(); var mockSessionConfigOptions = new Mock>(); var sessionConfig = new SessionConfiguration(); Assert.IsNotNull(generalConfig.OpenDreamGitUrl); mockGeneralConfigOptions.SetupGet(x => x.CurrentValue).Returns(generalConfig); mockSessionConfigOptions.SetupGet(x => x.CurrentValue).Returns(sessionConfig); var cloneAttempts = 0; var mockRepository = new Mock(); mockRepository.Setup(x => x.CommittishIsParent(It.IsNotNull(), It.IsAny())).ReturnsAsync(true); var mockRepositoryManager = new Mock(); mockRepositoryManager.Setup(x => x.CloneRepository( generalConfig.OpenDreamGitUrl, null, null, null, It.IsNotNull(), true, It.IsAny())) .Callback(() => ++cloneAttempts) .ReturnsAsync(needsClone ? mockRepository.Object : null) .Verifiable(Times.Exactly(1)); mockRepositoryManager.Setup(x => x.LoadRepository( It.IsAny())) .Returns(() => { Assert.AreEqual(1, cloneAttempts); return ValueTask.FromResult(mockRepository.Object); }) .Verifiable(Times.Exactly(needsClone ? 0 : 1)); var installer = new OpenDreamInstaller( Mock.Of(), Mock.Of>(), Mock.Of(), Mock.Of(), mockRepositoryManager.Object, Mock.Of(), Mock.Of(), mockGeneralConfigOptions.Object, mockSessionConfigOptions.Object); var data = await installer.DownloadVersion( new EngineVersion { Engine = EngineType.OpenDream, SourceSHA = new string('a', Limits.MaximumCommitShaLength), }, new JobProgressReporter(), CancellationToken.None); Assert.IsNotNull(data); mockRepositoryManager.VerifyAll(); } } }