package moving
This commit is contained in:
+63
@@ -0,0 +1,63 @@
|
||||
package p.studio.lsp.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.lsp.messages.LspProjectContext;
|
||||
import p.studio.lsp.messages.LspServerBootRequest;
|
||||
import p.studio.lsp.messages.LspServerEndpoint;
|
||||
import p.studio.lsp.services.compiler.CompilerLanguageServiceBridge;
|
||||
import p.studio.lsp.services.host.ProjectScopedLspServerHost;
|
||||
import p.studio.lsp.services.host.ProjectScopedLspServerHostFactory;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class LspServerLifecycleTest {
|
||||
|
||||
@Test
|
||||
void lifecycleBootsAndShutsDownProjectScopedHost() {
|
||||
final FakeHost host = new FakeHost();
|
||||
final ProjectScopedLspServerHostFactory factory = (request, compilerBridge) -> host;
|
||||
final var lifecycle = new LspServerLifecycle(factory, new CompilerLanguageServiceBridge());
|
||||
final var project = new LspProjectContext("demo", "pbs", Path.of("."));
|
||||
|
||||
final var handle = lifecycle.bootServer(LspServerBootRequest.defaults(project));
|
||||
|
||||
assertEquals(project, handle.project());
|
||||
assertEquals("127.0.0.1", handle.endpoint().host());
|
||||
assertEquals(7777, handle.endpoint().port());
|
||||
assertTrue(host.started);
|
||||
|
||||
lifecycle.shutdownServer(project);
|
||||
assertTrue(host.closed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void lifecycleRejectsDoubleBootForSameProject() {
|
||||
final ProjectScopedLspServerHostFactory factory = (request, compilerBridge) -> new FakeHost();
|
||||
final var lifecycle = new LspServerLifecycle(factory, new CompilerLanguageServiceBridge());
|
||||
final var request = LspServerBootRequest.defaults(new LspProjectContext("demo", "pbs", Path.of(".")));
|
||||
|
||||
lifecycle.bootServer(request);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> lifecycle.bootServer(request));
|
||||
}
|
||||
|
||||
private static final class FakeHost implements ProjectScopedLspServerHost {
|
||||
private boolean started;
|
||||
private boolean closed;
|
||||
|
||||
@Override
|
||||
public LspServerEndpoint start() {
|
||||
started = true;
|
||||
return new LspServerEndpoint("127.0.0.1", 7777);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
closed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package p.studio.lsp.services.boundary;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class LspV1BoundaryTest {
|
||||
|
||||
@Test
|
||||
void lsp4jMustRemainContainedInsideLspV1Sources() throws IOException {
|
||||
final List<Path> forbiddenRoots = List.of(
|
||||
Path.of("../prometeu-lsp-api/src/main/java"),
|
||||
Path.of("../../../prometeu-studio/src/main/java"),
|
||||
Path.of("../../../prometeu-app/src/main/java"),
|
||||
Path.of("../../../prometeu-compiler"),
|
||||
Path.of("../../../prometeu-packer"),
|
||||
Path.of("../../../prometeu-infra"));
|
||||
|
||||
for (final Path root : forbiddenRoots) {
|
||||
assertNoLsp4jImports(root.normalize());
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertNoLsp4jImports(final Path root) throws IOException {
|
||||
if (!Files.exists(root)) {
|
||||
return;
|
||||
}
|
||||
final List<Path> javaFiles;
|
||||
try (var stream = Files.walk(root)) {
|
||||
javaFiles = stream
|
||||
.filter(path -> path.toString().endsWith(".java"))
|
||||
.toList();
|
||||
}
|
||||
|
||||
for (final Path javaFile : javaFiles) {
|
||||
final String source = Files.readString(javaFile);
|
||||
assertFalse(source.contains("org.eclipse.lsp4j"), () -> "LSP4J leaked outside lsp-v1: " + javaFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package p.studio.lsp.services.compiler;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.lsp.messages.BaselineDocumentAnalysis;
|
||||
import p.studio.lsp.messages.BaselineHover;
|
||||
import p.studio.lsp.messages.BaselineServerDescription;
|
||||
import p.studio.lsp.messages.LspProjectContext;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class CompilerLanguageServiceBridgeTest {
|
||||
|
||||
@Test
|
||||
void bridgeProvidesStableBaselineResponses() {
|
||||
final CompilerLanguageServiceBridge bridge = new CompilerLanguageServiceBridge();
|
||||
final LspProjectContext project = new LspProjectContext("demo", "pbs", Path.of("."));
|
||||
|
||||
final BaselineServerDescription server = bridge.describeServer(project);
|
||||
final BaselineDocumentAnalysis analysis = bridge.analyzeDocument(project, "file:///demo.pbs", "let x = 1");
|
||||
final BaselineHover hover = bridge.hover(project, "file:///demo.pbs", 0, 0);
|
||||
|
||||
assertEquals("Prometeu Studio LSP", server.name());
|
||||
assertTrue(server.hoverSupported());
|
||||
assertFalse(analysis.issues().isEmpty());
|
||||
assertTrue(analysis.issues().getFirst().message().contains("BuilderPipelineService"));
|
||||
assertTrue(hover.markdown().contains("Compiler-backed baseline"));
|
||||
}
|
||||
}
|
||||
+175
@@ -0,0 +1,175 @@
|
||||
package p.studio.lsp.services.protocol;
|
||||
|
||||
import org.eclipse.lsp4j.InitializeParams;
|
||||
import org.eclipse.lsp4j.InitializeResult;
|
||||
import org.eclipse.lsp4j.WorkspaceFolder;
|
||||
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException;
|
||||
import org.eclipse.lsp4j.PublishDiagnosticsParams;
|
||||
import org.eclipse.lsp4j.services.TextDocumentService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.lsp.messages.BaselineDocumentAnalysis;
|
||||
import p.studio.lsp.messages.BaselineHover;
|
||||
import p.studio.lsp.messages.BaselineServerDescription;
|
||||
import p.studio.lsp.messages.LspProjectContext;
|
||||
import p.studio.lsp.services.LanguageServiceBridge;
|
||||
import p.studio.lsp.services.protocol.mapping.ProtocolMessageMapper;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletionException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PrometeuLanguageServerTest {
|
||||
|
||||
@Test
|
||||
void initializeDelegatesThroughBridgeAndMapper() {
|
||||
final RecordingBridge bridge = new RecordingBridge();
|
||||
final RecordingMapper mapper = new RecordingMapper();
|
||||
final InitializeResult expected = new InitializeResult();
|
||||
mapper.initializeResult = expected;
|
||||
|
||||
final PrometeuLanguageServer server = new PrometeuLanguageServer(
|
||||
new LspProjectContext("demo", "pbs", Path.of(".")),
|
||||
bridge,
|
||||
mapper,
|
||||
new NoopTextDocumentService(),
|
||||
new PrometeuWorkspaceService());
|
||||
|
||||
final InitializeParams params = new InitializeParams();
|
||||
params.setRootUri(Path.of(".").toAbsolutePath().normalize().toUri().toString());
|
||||
|
||||
final InitializeResult result = server.initialize(params).join();
|
||||
|
||||
assertSame(expected, result);
|
||||
assertEquals(1, bridge.describeServerCalls);
|
||||
assertSame(bridge.serverDescription, mapper.lastServerDescription);
|
||||
}
|
||||
|
||||
@Test
|
||||
void initializeRejectsClientBoundToDifferentProjectRoot() {
|
||||
final RecordingBridge bridge = new RecordingBridge();
|
||||
final RecordingMapper mapper = new RecordingMapper();
|
||||
final PrometeuLanguageServer server = new PrometeuLanguageServer(
|
||||
new LspProjectContext("demo", "pbs", Path.of("/tmp/studio-project")),
|
||||
bridge,
|
||||
mapper,
|
||||
new NoopTextDocumentService(),
|
||||
new PrometeuWorkspaceService());
|
||||
|
||||
final InitializeParams params = new InitializeParams();
|
||||
params.setRootUri(Path.of("/tmp/another-project").toUri().toString());
|
||||
|
||||
final CompletionException failure = org.junit.jupiter.api.Assertions.assertThrows(
|
||||
CompletionException.class,
|
||||
() -> server.initialize(params).join());
|
||||
|
||||
final ResponseErrorException responseError = assertInstanceOf(ResponseErrorException.class, failure.getCause());
|
||||
assertTrue(responseError.getMessage().contains("/tmp/studio-project"));
|
||||
assertEquals(0, bridge.describeServerCalls);
|
||||
}
|
||||
|
||||
@Test
|
||||
void initializeAcceptsMatchingWorkspaceFolder() {
|
||||
final RecordingBridge bridge = new RecordingBridge();
|
||||
final RecordingMapper mapper = new RecordingMapper();
|
||||
mapper.initializeResult = new InitializeResult();
|
||||
final Path projectRoot = Path.of("/tmp/studio-project").toAbsolutePath().normalize();
|
||||
final PrometeuLanguageServer server = new PrometeuLanguageServer(
|
||||
new LspProjectContext("demo", "pbs", projectRoot),
|
||||
bridge,
|
||||
mapper,
|
||||
new NoopTextDocumentService(),
|
||||
new PrometeuWorkspaceService());
|
||||
|
||||
final InitializeParams params = new InitializeParams();
|
||||
params.setWorkspaceFolders(List.of(new WorkspaceFolder(projectRoot.toUri().toString(), "studio-project")));
|
||||
|
||||
server.initialize(params).join();
|
||||
|
||||
assertEquals(1, bridge.describeServerCalls);
|
||||
}
|
||||
|
||||
private static final class RecordingBridge implements LanguageServiceBridge {
|
||||
private int describeServerCalls;
|
||||
private final BaselineServerDescription serverDescription = new BaselineServerDescription("demo", "1", true);
|
||||
|
||||
@Override
|
||||
public BaselineServerDescription describeServer(final LspProjectContext project) {
|
||||
describeServerCalls++;
|
||||
return serverDescription;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaselineDocumentAnalysis analyzeDocument(final LspProjectContext project, final String documentUri, final String text) {
|
||||
return new BaselineDocumentAnalysis(List.of());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaselineHover hover(final LspProjectContext project, final String documentUri, final int line, final int character) {
|
||||
return new BaselineHover("hover");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onSave(final LspProjectContext project, final String documentUri) {
|
||||
return "saved";
|
||||
}
|
||||
}
|
||||
|
||||
private static final class RecordingMapper implements ProtocolMessageMapper {
|
||||
private InitializeResult initializeResult;
|
||||
private BaselineServerDescription lastServerDescription;
|
||||
|
||||
@Override
|
||||
public InitializeResult mapInitializeResult(final BaselineServerDescription description) {
|
||||
lastServerDescription = description;
|
||||
return initializeResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PublishDiagnosticsParams mapDiagnostics(final String uri, final BaselineDocumentAnalysis analysis) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PublishDiagnosticsParams emptyDiagnostics(final String uri) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public org.eclipse.lsp4j.Hover mapHover(final BaselineHover hover) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public org.eclipse.lsp4j.MessageParams mapInfoMessage(final String message) {
|
||||
return new org.eclipse.lsp4j.MessageParams();
|
||||
}
|
||||
|
||||
@Override
|
||||
public org.eclipse.lsp4j.MessageParams mapErrorMessage(final String message) {
|
||||
return new org.eclipse.lsp4j.MessageParams();
|
||||
}
|
||||
}
|
||||
|
||||
private static final class NoopTextDocumentService implements TextDocumentService {
|
||||
@Override
|
||||
public void didOpen(final org.eclipse.lsp4j.DidOpenTextDocumentParams params) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didChange(final org.eclipse.lsp4j.DidChangeTextDocumentParams params) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didClose(final org.eclipse.lsp4j.DidCloseTextDocumentParams params) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didSave(final org.eclipse.lsp4j.DidSaveTextDocumentParams params) {
|
||||
}
|
||||
}
|
||||
}
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
package p.studio.lsp.services.protocol;
|
||||
|
||||
import org.eclipse.lsp4j.*;
|
||||
import org.eclipse.lsp4j.services.LanguageClient;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.lsp.messages.*;
|
||||
import p.studio.lsp.services.LanguageServiceBridge;
|
||||
import p.studio.lsp.services.protocol.mapping.ProtocolMessageMapper;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
class PrometeuTextDocumentServiceTest {
|
||||
|
||||
@Test
|
||||
void documentEventsDelegateToBridgeAndMapper() {
|
||||
final RecordingBridge bridge = new RecordingBridge();
|
||||
final RecordingMapper mapper = new RecordingMapper();
|
||||
final RecordingClient client = new RecordingClient();
|
||||
final PrometeuTextDocumentService service = new PrometeuTextDocumentService(
|
||||
new LspProjectContext("demo", "pbs", Path.of(".")),
|
||||
bridge,
|
||||
mapper);
|
||||
service.connect(client);
|
||||
|
||||
service.didOpen(new DidOpenTextDocumentParams(new TextDocumentItem("file:///demo.pbs", "pbs", 1, "abc")));
|
||||
service.didSave(new DidSaveTextDocumentParams(new TextDocumentIdentifier("file:///demo.pbs"), "abc"));
|
||||
service.hover(new HoverParams(new TextDocumentIdentifier("file:///demo.pbs"), new Position(0, 0))).join();
|
||||
|
||||
assertEquals(1, bridge.analyzeCalls);
|
||||
assertEquals(1, bridge.saveCalls);
|
||||
assertEquals(1, bridge.hoverCalls);
|
||||
assertSame(bridge.lastAnalysis, mapper.lastAnalysis);
|
||||
assertSame(bridge.lastHover, mapper.lastHover);
|
||||
assertSame(mapper.diagnostics, client.lastDiagnostics);
|
||||
assertSame(mapper.infoMessage, client.lastLogMessage);
|
||||
}
|
||||
|
||||
private static final class RecordingBridge implements LanguageServiceBridge {
|
||||
private int analyzeCalls;
|
||||
private int saveCalls;
|
||||
private int hoverCalls;
|
||||
private final BaselineDocumentAnalysis lastAnalysis = new BaselineDocumentAnalysis(List.of(
|
||||
new BaselineDocumentIssue(0, 0, 0, 1, BaselineIssueSeverity.INFORMATION, "src", "msg")));
|
||||
private final BaselineHover lastHover = new BaselineHover("hover");
|
||||
|
||||
@Override
|
||||
public BaselineServerDescription describeServer(final LspProjectContext project) {
|
||||
return new BaselineServerDescription("demo", "1", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaselineDocumentAnalysis analyzeDocument(final LspProjectContext project, final String documentUri, final String text) {
|
||||
analyzeCalls++;
|
||||
return lastAnalysis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaselineHover hover(final LspProjectContext project, final String documentUri, final int line, final int character) {
|
||||
hoverCalls++;
|
||||
return lastHover;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onSave(final LspProjectContext project, final String documentUri) {
|
||||
saveCalls++;
|
||||
return "saved";
|
||||
}
|
||||
}
|
||||
|
||||
private static final class RecordingMapper implements ProtocolMessageMapper {
|
||||
private final PublishDiagnosticsParams diagnostics = new PublishDiagnosticsParams("file:///demo.pbs", List.of());
|
||||
private final MessageParams infoMessage = new MessageParams(MessageType.Info, "saved");
|
||||
private BaselineDocumentAnalysis lastAnalysis;
|
||||
private BaselineHover lastHover;
|
||||
|
||||
@Override
|
||||
public InitializeResult mapInitializeResult(final BaselineServerDescription description) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PublishDiagnosticsParams mapDiagnostics(final String uri, final BaselineDocumentAnalysis analysis) {
|
||||
lastAnalysis = analysis;
|
||||
return diagnostics;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PublishDiagnosticsParams emptyDiagnostics(final String uri) {
|
||||
return new PublishDiagnosticsParams(uri, List.of());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Hover mapHover(final BaselineHover hover) {
|
||||
lastHover = hover;
|
||||
return new Hover();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageParams mapInfoMessage(final String message) {
|
||||
return infoMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageParams mapErrorMessage(final String message) {
|
||||
return infoMessage;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class RecordingClient implements LanguageClient {
|
||||
private PublishDiagnosticsParams lastDiagnostics;
|
||||
private MessageParams lastLogMessage;
|
||||
|
||||
@Override
|
||||
public void telemetryEvent(final Object object) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publishDiagnostics(final PublishDiagnosticsParams diagnostics) {
|
||||
lastDiagnostics = diagnostics;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showMessage(final MessageParams messageParams) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<MessageActionItem> showMessageRequest(final ShowMessageRequestParams requestParams) {
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logMessage(final MessageParams message) {
|
||||
lastLogMessage = message;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user