using AutoFixture; using FluentAssertions; using FluentTerminal.App.Services.Implementation; using FluentTerminal.Models; using FluentTerminal.Models.Enums; using FluentTerminal.Models.Requests; using FluentTerminal.Models.Responses; using Moq; using Newtonsoft.Json; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Windows.Foundation.Collections; using Xunit; namespace FluentTerminal.App.Services.Test { public class TrayProcessCommunicationServiceTests { private readonly Fixture _fixture; public TrayProcessCommunicationServiceTests() { _fixture = new Fixture(); } [Fact] public async Task CreateTerminal_Default_ReturnsResponseFromAppServiceConnection() { var settingsService = new Mock(); var keyBindings = _fixture.CreateMany(3); settingsService.Setup(x => x.GetCommandKeyBindings()).Returns(new Dictionary> { [nameof(Command.ToggleWindow)] = keyBindings.ToList() }); var appServiceConnection = new Mock(); var response = _fixture.Create(); var responseMessage = new ValueSet { [MessageKeys.Type] = _fixture.Create(), [MessageKeys.Content] = JsonConvert.SerializeObject(response) }; appServiceConnection.Setup(x => x.SendMessageAsync(It.IsAny())).Returns(Task.FromResult(responseMessage)); var terminalSize = _fixture.Create(); var shellProfile = _fixture.Create(); var sessionType = _fixture.Create(); var id = _fixture.Create(); var trayProcessCommunicationService = new TrayProcessCommunicationService(settingsService.Object); trayProcessCommunicationService.Initialize(appServiceConnection.Object); var result = await trayProcessCommunicationService.CreateTerminal(id, terminalSize, shellProfile, sessionType); result.Should().BeEquivalentTo(response); } [Fact] public async Task ResizeTerminal_Default_SendsResizeTerminalRequest() { var terminalId = _fixture.Create(); var terminalSize = _fixture.Create(); var settingsService = new Mock(); var keyBindings = _fixture.CreateMany(3); settingsService.Setup(x => x.GetCommandKeyBindings()).Returns(new Dictionary> { [nameof(Command.ToggleWindow)] = keyBindings.ToList() }); var appServiceConnection = new Mock(); var trayProcessCommunicationService = new TrayProcessCommunicationService(settingsService.Object); trayProcessCommunicationService.Initialize(appServiceConnection.Object); await trayProcessCommunicationService.ResizeTerminal(terminalId, terminalSize); appServiceConnection.Verify(x => x.SendMessageAsync(It.Is(d => (byte)d[MessageKeys.Type] == (byte)MessageIdentifiers.ResizeTerminalRequest)), Times.Once); } [Fact] public async Task Write_Default_SendsWriteDataRequest() { var terminalId = _fixture.Create(); var data = _fixture.Create(); var settingsService = new Mock(); var keyBindings = _fixture.CreateMany(3); settingsService.Setup(x => x.GetCommandKeyBindings()).Returns(new Dictionary> { [nameof(Command.ToggleWindow)] = keyBindings.ToList() }); var appServiceConnection = new Mock(); var trayProcessCommunicationService = new TrayProcessCommunicationService(settingsService.Object); trayProcessCommunicationService.Initialize(appServiceConnection.Object); await trayProcessCommunicationService.Write(terminalId, data); appServiceConnection.Verify(x => x.SendMessageAsync(It.Is(d => (byte)d[MessageKeys.Type] == Constants.TerminalBufferRequestIdentifier)), Times.Once); } [Fact] public async Task CloseTerminal_Default_SendsTerminalExitedRequest() { var terminalId = _fixture.Create(); var settingsService = new Mock(); var keyBindings = _fixture.CreateMany(3); settingsService.Setup(x => x.GetCommandKeyBindings()).Returns(new Dictionary> { [nameof(Command.ToggleWindow)] = keyBindings.ToList() }); var appServiceConnection = new Mock(); var trayProcessCommunicationService = new TrayProcessCommunicationService(settingsService.Object); trayProcessCommunicationService.Initialize(appServiceConnection.Object); await trayProcessCommunicationService.CloseTerminal(terminalId); appServiceConnection.Verify(x => x.SendMessageAsync(It.Is(d => (byte)d[MessageKeys.Type] == (byte)MessageIdentifiers.TerminalExitedRequest)), Times.Once); } [Fact] public void OnMessageReceived_TerminalExitedRequest_InvokesTerminalExitedEvent() { var request = new TerminalExitedRequest(_fixture.Create(), _fixture.Create()); var receivedExitStatus = (TerminalExitStatus) null; var message = new ValueSet { [MessageKeys.Type] = (byte) MessageIdentifiers.TerminalExitedRequest, [MessageKeys.Content] = JsonConvert.SerializeObject(request) }; var settingsService = new Mock(); var keyBindings = _fixture.CreateMany(3); settingsService.Setup(x => x.GetCommandKeyBindings()).Returns(new Dictionary> { [nameof(Command.ToggleWindow)] = keyBindings.ToList() }); var trayProcessCommunicationService = new TrayProcessCommunicationService(settingsService.Object); var appServiceConnection = new Mock(); trayProcessCommunicationService.Initialize(appServiceConnection.Object); trayProcessCommunicationService.TerminalExited += (s, status) => { receivedExitStatus = status; }; appServiceConnection.Raise(x => x.MessageReceived += null, null, message); receivedExitStatus.Should().NotBeNull(); receivedExitStatus.TerminalId.Should().Be(request.TerminalId); receivedExitStatus.ExitCode.Should().Be(request.ExitCode); } [Fact] public void OnMessageReceived_DisplayTerminalOutputRequest_InvokesCorrectOutputHandler() { var terminalId = _fixture.Create(); var output = _fixture.Create(); var receivedOutput = default(byte[]); var message = new ValueSet { [MessageKeys.Type] = Constants.TerminalBufferRequestIdentifier, [MessageKeys.TerminalId] = terminalId, [MessageKeys.Content] = output }; var settingsService = new Mock(); var keyBindings = _fixture.CreateMany(3); settingsService.Setup(x => x.GetCommandKeyBindings()).Returns(new Dictionary> { [nameof(Command.ToggleWindow)] = keyBindings.ToList() }); var trayProcessCommunicationService = new TrayProcessCommunicationService(settingsService.Object); var appServiceConnection = new Mock(); trayProcessCommunicationService.Initialize(appServiceConnection.Object); trayProcessCommunicationService.SubscribeForTerminalOutput(terminalId, o => { receivedOutput = o; }); appServiceConnection.Raise(x => x.MessageReceived += null, null, message); receivedOutput.Should().BeEquivalentTo(output); } } }