pi-lens 3.1.2 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +55 -0
- package/README.md +16 -12
- package/clients/ast-grep-client.js +8 -1
- package/clients/ast-grep-client.ts +9 -1
- package/clients/biome-client.js +51 -38
- package/clients/biome-client.ts +60 -58
- package/clients/dependency-checker.js +30 -1
- package/clients/dependency-checker.ts +35 -1
- package/clients/dispatch/__tests__/runner-registration.test.ts +286 -282
- package/clients/dispatch/bus-dispatcher.js +15 -14
- package/clients/dispatch/bus-dispatcher.ts +32 -25
- package/clients/dispatch/dispatcher.js +18 -25
- package/clients/dispatch/dispatcher.test.ts +2 -1
- package/clients/dispatch/dispatcher.ts +17 -28
- package/clients/dispatch/plan.js +77 -32
- package/clients/dispatch/plan.ts +78 -32
- package/clients/dispatch/runners/ast-grep-napi.js +36 -376
- package/clients/dispatch/runners/ast-grep-napi.ts +60 -433
- package/clients/dispatch/runners/index.js +8 -4
- package/clients/dispatch/runners/index.ts +8 -4
- package/clients/dispatch/runners/lsp.js +65 -0
- package/clients/dispatch/runners/lsp.ts +125 -0
- package/clients/dispatch/runners/oxlint.js +2 -2
- package/clients/dispatch/runners/oxlint.ts +2 -2
- package/clients/dispatch/runners/pyright.js +24 -8
- package/clients/dispatch/runners/pyright.ts +28 -14
- package/clients/dispatch/runners/rust-clippy.js +2 -2
- package/clients/dispatch/runners/rust-clippy.ts +2 -4
- package/clients/dispatch/runners/tree-sitter.js +14 -2
- package/clients/dispatch/runners/tree-sitter.ts +15 -2
- package/clients/dispatch/runners/ts-lsp.js +3 -3
- package/clients/dispatch/runners/ts-lsp.ts +8 -5
- package/clients/dispatch/runners/yaml-rule-parser.js +292 -0
- package/clients/dispatch/runners/yaml-rule-parser.ts +338 -0
- package/clients/dispatch/types.js +3 -0
- package/clients/dispatch/types.ts +3 -0
- package/clients/formatters.js +67 -14
- package/clients/formatters.ts +68 -15
- package/clients/installer/index.js +78 -10
- package/clients/installer/index.ts +519 -426
- package/clients/jscpd-client.js +28 -0
- package/clients/jscpd-client.ts +41 -3
- package/clients/knip-client.js +30 -1
- package/clients/knip-client.ts +34 -2
- package/clients/lsp/__tests__/client.test.ts +64 -41
- package/clients/lsp/__tests__/config.test.ts +25 -17
- package/clients/lsp/__tests__/launch.test.ts +108 -43
- package/clients/lsp/__tests__/service.test.ts +76 -48
- package/clients/lsp/client.js +87 -2
- package/clients/lsp/client.ts +150 -6
- package/clients/lsp/config.js +8 -11
- package/clients/lsp/config.ts +24 -21
- package/clients/lsp/index.js +69 -0
- package/clients/lsp/index.ts +82 -0
- package/clients/lsp/interactive-install.js +19 -8
- package/clients/lsp/interactive-install.ts +52 -27
- package/clients/lsp/launch.js +182 -32
- package/clients/lsp/launch.ts +241 -38
- package/clients/lsp/path-utils.js +3 -46
- package/clients/lsp/path-utils.ts +11 -51
- package/clients/lsp/server.js +93 -71
- package/clients/lsp/server.ts +173 -131
- package/clients/path-utils.js +142 -0
- package/clients/path-utils.ts +153 -0
- package/clients/ruff-client.js +33 -4
- package/clients/ruff-client.ts +44 -13
- package/clients/safe-spawn.js +3 -1
- package/clients/safe-spawn.ts +3 -1
- package/clients/services/effect-integration.js +11 -7
- package/clients/services/effect-integration.ts +34 -26
- package/clients/sg-runner.js +51 -9
- package/clients/sg-runner.ts +58 -15
- package/clients/tree-sitter-client.js +12 -0
- package/clients/tree-sitter-client.ts +12 -0
- package/clients/typescript-client.js +6 -2
- package/clients/typescript-client.ts +9 -2
- package/commands/booboo.js +2 -4
- package/commands/booboo.ts +2 -4
- package/index.ts +377 -93
- package/package.json +2 -1
- package/rules/tree-sitter-queries/tsx/no-nested-links.yml +45 -0
- package/rules/tree-sitter-queries/typescript/constructor-super.yml +55 -0
- package/rules/tree-sitter-queries/typescript/debugger.yml +1 -1
- package/rules/tree-sitter-queries/typescript/no-dupe-class-members.yml +47 -0
- package/tsconfig.json +1 -1
- package/clients/__tests__/file-time.test.js +0 -216
- package/clients/__tests__/format-service.test.js +0 -245
- package/clients/__tests__/formatters.test.js +0 -271
- package/clients/agent-behavior-client.test.js +0 -94
- package/clients/ast-grep-client.test.js +0 -129
- package/clients/ast-grep-client.test.ts +0 -155
- package/clients/biome-client.test.js +0 -144
- package/clients/cache-manager.test.js +0 -197
- package/clients/complexity-client.test.js +0 -234
- package/clients/dependency-checker.test.js +0 -60
- package/clients/dispatch/__tests__/autofix-integration.test.js +0 -245
- package/clients/dispatch/__tests__/runner-registration.test.js +0 -236
- package/clients/dispatch/dispatcher.edge.test.js +0 -82
- package/clients/dispatch/dispatcher.format.test.js +0 -46
- package/clients/dispatch/dispatcher.inline.test.js +0 -74
- package/clients/dispatch/dispatcher.test.js +0 -115
- package/clients/dispatch/runners/architect.test.js +0 -138
- package/clients/dispatch/runners/ast-grep-napi.test.js +0 -106
- package/clients/dispatch/runners/oxlint.test.js +0 -230
- package/clients/dispatch/runners/pyright.test.js +0 -98
- package/clients/dispatch/runners/python-slop.test.js +0 -203
- package/clients/dispatch/runners/scan_codebase.test.js +0 -89
- package/clients/dispatch/runners/shellcheck.test.js +0 -98
- package/clients/dispatch/runners/spellcheck.test.js +0 -158
- package/clients/dispatch/runners/ts-slop.test.js +0 -180
- package/clients/dispatch/runners/ts-slop.test.ts +0 -230
- package/clients/dogfood.test.js +0 -201
- package/clients/file-kinds.test.js +0 -169
- package/clients/go-client.test.js +0 -127
- package/clients/jscpd-client.test.js +0 -127
- package/clients/knip-client.test.js +0 -112
- package/clients/lsp/__tests__/client.test.js +0 -325
- package/clients/lsp/__tests__/config.test.js +0 -166
- package/clients/lsp/__tests__/error-recovery.test.js +0 -213
- package/clients/lsp/__tests__/integration.test.js +0 -127
- package/clients/lsp/__tests__/launch.test.js +0 -260
- package/clients/lsp/__tests__/server.test.js +0 -259
- package/clients/lsp/__tests__/service.test.js +0 -417
- package/clients/metrics-client.test.js +0 -141
- package/clients/ruff-client.test.js +0 -132
- package/clients/rust-client.test.js +0 -108
- package/clients/sanitize.test.js +0 -177
- package/clients/secrets-scanner.test.js +0 -100
- package/clients/services/__tests__/effect-integration.test.js +0 -86
- package/clients/test-runner-client.test.js +0 -192
- package/clients/todo-scanner.test.js +0 -301
- package/clients/type-coverage-client.test.js +0 -105
- package/clients/typescript-client.codefix.test.js +0 -157
- package/clients/typescript-client.test.js +0 -105
- package/commands/clients/ast-grep-client.js +0 -250
- package/commands/clients/ast-grep-parser.js +0 -86
- package/commands/clients/ast-grep-rule-manager.js +0 -91
- package/commands/clients/ast-grep-types.js +0 -9
- package/commands/clients/biome-client.js +0 -380
- package/commands/clients/complexity-client.js +0 -667
- package/commands/clients/file-kinds.js +0 -177
- package/commands/clients/file-utils.js +0 -40
- package/commands/clients/jscpd-client.js +0 -169
- package/commands/clients/knip-client.js +0 -211
- package/commands/clients/ruff-client.js +0 -297
- package/commands/clients/safe-spawn.js +0 -88
- package/commands/clients/scan-utils.js +0 -83
- package/commands/clients/sg-runner.js +0 -190
- package/commands/clients/types.js +0 -11
- package/commands/clients/typescript-client.js +0 -505
- package/commands/rate.test.js +0 -119
- package/rules/ast-grep-rules/rules/no-dangerously-set-inner-html.yml +0 -13
- package/rules/ast-grep-rules/rules/no-debugger.yml +0 -12
- package/rules/ast-grep-rules/rules/no-eval.yml +0 -13
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* LSP Server Definitions Test Suite
|
|
3
|
-
*
|
|
4
|
-
* Tests for server definitions including:
|
|
5
|
-
* - Root detection with various project markers
|
|
6
|
-
* - Server matching by extension
|
|
7
|
-
* - Custom server creation
|
|
8
|
-
* - Server registry operations
|
|
9
|
-
*/
|
|
10
|
-
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
11
|
-
import { LSP_SERVERS, TypeScriptServer, PythonServer, GoServer, RustServer, getServerForExtension, getServerById, getServersForFile, createRootDetector, } from "../server.js";
|
|
12
|
-
import * as fs from "fs/promises";
|
|
13
|
-
// Mock fs/promises - need to mock the module before it's imported
|
|
14
|
-
vi.mock("fs/promises", async () => {
|
|
15
|
-
const actual = await vi.importActual("fs/promises");
|
|
16
|
-
return {
|
|
17
|
-
...actual,
|
|
18
|
-
stat: vi.fn(),
|
|
19
|
-
access: vi.fn(),
|
|
20
|
-
};
|
|
21
|
-
});
|
|
22
|
-
describe("createRootDetector", () => {
|
|
23
|
-
const mockStat = vi.mocked(fs.stat);
|
|
24
|
-
beforeEach(() => {
|
|
25
|
-
vi.clearAllMocks();
|
|
26
|
-
});
|
|
27
|
-
it("should return undefined when no markers exist", async () => {
|
|
28
|
-
// Mock all stat calls to throw (file not found)
|
|
29
|
-
mockStat.mockRejectedValue(new Error("ENOENT"));
|
|
30
|
-
const detector = createRootDetector(["package.json"]);
|
|
31
|
-
const root = await detector("/project/src/components/Button.tsx");
|
|
32
|
-
expect(root).toBeUndefined();
|
|
33
|
-
});
|
|
34
|
-
it("should return undefined for empty marker list", async () => {
|
|
35
|
-
const detector = createRootDetector([]);
|
|
36
|
-
const root = await detector("/project/file.ts");
|
|
37
|
-
expect(root).toBeUndefined();
|
|
38
|
-
});
|
|
39
|
-
it("should stop at filesystem root", async () => {
|
|
40
|
-
mockStat.mockRejectedValue(new Error("ENOENT"));
|
|
41
|
-
const detector = createRootDetector(["package.json"]);
|
|
42
|
-
const root = await detector("C:/file.ts");
|
|
43
|
-
// Should reach root and return undefined
|
|
44
|
-
expect(root).toBeUndefined();
|
|
45
|
-
});
|
|
46
|
-
it("should respect exclude patterns", async () => {
|
|
47
|
-
// First call finds node_modules (excluded)
|
|
48
|
-
mockStat.mockImplementation(async (filepath) => {
|
|
49
|
-
if (filepath.includes("node_modules")) {
|
|
50
|
-
return { isDirectory: () => true };
|
|
51
|
-
}
|
|
52
|
-
throw new Error("ENOENT");
|
|
53
|
-
});
|
|
54
|
-
const detector = createRootDetector(["package.json"], ["node_modules"]);
|
|
55
|
-
const root = await detector("/project/node_modules/lib/index.js");
|
|
56
|
-
expect(root).toBeUndefined();
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
describe("TypeScriptServer", () => {
|
|
60
|
-
const mockStat = vi.mocked(fs.stat);
|
|
61
|
-
beforeEach(() => {
|
|
62
|
-
vi.clearAllMocks();
|
|
63
|
-
});
|
|
64
|
-
it("should have correct ID and name", () => {
|
|
65
|
-
expect(TypeScriptServer.id).toBe("typescript");
|
|
66
|
-
expect(TypeScriptServer.name).toBe("TypeScript Language Server");
|
|
67
|
-
});
|
|
68
|
-
it("should match TypeScript extensions", () => {
|
|
69
|
-
expect(TypeScriptServer.extensions).toContain(".ts");
|
|
70
|
-
expect(TypeScriptServer.extensions).toContain(".tsx");
|
|
71
|
-
expect(TypeScriptServer.extensions).toContain(".js");
|
|
72
|
-
expect(TypeScriptServer.extensions).toContain(".jsx");
|
|
73
|
-
expect(TypeScriptServer.extensions).toContain(".mts");
|
|
74
|
-
expect(TypeScriptServer.extensions).toContain(".cts");
|
|
75
|
-
});
|
|
76
|
-
it("should return undefined when no project markers found", async () => {
|
|
77
|
-
mockStat.mockRejectedValue(new Error("ENOENT"));
|
|
78
|
-
const root = await TypeScriptServer.root("/project/src/index.ts");
|
|
79
|
-
expect(root).toBeUndefined();
|
|
80
|
-
});
|
|
81
|
-
it("should have spawn function", () => {
|
|
82
|
-
expect(typeof TypeScriptServer.spawn).toBe("function");
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
describe("PythonServer", () => {
|
|
86
|
-
const mockStat = vi.mocked(fs.stat);
|
|
87
|
-
beforeEach(() => {
|
|
88
|
-
vi.clearAllMocks();
|
|
89
|
-
});
|
|
90
|
-
it("should have correct ID and name", () => {
|
|
91
|
-
expect(PythonServer.id).toBe("python");
|
|
92
|
-
expect(PythonServer.name).toBe("Pyright Language Server");
|
|
93
|
-
});
|
|
94
|
-
it("should match Python extensions", () => {
|
|
95
|
-
expect(PythonServer.extensions).toContain(".py");
|
|
96
|
-
expect(PythonServer.extensions).toContain(".pyi");
|
|
97
|
-
});
|
|
98
|
-
it("should return undefined when no project markers found", async () => {
|
|
99
|
-
mockStat.mockRejectedValue(new Error("ENOENT"));
|
|
100
|
-
const root = await PythonServer.root("/project/src/main.py");
|
|
101
|
-
expect(root).toBeUndefined();
|
|
102
|
-
});
|
|
103
|
-
it("should have spawn function", () => {
|
|
104
|
-
expect(typeof PythonServer.spawn).toBe("function");
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
describe("GoServer", () => {
|
|
108
|
-
it("should have correct ID and name", () => {
|
|
109
|
-
expect(GoServer.id).toBe("go");
|
|
110
|
-
expect(GoServer.name).toBe("gopls");
|
|
111
|
-
});
|
|
112
|
-
it("should match .go extension", () => {
|
|
113
|
-
expect(GoServer.extensions).toContain(".go");
|
|
114
|
-
});
|
|
115
|
-
it("should have root detection function", () => {
|
|
116
|
-
expect(typeof GoServer.root).toBe("function");
|
|
117
|
-
});
|
|
118
|
-
it("should have spawn function", () => {
|
|
119
|
-
expect(typeof GoServer.spawn).toBe("function");
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
describe("RustServer", () => {
|
|
123
|
-
it("should have correct ID and name", () => {
|
|
124
|
-
expect(RustServer.id).toBe("rust");
|
|
125
|
-
expect(RustServer.name).toBe("rust-analyzer");
|
|
126
|
-
});
|
|
127
|
-
it("should match .rs extension", () => {
|
|
128
|
-
expect(RustServer.extensions).toContain(".rs");
|
|
129
|
-
});
|
|
130
|
-
it("should have root detection function", () => {
|
|
131
|
-
expect(typeof RustServer.root).toBe("function");
|
|
132
|
-
});
|
|
133
|
-
it("should have spawn function", () => {
|
|
134
|
-
expect(typeof RustServer.spawn).toBe("function");
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
describe("getServerForExtension", () => {
|
|
138
|
-
it("should return TypeScript server for .ts files", () => {
|
|
139
|
-
const server = getServerForExtension(".ts");
|
|
140
|
-
expect(server?.id).toBe("typescript");
|
|
141
|
-
});
|
|
142
|
-
it("should return Python server for .py files", () => {
|
|
143
|
-
const server = getServerForExtension(".py");
|
|
144
|
-
expect(server?.id).toBe("python");
|
|
145
|
-
});
|
|
146
|
-
it("should return Go server for .go files", () => {
|
|
147
|
-
const server = getServerForExtension(".go");
|
|
148
|
-
expect(server?.id).toBe("go");
|
|
149
|
-
});
|
|
150
|
-
it("should return Rust server for .rs files", () => {
|
|
151
|
-
const server = getServerForExtension(".rs");
|
|
152
|
-
expect(server?.id).toBe("rust");
|
|
153
|
-
});
|
|
154
|
-
it("should return undefined for unknown extensions", () => {
|
|
155
|
-
const server = getServerForExtension(".unknown");
|
|
156
|
-
expect(server).toBeUndefined();
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
describe("getServerById", () => {
|
|
160
|
-
it("should return server by ID", () => {
|
|
161
|
-
const server = getServerById("typescript");
|
|
162
|
-
expect(server?.name).toBe("TypeScript Language Server");
|
|
163
|
-
});
|
|
164
|
-
it("should return Python server by ID", () => {
|
|
165
|
-
const server = getServerById("python");
|
|
166
|
-
expect(server?.name).toBe("Pyright Language Server");
|
|
167
|
-
});
|
|
168
|
-
it("should return Go server by ID", () => {
|
|
169
|
-
const server = getServerById("go");
|
|
170
|
-
expect(server?.name).toBe("gopls");
|
|
171
|
-
});
|
|
172
|
-
it("should return Rust server by ID", () => {
|
|
173
|
-
const server = getServerById("rust");
|
|
174
|
-
expect(server?.name).toBe("rust-analyzer");
|
|
175
|
-
});
|
|
176
|
-
it("should return undefined for unknown ID", () => {
|
|
177
|
-
const server = getServerById("unknown");
|
|
178
|
-
expect(server).toBeUndefined();
|
|
179
|
-
});
|
|
180
|
-
});
|
|
181
|
-
describe("getServersForFile", () => {
|
|
182
|
-
it("should find servers for TypeScript file", () => {
|
|
183
|
-
const servers = getServersForFile("/project/src/index.ts");
|
|
184
|
-
expect(servers.some((s) => s.id === "typescript")).toBe(true);
|
|
185
|
-
});
|
|
186
|
-
it("should find servers for Python file", () => {
|
|
187
|
-
const servers = getServersForFile("/project/src/main.py");
|
|
188
|
-
expect(servers.some((s) => s.id === "python")).toBe(true);
|
|
189
|
-
});
|
|
190
|
-
it("should find servers for Go file", () => {
|
|
191
|
-
const servers = getServersForFile("/project/cmd/app/main.go");
|
|
192
|
-
expect(servers.some((s) => s.id === "go")).toBe(true);
|
|
193
|
-
});
|
|
194
|
-
it("should find servers for Rust file", () => {
|
|
195
|
-
const servers = getServersForFile("/project/src/main.rs");
|
|
196
|
-
expect(servers.some((s) => s.id === "rust")).toBe(true);
|
|
197
|
-
});
|
|
198
|
-
it("should return empty array for unknown file type", () => {
|
|
199
|
-
const servers = getServersForFile("/project/file.unknown");
|
|
200
|
-
expect(servers).toEqual([]);
|
|
201
|
-
});
|
|
202
|
-
it("should be case insensitive for extensions", () => {
|
|
203
|
-
const servers1 = getServersForFile("/project/file.TS");
|
|
204
|
-
const servers2 = getServersForFile("/project/file.ts");
|
|
205
|
-
expect(servers1.map((s) => s.id)).toEqual(servers2.map((s) => s.id));
|
|
206
|
-
});
|
|
207
|
-
});
|
|
208
|
-
describe("LSP_SERVERS registry", () => {
|
|
209
|
-
it("should contain all expected servers", () => {
|
|
210
|
-
const serverIds = LSP_SERVERS.map((s) => s.id);
|
|
211
|
-
expect(serverIds).toContain("typescript");
|
|
212
|
-
expect(serverIds).toContain("python");
|
|
213
|
-
expect(serverIds).toContain("go");
|
|
214
|
-
expect(serverIds).toContain("rust");
|
|
215
|
-
expect(serverIds).toContain("ruby");
|
|
216
|
-
expect(serverIds).toContain("php");
|
|
217
|
-
expect(serverIds).toContain("java");
|
|
218
|
-
expect(serverIds).toContain("cpp");
|
|
219
|
-
expect(serverIds).toContain("bash");
|
|
220
|
-
expect(serverIds).toContain("yaml");
|
|
221
|
-
expect(serverIds).toContain("json");
|
|
222
|
-
expect(serverIds).toContain("docker");
|
|
223
|
-
expect(serverIds).toContain("vue");
|
|
224
|
-
expect(serverIds).toContain("svelte");
|
|
225
|
-
});
|
|
226
|
-
it("should have unique server IDs", () => {
|
|
227
|
-
const ids = LSP_SERVERS.map((s) => s.id);
|
|
228
|
-
const uniqueIds = new Set(ids);
|
|
229
|
-
expect(uniqueIds.size).toBe(ids.length);
|
|
230
|
-
});
|
|
231
|
-
it("should have non-empty extensions for each server", () => {
|
|
232
|
-
for (const server of LSP_SERVERS) {
|
|
233
|
-
expect(server.extensions.length).toBeGreaterThan(0);
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
it("should have valid extensions for each server", () => {
|
|
237
|
-
for (const server of LSP_SERVERS) {
|
|
238
|
-
// All extensions should start with . or be special names like "Dockerfile"
|
|
239
|
-
const validExtensions = server.extensions.every((ext) => ext.startsWith(".") || ext === "Dockerfile");
|
|
240
|
-
expect(validExtensions).toBe(true);
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
it("should have spawn function for each server", () => {
|
|
244
|
-
for (const server of LSP_SERVERS) {
|
|
245
|
-
expect(typeof server.spawn).toBe("function");
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
it("should have root detection function for each server", () => {
|
|
249
|
-
for (const server of LSP_SERVERS) {
|
|
250
|
-
expect(typeof server.root).toBe("function");
|
|
251
|
-
}
|
|
252
|
-
});
|
|
253
|
-
it("should have name for each server", () => {
|
|
254
|
-
for (const server of LSP_SERVERS) {
|
|
255
|
-
expect(server.name).toBeTruthy();
|
|
256
|
-
expect(typeof server.name).toBe("string");
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
});
|
|
@@ -1,417 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* LSP Service Test Suite
|
|
3
|
-
*
|
|
4
|
-
* Tests for the LSP Service layer including:
|
|
5
|
-
* - Client lifecycle management
|
|
6
|
-
* - Effect-TS integration
|
|
7
|
-
* - File operations (open/update/getDiagnostics)
|
|
8
|
-
* - Server availability checking
|
|
9
|
-
* - Cleanup and shutdown
|
|
10
|
-
*/
|
|
11
|
-
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
12
|
-
import { Effect } from "effect";
|
|
13
|
-
import { LSPService, lspEffect, getLSPService, resetLSPService } from "../index.js";
|
|
14
|
-
// Mock the dependencies
|
|
15
|
-
vi.mock("../server.js", async () => {
|
|
16
|
-
const actual = await vi.importActual("../server.js");
|
|
17
|
-
return {
|
|
18
|
-
...actual,
|
|
19
|
-
getServersForFileWithConfig: vi.fn(),
|
|
20
|
-
};
|
|
21
|
-
});
|
|
22
|
-
vi.mock("../config.js", () => ({
|
|
23
|
-
initLSPConfig: vi.fn(),
|
|
24
|
-
getServersForFileWithConfig: vi.fn(),
|
|
25
|
-
}));
|
|
26
|
-
vi.mock("../language.js", () => ({
|
|
27
|
-
getLanguageId: vi.fn(() => "typescript"),
|
|
28
|
-
}));
|
|
29
|
-
vi.mock("../launch.js", () => ({
|
|
30
|
-
launchLSP: vi.fn(),
|
|
31
|
-
}));
|
|
32
|
-
vi.mock("../client.js", () => ({
|
|
33
|
-
createLSPClient: vi.fn(),
|
|
34
|
-
}));
|
|
35
|
-
import { getServersForFileWithConfig as mockGetServersForFile } from "../config.js";
|
|
36
|
-
import { createLSPClient as mockCreateLSPClient } from "../client.js";
|
|
37
|
-
describe("LSPService", () => {
|
|
38
|
-
let service;
|
|
39
|
-
beforeEach(() => {
|
|
40
|
-
resetLSPService();
|
|
41
|
-
service = getLSPService();
|
|
42
|
-
vi.clearAllMocks();
|
|
43
|
-
});
|
|
44
|
-
afterEach(async () => {
|
|
45
|
-
await service.shutdown();
|
|
46
|
-
resetLSPService();
|
|
47
|
-
});
|
|
48
|
-
describe("constructor", () => {
|
|
49
|
-
it("should initialize with empty state", () => {
|
|
50
|
-
const status = service.getStatus();
|
|
51
|
-
expect(status).toEqual([]);
|
|
52
|
-
});
|
|
53
|
-
it("should create fresh LSPState", () => {
|
|
54
|
-
// Verify service was created with initial state
|
|
55
|
-
expect(service).toBeDefined();
|
|
56
|
-
expect(service).toBeInstanceOf(LSPService);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
describe("hasLSP", () => {
|
|
60
|
-
it("should return false when no servers match file extension", async () => {
|
|
61
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([]);
|
|
62
|
-
const result = await service.hasLSP("/test/file.unknown");
|
|
63
|
-
expect(result).toBe(false);
|
|
64
|
-
});
|
|
65
|
-
it("should return true when server matches and provides root", async () => {
|
|
66
|
-
const mockServer = {
|
|
67
|
-
id: "typescript",
|
|
68
|
-
name: "TypeScript Server",
|
|
69
|
-
extensions: [".ts"],
|
|
70
|
-
root: vi.fn().mockResolvedValue("/project"),
|
|
71
|
-
spawn: vi.fn(),
|
|
72
|
-
};
|
|
73
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([mockServer]);
|
|
74
|
-
const result = await service.hasLSP("/project/test.ts");
|
|
75
|
-
expect(result).toBe(true);
|
|
76
|
-
});
|
|
77
|
-
it("should return false when server cannot determine root", async () => {
|
|
78
|
-
const mockServer = {
|
|
79
|
-
id: "typescript",
|
|
80
|
-
name: "TypeScript Server",
|
|
81
|
-
extensions: [".ts"],
|
|
82
|
-
root: vi.fn().mockResolvedValue(undefined),
|
|
83
|
-
spawn: vi.fn(),
|
|
84
|
-
};
|
|
85
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([mockServer]);
|
|
86
|
-
const result = await service.hasLSP("/project/test.ts");
|
|
87
|
-
expect(result).toBe(false);
|
|
88
|
-
});
|
|
89
|
-
it("should check all matching servers until root is found", async () => {
|
|
90
|
-
const server1 = {
|
|
91
|
-
id: "server1",
|
|
92
|
-
name: "Server 1",
|
|
93
|
-
extensions: [".ts"],
|
|
94
|
-
root: vi.fn().mockResolvedValue(undefined),
|
|
95
|
-
spawn: vi.fn(),
|
|
96
|
-
};
|
|
97
|
-
const server2 = {
|
|
98
|
-
id: "server2",
|
|
99
|
-
name: "Server 2",
|
|
100
|
-
extensions: [".ts"],
|
|
101
|
-
root: vi.fn().mockResolvedValue("/project"),
|
|
102
|
-
spawn: vi.fn(),
|
|
103
|
-
};
|
|
104
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([server1, server2]);
|
|
105
|
-
const result = await service.hasLSP("/project/test.ts");
|
|
106
|
-
expect(result).toBe(true);
|
|
107
|
-
expect(server1.root).toHaveBeenCalled();
|
|
108
|
-
expect(server2.root).toHaveBeenCalled();
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
describe("getClientForFile", () => {
|
|
112
|
-
it("should return undefined when no servers match", async () => {
|
|
113
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([]);
|
|
114
|
-
const result = await service.getClientForFile("/test/file.unknown");
|
|
115
|
-
expect(result).toBeUndefined();
|
|
116
|
-
});
|
|
117
|
-
it("should cache and reuse existing client", async () => {
|
|
118
|
-
const mockClient = createMockClient();
|
|
119
|
-
const mockServer = {
|
|
120
|
-
id: "typescript",
|
|
121
|
-
name: "TypeScript Server",
|
|
122
|
-
extensions: [".ts"],
|
|
123
|
-
root: vi.fn().mockResolvedValue("/project"),
|
|
124
|
-
spawn: vi.fn().mockResolvedValue({
|
|
125
|
-
process: { pid: 123 },
|
|
126
|
-
}),
|
|
127
|
-
};
|
|
128
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([mockServer]);
|
|
129
|
-
vi.mocked(mockCreateLSPClient).mockResolvedValue(mockClient);
|
|
130
|
-
// First call creates client
|
|
131
|
-
const result1 = await service.getClientForFile("/project/test.ts");
|
|
132
|
-
expect(result1).toBeDefined();
|
|
133
|
-
expect(mockCreateLSPClient).toHaveBeenCalledTimes(1);
|
|
134
|
-
// Second call returns cached client
|
|
135
|
-
const result2 = await service.getClientForFile("/project/other.ts");
|
|
136
|
-
expect(result2?.client).toBe(result1?.client);
|
|
137
|
-
expect(mockCreateLSPClient).toHaveBeenCalledTimes(1); // Not called again
|
|
138
|
-
});
|
|
139
|
-
it("should handle server spawn failure", async () => {
|
|
140
|
-
const mockSpawn = vi.fn().mockRejectedValue(new Error("Spawn failed"));
|
|
141
|
-
const mockServer = {
|
|
142
|
-
id: "typescript",
|
|
143
|
-
name: "TypeScript Server",
|
|
144
|
-
extensions: [".ts"],
|
|
145
|
-
root: vi.fn().mockResolvedValue("/project"),
|
|
146
|
-
spawn: mockSpawn,
|
|
147
|
-
};
|
|
148
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([mockServer]);
|
|
149
|
-
const result = await service.getClientForFile("/project/test.ts");
|
|
150
|
-
expect(result).toBeUndefined();
|
|
151
|
-
});
|
|
152
|
-
it("should try next server when first fails", async () => {
|
|
153
|
-
const mockClient = createMockClient();
|
|
154
|
-
const failingServer = {
|
|
155
|
-
id: "server1",
|
|
156
|
-
name: "Failing Server",
|
|
157
|
-
extensions: [".ts"],
|
|
158
|
-
root: vi.fn().mockResolvedValue("/project"),
|
|
159
|
-
spawn: vi.fn().mockRejectedValue(new Error("Spawn failed")),
|
|
160
|
-
};
|
|
161
|
-
const workingServer = {
|
|
162
|
-
id: "server2",
|
|
163
|
-
name: "Working Server",
|
|
164
|
-
extensions: [".ts"],
|
|
165
|
-
root: vi.fn().mockResolvedValue("/project"),
|
|
166
|
-
spawn: vi.fn().mockResolvedValue({
|
|
167
|
-
process: { pid: 456 },
|
|
168
|
-
}),
|
|
169
|
-
};
|
|
170
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([failingServer, workingServer]);
|
|
171
|
-
vi.mocked(mockCreateLSPClient).mockResolvedValue(mockClient);
|
|
172
|
-
const result = await service.getClientForFile("/project/test.ts");
|
|
173
|
-
expect(result).toBeDefined();
|
|
174
|
-
expect(result?.info.id).toBe("server2");
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
describe("openFile", () => {
|
|
178
|
-
it("should send didOpen notification with correct parameters", async () => {
|
|
179
|
-
const mockClient = createMockClient();
|
|
180
|
-
const mockServer = {
|
|
181
|
-
id: "typescript",
|
|
182
|
-
name: "TypeScript Server",
|
|
183
|
-
extensions: [".ts"],
|
|
184
|
-
root: vi.fn().mockResolvedValue("/project"),
|
|
185
|
-
spawn: vi.fn().mockResolvedValue({
|
|
186
|
-
process: { pid: 123 },
|
|
187
|
-
}),
|
|
188
|
-
};
|
|
189
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([mockServer]);
|
|
190
|
-
vi.mocked(mockCreateLSPClient).mockResolvedValue(mockClient);
|
|
191
|
-
await service.openFile("/project/test.ts", "const x = 1;");
|
|
192
|
-
expect(mockClient.notify.open).toHaveBeenCalledWith("/project/test.ts", "const x = 1;", "typescript");
|
|
193
|
-
});
|
|
194
|
-
it("should do nothing when no LSP available", async () => {
|
|
195
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([]);
|
|
196
|
-
// Should not throw
|
|
197
|
-
await expect(service.openFile("/test.unknown", "content"))
|
|
198
|
-
.resolves.not.toThrow();
|
|
199
|
-
});
|
|
200
|
-
});
|
|
201
|
-
describe("updateFile", () => {
|
|
202
|
-
it("should send didChange notification", async () => {
|
|
203
|
-
const mockClient = createMockClient();
|
|
204
|
-
const mockServer = {
|
|
205
|
-
id: "typescript",
|
|
206
|
-
name: "TypeScript Server",
|
|
207
|
-
extensions: [".ts"],
|
|
208
|
-
root: vi.fn().mockResolvedValue("/project"),
|
|
209
|
-
spawn: vi.fn().mockResolvedValue({
|
|
210
|
-
process: { pid: 123 },
|
|
211
|
-
}),
|
|
212
|
-
};
|
|
213
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([mockServer]);
|
|
214
|
-
vi.mocked(mockCreateLSPClient).mockResolvedValue(mockClient);
|
|
215
|
-
await service.updateFile("/project/test.ts", "const x = 2;");
|
|
216
|
-
expect(mockClient.notify.change).toHaveBeenCalledWith("/project/test.ts", "const x = 2;");
|
|
217
|
-
});
|
|
218
|
-
});
|
|
219
|
-
describe("getDiagnostics", () => {
|
|
220
|
-
it("should return diagnostics from client", async () => {
|
|
221
|
-
const mockDiagnostics = [
|
|
222
|
-
{
|
|
223
|
-
severity: 1,
|
|
224
|
-
message: "Type error",
|
|
225
|
-
range: {
|
|
226
|
-
start: { line: 0, character: 0 },
|
|
227
|
-
end: { line: 0, character: 5 },
|
|
228
|
-
},
|
|
229
|
-
},
|
|
230
|
-
];
|
|
231
|
-
const mockClient = createMockClient(mockDiagnostics);
|
|
232
|
-
const mockServer = {
|
|
233
|
-
id: "typescript",
|
|
234
|
-
name: "TypeScript Server",
|
|
235
|
-
extensions: [".ts"],
|
|
236
|
-
root: vi.fn().mockResolvedValue("/project"),
|
|
237
|
-
spawn: vi.fn().mockResolvedValue({
|
|
238
|
-
process: { pid: 123 },
|
|
239
|
-
}),
|
|
240
|
-
};
|
|
241
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([mockServer]);
|
|
242
|
-
vi.mocked(mockCreateLSPClient).mockResolvedValue(mockClient);
|
|
243
|
-
const result = await service.getDiagnostics("/project/test.ts");
|
|
244
|
-
expect(result).toEqual(mockDiagnostics);
|
|
245
|
-
expect(mockClient.waitForDiagnostics).toHaveBeenCalled();
|
|
246
|
-
});
|
|
247
|
-
it("should return empty array when no LSP available", async () => {
|
|
248
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([]);
|
|
249
|
-
const result = await service.getDiagnostics("/test.unknown");
|
|
250
|
-
expect(result).toEqual([]);
|
|
251
|
-
});
|
|
252
|
-
});
|
|
253
|
-
describe("shutdown", () => {
|
|
254
|
-
it("should shutdown all clients", async () => {
|
|
255
|
-
const mockClient1 = createMockClient();
|
|
256
|
-
const mockClient2 = createMockClient();
|
|
257
|
-
// Add clients to service
|
|
258
|
-
const mockServer1 = {
|
|
259
|
-
id: "typescript",
|
|
260
|
-
name: "TypeScript Server",
|
|
261
|
-
extensions: [".ts"],
|
|
262
|
-
root: vi.fn().mockResolvedValue("/project1"),
|
|
263
|
-
spawn: vi.fn().mockResolvedValue({ process: { pid: 1 } }),
|
|
264
|
-
};
|
|
265
|
-
const mockServer2 = {
|
|
266
|
-
id: "python",
|
|
267
|
-
name: "Python Server",
|
|
268
|
-
extensions: [".py"],
|
|
269
|
-
root: vi.fn().mockResolvedValue("/project2"),
|
|
270
|
-
spawn: vi.fn().mockResolvedValue({ process: { pid: 2 } }),
|
|
271
|
-
};
|
|
272
|
-
vi.mocked(mockGetServersForFile)
|
|
273
|
-
.mockReturnValueOnce([mockServer1])
|
|
274
|
-
.mockReturnValueOnce([mockServer2]);
|
|
275
|
-
vi.mocked(mockCreateLSPClient)
|
|
276
|
-
.mockResolvedValueOnce(mockClient1)
|
|
277
|
-
.mockResolvedValueOnce(mockClient2);
|
|
278
|
-
// Create clients
|
|
279
|
-
await service.getClientForFile("/project1/test.ts");
|
|
280
|
-
await service.getClientForFile("/project2/test.py");
|
|
281
|
-
await service.shutdown();
|
|
282
|
-
expect(mockClient1.shutdown).toHaveBeenCalled();
|
|
283
|
-
expect(mockClient2.shutdown).toHaveBeenCalled();
|
|
284
|
-
});
|
|
285
|
-
it("should handle shutdown errors gracefully", async () => {
|
|
286
|
-
const mockClient = createMockClient();
|
|
287
|
-
mockClient.shutdown.mockRejectedValue(new Error("Shutdown failed"));
|
|
288
|
-
const mockServer = {
|
|
289
|
-
id: "typescript",
|
|
290
|
-
name: "TypeScript Server",
|
|
291
|
-
extensions: [".ts"],
|
|
292
|
-
root: vi.fn().mockResolvedValue("/project"),
|
|
293
|
-
spawn: vi.fn().mockResolvedValue({ process: { pid: 123 } }),
|
|
294
|
-
};
|
|
295
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([mockServer]);
|
|
296
|
-
vi.mocked(mockCreateLSPClient).mockResolvedValue(mockClient);
|
|
297
|
-
await service.getClientForFile("/project/test.ts");
|
|
298
|
-
// Should not throw even when client shutdown fails
|
|
299
|
-
await expect(service.shutdown()).resolves.not.toThrow();
|
|
300
|
-
});
|
|
301
|
-
});
|
|
302
|
-
describe("getStatus", () => {
|
|
303
|
-
it("should return status of all active clients", async () => {
|
|
304
|
-
const mockClient = createMockClient();
|
|
305
|
-
const mockServer = {
|
|
306
|
-
id: "typescript",
|
|
307
|
-
name: "TypeScript Server",
|
|
308
|
-
extensions: [".ts"],
|
|
309
|
-
root: vi.fn().mockResolvedValue("/project"),
|
|
310
|
-
spawn: vi.fn().mockResolvedValue({ process: { pid: 123 } }),
|
|
311
|
-
};
|
|
312
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([mockServer]);
|
|
313
|
-
vi.mocked(mockCreateLSPClient).mockResolvedValue(mockClient);
|
|
314
|
-
await service.getClientForFile("/project/test.ts");
|
|
315
|
-
const status = service.getStatus();
|
|
316
|
-
expect(status).toHaveLength(1);
|
|
317
|
-
expect(status[0]).toEqual({
|
|
318
|
-
serverId: "typescript",
|
|
319
|
-
root: "/project",
|
|
320
|
-
connected: true,
|
|
321
|
-
});
|
|
322
|
-
});
|
|
323
|
-
});
|
|
324
|
-
});
|
|
325
|
-
describe("lspEffect", () => {
|
|
326
|
-
let service;
|
|
327
|
-
let effect;
|
|
328
|
-
beforeEach(() => {
|
|
329
|
-
resetLSPService();
|
|
330
|
-
service = getLSPService();
|
|
331
|
-
effect = lspEffect(service);
|
|
332
|
-
vi.clearAllMocks();
|
|
333
|
-
});
|
|
334
|
-
afterEach(async () => {
|
|
335
|
-
await service.shutdown();
|
|
336
|
-
resetLSPService();
|
|
337
|
-
});
|
|
338
|
-
describe("openFile Effect", () => {
|
|
339
|
-
it("should wrap openFile in Effect", async () => {
|
|
340
|
-
const mockClient = createMockClient();
|
|
341
|
-
const mockServer = {
|
|
342
|
-
id: "typescript",
|
|
343
|
-
name: "TypeScript Server",
|
|
344
|
-
extensions: [".ts"],
|
|
345
|
-
root: vi.fn().mockResolvedValue("/project"),
|
|
346
|
-
spawn: vi.fn().mockResolvedValue({ process: { pid: 123 } }),
|
|
347
|
-
};
|
|
348
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([mockServer]);
|
|
349
|
-
vi.mocked(mockCreateLSPClient).mockResolvedValue(mockClient);
|
|
350
|
-
const program = effect.openFile("/project/test.ts", "content");
|
|
351
|
-
const result = await Effect.runPromise(program);
|
|
352
|
-
expect(result).toBeUndefined();
|
|
353
|
-
expect(mockClient.notify.open).toHaveBeenCalled();
|
|
354
|
-
});
|
|
355
|
-
it("should handle errors in Effect", async () => {
|
|
356
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([]);
|
|
357
|
-
const program = effect.openFile("/test.unknown", "content");
|
|
358
|
-
// Should complete successfully (no-op for unknown files)
|
|
359
|
-
await expect(Effect.runPromise(program)).resolves.not.toThrow();
|
|
360
|
-
});
|
|
361
|
-
});
|
|
362
|
-
describe("getDiagnostics Effect", () => {
|
|
363
|
-
it("should wrap getDiagnostics in Effect", async () => {
|
|
364
|
-
const mockDiagnostics = [{ severity: 1, message: "Error", range: { start: { line: 0, character: 0 }, end: { line: 0, character: 1 } } }];
|
|
365
|
-
const mockClient = createMockClient(mockDiagnostics);
|
|
366
|
-
const mockServer = {
|
|
367
|
-
id: "typescript",
|
|
368
|
-
name: "TypeScript Server",
|
|
369
|
-
extensions: [".ts"],
|
|
370
|
-
root: vi.fn().mockResolvedValue("/project"),
|
|
371
|
-
spawn: vi.fn().mockResolvedValue({ process: { pid: 123 } }),
|
|
372
|
-
};
|
|
373
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([mockServer]);
|
|
374
|
-
vi.mocked(mockCreateLSPClient).mockResolvedValue(mockClient);
|
|
375
|
-
const program = effect.getDiagnostics("/project/test.ts");
|
|
376
|
-
const result = await Effect.runPromise(program);
|
|
377
|
-
expect(result).toEqual(mockDiagnostics);
|
|
378
|
-
});
|
|
379
|
-
});
|
|
380
|
-
describe("hasLSP Effect", () => {
|
|
381
|
-
it("should wrap hasLSP in Effect", async () => {
|
|
382
|
-
const mockServer = {
|
|
383
|
-
id: "typescript",
|
|
384
|
-
name: "TypeScript Server",
|
|
385
|
-
extensions: [".ts"],
|
|
386
|
-
root: vi.fn().mockResolvedValue("/project"),
|
|
387
|
-
spawn: vi.fn(),
|
|
388
|
-
};
|
|
389
|
-
vi.mocked(mockGetServersForFile).mockReturnValue([mockServer]);
|
|
390
|
-
const program = effect.hasLSP("/project/test.ts");
|
|
391
|
-
const result = await Effect.runPromise(program);
|
|
392
|
-
expect(result).toBe(true);
|
|
393
|
-
});
|
|
394
|
-
});
|
|
395
|
-
describe("shutdown Effect", () => {
|
|
396
|
-
it("should wrap shutdown in Effect", async () => {
|
|
397
|
-
const program = effect.shutdown();
|
|
398
|
-
const result = await Effect.runPromise(program);
|
|
399
|
-
expect(result).toBeUndefined();
|
|
400
|
-
});
|
|
401
|
-
});
|
|
402
|
-
});
|
|
403
|
-
// Helper function to create mock client
|
|
404
|
-
function createMockClient(diagnostics = []) {
|
|
405
|
-
return {
|
|
406
|
-
serverId: "test-server",
|
|
407
|
-
root: "/test",
|
|
408
|
-
connection: {},
|
|
409
|
-
notify: {
|
|
410
|
-
open: vi.fn().mockResolvedValue(undefined),
|
|
411
|
-
change: vi.fn().mockResolvedValue(undefined),
|
|
412
|
-
},
|
|
413
|
-
getDiagnostics: vi.fn().mockReturnValue(diagnostics),
|
|
414
|
-
waitForDiagnostics: vi.fn().mockResolvedValue(undefined),
|
|
415
|
-
shutdown: vi.fn().mockResolvedValue(undefined),
|
|
416
|
-
};
|
|
417
|
-
}
|