summaryrefslogtreecommitdiffhomepage
path: root/packages/lsp/src/config.test.ts
blob: 6d51774538714154538830dc10af09176d621d0b (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { describe, expect, it } from "vitest";
import { resolveServers } from "./config.js";

describe("config", () => {
	it("built-in typescript resolves when tsconfig.json exists", async () => {
		const { servers } = await resolveServers({
			cwd: "/project",
			dispatchLspJson: null,
			opencodeJson: null,
			exists: async (path) => path === "/project/tsconfig.json",
		});

		const ts = servers.find((s) => s.id === "typescript");
		expect(ts).toBeDefined();
		expect(ts?.command).toEqual(["typescript-language-server", "--stdio"]);
		expect(ts?.extensions).toContain(".ts");
		expect(ts?.rootMarkers).toContain("tsconfig.json");
	});

	it(".dispatch/lsp.json servers resolve", async () => {
		const config = JSON.stringify({
			servers: {
				mylsp: {
					command: ["my-lsp", "--stdio"],
					extensions: [".ml"],
					rootMarkers: ["Makefile"],
				},
			},
		});

		const { servers } = await resolveServers({
			cwd: "/project",
			dispatchLspJson: config,
			opencodeJson: null,
			exists: async () => false,
		});

		expect(servers).toHaveLength(1);
		expect(servers[0]?.id).toBe("mylsp");
		expect(servers[0]?.command).toEqual(["my-lsp", "--stdio"]);
	});

	it("opencode.json lsp is used only as fallback", async () => {
		const opencodeConfig = JSON.stringify({
			lsp: {
				fallback: {
					command: ["fallback-lsp"],
					extensions: [".fb"],
				},
			},
		});

		const { servers } = await resolveServers({
			cwd: "/project",
			dispatchLspJson: null,
			opencodeJson: opencodeConfig,
			exists: async () => false,
		});

		expect(servers).toHaveLength(1);
		expect(servers[0]?.id).toBe("fallback");
	});

	it(".dispatch/lsp.json wins over opencode.json", async () => {
		const dispatchConfig = JSON.stringify({
			servers: { primary: { command: ["primary-lsp"], extensions: [".p"] } },
		});
		const opencodeConfig = JSON.stringify({
			lsp: { fallback: { command: ["fallback-lsp"], extensions: [".f"] } },
		});

		const { servers } = await resolveServers({
			cwd: "/project",
			dispatchLspJson: dispatchConfig,
			opencodeJson: opencodeConfig,
			exists: async () => false,
		});

		expect(servers).toHaveLength(1);
		expect(servers[0]?.id).toBe("primary");
	});

	it("luau-lsp sourcemap.autogenerate yields a rojo --watch sidecar", async () => {
		const config = JSON.stringify({
			servers: {
				luau: {
					command: ["luau-lsp", "lsp"],
					extensions: [".luau"],
					initialization: {
						"luau-lsp": {
							sourcemap: {
								autogenerate: true,
								rojoProjectFile: "default.project.json",
							},
						},
					},
				},
			},
		});

		const { servers } = await resolveServers({
			cwd: "/project",
			dispatchLspJson: config,
			opencodeJson: null,
			exists: async () => false,
		});

		expect(servers).toHaveLength(1);
		expect(servers[0]?.sidecar).toBeDefined();
		expect(servers[0]?.sidecar?.command).toEqual([
			"rojo",
			"sourcemap",
			"default.project.json",
			"--watch",
			"-o",
			"sourcemap.json",
		]);
	});

	it("config: resolveServers records configSource", async () => {
		// .dispatch/lsp.json entry → ".dispatch/lsp.json"
		const dispatch = JSON.stringify({
			servers: {
				mylsp: { command: ["my-lsp"], extensions: [".ml"] },
			},
		});
		const fromDispatch = await resolveServers({
			cwd: "/project",
			dispatchLspJson: dispatch,
			opencodeJson: null,
			exists: async () => false,
		});
		expect(fromDispatch.servers[0]?.configSource).toBe(".dispatch/lsp.json");

		// fallback to opencode.json → "opencode.json"
		const opencode = JSON.stringify({
			lsp: {
				oc: { command: ["oc-lsp"], extensions: [".oc"] },
			},
		});
		const fromOpencode = await resolveServers({
			cwd: "/project",
			dispatchLspJson: null,
			opencodeJson: opencode,
			exists: async () => false,
		});
		expect(fromOpencode.servers[0]?.configSource).toBe("opencode.json");

		// built-in TS → "built-in"
		const fromBuiltin = await resolveServers({
			cwd: "/project",
			dispatchLspJson: null,
			opencodeJson: null,
			exists: async () => false,
		});
		expect(fromBuiltin.servers[0]?.configSource).toBe("built-in");
	});

	it("config: shadowed flag is true when .dispatch/lsp.json shadows opencode.json lsp", async () => {
		const dispatch = JSON.stringify({
			servers: { primary: { command: ["primary-lsp"], extensions: [".p"] } },
		});
		const opencode = JSON.stringify({
			lsp: { fallback: { command: ["fallback-lsp"], extensions: [".f"] } },
		});

		const { shadowed, servers } = await resolveServers({
			cwd: "/project",
			dispatchLspJson: dispatch,
			opencodeJson: opencode,
			exists: async () => false,
		});

		// dispatch wins, opencode entry is shadowed
		expect(shadowed).toBe(true);
		expect(servers.map((s) => s.id)).toEqual(["primary"]);
	});

	it("config: shadowed flag is false when only one config source declares lsp", async () => {
		const dispatch = JSON.stringify({
			servers: { primary: { command: ["primary-lsp"], extensions: [".p"] } },
		});

		// dispatch present, opencode has NO lsp key → not shadowed
		const onlyDispatch = await resolveServers({
			cwd: "/project",
			dispatchLspJson: dispatch,
			opencodeJson: JSON.stringify({ lsp: {} }),
			exists: async () => false,
		});
		expect(onlyDispatch.shadowed).toBe(false);

		// dispatch absent, opencode has lsp → not shadowed (opencode is used)
		const onlyOpencode = await resolveServers({
			cwd: "/project",
			dispatchLspJson: null,
			opencodeJson: JSON.stringify({ lsp: { fb: { command: ["fb"] } } }),
			exists: async () => false,
		});
		expect(onlyOpencode.shadowed).toBe(false);
	});
});