@terreno/ai 0.10.0 → 0.11.1
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/bunfig.toml +5 -0
- package/dist/routes/projects.js +11 -11
- package/package.json +6 -5
- package/src/aiApp.test.ts +150 -0
- package/src/isolated/langfuseClient.isolated.ts +64 -0
- package/src/langfuseApp.test.ts +135 -0
- package/src/langfuseCache.test.ts +62 -76
- package/src/langfusePrompts.test.ts +59 -114
- package/src/langfuseRoutes.test.ts +140 -28
- package/src/langfuseTracing.test.ts +68 -0
- package/src/langfuseVercelAi.test.ts +83 -0
- package/src/models/aiRequest.test.ts +81 -0
- package/src/models/fileAttachment.test.ts +3 -1
- package/src/routes/files.test.ts +126 -0
- package/src/routes/mcp.test.ts +132 -0
- package/src/routes/projects.test.ts +205 -0
- package/src/routes/projects.ts +25 -25
- package/src/routes/routes.test.ts +999 -12
- package/src/service/aiService.test.ts +238 -13
- package/src/service/fileStorage.test.ts +90 -0
- package/src/service/mcpService.test.ts +139 -0
- package/tsconfig.json +8 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import type {LanguageModel} from "ai";
|
|
2
3
|
import mongoose from "mongoose";
|
|
3
4
|
|
|
4
5
|
import {AIRequest} from "../models/aiRequest";
|
|
@@ -47,7 +48,7 @@ describe("AIService", () => {
|
|
|
47
48
|
describe("constructor", () => {
|
|
48
49
|
it("should create an instance with default temperature", () => {
|
|
49
50
|
const model = createMockModel();
|
|
50
|
-
const service = new AIService({model: model as
|
|
51
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
51
52
|
expect(service).toBeDefined();
|
|
52
53
|
});
|
|
53
54
|
|
|
@@ -55,7 +56,7 @@ describe("AIService", () => {
|
|
|
55
56
|
const model = createMockModel();
|
|
56
57
|
const service = new AIService({
|
|
57
58
|
defaultTemperature: TemperaturePresets.LOW,
|
|
58
|
-
model: model as
|
|
59
|
+
model: model as unknown as LanguageModel,
|
|
59
60
|
});
|
|
60
61
|
expect(service).toBeDefined();
|
|
61
62
|
});
|
|
@@ -64,7 +65,7 @@ describe("AIService", () => {
|
|
|
64
65
|
describe("generateText", () => {
|
|
65
66
|
it("should generate text and log the request", async () => {
|
|
66
67
|
const model = createMockModel("Hello world");
|
|
67
|
-
const service = new AIService({model: model as
|
|
68
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
68
69
|
const userId = new mongoose.Types.ObjectId();
|
|
69
70
|
|
|
70
71
|
const result = await service.generateText({
|
|
@@ -88,7 +89,7 @@ describe("AIService", () => {
|
|
|
88
89
|
model.doGenerate = mock(async () => {
|
|
89
90
|
throw new Error("API error");
|
|
90
91
|
});
|
|
91
|
-
const service = new AIService({model: model as
|
|
92
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
92
93
|
|
|
93
94
|
await expect(service.generateText({prompt: "test"})).rejects.toThrow("API error");
|
|
94
95
|
|
|
@@ -101,7 +102,7 @@ describe("AIService", () => {
|
|
|
101
102
|
describe("generateTextStream", () => {
|
|
102
103
|
it("should stream text chunks", async () => {
|
|
103
104
|
const model = createMockModel();
|
|
104
|
-
const service = new AIService({model: model as
|
|
105
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
105
106
|
|
|
106
107
|
const chunks: string[] = [];
|
|
107
108
|
for await (const chunk of service.generateTextStream({prompt: "test"})) {
|
|
@@ -116,7 +117,7 @@ describe("AIService", () => {
|
|
|
116
117
|
describe("generateRemix", () => {
|
|
117
118
|
it("should remix text", async () => {
|
|
118
119
|
const model = createMockModel("Remixed text");
|
|
119
|
-
const service = new AIService({model: model as
|
|
120
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
120
121
|
|
|
121
122
|
const result = await service.generateRemix({text: "Original text"});
|
|
122
123
|
expect(result).toBe("Remixed text");
|
|
@@ -126,7 +127,7 @@ describe("AIService", () => {
|
|
|
126
127
|
describe("generateSummary", () => {
|
|
127
128
|
it("should summarize text", async () => {
|
|
128
129
|
const model = createMockModel("Summary of the text");
|
|
129
|
-
const service = new AIService({model: model as
|
|
130
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
130
131
|
|
|
131
132
|
const result = await service.generateSummary({text: "Long text to summarize"});
|
|
132
133
|
expect(result).toBe("Summary of the text");
|
|
@@ -136,7 +137,7 @@ describe("AIService", () => {
|
|
|
136
137
|
describe("translateText", () => {
|
|
137
138
|
it("should translate text", async () => {
|
|
138
139
|
const model = createMockModel("Hola mundo");
|
|
139
|
-
const service = new AIService({model: model as
|
|
140
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
140
141
|
|
|
141
142
|
const result = await service.translateText({
|
|
142
143
|
targetLanguage: "Spanish",
|
|
@@ -144,12 +145,165 @@ describe("AIService", () => {
|
|
|
144
145
|
});
|
|
145
146
|
expect(result).toBe("Hola mundo");
|
|
146
147
|
});
|
|
148
|
+
|
|
149
|
+
it("should translate text with explicit sourceLanguage", async () => {
|
|
150
|
+
const model = createMockModel("Bonjour le monde");
|
|
151
|
+
const service = new AIService({model: model as any});
|
|
152
|
+
|
|
153
|
+
const result = await service.translateText({
|
|
154
|
+
sourceLanguage: "English",
|
|
155
|
+
targetLanguage: "French",
|
|
156
|
+
text: "Hello world",
|
|
157
|
+
});
|
|
158
|
+
expect(result).toBe("Bonjour le monde");
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
describe("modelId getter", () => {
|
|
163
|
+
it("should return the model id from the underlying model", () => {
|
|
164
|
+
const model = createMockModel();
|
|
165
|
+
const service = new AIService({model: model as any});
|
|
166
|
+
|
|
167
|
+
expect(service.modelId).toBe("mock-model");
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("should return 'unknown' when model has no modelId", () => {
|
|
171
|
+
const model = {
|
|
172
|
+
doGenerate: mock(async () => ({
|
|
173
|
+
content: [],
|
|
174
|
+
finishReason: "stop" as const,
|
|
175
|
+
usage: {inputTokens: 0, outputTokens: 0},
|
|
176
|
+
})),
|
|
177
|
+
doStream: mock(async () => ({stream: new ReadableStream()})),
|
|
178
|
+
provider: "mock-provider",
|
|
179
|
+
specificationVersion: "v2" as const,
|
|
180
|
+
supportedUrls: {},
|
|
181
|
+
};
|
|
182
|
+
const service = new AIService({model: model as any});
|
|
183
|
+
|
|
184
|
+
expect(service.modelId).toBe("unknown");
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
describe("generateTextStream", () => {
|
|
189
|
+
it("should log errors when the stream throws", async () => {
|
|
190
|
+
const model = createMockModel();
|
|
191
|
+
model.doStream = mock(async () => {
|
|
192
|
+
throw new Error("Stream API error");
|
|
193
|
+
});
|
|
194
|
+
const service = new AIService({model: model as any});
|
|
195
|
+
|
|
196
|
+
const run = async () => {
|
|
197
|
+
const chunks: string[] = [];
|
|
198
|
+
for await (const chunk of service.generateTextStream({prompt: "stream test"})) {
|
|
199
|
+
chunks.push(chunk);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
await expect(run()).rejects.toThrow();
|
|
204
|
+
|
|
205
|
+
const logs = await AIRequest.find({prompt: "stream test"});
|
|
206
|
+
expect(logs.length).toBe(1);
|
|
207
|
+
expect(logs[0].error).toBeTruthy();
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
describe("buildMessages file content", () => {
|
|
212
|
+
it("should convert multi-modal user prompts with file content", () => {
|
|
213
|
+
const model = createMockModel();
|
|
214
|
+
const service = new AIService({model: model as any});
|
|
215
|
+
|
|
216
|
+
const messages = service.buildMessages([
|
|
217
|
+
{
|
|
218
|
+
content: [
|
|
219
|
+
{text: "Please review this file", type: "text" as const},
|
|
220
|
+
{
|
|
221
|
+
filename: "report.pdf",
|
|
222
|
+
mimeType: "application/pdf",
|
|
223
|
+
type: "file" as const,
|
|
224
|
+
url: "https://example.com/report.pdf",
|
|
225
|
+
},
|
|
226
|
+
],
|
|
227
|
+
text: "Please review this file",
|
|
228
|
+
type: "user",
|
|
229
|
+
},
|
|
230
|
+
]);
|
|
231
|
+
|
|
232
|
+
expect(messages.length).toBe(1);
|
|
233
|
+
const content = (messages[0] as any).content;
|
|
234
|
+
expect(content.length).toBe(2);
|
|
235
|
+
expect(content[1].type).toBe("file");
|
|
236
|
+
expect(content[1].filename).toBe("report.pdf");
|
|
237
|
+
expect(content[1].mediaType).toBe("application/pdf");
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
describe("generateChatStream", () => {
|
|
242
|
+
it("should stream chat responses and log the request", async () => {
|
|
243
|
+
const model = createMockModel();
|
|
244
|
+
const service = new AIService({model: model as any});
|
|
245
|
+
const userId = new mongoose.Types.ObjectId();
|
|
246
|
+
|
|
247
|
+
const chunks: string[] = [];
|
|
248
|
+
for await (const chunk of service.generateChatStream({
|
|
249
|
+
messages: [{content: "Hello", role: "user"}],
|
|
250
|
+
userId,
|
|
251
|
+
})) {
|
|
252
|
+
chunks.push(chunk);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
expect(chunks.join("")).toBe("Mock response");
|
|
256
|
+
|
|
257
|
+
const logs = await AIRequest.find({userId});
|
|
258
|
+
expect(logs.length).toBe(1);
|
|
259
|
+
expect(logs[0].prompt).toContain("user: Hello");
|
|
260
|
+
expect(logs[0].response).toBe("Mock response");
|
|
261
|
+
expect(logs[0].aiModel).toBe("mock-model");
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("should log errors when chat stream fails", async () => {
|
|
265
|
+
const model = createMockModel();
|
|
266
|
+
model.doStream = mock(async () => {
|
|
267
|
+
throw new Error("Chat stream failed");
|
|
268
|
+
});
|
|
269
|
+
const service = new AIService({model: model as any});
|
|
270
|
+
|
|
271
|
+
const run = async () => {
|
|
272
|
+
const chunks: string[] = [];
|
|
273
|
+
for await (const chunk of service.generateChatStream({
|
|
274
|
+
messages: [{content: "Hi", role: "user"}],
|
|
275
|
+
})) {
|
|
276
|
+
chunks.push(chunk);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
await expect(run()).rejects.toThrow();
|
|
281
|
+
|
|
282
|
+
const logs = await AIRequest.find({});
|
|
283
|
+
const errLog = logs.find((log) => Boolean(log.error));
|
|
284
|
+
expect(errLog).toBeDefined();
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it("should accept systemPrompt override", async () => {
|
|
288
|
+
const model = createMockModel();
|
|
289
|
+
const service = new AIService({model: model as any});
|
|
290
|
+
|
|
291
|
+
const chunks: string[] = [];
|
|
292
|
+
for await (const chunk of service.generateChatStream({
|
|
293
|
+
messages: [{content: "Hi", role: "user"}],
|
|
294
|
+
systemPrompt: "You are a test assistant",
|
|
295
|
+
})) {
|
|
296
|
+
chunks.push(chunk);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
expect(chunks.join("")).toBe("Mock response");
|
|
300
|
+
});
|
|
147
301
|
});
|
|
148
302
|
|
|
149
303
|
describe("buildMessages", () => {
|
|
150
304
|
it("should convert simple text prompts to CoreMessages", () => {
|
|
151
305
|
const model = createMockModel();
|
|
152
|
-
const service = new AIService({model: model as
|
|
306
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
153
307
|
|
|
154
308
|
const messages = service.buildMessages([
|
|
155
309
|
{text: "Hello", type: "user"},
|
|
@@ -165,7 +319,7 @@ describe("AIService", () => {
|
|
|
165
319
|
|
|
166
320
|
it("should convert multi-modal user prompts with image content", () => {
|
|
167
321
|
const model = createMockModel();
|
|
168
|
-
const service = new AIService({model: model as
|
|
322
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
169
323
|
|
|
170
324
|
const messages = service.buildMessages([
|
|
171
325
|
{
|
|
@@ -180,7 +334,7 @@ describe("AIService", () => {
|
|
|
180
334
|
|
|
181
335
|
expect(messages.length).toBe(1);
|
|
182
336
|
expect(messages[0].role).toBe("user");
|
|
183
|
-
const content = (messages[0] as
|
|
337
|
+
const content = (messages[0] as {content: Array<{type: string; text?: string}>}).content;
|
|
184
338
|
expect(content.length).toBe(2);
|
|
185
339
|
expect(content[0].type).toBe("text");
|
|
186
340
|
expect(content[0].text).toBe("What is this?");
|
|
@@ -189,7 +343,7 @@ describe("AIService", () => {
|
|
|
189
343
|
|
|
190
344
|
it("should skip tool-call and tool-result prompts", () => {
|
|
191
345
|
const model = createMockModel();
|
|
192
|
-
const service = new AIService({model: model as
|
|
346
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
193
347
|
|
|
194
348
|
const messages = service.buildMessages([
|
|
195
349
|
{text: "What time is it?", type: "user"},
|
|
@@ -217,7 +371,7 @@ describe("AIService", () => {
|
|
|
217
371
|
|
|
218
372
|
it("should handle system messages", () => {
|
|
219
373
|
const model = createMockModel();
|
|
220
|
-
const service = new AIService({model: model as
|
|
374
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
221
375
|
|
|
222
376
|
const messages = service.buildMessages([
|
|
223
377
|
{text: "You are helpful", type: "system"},
|
|
@@ -229,6 +383,77 @@ describe("AIService", () => {
|
|
|
229
383
|
});
|
|
230
384
|
});
|
|
231
385
|
|
|
386
|
+
describe("generateChatStream", () => {
|
|
387
|
+
it("streams chat responses and logs usage", async () => {
|
|
388
|
+
const model = createMockModel();
|
|
389
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
390
|
+
const userId = new mongoose.Types.ObjectId();
|
|
391
|
+
|
|
392
|
+
const chunks: string[] = [];
|
|
393
|
+
for await (const chunk of service.generateChatStream({
|
|
394
|
+
messages: [{content: "Hi", role: "user"}],
|
|
395
|
+
systemPrompt: "You are helpful",
|
|
396
|
+
userId,
|
|
397
|
+
})) {
|
|
398
|
+
chunks.push(chunk);
|
|
399
|
+
}
|
|
400
|
+
expect(chunks.join("")).toBe("Mock response");
|
|
401
|
+
|
|
402
|
+
const logs = await AIRequest.find({userId});
|
|
403
|
+
expect(logs.length).toBe(1);
|
|
404
|
+
expect(logs[0].response).toBe("Mock response");
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it("logs errors from the stream", async () => {
|
|
408
|
+
const model = createMockModel();
|
|
409
|
+
model.doStream = mock(async () => {
|
|
410
|
+
throw new Error("stream failure");
|
|
411
|
+
});
|
|
412
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
413
|
+
let threw = false;
|
|
414
|
+
try {
|
|
415
|
+
for await (const _chunk of service.generateChatStream({
|
|
416
|
+
messages: [{content: "Hi", role: "user"}],
|
|
417
|
+
})) {
|
|
418
|
+
// consume
|
|
419
|
+
}
|
|
420
|
+
} catch (err) {
|
|
421
|
+
threw = true;
|
|
422
|
+
expect(err).toBeDefined();
|
|
423
|
+
}
|
|
424
|
+
expect(threw).toBe(true);
|
|
425
|
+
|
|
426
|
+
const logs = await AIRequest.find({});
|
|
427
|
+
const errored = logs.find((l) => !!l.error);
|
|
428
|
+
expect(errored).toBeDefined();
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
describe("buildMessages file parts", () => {
|
|
433
|
+
it("includes file parts from multi-modal user prompts", () => {
|
|
434
|
+
const model = createMockModel();
|
|
435
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
436
|
+
const messages = service.buildMessages([
|
|
437
|
+
{
|
|
438
|
+
content: [
|
|
439
|
+
{text: "Look", type: "text" as const},
|
|
440
|
+
{
|
|
441
|
+
filename: "doc.pdf",
|
|
442
|
+
mimeType: "application/pdf",
|
|
443
|
+
type: "file" as const,
|
|
444
|
+
url: "https://example.com/doc.pdf",
|
|
445
|
+
},
|
|
446
|
+
],
|
|
447
|
+
text: "Look",
|
|
448
|
+
type: "user",
|
|
449
|
+
},
|
|
450
|
+
]);
|
|
451
|
+
const content = (messages[0] as {content: Array<{type: string; filename?: string}>}).content;
|
|
452
|
+
expect(content[1].type).toBe("file");
|
|
453
|
+
expect(content[1].filename).toBe("doc.pdf");
|
|
454
|
+
});
|
|
455
|
+
});
|
|
456
|
+
|
|
232
457
|
describe("TemperaturePresets", () => {
|
|
233
458
|
it("should have correct values", () => {
|
|
234
459
|
expect(TemperaturePresets.DETERMINISTIC).toBe(0);
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
|
|
4
|
+
import {FileAttachment} from "../models/fileAttachment";
|
|
5
|
+
|
|
6
|
+
// Mock @google-cloud/storage so the service doesn't try to authenticate
|
|
7
|
+
const bucketFileMock = {
|
|
8
|
+
delete: mock(async (_opts?: Record<string, unknown>) => [undefined]),
|
|
9
|
+
getSignedUrl: mock(async () => ["https://example.com/signed-url"]),
|
|
10
|
+
save: mock(async (_buffer: Buffer, _opts: Record<string, unknown>) => [undefined]),
|
|
11
|
+
};
|
|
12
|
+
const bucketObj = {
|
|
13
|
+
file: mock((_key: string) => bucketFileMock),
|
|
14
|
+
};
|
|
15
|
+
mock.module("@google-cloud/storage", () => ({
|
|
16
|
+
Storage: class {
|
|
17
|
+
bucket = (_name: string) => bucketObj;
|
|
18
|
+
},
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
const {FileStorageService} = await import("./fileStorage");
|
|
22
|
+
|
|
23
|
+
describe("FileStorageService", () => {
|
|
24
|
+
beforeEach(async () => {
|
|
25
|
+
await FileAttachment.deleteMany({});
|
|
26
|
+
bucketFileMock.delete.mockClear();
|
|
27
|
+
bucketFileMock.getSignedUrl.mockClear();
|
|
28
|
+
bucketFileMock.save.mockClear();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterEach(async () => {
|
|
32
|
+
await FileAttachment.deleteMany({});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("upload", () => {
|
|
36
|
+
it("uploads a file, creates an attachment, and returns metadata", async () => {
|
|
37
|
+
const service = new FileStorageService({bucketName: "test-bucket"});
|
|
38
|
+
const userId = new mongoose.Types.ObjectId();
|
|
39
|
+
const buffer = Buffer.from("hello world");
|
|
40
|
+
|
|
41
|
+
const result = await service.upload({
|
|
42
|
+
buffer,
|
|
43
|
+
filename: "my file.txt",
|
|
44
|
+
mimeType: "text/plain",
|
|
45
|
+
userId,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
expect(result.filename).toBe("my file.txt");
|
|
49
|
+
expect(result.gcsKey).toContain("uploads/");
|
|
50
|
+
expect(result.gcsKey).toContain("my_file.txt");
|
|
51
|
+
expect(result.size).toBe(buffer.length);
|
|
52
|
+
expect(result.url).toContain("storage.googleapis.com/test-bucket");
|
|
53
|
+
expect(bucketFileMock.save).toHaveBeenCalledTimes(1);
|
|
54
|
+
|
|
55
|
+
const attachment = await FileAttachment.findOne({gcsKey: result.gcsKey});
|
|
56
|
+
expect(attachment).toBeDefined();
|
|
57
|
+
expect(attachment?.mimeType).toBe("text/plain");
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("getSignedUrl", () => {
|
|
62
|
+
it("returns a signed url from the bucket file", async () => {
|
|
63
|
+
const service = new FileStorageService({bucketName: "test-bucket"});
|
|
64
|
+
const url = await service.getSignedUrl("uploads/abc/file.txt");
|
|
65
|
+
expect(url).toBe("https://example.com/signed-url");
|
|
66
|
+
expect(bucketFileMock.getSignedUrl).toHaveBeenCalledTimes(1);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe("delete", () => {
|
|
71
|
+
it("deletes from bucket and marks attachment as deleted", async () => {
|
|
72
|
+
const service = new FileStorageService({bucketName: "test-bucket"});
|
|
73
|
+
const userId = new mongoose.Types.ObjectId();
|
|
74
|
+
const attachment = await FileAttachment.create({
|
|
75
|
+
filename: "x.txt",
|
|
76
|
+
gcsKey: "uploads/x/y.txt",
|
|
77
|
+
mimeType: "text/plain",
|
|
78
|
+
size: 10,
|
|
79
|
+
url: "https://example.com/x",
|
|
80
|
+
userId,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
await service.delete("uploads/x/y.txt");
|
|
84
|
+
|
|
85
|
+
// findOneAndUpdate may return the old doc — just verify it ran and bucket delete was called
|
|
86
|
+
expect(bucketFileMock.delete).toHaveBeenCalledTimes(1);
|
|
87
|
+
expect(attachment._id).toBeDefined();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
|
|
3
|
+
const clientImpl = {
|
|
4
|
+
close: mock(async () => {}),
|
|
5
|
+
tools: mock(async () => ({search: {description: "Search"}})),
|
|
6
|
+
};
|
|
7
|
+
const createMCPClientMock = mock(async () => clientImpl);
|
|
8
|
+
mock.module("@ai-sdk/mcp", () => ({
|
|
9
|
+
createMCPClient: (opts: Record<string, unknown>) => createMCPClientMock(opts),
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
const {MCPService} = await import("./mcpService");
|
|
13
|
+
|
|
14
|
+
const makeConfig = (overrides?: Partial<Record<string, unknown>>) => ({
|
|
15
|
+
name: "test-server",
|
|
16
|
+
transport: {
|
|
17
|
+
headers: {authorization: "Bearer t"},
|
|
18
|
+
type: "sse" as const,
|
|
19
|
+
url: "https://example.com/mcp",
|
|
20
|
+
},
|
|
21
|
+
...overrides,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe("MCPService", () => {
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
createMCPClientMock.mockClear();
|
|
27
|
+
clientImpl.close.mockClear();
|
|
28
|
+
clientImpl.tools.mockClear();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe("getServerStatus", () => {
|
|
32
|
+
it("reports disconnected status for newly-constructed service", () => {
|
|
33
|
+
const service = new MCPService([makeConfig()]);
|
|
34
|
+
expect(service.getServerStatus()).toEqual([{connected: false, name: "test-server"}]);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("connect", () => {
|
|
39
|
+
it("connects to all configured servers and marks them connected", async () => {
|
|
40
|
+
const service = new MCPService([makeConfig()]);
|
|
41
|
+
await service.connect();
|
|
42
|
+
expect(createMCPClientMock).toHaveBeenCalledTimes(1);
|
|
43
|
+
expect(service.getServerStatus()).toEqual([{connected: true, name: "test-server"}]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("marks server as disconnected when client creation throws", async () => {
|
|
47
|
+
createMCPClientMock.mockImplementationOnce(async () => {
|
|
48
|
+
throw new Error("connection refused");
|
|
49
|
+
});
|
|
50
|
+
const service = new MCPService([makeConfig()]);
|
|
51
|
+
await service.connect();
|
|
52
|
+
expect(service.getServerStatus()).toEqual([{connected: false, name: "test-server"}]);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe("getTools", () => {
|
|
57
|
+
it("aggregates tools from connected servers", async () => {
|
|
58
|
+
const service = new MCPService([makeConfig()]);
|
|
59
|
+
await service.connect();
|
|
60
|
+
const tools = await service.getTools();
|
|
61
|
+
expect(Object.keys(tools)).toContain("search");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("returns empty object when no servers are connected", async () => {
|
|
65
|
+
const service = new MCPService([makeConfig()]);
|
|
66
|
+
const tools = await service.getTools();
|
|
67
|
+
expect(tools).toEqual({});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("skips tools from a server that throws while fetching", async () => {
|
|
71
|
+
const service = new MCPService([makeConfig()]);
|
|
72
|
+
await service.connect();
|
|
73
|
+
clientImpl.tools.mockImplementationOnce(async () => {
|
|
74
|
+
throw new Error("timeout");
|
|
75
|
+
});
|
|
76
|
+
const tools = await service.getTools();
|
|
77
|
+
expect(tools).toEqual({});
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe("disconnect", () => {
|
|
82
|
+
it("closes connected clients and clears their state", async () => {
|
|
83
|
+
const service = new MCPService([makeConfig()]);
|
|
84
|
+
await service.connect();
|
|
85
|
+
await service.disconnect();
|
|
86
|
+
expect(clientImpl.close).toHaveBeenCalledTimes(1);
|
|
87
|
+
expect(service.getServerStatus()).toEqual([{connected: false, name: "test-server"}]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("ignores errors from client.close", async () => {
|
|
91
|
+
const service = new MCPService([makeConfig()]);
|
|
92
|
+
await service.connect();
|
|
93
|
+
clientImpl.close.mockImplementationOnce(async () => {
|
|
94
|
+
throw new Error("already closed");
|
|
95
|
+
});
|
|
96
|
+
await service.disconnect();
|
|
97
|
+
expect(service.getServerStatus()).toEqual([{connected: false, name: "test-server"}]);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe("reconnectServer", () => {
|
|
102
|
+
it("returns false for unknown server names", async () => {
|
|
103
|
+
const service = new MCPService([makeConfig()]);
|
|
104
|
+
const ok = await service.reconnectServer("unknown");
|
|
105
|
+
expect(ok).toBe(false);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("closes the existing client and reconnects successfully", async () => {
|
|
109
|
+
const service = new MCPService([makeConfig()]);
|
|
110
|
+
await service.connect();
|
|
111
|
+
clientImpl.close.mockClear();
|
|
112
|
+
const ok = await service.reconnectServer("test-server");
|
|
113
|
+
expect(ok).toBe(true);
|
|
114
|
+
expect(clientImpl.close).toHaveBeenCalledTimes(1);
|
|
115
|
+
expect(service.getServerStatus()).toEqual([{connected: true, name: "test-server"}]);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("ignores errors from closing the existing client during reconnect", async () => {
|
|
119
|
+
const service = new MCPService([makeConfig()]);
|
|
120
|
+
await service.connect();
|
|
121
|
+
clientImpl.close.mockImplementationOnce(async () => {
|
|
122
|
+
throw new Error("already closed");
|
|
123
|
+
});
|
|
124
|
+
const ok = await service.reconnectServer("test-server");
|
|
125
|
+
expect(ok).toBe(true);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("returns false when the reconnect attempt itself fails", async () => {
|
|
129
|
+
const service = new MCPService([makeConfig()]);
|
|
130
|
+
await service.connect();
|
|
131
|
+
createMCPClientMock.mockImplementationOnce(async () => {
|
|
132
|
+
throw new Error("boom");
|
|
133
|
+
});
|
|
134
|
+
const ok = await service.reconnectServer("test-server");
|
|
135
|
+
expect(ok).toBe(false);
|
|
136
|
+
expect(service.getServerStatus()).toEqual([{connected: false, name: "test-server"}]);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
});
|
package/tsconfig.json
CHANGED
|
@@ -26,6 +26,13 @@
|
|
|
26
26
|
"strictNullChecks": true,
|
|
27
27
|
"target": "es5"
|
|
28
28
|
},
|
|
29
|
-
"exclude": [
|
|
29
|
+
"exclude": [
|
|
30
|
+
"node_modules",
|
|
31
|
+
"src/**/*.test.ts",
|
|
32
|
+
"src/**/*.test.tsx",
|
|
33
|
+
"src/**/*.isolated.ts",
|
|
34
|
+
"src/**/*.isolated.tsx",
|
|
35
|
+
"src/tests"
|
|
36
|
+
],
|
|
30
37
|
"include": ["src/**/*.ts*", "src/*.ts", "types.d.ts"]
|
|
31
38
|
}
|