@terreno/ai 0.20.2 → 0.22.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/.github/copilot-instructions.md +1 -1
- package/AGENTS.md +1 -1
- package/package.json +5 -4
- package/src/aiApp.test.ts +25 -42
- package/src/langfuseApp.test.ts +4 -4
- package/src/langfuseRoutes.test.ts +24 -46
- package/src/routes/files.test.ts +10 -39
- package/src/routes/mcp.test.ts +9 -41
- package/src/routes/projects.test.ts +8 -40
- package/src/routes/routes.test.ts +90 -126
- package/src/service/gemini.test.ts +60 -0
- package/src/service/parseAiJson.test.ts +159 -0
- package/src/service/vertex.test.ts +113 -0
- package/src/tests/bunSetup.ts +12 -178
- package/src/tests/helpers.ts +81 -0
|
@@ -63,6 +63,165 @@ describe("parseAiJson", () => {
|
|
|
63
63
|
expect(r.raw).toBe("not json at all");
|
|
64
64
|
}
|
|
65
65
|
});
|
|
66
|
+
|
|
67
|
+
it("returns failure for empty string", () => {
|
|
68
|
+
const r = parseAiJson("");
|
|
69
|
+
expect(r.success).toBe(false);
|
|
70
|
+
if (!r.success) {
|
|
71
|
+
expect(r.error).toBe("Empty or non-string input");
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("returns failure for whitespace-only string", () => {
|
|
76
|
+
const r = parseAiJson(" ");
|
|
77
|
+
expect(r.success).toBe(false);
|
|
78
|
+
if (!r.success) {
|
|
79
|
+
expect(r.error).toBe("Empty or non-string input");
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("handles escaped characters inside JSON strings during balanced extraction", () => {
|
|
84
|
+
const r = parseAiJson(`preamble {"msg":"line1\\nline2","path":"C:\\\\dir"} tail`);
|
|
85
|
+
expect(r.success).toBe(true);
|
|
86
|
+
if (r.success) {
|
|
87
|
+
expect(r.data).toEqual({msg: "line1\nline2", path: "C:\\dir"});
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("handles escaped quotes inside JSON strings during balanced extraction", () => {
|
|
92
|
+
const r = parseAiJson(`noise {"key":"value with \\"quotes\\""} done`);
|
|
93
|
+
expect(r.success).toBe(true);
|
|
94
|
+
if (r.success) {
|
|
95
|
+
expect(r.data).toEqual({key: 'value with "quotes"'});
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("returns failure for mismatched brackets", () => {
|
|
100
|
+
const r = parseAiJson("{]");
|
|
101
|
+
expect(r.success).toBe(false);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("returns failure for unclosed JSON (unbalanced brackets)", () => {
|
|
105
|
+
const r = parseAiJson(`{"a": {"b": 1}`);
|
|
106
|
+
expect(r.success).toBe(false);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("repairs trailing comma inside a balanced extraction that otherwise fails parse", () => {
|
|
110
|
+
const r = parseAiJson(`Sure! {"items": [1, 2, 3,],} enjoy!`);
|
|
111
|
+
expect(r.success).toBe(true);
|
|
112
|
+
if (r.success) {
|
|
113
|
+
expect(r.repaired).toBe(true);
|
|
114
|
+
expect(r.data).toEqual({items: [1, 2, 3]});
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("repairs trailing commas inside balanced extraction with backslash content", () => {
|
|
119
|
+
const input = `here {"a": "x\\\\y", "b": 1,}`;
|
|
120
|
+
const r = parseAiJson(input);
|
|
121
|
+
expect(r.success).toBe(true);
|
|
122
|
+
if (r.success) {
|
|
123
|
+
expect(r.repaired).toBe(true);
|
|
124
|
+
expect(r.data).toEqual({a: "x\\y", b: 1});
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("repairs smart quotes via whole-string repair when balanced extraction fails", () => {
|
|
129
|
+
const r = parseAiJson(`\u201Ckey\u201D: \u201Cvalue\u201D`);
|
|
130
|
+
expect(r.success).toBe(false);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("repairs smart quotes on a full JSON object", () => {
|
|
134
|
+
const r = parseAiJson(`{\u201Ckey\u201D: \u201Cvalue\u201D}`);
|
|
135
|
+
expect(r.success).toBe(true);
|
|
136
|
+
if (r.success) {
|
|
137
|
+
expect(r.repaired).toBe(true);
|
|
138
|
+
expect(r.data).toEqual({key: "value"});
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("repairs escaped characters in repair path outside strings", () => {
|
|
143
|
+
const r = parseAiJson(`{"escaped": "a\\b", "trailing": true,}`);
|
|
144
|
+
expect(r.success).toBe(true);
|
|
145
|
+
if (r.success) {
|
|
146
|
+
expect(r.repaired).toBe(true);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("strips partial opening fence and trailing fence", () => {
|
|
151
|
+
const r = parseAiJson('```json\n{"x": 1}\n```');
|
|
152
|
+
expect(r.success).toBe(true);
|
|
153
|
+
if (r.success) {
|
|
154
|
+
expect(r.data).toEqual({x: 1});
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("strips partial opening fence without full closure", () => {
|
|
159
|
+
const r = parseAiJson('```json\n{"a":1}');
|
|
160
|
+
expect(r.success).toBe(true);
|
|
161
|
+
if (r.success) {
|
|
162
|
+
expect(r.data).toEqual({a: 1});
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("strips opening fence with colon separator", () => {
|
|
167
|
+
const r = parseAiJson('```json:\n{"y": 2}\n```');
|
|
168
|
+
expect(r.success).toBe(true);
|
|
169
|
+
if (r.success) {
|
|
170
|
+
expect(r.data).toEqual({y: 2});
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("parses arrays at top level", () => {
|
|
175
|
+
const r = parseAiJson("[1, 2, 3]");
|
|
176
|
+
expect(r.success).toBe(true);
|
|
177
|
+
if (r.success) {
|
|
178
|
+
expect(r.data).toEqual([1, 2, 3]);
|
|
179
|
+
expect(r.repaired).toBe(false);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("extracts a JSON array from prose", () => {
|
|
184
|
+
const r = parseAiJson("Here are the items: [1, 2, 3] — done.");
|
|
185
|
+
expect(r.success).toBe(true);
|
|
186
|
+
if (r.success) {
|
|
187
|
+
expect(r.data).toEqual([1, 2, 3]);
|
|
188
|
+
expect(r.repaired).toBe(true);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("returns failure when no brackets are present at all", () => {
|
|
193
|
+
const r = parseAiJson("just plain text with no JSON");
|
|
194
|
+
expect(r.success).toBe(false);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("returns failure for unterminated JSON", () => {
|
|
198
|
+
const r = parseAiJson('{"a": 1');
|
|
199
|
+
expect(r.success).toBe(false);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("repairs smart quotes inside a balanced extraction (repairedSlice path)", () => {
|
|
203
|
+
const r = parseAiJson(`prefix {\u201Ckey\u201D: 1} suffix`);
|
|
204
|
+
expect(r.success).toBe(true);
|
|
205
|
+
if (r.success) {
|
|
206
|
+
expect(r.repaired).toBe(true);
|
|
207
|
+
expect(r.data).toEqual({key: 1});
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("repairs whole cleaned text when balanced extraction is not possible", () => {
|
|
212
|
+
const r = parseAiJson(`\u201Chello\u201D`);
|
|
213
|
+
expect(r.success).toBe(true);
|
|
214
|
+
if (r.success) {
|
|
215
|
+
expect(r.repaired).toBe(true);
|
|
216
|
+
expect(r.data).toBe("hello");
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("falls through balanced extraction when neither direct nor repaired parse succeeds", () => {
|
|
221
|
+
// extractBalancedJson finds {invalid: json broken} but neither parse nor repair can fix it
|
|
222
|
+
const r = parseAiJson("prefix {invalid: json broken} suffix");
|
|
223
|
+
expect(r.success).toBe(false);
|
|
224
|
+
});
|
|
66
225
|
});
|
|
67
226
|
|
|
68
227
|
describe("normalizeLlmJsonTextForStructuredOutput", () => {
|
|
@@ -325,5 +325,118 @@ describe("vertex helpers", () => {
|
|
|
325
325
|
});
|
|
326
326
|
expect(models).toBeUndefined();
|
|
327
327
|
});
|
|
328
|
+
it("skips models without a name in the response", async () => {
|
|
329
|
+
const fetchImpl = mock(async () => ({
|
|
330
|
+
json: async () => ({
|
|
331
|
+
publisherModels: [
|
|
332
|
+
{name: "publishers/google/models/gemini-2.5-flash"},
|
|
333
|
+
{},
|
|
334
|
+
{name: undefined},
|
|
335
|
+
{name: "publishers/google/models/gemini-2.5-pro"},
|
|
336
|
+
],
|
|
337
|
+
}),
|
|
338
|
+
ok: true,
|
|
339
|
+
status: 200,
|
|
340
|
+
})) as unknown as typeof fetch;
|
|
341
|
+
const models = await listEnabledVertexModels({
|
|
342
|
+
fetchImpl,
|
|
343
|
+
getAccessToken: async () => "fake-token",
|
|
344
|
+
project: "demo-project",
|
|
345
|
+
});
|
|
346
|
+
expect(models).toEqual(["gemini-2.5-flash", "gemini-2.5-pro"]);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it("uses global location when GOOGLE_VERTEX_LOCATION env is set", async () => {
|
|
350
|
+
process.env.GOOGLE_VERTEX_LOCATION = "europe-west4";
|
|
351
|
+
const fetchImpl = mock(async (input: string | URL) => {
|
|
352
|
+
const url = input.toString();
|
|
353
|
+
expect(url).toContain("europe-west4-aiplatform.googleapis.com");
|
|
354
|
+
return {
|
|
355
|
+
json: async () => ({
|
|
356
|
+
publisherModels: [{name: "publishers/google/models/gemini-2.5-flash"}],
|
|
357
|
+
}),
|
|
358
|
+
ok: true,
|
|
359
|
+
status: 200,
|
|
360
|
+
} as unknown as Response;
|
|
361
|
+
});
|
|
362
|
+
const models = await listEnabledVertexModels({
|
|
363
|
+
fetchImpl: fetchImpl as unknown as typeof fetch,
|
|
364
|
+
getAccessToken: async () => "fake-token",
|
|
365
|
+
project: "demo-project",
|
|
366
|
+
});
|
|
367
|
+
expect(models).toEqual(["gemini-2.5-flash"]);
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
describe("createVertexProvider", () => {
|
|
372
|
+
it("returns undefined and logs a warning when factory is not available and module is not installed", () => {
|
|
373
|
+
const provider = createVertexProvider({
|
|
374
|
+
project: "demo-project",
|
|
375
|
+
vertexFactory: undefined,
|
|
376
|
+
});
|
|
377
|
+
// loadVertexModule returns undefined because @ai-sdk/google-vertex is not installed in test env
|
|
378
|
+
expect(provider).toBeUndefined();
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it("uses GOOGLE_VERTEX_LOCATION env for location resolution", () => {
|
|
382
|
+
process.env.GOOGLE_VERTEX_LOCATION = "asia-east1";
|
|
383
|
+
const provider = createVertexProvider({
|
|
384
|
+
project: "demo-project",
|
|
385
|
+
vertexFactory: makeVertexFactory(),
|
|
386
|
+
});
|
|
387
|
+
expect(provider?.location).toBe("asia-east1");
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
it("uses GOOGLE_VERTEX_PROJECT env when project option is omitted", () => {
|
|
391
|
+
process.env.GOOGLE_VERTEX_PROJECT = "env-project";
|
|
392
|
+
const provider = createVertexProvider({
|
|
393
|
+
vertexFactory: makeVertexFactory(),
|
|
394
|
+
});
|
|
395
|
+
expect(provider).toBeDefined();
|
|
396
|
+
expect(provider?.project).toBe("env-project");
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it("exposes the raw provider", () => {
|
|
400
|
+
const provider = createVertexProvider({
|
|
401
|
+
project: "demo-project",
|
|
402
|
+
vertexFactory: makeVertexFactory(),
|
|
403
|
+
});
|
|
404
|
+
expect(provider?.raw).toBeDefined();
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it("resolves image models through the allow-list", () => {
|
|
408
|
+
const provider = createVertexProvider({
|
|
409
|
+
project: "demo-project",
|
|
410
|
+
vertexFactory: makeVertexFactory(),
|
|
411
|
+
});
|
|
412
|
+
const img = provider?.imageModel("imagen-4.0-fast-generate-001");
|
|
413
|
+
expect(img).toBeDefined();
|
|
414
|
+
});
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
describe("verifyVertexModelsEnabled", () => {
|
|
418
|
+
it("deduplicates requested models before checking", async () => {
|
|
419
|
+
const result = await verifyVertexModelsEnabled({
|
|
420
|
+
listModelsFn: mock(async () => ["gemini-2.5-flash"]),
|
|
421
|
+
models: ["gemini-2.5-flash", "gemini-2.5-flash@001"],
|
|
422
|
+
project: "demo-project",
|
|
423
|
+
});
|
|
424
|
+
expect(result.checked).toBe(true);
|
|
425
|
+
expect(result.available).toEqual(["gemini-2.5-flash"]);
|
|
426
|
+
expect(result.unavailable).toEqual([]);
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
describe("assertVertexModelsEnabled", () => {
|
|
431
|
+
it("returns the result when all models are available", async () => {
|
|
432
|
+
const result = await assertVertexModelsEnabled({
|
|
433
|
+
listModelsFn: mock(async () => ["gemini-2.5-flash", "gemini-2.5-pro"]),
|
|
434
|
+
models: ["gemini-2.5-flash"],
|
|
435
|
+
project: "demo-project",
|
|
436
|
+
});
|
|
437
|
+
expect(result.checked).toBe(true);
|
|
438
|
+
expect(result.available).toEqual(["gemini-2.5-flash"]);
|
|
439
|
+
expect(result.unavailable).toEqual([]);
|
|
440
|
+
});
|
|
328
441
|
});
|
|
329
442
|
});
|
package/src/tests/bunSetup.ts
CHANGED
|
@@ -1,111 +1,21 @@
|
|
|
1
1
|
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
2
|
-
import {
|
|
3
|
-
import {Writable} from "node:stream";
|
|
4
|
-
import {setupEnvironment, winstonLogger} from "@terreno/api";
|
|
5
|
-
import mongoose from "mongoose";
|
|
6
|
-
import winston from "winston";
|
|
2
|
+
import {registerSimpleMongoPreload} from "@terreno/test";
|
|
7
3
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
console.error("Failed to connect to MongoDB:", err);
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// Close MongoDB connection after all tests
|
|
22
|
-
if (shouldConnectToTestDb) {
|
|
23
|
-
afterAll(async () => {
|
|
24
|
-
await mongoose.connection.close();
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
let logs: string[] = [];
|
|
29
|
-
|
|
30
|
-
const SHOW_ALL_LOGS = process.env.SHOW_ALL_TEST_LOGS === "true";
|
|
31
|
-
|
|
32
|
-
// Create a custom stream that captures logs
|
|
33
|
-
const logStream = new Writable({
|
|
34
|
-
write(chunk: unknown, _encoding: unknown, callback: () => void) {
|
|
35
|
-
logs.push(String(chunk));
|
|
36
|
-
if (SHOW_ALL_LOGS) {
|
|
37
|
-
process.stdout.write(String(chunk));
|
|
38
|
-
}
|
|
39
|
-
callback();
|
|
4
|
+
registerSimpleMongoPreload({
|
|
5
|
+
defaultLocalMongoUri: "mongodb://127.0.0.1/terreno-ai-test?&connectTimeoutMS=360000",
|
|
6
|
+
onBeforeEach: async () => {
|
|
7
|
+
const {shutdownLangfuseClient} = await import("../langfuseClient");
|
|
8
|
+
await shutdownLangfuseClient();
|
|
9
|
+
},
|
|
10
|
+
testEnv: {
|
|
11
|
+
tokenIssuer: "terreno-ai.test",
|
|
40
12
|
},
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
// Silence both winston loggers by replacing all transports with our capturing stream
|
|
44
|
-
const silentTransport = new winston.transports.Stream({
|
|
45
|
-
format: winston.format.simple(),
|
|
46
|
-
stream: logStream,
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// Clear and silence the default winston logger
|
|
50
|
-
winston.clear();
|
|
51
|
-
winston.add(silentTransport);
|
|
52
|
-
|
|
53
|
-
// Clear and silence the custom winstonLogger
|
|
54
|
-
winstonLogger.clear();
|
|
55
|
-
winstonLogger.add(silentTransport);
|
|
56
|
-
|
|
57
|
-
// Capture and silence console methods
|
|
58
|
-
const originalConsole = {
|
|
59
|
-
debug: console.debug,
|
|
60
|
-
error: console.error,
|
|
61
|
-
info: console.info,
|
|
62
|
-
// biome-ignore lint/suspicious/noConsole: We keep the original reference.
|
|
63
|
-
log: console.log,
|
|
64
|
-
warn: console.warn,
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const captureConsoleMethod = (method: keyof typeof originalConsole): void => {
|
|
68
|
-
(console as any)[method] = (...args: any[]) => {
|
|
69
|
-
const logMessage = `[console.${method}] ${args.map((arg) => (typeof arg === "object" ? JSON.stringify(arg) : String(arg))).join(" ")}`;
|
|
70
|
-
logs.push(logMessage);
|
|
71
|
-
if (SHOW_ALL_LOGS) {
|
|
72
|
-
originalConsole[method](...args);
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
captureConsoleMethod("log");
|
|
78
|
-
captureConsoleMethod("info");
|
|
79
|
-
captureConsoleMethod("warn");
|
|
80
|
-
captureConsoleMethod("error");
|
|
81
|
-
captureConsoleMethod("debug");
|
|
82
|
-
|
|
83
|
-
// Setup before each test
|
|
84
|
-
beforeEach(() => {
|
|
85
|
-
process.env.TOKEN_SECRET = "secret";
|
|
86
|
-
process.env.TOKEN_ISSUER = "terreno-ai.test";
|
|
87
|
-
process.env.SESSION_SECRET = "sessionSecret";
|
|
88
|
-
process.env.REFRESH_TOKEN_SECRET = "refreshTokenSecret";
|
|
89
|
-
setupEnvironment();
|
|
90
|
-
// Re-silence loggers after setupEnvironment which may reconfigure them
|
|
91
|
-
winston.clear();
|
|
92
|
-
winston.add(silentTransport);
|
|
93
|
-
winstonLogger.clear();
|
|
94
|
-
winstonLogger.add(silentTransport);
|
|
95
|
-
logs = [];
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
// Clear logs after each test
|
|
99
|
-
afterEach(() => {
|
|
100
|
-
logs = [];
|
|
101
13
|
});
|
|
102
14
|
|
|
103
15
|
// Mock @langfuse/client globally so the real `./langfuseClient` module runs
|
|
104
|
-
// without making network calls.
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
// here, rather than mocking `./langfuseClient` per-test-file, prevents the
|
|
108
|
-
// `mock.module()` global cache from leaking different shapes across files.
|
|
16
|
+
// without making network calls.
|
|
17
|
+
import {mock} from "bun:test";
|
|
18
|
+
|
|
109
19
|
mock.module("@langfuse/client", () => {
|
|
110
20
|
return {
|
|
111
21
|
LangfuseClient: class FakeLangfuseClient {
|
|
@@ -151,79 +61,3 @@ mock.module("@langfuse/client", () => {
|
|
|
151
61
|
},
|
|
152
62
|
};
|
|
153
63
|
});
|
|
154
|
-
|
|
155
|
-
// Ensure no langfuse client instance leaks across tests in different files.
|
|
156
|
-
// The real langfuseInstance module variable is reset to null between tests.
|
|
157
|
-
beforeEach(async () => {
|
|
158
|
-
const {shutdownLangfuseClient} = await import("../langfuseClient");
|
|
159
|
-
await shutdownLangfuseClient();
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
// Mock @sentry/bun module
|
|
163
|
-
mock.module("@sentry/bun", () => {
|
|
164
|
-
const mockFn = (): ReturnType<typeof mock> => mock(() => {});
|
|
165
|
-
|
|
166
|
-
const mockScope = {
|
|
167
|
-
addBreadcrumb: mockFn(),
|
|
168
|
-
clear: mockFn(),
|
|
169
|
-
getSpan: mockFn(),
|
|
170
|
-
setContext: mockFn(),
|
|
171
|
-
setFingerprint: mockFn(),
|
|
172
|
-
setLevel: mockFn(),
|
|
173
|
-
setSpan: mockFn(),
|
|
174
|
-
setTag: mockFn(),
|
|
175
|
-
setTags: mockFn(),
|
|
176
|
-
setTransactionName: mockFn(),
|
|
177
|
-
setUser: mockFn(),
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
const mockClient = {
|
|
181
|
-
captureException: mockFn(),
|
|
182
|
-
captureMessage: mockFn(),
|
|
183
|
-
close: mock(() => Promise.resolve(true)),
|
|
184
|
-
flush: mock(() => Promise.resolve(true)),
|
|
185
|
-
getOptions: mock(() => ({})),
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
return {
|
|
189
|
-
addBreadcrumb: mockFn(),
|
|
190
|
-
captureException: mockFn(),
|
|
191
|
-
captureMessage: mockFn(),
|
|
192
|
-
clearScope: mockFn(),
|
|
193
|
-
close: mock(() => Promise.resolve(true)),
|
|
194
|
-
configureScope: mockFn(),
|
|
195
|
-
default: {
|
|
196
|
-
captureException: mockFn(),
|
|
197
|
-
captureMessage: mockFn(),
|
|
198
|
-
Handlers: {
|
|
199
|
-
errorHandler: mock(
|
|
200
|
-
() => (err: unknown, _req: unknown, _res: unknown, next: (e?: unknown) => void) =>
|
|
201
|
-
next(err)
|
|
202
|
-
),
|
|
203
|
-
requestHandler: mock(() => (_req: unknown, _res: unknown, next: () => void) => next()),
|
|
204
|
-
tracingHandler: mock(() => (_req: unknown, _res: unknown, next: () => void) => next()),
|
|
205
|
-
},
|
|
206
|
-
init: mockFn(),
|
|
207
|
-
isInitialized: mock(() => true),
|
|
208
|
-
setupExpressErrorHandler: mockFn(),
|
|
209
|
-
},
|
|
210
|
-
flush: mock(() => Promise.resolve(true)),
|
|
211
|
-
getClient: mock(() => mockClient),
|
|
212
|
-
getCurrentScope: mock(() => mockScope),
|
|
213
|
-
Handlers: {
|
|
214
|
-
errorHandler: mock(
|
|
215
|
-
() => (err: unknown, _req: unknown, _res: unknown, next: (e?: unknown) => void) => next(err)
|
|
216
|
-
),
|
|
217
|
-
requestHandler: mock(() => (_req: unknown, _res: unknown, next: () => void) => next()),
|
|
218
|
-
tracingHandler: mock(() => (_req: unknown, _res: unknown, next: () => void) => next()),
|
|
219
|
-
},
|
|
220
|
-
init: mockFn(),
|
|
221
|
-
isInitialized: mock(() => true),
|
|
222
|
-
setContext: mockFn(),
|
|
223
|
-
setTag: mockFn(),
|
|
224
|
-
setTags: mockFn(),
|
|
225
|
-
setUser: mockFn(),
|
|
226
|
-
setupExpressErrorHandler: mockFn(),
|
|
227
|
-
withScope: mock((callback: (scope: typeof mockScope) => void) => callback(mockScope)),
|
|
228
|
-
};
|
|
229
|
-
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import {createdUpdatedPlugin} from "@terreno/api";
|
|
2
|
+
import {authAsUser as authAsUserWithCredentials} from "@terreno/test";
|
|
3
|
+
import type express from "express";
|
|
4
|
+
import mongoose, {type Model} from "mongoose";
|
|
5
|
+
import passportLocalMongoose from "passport-local-mongoose";
|
|
6
|
+
import type TestAgent from "supertest/lib/agent";
|
|
7
|
+
|
|
8
|
+
export type PasswordedUser = {setPassword: (password: string) => Promise<void>};
|
|
9
|
+
|
|
10
|
+
export interface AiTestUser {
|
|
11
|
+
admin?: boolean;
|
|
12
|
+
email: string;
|
|
13
|
+
name: string;
|
|
14
|
+
password: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface AiTestUserDoc {
|
|
18
|
+
admin?: boolean;
|
|
19
|
+
email: string;
|
|
20
|
+
name?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const createTestUserModel = (): Model<AiTestUserDoc> => {
|
|
24
|
+
if (mongoose.models.User) {
|
|
25
|
+
return mongoose.models.User as Model<AiTestUserDoc>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const userSchema = new mongoose.Schema({
|
|
29
|
+
admin: {default: false, type: Boolean},
|
|
30
|
+
email: {index: true, type: String},
|
|
31
|
+
name: String,
|
|
32
|
+
});
|
|
33
|
+
userSchema.plugin(
|
|
34
|
+
passportLocalMongoose as unknown as (
|
|
35
|
+
schema: mongoose.Schema,
|
|
36
|
+
options: {usernameField: string}
|
|
37
|
+
) => void,
|
|
38
|
+
{usernameField: "email"}
|
|
39
|
+
);
|
|
40
|
+
userSchema.plugin(createdUpdatedPlugin);
|
|
41
|
+
return mongoose.model<AiTestUserDoc>("User", userSchema);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const UserModel = createTestUserModel();
|
|
45
|
+
|
|
46
|
+
export const STANDARD_AI_TEST_USERS = {
|
|
47
|
+
admin: {admin: true, email: "admin@example.com", name: "Admin", password: "securePassword"},
|
|
48
|
+
notAdmin: {admin: false, email: "notAdmin@example.com", name: "User", password: "password"},
|
|
49
|
+
} as const;
|
|
50
|
+
|
|
51
|
+
export type StandardAiTestUserRole = keyof typeof STANDARD_AI_TEST_USERS;
|
|
52
|
+
|
|
53
|
+
export const ensureTestUsers = async (
|
|
54
|
+
users: AiTestUser[] = [STANDARD_AI_TEST_USERS.admin, STANDARD_AI_TEST_USERS.notAdmin]
|
|
55
|
+
): Promise<AiTestUserDoc[]> => {
|
|
56
|
+
await UserModel.deleteMany({email: {$in: users.map((user) => user.email)}});
|
|
57
|
+
|
|
58
|
+
const createdUsers: AiTestUserDoc[] = [];
|
|
59
|
+
for (const user of users) {
|
|
60
|
+
const doc = await UserModel.create({
|
|
61
|
+
admin: user.admin ?? false,
|
|
62
|
+
email: user.email,
|
|
63
|
+
name: user.name,
|
|
64
|
+
});
|
|
65
|
+
await (doc as unknown as PasswordedUser).setPassword(user.password);
|
|
66
|
+
await doc.save();
|
|
67
|
+
createdUsers.push(doc);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return createdUsers;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export const authAsUser = async (
|
|
74
|
+
app: express.Application,
|
|
75
|
+
type: StandardAiTestUserRole
|
|
76
|
+
): Promise<TestAgent> => {
|
|
77
|
+
const preset = STANDARD_AI_TEST_USERS[type];
|
|
78
|
+
return authAsUserWithCredentials(app, {email: preset.email, password: preset.password});
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export {authAsUserWithCredentials};
|