@terreno/ai 0.21.0 → 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/package.json CHANGED
@@ -18,13 +18,14 @@
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.21.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.21.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.21.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 {createdUpdatedPlugin, TerrenoApp} from "@terreno/api";
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
- type PasswordedUser = {setPassword: (password: string) => Promise<void>};
14
-
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);
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,20 +42,14 @@ const createMockModel = () => ({
53
42
 
54
43
  describe("AiApp", () => {
55
44
  beforeAll(async () => {
56
- await UserModel.deleteMany({});
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
- const agent = supertest.agent(app);
64
- const res = await agent
65
- .post("/auth/login")
66
- .send({email: "aiapp@example.com", password: "password"})
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 () => {
@@ -1,15 +1,28 @@
1
1
  import {beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
2
- import {createdUpdatedPlugin, TerrenoApp, type TerrenoAppOptions} from "@terreno/api";
2
+ import {TerrenoApp, type TerrenoAppOptions} from "@terreno/api";
3
3
  import type express from "express";
4
- import mongoose from "mongoose";
5
- import passportLocalMongoose from "passport-local-mongoose";
6
- import supertest from "supertest";
7
4
 
8
5
  import {getLangfuseClient, initLangfuseClient} from "./langfuseClient";
9
6
  import {addEvaluationRoutes} from "./langfuseRoutesEvaluations";
10
7
  import {addPlaygroundRoutes} from "./langfuseRoutesPlayground";
11
8
  import {addPromptRoutes} from "./langfuseRoutesPrompts";
12
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
+ };
13
26
 
14
27
  const scoreCreate = mock(() => {});
15
28
  const promptCreate = mock(async (_params: Record<string, unknown>) => ({}));
@@ -33,47 +46,11 @@ const traceList = mock(async () => ({
33
46
  const traceGet = mock(async (traceId: string) => ({id: traceId, name: "Trace"}));
34
47
  const flush = mock(async () => {});
35
48
 
36
- const userSchema = new mongoose.Schema({
37
- admin: {default: false, type: Boolean},
38
- email: {index: true, type: String},
39
- name: String,
40
- });
41
- userSchema.plugin(passportLocalMongoose as Parameters<typeof userSchema.plugin>[0], {
42
- usernameField: "email",
43
- });
44
- userSchema.plugin(createdUpdatedPlugin);
45
-
46
- const UserModel = mongoose.models.User || mongoose.model("User", userSchema);
47
-
48
- const authAsUser = async (
49
- appInstance: Parameters<typeof supertest>[0],
50
- type: "admin" | "notAdmin"
51
- ) => {
52
- const email = type === "admin" ? "lf-admin@example.com" : "lf-user@example.com";
53
- const password = type === "admin" ? "adminPass123" : "userPass123";
54
- const agent = supertest.agent(appInstance);
55
- const res = await agent.post("/auth/login").send({email, password}).expect(200);
56
- await agent.set("authorization", `Bearer ${res.body.data.token}`);
57
- return agent;
58
- };
59
-
60
49
  let app: express.Application;
61
50
 
62
51
  describe("Langfuse routes", () => {
63
52
  beforeAll(async () => {
64
- await UserModel.deleteMany({email: {$in: ["lf-admin@example.com", "lf-user@example.com"]}});
65
-
66
- const admin = await UserModel.create({
67
- admin: true,
68
- email: "lf-admin@example.com",
69
- name: "LF Admin",
70
- });
71
- await (admin as {setPassword: (p: string) => Promise<void>}).setPassword("adminPass123");
72
- await admin.save();
73
-
74
- const user = await UserModel.create({email: "lf-user@example.com", name: "LF User"});
75
- await (user as {setPassword: (p: string) => Promise<void>}).setPassword("userPass123");
76
- await user.save();
53
+ await ensureTestUsers([...LANGFUSE_TEST_USERS]);
77
54
  });
78
55
 
79
56
  beforeEach(() => {
@@ -1,41 +1,16 @@
1
1
  import {beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
2
- import {createdUpdatedPlugin, TerrenoApp} from "@terreno/api";
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 UserModel.deleteMany({});
47
- const admin = await UserModel.create({admin: true, email: "admin@example.com", name: "Admin"});
48
- await (admin as unknown as PasswordedUser).setPassword("securePassword");
49
- await admin.save();
50
- adminId = admin._id;
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 () => {
@@ -1,52 +1,20 @@
1
1
  import {beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
2
- import {createdUpdatedPlugin, TerrenoApp} from "@terreno/api";
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 UserModel.deleteMany({});
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(() => {
@@ -1,52 +1,20 @@
1
1
  import {beforeAll, beforeEach, describe, expect, it} from "bun:test";
2
- import {createdUpdatedPlugin, TerrenoApp} from "@terreno/api";
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 UserModel.deleteMany({});
43
- const admin = await UserModel.create({admin: true, email: "admin@example.com", name: "Admin"});
44
- await (admin as unknown as PasswordedUser).setPassword("securePassword");
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 () => {
@@ -1,9 +1,8 @@
1
1
  import {afterEach, beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
2
- import {createdUpdatedPlugin, TerrenoApp} from "@terreno/api";
2
+ import {TerrenoApp} from "@terreno/api";
3
3
  import {jsonSchema, type LanguageModel, type Tool, tool} from "ai";
4
4
  import type express from "express";
5
5
  import mongoose from "mongoose";
6
- import passportLocalMongoose from "passport-local-mongoose";
7
6
  import supertest from "supertest";
8
7
 
9
8
  import {AIRequest} from "../models/aiRequest";
@@ -11,10 +10,10 @@ import {GptHistory} from "../models/gptHistory";
11
10
  import {Project} from "../models/project";
12
11
  import {AIService} from "../service/aiService";
13
12
  import type {MCPService} from "../service/mcpService";
13
+ import {authAsUser, ensureTestUsers, UserModel} from "../tests/helpers";
14
14
  import {addAiRequestsExplorerRoutes} from "./aiRequestsExplorer";
15
15
  import {addGptHistoryRoutes} from "./gptHistories";
16
16
 
17
- type PasswordedUser = {setPassword: (password: string) => Promise<void>};
18
17
  type SseResponse = supertest.Response & {body: string};
19
18
 
20
19
  // Mock langfuseVercelAi to avoid transitive langfuse SDK import in tests.
@@ -28,23 +27,6 @@ mock.module("../langfuseVercelAi", () => ({
28
27
 
29
28
  const {addGptRoutes} = await import("./gpt");
30
29
 
31
- // Test user schema
32
- const userSchema = new mongoose.Schema({
33
- admin: {default: false, type: Boolean},
34
- email: {index: true, type: String},
35
- name: String,
36
- });
37
- userSchema.plugin(
38
- passportLocalMongoose as unknown as (
39
- schema: mongoose.Schema,
40
- options: {usernameField: string}
41
- ) => void,
42
- {usernameField: "email"}
43
- );
44
- userSchema.plugin(createdUpdatedPlugin);
45
-
46
- const UserModel = mongoose.models.User || mongoose.model("User", userSchema);
47
-
48
30
  // Create mock AI model (LanguageModelV2)
49
31
  const createMockModel = (modelId = "mock-model") => ({
50
32
  doGenerate: mock(async () => ({
@@ -126,29 +108,11 @@ const createErrorModel = () => ({
126
108
  let app: express.Application;
127
109
  let aiService: AIService;
128
110
 
129
- const authAsUser = async (appInstance: express.Application, type: "admin" | "notAdmin") => {
130
- const email = type === "admin" ? "admin@example.com" : "notAdmin@example.com";
131
- const password = type === "admin" ? "securePassword" : "password";
132
- const agent = supertest.agent(appInstance);
133
- const res = await agent.post("/auth/login").send({email, password}).expect(200);
134
- await agent.set("authorization", `Bearer ${res.body.data.token}`);
135
- return agent;
136
- };
137
-
138
111
  describe("AI Routes", () => {
139
112
  beforeAll(async () => {
140
- // Clean up and create test users
141
- await UserModel.deleteMany({});
113
+ await ensureTestUsers();
142
114
  await AIRequest.deleteMany({});
143
115
  await GptHistory.deleteMany({});
144
-
145
- const admin = await UserModel.create({admin: true, email: "admin@example.com", name: "Admin"});
146
- await (admin as unknown as PasswordedUser).setPassword("securePassword");
147
- await admin.save();
148
-
149
- const user = await UserModel.create({email: "notAdmin@example.com", name: "User"});
150
- await (user as unknown as PasswordedUser).setPassword("password");
151
- await user.save();
152
116
  });
153
117
 
154
118
  beforeEach(() => {
@@ -216,6 +216,12 @@ describe("parseAiJson", () => {
216
216
  expect(r.data).toBe("hello");
217
217
  }
218
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
+ });
219
225
  });
220
226
 
221
227
  describe("normalizeLlmJsonTextForStructuredOutput", () => {
@@ -1,111 +1,21 @@
1
1
  // biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
2
- import {afterAll, afterEach, beforeAll, beforeEach, mock} from "bun:test";
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
- const shouldConnectToTestDb = process.env.BUN_TEST_DISABLE_DB !== "true";
9
-
10
- // Connect to MongoDB once for all tests
11
- if (shouldConnectToTestDb) {
12
- beforeAll(async () => {
13
- await mongoose
14
- .connect("mongodb://127.0.0.1/terreno-ai-test?&connectTimeoutMS=360000")
15
- .catch((err) => {
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. Each `new LangfuseClient(...)` gets its own
105
- // mock functions so tests can customize per-instance behavior (via
106
- // `getLangfuseClient()` after `initLangfuseClient(...)`). Mocking the SDK
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};