@jaypie/testkit 1.1.25 → 1.1.27
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/index.d.ts +241 -5
- package/dist/index.js +36 -15
- package/dist/index.js.map +1 -1
- package/dist/matchers/toBeMockFunction.matcher.d.ts +10 -0
- package/dist/matchers.module.d.ts +2 -0
- package/dist/mock/aws.d.ts +22 -0
- package/dist/mock/constants.d.ts +19 -0
- package/dist/mock/core.d.ts +306 -0
- package/dist/mock/datadog.d.ts +4 -0
- package/dist/mock/express.d.ts +32 -0
- package/dist/mock/index.d.ts +470 -0
- package/dist/mock/index.js +825 -0
- package/dist/mock/index.js.map +1 -0
- package/dist/mock/jsonApiSchema.module.d.ts +61 -0
- package/dist/mock/lambda.d.ts +15 -0
- package/dist/mock/llm.d.ts +66 -0
- package/dist/mock/matchers/toBeCalledAboveTrace.matcher.d.ts +3 -0
- package/dist/mock/matchers/toBeCalledWithInitialParams.matcher.d.ts +4 -0
- package/dist/mock/matchers/toBeClass.matcher.d.ts +3 -0
- package/dist/mock/matchers/toBeJaypieError.matcher.d.ts +3 -0
- package/dist/mock/matchers/toBeMockFunction.matcher.d.ts +10 -0
- package/dist/mock/matchers/toMatch.matcher.d.ts +12 -0
- package/dist/mock/matchers/toThrowError.matcher.d.ts +4 -0
- package/dist/mock/matchers/toThrowJaypieError.matcher.d.ts +16 -0
- package/dist/mock/matchers.module.d.ts +97 -0
- package/dist/mock/mock/aws.d.ts +22 -0
- package/dist/mock/mock/core.d.ts +306 -0
- package/dist/mock/mock/datadog.d.ts +4 -0
- package/dist/mock/mock/express.d.ts +32 -0
- package/dist/mock/mock/index.d.ts +10 -0
- package/dist/mock/mock/lambda.d.ts +15 -0
- package/dist/mock/mock/llm.d.ts +66 -0
- package/dist/mock/mock/mongoose.d.ts +4 -0
- package/dist/mock/mock/original.d.ts +19 -0
- package/dist/mock/mock/textract.d.ts +14 -0
- package/dist/mock/mock/utils.d.ts +42 -0
- package/dist/mock/mockLog.module.d.ts +5 -0
- package/dist/mock/mongoose.d.ts +4 -0
- package/dist/mock/original.d.ts +19 -0
- package/dist/mock/placeholders.d.ts +9 -0
- package/dist/mock/sqsTestRecords.function.d.ts +14 -0
- package/dist/mock/textract.d.ts +14 -0
- package/dist/mock/utils.d.ts +42 -0
- package/dist/mockLog.module.d.ts +3 -3
- package/dist/mockTextract.json +87 -752
- package/package.json +8 -7
- package/dist/jaypie.mock.d.ts +0 -69
- package/dist/jaypie.mock.js +0 -633
- package/dist/jaypie.mock.js.map +0 -1
package/dist/jaypie.mock.js
DELETED
|
@@ -1,633 +0,0 @@
|
|
|
1
|
-
import { getMessages as getMessages$1 } from '@jaypie/aws';
|
|
2
|
-
import { JAYPIE, BadGatewayError as BadGatewayError$1, BadRequestError as BadRequestError$1, ConfigurationError as ConfigurationError$1, ForbiddenError as ForbiddenError$1, GatewayTimeoutError as GatewayTimeoutError$1, GoneError as GoneError$1, IllogicalError as IllogicalError$1, InternalError as InternalError$1, MethodNotAllowedError as MethodNotAllowedError$1, MultiError as MultiError$1, NotFoundError as NotFoundError$1, NotImplementedError as NotImplementedError$1, ProjectError as ProjectError$1, ProjectMultiError as ProjectMultiError$1, RejectedError as RejectedError$1, TeapotError as TeapotError$1, UnauthorizedError as UnauthorizedError$1, UnavailableError as UnavailableError$1, UnhandledError as UnhandledError$1, UnreachableCodeError as UnreachableCodeError$1, force, uuid as uuid$1, HTTP, log } from '@jaypie/core';
|
|
3
|
-
import { mongoose } from '@jaypie/mongoose';
|
|
4
|
-
import { vi, beforeAll } from 'vitest';
|
|
5
|
-
import { readFile } from 'fs/promises';
|
|
6
|
-
import { TextractDocument } from 'amazon-textract-response-parser';
|
|
7
|
-
import { dirname, join } from 'path';
|
|
8
|
-
import { fileURLToPath } from 'url';
|
|
9
|
-
|
|
10
|
-
function mockLogFactory() {
|
|
11
|
-
// Create skeleton of mock objects
|
|
12
|
-
const mock = {
|
|
13
|
-
debug: vi.fn(),
|
|
14
|
-
error: vi.fn(),
|
|
15
|
-
fatal: vi.fn(),
|
|
16
|
-
info: vi.fn(),
|
|
17
|
-
init: vi.fn(),
|
|
18
|
-
lib: vi.fn(),
|
|
19
|
-
tag: vi.fn(),
|
|
20
|
-
trace: vi.fn(),
|
|
21
|
-
untag: vi.fn(),
|
|
22
|
-
var: vi.fn(),
|
|
23
|
-
warn: vi.fn(),
|
|
24
|
-
with: vi.fn(),
|
|
25
|
-
};
|
|
26
|
-
// Fill out nested mocks
|
|
27
|
-
mock.debug.var = mock.var;
|
|
28
|
-
mock.error.var = mock.var;
|
|
29
|
-
mock.fatal.var = mock.var;
|
|
30
|
-
mock.info.var = mock.var;
|
|
31
|
-
mock.trace.var = mock.var;
|
|
32
|
-
mock.warn.var = mock.var;
|
|
33
|
-
// Have modules return correct objects
|
|
34
|
-
mock.init.mockReturnValue(null);
|
|
35
|
-
mock.lib.mockReturnValue(mock);
|
|
36
|
-
mock.with.mockReturnValue(mock);
|
|
37
|
-
// Pin mocks to the module
|
|
38
|
-
mock.mock = {
|
|
39
|
-
debug: mock.debug,
|
|
40
|
-
error: mock.error,
|
|
41
|
-
fatal: mock.fatal,
|
|
42
|
-
info: mock.info,
|
|
43
|
-
init: mock.init,
|
|
44
|
-
lib: mock.lib,
|
|
45
|
-
tag: mock.tag,
|
|
46
|
-
trace: mock.trace,
|
|
47
|
-
untag: mock.untag,
|
|
48
|
-
var: mock.var,
|
|
49
|
-
warn: mock.warn,
|
|
50
|
-
with: mock.with,
|
|
51
|
-
};
|
|
52
|
-
return mock;
|
|
53
|
-
}
|
|
54
|
-
const LOG_METHOD_NAMES = [
|
|
55
|
-
"debug",
|
|
56
|
-
"error",
|
|
57
|
-
"fatal",
|
|
58
|
-
"info",
|
|
59
|
-
"init",
|
|
60
|
-
"lib",
|
|
61
|
-
"tag",
|
|
62
|
-
"trace",
|
|
63
|
-
"untag",
|
|
64
|
-
"var",
|
|
65
|
-
"warn",
|
|
66
|
-
"with",
|
|
67
|
-
];
|
|
68
|
-
const originalLogMethods = new WeakMap();
|
|
69
|
-
function spyLog(log) {
|
|
70
|
-
if (!originalLogMethods.has(log)) {
|
|
71
|
-
const mockLog = mockLogFactory();
|
|
72
|
-
const originalMethods = {};
|
|
73
|
-
LOG_METHOD_NAMES.forEach((method) => {
|
|
74
|
-
originalMethods[method] = log[method];
|
|
75
|
-
log[method] = mockLog[method];
|
|
76
|
-
});
|
|
77
|
-
originalLogMethods.set(log, originalMethods);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/* eslint-disable no-shadow */
|
|
82
|
-
//
|
|
83
|
-
//
|
|
84
|
-
// Helper Functions
|
|
85
|
-
//
|
|
86
|
-
/*!
|
|
87
|
-
* Get an object value from a specific path
|
|
88
|
-
* (c) 2018 Chris Ferdinandi, MIT License, https://gomakethings.com
|
|
89
|
-
* @param {Object} obj The object
|
|
90
|
-
* @param {String|Array} path The path
|
|
91
|
-
* @param {*} def A default value to return [optional]
|
|
92
|
-
* @return {*} The value
|
|
93
|
-
*/
|
|
94
|
-
function get(obj, path, def) {
|
|
95
|
-
/**
|
|
96
|
-
* If the path is a string, convert it to an array
|
|
97
|
-
* @param {String|Array} path The path
|
|
98
|
-
* @return {Array} The path array
|
|
99
|
-
*/
|
|
100
|
-
const stringToPath = (path) => {
|
|
101
|
-
// If the path isn't a string, return it
|
|
102
|
-
if (typeof path !== "string")
|
|
103
|
-
return path;
|
|
104
|
-
// Create new array
|
|
105
|
-
const output = [];
|
|
106
|
-
// Split to an array with dot notation
|
|
107
|
-
path.split(".").forEach((item) => {
|
|
108
|
-
// Split to an array with bracket notation
|
|
109
|
-
item.split(/\[([^}]+)\]/g).forEach((key) => {
|
|
110
|
-
// Push to the new array
|
|
111
|
-
if (key.length > 0) {
|
|
112
|
-
output.push(key);
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
return output;
|
|
117
|
-
};
|
|
118
|
-
// Get the path as an array
|
|
119
|
-
path = stringToPath(path);
|
|
120
|
-
// Cache the current object
|
|
121
|
-
let current = obj;
|
|
122
|
-
// For each item in the path, dig into the object
|
|
123
|
-
for (let i = 0; i < path.length; i++) {
|
|
124
|
-
// If the item isn't found, return the default (or null)
|
|
125
|
-
if (!current ||
|
|
126
|
-
typeof current !== "object" ||
|
|
127
|
-
!(path[i] in current))
|
|
128
|
-
return def;
|
|
129
|
-
// Otherwise, update the current value
|
|
130
|
-
current = current[path[i]];
|
|
131
|
-
}
|
|
132
|
-
return current;
|
|
133
|
-
}
|
|
134
|
-
//
|
|
135
|
-
//
|
|
136
|
-
// Main
|
|
137
|
-
//
|
|
138
|
-
/*!
|
|
139
|
-
* Replaces placeholders with real content
|
|
140
|
-
* Requires get() - https://vanillajstoolkit.com/helpers/get/
|
|
141
|
-
* (c) 2019 Chris Ferdinandi, MIT License, https://gomakethings.com
|
|
142
|
-
* @param {String} template The template string
|
|
143
|
-
* @param {String} local A local placeholder to use, if any
|
|
144
|
-
*/
|
|
145
|
-
function placeholders$1(template, data) {
|
|
146
|
-
// Check if the template is a string or a function
|
|
147
|
-
template = typeof template === "function" ? template() : template;
|
|
148
|
-
if (["string", "number"].indexOf(typeof template) === -1)
|
|
149
|
-
throw Error("PlaceholdersJS: please provide a valid template");
|
|
150
|
-
// If no data, return template as-is
|
|
151
|
-
if (!data)
|
|
152
|
-
return template;
|
|
153
|
-
// Replace our curly braces with data
|
|
154
|
-
template = template.replace(/\{\{([^}]+)\}\}/g, (match) => {
|
|
155
|
-
// Remove the wrapping curly braces
|
|
156
|
-
match = match.slice(2, -2);
|
|
157
|
-
// Get the value
|
|
158
|
-
const val = get(data, match.trim(), undefined);
|
|
159
|
-
// Replace
|
|
160
|
-
if (!val)
|
|
161
|
-
return `{{${match}}}`;
|
|
162
|
-
return String(val);
|
|
163
|
-
});
|
|
164
|
-
return template;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
//
|
|
168
|
-
//
|
|
169
|
-
// Setup
|
|
170
|
-
//
|
|
171
|
-
const TAG = JAYPIE.LIB.TESTKIT;
|
|
172
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
173
|
-
const __dirname = dirname(__filename);
|
|
174
|
-
const MOCK_TEXTRACT_DOCUMENT_PATH = join(__dirname, "mockTextract.json");
|
|
175
|
-
let textractJsonToMarkdownOriginal = vi.fn();
|
|
176
|
-
let MarkdownPageOriginal;
|
|
177
|
-
let mockTextractContents;
|
|
178
|
-
// Spy on log:
|
|
179
|
-
beforeAll(async () => {
|
|
180
|
-
const textract = await vi.importActual("@jaypie/textract");
|
|
181
|
-
textractJsonToMarkdownOriginal.mockImplementation(textract.textractJsonToMarkdown);
|
|
182
|
-
MarkdownPageOriginal = textract.MarkdownPage;
|
|
183
|
-
mockTextractContents = await readFile(MOCK_TEXTRACT_DOCUMENT_PATH, "utf-8");
|
|
184
|
-
spyLog(log);
|
|
185
|
-
});
|
|
186
|
-
//
|
|
187
|
-
//
|
|
188
|
-
// Mock Functions
|
|
189
|
-
//
|
|
190
|
-
// @jaypie/aws
|
|
191
|
-
const getEnvSecret = vi.fn(() => {
|
|
192
|
-
return `_MOCK_ENV_SECRET_[${TAG}]`;
|
|
193
|
-
});
|
|
194
|
-
const getMessages = vi.fn((...params) => getMessages$1(...params));
|
|
195
|
-
const getSecret = vi.fn(() => {
|
|
196
|
-
return `_MOCK_SECRET_[${TAG}]`;
|
|
197
|
-
});
|
|
198
|
-
const getTextractJob = vi.fn((job) => {
|
|
199
|
-
return { value: `_MOCK_TEXTRACT_JOB_[${job}]` };
|
|
200
|
-
});
|
|
201
|
-
const sendBatchMessages = vi.fn(() => {
|
|
202
|
-
return { value: `_MOCK_BATCH_MESSAGES_[${TAG}]` };
|
|
203
|
-
});
|
|
204
|
-
const sendMessage = vi.fn(() => {
|
|
205
|
-
return { value: `_MOCK_MESSAGE_[${TAG}]` };
|
|
206
|
-
});
|
|
207
|
-
const sendTextractJob = vi.fn(({ key, bucket }) => {
|
|
208
|
-
if (!key || !bucket) {
|
|
209
|
-
throw new ConfigurationError("[sendTextractJob] Missing key or bucket");
|
|
210
|
-
}
|
|
211
|
-
return [`_MOCK_TEXTRACT_JOB_[${bucket}/${key}]`];
|
|
212
|
-
});
|
|
213
|
-
// @jaypie/core Errors
|
|
214
|
-
const BadGatewayError = vi.fn((...params) => {
|
|
215
|
-
return new BadGatewayError$1(...params);
|
|
216
|
-
});
|
|
217
|
-
const BadRequestError = vi.fn((...params) => {
|
|
218
|
-
return new BadRequestError$1(...params);
|
|
219
|
-
});
|
|
220
|
-
const ConfigurationError = vi.fn((...params) => {
|
|
221
|
-
return new ConfigurationError$1(...params);
|
|
222
|
-
});
|
|
223
|
-
// Complete the error mocks
|
|
224
|
-
const ForbiddenError = vi.fn((...params) => {
|
|
225
|
-
return new ForbiddenError$1(...params);
|
|
226
|
-
});
|
|
227
|
-
const GatewayTimeoutError = vi.fn((...params) => {
|
|
228
|
-
return new GatewayTimeoutError$1(...params);
|
|
229
|
-
});
|
|
230
|
-
const GoneError = vi.fn((...params) => {
|
|
231
|
-
return new GoneError$1(...params);
|
|
232
|
-
});
|
|
233
|
-
const IllogicalError = vi.fn((...params) => {
|
|
234
|
-
return new IllogicalError$1(...params);
|
|
235
|
-
});
|
|
236
|
-
const InternalError = vi.fn((...params) => {
|
|
237
|
-
return new InternalError$1(...params);
|
|
238
|
-
});
|
|
239
|
-
const MethodNotAllowedError = vi.fn((...params) => {
|
|
240
|
-
return new MethodNotAllowedError$1(...params);
|
|
241
|
-
});
|
|
242
|
-
const MultiError = vi.fn((...params) => {
|
|
243
|
-
return new MultiError$1(...params);
|
|
244
|
-
});
|
|
245
|
-
const NotFoundError = vi.fn((...params) => {
|
|
246
|
-
return new NotFoundError$1(...params);
|
|
247
|
-
});
|
|
248
|
-
const NotImplementedError = vi.fn((...params) => {
|
|
249
|
-
return new NotImplementedError$1(...params);
|
|
250
|
-
});
|
|
251
|
-
const ProjectError = vi.fn((...params) => {
|
|
252
|
-
return new ProjectError$1(...params);
|
|
253
|
-
});
|
|
254
|
-
const ProjectMultiError = vi.fn((...params) => {
|
|
255
|
-
return new ProjectMultiError$1(...params);
|
|
256
|
-
});
|
|
257
|
-
const RejectedError = vi.fn((...params) => {
|
|
258
|
-
return new RejectedError$1(...params);
|
|
259
|
-
});
|
|
260
|
-
const TeapotError = vi.fn((...params) => {
|
|
261
|
-
return new TeapotError$1(...params);
|
|
262
|
-
});
|
|
263
|
-
const UnauthorizedError = vi.fn((...params) => {
|
|
264
|
-
return new UnauthorizedError$1(...params);
|
|
265
|
-
});
|
|
266
|
-
const UnavailableError = vi.fn((...params) => {
|
|
267
|
-
return new UnavailableError$1(...params);
|
|
268
|
-
});
|
|
269
|
-
const UnhandledError = vi.fn((...params) => {
|
|
270
|
-
return new UnhandledError$1(...params);
|
|
271
|
-
});
|
|
272
|
-
const UnreachableCodeError = vi.fn((...params) => {
|
|
273
|
-
return new UnreachableCodeError$1(...params);
|
|
274
|
-
});
|
|
275
|
-
// @jaypie/core Functions
|
|
276
|
-
const envBoolean = vi.fn(() => {
|
|
277
|
-
return true;
|
|
278
|
-
});
|
|
279
|
-
const placeholders = vi.fn((...params) => placeholders$1(...params));
|
|
280
|
-
const jaypieHandler = vi.fn((handler, { setup = [], teardown = [], unavailable = force.boolean(process.env.PROJECT_UNAVAILABLE), validate = [], } = {}) => {
|
|
281
|
-
return async (...args) => {
|
|
282
|
-
let result;
|
|
283
|
-
let thrownError;
|
|
284
|
-
if (unavailable)
|
|
285
|
-
throw UnavailableError();
|
|
286
|
-
validate = force.array(validate);
|
|
287
|
-
for (const validator of validate) {
|
|
288
|
-
if (typeof validator === "function") {
|
|
289
|
-
const valid = await validator(...args);
|
|
290
|
-
if (valid === false) {
|
|
291
|
-
throw new BadRequestError();
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
try {
|
|
296
|
-
setup = force.array(setup);
|
|
297
|
-
for (const setupFunction of setup) {
|
|
298
|
-
if (typeof setupFunction === "function") {
|
|
299
|
-
await setupFunction(...args);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
// @ts-expect-error TODO: cannot resolve; fix when JaypieHandler moves to TypeScript
|
|
303
|
-
result = handler(...args);
|
|
304
|
-
}
|
|
305
|
-
catch (error) {
|
|
306
|
-
thrownError = error;
|
|
307
|
-
}
|
|
308
|
-
teardown = force.array(teardown);
|
|
309
|
-
for (const teardownFunction of teardown) {
|
|
310
|
-
if (typeof teardownFunction === "function") {
|
|
311
|
-
try {
|
|
312
|
-
await teardownFunction(...args);
|
|
313
|
-
}
|
|
314
|
-
catch (error) {
|
|
315
|
-
// eslint-disable-next-line no-console
|
|
316
|
-
console.error(error);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
if (thrownError) {
|
|
321
|
-
throw thrownError;
|
|
322
|
-
}
|
|
323
|
-
return result;
|
|
324
|
-
};
|
|
325
|
-
});
|
|
326
|
-
const sleep = vi.fn(() => {
|
|
327
|
-
return true;
|
|
328
|
-
});
|
|
329
|
-
const uuid = vi.fn(uuid$1);
|
|
330
|
-
// @jaypie/datadog
|
|
331
|
-
const submitMetric = vi.fn(() => {
|
|
332
|
-
return true;
|
|
333
|
-
});
|
|
334
|
-
const submitMetricSet = vi.fn(() => {
|
|
335
|
-
return true;
|
|
336
|
-
});
|
|
337
|
-
// @jaypie/express
|
|
338
|
-
const expressHandler = vi.fn((handlerOrProps, propsOrHandler) => {
|
|
339
|
-
let handler;
|
|
340
|
-
let props;
|
|
341
|
-
if (typeof handlerOrProps === "object" &&
|
|
342
|
-
typeof propsOrHandler === "function") {
|
|
343
|
-
handler = propsOrHandler;
|
|
344
|
-
props = handlerOrProps;
|
|
345
|
-
}
|
|
346
|
-
else if (typeof handlerOrProps === "function") {
|
|
347
|
-
handler = handlerOrProps;
|
|
348
|
-
props = (propsOrHandler || {});
|
|
349
|
-
}
|
|
350
|
-
else {
|
|
351
|
-
throw BadRequestError("handler must be a function");
|
|
352
|
-
}
|
|
353
|
-
// Add locals setup if needed
|
|
354
|
-
if (props.locals &&
|
|
355
|
-
typeof props.locals === "object" &&
|
|
356
|
-
!Array.isArray(props.locals)) {
|
|
357
|
-
const keys = Object.keys(props.locals);
|
|
358
|
-
if (!props.setup)
|
|
359
|
-
props.setup = [];
|
|
360
|
-
props.setup = force.array(props.setup);
|
|
361
|
-
// @ts-expect-error TODO: cannot resolve; fix when JaypieHandler moves to TypeScript
|
|
362
|
-
props.setup.unshift((req) => {
|
|
363
|
-
if (!req || typeof req !== "object") {
|
|
364
|
-
throw new BadRequestError("req must be an object");
|
|
365
|
-
}
|
|
366
|
-
// Set req.locals if it doesn't exist
|
|
367
|
-
if (!req.locals)
|
|
368
|
-
req.locals = {};
|
|
369
|
-
if (typeof req.locals !== "object" || Array.isArray(req.locals)) {
|
|
370
|
-
throw new BadRequestError("req.locals must be an object");
|
|
371
|
-
}
|
|
372
|
-
if (!req.locals._jaypie)
|
|
373
|
-
req.locals._jaypie = {};
|
|
374
|
-
});
|
|
375
|
-
const localsSetup = async (localsReq, localsRes) => {
|
|
376
|
-
for (let i = 0; i < keys.length; i += 1) {
|
|
377
|
-
const key = keys[i];
|
|
378
|
-
if (typeof props.locals[key] === "function") {
|
|
379
|
-
localsReq.locals[key] = await props.locals[key](localsReq, localsRes);
|
|
380
|
-
}
|
|
381
|
-
else {
|
|
382
|
-
localsReq.locals[key] = props.locals[key];
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
};
|
|
386
|
-
// @ts-expect-error TODO: cannot resolve; fix when JaypieHandler moves to TypeScript
|
|
387
|
-
props.setup.push(localsSetup);
|
|
388
|
-
}
|
|
389
|
-
if (props.locals && typeof props.locals !== "object") {
|
|
390
|
-
throw new BadRequestError("props.locals must be an object");
|
|
391
|
-
}
|
|
392
|
-
if (props.locals && Array.isArray(props.locals)) {
|
|
393
|
-
throw new BadRequestError("props.locals must be an object");
|
|
394
|
-
}
|
|
395
|
-
if (props.locals === null) {
|
|
396
|
-
throw new BadRequestError("props.locals must be an object");
|
|
397
|
-
}
|
|
398
|
-
const jaypieFunction = jaypieHandler(handler, props);
|
|
399
|
-
return async (req = {}, res = {}, ...extra) => {
|
|
400
|
-
const status = HTTP.CODE.OK;
|
|
401
|
-
let response;
|
|
402
|
-
let supertestMode = false;
|
|
403
|
-
if (res &&
|
|
404
|
-
typeof res === "object" &&
|
|
405
|
-
"socket" in res &&
|
|
406
|
-
res.constructor.name === "ServerResponse") {
|
|
407
|
-
// Use the response object in supertest mode
|
|
408
|
-
supertestMode = true;
|
|
409
|
-
}
|
|
410
|
-
try {
|
|
411
|
-
response = await jaypieFunction(req, res, ...extra);
|
|
412
|
-
}
|
|
413
|
-
catch (error) {
|
|
414
|
-
// In the mock context, if status is a function we are in a "supertest"
|
|
415
|
-
if (supertestMode &&
|
|
416
|
-
typeof res.status === "function") {
|
|
417
|
-
// In theory jaypieFunction has handled all errors
|
|
418
|
-
const errorStatus = error.status ||
|
|
419
|
-
HTTP.CODE.INTERNAL_SERVER_ERROR;
|
|
420
|
-
let errorResponse;
|
|
421
|
-
if (typeof error.json === "function") {
|
|
422
|
-
errorResponse = error.json();
|
|
423
|
-
}
|
|
424
|
-
else {
|
|
425
|
-
// This should never happen
|
|
426
|
-
errorResponse = UnhandledError().json();
|
|
427
|
-
}
|
|
428
|
-
res.status(errorStatus).json(errorResponse);
|
|
429
|
-
return;
|
|
430
|
-
}
|
|
431
|
-
else {
|
|
432
|
-
// else, res.status is not a function, throw the error
|
|
433
|
-
throw error;
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
if (supertestMode &&
|
|
437
|
-
typeof res.status === "function") {
|
|
438
|
-
if (response) {
|
|
439
|
-
if (typeof response === "object") {
|
|
440
|
-
if (typeof response.json === "function") {
|
|
441
|
-
res.json(response.json());
|
|
442
|
-
}
|
|
443
|
-
else {
|
|
444
|
-
res.status(status).json(response);
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
else if (typeof response === "string") {
|
|
448
|
-
try {
|
|
449
|
-
res
|
|
450
|
-
.status(status)
|
|
451
|
-
.json(JSON.parse(response));
|
|
452
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
453
|
-
}
|
|
454
|
-
catch (error) {
|
|
455
|
-
res.status(status).send(response);
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
else if (response === true) {
|
|
459
|
-
res.status(HTTP.CODE.CREATED).send();
|
|
460
|
-
}
|
|
461
|
-
else {
|
|
462
|
-
res.status(status).send(response);
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
else {
|
|
466
|
-
res.status(HTTP.CODE.NO_CONTENT).send();
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
else {
|
|
470
|
-
return response;
|
|
471
|
-
}
|
|
472
|
-
};
|
|
473
|
-
});
|
|
474
|
-
// @jaypie/lambda
|
|
475
|
-
const lambdaHandler = vi.fn((handler, props = {}) => {
|
|
476
|
-
// If handler is an object and options is a function, swap them
|
|
477
|
-
if (typeof handler === "object" && typeof props === "function") {
|
|
478
|
-
const temp = handler;
|
|
479
|
-
handler = props;
|
|
480
|
-
props = temp;
|
|
481
|
-
}
|
|
482
|
-
return async (event, context, ...extra) => {
|
|
483
|
-
return jaypieHandler(handler, props)(event, context, ...extra);
|
|
484
|
-
};
|
|
485
|
-
});
|
|
486
|
-
// @jaypie/llm
|
|
487
|
-
const mockOperate = vi.fn().mockResolvedValue({
|
|
488
|
-
history: [
|
|
489
|
-
{
|
|
490
|
-
content: "_MOCK_USER_INPUT",
|
|
491
|
-
role: "user",
|
|
492
|
-
type: "message",
|
|
493
|
-
},
|
|
494
|
-
{
|
|
495
|
-
id: "_MOCK_MESSAGE_ID",
|
|
496
|
-
type: "message",
|
|
497
|
-
status: "completed",
|
|
498
|
-
content: "_MOCK_CONTENT",
|
|
499
|
-
role: "assistant",
|
|
500
|
-
},
|
|
501
|
-
],
|
|
502
|
-
output: [
|
|
503
|
-
{
|
|
504
|
-
id: "_MOCK_MESSAGE_ID",
|
|
505
|
-
type: "message",
|
|
506
|
-
status: "completed",
|
|
507
|
-
content: "_MOCK_CONTENT",
|
|
508
|
-
role: "assistant",
|
|
509
|
-
},
|
|
510
|
-
],
|
|
511
|
-
responses: [
|
|
512
|
-
{
|
|
513
|
-
id: "_MOCK_RESPONSE_ID",
|
|
514
|
-
object: "response",
|
|
515
|
-
created_at: Date.now() / 1000,
|
|
516
|
-
status: "completed",
|
|
517
|
-
error: null,
|
|
518
|
-
output_text: "_MOCK_OUTPUT_TEXT",
|
|
519
|
-
},
|
|
520
|
-
],
|
|
521
|
-
status: "completed",
|
|
522
|
-
usage: { input: 100, output: 20, reasoning: 0, total: 120 },
|
|
523
|
-
content: "_MOCK_OUTPUT_TEXT",
|
|
524
|
-
});
|
|
525
|
-
const mockSend = vi.fn().mockResolvedValue("_MOCK_LLM_RESPONSE");
|
|
526
|
-
const Llm = Object.assign(vi.fn().mockImplementation((providerName = "_MOCK_LLM_PROVIDER") => ({
|
|
527
|
-
_provider: providerName,
|
|
528
|
-
_llm: {
|
|
529
|
-
operate: mockOperate,
|
|
530
|
-
send: mockSend,
|
|
531
|
-
},
|
|
532
|
-
operate: mockOperate,
|
|
533
|
-
send: mockSend,
|
|
534
|
-
})), {
|
|
535
|
-
operate: mockOperate,
|
|
536
|
-
send: mockSend,
|
|
537
|
-
});
|
|
538
|
-
// @jaypie/mongoose
|
|
539
|
-
const connect = vi.fn(() => {
|
|
540
|
-
return true;
|
|
541
|
-
});
|
|
542
|
-
const connectFromSecretEnv = vi.fn(() => {
|
|
543
|
-
return true;
|
|
544
|
-
});
|
|
545
|
-
const disconnect = vi.fn(() => {
|
|
546
|
-
return true;
|
|
547
|
-
});
|
|
548
|
-
// @jaypie/textract
|
|
549
|
-
const MarkdownPage = vi
|
|
550
|
-
.fn()
|
|
551
|
-
.mockImplementation((page) => {
|
|
552
|
-
try {
|
|
553
|
-
return new MarkdownPageOriginal(page);
|
|
554
|
-
}
|
|
555
|
-
catch {
|
|
556
|
-
// eslint-disable-next-line no-console
|
|
557
|
-
console.warn("[MarkdownPage] Actual implementation failed. To suppress this warning, manually mock the response with mockReturnValue");
|
|
558
|
-
const mockDocument = new TextractDocument(JSON.parse(mockTextractContents));
|
|
559
|
-
// Double type assertion needed to bridge incompatible types
|
|
560
|
-
return new MarkdownPageOriginal(mockDocument._pages[0]);
|
|
561
|
-
}
|
|
562
|
-
});
|
|
563
|
-
const textractJsonToMarkdown = vi.fn((textractResults) => {
|
|
564
|
-
try {
|
|
565
|
-
const result = textractJsonToMarkdownOriginal(textractResults);
|
|
566
|
-
return result;
|
|
567
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
568
|
-
}
|
|
569
|
-
catch (error) {
|
|
570
|
-
// eslint-disable-next-line no-console
|
|
571
|
-
console.warn("[textractJsonToMarkdown] Actual implementation failed. To suppress this warning, manually mock the response with mockReturnValue");
|
|
572
|
-
return `_MOCK_TEXTRACT_JSON_TO_MARKDOWN_{{${textractResults}}}`;
|
|
573
|
-
}
|
|
574
|
-
});
|
|
575
|
-
// Export default for convenience
|
|
576
|
-
var jaypie_mock = {
|
|
577
|
-
// AWS
|
|
578
|
-
getEnvSecret,
|
|
579
|
-
getMessages,
|
|
580
|
-
getSecret,
|
|
581
|
-
getTextractJob,
|
|
582
|
-
sendBatchMessages,
|
|
583
|
-
sendMessage,
|
|
584
|
-
sendTextractJob,
|
|
585
|
-
// Core
|
|
586
|
-
BadGatewayError,
|
|
587
|
-
BadRequestError,
|
|
588
|
-
ConfigurationError,
|
|
589
|
-
envBoolean,
|
|
590
|
-
ForbiddenError,
|
|
591
|
-
GatewayTimeoutError,
|
|
592
|
-
GoneError,
|
|
593
|
-
HTTP,
|
|
594
|
-
IllogicalError,
|
|
595
|
-
InternalError,
|
|
596
|
-
jaypieHandler,
|
|
597
|
-
log,
|
|
598
|
-
MethodNotAllowedError,
|
|
599
|
-
MultiError,
|
|
600
|
-
NotFoundError,
|
|
601
|
-
NotImplementedError,
|
|
602
|
-
placeholders,
|
|
603
|
-
ProjectError,
|
|
604
|
-
ProjectMultiError,
|
|
605
|
-
RejectedError,
|
|
606
|
-
sleep,
|
|
607
|
-
TeapotError,
|
|
608
|
-
UnauthorizedError,
|
|
609
|
-
UnavailableError,
|
|
610
|
-
UnhandledError,
|
|
611
|
-
UnreachableCodeError,
|
|
612
|
-
uuid,
|
|
613
|
-
// Datadog
|
|
614
|
-
submitMetric,
|
|
615
|
-
submitMetricSet,
|
|
616
|
-
// Express
|
|
617
|
-
expressHandler,
|
|
618
|
-
// Lambda
|
|
619
|
-
lambdaHandler,
|
|
620
|
-
// LLM
|
|
621
|
-
Llm,
|
|
622
|
-
// Mongoose
|
|
623
|
-
connect,
|
|
624
|
-
connectFromSecretEnv,
|
|
625
|
-
disconnect,
|
|
626
|
-
mongoose,
|
|
627
|
-
// Textract
|
|
628
|
-
MarkdownPage,
|
|
629
|
-
textractJsonToMarkdown,
|
|
630
|
-
};
|
|
631
|
-
|
|
632
|
-
export { jaypie_mock as default };
|
|
633
|
-
//# sourceMappingURL=jaypie.mock.js.map
|