@terreno/api 0.23.0 → 0.23.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/dist/api.test.js +6 -2
- package/dist/auth.test.js +52 -0
- package/dist/consentApp.d.ts +1 -1
- package/dist/envConfigurationPlugin.test.js +4 -6
- package/dist/notifiers/zoomNotifier.test.js +3 -2
- package/dist/scriptRunner.test.js +235 -0
- package/package.json +11 -11
- package/src/api.test.ts +5 -1
- package/src/auth.test.ts +38 -0
- package/src/consentApp.ts +1 -1
- package/src/envConfigurationPlugin.test.ts +4 -6
- package/src/notifiers/zoomNotifier.test.ts +7 -4
- package/src/scriptRunner.test.ts +111 -2
package/dist/api.test.js
CHANGED
|
@@ -2639,15 +2639,19 @@ var transformers_1 = require("./transformers");
|
|
|
2639
2639
|
});
|
|
2640
2640
|
}); });
|
|
2641
2641
|
(0, bun_test_1.it)("handles doc timestamp stored as ISO string", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
2642
|
-
var res;
|
|
2642
|
+
var ifUnmodifiedSince, res;
|
|
2643
2643
|
return __generator(this, function (_a) {
|
|
2644
2644
|
switch (_a.label) {
|
|
2645
2645
|
case 0: return [4 /*yield*/, tests_1.FoodModel.collection.updateOne({ _id: spinach._id }, { $set: { updated: "2025-06-15T12:00:00.000Z" } })];
|
|
2646
2646
|
case 1:
|
|
2647
2647
|
_a.sent();
|
|
2648
|
+
ifUnmodifiedSince = luxon_1.DateTime.fromISO("2025-06-15T11:00:00.000Z").toHTTP();
|
|
2649
|
+
if (!ifUnmodifiedSince) {
|
|
2650
|
+
throw new Error("Expected valid HTTP date");
|
|
2651
|
+
}
|
|
2648
2652
|
return [4 /*yield*/, agent
|
|
2649
2653
|
.patch("/food/".concat(spinach._id))
|
|
2650
|
-
.set("If-Unmodified-Since",
|
|
2654
|
+
.set("If-Unmodified-Since", ifUnmodifiedSince)
|
|
2651
2655
|
.send({ name: "String Timestamp" })
|
|
2652
2656
|
.expect(409)];
|
|
2653
2657
|
case 2:
|
package/dist/auth.test.js
CHANGED
|
@@ -1702,3 +1702,55 @@ var decodeTokenPayload = function (token) {
|
|
|
1702
1702
|
});
|
|
1703
1703
|
}); });
|
|
1704
1704
|
});
|
|
1705
|
+
(0, bun_test_1.describe)("PATCH /me route edge cases", function () {
|
|
1706
|
+
var app;
|
|
1707
|
+
var agent;
|
|
1708
|
+
(0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1709
|
+
return __generator(this, function (_a) {
|
|
1710
|
+
switch (_a.label) {
|
|
1711
|
+
case 0:
|
|
1712
|
+
(0, bun_test_1.setSystemTime)();
|
|
1713
|
+
return [4 /*yield*/, (0, tests_1.setupTestData)()];
|
|
1714
|
+
case 1:
|
|
1715
|
+
_a.sent();
|
|
1716
|
+
app = new terrenoApp_1.TerrenoApp({
|
|
1717
|
+
configureApp: function () { },
|
|
1718
|
+
skipListen: true,
|
|
1719
|
+
userModel: tests_1.UserModel,
|
|
1720
|
+
}).build();
|
|
1721
|
+
agent = supertest_1.default.agent(app);
|
|
1722
|
+
return [2 /*return*/];
|
|
1723
|
+
}
|
|
1724
|
+
});
|
|
1725
|
+
}); });
|
|
1726
|
+
(0, bun_test_1.afterEach)(function () {
|
|
1727
|
+
(0, bun_test_1.setSystemTime)();
|
|
1728
|
+
});
|
|
1729
|
+
(0, bun_test_1.it)("returns 404 for PATCH /me when authenticated user is deleted from DB", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1730
|
+
var loginRes, _a, token, userId, freshAgent, res;
|
|
1731
|
+
return __generator(this, function (_b) {
|
|
1732
|
+
switch (_b.label) {
|
|
1733
|
+
case 0: return [4 /*yield*/, agent
|
|
1734
|
+
.post("/auth/login")
|
|
1735
|
+
.send({ email: "notAdmin@example.com", password: "password" })
|
|
1736
|
+
.expect(200)];
|
|
1737
|
+
case 1:
|
|
1738
|
+
loginRes = _b.sent();
|
|
1739
|
+
_a = loginRes.body.data, token = _a.token, userId = _a.userId;
|
|
1740
|
+
return [4 /*yield*/, tests_1.UserModel.deleteOne({ _id: userId })];
|
|
1741
|
+
case 2:
|
|
1742
|
+
_b.sent();
|
|
1743
|
+
freshAgent = supertest_1.default.agent(app);
|
|
1744
|
+
return [4 /*yield*/, freshAgent
|
|
1745
|
+
.patch("/auth/me")
|
|
1746
|
+
.set("authorization", "Bearer ".concat(token))
|
|
1747
|
+
.send({ name: "Updated" })];
|
|
1748
|
+
case 3:
|
|
1749
|
+
res = _b.sent();
|
|
1750
|
+
// Without user in DB, should get 401 or 404
|
|
1751
|
+
(0, bun_test_1.expect)([401, 404]).toContain(res.status);
|
|
1752
|
+
return [2 /*return*/];
|
|
1753
|
+
}
|
|
1754
|
+
});
|
|
1755
|
+
}); });
|
|
1756
|
+
});
|
package/dist/consentApp.d.ts
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { type Application } from "express";
|
|
9
9
|
import type { User } from "./auth";
|
|
10
|
-
import type { TerrenoPlugin } from "./terrenoPlugin";
|
|
11
10
|
import type { PopulatePath } from "./populate";
|
|
11
|
+
import type { TerrenoPlugin } from "./terrenoPlugin";
|
|
12
12
|
import type { ConsentFormDocument } from "./types/consentForm";
|
|
13
13
|
/** Shared populate config for consent response list/read endpoints. */
|
|
14
14
|
export declare const consentResponsePopulatePaths: PopulatePath[];
|
|
@@ -108,7 +108,6 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
108
108
|
};
|
|
109
109
|
var _a;
|
|
110
110
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
111
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test model typing
|
|
112
111
|
var bun_test_1 = require("bun:test");
|
|
113
112
|
var mongoose_1 = __importStar(require("mongoose"));
|
|
114
113
|
var config_1 = require("./config");
|
|
@@ -340,7 +339,7 @@ var setupLoader = function () {
|
|
|
340
339
|
});
|
|
341
340
|
}); });
|
|
342
341
|
(0, bun_test_1.it)("refreshFromDoc logs a warning and does not throw when the model query fails", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
343
|
-
var doc,
|
|
342
|
+
var doc, findSpy;
|
|
344
343
|
return __generator(this, function (_a) {
|
|
345
344
|
switch (_a.label) {
|
|
346
345
|
case 0:
|
|
@@ -351,17 +350,16 @@ var setupLoader = function () {
|
|
|
351
350
|
_a.sent();
|
|
352
351
|
// Pre-set cache to verify it is NOT overwritten when the hook errors
|
|
353
352
|
config_1.Config.setCachedEnv({ TERRENO_PLUGIN_KEY: "cached" });
|
|
354
|
-
|
|
355
|
-
TestEnvConfig.find = function () {
|
|
353
|
+
findSpy = (0, bun_test_1.spyOn)(TestEnvConfig, "find").mockImplementation(function () {
|
|
356
354
|
throw new Error("Simulated DB error");
|
|
357
|
-
};
|
|
355
|
+
});
|
|
358
356
|
// Trigger the post-findOneAndUpdate hook → refreshFromDoc → findOneOrNoneFor → throws
|
|
359
357
|
return [4 /*yield*/, TestEnvConfig.findOneAndUpdate({ _id: doc._id }, { $set: { __v: 2 } })];
|
|
360
358
|
case 2:
|
|
361
359
|
// Trigger the post-findOneAndUpdate hook → refreshFromDoc → findOneOrNoneFor → throws
|
|
362
360
|
_a.sent();
|
|
363
361
|
// Restore immediately
|
|
364
|
-
|
|
362
|
+
findSpy.mockRestore();
|
|
365
363
|
// The catch block should have swallowed the error; cache keeps old value
|
|
366
364
|
(0, bun_test_1.expect)(config_1.Config.get("TERRENO_PLUGIN_KEY")).toBe("cached");
|
|
367
365
|
return [2 /*return*/];
|
|
@@ -99,7 +99,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
99
99
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
100
100
|
};
|
|
101
101
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
102
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
103
102
|
var bun_test_1 = require("bun:test");
|
|
104
103
|
var Sentry = __importStar(require("@sentry/bun"));
|
|
105
104
|
var axios_1 = __importDefault(require("axios"));
|
|
@@ -108,7 +107,9 @@ var zoomNotifier_1 = require("./zoomNotifier");
|
|
|
108
107
|
var mockAxiosPost;
|
|
109
108
|
var ORIGINAL_ENV = process.env;
|
|
110
109
|
(0, bun_test_1.beforeEach)(function () {
|
|
111
|
-
mockAxiosPost = (0, bun_test_1.spyOn)(axios_1.default, "post").mockResolvedValue({
|
|
110
|
+
mockAxiosPost = (0, bun_test_1.spyOn)(axios_1.default, "post").mockResolvedValue({
|
|
111
|
+
status: 200,
|
|
112
|
+
});
|
|
112
113
|
process.env = __assign({}, ORIGINAL_ENV);
|
|
113
114
|
process.env.ZOOM_CHAT_WEBHOOKS = undefined;
|
|
114
115
|
Sentry.captureException.mockClear();
|
|
@@ -1,6 +1,46 @@
|
|
|
1
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
|
+
};
|
|
2
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
42
|
var bun_test_1 = require("bun:test");
|
|
43
|
+
var mongoose_1 = __importDefault(require("mongoose"));
|
|
4
44
|
var scriptRunner_1 = require("./scriptRunner");
|
|
5
45
|
(0, bun_test_1.describe)("parseScriptArgs", function () {
|
|
6
46
|
(0, bun_test_1.it)("parses --name=value form", function () {
|
|
@@ -149,3 +189,198 @@ var scriptRunner_1 = require("./scriptRunner");
|
|
|
149
189
|
});
|
|
150
190
|
});
|
|
151
191
|
});
|
|
192
|
+
(0, bun_test_1.describe)("TaskCancelledError", function () {
|
|
193
|
+
(0, bun_test_1.it)("sets the message and name", function () {
|
|
194
|
+
var err = new scriptRunner_1.TaskCancelledError("task-123");
|
|
195
|
+
(0, bun_test_1.expect)(err.message).toBe("Task task-123 was cancelled");
|
|
196
|
+
(0, bun_test_1.expect)(err.name).toBe("TaskCancelledError");
|
|
197
|
+
});
|
|
198
|
+
(0, bun_test_1.it)("is an instance of Error", function () {
|
|
199
|
+
var err = new scriptRunner_1.TaskCancelledError("abc");
|
|
200
|
+
(0, bun_test_1.expect)(err).toBeInstanceOf(Error);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
(0, bun_test_1.describe)("BackgroundTask model", function () {
|
|
204
|
+
(0, bun_test_1.beforeAll)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
205
|
+
var _a;
|
|
206
|
+
return __generator(this, function (_b) {
|
|
207
|
+
switch (_b.label) {
|
|
208
|
+
case 0:
|
|
209
|
+
if (!(mongoose_1.default.connection.readyState !== 1)) return [3 /*break*/, 2];
|
|
210
|
+
return [4 /*yield*/, mongoose_1.default.connect((_a = process.env.MONGO_URL) !== null && _a !== void 0 ? _a : "mongodb://127.0.0.1/terreno-test")];
|
|
211
|
+
case 1:
|
|
212
|
+
_b.sent();
|
|
213
|
+
_b.label = 2;
|
|
214
|
+
case 2: return [4 /*yield*/, scriptRunner_1.BackgroundTask.deleteMany({})];
|
|
215
|
+
case 3:
|
|
216
|
+
_b.sent();
|
|
217
|
+
return [2 /*return*/];
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}); });
|
|
221
|
+
(0, bun_test_1.afterAll)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
222
|
+
return __generator(this, function (_a) {
|
|
223
|
+
switch (_a.label) {
|
|
224
|
+
case 0: return [4 /*yield*/, scriptRunner_1.BackgroundTask.deleteMany({})];
|
|
225
|
+
case 1:
|
|
226
|
+
_a.sent();
|
|
227
|
+
return [2 /*return*/];
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
}); });
|
|
231
|
+
(0, bun_test_1.it)("creates a task with required fields", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
232
|
+
var task;
|
|
233
|
+
return __generator(this, function (_a) {
|
|
234
|
+
switch (_a.label) {
|
|
235
|
+
case 0: return [4 /*yield*/, scriptRunner_1.BackgroundTask.create({ taskType: "test-task" })];
|
|
236
|
+
case 1:
|
|
237
|
+
task = _a.sent();
|
|
238
|
+
(0, bun_test_1.expect)(task.taskType).toBe("test-task");
|
|
239
|
+
(0, bun_test_1.expect)(task.status).toBe("pending");
|
|
240
|
+
(0, bun_test_1.expect)(task.isDryRun).toBe(false);
|
|
241
|
+
(0, bun_test_1.expect)(task.logs).toEqual([]);
|
|
242
|
+
return [2 /*return*/];
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
}); });
|
|
246
|
+
(0, bun_test_1.describe)("addLog", function () {
|
|
247
|
+
(0, bun_test_1.it)("appends a log entry and saves", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
248
|
+
var task, reloaded;
|
|
249
|
+
return __generator(this, function (_a) {
|
|
250
|
+
switch (_a.label) {
|
|
251
|
+
case 0: return [4 /*yield*/, scriptRunner_1.BackgroundTask.create({ taskType: "log-test" })];
|
|
252
|
+
case 1:
|
|
253
|
+
task = _a.sent();
|
|
254
|
+
return [4 /*yield*/, task.addLog("info", "step completed")];
|
|
255
|
+
case 2:
|
|
256
|
+
_a.sent();
|
|
257
|
+
return [4 /*yield*/, scriptRunner_1.BackgroundTask.findById(task._id)];
|
|
258
|
+
case 3:
|
|
259
|
+
reloaded = _a.sent();
|
|
260
|
+
(0, bun_test_1.expect)(reloaded === null || reloaded === void 0 ? void 0 : reloaded.logs).toHaveLength(1);
|
|
261
|
+
(0, bun_test_1.expect)(reloaded === null || reloaded === void 0 ? void 0 : reloaded.logs[0].level).toBe("info");
|
|
262
|
+
(0, bun_test_1.expect)(reloaded === null || reloaded === void 0 ? void 0 : reloaded.logs[0].message).toBe("step completed");
|
|
263
|
+
(0, bun_test_1.expect)(reloaded === null || reloaded === void 0 ? void 0 : reloaded.logs[0].timestamp).toBeDefined();
|
|
264
|
+
return [2 /*return*/];
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}); });
|
|
268
|
+
(0, bun_test_1.it)("appends multiple log entries", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
269
|
+
var task, reloaded;
|
|
270
|
+
return __generator(this, function (_a) {
|
|
271
|
+
switch (_a.label) {
|
|
272
|
+
case 0: return [4 /*yield*/, scriptRunner_1.BackgroundTask.create({ taskType: "multi-log" })];
|
|
273
|
+
case 1:
|
|
274
|
+
task = _a.sent();
|
|
275
|
+
return [4 /*yield*/, task.addLog("info", "first")];
|
|
276
|
+
case 2:
|
|
277
|
+
_a.sent();
|
|
278
|
+
return [4 /*yield*/, task.addLog("warn", "second")];
|
|
279
|
+
case 3:
|
|
280
|
+
_a.sent();
|
|
281
|
+
return [4 /*yield*/, task.addLog("error", "third")];
|
|
282
|
+
case 4:
|
|
283
|
+
_a.sent();
|
|
284
|
+
return [4 /*yield*/, scriptRunner_1.BackgroundTask.findById(task._id)];
|
|
285
|
+
case 5:
|
|
286
|
+
reloaded = _a.sent();
|
|
287
|
+
(0, bun_test_1.expect)(reloaded === null || reloaded === void 0 ? void 0 : reloaded.logs).toHaveLength(3);
|
|
288
|
+
(0, bun_test_1.expect)(reloaded === null || reloaded === void 0 ? void 0 : reloaded.logs[0].level).toBe("info");
|
|
289
|
+
(0, bun_test_1.expect)(reloaded === null || reloaded === void 0 ? void 0 : reloaded.logs[1].level).toBe("warn");
|
|
290
|
+
(0, bun_test_1.expect)(reloaded === null || reloaded === void 0 ? void 0 : reloaded.logs[2].level).toBe("error");
|
|
291
|
+
return [2 /*return*/];
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}); });
|
|
295
|
+
});
|
|
296
|
+
(0, bun_test_1.describe)("updateProgress", function () {
|
|
297
|
+
(0, bun_test_1.it)("sets progress percentage and stage", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
298
|
+
var task, reloaded;
|
|
299
|
+
var _a, _b, _c;
|
|
300
|
+
return __generator(this, function (_d) {
|
|
301
|
+
switch (_d.label) {
|
|
302
|
+
case 0: return [4 /*yield*/, scriptRunner_1.BackgroundTask.create({ taskType: "progress-test" })];
|
|
303
|
+
case 1:
|
|
304
|
+
task = _d.sent();
|
|
305
|
+
return [4 /*yield*/, task.updateProgress(50, "processing", "halfway done")];
|
|
306
|
+
case 2:
|
|
307
|
+
_d.sent();
|
|
308
|
+
return [4 /*yield*/, scriptRunner_1.BackgroundTask.findById(task._id)];
|
|
309
|
+
case 3:
|
|
310
|
+
reloaded = _d.sent();
|
|
311
|
+
(0, bun_test_1.expect)((_a = reloaded === null || reloaded === void 0 ? void 0 : reloaded.progress) === null || _a === void 0 ? void 0 : _a.percentage).toBe(50);
|
|
312
|
+
(0, bun_test_1.expect)((_b = reloaded === null || reloaded === void 0 ? void 0 : reloaded.progress) === null || _b === void 0 ? void 0 : _b.stage).toBe("processing");
|
|
313
|
+
(0, bun_test_1.expect)((_c = reloaded === null || reloaded === void 0 ? void 0 : reloaded.progress) === null || _c === void 0 ? void 0 : _c.message).toBe("halfway done");
|
|
314
|
+
return [2 /*return*/];
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
}); });
|
|
318
|
+
(0, bun_test_1.it)("preserves previous stage and message when not provided", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
319
|
+
var task, reloaded;
|
|
320
|
+
var _a, _b, _c;
|
|
321
|
+
return __generator(this, function (_d) {
|
|
322
|
+
switch (_d.label) {
|
|
323
|
+
case 0: return [4 /*yield*/, scriptRunner_1.BackgroundTask.create({ taskType: "progress-retain" })];
|
|
324
|
+
case 1:
|
|
325
|
+
task = _d.sent();
|
|
326
|
+
return [4 /*yield*/, task.updateProgress(25, "init", "starting")];
|
|
327
|
+
case 2:
|
|
328
|
+
_d.sent();
|
|
329
|
+
return [4 /*yield*/, task.updateProgress(75)];
|
|
330
|
+
case 3:
|
|
331
|
+
_d.sent();
|
|
332
|
+
return [4 /*yield*/, scriptRunner_1.BackgroundTask.findById(task._id)];
|
|
333
|
+
case 4:
|
|
334
|
+
reloaded = _d.sent();
|
|
335
|
+
(0, bun_test_1.expect)((_a = reloaded === null || reloaded === void 0 ? void 0 : reloaded.progress) === null || _a === void 0 ? void 0 : _a.percentage).toBe(75);
|
|
336
|
+
(0, bun_test_1.expect)((_b = reloaded === null || reloaded === void 0 ? void 0 : reloaded.progress) === null || _b === void 0 ? void 0 : _b.stage).toBe("init");
|
|
337
|
+
(0, bun_test_1.expect)((_c = reloaded === null || reloaded === void 0 ? void 0 : reloaded.progress) === null || _c === void 0 ? void 0 : _c.message).toBe("starting");
|
|
338
|
+
return [2 /*return*/];
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
}); });
|
|
342
|
+
});
|
|
343
|
+
(0, bun_test_1.describe)("checkCancellation", function () {
|
|
344
|
+
(0, bun_test_1.it)("does nothing when task is not cancelled", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
345
|
+
var task;
|
|
346
|
+
return __generator(this, function (_a) {
|
|
347
|
+
switch (_a.label) {
|
|
348
|
+
case 0: return [4 /*yield*/, scriptRunner_1.BackgroundTask.create({ status: "running", taskType: "check-cancel" })];
|
|
349
|
+
case 1:
|
|
350
|
+
task = _a.sent();
|
|
351
|
+
return [4 /*yield*/, (0, bun_test_1.expect)(scriptRunner_1.BackgroundTask.checkCancellation(task._id.toString())).resolves.toBeUndefined()];
|
|
352
|
+
case 2:
|
|
353
|
+
_a.sent();
|
|
354
|
+
return [2 /*return*/];
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
}); });
|
|
358
|
+
(0, bun_test_1.it)("throws TaskCancelledError when task is cancelled", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
359
|
+
var task;
|
|
360
|
+
return __generator(this, function (_a) {
|
|
361
|
+
switch (_a.label) {
|
|
362
|
+
case 0: return [4 /*yield*/, scriptRunner_1.BackgroundTask.create({ status: "cancelled", taskType: "check-cancel" })];
|
|
363
|
+
case 1:
|
|
364
|
+
task = _a.sent();
|
|
365
|
+
return [4 /*yield*/, (0, bun_test_1.expect)(scriptRunner_1.BackgroundTask.checkCancellation(task._id.toString())).rejects.toThrow(scriptRunner_1.TaskCancelledError)];
|
|
366
|
+
case 2:
|
|
367
|
+
_a.sent();
|
|
368
|
+
return [2 /*return*/];
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
}); });
|
|
372
|
+
(0, bun_test_1.it)("does nothing when task does not exist", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
373
|
+
var fakeId;
|
|
374
|
+
return __generator(this, function (_a) {
|
|
375
|
+
switch (_a.label) {
|
|
376
|
+
case 0:
|
|
377
|
+
fakeId = new mongoose_1.default.Types.ObjectId().toString();
|
|
378
|
+
return [4 /*yield*/, (0, bun_test_1.expect)(scriptRunner_1.BackgroundTask.checkCancellation(fakeId)).resolves.toBeUndefined()];
|
|
379
|
+
case 1:
|
|
380
|
+
_a.sent();
|
|
381
|
+
return [2 /*return*/];
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
}); });
|
|
385
|
+
});
|
|
386
|
+
});
|
package/package.json
CHANGED
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@asteasolutions/zod-to-openapi": "^8.5.0",
|
|
8
|
-
"@sentry/bun": "^10.
|
|
8
|
+
"@sentry/bun": "^10.39.0",
|
|
9
9
|
"@sentry/profiling-node": "^10.25.0",
|
|
10
|
-
"@terreno/test": "0.23.
|
|
10
|
+
"@terreno/test": "0.23.1",
|
|
11
11
|
"@types/qs": "^6.14.0",
|
|
12
12
|
"ajv": "8.18.0",
|
|
13
13
|
"ajv-formats": "^3.0.1",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"passport-jwt": "^4.0.0",
|
|
39
39
|
"passport-local": "^1.0.0",
|
|
40
40
|
"passport-local-mongoose": "^9.0.1",
|
|
41
|
-
"qs": "^6.
|
|
41
|
+
"qs": "^6.15.0",
|
|
42
42
|
"@thream/socketio-jwt": "^3.1.4",
|
|
43
43
|
"scmp": "^2.1.0",
|
|
44
44
|
"socket.io": "^4.8.1",
|
|
@@ -48,12 +48,12 @@
|
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@biomejs/biome": "^2.3.6",
|
|
50
50
|
"@types/bcrypt": "^6.0.0",
|
|
51
|
-
"@types/bun": "^1.
|
|
51
|
+
"@types/bun": "^1.3.8",
|
|
52
52
|
"@types/cors": "^2.8.17",
|
|
53
53
|
"@types/cron": "^2.4.3",
|
|
54
54
|
"@types/express": "^5.0.6",
|
|
55
55
|
"@types/jsonwebtoken": "^9.0.9",
|
|
56
|
-
"@types/lodash": "^4.17.
|
|
56
|
+
"@types/lodash": "^4.17.17",
|
|
57
57
|
"@types/mongodb": "^4.0.7",
|
|
58
58
|
"@types/node": "^25.3.0",
|
|
59
59
|
"@types/on-finished": "^2.3.4",
|
|
@@ -63,14 +63,14 @@
|
|
|
63
63
|
"@types/passport-jwt": "^4.0.1",
|
|
64
64
|
"@types/passport-local": "^1.0.38",
|
|
65
65
|
"@types/sinon": "^21.0.0",
|
|
66
|
-
"@types/supertest": "^
|
|
66
|
+
"@types/supertest": "^7.2.0",
|
|
67
67
|
"mongodb-memory-server": "^11.0.1",
|
|
68
68
|
"mongoose": "8.23.0",
|
|
69
69
|
"sinon": "^21.0.1",
|
|
70
|
-
"supertest": "^7.
|
|
71
|
-
"typedoc": "
|
|
72
|
-
"typescript": "5.9.
|
|
73
|
-
"zod": "^4.3
|
|
70
|
+
"supertest": "^7.2.2",
|
|
71
|
+
"typedoc": "^0.28.17",
|
|
72
|
+
"typescript": "~5.9.2",
|
|
73
|
+
"zod": "^4.4.3"
|
|
74
74
|
},
|
|
75
75
|
"homepage": "https://github.com/FlourishHealth/terreno#readme",
|
|
76
76
|
"keywords": [
|
|
@@ -123,5 +123,5 @@
|
|
|
123
123
|
"updateSnapshot": "bun test --update-snapshots"
|
|
124
124
|
},
|
|
125
125
|
"types": "dist/index.d.ts",
|
|
126
|
-
"version": "0.23.
|
|
126
|
+
"version": "0.23.1"
|
|
127
127
|
}
|
package/src/api.test.ts
CHANGED
|
@@ -2078,10 +2078,14 @@ describe("@terreno/api", () => {
|
|
|
2078
2078
|
{_id: spinach._id as unknown as mongoose.Types.ObjectId},
|
|
2079
2079
|
{$set: {updated: "2025-06-15T12:00:00.000Z"}}
|
|
2080
2080
|
);
|
|
2081
|
+
const ifUnmodifiedSince = DateTime.fromISO("2025-06-15T11:00:00.000Z").toHTTP();
|
|
2082
|
+
if (!ifUnmodifiedSince) {
|
|
2083
|
+
throw new Error("Expected valid HTTP date");
|
|
2084
|
+
}
|
|
2081
2085
|
|
|
2082
2086
|
const res = await agent
|
|
2083
2087
|
.patch(`/food/${spinach._id}`)
|
|
2084
|
-
.set("If-Unmodified-Since",
|
|
2088
|
+
.set("If-Unmodified-Since", ifUnmodifiedSince)
|
|
2085
2089
|
.send({name: "String Timestamp"})
|
|
2086
2090
|
.expect(409);
|
|
2087
2091
|
|
package/src/auth.test.ts
CHANGED
|
@@ -1175,3 +1175,41 @@ describe("login error and disabled user paths", () => {
|
|
|
1175
1175
|
expect(res.body.title).toContain("disabled");
|
|
1176
1176
|
});
|
|
1177
1177
|
});
|
|
1178
|
+
|
|
1179
|
+
describe("PATCH /me route edge cases", () => {
|
|
1180
|
+
let app: express.Application;
|
|
1181
|
+
let agent: TestAgent;
|
|
1182
|
+
|
|
1183
|
+
beforeEach(async () => {
|
|
1184
|
+
setSystemTime();
|
|
1185
|
+
await setupTestData();
|
|
1186
|
+
app = new TerrenoApp({
|
|
1187
|
+
configureApp: () => {},
|
|
1188
|
+
skipListen: true,
|
|
1189
|
+
userModel: UserModel as any,
|
|
1190
|
+
}).build();
|
|
1191
|
+
agent = supertest.agent(app);
|
|
1192
|
+
});
|
|
1193
|
+
|
|
1194
|
+
afterEach(() => {
|
|
1195
|
+
setSystemTime();
|
|
1196
|
+
});
|
|
1197
|
+
|
|
1198
|
+
it("returns 404 for PATCH /me when authenticated user is deleted from DB", async () => {
|
|
1199
|
+
const loginRes = await agent
|
|
1200
|
+
.post("/auth/login")
|
|
1201
|
+
.send({email: "notAdmin@example.com", password: "password"})
|
|
1202
|
+
.expect(200);
|
|
1203
|
+
const {token, userId} = loginRes.body.data;
|
|
1204
|
+
|
|
1205
|
+
await UserModel.deleteOne({_id: userId});
|
|
1206
|
+
|
|
1207
|
+
const freshAgent = supertest.agent(app);
|
|
1208
|
+
const res = await freshAgent
|
|
1209
|
+
.patch("/auth/me")
|
|
1210
|
+
.set("authorization", `Bearer ${token}`)
|
|
1211
|
+
.send({name: "Updated"});
|
|
1212
|
+
// Without user in DB, should get 401 or 404
|
|
1213
|
+
expect([401, 404]).toContain(res.status);
|
|
1214
|
+
});
|
|
1215
|
+
});
|
package/src/consentApp.ts
CHANGED
|
@@ -17,8 +17,8 @@ import {logger} from "./logger";
|
|
|
17
17
|
import {ConsentForm} from "./models/consentForm";
|
|
18
18
|
import {ConsentResponse} from "./models/consentResponse";
|
|
19
19
|
import {Permissions} from "./permissions";
|
|
20
|
-
import type {TerrenoPlugin} from "./terrenoPlugin";
|
|
21
20
|
import type {PopulatePath} from "./populate";
|
|
21
|
+
import type {TerrenoPlugin} from "./terrenoPlugin";
|
|
22
22
|
import type {ConsentFormDocument} from "./types/consentForm";
|
|
23
23
|
|
|
24
24
|
/** Shared populate config for consent response list/read endpoints. */
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import {afterEach, beforeEach, describe, expect, it} from "bun:test";
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it, spyOn} from "bun:test";
|
|
3
2
|
import mongoose, {Schema} from "mongoose";
|
|
4
3
|
|
|
5
4
|
import {Config} from "./config";
|
|
@@ -163,16 +162,15 @@ describe("envConfigurationPlugin", () => {
|
|
|
163
162
|
|
|
164
163
|
// Override Model.find to simulate a DB error inside refreshFromDoc
|
|
165
164
|
// (findOneOrNoneFor falls back to model.find when the findOneOrNone static is absent)
|
|
166
|
-
const
|
|
167
|
-
(TestEnvConfig as any).find = () => {
|
|
165
|
+
const findSpy = spyOn(TestEnvConfig, "find").mockImplementation(() => {
|
|
168
166
|
throw new Error("Simulated DB error");
|
|
169
|
-
};
|
|
167
|
+
});
|
|
170
168
|
|
|
171
169
|
// Trigger the post-findOneAndUpdate hook → refreshFromDoc → findOneOrNoneFor → throws
|
|
172
170
|
await TestEnvConfig.findOneAndUpdate({_id: doc._id}, {$set: {__v: 2}});
|
|
173
171
|
|
|
174
172
|
// Restore immediately
|
|
175
|
-
(
|
|
173
|
+
findSpy.mockRestore();
|
|
176
174
|
|
|
177
175
|
// The catch block should have swallowed the error; cache keeps old value
|
|
178
176
|
expect(Config.get("TERRENO_PLUGIN_KEY")).toBe("cached");
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
2
1
|
import {afterAll, afterEach, beforeEach, describe, expect, it, type Mock, spyOn} from "bun:test";
|
|
3
2
|
import * as Sentry from "@sentry/bun";
|
|
3
|
+
import type {AxiosResponse} from "axios";
|
|
4
4
|
import axios from "axios";
|
|
5
5
|
|
|
6
|
+
import type {APIError} from "../errors";
|
|
6
7
|
import {sendToZoom} from "./zoomNotifier";
|
|
7
8
|
|
|
8
9
|
describe("sendToZoom", () => {
|
|
@@ -11,7 +12,9 @@ describe("sendToZoom", () => {
|
|
|
11
12
|
const ORIGINAL_ENV = process.env;
|
|
12
13
|
|
|
13
14
|
beforeEach(() => {
|
|
14
|
-
mockAxiosPost = spyOn(axios, "post").mockResolvedValue({
|
|
15
|
+
mockAxiosPost = spyOn(axios, "post").mockResolvedValue({
|
|
16
|
+
status: 200,
|
|
17
|
+
} as unknown as AxiosResponse);
|
|
15
18
|
process.env = {...ORIGINAL_ENV};
|
|
16
19
|
process.env.ZOOM_CHAT_WEBHOOKS = undefined;
|
|
17
20
|
(Sentry.captureException as Mock<typeof Sentry.captureException>).mockClear();
|
|
@@ -187,8 +190,8 @@ describe("sendToZoom", () => {
|
|
|
187
190
|
await sendToZoom({body: "err", header: "err"}, {channel: "default", shouldThrow: true});
|
|
188
191
|
throw new Error("Expected sendToZoom to throw APIError");
|
|
189
192
|
} catch (error) {
|
|
190
|
-
expect((error as
|
|
191
|
-
expect((error as
|
|
193
|
+
expect((error as APIError).name).toBe("APIError");
|
|
194
|
+
expect((error as APIError).title).toMatch(/Error posting to Zoom/i);
|
|
192
195
|
}
|
|
193
196
|
expect(mockAxiosPost.mock.calls.length).toBe(1);
|
|
194
197
|
});
|
package/src/scriptRunner.test.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
import {describe, expect, it} from "bun:test";
|
|
1
|
+
import {afterAll, beforeAll, describe, expect, it} from "bun:test";
|
|
2
|
+
import mongoose from "mongoose";
|
|
2
3
|
|
|
3
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
BackgroundTask,
|
|
6
|
+
createScriptArgs,
|
|
7
|
+
parseScriptArgs,
|
|
8
|
+
type ScriptArgDef,
|
|
9
|
+
TaskCancelledError,
|
|
10
|
+
} from "./scriptRunner";
|
|
4
11
|
|
|
5
12
|
describe("parseScriptArgs", () => {
|
|
6
13
|
it("parses --name=value form", () => {
|
|
@@ -173,3 +180,105 @@ describe("createScriptArgs", () => {
|
|
|
173
180
|
});
|
|
174
181
|
});
|
|
175
182
|
});
|
|
183
|
+
|
|
184
|
+
describe("TaskCancelledError", () => {
|
|
185
|
+
it("sets the message and name", () => {
|
|
186
|
+
const err = new TaskCancelledError("task-123");
|
|
187
|
+
expect(err.message).toBe("Task task-123 was cancelled");
|
|
188
|
+
expect(err.name).toBe("TaskCancelledError");
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("is an instance of Error", () => {
|
|
192
|
+
const err = new TaskCancelledError("abc");
|
|
193
|
+
expect(err).toBeInstanceOf(Error);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
describe("BackgroundTask model", () => {
|
|
198
|
+
beforeAll(async () => {
|
|
199
|
+
if (mongoose.connection.readyState !== 1) {
|
|
200
|
+
await mongoose.connect(process.env.MONGO_URL ?? "mongodb://127.0.0.1/terreno-test");
|
|
201
|
+
}
|
|
202
|
+
await BackgroundTask.deleteMany({});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
afterAll(async () => {
|
|
206
|
+
await BackgroundTask.deleteMany({});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("creates a task with required fields", async () => {
|
|
210
|
+
const task = await BackgroundTask.create({taskType: "test-task"});
|
|
211
|
+
expect(task.taskType).toBe("test-task");
|
|
212
|
+
expect(task.status).toBe("pending");
|
|
213
|
+
expect(task.isDryRun).toBe(false);
|
|
214
|
+
expect(task.logs).toEqual([]);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
describe("addLog", () => {
|
|
218
|
+
it("appends a log entry and saves", async () => {
|
|
219
|
+
const task = await BackgroundTask.create({taskType: "log-test"});
|
|
220
|
+
await task.addLog("info", "step completed");
|
|
221
|
+
|
|
222
|
+
const reloaded = await BackgroundTask.findById(task._id);
|
|
223
|
+
expect(reloaded?.logs).toHaveLength(1);
|
|
224
|
+
expect(reloaded?.logs[0].level).toBe("info");
|
|
225
|
+
expect(reloaded?.logs[0].message).toBe("step completed");
|
|
226
|
+
expect(reloaded?.logs[0].timestamp).toBeDefined();
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("appends multiple log entries", async () => {
|
|
230
|
+
const task = await BackgroundTask.create({taskType: "multi-log"});
|
|
231
|
+
await task.addLog("info", "first");
|
|
232
|
+
await task.addLog("warn", "second");
|
|
233
|
+
await task.addLog("error", "third");
|
|
234
|
+
|
|
235
|
+
const reloaded = await BackgroundTask.findById(task._id);
|
|
236
|
+
expect(reloaded?.logs).toHaveLength(3);
|
|
237
|
+
expect(reloaded?.logs[0].level).toBe("info");
|
|
238
|
+
expect(reloaded?.logs[1].level).toBe("warn");
|
|
239
|
+
expect(reloaded?.logs[2].level).toBe("error");
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
describe("updateProgress", () => {
|
|
244
|
+
it("sets progress percentage and stage", async () => {
|
|
245
|
+
const task = await BackgroundTask.create({taskType: "progress-test"});
|
|
246
|
+
await task.updateProgress(50, "processing", "halfway done");
|
|
247
|
+
|
|
248
|
+
const reloaded = await BackgroundTask.findById(task._id);
|
|
249
|
+
expect(reloaded?.progress?.percentage).toBe(50);
|
|
250
|
+
expect(reloaded?.progress?.stage).toBe("processing");
|
|
251
|
+
expect(reloaded?.progress?.message).toBe("halfway done");
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it("preserves previous stage and message when not provided", async () => {
|
|
255
|
+
const task = await BackgroundTask.create({taskType: "progress-retain"});
|
|
256
|
+
await task.updateProgress(25, "init", "starting");
|
|
257
|
+
await task.updateProgress(75);
|
|
258
|
+
|
|
259
|
+
const reloaded = await BackgroundTask.findById(task._id);
|
|
260
|
+
expect(reloaded?.progress?.percentage).toBe(75);
|
|
261
|
+
expect(reloaded?.progress?.stage).toBe("init");
|
|
262
|
+
expect(reloaded?.progress?.message).toBe("starting");
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
describe("checkCancellation", () => {
|
|
267
|
+
it("does nothing when task is not cancelled", async () => {
|
|
268
|
+
const task = await BackgroundTask.create({status: "running", taskType: "check-cancel"});
|
|
269
|
+
await expect(BackgroundTask.checkCancellation(task._id.toString())).resolves.toBeUndefined();
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("throws TaskCancelledError when task is cancelled", async () => {
|
|
273
|
+
const task = await BackgroundTask.create({status: "cancelled", taskType: "check-cancel"});
|
|
274
|
+
await expect(BackgroundTask.checkCancellation(task._id.toString())).rejects.toThrow(
|
|
275
|
+
TaskCancelledError
|
|
276
|
+
);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("does nothing when task does not exist", async () => {
|
|
280
|
+
const fakeId = new mongoose.Types.ObjectId().toString();
|
|
281
|
+
await expect(BackgroundTask.checkCancellation(fakeId)).resolves.toBeUndefined();
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
});
|