@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
|
@@ -9,7 +9,7 @@ A monorepo containing shared packages for building full-stack applications with
|
|
|
9
9
|
- **rtk/** - Redux Toolkit Query utilities for API backends (`@terreno/rtk`)
|
|
10
10
|
- **admin-backend/** - Admin panel backend plugin for @terreno/api (`@terreno/admin-backend`)
|
|
11
11
|
- **admin-frontend/** - Admin panel frontend screens for @terreno/api backends (`@terreno/admin-frontend`)
|
|
12
|
-
- **mcp-server/** - MCP server for AI assistant integration (`@terreno/mcp-
|
|
12
|
+
- **mcp-server/** - MCP server for AI assistant integration (`@terreno/mcp`, bins `terreno-mcp` / `terreno-mcp-local`)
|
|
13
13
|
- **demo/** - Demo app for showcasing and testing UI components
|
|
14
14
|
- **example-frontend/** - Example Expo app demonstrating full stack usage
|
|
15
15
|
- **example-backend/** - Example Express backend using @terreno/api
|
package/AGENTS.md
CHANGED
|
@@ -9,7 +9,7 @@ A monorepo containing shared packages for building full-stack applications with
|
|
|
9
9
|
- **rtk/** - Redux Toolkit Query utilities for API backends (`@terreno/rtk`)
|
|
10
10
|
- **admin-backend/** - Admin panel backend plugin for @terreno/api (`@terreno/admin-backend`)
|
|
11
11
|
- **admin-frontend/** - Admin panel frontend screens for @terreno/api backends (`@terreno/admin-frontend`)
|
|
12
|
-
- **mcp-server/** - MCP server for AI assistant integration (`@terreno/mcp-
|
|
12
|
+
- **mcp-server/** - MCP server for AI assistant integration (`@terreno/mcp`, bins `terreno-mcp` / `terreno-mcp-local`)
|
|
13
13
|
- **demo/** - Demo app for showcasing and testing UI components
|
|
14
14
|
- **example-frontend/** - Example Expo app demonstrating full stack usage
|
|
15
15
|
- **example-backend/** - Example Express backend using @terreno/api
|
package/package.json
CHANGED
|
@@ -13,18 +13,19 @@
|
|
|
13
13
|
"ai": "^6.0.97",
|
|
14
14
|
"google-auth-library": "^10.5.0",
|
|
15
15
|
"luxon": "^3.7.2",
|
|
16
|
-
"multer": "
|
|
16
|
+
"multer": "2.2.0"
|
|
17
17
|
},
|
|
18
18
|
"description": "AI service layer for Terreno apps - GPT chat, request logging, and admin tools.",
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@biomejs/biome": "^2.3.6",
|
|
21
|
+
"@terreno/test": "0.22.1",
|
|
21
22
|
"@types/bun": "^1.2.4",
|
|
22
23
|
"@types/express": "^5.0.6",
|
|
23
24
|
"@types/luxon": "^3.7.1",
|
|
24
25
|
"@types/multer": "^1.4.12",
|
|
25
26
|
"@types/passport-local-mongoose": "^6.1.5",
|
|
26
27
|
"@types/supertest": "^6.0.2",
|
|
27
|
-
"@terreno/api": "0.
|
|
28
|
+
"@terreno/api": "0.22.1",
|
|
28
29
|
"mongoose": "8.23.0",
|
|
29
30
|
"passport-local-mongoose": "^9.0.1",
|
|
30
31
|
"supertest": "^7.0.0",
|
|
@@ -71,7 +72,7 @@
|
|
|
71
72
|
"access": "public"
|
|
72
73
|
},
|
|
73
74
|
"peerDependencies": {
|
|
74
|
-
"@terreno/api": "0.
|
|
75
|
+
"@terreno/api": "0.22.1",
|
|
75
76
|
"mongoose": "^8.0.0"
|
|
76
77
|
},
|
|
77
78
|
"repository": {
|
|
@@ -90,5 +91,5 @@
|
|
|
90
91
|
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
91
92
|
},
|
|
92
93
|
"types": "dist/index.d.ts",
|
|
93
|
-
"version": "0.
|
|
94
|
+
"version": "0.22.1"
|
|
94
95
|
}
|
package/src/aiApp.test.ts
CHANGED
|
@@ -1,31 +1,20 @@
|
|
|
1
1
|
import {beforeAll, describe, expect, it, mock} from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import {TerrenoApp} from "@terreno/api";
|
|
3
3
|
import type {LanguageModel} from "ai";
|
|
4
4
|
import type express from "express";
|
|
5
|
-
import mongoose from "mongoose";
|
|
6
|
-
import passportLocalMongoose from "passport-local-mongoose";
|
|
7
5
|
import supertest from "supertest";
|
|
8
6
|
|
|
9
7
|
import {AiApp} from "./aiApp";
|
|
10
8
|
import type {FileStorageService} from "./service/fileStorage";
|
|
11
9
|
import type {MCPService} from "./service/mcpService";
|
|
10
|
+
import {authAsUserWithCredentials, ensureTestUsers, UserModel} from "./tests/helpers";
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
});
|
|
20
|
-
userSchema.plugin(
|
|
21
|
-
passportLocalMongoose as unknown as (
|
|
22
|
-
schema: mongoose.Schema,
|
|
23
|
-
options: {usernameField: string}
|
|
24
|
-
) => void,
|
|
25
|
-
{usernameField: "email"}
|
|
26
|
-
);
|
|
27
|
-
userSchema.plugin(createdUpdatedPlugin);
|
|
28
|
-
const UserModel = mongoose.models.User || mongoose.model("User", userSchema);
|
|
12
|
+
const AI_APP_TEST_USER = {
|
|
13
|
+
admin: false,
|
|
14
|
+
email: "aiapp@example.com",
|
|
15
|
+
name: "User",
|
|
16
|
+
password: "password",
|
|
17
|
+
} as const;
|
|
29
18
|
|
|
30
19
|
const createMockModel = () => ({
|
|
31
20
|
doGenerate: mock(async () => ({
|
|
@@ -53,31 +42,25 @@ const createMockModel = () => ({
|
|
|
53
42
|
|
|
54
43
|
describe("AiApp", () => {
|
|
55
44
|
beforeAll(async () => {
|
|
56
|
-
await
|
|
57
|
-
const user = await UserModel.create({email: "aiapp@example.com", name: "User"});
|
|
58
|
-
await (user as unknown as PasswordedUser).setPassword("password");
|
|
59
|
-
await user.save();
|
|
45
|
+
await ensureTestUsers([AI_APP_TEST_USER]);
|
|
60
46
|
});
|
|
61
47
|
|
|
62
48
|
const authAsUser = async (app: express.Application) => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
.
|
|
66
|
-
|
|
67
|
-
.expect(200);
|
|
68
|
-
await agent.set("authorization", `Bearer ${res.body.data.token}`);
|
|
69
|
-
return agent;
|
|
49
|
+
return authAsUserWithCredentials(app, {
|
|
50
|
+
email: AI_APP_TEST_USER.email,
|
|
51
|
+
password: AI_APP_TEST_USER.password,
|
|
52
|
+
});
|
|
70
53
|
};
|
|
71
54
|
|
|
72
55
|
it("registers gpt and project routes by default", async () => {
|
|
73
56
|
const {AIService} = await import("./service/aiService");
|
|
74
57
|
const aiService = new AIService({model: createMockModel() as unknown as LanguageModel});
|
|
75
58
|
const plugin = new AiApp({aiService});
|
|
76
|
-
const app =
|
|
77
|
-
|
|
59
|
+
const app = new TerrenoApp({
|
|
60
|
+
configureApp: (router) => plugin.register(router as unknown as express.Application),
|
|
78
61
|
skipListen: true,
|
|
79
62
|
userModel: UserModel,
|
|
80
|
-
});
|
|
63
|
+
}).build();
|
|
81
64
|
|
|
82
65
|
const agent = await authAsUser(app);
|
|
83
66
|
const tools = await agent.get("/gpt/tools");
|
|
@@ -101,11 +84,11 @@ describe("AiApp", () => {
|
|
|
101
84
|
})),
|
|
102
85
|
} as unknown as FileStorageService;
|
|
103
86
|
const plugin = new AiApp({fileStorageService, gcsBucket: "test-bucket"});
|
|
104
|
-
const app =
|
|
105
|
-
|
|
87
|
+
const app = new TerrenoApp({
|
|
88
|
+
configureApp: (router) => plugin.register(router as unknown as express.Application),
|
|
106
89
|
skipListen: true,
|
|
107
90
|
userModel: UserModel,
|
|
108
|
-
});
|
|
91
|
+
}).build();
|
|
109
92
|
// Unauthenticated access to upload route: ensure it exists (returns 401, not 404).
|
|
110
93
|
const upload = await supertest(app).post("/files/upload");
|
|
111
94
|
expect(upload.status).not.toBe(404);
|
|
@@ -122,11 +105,11 @@ describe("AiApp", () => {
|
|
|
122
105
|
})),
|
|
123
106
|
} as unknown as FileStorageService;
|
|
124
107
|
const plugin = new AiApp({fileStorageService});
|
|
125
|
-
const app =
|
|
126
|
-
|
|
108
|
+
const app = new TerrenoApp({
|
|
109
|
+
configureApp: (router) => plugin.register(router as unknown as express.Application),
|
|
127
110
|
skipListen: true,
|
|
128
111
|
userModel: UserModel,
|
|
129
|
-
});
|
|
112
|
+
}).build();
|
|
130
113
|
const upload = await supertest(app).post("/files/upload");
|
|
131
114
|
expect(upload.status).toBe(404);
|
|
132
115
|
});
|
|
@@ -138,11 +121,11 @@ describe("AiApp", () => {
|
|
|
138
121
|
reconnectServer: mock(async () => {}),
|
|
139
122
|
} as unknown as MCPService;
|
|
140
123
|
const plugin = new AiApp({mcpService});
|
|
141
|
-
const app =
|
|
142
|
-
|
|
124
|
+
const app = new TerrenoApp({
|
|
125
|
+
configureApp: (router) => plugin.register(router as unknown as express.Application),
|
|
143
126
|
skipListen: true,
|
|
144
127
|
userModel: UserModel,
|
|
145
|
-
});
|
|
128
|
+
}).build();
|
|
146
129
|
const agent = await authAsUser(app);
|
|
147
130
|
const servers = await agent.get("/mcp/servers");
|
|
148
131
|
expect(servers.status).not.toBe(404);
|
package/src/langfuseApp.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {beforeAll, beforeEach, describe, expect, it} from "bun:test";
|
|
2
|
-
import {createdUpdatedPlugin,
|
|
2
|
+
import {createdUpdatedPlugin, TerrenoApp} from "@terreno/api";
|
|
3
3
|
import type express from "express";
|
|
4
4
|
import mongoose from "mongoose";
|
|
5
5
|
import passportLocalMongoose from "passport-local-mongoose";
|
|
@@ -25,11 +25,11 @@ userSchema.plugin(createdUpdatedPlugin);
|
|
|
25
25
|
const UserModel = mongoose.models.User || mongoose.model("User", userSchema);
|
|
26
26
|
|
|
27
27
|
const buildApp = (plugin: LangfuseApp) =>
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
new TerrenoApp({
|
|
29
|
+
configureApp: (router) => plugin.register(router as unknown as express.Application),
|
|
30
30
|
skipListen: true,
|
|
31
31
|
userModel: UserModel,
|
|
32
|
-
});
|
|
32
|
+
}).build();
|
|
33
33
|
|
|
34
34
|
describe("LangfuseApp", () => {
|
|
35
35
|
beforeAll(async () => {
|
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
import {beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import passportLocalMongoose from "passport-local-mongoose";
|
|
5
|
-
import supertest from "supertest";
|
|
2
|
+
import {TerrenoApp, type TerrenoAppOptions} from "@terreno/api";
|
|
3
|
+
import type express from "express";
|
|
6
4
|
|
|
7
5
|
import {getLangfuseClient, initLangfuseClient} from "./langfuseClient";
|
|
8
6
|
import {addEvaluationRoutes} from "./langfuseRoutesEvaluations";
|
|
9
7
|
import {addPlaygroundRoutes} from "./langfuseRoutesPlayground";
|
|
10
8
|
import {addPromptRoutes} from "./langfuseRoutesPrompts";
|
|
11
9
|
import {addTraceRoutes} from "./langfuseRoutesTraces";
|
|
10
|
+
import {
|
|
11
|
+
authAsUserWithCredentials,
|
|
12
|
+
ensureTestUsers,
|
|
13
|
+
type StandardAiTestUserRole,
|
|
14
|
+
UserModel,
|
|
15
|
+
} from "./tests/helpers";
|
|
16
|
+
|
|
17
|
+
const LANGFUSE_TEST_USERS = [
|
|
18
|
+
{admin: true, email: "lf-admin@example.com", name: "LF Admin", password: "adminPass123"},
|
|
19
|
+
{admin: false, email: "lf-user@example.com", name: "LF User", password: "userPass123"},
|
|
20
|
+
] as const;
|
|
21
|
+
|
|
22
|
+
const authAsUser = async (appInstance: express.Application, type: StandardAiTestUserRole) => {
|
|
23
|
+
const user = LANGFUSE_TEST_USERS[type === "admin" ? 0 : 1];
|
|
24
|
+
return authAsUserWithCredentials(appInstance, {email: user.email, password: user.password});
|
|
25
|
+
};
|
|
12
26
|
|
|
13
27
|
const scoreCreate = mock(() => {});
|
|
14
28
|
const promptCreate = mock(async (_params: Record<string, unknown>) => ({}));
|
|
@@ -32,47 +46,11 @@ const traceList = mock(async () => ({
|
|
|
32
46
|
const traceGet = mock(async (traceId: string) => ({id: traceId, name: "Trace"}));
|
|
33
47
|
const flush = mock(async () => {});
|
|
34
48
|
|
|
35
|
-
|
|
36
|
-
admin: {default: false, type: Boolean},
|
|
37
|
-
email: {index: true, type: String},
|
|
38
|
-
name: String,
|
|
39
|
-
});
|
|
40
|
-
userSchema.plugin(passportLocalMongoose as Parameters<typeof userSchema.plugin>[0], {
|
|
41
|
-
usernameField: "email",
|
|
42
|
-
});
|
|
43
|
-
userSchema.plugin(createdUpdatedPlugin);
|
|
44
|
-
|
|
45
|
-
const UserModel = mongoose.models.User || mongoose.model("User", userSchema);
|
|
46
|
-
|
|
47
|
-
const authAsUser = async (
|
|
48
|
-
appInstance: Parameters<typeof supertest>[0],
|
|
49
|
-
type: "admin" | "notAdmin"
|
|
50
|
-
) => {
|
|
51
|
-
const email = type === "admin" ? "lf-admin@example.com" : "lf-user@example.com";
|
|
52
|
-
const password = type === "admin" ? "adminPass123" : "userPass123";
|
|
53
|
-
const agent = supertest.agent(appInstance);
|
|
54
|
-
const res = await agent.post("/auth/login").send({email, password}).expect(200);
|
|
55
|
-
await agent.set("authorization", `Bearer ${res.body.data.token}`);
|
|
56
|
-
return agent;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
let app: ReturnType<typeof setupServer>;
|
|
49
|
+
let app: express.Application;
|
|
60
50
|
|
|
61
51
|
describe("Langfuse routes", () => {
|
|
62
52
|
beforeAll(async () => {
|
|
63
|
-
await
|
|
64
|
-
|
|
65
|
-
const admin = await UserModel.create({
|
|
66
|
-
admin: true,
|
|
67
|
-
email: "lf-admin@example.com",
|
|
68
|
-
name: "LF Admin",
|
|
69
|
-
});
|
|
70
|
-
await (admin as {setPassword: (p: string) => Promise<void>}).setPassword("adminPass123");
|
|
71
|
-
await admin.save();
|
|
72
|
-
|
|
73
|
-
const user = await UserModel.create({email: "lf-user@example.com", name: "LF User"});
|
|
74
|
-
await (user as {setPassword: (p: string) => Promise<void>}).setPassword("userPass123");
|
|
75
|
-
await user.save();
|
|
53
|
+
await ensureTestUsers([...LANGFUSE_TEST_USERS]);
|
|
76
54
|
});
|
|
77
55
|
|
|
78
56
|
beforeEach(() => {
|
|
@@ -97,16 +75,16 @@ describe("Langfuse routes", () => {
|
|
|
97
75
|
client.prompt.get = promptGet;
|
|
98
76
|
client.score.create = scoreCreate;
|
|
99
77
|
|
|
100
|
-
app =
|
|
101
|
-
|
|
78
|
+
app = new TerrenoApp({
|
|
79
|
+
configureApp: (router) => {
|
|
102
80
|
addPromptRoutes(router, "/admin/langfuse");
|
|
103
81
|
addTraceRoutes(router, "/admin/langfuse");
|
|
104
82
|
addPlaygroundRoutes(router, "/admin/langfuse");
|
|
105
83
|
addEvaluationRoutes(router, "/admin/langfuse", []);
|
|
106
84
|
},
|
|
107
85
|
skipListen: true,
|
|
108
|
-
userModel: UserModel as
|
|
109
|
-
});
|
|
86
|
+
userModel: UserModel as unknown as TerrenoAppOptions["userModel"],
|
|
87
|
+
}).build();
|
|
110
88
|
});
|
|
111
89
|
|
|
112
90
|
describe("prompt routes", () => {
|
package/src/routes/files.test.ts
CHANGED
|
@@ -1,41 +1,16 @@
|
|
|
1
1
|
import {beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import {TerrenoApp} from "@terreno/api";
|
|
3
3
|
import type express from "express";
|
|
4
4
|
import mongoose from "mongoose";
|
|
5
|
-
import passportLocalMongoose from "passport-local-mongoose";
|
|
6
5
|
import supertest from "supertest";
|
|
7
6
|
|
|
8
7
|
import {FileAttachment} from "../models/fileAttachment";
|
|
9
8
|
import type {FileStorageService, UploadFileParams, UploadFileResult} from "../service/fileStorage";
|
|
9
|
+
import {authAsUser, ensureTestUsers, UserModel} from "../tests/helpers";
|
|
10
10
|
import {addFileRoutes} from "./files";
|
|
11
11
|
|
|
12
|
-
type PasswordedUser = {setPassword: (password: string) => Promise<void>};
|
|
13
12
|
type MockFileStorageService = Pick<FileStorageService, "upload" | "delete" | "getSignedUrl">;
|
|
14
13
|
|
|
15
|
-
const userSchema = new mongoose.Schema({
|
|
16
|
-
admin: {default: false, type: Boolean},
|
|
17
|
-
email: {index: true, type: String},
|
|
18
|
-
name: String,
|
|
19
|
-
});
|
|
20
|
-
userSchema.plugin(
|
|
21
|
-
passportLocalMongoose as unknown as (
|
|
22
|
-
schema: mongoose.Schema,
|
|
23
|
-
options: {usernameField: string}
|
|
24
|
-
) => void,
|
|
25
|
-
{usernameField: "email"}
|
|
26
|
-
);
|
|
27
|
-
userSchema.plugin(createdUpdatedPlugin);
|
|
28
|
-
const UserModel = mongoose.models.User || mongoose.model("User", userSchema);
|
|
29
|
-
|
|
30
|
-
const authAsUser = async (appInstance: express.Application, type: "admin" | "notAdmin") => {
|
|
31
|
-
const email = type === "admin" ? "admin@example.com" : "notAdmin@example.com";
|
|
32
|
-
const password = type === "admin" ? "securePassword" : "password";
|
|
33
|
-
const agent = supertest.agent(appInstance);
|
|
34
|
-
const res = await agent.post("/auth/login").send({email, password}).expect(200);
|
|
35
|
-
await agent.set("authorization", `Bearer ${res.body.data.token}`);
|
|
36
|
-
return agent;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
14
|
describe("File Routes", () => {
|
|
40
15
|
let app: express.Application;
|
|
41
16
|
let fileStorageService: MockFileStorageService;
|
|
@@ -43,15 +18,11 @@ describe("File Routes", () => {
|
|
|
43
18
|
let userId: mongoose.Types.ObjectId;
|
|
44
19
|
|
|
45
20
|
beforeAll(async () => {
|
|
46
|
-
await
|
|
47
|
-
const admin = await UserModel.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const user = await UserModel.create({email: "notAdmin@example.com", name: "User"});
|
|
52
|
-
await (user as unknown as PasswordedUser).setPassword("password");
|
|
53
|
-
await user.save();
|
|
54
|
-
userId = user._id;
|
|
21
|
+
await ensureTestUsers();
|
|
22
|
+
const admin = await UserModel.findOne({email: "admin@example.com"});
|
|
23
|
+
const user = await UserModel.findOne({email: "notAdmin@example.com"});
|
|
24
|
+
adminId = admin!._id as mongoose.Types.ObjectId;
|
|
25
|
+
userId = user!._id as mongoose.Types.ObjectId;
|
|
55
26
|
});
|
|
56
27
|
|
|
57
28
|
beforeEach(async () => {
|
|
@@ -69,8 +40,8 @@ describe("File Routes", () => {
|
|
|
69
40
|
})
|
|
70
41
|
),
|
|
71
42
|
};
|
|
72
|
-
app =
|
|
73
|
-
|
|
43
|
+
app = new TerrenoApp({
|
|
44
|
+
configureApp: (router, options) => {
|
|
74
45
|
addFileRoutes(router, {
|
|
75
46
|
fileStorageService: fileStorageService as FileStorageService,
|
|
76
47
|
openApiOptions: options,
|
|
@@ -78,7 +49,7 @@ describe("File Routes", () => {
|
|
|
78
49
|
},
|
|
79
50
|
skipListen: true,
|
|
80
51
|
userModel: UserModel,
|
|
81
|
-
});
|
|
52
|
+
}).build();
|
|
82
53
|
});
|
|
83
54
|
|
|
84
55
|
describe("POST /files/upload", () => {
|
package/src/routes/mcp.test.ts
CHANGED
|
@@ -1,52 +1,20 @@
|
|
|
1
1
|
import {beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import {TerrenoApp} from "@terreno/api";
|
|
3
3
|
import type express from "express";
|
|
4
|
-
import mongoose from "mongoose";
|
|
5
|
-
import passportLocalMongoose from "passport-local-mongoose";
|
|
6
4
|
import supertest from "supertest";
|
|
7
5
|
|
|
8
6
|
import type {MCPService} from "../service/mcpService";
|
|
7
|
+
import {authAsUser, ensureTestUsers, UserModel} from "../tests/helpers";
|
|
9
8
|
import {addMcpRoutes} from "./mcp";
|
|
10
9
|
|
|
11
|
-
type PasswordedUser = {setPassword: (password: string) => Promise<void>};
|
|
12
10
|
type MockMcpService = Pick<MCPService, "getServerStatus" | "getTools" | "reconnectServer">;
|
|
13
11
|
|
|
14
|
-
const userSchema = new mongoose.Schema({
|
|
15
|
-
admin: {default: false, type: Boolean},
|
|
16
|
-
email: {index: true, type: String},
|
|
17
|
-
name: String,
|
|
18
|
-
});
|
|
19
|
-
userSchema.plugin(
|
|
20
|
-
passportLocalMongoose as unknown as (
|
|
21
|
-
schema: mongoose.Schema,
|
|
22
|
-
options: {usernameField: string}
|
|
23
|
-
) => void,
|
|
24
|
-
{usernameField: "email"}
|
|
25
|
-
);
|
|
26
|
-
userSchema.plugin(createdUpdatedPlugin);
|
|
27
|
-
const UserModel = mongoose.models.User || mongoose.model("User", userSchema);
|
|
28
|
-
|
|
29
|
-
const authAsUser = async (appInstance: express.Application, type: "admin" | "notAdmin") => {
|
|
30
|
-
const email = type === "admin" ? "admin@example.com" : "notAdmin@example.com";
|
|
31
|
-
const password = type === "admin" ? "securePassword" : "password";
|
|
32
|
-
const agent = supertest.agent(appInstance);
|
|
33
|
-
const res = await agent.post("/auth/login").send({email, password}).expect(200);
|
|
34
|
-
await agent.set("authorization", `Bearer ${res.body.data.token}`);
|
|
35
|
-
return agent;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
12
|
describe("MCP Routes", () => {
|
|
39
13
|
let app: express.Application;
|
|
40
14
|
let mcpService: MockMcpService;
|
|
41
15
|
|
|
42
16
|
beforeAll(async () => {
|
|
43
|
-
await
|
|
44
|
-
const admin = await UserModel.create({admin: true, email: "admin@example.com", name: "Admin"});
|
|
45
|
-
await (admin as unknown as PasswordedUser).setPassword("securePassword");
|
|
46
|
-
await admin.save();
|
|
47
|
-
const user = await UserModel.create({email: "notAdmin@example.com", name: "User"});
|
|
48
|
-
await (user as unknown as PasswordedUser).setPassword("password");
|
|
49
|
-
await user.save();
|
|
17
|
+
await ensureTestUsers();
|
|
50
18
|
});
|
|
51
19
|
|
|
52
20
|
beforeEach(() => {
|
|
@@ -57,13 +25,13 @@ describe("MCP Routes", () => {
|
|
|
57
25
|
})),
|
|
58
26
|
reconnectServer: mock(async () => true),
|
|
59
27
|
} as unknown as MockMcpService;
|
|
60
|
-
app =
|
|
61
|
-
|
|
28
|
+
app = new TerrenoApp({
|
|
29
|
+
configureApp: (router, options) => {
|
|
62
30
|
addMcpRoutes(router, {mcpService: mcpService as MCPService, openApiOptions: options});
|
|
63
31
|
},
|
|
64
32
|
skipListen: true,
|
|
65
33
|
userModel: UserModel,
|
|
66
|
-
});
|
|
34
|
+
}).build();
|
|
67
35
|
});
|
|
68
36
|
|
|
69
37
|
describe("GET /mcp/servers", () => {
|
|
@@ -116,13 +84,13 @@ describe("MCP Routes", () => {
|
|
|
116
84
|
|
|
117
85
|
it("returns connected=false when reconnect fails", async () => {
|
|
118
86
|
mcpService.reconnectServer = mock(async () => false);
|
|
119
|
-
const customApp =
|
|
120
|
-
|
|
87
|
+
const customApp = new TerrenoApp({
|
|
88
|
+
configureApp: (router, options) => {
|
|
121
89
|
addMcpRoutes(router, {mcpService: mcpService as MCPService, openApiOptions: options});
|
|
122
90
|
},
|
|
123
91
|
skipListen: true,
|
|
124
92
|
userModel: UserModel,
|
|
125
|
-
});
|
|
93
|
+
}).build();
|
|
126
94
|
const agent = await authAsUser(customApp, "admin");
|
|
127
95
|
const res = await agent.post("/mcp/servers/unknown/reconnect");
|
|
128
96
|
expect(res.status).toBe(200);
|
|
@@ -1,63 +1,31 @@
|
|
|
1
1
|
import {beforeAll, beforeEach, describe, expect, it} from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import {TerrenoApp} from "@terreno/api";
|
|
3
3
|
import type express from "express";
|
|
4
4
|
import mongoose from "mongoose";
|
|
5
|
-
import passportLocalMongoose from "passport-local-mongoose";
|
|
6
|
-
import supertest from "supertest";
|
|
7
5
|
|
|
8
6
|
import {Project} from "../models/project";
|
|
7
|
+
import {authAsUser, ensureTestUsers, UserModel} from "../tests/helpers";
|
|
9
8
|
import {addProjectRoutes} from "./projects";
|
|
10
9
|
|
|
11
|
-
const userSchema = new mongoose.Schema({
|
|
12
|
-
admin: {default: false, type: Boolean},
|
|
13
|
-
email: {index: true, type: String},
|
|
14
|
-
name: String,
|
|
15
|
-
});
|
|
16
|
-
userSchema.plugin(
|
|
17
|
-
passportLocalMongoose as unknown as (
|
|
18
|
-
schema: mongoose.Schema,
|
|
19
|
-
options: {usernameField: string}
|
|
20
|
-
) => void,
|
|
21
|
-
{usernameField: "email"}
|
|
22
|
-
);
|
|
23
|
-
userSchema.plugin(createdUpdatedPlugin);
|
|
24
|
-
const UserModel = mongoose.models.User || mongoose.model("User", userSchema);
|
|
25
|
-
|
|
26
|
-
type PasswordedUser = {setPassword: (password: string) => Promise<void>};
|
|
27
|
-
|
|
28
|
-
const authAsUser = async (appInstance: express.Application, type: "admin" | "notAdmin") => {
|
|
29
|
-
const email = type === "admin" ? "admin@example.com" : "notAdmin@example.com";
|
|
30
|
-
const password = type === "admin" ? "securePassword" : "password";
|
|
31
|
-
const agent = supertest.agent(appInstance);
|
|
32
|
-
const res = await agent.post("/auth/login").send({email, password}).expect(200);
|
|
33
|
-
await agent.set("authorization", `Bearer ${res.body.data.token}`);
|
|
34
|
-
return agent;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
10
|
describe("Project Routes", () => {
|
|
38
11
|
let app: express.Application;
|
|
39
12
|
let notAdminId: mongoose.Types.ObjectId;
|
|
40
13
|
|
|
41
14
|
beforeAll(async () => {
|
|
42
|
-
await
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
await admin.save();
|
|
46
|
-
const user = await UserModel.create({email: "notAdmin@example.com", name: "User"});
|
|
47
|
-
await (user as unknown as PasswordedUser).setPassword("password");
|
|
48
|
-
await user.save();
|
|
49
|
-
notAdminId = user._id as mongoose.Types.ObjectId;
|
|
15
|
+
await ensureTestUsers();
|
|
16
|
+
const user = await UserModel.findOne({email: "notAdmin@example.com"});
|
|
17
|
+
notAdminId = user!._id as mongoose.Types.ObjectId;
|
|
50
18
|
});
|
|
51
19
|
|
|
52
20
|
beforeEach(async () => {
|
|
53
21
|
await Project.deleteMany({});
|
|
54
|
-
app =
|
|
55
|
-
|
|
22
|
+
app = new TerrenoApp({
|
|
23
|
+
configureApp: (router, options) => {
|
|
56
24
|
addProjectRoutes(router, {openApiOptions: options});
|
|
57
25
|
},
|
|
58
26
|
skipListen: true,
|
|
59
27
|
userModel: UserModel,
|
|
60
|
-
});
|
|
28
|
+
}).build();
|
|
61
29
|
});
|
|
62
30
|
|
|
63
31
|
describe("project memories (subdoc)", () => {
|