@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.
@@ -1,9 +1,8 @@
1
1
  import {afterEach, beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
2
- import {createdUpdatedPlugin, setupServer} 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,44 +108,26 @@ 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(() => {
155
119
  const mockModel = createMockModel();
156
120
  aiService = new AIService({model: mockModel as unknown as LanguageModel});
157
121
 
158
- app = setupServer({
159
- addRoutes: (router, options) => {
122
+ app = new TerrenoApp({
123
+ configureApp: (router, options) => {
160
124
  addGptHistoryRoutes(router, options);
161
125
  addGptRoutes(router, {aiService, openApiOptions: options});
162
126
  addAiRequestsExplorerRoutes(router, {openApiOptions: options});
163
127
  },
164
128
  skipListen: true,
165
129
  userModel: UserModel,
166
- });
130
+ }).build();
167
131
  });
168
132
 
169
133
  afterEach(async () => {
@@ -209,13 +173,13 @@ describe("AI Routes", () => {
209
173
  });
210
174
 
211
175
  it("returns demo response when no aiService configured", async () => {
212
- const demoApp = setupServer({
213
- addRoutes: (router, options) => {
176
+ const demoApp = new TerrenoApp({
177
+ configureApp: (router, options) => {
214
178
  addGptRoutes(router, {openApiOptions: options});
215
179
  },
216
180
  skipListen: true,
217
181
  userModel: UserModel,
218
- });
182
+ }).build();
219
183
  const agent = await authAsUser(demoApp, "notAdmin");
220
184
  const res = await agent.post("/gpt/remix").send({text: "Hello"});
221
185
  expect(res.status).toBe(200);
@@ -260,13 +224,13 @@ describe("AI Routes", () => {
260
224
  });
261
225
 
262
226
  it("sends demo response when no ai service configured", async () => {
263
- const demoApp = setupServer({
264
- addRoutes: (router, options) => {
227
+ const demoApp = new TerrenoApp({
228
+ configureApp: (router, options) => {
265
229
  addGptRoutes(router, {openApiOptions: options});
266
230
  },
267
231
  skipListen: true,
268
232
  userModel: UserModel,
269
- });
233
+ }).build();
270
234
  const agent = await authAsUser(demoApp, "notAdmin");
271
235
  const res = await agent
272
236
  .post("/gpt/prompt")
@@ -282,13 +246,13 @@ describe("AI Routes", () => {
282
246
  const createModelFn = mock((_apiKey: string, _modelId?: string) => {
283
247
  return createMockModel() as unknown as LanguageModel;
284
248
  });
285
- const customApp = setupServer({
286
- addRoutes: (router, options) => {
249
+ const customApp = new TerrenoApp({
250
+ configureApp: (router, options) => {
287
251
  addGptRoutes(router, {createModelFn, openApiOptions: options});
288
252
  },
289
253
  skipListen: true,
290
254
  userModel: UserModel,
291
- });
255
+ }).build();
292
256
  const agent = await authAsUser(customApp, "notAdmin");
293
257
  const res = await agent
294
258
  .post("/gpt/prompt")
@@ -304,13 +268,13 @@ describe("AI Routes", () => {
304
268
  const createServerModelFn = mock(
305
269
  (_modelId?: string) => createMockModel() as unknown as LanguageModel
306
270
  );
307
- const customApp = setupServer({
308
- addRoutes: (router, options) => {
271
+ const customApp = new TerrenoApp({
272
+ configureApp: (router, options) => {
309
273
  addGptRoutes(router, {createServerModelFn, openApiOptions: options});
310
274
  },
311
275
  skipListen: true,
312
276
  userModel: UserModel,
313
- });
277
+ }).build();
314
278
  const agent = await authAsUser(customApp, "notAdmin");
315
279
  const res = await agent
316
280
  .post("/gpt/prompt")
@@ -323,13 +287,13 @@ describe("AI Routes", () => {
323
287
 
324
288
  it("falls back to default aiService when createServerModelFn returns null", async () => {
325
289
  const createServerModelFn = mock((_modelId?: string) => null);
326
- const customApp = setupServer({
327
- addRoutes: (router, options) => {
290
+ const customApp = new TerrenoApp({
291
+ configureApp: (router, options) => {
328
292
  addGptRoutes(router, {aiService, createServerModelFn, openApiOptions: options});
329
293
  },
330
294
  skipListen: true,
331
295
  userModel: UserModel,
332
- });
296
+ }).build();
333
297
  const agent = await authAsUser(customApp, "notAdmin");
334
298
  const res = await agent
335
299
  .post("/gpt/prompt")
@@ -432,13 +396,13 @@ describe("AI Routes", () => {
432
396
  const imageService = new AIService({
433
397
  model: createImageModel() as unknown as LanguageModel,
434
398
  });
435
- const imgApp = setupServer({
436
- addRoutes: (router, options) => {
399
+ const imgApp = new TerrenoApp({
400
+ configureApp: (router, options) => {
437
401
  addGptRoutes(router, {aiService: imageService, openApiOptions: options});
438
402
  },
439
403
  skipListen: true,
440
404
  userModel: UserModel,
441
- });
405
+ }).build();
442
406
  const agent = await authAsUser(imgApp, "notAdmin");
443
407
  const res = await agent
444
408
  .post("/gpt/prompt")
@@ -454,13 +418,13 @@ describe("AI Routes", () => {
454
418
  const errService = new AIService({
455
419
  model: createErrorModel() as unknown as LanguageModel,
456
420
  });
457
- const errApp = setupServer({
458
- addRoutes: (router, options) => {
421
+ const errApp = new TerrenoApp({
422
+ configureApp: (router, options) => {
459
423
  addGptRoutes(router, {aiService: errService, openApiOptions: options});
460
424
  },
461
425
  skipListen: true,
462
426
  userModel: UserModel,
463
- });
427
+ }).build();
464
428
  const agent = await authAsUser(errApp, "notAdmin");
465
429
  const res = await agent
466
430
  .post("/gpt/prompt")
@@ -478,13 +442,13 @@ describe("AI Routes", () => {
478
442
  throw new Error("mcp unavailable");
479
443
  }),
480
444
  } as unknown as MCPService;
481
- const customApp = setupServer({
482
- addRoutes: (router, options) => {
445
+ const customApp = new TerrenoApp({
446
+ configureApp: (router, options) => {
483
447
  addGptRoutes(router, {aiService, mcpService, openApiOptions: options});
484
448
  },
485
449
  skipListen: true,
486
450
  userModel: UserModel,
487
- });
451
+ }).build();
488
452
  const agent = await authAsUser(customApp, "notAdmin");
489
453
  const res = await agent
490
454
  .post("/gpt/prompt")
@@ -501,13 +465,13 @@ describe("AI Routes", () => {
501
465
  search: {description: "web search"},
502
466
  })),
503
467
  } as unknown as MCPService;
504
- const customApp = setupServer({
505
- addRoutes: (router, options) => {
468
+ const customApp = new TerrenoApp({
469
+ configureApp: (router, options) => {
506
470
  addGptRoutes(router, {aiService, mcpService, openApiOptions: options});
507
471
  },
508
472
  skipListen: true,
509
473
  userModel: UserModel,
510
- });
474
+ }).build();
511
475
  const agent = await authAsUser(customApp, "notAdmin");
512
476
  const res = await agent
513
477
  .post("/gpt/prompt")
@@ -527,8 +491,8 @@ describe("AI Routes", () => {
527
491
  }
528
492
  return mainFn();
529
493
  });
530
- const customApp = setupServer({
531
- addRoutes: (router, options) => {
494
+ const customApp = new TerrenoApp({
495
+ configureApp: (router, options) => {
532
496
  addGptRoutes(router, {
533
497
  createServerModelFn,
534
498
  openApiOptions: options,
@@ -537,7 +501,7 @@ describe("AI Routes", () => {
537
501
  },
538
502
  skipListen: true,
539
503
  userModel: UserModel,
540
- });
504
+ }).build();
541
505
  const agent = await authAsUser(customApp, "notAdmin");
542
506
  const res = await agent
543
507
  .post("/gpt/prompt")
@@ -552,8 +516,8 @@ describe("AI Routes", () => {
552
516
  const createModelFn = mock((_apiKey: string, _modelId?: string) => {
553
517
  return createMockModel() as unknown as LanguageModel;
554
518
  });
555
- const customApp = setupServer({
556
- addRoutes: (router, options) => {
519
+ const customApp = new TerrenoApp({
520
+ configureApp: (router, options) => {
557
521
  addGptRoutes(router, {
558
522
  createModelFn,
559
523
  openApiOptions: options,
@@ -562,7 +526,7 @@ describe("AI Routes", () => {
562
526
  },
563
527
  skipListen: true,
564
528
  userModel: UserModel,
565
- });
529
+ }).build();
566
530
  const agent = await authAsUser(customApp, "notAdmin");
567
531
  const res = await agent
568
532
  .post("/gpt/prompt")
@@ -583,8 +547,8 @@ describe("AI Routes", () => {
583
547
  const imageService = new AIService({
584
548
  model: createImageModel() as unknown as LanguageModel,
585
549
  });
586
- const imgApp = setupServer({
587
- addRoutes: (router, options) => {
550
+ const imgApp = new TerrenoApp({
551
+ configureApp: (router, options) => {
588
552
  addGptRoutes(router, {
589
553
  aiService: imageService,
590
554
  openApiOptions: options,
@@ -595,7 +559,7 @@ describe("AI Routes", () => {
595
559
  },
596
560
  skipListen: true,
597
561
  userModel: UserModel,
598
- });
562
+ }).build();
599
563
  const agent = await authAsUser(imgApp, "notAdmin");
600
564
  const res = await agent
601
565
  .post("/gpt/prompt")
@@ -606,8 +570,8 @@ describe("AI Routes", () => {
606
570
  });
607
571
 
608
572
  it("uses the langfuse system prompt when configured", async () => {
609
- const customApp = setupServer({
610
- addRoutes: (router, options) => {
573
+ const customApp = new TerrenoApp({
574
+ configureApp: (router, options) => {
611
575
  addGptRoutes(router, {
612
576
  aiService,
613
577
  langfuseSystemPromptName: "missing-prompt-name",
@@ -616,7 +580,7 @@ describe("AI Routes", () => {
616
580
  },
617
581
  skipListen: true,
618
582
  userModel: UserModel,
619
- });
583
+ }).build();
620
584
  const agent = await authAsUser(customApp, "notAdmin");
621
585
  const res = await agent
622
586
  .post("/gpt/prompt")
@@ -676,8 +640,8 @@ describe("AI Routes", () => {
676
640
  specificationVersion: "v2" as const,
677
641
  supportedUrls: {},
678
642
  };
679
- const toolApp = setupServer({
680
- addRoutes: (router, options) => {
643
+ const toolApp = new TerrenoApp({
644
+ configureApp: (router, options) => {
681
645
  addGptRoutes(router, {
682
646
  aiService: new AIService({model: toolModel as unknown as LanguageModel}),
683
647
  openApiOptions: options,
@@ -688,7 +652,7 @@ describe("AI Routes", () => {
688
652
  },
689
653
  skipListen: true,
690
654
  userModel: UserModel,
691
- });
655
+ }).build();
692
656
  const agent = await authAsUser(toolApp, "notAdmin");
693
657
  const res = await agent
694
658
  .post("/gpt/prompt")
@@ -761,8 +725,8 @@ describe("AI Routes", () => {
761
725
  }),
762
726
  inputSchema: jsonSchema({properties: {}, type: "object"}),
763
727
  });
764
- const fileToolApp = setupServer({
765
- addRoutes: (router, options) => {
728
+ const fileToolApp = new TerrenoApp({
729
+ configureApp: (router, options) => {
766
730
  addGptRoutes(router, {
767
731
  aiService: new AIService({model: fileToolModel as unknown as LanguageModel}),
768
732
  openApiOptions: options,
@@ -771,7 +735,7 @@ describe("AI Routes", () => {
771
735
  },
772
736
  skipListen: true,
773
737
  userModel: UserModel,
774
- });
738
+ }).build();
775
739
  const agent = await authAsUser(fileToolApp, "notAdmin");
776
740
  const res = await agent
777
741
  .post("/gpt/prompt")
@@ -816,8 +780,8 @@ describe("AI Routes", () => {
816
780
  specificationVersion: "v2" as const,
817
781
  supportedUrls: {},
818
782
  };
819
- const errApp = setupServer({
820
- addRoutes: (router, options) => {
783
+ const errApp = new TerrenoApp({
784
+ configureApp: (router, options) => {
821
785
  addGptRoutes(router, {
822
786
  aiService: new AIService({
823
787
  model: errEventModel as unknown as LanguageModel,
@@ -827,7 +791,7 @@ describe("AI Routes", () => {
827
791
  },
828
792
  skipListen: true,
829
793
  userModel: UserModel,
830
- });
794
+ }).build();
831
795
  const agent = await authAsUser(errApp, "notAdmin");
832
796
  const res = await agent
833
797
  .post("/gpt/prompt")
@@ -870,8 +834,8 @@ describe("AI Routes", () => {
870
834
  }
871
835
  return createMockModel() as unknown as LanguageModel;
872
836
  });
873
- const customApp = setupServer({
874
- addRoutes: (router, options) => {
837
+ const customApp = new TerrenoApp({
838
+ configureApp: (router, options) => {
875
839
  addGptRoutes(router, {
876
840
  createServerModelFn,
877
841
  openApiOptions: options,
@@ -880,7 +844,7 @@ describe("AI Routes", () => {
880
844
  },
881
845
  skipListen: true,
882
846
  userModel: UserModel,
883
- });
847
+ }).build();
884
848
  const agent = await authAsUser(customApp, "notAdmin");
885
849
  const res = await agent
886
850
  .post("/gpt/prompt")
@@ -943,8 +907,8 @@ describe("AI Routes", () => {
943
907
  specificationVersion: "v2" as const,
944
908
  supportedUrls: {},
945
909
  };
946
- const customApp = setupServer({
947
- addRoutes: (router, options) => {
910
+ const customApp = new TerrenoApp({
911
+ configureApp: (router, options) => {
948
912
  addGptRoutes(router, {
949
913
  aiService: new AIService({model: noStepModel as unknown as LanguageModel}),
950
914
  openApiOptions: options,
@@ -952,7 +916,7 @@ describe("AI Routes", () => {
952
916
  },
953
917
  skipListen: true,
954
918
  userModel: UserModel,
955
- });
919
+ }).build();
956
920
  const agent = await authAsUser(customApp, "notAdmin");
957
921
  const res = await agent
958
922
  .post("/gpt/prompt")
@@ -968,8 +932,8 @@ describe("AI Routes", () => {
968
932
  const createRequestTools = mock(() => ({
969
933
  localLookup: {description: "Per-request tool"} as unknown as Tool,
970
934
  }));
971
- const customApp = setupServer({
972
- addRoutes: (router, options) => {
935
+ const customApp = new TerrenoApp({
936
+ configureApp: (router, options) => {
973
937
  addGptRoutes(router, {
974
938
  aiService,
975
939
  createRequestTools,
@@ -978,7 +942,7 @@ describe("AI Routes", () => {
978
942
  },
979
943
  skipListen: true,
980
944
  userModel: UserModel,
981
- });
945
+ }).build();
982
946
  const agent = await authAsUser(customApp, "notAdmin");
983
947
  const res = await agent
984
948
  .post("/gpt/prompt")
@@ -1021,8 +985,8 @@ describe("AI Routes", () => {
1021
985
  specificationVersion: "v2" as const,
1022
986
  supportedUrls: {},
1023
987
  };
1024
- const fileApp = setupServer({
1025
- addRoutes: (router, options) => {
988
+ const fileApp = new TerrenoApp({
989
+ configureApp: (router, options) => {
1026
990
  addGptRoutes(router, {
1027
991
  aiService: new AIService({model: nonImageFileModel as unknown as LanguageModel}),
1028
992
  openApiOptions: options,
@@ -1030,7 +994,7 @@ describe("AI Routes", () => {
1030
994
  },
1031
995
  skipListen: true,
1032
996
  userModel: UserModel,
1033
- });
997
+ }).build();
1034
998
  const agent = await authAsUser(fileApp, "notAdmin");
1035
999
  const res = await agent
1036
1000
  .post("/gpt/prompt")
@@ -1179,8 +1143,8 @@ describe("AI Routes", () => {
1179
1143
  specificationVersion: "v2" as const,
1180
1144
  supportedUrls: {},
1181
1145
  };
1182
- const errApp = setupServer({
1183
- addRoutes: (router, options) => {
1146
+ const errApp = new TerrenoApp({
1147
+ configureApp: (router, options) => {
1184
1148
  addGptRoutes(router, {
1185
1149
  aiService: new AIService({
1186
1150
  model: streamIterationErrorModel as unknown as LanguageModel,
@@ -1190,7 +1154,7 @@ describe("AI Routes", () => {
1190
1154
  },
1191
1155
  skipListen: true,
1192
1156
  userModel: UserModel,
1193
- });
1157
+ }).build();
1194
1158
  const agent = await authAsUser(errApp, "notAdmin");
1195
1159
  const res = await agent
1196
1160
  .post("/gpt/prompt")
@@ -1233,8 +1197,8 @@ describe("AI Routes", () => {
1233
1197
  },
1234
1198
  }));
1235
1199
  try {
1236
- const customApp = setupServer({
1237
- addRoutes: (router, options) => {
1200
+ const customApp = new TerrenoApp({
1201
+ configureApp: (router, options) => {
1238
1202
  addGptRoutes(router, {
1239
1203
  aiService,
1240
1204
  langfuseSystemPromptName: "broken-prompt",
@@ -1243,7 +1207,7 @@ describe("AI Routes", () => {
1243
1207
  },
1244
1208
  skipListen: true,
1245
1209
  userModel: UserModel,
1246
- });
1210
+ }).build();
1247
1211
  const agent = await authAsUser(customApp, "notAdmin");
1248
1212
  const res = await agent
1249
1213
  .post("/gpt/prompt")
@@ -1296,8 +1260,8 @@ describe("AI Routes", () => {
1296
1260
  specificationVersion: "v2" as const,
1297
1261
  supportedUrls: {},
1298
1262
  };
1299
- const errApp = setupServer({
1300
- addRoutes: (router, options) => {
1263
+ const errApp = new TerrenoApp({
1264
+ configureApp: (router, options) => {
1301
1265
  addGptRoutes(router, {
1302
1266
  aiService: new AIService({
1303
1267
  model: streamIterationErrorModel as unknown as LanguageModel,
@@ -1307,7 +1271,7 @@ describe("AI Routes", () => {
1307
1271
  },
1308
1272
  skipListen: true,
1309
1273
  userModel: UserModel,
1310
- });
1274
+ }).build();
1311
1275
  const originalLogRequest = AIRequest.logRequest;
1312
1276
  AIRequest.logRequest = mock(async () => {
1313
1277
  throw new Error("log write failed");
@@ -1331,8 +1295,8 @@ describe("AI Routes", () => {
1331
1295
 
1332
1296
  describe("GPT tools route", () => {
1333
1297
  it("lists built-in tools from route config", async () => {
1334
- const customApp = setupServer({
1335
- addRoutes: (router, options) => {
1298
+ const customApp = new TerrenoApp({
1299
+ configureApp: (router, options) => {
1336
1300
  addGptRoutes(router, {
1337
1301
  aiService,
1338
1302
  openApiOptions: options,
@@ -1341,7 +1305,7 @@ describe("AI Routes", () => {
1341
1305
  },
1342
1306
  skipListen: true,
1343
1307
  userModel: UserModel,
1344
- });
1308
+ }).build();
1345
1309
  const agent = await authAsUser(customApp, "notAdmin");
1346
1310
  const res = await agent.get("/gpt/tools");
1347
1311
  expect(res.status).toBe(200);
@@ -1352,13 +1316,13 @@ describe("AI Routes", () => {
1352
1316
  const createRequestTools = mock(() => ({
1353
1317
  now: {description: "Current time"} as unknown as Tool,
1354
1318
  }));
1355
- const customApp = setupServer({
1356
- addRoutes: (router, options) => {
1319
+ const customApp = new TerrenoApp({
1320
+ configureApp: (router, options) => {
1357
1321
  addGptRoutes(router, {aiService, createRequestTools, openApiOptions: options});
1358
1322
  },
1359
1323
  skipListen: true,
1360
1324
  userModel: UserModel,
1361
- });
1325
+ }).build();
1362
1326
  const agent = await authAsUser(customApp, "notAdmin");
1363
1327
  const res = await agent.get("/gpt/tools");
1364
1328
  expect(res.status).toBe(200);
@@ -1369,13 +1333,13 @@ describe("AI Routes", () => {
1369
1333
  const mcpService = {
1370
1334
  getTools: mock(async () => ({search: {description: "web search"}})),
1371
1335
  } as unknown as MCPService;
1372
- const customApp = setupServer({
1373
- addRoutes: (router, options) => {
1336
+ const customApp = new TerrenoApp({
1337
+ configureApp: (router, options) => {
1374
1338
  addGptRoutes(router, {aiService, mcpService, openApiOptions: options});
1375
1339
  },
1376
1340
  skipListen: true,
1377
1341
  userModel: UserModel,
1378
- });
1342
+ }).build();
1379
1343
  const agent = await authAsUser(customApp, "notAdmin");
1380
1344
  const res = await agent.get("/gpt/tools");
1381
1345
  expect(res.status).toBe(200);
@@ -1388,13 +1352,13 @@ describe("AI Routes", () => {
1388
1352
  throw new Error("boom");
1389
1353
  }),
1390
1354
  } as unknown as MCPService;
1391
- const customApp = setupServer({
1392
- addRoutes: (router, options) => {
1355
+ const customApp = new TerrenoApp({
1356
+ configureApp: (router, options) => {
1393
1357
  addGptRoutes(router, {aiService, mcpService, openApiOptions: options});
1394
1358
  },
1395
1359
  skipListen: true,
1396
1360
  userModel: UserModel,
1397
- });
1361
+ }).build();
1398
1362
  const agent = await authAsUser(customApp, "notAdmin");
1399
1363
  const res = await agent.get("/gpt/tools");
1400
1364
  expect(res.status).toBe(200);
@@ -1567,8 +1531,8 @@ describe("AI Routes", () => {
1567
1531
  specificationVersion: "v2" as const,
1568
1532
  supportedUrls: {},
1569
1533
  };
1570
- const fileApp = setupServer({
1571
- addRoutes: (router, options) => {
1534
+ const fileApp = new TerrenoApp({
1535
+ configureApp: (router, options) => {
1572
1536
  addGptRoutes(router, {
1573
1537
  aiService: new AIService({model: nonImageFileModel as unknown as LanguageModel}),
1574
1538
  openApiOptions: options,
@@ -1576,7 +1540,7 @@ describe("AI Routes", () => {
1576
1540
  },
1577
1541
  skipListen: true,
1578
1542
  userModel: UserModel,
1579
- });
1543
+ }).build();
1580
1544
  const agent = await authAsUser(fileApp, "notAdmin");
1581
1545
  const res = await agent
1582
1546
  .post("/gpt/prompt")
@@ -92,5 +92,65 @@ describe("gemini model listing helpers", () => {
92
92
  const result = await listGeminiApiModels({apiKey: "key", fetchImpl});
93
93
  expect(result).toBeUndefined();
94
94
  });
95
+
96
+ it("returns undefined when fetchImpl is not available", async () => {
97
+ // Pass a falsy non-nullish value so the `??` operator keeps it (instead of
98
+ // falling through to globalThis.fetch), and `!fetchImpl` evaluates to true.
99
+ const result = await listGeminiApiModels({
100
+ apiKey: "key",
101
+ fetchImpl: 0 as unknown as typeof fetch,
102
+ });
103
+ expect(result).toBeUndefined();
104
+ });
105
+
106
+ it("returns a partial list when pagination exceeds the maximum page limit", async () => {
107
+ let callCount = 0;
108
+ const fetchImpl = mock(async () => {
109
+ callCount += 1;
110
+ return jsonResponse({
111
+ models: [
112
+ {
113
+ name: `models/gemini-page-${callCount}`,
114
+ supportedGenerationMethods: ["generateContent"],
115
+ },
116
+ ],
117
+ nextPageToken: `page-${callCount + 1}`,
118
+ });
119
+ }) as unknown as typeof fetch;
120
+
121
+ const result = await listGeminiApiModels({apiKey: "key", fetchImpl});
122
+ expect(result).toBeDefined();
123
+ expect(result?.length).toBe(10);
124
+ expect(callCount).toBe(10);
125
+ });
126
+
127
+ it("skips models with missing name", async () => {
128
+ const fetchImpl = mock(async () =>
129
+ jsonResponse({
130
+ models: [
131
+ {name: "models/gemini-valid", supportedGenerationMethods: ["generateContent"]},
132
+ {supportedGenerationMethods: ["generateContent"]},
133
+ {name: undefined, supportedGenerationMethods: ["generateContent"]},
134
+ ],
135
+ })
136
+ ) as unknown as typeof fetch;
137
+
138
+ const result = await listGeminiApiModels({apiKey: "key", fetchImpl});
139
+ expect(result).toEqual(["gemini-valid"]);
140
+ });
141
+
142
+ it("skips models with no supportedGenerationMethods when chatOnly is true", async () => {
143
+ const fetchImpl = mock(async () =>
144
+ jsonResponse({
145
+ models: [
146
+ {name: "models/gemini-chat", supportedGenerationMethods: ["generateContent"]},
147
+ {name: "models/gemini-no-methods"},
148
+ ],
149
+ })
150
+ ) as unknown as typeof fetch;
151
+
152
+ const result = await listGeminiApiModels({apiKey: "key", chatOnly: true, fetchImpl});
153
+ expect(result).toEqual(["gemini-chat"]);
154
+ });
95
155
  });
96
156
  });