summaryrefslogtreecommitdiffhomepage
path: root/packages/core/tests/lsp/client.test.ts
blob: 8daf8aba6ba1a93f00b98a2c5d98a0b0916ffba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { spawn } from "node:child_process";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { Diagnostic } from "vscode-languageserver-types";
import { createLspClient, type LspServerHandle } from "../../src/lsp/client.js";

const FIXTURE = join(dirname(fileURLToPath(import.meta.url)), "../fixture/lsp/fake-lsp-server.js");

function spawnFakeServer(): LspServerHandle {
	const proc = spawn(process.execPath, [FIXTURE], { stdio: "pipe" });
	return { process: proc as LspServerHandle["process"] };
}

const ERROR_DIAG: Diagnostic = {
	range: { start: { line: 0, character: 0 }, end: { line: 0, character: 5 } },
	severity: 1,
	message: "fake type error",
	source: "Fake",
};

describe("lsp/client (fake server)", () => {
	let workDir: string;

	beforeEach(async () => {
		workDir = await mkdtemp(join(tmpdir(), "dispatch-lsp-"));
	});
	afterEach(async () => {
		await rm(workDir, { recursive: true, force: true });
	});

	it("completes the initialize handshake and forwards initializationOptions", async () => {
		const handle = spawnFakeServer();
		handle.initialization = { "luau-lsp": { platform: { type: "roblox" } } };
		const client = await createLspClient({
			serverID: "fake",
			server: handle,
			root: workDir,
			directory: workDir,
		});

		const params = await client.connection.sendRequest<{ initializationOptions?: unknown }>(
			"test/get-initialize-params",
			{},
		);
		expect(params.initializationOptions).toEqual({
			"luau-lsp": { platform: { type: "roblox" } },
		});
		await client.shutdown();
	});

	it("opens a file and receives push diagnostics", async () => {
		const handle = spawnFakeServer();
		const client = await createLspClient({
			serverID: "fake",
			server: handle,
			root: workDir,
			directory: workDir,
		});

		const file = join(workDir, "a.luau");
		await writeFile(file, "local x = 1\n");
		const version = await client.notifyOpen(file);
		expect(version).toBe(0);

		// Drive a push from the fake server, then assert it lands in the map.
		await client.connection.sendRequest("test/publish-diagnostics", {
			uri: pathToFileURL(file).href,
			diagnostics: [ERROR_DIAG],
		});
		await new Promise((r) => setTimeout(r, 50));

		expect(client.diagnostics.get(file)?.[0]?.message).toBe("fake type error");
		await client.shutdown();
	});

	it("bumps the document version on re-open (didChange)", async () => {
		const handle = spawnFakeServer();
		const client = await createLspClient({
			serverID: "fake",
			server: handle,
			root: workDir,
			directory: workDir,
		});
		const file = join(workDir, "a.luau");
		await writeFile(file, "local x = 1\n");
		expect(await client.notifyOpen(file)).toBe(0);
		await writeFile(file, "local x = 2\n");
		expect(await client.notifyOpen(file)).toBe(1);

		const lastChange = await client.connection.sendRequest<{ textDocument?: { version?: number } }>(
			"test/get-last-change",
			{},
		);
		expect(lastChange?.textDocument?.version).toBe(1);
		await client.shutdown();
	});

	it("waits for pull diagnostics when the server advertises a diagnostic provider", async () => {
		const handle = spawnFakeServer();
		const client = await createLspClient({
			serverID: "fake",
			server: handle,
			root: workDir,
			directory: workDir,
		});
		// Tell the fake server (before initialize? no — it persists) to answer
		// pull requests. We configure AFTER connect; the static provider flag is
		// read at initialize, so this test exercises the dynamic registration
		// path instead.
		await client.connection.sendRequest("test/configure-pull-diagnostics", {
			registerOn: "didOpen",
			registrations: [{ id: "d1", registerOptions: { identifier: "fake" } }],
			documentDiagnostics: [ERROR_DIAG],
		});

		const file = join(workDir, "a.luau");
		await writeFile(file, "bad\n");
		const version = await client.notifyOpen(file);
		await client.waitForDiagnostics({ path: file, version, mode: "document" });

		expect(client.diagnostics.get(file)?.some((d) => d.message === "fake type error")).toBe(true);
		await client.shutdown();
	});

	it("request() passes through to the server (hover)", async () => {
		const handle = spawnFakeServer();
		const client = await createLspClient({
			serverID: "fake",
			server: handle,
			root: workDir,
			directory: workDir,
		});
		const file = join(workDir, "a.luau");
		await writeFile(file, "local x = 1\n");
		await client.notifyOpen(file);
		const hover = await client.request<{ contents?: { value?: string } }>("textDocument/hover", {
			textDocument: { uri: pathToFileURL(file).href },
			position: { line: 0, character: 6 },
		});
		expect(hover?.contents?.value).toBe("fake hover");
		await client.shutdown();
	});
});