@terreno/admin-backend 0.3.2 → 0.5.0

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.
@@ -0,0 +1,559 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ var bun_test_1 = require("bun:test");
43
+ var api_1 = require("@terreno/api");
44
+ var tests_1 = require("@terreno/api/src/tests");
45
+ var supertest_1 = __importDefault(require("supertest"));
46
+ var adminApp_1 = require("./adminApp");
47
+ var createTestScript = function (overrides) {
48
+ var _a, _b, _c;
49
+ return ({
50
+ description: (_a = overrides === null || overrides === void 0 ? void 0 : overrides.description) !== null && _a !== void 0 ? _a : "A test script",
51
+ name: (_b = overrides === null || overrides === void 0 ? void 0 : overrides.name) !== null && _b !== void 0 ? _b : "test-script",
52
+ runner: (_c = overrides === null || overrides === void 0 ? void 0 : overrides.runner) !== null && _c !== void 0 ? _c : (function (wetRun) { return __awaiter(void 0, void 0, void 0, function () {
53
+ return __generator(this, function (_a) {
54
+ return [2 /*return*/, ({
55
+ results: ["Ran in ".concat(wetRun ? "wet" : "dry", " mode")],
56
+ success: true,
57
+ })];
58
+ });
59
+ }); }),
60
+ });
61
+ };
62
+ var createSlowScript = function () { return ({
63
+ description: "A slow script for testing async behavior",
64
+ name: "slow-script",
65
+ runner: function (_wetRun) { return __awaiter(void 0, void 0, void 0, function () {
66
+ return __generator(this, function (_a) {
67
+ switch (_a.label) {
68
+ case 0: return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 200); })];
69
+ case 1:
70
+ _a.sent();
71
+ return [2 /*return*/, { results: ["Done"], success: true }];
72
+ }
73
+ });
74
+ }); },
75
+ }); };
76
+ var createFailingScript = function () { return ({
77
+ description: "A script that throws",
78
+ name: "failing-script",
79
+ runner: function () { return __awaiter(void 0, void 0, void 0, function () {
80
+ return __generator(this, function (_a) {
81
+ throw new Error("Script exploded");
82
+ });
83
+ }); },
84
+ }); };
85
+ var buildApp = function (scripts) {
86
+ if (scripts === void 0) { scripts = [createTestScript()]; }
87
+ var app = (0, tests_1.getBaseServer)();
88
+ (0, api_1.setupAuth)(app, tests_1.UserModel);
89
+ (0, api_1.addAuthRoutes)(app, tests_1.UserModel);
90
+ var admin = new adminApp_1.AdminApp({
91
+ basePath: "/admin",
92
+ models: [],
93
+ scripts: scripts,
94
+ });
95
+ admin.register(app);
96
+ // Error middleware must come after routes
97
+ app.use(api_1.apiUnauthorizedMiddleware);
98
+ app.use(api_1.apiErrorMiddleware);
99
+ return app;
100
+ };
101
+ /** Wait long enough for async scripts to settle before cleanup. */
102
+ var waitForScripts = function () { return new Promise(function (resolve) { return setTimeout(resolve, 500); }); };
103
+ (0, bun_test_1.describe)("AdminApp script routes", function () {
104
+ var app;
105
+ var adminAgent;
106
+ var notAdminAgent;
107
+ (0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
108
+ return __generator(this, function (_a) {
109
+ switch (_a.label) {
110
+ case 0:
111
+ // Wait for any async script runners to finish before deleting tasks
112
+ return [4 /*yield*/, waitForScripts()];
113
+ case 1:
114
+ // Wait for any async script runners to finish before deleting tasks
115
+ _a.sent();
116
+ return [4 /*yield*/, api_1.BackgroundTask.deleteMany({})];
117
+ case 2:
118
+ _a.sent();
119
+ return [2 /*return*/];
120
+ }
121
+ });
122
+ }); });
123
+ (0, bun_test_1.describe)("POST /admin/scripts/:name/run", function () {
124
+ (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
125
+ return __generator(this, function (_a) {
126
+ switch (_a.label) {
127
+ case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
128
+ case 1:
129
+ _a.sent();
130
+ app = buildApp();
131
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
132
+ case 2:
133
+ adminAgent = _a.sent();
134
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
135
+ case 3:
136
+ notAdminAgent = _a.sent();
137
+ return [2 /*return*/];
138
+ }
139
+ });
140
+ }); });
141
+ (0, bun_test_1.it)("starts a script and returns a taskId", function () { return __awaiter(void 0, void 0, void 0, function () {
142
+ var res, task;
143
+ return __generator(this, function (_a) {
144
+ switch (_a.label) {
145
+ case 0: return [4 /*yield*/, adminAgent.post("/admin/scripts/test-script/run").expect(201)];
146
+ case 1:
147
+ res = _a.sent();
148
+ (0, bun_test_1.expect)(res.body.taskId).toBeDefined();
149
+ (0, bun_test_1.expect)(typeof res.body.taskId).toBe("string");
150
+ return [4 /*yield*/, api_1.BackgroundTask.findById(res.body.taskId)];
151
+ case 2:
152
+ task = _a.sent();
153
+ (0, bun_test_1.expect)(task).not.toBeNull();
154
+ (0, bun_test_1.expect)(task.taskType).toBe("test-script");
155
+ (0, bun_test_1.expect)(task.isDryRun).toBe(true);
156
+ return [2 /*return*/];
157
+ }
158
+ });
159
+ }); });
160
+ (0, bun_test_1.it)("creates a wet run task when wetRun=true", function () { return __awaiter(void 0, void 0, void 0, function () {
161
+ var res, task;
162
+ return __generator(this, function (_a) {
163
+ switch (_a.label) {
164
+ case 0: return [4 /*yield*/, adminAgent.post("/admin/scripts/test-script/run?wetRun=true").expect(201)];
165
+ case 1:
166
+ res = _a.sent();
167
+ return [4 /*yield*/, api_1.BackgroundTask.findById(res.body.taskId)];
168
+ case 2:
169
+ task = _a.sent();
170
+ (0, bun_test_1.expect)(task).not.toBeNull();
171
+ (0, bun_test_1.expect)(task.isDryRun).toBe(false);
172
+ return [2 /*return*/];
173
+ }
174
+ });
175
+ }); });
176
+ (0, bun_test_1.it)("creates a dry run task by default", function () { return __awaiter(void 0, void 0, void 0, function () {
177
+ var res, task;
178
+ return __generator(this, function (_a) {
179
+ switch (_a.label) {
180
+ case 0: return [4 /*yield*/, adminAgent.post("/admin/scripts/test-script/run").expect(201)];
181
+ case 1:
182
+ res = _a.sent();
183
+ return [4 /*yield*/, api_1.BackgroundTask.findById(res.body.taskId)];
184
+ case 2:
185
+ task = _a.sent();
186
+ (0, bun_test_1.expect)(task).not.toBeNull();
187
+ (0, bun_test_1.expect)(task.isDryRun).toBe(true);
188
+ return [2 /*return*/];
189
+ }
190
+ });
191
+ }); });
192
+ (0, bun_test_1.it)("returns 404 for unknown script", function () { return __awaiter(void 0, void 0, void 0, function () {
193
+ var res;
194
+ return __generator(this, function (_a) {
195
+ switch (_a.label) {
196
+ case 0: return [4 /*yield*/, adminAgent.post("/admin/scripts/nonexistent/run").expect(404)];
197
+ case 1:
198
+ res = _a.sent();
199
+ (0, bun_test_1.expect)(res.body.title).toInclude("Script not found");
200
+ return [2 /*return*/];
201
+ }
202
+ });
203
+ }); });
204
+ (0, bun_test_1.it)("returns 403 for non-admin user", function () { return __awaiter(void 0, void 0, void 0, function () {
205
+ var res;
206
+ return __generator(this, function (_a) {
207
+ switch (_a.label) {
208
+ case 0: return [4 /*yield*/, notAdminAgent.post("/admin/scripts/test-script/run").expect(403)];
209
+ case 1:
210
+ res = _a.sent();
211
+ (0, bun_test_1.expect)(res.body.title).toInclude("Only admins can run scripts");
212
+ return [2 /*return*/];
213
+ }
214
+ });
215
+ }); });
216
+ (0, bun_test_1.it)("returns 401 for unauthenticated user", function () { return __awaiter(void 0, void 0, void 0, function () {
217
+ var unauthAgent;
218
+ return __generator(this, function (_a) {
219
+ switch (_a.label) {
220
+ case 0:
221
+ unauthAgent = (0, supertest_1.default)(app);
222
+ return [4 /*yield*/, unauthAgent.post("/admin/scripts/test-script/run").expect(401)];
223
+ case 1:
224
+ _a.sent();
225
+ return [2 /*return*/];
226
+ }
227
+ });
228
+ }); });
229
+ (0, bun_test_1.it)("records initial log with admin name", function () { return __awaiter(void 0, void 0, void 0, function () {
230
+ var res, task;
231
+ return __generator(this, function (_a) {
232
+ switch (_a.label) {
233
+ case 0: return [4 /*yield*/, adminAgent.post("/admin/scripts/test-script/run").expect(201)];
234
+ case 1:
235
+ res = _a.sent();
236
+ return [4 /*yield*/, api_1.BackgroundTask.findById(res.body.taskId)];
237
+ case 2:
238
+ task = _a.sent();
239
+ (0, bun_test_1.expect)(task.logs).toHaveLength(1);
240
+ (0, bun_test_1.expect)(task.logs[0].level).toBe("info");
241
+ (0, bun_test_1.expect)(task.logs[0].message).toInclude("Script started by");
242
+ return [2 /*return*/];
243
+ }
244
+ });
245
+ }); });
246
+ (0, bun_test_1.it)("completes the task asynchronously with results", function () { return __awaiter(void 0, void 0, void 0, function () {
247
+ var res, task;
248
+ var _a;
249
+ return __generator(this, function (_b) {
250
+ switch (_b.label) {
251
+ case 0: return [4 /*yield*/, adminAgent.post("/admin/scripts/test-script/run").expect(201)];
252
+ case 1:
253
+ res = _b.sent();
254
+ // Wait for async script execution
255
+ return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 100); })];
256
+ case 2:
257
+ // Wait for async script execution
258
+ _b.sent();
259
+ return [4 /*yield*/, api_1.BackgroundTask.findById(res.body.taskId)];
260
+ case 3:
261
+ task = _b.sent();
262
+ (0, bun_test_1.expect)(task.status).toBe("completed");
263
+ (0, bun_test_1.expect)(task.result).toContain("Ran in dry mode");
264
+ (0, bun_test_1.expect)(task.completedAt).toBeDefined();
265
+ (0, bun_test_1.expect)((_a = task.progress) === null || _a === void 0 ? void 0 : _a.percentage).toBe(100);
266
+ return [2 /*return*/];
267
+ }
268
+ });
269
+ }); });
270
+ (0, bun_test_1.it)("marks task as failed when script throws", function () { return __awaiter(void 0, void 0, void 0, function () {
271
+ var res, task;
272
+ return __generator(this, function (_a) {
273
+ switch (_a.label) {
274
+ case 0:
275
+ app = buildApp([createFailingScript()]);
276
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
277
+ case 1:
278
+ adminAgent = _a.sent();
279
+ return [4 /*yield*/, adminAgent.post("/admin/scripts/failing-script/run").expect(201)];
280
+ case 2:
281
+ res = _a.sent();
282
+ return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 100); })];
283
+ case 3:
284
+ _a.sent();
285
+ return [4 /*yield*/, api_1.BackgroundTask.findById(res.body.taskId)];
286
+ case 4:
287
+ task = _a.sent();
288
+ (0, bun_test_1.expect)(task.status).toBe("failed");
289
+ (0, bun_test_1.expect)(task === null || task === void 0 ? void 0 : task.error).toBe("Script exploded");
290
+ (0, bun_test_1.expect)(task.result).toContain("Script exploded");
291
+ return [2 /*return*/];
292
+ }
293
+ });
294
+ }); });
295
+ });
296
+ (0, bun_test_1.describe)("GET /admin/scripts/tasks/:id", function () {
297
+ var taskId;
298
+ (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
299
+ var res;
300
+ return __generator(this, function (_a) {
301
+ switch (_a.label) {
302
+ case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
303
+ case 1:
304
+ _a.sent();
305
+ app = buildApp([createSlowScript()]);
306
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
307
+ case 2:
308
+ adminAgent = _a.sent();
309
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
310
+ case 3:
311
+ notAdminAgent = _a.sent();
312
+ return [4 /*yield*/, adminAgent.post("/admin/scripts/slow-script/run").expect(201)];
313
+ case 4:
314
+ res = _a.sent();
315
+ taskId = res.body.taskId;
316
+ return [2 /*return*/];
317
+ }
318
+ });
319
+ }); });
320
+ (0, bun_test_1.it)("returns task status for admin", function () { return __awaiter(void 0, void 0, void 0, function () {
321
+ var res;
322
+ return __generator(this, function (_a) {
323
+ switch (_a.label) {
324
+ case 0: return [4 /*yield*/, adminAgent.get("/admin/scripts/tasks/".concat(taskId)).expect(200)];
325
+ case 1:
326
+ res = _a.sent();
327
+ (0, bun_test_1.expect)(res.body.task).toBeDefined();
328
+ (0, bun_test_1.expect)(res.body.task._id).toBe(taskId);
329
+ (0, bun_test_1.expect)(res.body.task.taskType).toBe("slow-script");
330
+ (0, bun_test_1.expect)(res.body.task.status).toBeDefined();
331
+ return [2 /*return*/];
332
+ }
333
+ });
334
+ }); });
335
+ (0, bun_test_1.it)("returns 403 for non-admin user", function () { return __awaiter(void 0, void 0, void 0, function () {
336
+ var res;
337
+ return __generator(this, function (_a) {
338
+ switch (_a.label) {
339
+ case 0: return [4 /*yield*/, notAdminAgent.get("/admin/scripts/tasks/".concat(taskId)).expect(403)];
340
+ case 1:
341
+ res = _a.sent();
342
+ (0, bun_test_1.expect)(res.body.title).toInclude("Only admins can view tasks");
343
+ return [2 /*return*/];
344
+ }
345
+ });
346
+ }); });
347
+ (0, bun_test_1.it)("returns 404 for nonexistent task", function () { return __awaiter(void 0, void 0, void 0, function () {
348
+ var fakeId, res;
349
+ return __generator(this, function (_a) {
350
+ switch (_a.label) {
351
+ case 0:
352
+ fakeId = "000000000000000000000000";
353
+ return [4 /*yield*/, adminAgent.get("/admin/scripts/tasks/".concat(fakeId)).expect(404)];
354
+ case 1:
355
+ res = _a.sent();
356
+ (0, bun_test_1.expect)(res.body.title).toInclude("Task not found");
357
+ return [2 /*return*/];
358
+ }
359
+ });
360
+ }); });
361
+ (0, bun_test_1.it)("returns 400 for invalid task ID", function () { return __awaiter(void 0, void 0, void 0, function () {
362
+ var res;
363
+ return __generator(this, function (_a) {
364
+ switch (_a.label) {
365
+ case 0: return [4 /*yield*/, adminAgent.get("/admin/scripts/tasks/not-a-valid-id").expect(400)];
366
+ case 1:
367
+ res = _a.sent();
368
+ (0, bun_test_1.expect)(res.body.title).toInclude("Invalid task ID");
369
+ return [2 /*return*/];
370
+ }
371
+ });
372
+ }); });
373
+ (0, bun_test_1.it)("shows completed task with results after script finishes", function () { return __awaiter(void 0, void 0, void 0, function () {
374
+ var res;
375
+ return __generator(this, function (_a) {
376
+ switch (_a.label) {
377
+ case 0:
378
+ // Wait for the slow script to finish
379
+ return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 400); })];
380
+ case 1:
381
+ // Wait for the slow script to finish
382
+ _a.sent();
383
+ return [4 /*yield*/, adminAgent.get("/admin/scripts/tasks/".concat(taskId)).expect(200)];
384
+ case 2:
385
+ res = _a.sent();
386
+ (0, bun_test_1.expect)(res.body.task.status).toBe("completed");
387
+ (0, bun_test_1.expect)(res.body.task.result).toContain("Done");
388
+ return [2 /*return*/];
389
+ }
390
+ });
391
+ }); });
392
+ });
393
+ (0, bun_test_1.describe)("DELETE /admin/scripts/tasks/:id", function () {
394
+ (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
395
+ return __generator(this, function (_a) {
396
+ switch (_a.label) {
397
+ case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
398
+ case 1:
399
+ _a.sent();
400
+ app = buildApp([createSlowScript()]);
401
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
402
+ case 2:
403
+ adminAgent = _a.sent();
404
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
405
+ case 3:
406
+ notAdminAgent = _a.sent();
407
+ return [2 /*return*/];
408
+ }
409
+ });
410
+ }); });
411
+ (0, bun_test_1.it)("cancels a running task", function () { return __awaiter(void 0, void 0, void 0, function () {
412
+ var res1, res;
413
+ return __generator(this, function (_a) {
414
+ switch (_a.label) {
415
+ case 0: return [4 /*yield*/, adminAgent.post("/admin/scripts/slow-script/run").expect(201)];
416
+ case 1:
417
+ res1 = _a.sent();
418
+ return [4 /*yield*/, adminAgent.delete("/admin/scripts/tasks/".concat(res1.body.taskId)).expect(200)];
419
+ case 2:
420
+ res = _a.sent();
421
+ (0, bun_test_1.expect)(res.body.message).toBe("Task cancelled");
422
+ (0, bun_test_1.expect)(res.body.task.status).toBe("cancelled");
423
+ (0, bun_test_1.expect)(res.body.task.completedAt).toBeDefined();
424
+ return [2 /*return*/];
425
+ }
426
+ });
427
+ }); });
428
+ (0, bun_test_1.it)("adds cancellation log entry", function () { return __awaiter(void 0, void 0, void 0, function () {
429
+ var res1, res, logs, cancelLog;
430
+ return __generator(this, function (_a) {
431
+ switch (_a.label) {
432
+ case 0: return [4 /*yield*/, adminAgent.post("/admin/scripts/slow-script/run").expect(201)];
433
+ case 1:
434
+ res1 = _a.sent();
435
+ return [4 /*yield*/, adminAgent.delete("/admin/scripts/tasks/".concat(res1.body.taskId)).expect(200)];
436
+ case 2:
437
+ res = _a.sent();
438
+ logs = res.body.task.logs;
439
+ cancelLog = logs.find(function (l) { return l.message.includes("cancelled"); });
440
+ (0, bun_test_1.expect)(cancelLog).toBeDefined();
441
+ (0, bun_test_1.expect)(cancelLog.level).toBe("info");
442
+ return [2 /*return*/];
443
+ }
444
+ });
445
+ }); });
446
+ (0, bun_test_1.it)("returns 403 for non-admin user", function () { return __awaiter(void 0, void 0, void 0, function () {
447
+ var res1, res;
448
+ return __generator(this, function (_a) {
449
+ switch (_a.label) {
450
+ case 0: return [4 /*yield*/, adminAgent.post("/admin/scripts/slow-script/run").expect(201)];
451
+ case 1:
452
+ res1 = _a.sent();
453
+ return [4 /*yield*/, notAdminAgent
454
+ .delete("/admin/scripts/tasks/".concat(res1.body.taskId))
455
+ .expect(403)];
456
+ case 2:
457
+ res = _a.sent();
458
+ (0, bun_test_1.expect)(res.body.title).toInclude("Only admins can cancel tasks");
459
+ return [2 /*return*/];
460
+ }
461
+ });
462
+ }); });
463
+ (0, bun_test_1.it)("returns 400 when cancelling a completed task", function () { return __awaiter(void 0, void 0, void 0, function () {
464
+ var res1, res;
465
+ return __generator(this, function (_a) {
466
+ switch (_a.label) {
467
+ case 0: return [4 /*yield*/, adminAgent.post("/admin/scripts/slow-script/run").expect(201)];
468
+ case 1:
469
+ res1 = _a.sent();
470
+ // Wait for task to complete
471
+ return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 400); })];
472
+ case 2:
473
+ // Wait for task to complete
474
+ _a.sent();
475
+ return [4 /*yield*/, adminAgent.delete("/admin/scripts/tasks/".concat(res1.body.taskId)).expect(400)];
476
+ case 3:
477
+ res = _a.sent();
478
+ (0, bun_test_1.expect)(res.body.title).toInclude("Cannot cancel task with status");
479
+ return [2 /*return*/];
480
+ }
481
+ });
482
+ }); });
483
+ (0, bun_test_1.it)("returns 404 for nonexistent task", function () { return __awaiter(void 0, void 0, void 0, function () {
484
+ var fakeId, res;
485
+ return __generator(this, function (_a) {
486
+ switch (_a.label) {
487
+ case 0:
488
+ fakeId = "000000000000000000000000";
489
+ return [4 /*yield*/, adminAgent.delete("/admin/scripts/tasks/".concat(fakeId)).expect(404)];
490
+ case 1:
491
+ res = _a.sent();
492
+ (0, bun_test_1.expect)(res.body.title).toInclude("Task not found");
493
+ return [2 /*return*/];
494
+ }
495
+ });
496
+ }); });
497
+ (0, bun_test_1.it)("returns 400 for invalid task ID", function () { return __awaiter(void 0, void 0, void 0, function () {
498
+ var res;
499
+ return __generator(this, function (_a) {
500
+ switch (_a.label) {
501
+ case 0: return [4 /*yield*/, adminAgent.delete("/admin/scripts/tasks/not-a-valid-id").expect(400)];
502
+ case 1:
503
+ res = _a.sent();
504
+ (0, bun_test_1.expect)(res.body.title).toInclude("Invalid task ID");
505
+ return [2 /*return*/];
506
+ }
507
+ });
508
+ }); });
509
+ });
510
+ (0, bun_test_1.describe)("GET /admin/config with scripts", function () {
511
+ (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
512
+ return __generator(this, function (_a) {
513
+ switch (_a.label) {
514
+ case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
515
+ case 1:
516
+ _a.sent();
517
+ app = buildApp([
518
+ createTestScript({ description: "Migrate old data", name: "migrate-data" }),
519
+ createTestScript({ description: "Clean up orphans", name: "cleanup" }),
520
+ ]);
521
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
522
+ case 2:
523
+ adminAgent = _a.sent();
524
+ return [2 /*return*/];
525
+ }
526
+ });
527
+ }); });
528
+ (0, bun_test_1.it)("includes scripts in config response", function () { return __awaiter(void 0, void 0, void 0, function () {
529
+ var res;
530
+ return __generator(this, function (_a) {
531
+ switch (_a.label) {
532
+ case 0: return [4 /*yield*/, (0, supertest_1.default)(app).get("/admin/config").expect(200)];
533
+ case 1:
534
+ res = _a.sent();
535
+ (0, bun_test_1.expect)(res.body.scripts).toHaveLength(2);
536
+ (0, bun_test_1.expect)(res.body.scripts[0].name).toBe("migrate-data");
537
+ (0, bun_test_1.expect)(res.body.scripts[0].description).toBe("Migrate old data");
538
+ (0, bun_test_1.expect)(res.body.scripts[1].name).toBe("cleanup");
539
+ (0, bun_test_1.expect)(res.body.scripts[1].description).toBe("Clean up orphans");
540
+ return [2 /*return*/];
541
+ }
542
+ });
543
+ }); });
544
+ (0, bun_test_1.it)("returns empty scripts array when no scripts configured", function () { return __awaiter(void 0, void 0, void 0, function () {
545
+ var res;
546
+ return __generator(this, function (_a) {
547
+ switch (_a.label) {
548
+ case 0:
549
+ app = buildApp([]);
550
+ return [4 /*yield*/, (0, supertest_1.default)(app).get("/admin/config").expect(200)];
551
+ case 1:
552
+ res = _a.sent();
553
+ (0, bun_test_1.expect)(res.body.scripts).toHaveLength(0);
554
+ return [2 /*return*/];
555
+ }
556
+ });
557
+ }); });
558
+ });
559
+ });
@@ -0,0 +1,31 @@
1
+ import type express from "express";
2
+ export interface DocumentStorageOptions {
3
+ bucketName: string;
4
+ folderPrefix?: string;
5
+ basePath?: string;
6
+ allowedMimeTypes?: string[];
7
+ maxFileSize?: number;
8
+ signedUrlExpiration?: number;
9
+ }
10
+ export interface DocumentFile {
11
+ name: string;
12
+ fullPath: string;
13
+ size: number;
14
+ contentType: string | undefined;
15
+ updated: string;
16
+ isFolder: boolean;
17
+ }
18
+ export interface DocumentListResponse {
19
+ files: DocumentFile[];
20
+ folders: string[];
21
+ prefix: string;
22
+ }
23
+ export declare class DocumentStorageApp {
24
+ private options;
25
+ private storage;
26
+ constructor(options: DocumentStorageOptions);
27
+ private get bucket();
28
+ private get prefix();
29
+ private get allowedMimeTypes();
30
+ register(app: express.Application): void;
31
+ }