connect MCP language queries to project LSP
This commit is contained in:
+37
@@ -0,0 +1,37 @@
|
||||
package p.studio.lsp.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.lsp.messages.*;
|
||||
import java.nio.file.Path;
|
||||
import java.net.Socket;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class LspServerLifecycleTest {
|
||||
@TempDir Path temporary;
|
||||
|
||||
@Test void projectEndpointsAreIndependentAndShutdownClosesConnectedSockets() throws Exception {
|
||||
var lifecycle = new LspServerLifecycleImpl();
|
||||
var first = new LspProjectContext("first", "pbs", temporary);
|
||||
var second = new LspProjectContext("second", "pbs", temporary.resolve("second"));
|
||||
var configuration = LspServerConfiguration.builder().endpoint(new LspServerEndpoint("127.0.0.1", 0)).build();
|
||||
lifecycle.bootServer(configuration, first); lifecycle.bootServer(configuration, second);
|
||||
try {
|
||||
var endpoint = lifecycle.endpoint(first).orElseThrow();
|
||||
assertNotEquals(endpoint.port(), lifecycle.endpoint(second).orElseThrow().port());
|
||||
assertTrue(lifecycle.endpoint(new LspProjectContext("first", "other-language", temporary)).isEmpty());
|
||||
try (var socket = new Socket(endpoint.host(), endpoint.port())) {
|
||||
socket.setSoTimeout(2000);
|
||||
String request = "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{\"rootUri\":\"" + temporary.toUri() + "\",\"capabilities\":{}}}";
|
||||
byte[] body = request.getBytes(java.nio.charset.StandardCharsets.UTF_8);
|
||||
socket.getOutputStream().write(("Content-Length: " + body.length + "\r\n\r\n").getBytes(java.nio.charset.StandardCharsets.US_ASCII));
|
||||
socket.getOutputStream().write(body); socket.getOutputStream().flush();
|
||||
assertNotEquals(-1, socket.getInputStream().read());
|
||||
lifecycle.shutdownServer(first);
|
||||
socket.getInputStream().readAllBytes(); // Buffered reply drains, then EOF; no abandoned client connection.
|
||||
assertTrue(lifecycle.endpoint(first).isEmpty());
|
||||
assertTrue(lifecycle.endpoint(second).isPresent());
|
||||
}
|
||||
} finally { lifecycle.shutdownServer(first); lifecycle.shutdownServer(second); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user