131 lines
3.5 KiB
C#
131 lines
3.5 KiB
C#
using System.Diagnostics;
|
|
using FluentAssertions;
|
|
using FreeCode.Lsp;
|
|
using Xunit;
|
|
|
|
namespace FreeCode.Tests.Integration;
|
|
|
|
public sealed class LspIntegrationTests
|
|
{
|
|
[Fact]
|
|
public async Task InitializeAsync_CompletesWithoutError()
|
|
{
|
|
using var testHome = new TestHomeDirectoryScope();
|
|
var sut = new LspClientManager();
|
|
|
|
var act = () => sut.InitializeAsync();
|
|
|
|
await act.Should().NotThrowAsync();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ShutdownAsync_CompletesWithoutError()
|
|
{
|
|
using var testHome = new TestHomeDirectoryScope();
|
|
var sut = new LspClientManager();
|
|
|
|
var act = () => sut.ShutdownAsync();
|
|
|
|
await act.Should().NotThrowAsync();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsConnected_InitiallyFalse()
|
|
{
|
|
using var testHome = new TestHomeDirectoryScope();
|
|
var sut = new LspClientManager();
|
|
|
|
sut.IsConnected.Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetServerForFile_NoServers_ReturnsNull()
|
|
{
|
|
using var testHome = new TestHomeDirectoryScope();
|
|
var sut = new LspClientManager();
|
|
|
|
sut.GetServerForFile("test.cs").Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task EnsureServerStartedAsync_NoServer_ReturnsNull()
|
|
{
|
|
using var testHome = new TestHomeDirectoryScope();
|
|
var sut = new LspClientManager();
|
|
|
|
var server = await sut.EnsureServerStartedAsync("test.cs");
|
|
|
|
server.Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void RegisterServer_RegistersExtensions()
|
|
{
|
|
using var testHome = new TestHomeDirectoryScope();
|
|
var sut = new LspClientManager();
|
|
|
|
sut.RegisterServer("csharp", CreateStartInfo(), ".cs");
|
|
|
|
sut.GetServerForFile("test.cs").Should().BeOfType<LspServerInstance>();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetServerForFile_MultipleExtensions_RoutesCorrectly()
|
|
{
|
|
using var testHome = new TestHomeDirectoryScope();
|
|
var sut = new LspClientManager();
|
|
|
|
sut.RegisterServer("polyglot", CreateStartInfo(), ".cs", ".ts");
|
|
|
|
var csharpServer = sut.GetServerForFile("test.cs");
|
|
var typescriptServer = sut.GetServerForFile("test.ts");
|
|
|
|
csharpServer.Should().NotBeNull();
|
|
typescriptServer.Should().NotBeNull();
|
|
csharpServer.Should().BeSameAs(typescriptServer);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAllServers_InitiallyEmpty()
|
|
{
|
|
using var testHome = new TestHomeDirectoryScope();
|
|
var sut = new LspClientManager();
|
|
|
|
sut.GetAllServers().Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAllServers_AfterRegistration_ContainsServer()
|
|
{
|
|
using var testHome = new TestHomeDirectoryScope();
|
|
var sut = new LspClientManager();
|
|
|
|
sut.RegisterServer("csharp", CreateStartInfo(), ".cs");
|
|
|
|
sut.GetAllServers().Should().ContainKey("csharp");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task OpenFileAsync_NoRegisteredServer_NoThrow()
|
|
{
|
|
using var testHome = new TestHomeDirectoryScope();
|
|
var sut = new LspClientManager();
|
|
|
|
var act = () => sut.OpenFileAsync("test.cs", "class C {}");
|
|
|
|
await act.Should().NotThrowAsync();
|
|
}
|
|
|
|
private static ProcessStartInfo CreateStartInfo()
|
|
=> new()
|
|
{
|
|
FileName = "/bin/zsh",
|
|
Arguments = "-lc \"exit 0\"",
|
|
UseShellExecute = false,
|
|
RedirectStandardInput = true,
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
CreateNoWindow = true
|
|
};
|
|
}
|