@terreno/test 56.0.0-beta.0 → 57.0.0-beta.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/LICENSE +21 -0
- package/dist/cache/mongoTestCache.js +3 -2
- package/dist/mongo/mongoServer.js +7 -6
- package/dist/utils/waitForDocuments.d.ts +5 -3
- package/dist/utils/waitForDocuments.js +3 -2
- package/package.json +6 -4
- package/src/cache/mongoTestCache.ts +3 -2
- package/src/mongo/mongoServer.ts +9 -6
- package/src/utils/waitForDocuments.ts +10 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Flourish Health, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -112,6 +112,7 @@ exports.createMongoTestCache = void 0;
|
|
|
112
112
|
var crypto = __importStar(require("node:crypto"));
|
|
113
113
|
var fs = __importStar(require("node:fs"));
|
|
114
114
|
var path = __importStar(require("node:path"));
|
|
115
|
+
var luxon_1 = require("luxon");
|
|
115
116
|
var mongoose_1 = __importDefault(require("mongoose"));
|
|
116
117
|
var testLogger_1 = require("../logging/testLogger");
|
|
117
118
|
var connection_1 = require("../mongo/connection");
|
|
@@ -196,7 +197,7 @@ var cacheFilesExist = function (cacheDir) {
|
|
|
196
197
|
return false;
|
|
197
198
|
}
|
|
198
199
|
if (!process.env.CI) {
|
|
199
|
-
var cacheAge =
|
|
200
|
+
var cacheAge = luxon_1.DateTime.now().toMillis() - fs.statSync(dataFilePath).mtimeMs;
|
|
200
201
|
if (cacheAge > MAX_CACHE_AGE_MS) {
|
|
201
202
|
return false;
|
|
202
203
|
}
|
|
@@ -210,7 +211,7 @@ var convertValue = function (value) {
|
|
|
210
211
|
}
|
|
211
212
|
if (typeof value === "string") {
|
|
212
213
|
if (value.match(/^\d{4}-\d{2}-\d{2}T/)) {
|
|
213
|
-
return
|
|
214
|
+
return luxon_1.DateTime.fromISO(value).toJSDate();
|
|
214
215
|
}
|
|
215
216
|
if (value.match(/^[0-9a-fA-F]{24}$/)) {
|
|
216
217
|
return new mongoose_1.default.Types.ObjectId(value);
|
|
@@ -120,6 +120,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
120
120
|
};
|
|
121
121
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
122
122
|
exports.getMongoServerUri = exports.stopMongoServer = exports.restartMongoServer = exports.startMongoServer = exports.initializeModels = exports.waitForDatabaseReady = void 0;
|
|
123
|
+
var luxon_1 = require("luxon");
|
|
123
124
|
var mongoose_1 = __importDefault(require("mongoose"));
|
|
124
125
|
var testLogger_1 = require("../logging/testLogger");
|
|
125
126
|
var connection_1 = require("./connection");
|
|
@@ -195,7 +196,7 @@ var initializeModels = function () { return __awaiter(void 0, void 0, void 0, fu
|
|
|
195
196
|
if (newModels.length === 0) {
|
|
196
197
|
return [2 /*return*/, { modelCount: models.length, modelInitMs: 0 }];
|
|
197
198
|
}
|
|
198
|
-
startTime =
|
|
199
|
+
startTime = luxon_1.DateTime.now().toMillis();
|
|
199
200
|
initPromises = newModels.map(function (modelName) { return __awaiter(void 0, void 0, void 0, function () {
|
|
200
201
|
return __generator(this, function (_a) {
|
|
201
202
|
switch (_a.label) {
|
|
@@ -222,7 +223,7 @@ var initializeModels = function () { return __awaiter(void 0, void 0, void 0, fu
|
|
|
222
223
|
}
|
|
223
224
|
finally { if (e_1) throw e_1.error; }
|
|
224
225
|
}
|
|
225
|
-
return [2 /*return*/, { modelCount: models.length, modelInitMs:
|
|
226
|
+
return [2 /*return*/, { modelCount: models.length, modelInitMs: luxon_1.DateTime.now().toMillis() - startTime }];
|
|
226
227
|
}
|
|
227
228
|
});
|
|
228
229
|
}); };
|
|
@@ -286,7 +287,7 @@ var startMongoServer = function () {
|
|
|
286
287
|
return [2 /*return*/, mongoServerUri];
|
|
287
288
|
}
|
|
288
289
|
merged = __assign(__assign({}, DEFAULT_OPTIONS), options);
|
|
289
|
-
startTime =
|
|
290
|
+
startTime = luxon_1.DateTime.now().toMillis();
|
|
290
291
|
externalUri = (_b = process.env[merged.externalUriEnvVar]) === null || _b === void 0 ? void 0 : _b.trim();
|
|
291
292
|
if (!externalUri) return [3 /*break*/, 1];
|
|
292
293
|
uri = externalUri;
|
|
@@ -318,7 +319,7 @@ var startMongoServer = function () {
|
|
|
318
319
|
case 6:
|
|
319
320
|
_a = _c.sent(), modelCount = _a.modelCount, modelInitMs = _a.modelInitMs;
|
|
320
321
|
if (process.env.DEBUG_MONGO_PRELOAD === "true") {
|
|
321
|
-
testLogger_1.testLogger.debug("[mongoServer] Initialized ".concat(modelCount, " models in ").concat(modelInitMs, "ms, connected in ").concat(
|
|
322
|
+
testLogger_1.testLogger.debug("[mongoServer] Initialized ".concat(modelCount, " models in ").concat(modelInitMs, "ms, connected in ").concat(luxon_1.DateTime.now().toMillis() - startTime, "ms"));
|
|
322
323
|
}
|
|
323
324
|
return [2 /*return*/, uri];
|
|
324
325
|
}
|
|
@@ -339,7 +340,7 @@ var restartMongoServer = function () {
|
|
|
339
340
|
switch (_c.label) {
|
|
340
341
|
case 0:
|
|
341
342
|
merged = __assign(__assign({}, DEFAULT_OPTIONS), options);
|
|
342
|
-
startTime =
|
|
343
|
+
startTime = luxon_1.DateTime.now().toMillis();
|
|
343
344
|
testLogger_1.testLogger.warn("[mongoServer] Force-restarting MongoDB server...");
|
|
344
345
|
_c.label = 1;
|
|
345
346
|
case 1:
|
|
@@ -394,7 +395,7 @@ var restartMongoServer = function () {
|
|
|
394
395
|
return [4 /*yield*/, (0, exports.initializeModels)()];
|
|
395
396
|
case 13:
|
|
396
397
|
_c.sent();
|
|
397
|
-
testLogger_1.testLogger.warn("[mongoServer] MongoDB server restarted in ".concat(
|
|
398
|
+
testLogger_1.testLogger.warn("[mongoServer] MongoDB server restarted in ".concat(luxon_1.DateTime.now().toMillis() - startTime, "ms"));
|
|
398
399
|
return [2 /*return*/];
|
|
399
400
|
}
|
|
400
401
|
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Model } from "mongoose";
|
|
2
|
+
type ModelQuery<T> = Partial<Record<keyof T, unknown>> & Record<string, unknown>;
|
|
2
3
|
export interface WaitForDocumentsOptions {
|
|
3
4
|
timeoutMs?: number;
|
|
4
5
|
intervalMs?: number;
|
|
@@ -8,6 +9,7 @@ export interface WaitForDocumentsOptions {
|
|
|
8
9
|
* Polls until at least `count` documents match the query.
|
|
9
10
|
* Useful when writes are asynchronous (audit logs, webhooks, etc.).
|
|
10
11
|
*/
|
|
11
|
-
export declare const waitForDocuments: <T>(model: Model<T>, query:
|
|
12
|
+
export declare const waitForDocuments: <T>(model: Model<T>, query: ModelQuery<T>, count?: number, { timeoutMs, intervalMs, sort }?: WaitForDocumentsOptions) => Promise<T[]>;
|
|
12
13
|
/** Polls until a single document matches the query. */
|
|
13
|
-
export declare const waitForDocument: <T>(model: Model<T>, query:
|
|
14
|
+
export declare const waitForDocument: <T>(model: Model<T>, query: ModelQuery<T>, options?: WaitForDocumentsOptions) => Promise<T>;
|
|
15
|
+
export {};
|
|
@@ -62,6 +62,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
62
62
|
};
|
|
63
63
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
64
64
|
exports.waitForDocument = exports.waitForDocuments = void 0;
|
|
65
|
+
var luxon_1 = require("luxon");
|
|
65
66
|
/**
|
|
66
67
|
* Polls until at least `count` documents match the query.
|
|
67
68
|
* Useful when writes are asynchronous (audit logs, webhooks, etc.).
|
|
@@ -78,10 +79,10 @@ var waitForDocuments = function (model_1, query_1) {
|
|
|
78
79
|
return __generator(this, function (_e) {
|
|
79
80
|
switch (_e.label) {
|
|
80
81
|
case 0:
|
|
81
|
-
start =
|
|
82
|
+
start = luxon_1.DateTime.now().toMillis();
|
|
82
83
|
_e.label = 1;
|
|
83
84
|
case 1:
|
|
84
|
-
if (!(
|
|
85
|
+
if (!(luxon_1.DateTime.now().toMillis() - start < timeoutMs)) return [3 /*break*/, 4];
|
|
85
86
|
q = model.find(query);
|
|
86
87
|
if (sort) {
|
|
87
88
|
q = q.sort(sort);
|
package/package.json
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"express": "^5.2.1",
|
|
8
8
|
"lodash": "^4.18.1",
|
|
9
|
+
"luxon": "^3.7.2",
|
|
9
10
|
"qs": "^6.15.0",
|
|
10
11
|
"winston": "^3.18.3"
|
|
11
12
|
},
|
|
@@ -14,11 +15,12 @@
|
|
|
14
15
|
"@biomejs/biome": "^2.3.6",
|
|
15
16
|
"@types/bun": "^1.3.8",
|
|
16
17
|
"@types/express": "^5.0.6",
|
|
18
|
+
"@types/luxon": "^3.7.1",
|
|
17
19
|
"@types/supertest": "^7.2.0",
|
|
18
20
|
"mongodb-memory-server": "^11.0.1",
|
|
19
21
|
"mongodb-memory-server-core": "^11.0.1",
|
|
20
22
|
"mongodb-memory-server-global": "^11.0.1",
|
|
21
|
-
"mongoose": "
|
|
23
|
+
"mongoose": "9.7.4",
|
|
22
24
|
"supertest": "^7.2.2",
|
|
23
25
|
"typescript": "~6.0.3"
|
|
24
26
|
},
|
|
@@ -30,7 +32,7 @@
|
|
|
30
32
|
"terreno",
|
|
31
33
|
"testing"
|
|
32
34
|
],
|
|
33
|
-
"license": "
|
|
35
|
+
"license": "MIT",
|
|
34
36
|
"main": "dist/index.js",
|
|
35
37
|
"exports": {
|
|
36
38
|
".": {
|
|
@@ -41,7 +43,7 @@
|
|
|
41
43
|
},
|
|
42
44
|
"name": "@terreno/test",
|
|
43
45
|
"peerDependencies": {
|
|
44
|
-
"mongoose": "
|
|
46
|
+
"mongoose": "^8.0.0 || ^9.0.0"
|
|
45
47
|
},
|
|
46
48
|
"publishConfig": {
|
|
47
49
|
"access": "public"
|
|
@@ -61,5 +63,5 @@
|
|
|
61
63
|
"test:ci": "bun test"
|
|
62
64
|
},
|
|
63
65
|
"types": "dist/index.d.ts",
|
|
64
|
-
"version": "
|
|
66
|
+
"version": "57.0.0-beta.1"
|
|
65
67
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as crypto from "node:crypto";
|
|
2
2
|
import * as fs from "node:fs";
|
|
3
3
|
import * as path from "node:path";
|
|
4
|
+
import {DateTime} from "luxon";
|
|
4
5
|
import mongoose from "mongoose";
|
|
5
6
|
|
|
6
7
|
import {testLogger} from "../logging/testLogger";
|
|
@@ -89,7 +90,7 @@ const cacheFilesExist = (cacheDir: string): boolean => {
|
|
|
89
90
|
}
|
|
90
91
|
|
|
91
92
|
if (!process.env.CI) {
|
|
92
|
-
const cacheAge =
|
|
93
|
+
const cacheAge = DateTime.now().toMillis() - fs.statSync(dataFilePath).mtimeMs;
|
|
93
94
|
if (cacheAge > MAX_CACHE_AGE_MS) {
|
|
94
95
|
return false;
|
|
95
96
|
}
|
|
@@ -104,7 +105,7 @@ const convertValue = (value: unknown): unknown => {
|
|
|
104
105
|
}
|
|
105
106
|
if (typeof value === "string") {
|
|
106
107
|
if (value.match(/^\d{4}-\d{2}-\d{2}T/)) {
|
|
107
|
-
return
|
|
108
|
+
return DateTime.fromISO(value).toJSDate();
|
|
108
109
|
}
|
|
109
110
|
if (value.match(/^[0-9a-fA-F]{24}$/)) {
|
|
110
111
|
return new mongoose.Types.ObjectId(value);
|
package/src/mongo/mongoServer.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {DateTime} from "luxon";
|
|
1
2
|
import mongoose from "mongoose";
|
|
2
3
|
|
|
3
4
|
import {testLogger} from "../logging/testLogger";
|
|
@@ -69,7 +70,7 @@ export const initializeModels = async (): Promise<{modelCount: number; modelInit
|
|
|
69
70
|
return {modelCount: models.length, modelInitMs: 0};
|
|
70
71
|
}
|
|
71
72
|
|
|
72
|
-
const startTime =
|
|
73
|
+
const startTime = DateTime.now().toMillis();
|
|
73
74
|
const initPromises = newModels.map(async (modelName) => {
|
|
74
75
|
await mongoose.models[modelName].init();
|
|
75
76
|
});
|
|
@@ -79,7 +80,7 @@ export const initializeModels = async (): Promise<{modelCount: number; modelInit
|
|
|
79
80
|
initializedModelNames.add(modelName);
|
|
80
81
|
}
|
|
81
82
|
|
|
82
|
-
return {modelCount: models.length, modelInitMs:
|
|
83
|
+
return {modelCount: models.length, modelInitMs: DateTime.now().toMillis() - startTime};
|
|
83
84
|
};
|
|
84
85
|
|
|
85
86
|
const shouldUseReplSet = (options: MongoServerOptions): boolean => {
|
|
@@ -119,7 +120,7 @@ export const startMongoServer = async (options: MongoServerOptions = {}): Promis
|
|
|
119
120
|
}
|
|
120
121
|
|
|
121
122
|
const merged = {...DEFAULT_OPTIONS, ...options};
|
|
122
|
-
const startTime =
|
|
123
|
+
const startTime = DateTime.now().toMillis();
|
|
123
124
|
const externalUri = process.env[merged.externalUriEnvVar]?.trim();
|
|
124
125
|
let uri: string;
|
|
125
126
|
|
|
@@ -149,7 +150,7 @@ export const startMongoServer = async (options: MongoServerOptions = {}): Promis
|
|
|
149
150
|
|
|
150
151
|
if (process.env.DEBUG_MONGO_PRELOAD === "true") {
|
|
151
152
|
testLogger.debug(
|
|
152
|
-
`[mongoServer] Initialized ${modelCount} models in ${modelInitMs}ms, connected in ${
|
|
153
|
+
`[mongoServer] Initialized ${modelCount} models in ${modelInitMs}ms, connected in ${DateTime.now().toMillis() - startTime}ms`
|
|
153
154
|
);
|
|
154
155
|
}
|
|
155
156
|
|
|
@@ -159,7 +160,7 @@ export const startMongoServer = async (options: MongoServerOptions = {}): Promis
|
|
|
159
160
|
/** Force-restarts the in-memory MongoDB server and reconnects mongoose. */
|
|
160
161
|
export const restartMongoServer = async (options: MongoServerOptions = {}): Promise<void> => {
|
|
161
162
|
const merged = {...DEFAULT_OPTIONS, ...options};
|
|
162
|
-
const startTime =
|
|
163
|
+
const startTime = DateTime.now().toMillis();
|
|
163
164
|
testLogger.warn("[mongoServer] Force-restarting MongoDB server...");
|
|
164
165
|
|
|
165
166
|
try {
|
|
@@ -199,7 +200,9 @@ export const restartMongoServer = async (options: MongoServerOptions = {}): Prom
|
|
|
199
200
|
initializedModelNames.clear();
|
|
200
201
|
await initializeModels();
|
|
201
202
|
|
|
202
|
-
testLogger.warn(
|
|
203
|
+
testLogger.warn(
|
|
204
|
+
`[mongoServer] MongoDB server restarted in ${DateTime.now().toMillis() - startTime}ms`
|
|
205
|
+
);
|
|
203
206
|
};
|
|
204
207
|
|
|
205
208
|
export const stopMongoServer = async (): Promise<void> => {
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {DateTime} from "luxon";
|
|
2
|
+
import type {Model} from "mongoose";
|
|
3
|
+
|
|
4
|
+
type ModelQuery<T> = Partial<Record<keyof T, unknown>> & Record<string, unknown>;
|
|
2
5
|
|
|
3
6
|
export interface WaitForDocumentsOptions {
|
|
4
7
|
timeoutMs?: number;
|
|
@@ -12,13 +15,13 @@ export interface WaitForDocumentsOptions {
|
|
|
12
15
|
*/
|
|
13
16
|
export const waitForDocuments = async <T>(
|
|
14
17
|
model: Model<T>,
|
|
15
|
-
query:
|
|
18
|
+
query: ModelQuery<T>,
|
|
16
19
|
count = 1,
|
|
17
20
|
{timeoutMs = 5000, intervalMs = 100, sort}: WaitForDocumentsOptions = {}
|
|
18
21
|
): Promise<T[]> => {
|
|
19
|
-
const start =
|
|
20
|
-
while (
|
|
21
|
-
let q = model.find(query);
|
|
22
|
+
const start = DateTime.now().toMillis();
|
|
23
|
+
while (DateTime.now().toMillis() - start < timeoutMs) {
|
|
24
|
+
let q = model.find(query as never);
|
|
22
25
|
if (sort) {
|
|
23
26
|
q = q.sort(sort);
|
|
24
27
|
}
|
|
@@ -29,7 +32,7 @@ export const waitForDocuments = async <T>(
|
|
|
29
32
|
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
30
33
|
}
|
|
31
34
|
|
|
32
|
-
const found = await model.countDocuments(query);
|
|
35
|
+
const found = await model.countDocuments(query as never);
|
|
33
36
|
throw new Error(
|
|
34
37
|
`Timed out after ${timeoutMs}ms waiting for ${count} document(s) on ${model.modelName} matching ${JSON.stringify(query)} (found ${found})`
|
|
35
38
|
);
|
|
@@ -38,7 +41,7 @@ export const waitForDocuments = async <T>(
|
|
|
38
41
|
/** Polls until a single document matches the query. */
|
|
39
42
|
export const waitForDocument = async <T>(
|
|
40
43
|
model: Model<T>,
|
|
41
|
-
query:
|
|
44
|
+
query: ModelQuery<T>,
|
|
42
45
|
options: WaitForDocumentsOptions = {}
|
|
43
46
|
): Promise<T> => {
|
|
44
47
|
const results = await waitForDocuments(model, query, 1, options);
|