@nexiq/cli 1.0.1 → 1.0.6
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 +19 -0
- package/dist/cli.js +320 -0
- package/dist/devtools-3T2XQNYQ.js +5 -0
- package/package.json +25 -37
- package/src/cli.tsx +107 -0
- package/src/components/CacheApp.tsx +187 -0
- package/src/components/HelpApp.tsx +90 -0
- package/src/components/StartApp.tsx +125 -0
- package/src/components/StatusApp.tsx +116 -0
- package/src/components/StopApp.tsx +90 -0
- package/src/components/ui.tsx +128 -0
- package/src/server-process.ts +392 -0
- package/src/ws.d.ts +9 -0
- package/tsconfig.json +6 -13
- package/tsup.config.ts +45 -4
- package/dist/index.d.ts +0 -365
- package/dist/index.js +0 -2438
- package/eslint.config.js +0 -33
- package/src/index.ts +0 -32
- package/src/projectManager.test.ts +0 -897
- package/src/projectManager.ts +0 -1550
- package/src/server.test.ts +0 -782
- package/src/server.ts +0 -1710
- package/src/token-optimization.test.ts +0 -198
- package/src/tools.test.ts +0 -309
- package/vitest.config.ts +0 -36
package/src/server.test.ts
DELETED
|
@@ -1,782 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
-
import { BackendServer, type SymbolInfoResult } from "./server.js";
|
|
3
|
-
import { ProjectManager } from "./projectManager.js";
|
|
4
|
-
import { WebSocketServer } from "ws";
|
|
5
|
-
import {
|
|
6
|
-
CallToolRequestSchema,
|
|
7
|
-
ListToolsRequestSchema,
|
|
8
|
-
} from "@modelcontextprotocol/sdk/types.js";
|
|
9
|
-
import type { JsonData } from "@nexiq/shared";
|
|
10
|
-
import type { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
11
|
-
|
|
12
|
-
import type { Extension } from "@nexiq/extension-sdk";
|
|
13
|
-
|
|
14
|
-
vi.mock("ws");
|
|
15
|
-
vi.mock("@modelcontextprotocol/sdk/server/index.js");
|
|
16
|
-
vi.mock("@modelcontextprotocol/sdk/server/stdio.js");
|
|
17
|
-
vi.mock("./projectManager.js");
|
|
18
|
-
|
|
19
|
-
describe("BackendServer", () => {
|
|
20
|
-
let server: BackendServer;
|
|
21
|
-
let mockProjectManager: ProjectManager;
|
|
22
|
-
|
|
23
|
-
beforeEach(() => {
|
|
24
|
-
vi.clearAllMocks();
|
|
25
|
-
vi.spyOn(process, "exit").mockImplementation(() => undefined as never);
|
|
26
|
-
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
27
|
-
mockProjectManager = new ProjectManager();
|
|
28
|
-
vi.mocked(mockProjectManager.openProject).mockResolvedValue({
|
|
29
|
-
projectPath: "/test",
|
|
30
|
-
sqlitePath: "/test/db.sqlite",
|
|
31
|
-
extensions: [],
|
|
32
|
-
} as any);
|
|
33
|
-
vi.mocked(mockProjectManager.getDatabaseData).mockResolvedValue({
|
|
34
|
-
files: [],
|
|
35
|
-
entities: [],
|
|
36
|
-
scopes: [],
|
|
37
|
-
symbols: [],
|
|
38
|
-
renders: [],
|
|
39
|
-
exports: [],
|
|
40
|
-
relations: [],
|
|
41
|
-
});
|
|
42
|
-
vi.mocked(mockProjectManager.getAllExtensions).mockReturnValue([]);
|
|
43
|
-
server = new BackendServer(mockProjectManager, 3030);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
afterEach(() => {
|
|
47
|
-
vi.restoreAllMocks();
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
describe("MCP Tool Handlers", () => {
|
|
51
|
-
it("should handle open_project tool", async () => {
|
|
52
|
-
const args = { projectPath: "/test" };
|
|
53
|
-
const result = await server.handleCallTool({
|
|
54
|
-
name: "open_project",
|
|
55
|
-
args: args,
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
expect(mockProjectManager.openProject).toHaveBeenCalledWith(
|
|
59
|
-
"/test",
|
|
60
|
-
undefined,
|
|
61
|
-
);
|
|
62
|
-
expect(result).toEqual({
|
|
63
|
-
content: [
|
|
64
|
-
{ type: "text", text: expect.stringContaining("successfully") },
|
|
65
|
-
],
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it("should handle get_symbol_info tool", async () => {
|
|
70
|
-
const mockResult = {
|
|
71
|
-
definitions: [
|
|
72
|
-
{
|
|
73
|
-
type: "definition" as const,
|
|
74
|
-
kind: "component",
|
|
75
|
-
name: "App",
|
|
76
|
-
file: "src/App.tsx",
|
|
77
|
-
loc: { line: 1, column: 1 },
|
|
78
|
-
usages: [],
|
|
79
|
-
},
|
|
80
|
-
],
|
|
81
|
-
externalUsages: [],
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
vi.mocked(mockProjectManager.findSymbol).mockResolvedValue(mockResult);
|
|
85
|
-
|
|
86
|
-
const args = { projectPath: "/test", query: "App", usages: true };
|
|
87
|
-
const result = await server.handleCallTool({
|
|
88
|
-
name: "get_symbol_info",
|
|
89
|
-
args: args,
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
expect(mockProjectManager.findSymbol).toHaveBeenCalledWith(
|
|
93
|
-
"/test",
|
|
94
|
-
"App",
|
|
95
|
-
undefined,
|
|
96
|
-
true,
|
|
97
|
-
false,
|
|
98
|
-
true,
|
|
99
|
-
expect.arrayContaining(["**/node_modules/**"]),
|
|
100
|
-
);
|
|
101
|
-
|
|
102
|
-
const content = JSON.parse(
|
|
103
|
-
(result as { content: { text: string }[] }).content[0].text,
|
|
104
|
-
) as {
|
|
105
|
-
definitions: (SymbolInfoResult & { usages?: SymbolInfoResult[] })[];
|
|
106
|
-
externalUsages: SymbolInfoResult[];
|
|
107
|
-
};
|
|
108
|
-
expect(content.definitions).toHaveLength(1);
|
|
109
|
-
expect(content.definitions[0].name).toBe("App");
|
|
110
|
-
expect(content.definitions[0].type).toBe("definition");
|
|
111
|
-
expect(content.definitions[0].usages).toHaveLength(0);
|
|
112
|
-
expect(content.externalUsages).toHaveLength(0);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it("should handle get_symbol_info tool with loc: false", async () => {
|
|
116
|
-
const mockResult = {
|
|
117
|
-
definitions: [
|
|
118
|
-
{
|
|
119
|
-
type: "definition" as const,
|
|
120
|
-
kind: "component",
|
|
121
|
-
name: "App",
|
|
122
|
-
file: "src/App.tsx",
|
|
123
|
-
loc: { line: 1, column: 1 },
|
|
124
|
-
usages: [],
|
|
125
|
-
},
|
|
126
|
-
],
|
|
127
|
-
externalUsages: [],
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
vi.mocked(mockProjectManager.findSymbol).mockResolvedValue(mockResult);
|
|
131
|
-
|
|
132
|
-
const args = { projectPath: "/test", query: "App", loc: false };
|
|
133
|
-
const result = await server.handleCallTool({
|
|
134
|
-
name: "get_symbol_info",
|
|
135
|
-
args: args,
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
expect(mockProjectManager.findSymbol).toHaveBeenCalledWith(
|
|
139
|
-
"/test",
|
|
140
|
-
"App",
|
|
141
|
-
undefined,
|
|
142
|
-
true,
|
|
143
|
-
false,
|
|
144
|
-
false,
|
|
145
|
-
expect.any(Array),
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
const content = JSON.parse(
|
|
149
|
-
(result as { content: { text: string }[] }).content[0].text,
|
|
150
|
-
) as {
|
|
151
|
-
definitions: (SymbolInfoResult & { usages?: SymbolInfoResult[] })[];
|
|
152
|
-
externalUsages: SymbolInfoResult[];
|
|
153
|
-
};
|
|
154
|
-
expect(content.definitions[0].loc).toBeUndefined();
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it("should handle get_symbol_info tool with nested and external usages", async () => {
|
|
158
|
-
const mockResult = {
|
|
159
|
-
definitions: [
|
|
160
|
-
{
|
|
161
|
-
kind: "component",
|
|
162
|
-
name: "Button",
|
|
163
|
-
file: "src/components/Button.tsx",
|
|
164
|
-
loc: { line: 1, column: 1 },
|
|
165
|
-
usages: [
|
|
166
|
-
{
|
|
167
|
-
kind: "render",
|
|
168
|
-
name: "Button",
|
|
169
|
-
file: "src/App.tsx",
|
|
170
|
-
loc: { line: 10, column: 5 },
|
|
171
|
-
in: "App",
|
|
172
|
-
},
|
|
173
|
-
],
|
|
174
|
-
},
|
|
175
|
-
],
|
|
176
|
-
externalUsages: [
|
|
177
|
-
{
|
|
178
|
-
kind: "render",
|
|
179
|
-
name: "ExternalComponent",
|
|
180
|
-
file: "src/External.tsx",
|
|
181
|
-
loc: { line: 5, column: 1 },
|
|
182
|
-
in: "Unknown",
|
|
183
|
-
},
|
|
184
|
-
],
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
vi.mocked(mockProjectManager.findSymbol).mockResolvedValue(mockResult);
|
|
188
|
-
|
|
189
|
-
const args = { projectPath: "/test", query: "Button", usages: true };
|
|
190
|
-
const result = await server.handleCallTool({
|
|
191
|
-
name: "get_symbol_info",
|
|
192
|
-
args: args,
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
expect(mockProjectManager.findSymbol).toHaveBeenCalledWith(
|
|
196
|
-
"/test",
|
|
197
|
-
"Button",
|
|
198
|
-
undefined,
|
|
199
|
-
true,
|
|
200
|
-
false,
|
|
201
|
-
true,
|
|
202
|
-
expect.any(Array),
|
|
203
|
-
);
|
|
204
|
-
|
|
205
|
-
const content = JSON.parse(
|
|
206
|
-
(result as { content: { text: string }[] }).content[0].text,
|
|
207
|
-
) as {
|
|
208
|
-
definitions: (SymbolInfoResult & { usages?: SymbolInfoResult[] })[];
|
|
209
|
-
externalUsages: SymbolInfoResult[];
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
expect(content.definitions).toHaveLength(1);
|
|
213
|
-
expect(content.definitions[0].usages).toHaveLength(1);
|
|
214
|
-
expect(content.definitions[0].usages![0].name).toBe("Button");
|
|
215
|
-
expect(content.externalUsages).toHaveLength(1);
|
|
216
|
-
expect(content.externalUsages[0].name).toBe("ExternalComponent");
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
it("should handle list_files tool", async () => {
|
|
220
|
-
const mockGraph = {
|
|
221
|
-
files: {
|
|
222
|
-
"src/index.ts": {
|
|
223
|
-
var: {
|
|
224
|
-
v1: {
|
|
225
|
-
id: "v1",
|
|
226
|
-
name: { type: "identifier" as const, name: "main" },
|
|
227
|
-
kind: "normal" as const,
|
|
228
|
-
loc: { line: 1, column: 1 },
|
|
229
|
-
},
|
|
230
|
-
},
|
|
231
|
-
},
|
|
232
|
-
},
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
vi.mocked(mockProjectManager.openProject).mockResolvedValue({
|
|
236
|
-
projectPath: "/test",
|
|
237
|
-
extensions: [],
|
|
238
|
-
sqlitePath: "test.sqlite",
|
|
239
|
-
graph: mockGraph as unknown as JsonData,
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
const args = { projectPath: "/test" };
|
|
243
|
-
const result = await server.handleCallTool({
|
|
244
|
-
name: "list_files",
|
|
245
|
-
args: args,
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
const content = JSON.parse(
|
|
249
|
-
(result as { content: { text: string }[] }).content[0].text,
|
|
250
|
-
) as { totalFiles: number; files: { path: string }[] };
|
|
251
|
-
expect(content.totalFiles).toBe(1);
|
|
252
|
-
expect(content.files).toHaveLength(1);
|
|
253
|
-
expect(content.files[0].path).toBe("src/index.ts");
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
it("should handle read_file tool", async () => {
|
|
257
|
-
vi.mocked(mockProjectManager.readFile).mockResolvedValue("file content");
|
|
258
|
-
const args = { projectPath: "/test", filePath: "src/App.tsx" };
|
|
259
|
-
const result = await server.handleCallTool({
|
|
260
|
-
name: "read_file",
|
|
261
|
-
args: args,
|
|
262
|
-
});
|
|
263
|
-
expect((result as { content: { text: string }[] }).content[0].text).toBe(
|
|
264
|
-
"file content",
|
|
265
|
-
);
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
it("should handle grep_search tool", async () => {
|
|
269
|
-
const mockResult = [{ file: "src/App.tsx", line: 1, content: "import" }];
|
|
270
|
-
vi.mocked(mockProjectManager.grepSearch).mockResolvedValue(mockResult);
|
|
271
|
-
const args = { projectPath: "/test", pattern: "import" };
|
|
272
|
-
const result = await server.handleCallTool({
|
|
273
|
-
name: "grep_search",
|
|
274
|
-
args: args,
|
|
275
|
-
});
|
|
276
|
-
expect(
|
|
277
|
-
JSON.parse((result as { content: { text: string }[] }).content[0].text),
|
|
278
|
-
).toEqual(mockResult);
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
it("should open project automatically for list_files tool", async () => {
|
|
282
|
-
const mockGraph = { files: {} };
|
|
283
|
-
vi.mocked(mockProjectManager.openProject).mockResolvedValue({
|
|
284
|
-
projectPath: "/p",
|
|
285
|
-
extensions: [],
|
|
286
|
-
sqlitePath: "test.sqlite",
|
|
287
|
-
graph: mockGraph as unknown as JsonData,
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
const result = await server.handleCallTool({
|
|
291
|
-
name: "list_files",
|
|
292
|
-
args: {
|
|
293
|
-
projectPath: "/p",
|
|
294
|
-
},
|
|
295
|
-
});
|
|
296
|
-
expect(result).toBeDefined();
|
|
297
|
-
expect(mockProjectManager.openProject).toHaveBeenCalledWith(
|
|
298
|
-
"/p",
|
|
299
|
-
undefined,
|
|
300
|
-
);
|
|
301
|
-
});
|
|
302
|
-
|
|
303
|
-
it("should handle labeling tools", async () => {
|
|
304
|
-
vi.mocked(mockProjectManager.addLabel).mockResolvedValue(["tag1"]);
|
|
305
|
-
const addResult = await server.handleCallTool({
|
|
306
|
-
name: "add_label",
|
|
307
|
-
args: {
|
|
308
|
-
projectPath: "/p",
|
|
309
|
-
id: "id1",
|
|
310
|
-
label: "tag1",
|
|
311
|
-
},
|
|
312
|
-
});
|
|
313
|
-
expect(
|
|
314
|
-
JSON.parse(
|
|
315
|
-
(addResult as { content: { text: string }[] }).content[0].text,
|
|
316
|
-
),
|
|
317
|
-
).toEqual(["tag1"]);
|
|
318
|
-
|
|
319
|
-
vi.mocked(mockProjectManager.getLabels).mockResolvedValue({
|
|
320
|
-
id1: ["tag1"],
|
|
321
|
-
});
|
|
322
|
-
const listResult = await server.handleCallTool({
|
|
323
|
-
name: "list_labels",
|
|
324
|
-
args: {
|
|
325
|
-
projectPath: "/p",
|
|
326
|
-
},
|
|
327
|
-
});
|
|
328
|
-
expect(
|
|
329
|
-
JSON.parse(
|
|
330
|
-
(listResult as { content: { text: string }[] }).content[0].text,
|
|
331
|
-
),
|
|
332
|
-
).toEqual({
|
|
333
|
-
id1: ["tag1"],
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
vi.mocked(mockProjectManager.findEntitiesByLabel).mockResolvedValue([
|
|
337
|
-
"id1",
|
|
338
|
-
]);
|
|
339
|
-
const searchResult = await server.handleCallTool({
|
|
340
|
-
name: "search_by_label",
|
|
341
|
-
args: {
|
|
342
|
-
projectPath: "/p",
|
|
343
|
-
label: "tag1",
|
|
344
|
-
},
|
|
345
|
-
});
|
|
346
|
-
expect(
|
|
347
|
-
JSON.parse(
|
|
348
|
-
(searchResult as { content: { text: string }[] }).content[0].text,
|
|
349
|
-
),
|
|
350
|
-
).toEqual(["id1"]);
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
it("should handle enhanced navigation tools", async () => {
|
|
354
|
-
const mockDir = { directories: ["d1"], files: ["f1"] };
|
|
355
|
-
vi.mocked(mockProjectManager.listDirectory).mockResolvedValue(mockDir);
|
|
356
|
-
const dirResult = await server.handleCallTool({
|
|
357
|
-
name: "list_directory",
|
|
358
|
-
args: {
|
|
359
|
-
projectPath: "/p",
|
|
360
|
-
dirPath: "src",
|
|
361
|
-
},
|
|
362
|
-
});
|
|
363
|
-
expect(
|
|
364
|
-
JSON.parse(
|
|
365
|
-
(dirResult as { content: { text: string }[] }).content[0].text,
|
|
366
|
-
),
|
|
367
|
-
).toEqual(mockDir);
|
|
368
|
-
|
|
369
|
-
const mockOutline = [
|
|
370
|
-
{
|
|
371
|
-
name: "Comp",
|
|
372
|
-
line: 1,
|
|
373
|
-
id: "c1",
|
|
374
|
-
kind: "component" as const,
|
|
375
|
-
type: "function" as const,
|
|
376
|
-
},
|
|
377
|
-
];
|
|
378
|
-
vi.mocked(mockProjectManager.getFileOutline).mockResolvedValue(
|
|
379
|
-
mockOutline,
|
|
380
|
-
);
|
|
381
|
-
const outlineResult = await server.handleCallTool({
|
|
382
|
-
name: "get_file_outline",
|
|
383
|
-
args: {
|
|
384
|
-
projectPath: "/p",
|
|
385
|
-
filePath: "f1",
|
|
386
|
-
},
|
|
387
|
-
});
|
|
388
|
-
expect(
|
|
389
|
-
JSON.parse(
|
|
390
|
-
(outlineResult as { content: { text: string }[] }).content[0].text,
|
|
391
|
-
),
|
|
392
|
-
).toEqual(mockOutline);
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
it("should handle symbol exploration tools", async () => {
|
|
396
|
-
const mockLoc = [
|
|
397
|
-
{
|
|
398
|
-
file: "f1",
|
|
399
|
-
line: 1,
|
|
400
|
-
id: "s1",
|
|
401
|
-
name: "S",
|
|
402
|
-
loc: { line: 1, column: 1 },
|
|
403
|
-
kind: "component",
|
|
404
|
-
type: "function",
|
|
405
|
-
},
|
|
406
|
-
];
|
|
407
|
-
vi.mocked(mockProjectManager.getSymbolLocation).mockResolvedValue(
|
|
408
|
-
mockLoc,
|
|
409
|
-
);
|
|
410
|
-
const locResult = await server.handleCallTool({
|
|
411
|
-
name: "get_symbol_location",
|
|
412
|
-
args: {
|
|
413
|
-
projectPath: "/p",
|
|
414
|
-
query: "S",
|
|
415
|
-
},
|
|
416
|
-
});
|
|
417
|
-
expect(
|
|
418
|
-
JSON.parse(
|
|
419
|
-
(locResult as { content: { text: string }[] }).content[0].text,
|
|
420
|
-
),
|
|
421
|
-
).toEqual(mockLoc);
|
|
422
|
-
|
|
423
|
-
const mockContent = [
|
|
424
|
-
{
|
|
425
|
-
id: "s1",
|
|
426
|
-
name: "S",
|
|
427
|
-
file: "f1",
|
|
428
|
-
loc: { line: 1, column: 1 },
|
|
429
|
-
kind: "component",
|
|
430
|
-
type: "function",
|
|
431
|
-
content: "code",
|
|
432
|
-
},
|
|
433
|
-
];
|
|
434
|
-
vi.mocked(mockProjectManager.getSymbolContent).mockResolvedValue(
|
|
435
|
-
mockContent,
|
|
436
|
-
);
|
|
437
|
-
const contentResult = await server.handleCallTool({
|
|
438
|
-
name: "get_symbol_content",
|
|
439
|
-
args: {
|
|
440
|
-
projectPath: "/p",
|
|
441
|
-
query: "S",
|
|
442
|
-
},
|
|
443
|
-
});
|
|
444
|
-
expect(
|
|
445
|
-
JSON.parse(
|
|
446
|
-
(contentResult as { content: { text: string }[] }).content[0].text,
|
|
447
|
-
),
|
|
448
|
-
).toEqual(mockContent);
|
|
449
|
-
});
|
|
450
|
-
|
|
451
|
-
it("should handle find_files tool", async () => {
|
|
452
|
-
const mockResult = ["src/App.tsx"];
|
|
453
|
-
vi.mocked(mockProjectManager.findFiles).mockResolvedValue(mockResult);
|
|
454
|
-
const result = await server.handleCallTool({
|
|
455
|
-
name: "find_files",
|
|
456
|
-
args: {
|
|
457
|
-
projectPath: "/p",
|
|
458
|
-
pattern: "App",
|
|
459
|
-
},
|
|
460
|
-
});
|
|
461
|
-
expect(
|
|
462
|
-
JSON.parse((result as { content: { text: string }[] }).content[0].text),
|
|
463
|
-
).toEqual(mockResult);
|
|
464
|
-
});
|
|
465
|
-
|
|
466
|
-
it("should handle get_file_imports tool", async () => {
|
|
467
|
-
const mockResult = {
|
|
468
|
-
react: {
|
|
469
|
-
localName: "React",
|
|
470
|
-
importedName: "React",
|
|
471
|
-
source: "react",
|
|
472
|
-
type: "default" as const,
|
|
473
|
-
importKind: "value" as const,
|
|
474
|
-
},
|
|
475
|
-
};
|
|
476
|
-
vi.mocked(mockProjectManager.getFileImports).mockResolvedValue(
|
|
477
|
-
mockResult,
|
|
478
|
-
);
|
|
479
|
-
const result = await server.handleCallTool({
|
|
480
|
-
name: "get_file_imports",
|
|
481
|
-
args: {
|
|
482
|
-
projectPath: "/p",
|
|
483
|
-
filePath: "f1",
|
|
484
|
-
},
|
|
485
|
-
});
|
|
486
|
-
expect(
|
|
487
|
-
JSON.parse((result as { content: { text: string }[] }).content[0].text),
|
|
488
|
-
).toEqual(mockResult);
|
|
489
|
-
});
|
|
490
|
-
|
|
491
|
-
it("should handle get_project_tree tool", async () => {
|
|
492
|
-
const mockResult = { name: "/", children: [] };
|
|
493
|
-
vi.mocked(mockProjectManager.getProjectTree).mockResolvedValue(
|
|
494
|
-
mockResult,
|
|
495
|
-
);
|
|
496
|
-
const result = await server.handleCallTool({
|
|
497
|
-
name: "get_project_tree",
|
|
498
|
-
args: {
|
|
499
|
-
projectPath: "/p",
|
|
500
|
-
},
|
|
501
|
-
});
|
|
502
|
-
expect(
|
|
503
|
-
JSON.parse((result as { content: { text: string }[] }).content[0].text),
|
|
504
|
-
).toEqual(mockResult);
|
|
505
|
-
});
|
|
506
|
-
|
|
507
|
-
it("should handle get_component_hierarchy tool", async () => {
|
|
508
|
-
const mockResult = { component: "App", hierarchies: [], renderedBy: [] };
|
|
509
|
-
vi.mocked(mockProjectManager.getComponentHierarchy).mockResolvedValue(
|
|
510
|
-
mockResult,
|
|
511
|
-
);
|
|
512
|
-
const result = await server.handleCallTool({
|
|
513
|
-
name: "get_component_hierarchy",
|
|
514
|
-
args: {
|
|
515
|
-
projectPath: "/p",
|
|
516
|
-
componentName: "App",
|
|
517
|
-
},
|
|
518
|
-
});
|
|
519
|
-
expect(
|
|
520
|
-
JSON.parse((result as { content: { text: string }[] }).content[0].text),
|
|
521
|
-
).toEqual(mockResult);
|
|
522
|
-
});
|
|
523
|
-
|
|
524
|
-
it("should handle run_shell_command tool", async () => {
|
|
525
|
-
const mockResult = { stdout: "ok", stderr: "", exitCode: 0 };
|
|
526
|
-
vi.mocked(mockProjectManager.runShellCommand).mockResolvedValue(
|
|
527
|
-
mockResult,
|
|
528
|
-
);
|
|
529
|
-
const result = await server.handleCallTool({
|
|
530
|
-
name: "run_shell_command",
|
|
531
|
-
args: {
|
|
532
|
-
projectPath: "/p",
|
|
533
|
-
command: "ls",
|
|
534
|
-
},
|
|
535
|
-
});
|
|
536
|
-
expect(
|
|
537
|
-
JSON.parse((result as { content: { text: string }[] }).content[0].text),
|
|
538
|
-
).toEqual(mockResult);
|
|
539
|
-
});
|
|
540
|
-
|
|
541
|
-
it("should handle list_files tool with large project", async () => {
|
|
542
|
-
const mockGraph = {
|
|
543
|
-
files: Object.fromEntries(
|
|
544
|
-
Array.from({ length: 101 }, (_, i) => [`f${i}.ts`, { var: {} }]),
|
|
545
|
-
),
|
|
546
|
-
};
|
|
547
|
-
vi.mocked(mockProjectManager.openProject).mockResolvedValue({
|
|
548
|
-
projectPath: "/p",
|
|
549
|
-
extensions: [],
|
|
550
|
-
sqlitePath: "test.sqlite",
|
|
551
|
-
graph: mockGraph as unknown as JsonData,
|
|
552
|
-
});
|
|
553
|
-
const result = await server.handleCallTool({
|
|
554
|
-
name: "list_files",
|
|
555
|
-
args: {
|
|
556
|
-
projectPath: "/p",
|
|
557
|
-
},
|
|
558
|
-
});
|
|
559
|
-
const content = JSON.parse(
|
|
560
|
-
(result as { content: { text: string }[] }).content[0].text,
|
|
561
|
-
) as { totalFiles: number; files: { path: string; exports?: unknown }[] };
|
|
562
|
-
expect(content.totalFiles).toBe(101);
|
|
563
|
-
expect(content.files[0]).not.toHaveProperty("exports");
|
|
564
|
-
});
|
|
565
|
-
|
|
566
|
-
it("should return error if tool is unknown", async () => {
|
|
567
|
-
await expect(
|
|
568
|
-
server.handleCallTool({
|
|
569
|
-
name: "unknown_tool",
|
|
570
|
-
args: {},
|
|
571
|
-
}),
|
|
572
|
-
).rejects.toThrow("Unknown tool");
|
|
573
|
-
});
|
|
574
|
-
|
|
575
|
-
it("should handle list tools request", async () => {
|
|
576
|
-
const listToolsHandler = vi
|
|
577
|
-
.mocked((server as unknown as { mcpServer: Server }).mcpServer)
|
|
578
|
-
.setRequestHandler.mock.calls.find(
|
|
579
|
-
(args) => args[0] === ListToolsRequestSchema,
|
|
580
|
-
)![1] as () => Promise<{ tools: unknown[] }>;
|
|
581
|
-
const result = await listToolsHandler();
|
|
582
|
-
expect(result.tools).toBeDefined();
|
|
583
|
-
expect(
|
|
584
|
-
(result.tools as { name: string }[]).some(
|
|
585
|
-
(t) => t.name === "open_project",
|
|
586
|
-
),
|
|
587
|
-
).toBe(true);
|
|
588
|
-
});
|
|
589
|
-
|
|
590
|
-
it("should handle call tool request with error", async () => {
|
|
591
|
-
const callToolHandler = vi
|
|
592
|
-
.mocked((server as unknown as { mcpServer: Server }).mcpServer)
|
|
593
|
-
.setRequestHandler.mock.calls.find(
|
|
594
|
-
(args) => args[0] === CallToolRequestSchema,
|
|
595
|
-
)![1] as (req: {
|
|
596
|
-
params: { name: string };
|
|
597
|
-
}) => Promise<{ isError: boolean }>;
|
|
598
|
-
|
|
599
|
-
const result = await callToolHandler({ params: { name: "unknown" } });
|
|
600
|
-
expect(result.isError).toBe(true);
|
|
601
|
-
});
|
|
602
|
-
|
|
603
|
-
it("should handle extension tools", async () => {
|
|
604
|
-
const mockExt = {
|
|
605
|
-
id: "ext1",
|
|
606
|
-
mcpTools: [
|
|
607
|
-
{
|
|
608
|
-
name: "tool1",
|
|
609
|
-
description: "desc",
|
|
610
|
-
inputSchema: {},
|
|
611
|
-
handler: vi.fn().mockResolvedValue({ ok: true }),
|
|
612
|
-
},
|
|
613
|
-
],
|
|
614
|
-
};
|
|
615
|
-
vi.mocked(mockProjectManager.getAllExtensions).mockReturnValue([
|
|
616
|
-
mockExt as unknown as Extension,
|
|
617
|
-
]);
|
|
618
|
-
|
|
619
|
-
// List tools
|
|
620
|
-
const listToolsHandler = vi
|
|
621
|
-
.mocked((server as unknown as { mcpServer: Server }).mcpServer)
|
|
622
|
-
.setRequestHandler.mock.calls.find(
|
|
623
|
-
(args) => args[0] === ListToolsRequestSchema,
|
|
624
|
-
)![1] as () => Promise<{ tools: unknown[] }>;
|
|
625
|
-
const tools = await listToolsHandler();
|
|
626
|
-
expect(
|
|
627
|
-
(tools.tools as { name: string }[]).some(
|
|
628
|
-
(t) => t.name === "ext1_tool1",
|
|
629
|
-
),
|
|
630
|
-
).toBe(true);
|
|
631
|
-
|
|
632
|
-
// Call tool
|
|
633
|
-
const result = await server.handleCallTool({
|
|
634
|
-
name: "ext1_tool1",
|
|
635
|
-
args: { some: "arg" },
|
|
636
|
-
});
|
|
637
|
-
expect(mockExt.mcpTools[0].handler).toHaveBeenCalled();
|
|
638
|
-
expect(
|
|
639
|
-
JSON.parse((result as { content: { text: string }[] }).content[0].text),
|
|
640
|
-
).toEqual({ ok: true });
|
|
641
|
-
});
|
|
642
|
-
});
|
|
643
|
-
|
|
644
|
-
describe("WebSocket Handlers", () => {
|
|
645
|
-
let mockWs: {
|
|
646
|
-
send: ReturnType<typeof vi.fn>;
|
|
647
|
-
on: ReturnType<typeof vi.fn>;
|
|
648
|
-
};
|
|
649
|
-
|
|
650
|
-
beforeEach(() => {
|
|
651
|
-
mockWs = {
|
|
652
|
-
send: vi.fn(),
|
|
653
|
-
on: vi.fn(),
|
|
654
|
-
};
|
|
655
|
-
server.startWebSocketServer();
|
|
656
|
-
const wssInstance = vi.mocked(WebSocketServer).mock.instances[0];
|
|
657
|
-
const connectionHandler = vi
|
|
658
|
-
.mocked(wssInstance.on)
|
|
659
|
-
.mock.calls.find((args) => args[0] === "connection")![1] as (
|
|
660
|
-
ws: unknown,
|
|
661
|
-
) => void;
|
|
662
|
-
connectionHandler(mockWs);
|
|
663
|
-
});
|
|
664
|
-
|
|
665
|
-
it("should handle open_project message", async () => {
|
|
666
|
-
const messageHandler = mockWs.on.mock.calls.find(
|
|
667
|
-
(args) => args[0] === "message",
|
|
668
|
-
)![1] as (msg: string) => Promise<void>;
|
|
669
|
-
|
|
670
|
-
const payload = {
|
|
671
|
-
type: "open_project",
|
|
672
|
-
payload: { projectPath: "/test" },
|
|
673
|
-
requestId: "req-1",
|
|
674
|
-
};
|
|
675
|
-
|
|
676
|
-
await messageHandler(JSON.stringify(payload));
|
|
677
|
-
|
|
678
|
-
expect(mockProjectManager.openProject).toHaveBeenCalledWith(
|
|
679
|
-
"/test",
|
|
680
|
-
undefined,
|
|
681
|
-
);
|
|
682
|
-
expect(mockWs.send).toHaveBeenCalledWith(
|
|
683
|
-
expect.stringContaining("project_opened"),
|
|
684
|
-
);
|
|
685
|
-
expect(mockWs.send).toHaveBeenCalledWith(
|
|
686
|
-
expect.stringContaining("req-1"),
|
|
687
|
-
);
|
|
688
|
-
});
|
|
689
|
-
|
|
690
|
-
it("should handle call_tool message", async () => {
|
|
691
|
-
const messageHandler = mockWs.on.mock.calls.find(
|
|
692
|
-
(args) => args[0] === "message",
|
|
693
|
-
)![1] as (msg: string) => Promise<void>;
|
|
694
|
-
|
|
695
|
-
const payload = {
|
|
696
|
-
type: "call_tool",
|
|
697
|
-
payload: { name: "open_project", arguments: { projectPath: "/test" } },
|
|
698
|
-
requestId: "req-3",
|
|
699
|
-
};
|
|
700
|
-
|
|
701
|
-
await messageHandler(JSON.stringify(payload));
|
|
702
|
-
|
|
703
|
-
expect(mockWs.send).toHaveBeenCalledWith(
|
|
704
|
-
expect.stringContaining("tool_result"),
|
|
705
|
-
);
|
|
706
|
-
});
|
|
707
|
-
|
|
708
|
-
it("should handle unknown message type", async () => {
|
|
709
|
-
const messageHandler = mockWs.on.mock.calls.find(
|
|
710
|
-
(args) => args[0] === "message",
|
|
711
|
-
)![1] as (msg: string) => Promise<void>;
|
|
712
|
-
await messageHandler(JSON.stringify({ type: "unknown", payload: {} }));
|
|
713
|
-
expect(mockWs.send).toHaveBeenCalledWith(
|
|
714
|
-
expect.stringContaining("Unknown message type"),
|
|
715
|
-
);
|
|
716
|
-
});
|
|
717
|
-
|
|
718
|
-
it("should handle WebSocket server error", () => {
|
|
719
|
-
server.startWebSocketServer();
|
|
720
|
-
const wssInstance = vi.mocked(WebSocketServer).mock.instances[0];
|
|
721
|
-
const errorHandler = vi
|
|
722
|
-
.mocked(wssInstance.on)
|
|
723
|
-
.mock.calls.find((args) => args[0] === "error")![1] as (
|
|
724
|
-
err: unknown,
|
|
725
|
-
) => void;
|
|
726
|
-
|
|
727
|
-
errorHandler(new Error("Test error"));
|
|
728
|
-
expect(console.error).toHaveBeenCalledWith(
|
|
729
|
-
expect.stringContaining("WebSocket server error"),
|
|
730
|
-
);
|
|
731
|
-
|
|
732
|
-
const addrInUseError = new Error("Address in use");
|
|
733
|
-
(addrInUseError as unknown as Record<string, unknown>).code =
|
|
734
|
-
"EADDRINUSE";
|
|
735
|
-
errorHandler(addrInUseError);
|
|
736
|
-
expect(console.error).toHaveBeenCalledWith(
|
|
737
|
-
expect.stringContaining("Port 3030 is already in use"),
|
|
738
|
-
);
|
|
739
|
-
});
|
|
740
|
-
|
|
741
|
-
it("should handle malformed JSON in websocket", async () => {
|
|
742
|
-
const messageHandler = mockWs.on.mock.calls.find(
|
|
743
|
-
(args) => args[0] === "message",
|
|
744
|
-
)![1] as (msg: string) => Promise<void>;
|
|
745
|
-
await messageHandler("not-json");
|
|
746
|
-
expect(mockWs.send).toHaveBeenCalledWith(
|
|
747
|
-
expect.stringContaining("error"),
|
|
748
|
-
);
|
|
749
|
-
});
|
|
750
|
-
});
|
|
751
|
-
|
|
752
|
-
describe("Self-Termination", () => {
|
|
753
|
-
it("should start exit countdown when last connection drops", async () => {
|
|
754
|
-
vi.useFakeTimers();
|
|
755
|
-
process.env.NODE_ENV = "production";
|
|
756
|
-
|
|
757
|
-
server.startWebSocketServer();
|
|
758
|
-
const wssInstance = vi.mocked(WebSocketServer).mock.instances[0];
|
|
759
|
-
const connectionHandler = vi
|
|
760
|
-
.mocked(wssInstance.on)
|
|
761
|
-
.mock.calls.find((args) => args[0] === "connection")![1] as (
|
|
762
|
-
ws: unknown,
|
|
763
|
-
) => void;
|
|
764
|
-
|
|
765
|
-
const mockWs = { on: vi.fn(), send: vi.fn() };
|
|
766
|
-
connectionHandler(mockWs);
|
|
767
|
-
|
|
768
|
-
const closeHandler = mockWs.on.mock.calls.find(
|
|
769
|
-
(args) => args[0] === "close",
|
|
770
|
-
)![1] as () => void;
|
|
771
|
-
closeHandler();
|
|
772
|
-
|
|
773
|
-
// Countdown started
|
|
774
|
-
await vi.advanceTimersByTimeAsync(10000);
|
|
775
|
-
|
|
776
|
-
expect(mockProjectManager.closeAll).toHaveBeenCalled();
|
|
777
|
-
expect(process.exit).toHaveBeenCalledWith(0);
|
|
778
|
-
|
|
779
|
-
vi.useRealTimers();
|
|
780
|
-
});
|
|
781
|
-
});
|
|
782
|
-
});
|