Repository navigation
test(lsp): add initialization tests for installed servers #2515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| import { | ||
| checkRuntimeServerInstallation, | ||
| createTransport, | ||
| selectRuntimeProvider, | ||
| } from "cm/lsp"; | ||
| import lspApi from "cm/lsp/api"; | ||
| import { TestRunner } from "./tester"; | ||
|
|
||
| const ROOT_URI = "file:///tmp"; | ||
| const DEFAULT_TIMEOUT = 10000; | ||
| const CLEANUP_GRACE_PERIOD = 5000; | ||
|
|
||
| function timeoutFor(server) { | ||
| const timeout = Number(server.startupTimeout); | ||
| return Number.isFinite(timeout) && timeout > 0 ? timeout : DEFAULT_TIMEOUT; | ||
| } | ||
|
|
||
| function withTimeout(promise, timeout, message) { | ||
| return new Promise((resolve, reject) => { | ||
| const timer = setTimeout(() => reject(new Error(message)), timeout); | ||
| Promise.resolve(promise).then( | ||
| (value) => { | ||
| clearTimeout(timer); | ||
| resolve(value); | ||
| }, | ||
| (error) => { | ||
| clearTimeout(timer); | ||
| reject(error); | ||
| }, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| function initializationRequest(id = 1) { | ||
| return { | ||
| jsonrpc: "2.0", | ||
| id, | ||
| method: "initialize", | ||
| params: { | ||
| processId: null, | ||
| clientInfo: { | ||
| name: "Acode", | ||
| version: "1.0", | ||
| }, | ||
| rootUri: ROOT_URI, | ||
| capabilities: {}, | ||
| workspaceFolders: null, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function isObject(value) { | ||
| return value !== null && typeof value === "object" && !Array.isArray(value); | ||
| } | ||
|
|
||
| export function isValidInitializationResponse(response, id = 1) { | ||
| return ( | ||
| isObject(response) && | ||
| response.jsonrpc === "2.0" && | ||
| response.id === id && | ||
| isObject(response.result) && | ||
| isObject(response.result.capabilities) | ||
| ); | ||
| } | ||
|
|
||
| function waitForInitialization(transport, server, timeout) { | ||
| const id = 1; | ||
| return new Promise((resolve, reject) => { | ||
| let settled = false; | ||
| const finish = (error, response) => { | ||
| if (settled) return; | ||
| settled = true; | ||
| clearTimeout(timer); | ||
| transport.unsubscribe?.(onMessage); | ||
| if (error) reject(error); | ||
| else resolve(response); | ||
| }; | ||
| const onMessage = (data) => { | ||
| let payload; | ||
| try { | ||
| payload = typeof data === "string" ? JSON.parse(data) : data; | ||
| } catch { | ||
| return; | ||
| } | ||
| const messages = Array.isArray(payload) ? payload : [payload]; | ||
| const response = messages.find((message) => message?.id === id); | ||
| if (!response) return; | ||
| if (response.error) { | ||
| const detail = response.error.message || JSON.stringify(response.error); | ||
| finish(new Error(`${server.label} rejected initialize: ${detail}`)); | ||
| return; | ||
| } | ||
| if (!isValidInitializationResponse(response, id)) { | ||
| finish( | ||
| new Error(`${server.label} returned an invalid initialize result`), | ||
| ); | ||
| return; | ||
| } | ||
| finish(null, response); | ||
| }; | ||
| const timer = setTimeout(() => { | ||
| finish( | ||
| new Error( | ||
| `${server.label} did not respond to initialize within ${timeout}ms`, | ||
| ), | ||
| ); | ||
| }, timeout); | ||
|
|
||
| transport.subscribe(onMessage); | ||
| try { | ||
| transport.send(JSON.stringify(initializationRequest(id))); | ||
| } catch (error) { | ||
| finish(error); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function createRuntimeContext(server) { | ||
| return { | ||
| uri: ROOT_URI, | ||
| documentUri: ROOT_URI, | ||
| originalDocumentUri: ROOT_URI, | ||
| rootUri: ROOT_URI, | ||
| originalRootUri: ROOT_URI, | ||
| serverId: server.id, | ||
| workspaceKind: "app-private", | ||
| allowNonTerminalWorkspace: true, | ||
| }; | ||
| } | ||
|
|
||
| function transportFromConnection(server, context, connection) { | ||
| if (connection.kind === "transport") { | ||
| return connection.transport; | ||
| } | ||
|
|
||
| return createTransport( | ||
| { | ||
| ...server, | ||
| transport: { | ||
| ...server.transport, | ||
| kind: "websocket", | ||
| url: connection.url, | ||
| protocols: connection.protocols, | ||
| }, | ||
| }, | ||
| context, | ||
| ); | ||
| } | ||
|
|
||
| async function cleanup(provider, connection, transportHandle) { | ||
| const errors = []; | ||
| try { | ||
| await transportHandle?.dispose?.(); | ||
| } catch (error) { | ||
| errors.push(error); | ||
| } | ||
| try { | ||
| await connection?.dispose?.(); | ||
| } catch (error) { | ||
| errors.push(error); | ||
| } | ||
| try { | ||
| if (connection) await provider?.stop?.(connection); | ||
| } catch (error) { | ||
| errors.push(error); | ||
| } | ||
| if (errors.length) { | ||
| console.warn("Failed to completely clean up LSP test connection", errors); | ||
| } | ||
| } | ||
|
|
||
| async function testServer(server, test) { | ||
| const timeout = timeoutFor(server); | ||
| const context = createRuntimeContext(server); | ||
| const installation = await withTimeout( | ||
| checkRuntimeServerInstallation(server, context), | ||
| timeout, | ||
| `Timed out checking whether ${server.label} is installed`, | ||
| ); | ||
|
|
||
| if (installation.status === "missing") { | ||
| return test.skip(`${server.label} is not installed`); | ||
| } | ||
|
|
||
| const provider = await withTimeout( | ||
| selectRuntimeProvider(server, context), | ||
| timeout, | ||
| `Timed out selecting a runtime for ${server.label}`, | ||
| ); | ||
| if (!provider) { | ||
| throw new Error(`No runtime can start ${server.label}`); | ||
| } | ||
|
|
||
| let connection; | ||
| let transportHandle; | ||
| try { | ||
| connection = await withTimeout( | ||
| provider.start(server, context), | ||
| timeout, | ||
| `${server.label} did not start within ${timeout}ms`, | ||
| ); | ||
| transportHandle = transportFromConnection(server, context, connection); | ||
| await withTimeout( | ||
| transportHandle.ready, | ||
| timeout, | ||
| `${server.label} transport was not ready within ${timeout}ms`, | ||
| ); | ||
| await waitForInitialization(transportHandle.transport, server, timeout); | ||
| } finally { | ||
| await cleanup(provider, connection, transportHandle); | ||
| } | ||
| } | ||
|
|
||
| export async function runLspTests(writeOutput) { | ||
| const runner = new TestRunner("LSP Server Tests"); | ||
| const servers = lspApi.servers.list(); | ||
|
|
||
| for (const server of servers) { | ||
| const timeout = timeoutFor(server); | ||
| runner.test( | ||
| `${server.label} (${server.id}) initializes`, | ||
| (test) => testServer(server, test), | ||
| { | ||
| timeout: timeout * 5 + CLEANUP_GRACE_PERIOD, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| return await runner.run(writeOutput); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.