@blokjs/shared 0.6.18 → 0.6.19
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/utils/Mapper.d.ts +14 -1
- package/dist/utils/Mapper.js +29 -2
- package/dist/utils/Mapper.js.map +1 -1
- package/package.json +2 -1
- package/CHANGELOG.md +0 -69
- package/__tests__/unit/BlokError.test.ts +0 -294
- package/__tests__/unit/GlobalError.test.ts +0 -93
- package/__tests__/unit/GlobalLogger.test.ts +0 -77
- package/__tests__/unit/Metrics.test.ts +0 -77
- package/__tests__/unit/NodeBase.test.ts +0 -290
- package/__tests__/unit/Trigger.test.ts +0 -23
- package/__tests__/unit/utils/CpuUsage.test.ts +0 -102
- package/__tests__/unit/utils/Mapper.test.ts +0 -393
- package/__tests__/unit/utils/MapperResolutionError.test.ts +0 -64
- package/__tests__/unit/utils/MemoryUsage.test.ts +0 -121
- package/__tests__/unit/utils/Time.test.ts +0 -60
- package/tsconfig.json +0 -19
- package/vitest.config.ts +0 -29
|
@@ -1,393 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import type Context from "../../../src/types/Context";
|
|
3
|
-
import type ParamsDictionary from "../../../src/types/ParamsDictionary";
|
|
4
|
-
import mapper from "../../../src/utils/Mapper";
|
|
5
|
-
import { MapperResolutionError } from "../../../src/utils/MapperResolutionError";
|
|
6
|
-
|
|
7
|
-
function createMockContext(overrides: Partial<Context> = {}): Context {
|
|
8
|
-
return {
|
|
9
|
-
id: "test-id",
|
|
10
|
-
workflow_name: "test-workflow",
|
|
11
|
-
request: { body: {}, headers: {}, query: {}, params: {} },
|
|
12
|
-
response: { data: null, error: null, success: true },
|
|
13
|
-
error: { message: "" },
|
|
14
|
-
logger: { log: vi.fn(), logLevel: vi.fn(), error: vi.fn() },
|
|
15
|
-
config: {},
|
|
16
|
-
func: {},
|
|
17
|
-
vars: {},
|
|
18
|
-
eventLogger: null,
|
|
19
|
-
_PRIVATE_: null,
|
|
20
|
-
...overrides,
|
|
21
|
-
} as Context;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Test helper — set the resolution mode for the duration of one test.
|
|
26
|
-
* Resets to the v0.3.x default (`"warn"`) after each case.
|
|
27
|
-
*/
|
|
28
|
-
function setMode(mode: "warn" | "strict" | "silent"): void {
|
|
29
|
-
process.env.BLOK_MAPPER_MODE = mode;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
describe("Mapper", () => {
|
|
33
|
-
beforeEach(() => {
|
|
34
|
-
vi.restoreAllMocks();
|
|
35
|
-
// Reset to the v0.3.x default ("warn"). Assignment to undefined
|
|
36
|
-
// keeps biome's `noDelete` rule happy without changing semantics —
|
|
37
|
-
// `process.env.X = undefined` makes `process.env.X` evaluate to
|
|
38
|
-
// the string `"undefined"` in Node, but Mapper's `readMode()`
|
|
39
|
-
// treats anything that isn't "strict" or "silent" as warn, so
|
|
40
|
-
// the default is preserved.
|
|
41
|
-
process.env.BLOK_MAPPER_MODE = undefined;
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
afterEach(() => {
|
|
45
|
-
process.env.BLOK_MAPPER_MODE = undefined;
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// =========================================================================
|
|
49
|
-
// Pre-existing behavior — replaceString happy paths (preserved across rewrite)
|
|
50
|
-
// =========================================================================
|
|
51
|
-
|
|
52
|
-
describe("replaceString() — happy paths", () => {
|
|
53
|
-
it("replaces ${key} with data value", () => {
|
|
54
|
-
const ctx = createMockContext();
|
|
55
|
-
const data = { name: "John" };
|
|
56
|
-
const result = mapper.replaceString("Hello ${name}", ctx, data);
|
|
57
|
-
expect(result).toBe("Hello John");
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it("replaces multiple placeholders", () => {
|
|
61
|
-
const ctx = createMockContext();
|
|
62
|
-
const data = { first: "John", last: "Doe" };
|
|
63
|
-
const result = mapper.replaceString("${first} ${last}", ctx, data);
|
|
64
|
-
expect(result).toBe("John Doe");
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("handles nested data access via lodash.get", () => {
|
|
68
|
-
const ctx = createMockContext();
|
|
69
|
-
const data = { user: { name: "Alice" } };
|
|
70
|
-
const result = mapper.replaceString("Hi ${user.name}", ctx, data as unknown as ParamsDictionary);
|
|
71
|
-
expect(result).toBe("Hi Alice");
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it("handles no matches (no ${})", () => {
|
|
75
|
-
const ctx = createMockContext();
|
|
76
|
-
const result = mapper.replaceString("plain text", ctx, {});
|
|
77
|
-
expect(result).toBe("plain text");
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it("executes js/ prefix expressions", () => {
|
|
81
|
-
const ctx = createMockContext();
|
|
82
|
-
const result = mapper.replaceString("js/1 + 2", ctx, {});
|
|
83
|
-
expect(result).toBe(3);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it("passes through non-js strings", () => {
|
|
87
|
-
const ctx = createMockContext();
|
|
88
|
-
const result = mapper.replaceString("hello world", ctx, {});
|
|
89
|
-
expect(result).toBe("hello world");
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// =========================================================================
|
|
94
|
-
// Bug fixes shipped with the rewrite (v0.3.x)
|
|
95
|
-
// =========================================================================
|
|
96
|
-
|
|
97
|
-
describe("replaceString() — bug fixes (v0.3.x)", () => {
|
|
98
|
-
it("preserves falsy-but-valid lookup values (was: || fell through to runJs)", () => {
|
|
99
|
-
const ctx = createMockContext();
|
|
100
|
-
// Pre-v0.3.x: `_.get(data, key) || runJs(key)` — when lookup
|
|
101
|
-
// returned 0, the `||` fell through to runJs (which would throw
|
|
102
|
-
// for "count" not being in scope). Now `=== undefined` check
|
|
103
|
-
// preserves the 0.
|
|
104
|
-
expect(mapper.replaceString("${count}", ctx, { count: 0 } as unknown as ParamsDictionary)).toBe("0");
|
|
105
|
-
expect(mapper.replaceString("${flag}", ctx, { flag: false } as unknown as ParamsDictionary)).toBe("false");
|
|
106
|
-
expect(mapper.replaceString("${empty}", ctx, { empty: "" } as unknown as ParamsDictionary)).toBe("");
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it("JSON-encodes object values in interpolation (was: '[object Object]')", () => {
|
|
110
|
-
const ctx = createMockContext();
|
|
111
|
-
const data = { user: { id: 1, name: "Alice" } };
|
|
112
|
-
const result = mapper.replaceString("payload=${user}", ctx, data as unknown as ParamsDictionary);
|
|
113
|
-
// Pre-v0.3.x: `value as string` → "[object Object]". Now JSON.
|
|
114
|
-
expect(result).toBe('payload={"id":1,"name":"Alice"}');
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it("renders null/undefined interpolation values as empty string", () => {
|
|
118
|
-
const ctx = createMockContext();
|
|
119
|
-
const result = mapper.replaceString("v=${x}", ctx, { x: null } as unknown as ParamsDictionary);
|
|
120
|
-
expect(result).toBe("v=");
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it("strips only the `js/` prefix (slice(3) vs replace('js/', ''))", () => {
|
|
124
|
-
const ctx = createMockContext();
|
|
125
|
-
// A fabricated edge case — an expression that contains the
|
|
126
|
-
// substring "js/" later. Pre-v0.3.x's `replace("js/", "")`
|
|
127
|
-
// would strip the wrong occurrence and break the eval.
|
|
128
|
-
// (The expression here evaluates safely; we just check
|
|
129
|
-
// the prefix-stripping doesn't double-strip.)
|
|
130
|
-
const result = mapper.replaceString('js/"prefix:" + "js/inside"', ctx, {});
|
|
131
|
-
expect(result).toBe("prefix:js/inside");
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
it("provides symmetric scope (func + vars) inside ${...} expressions", () => {
|
|
135
|
-
// Pre-v0.3.x: `${func.X}` threw because runJs was called with
|
|
136
|
-
// only 3 args. Now func/vars are bound in both syntaxes for
|
|
137
|
-
// consistency with `js/...`.
|
|
138
|
-
const ctx = createMockContext({ vars: { count: 7 } });
|
|
139
|
-
const result = mapper.replaceString("${vars.count}", ctx, {});
|
|
140
|
-
expect(result).toBe("7");
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
// =========================================================================
|
|
145
|
-
// Failure modes (BLOK_MAPPER_MODE)
|
|
146
|
-
// =========================================================================
|
|
147
|
-
|
|
148
|
-
describe('mode = "warn" (default) — log + pass-through', () => {
|
|
149
|
-
it("logs an actionable warning via ctx.logger.logLevel", () => {
|
|
150
|
-
const logLevel = vi.fn();
|
|
151
|
-
const ctx = createMockContext({
|
|
152
|
-
logger: { log: vi.fn(), logLevel, error: vi.fn() } as unknown as Context["logger"],
|
|
153
|
-
workflow_name: "wf-X",
|
|
154
|
-
});
|
|
155
|
-
(ctx as Record<string, unknown>)._stepInfo = { name: "step-Y" };
|
|
156
|
-
|
|
157
|
-
const result = mapper.replaceString("js/ctx.req.body.bad.path", ctx, {});
|
|
158
|
-
|
|
159
|
-
// Original literal passes through (back-compat).
|
|
160
|
-
expect(result).toBe("js/ctx.req.body.bad.path");
|
|
161
|
-
// Single warn call with the structured message.
|
|
162
|
-
expect(logLevel).toHaveBeenCalledTimes(1);
|
|
163
|
-
expect(logLevel.mock.calls[0][0]).toBe("warn");
|
|
164
|
-
const message = logLevel.mock.calls[0][1] as string;
|
|
165
|
-
expect(message).toContain('step "step-Y"');
|
|
166
|
-
expect(message).toContain('workflow "wf-X"');
|
|
167
|
-
expect(message).toContain("ctx.req.body.bad.path");
|
|
168
|
-
expect(message).toContain("hint:");
|
|
169
|
-
expect(message).toContain("BLOK_MAPPER_MODE=strict");
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
it("falls back to console.warn when ctx.logger has neither logLevel nor log", () => {
|
|
173
|
-
const consoleWarn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
174
|
-
// Logger object lacking logLevel + log methods.
|
|
175
|
-
const ctx = createMockContext({
|
|
176
|
-
logger: { error: vi.fn() } as unknown as Context["logger"],
|
|
177
|
-
});
|
|
178
|
-
mapper.replaceString("js/ctx.bad.access", ctx, {});
|
|
179
|
-
expect(consoleWarn).toHaveBeenCalledTimes(1);
|
|
180
|
-
expect(consoleWarn.mock.calls[0][0] as string).toContain("[blok][mapper]");
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it("returns the literal placeholder for failed ${...} interpolation", () => {
|
|
184
|
-
const ctx = createMockContext({
|
|
185
|
-
logger: { log: vi.fn(), logLevel: vi.fn(), error: vi.fn() } as unknown as Context["logger"],
|
|
186
|
-
});
|
|
187
|
-
const result = mapper.replaceString("hi ${ctx.req.body.bad.path}", ctx, {});
|
|
188
|
-
// Pre-v0.3.x also did this — we preserve the back-compat.
|
|
189
|
-
expect(result).toBe("hi ${ctx.req.body.bad.path}");
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
describe('mode = "strict" — throws MapperResolutionError', () => {
|
|
194
|
-
it("throws on failed js/ expression with full context", () => {
|
|
195
|
-
setMode("strict");
|
|
196
|
-
const ctx = createMockContext({ workflow_name: "wf-strict" });
|
|
197
|
-
(ctx as Record<string, unknown>)._stepInfo = { name: "step-A" };
|
|
198
|
-
|
|
199
|
-
let thrown: unknown = null;
|
|
200
|
-
try {
|
|
201
|
-
mapper.replaceString("js/ctx.req.body.bad.path", ctx, {});
|
|
202
|
-
} catch (e) {
|
|
203
|
-
thrown = e;
|
|
204
|
-
}
|
|
205
|
-
expect(thrown).toBeInstanceOf(MapperResolutionError);
|
|
206
|
-
const err = thrown as MapperResolutionError;
|
|
207
|
-
expect(err.context.expression).toBe("ctx.req.body.bad.path");
|
|
208
|
-
expect(err.context.syntax).toBe("js");
|
|
209
|
-
expect(err.context.workflowName).toBe("wf-strict");
|
|
210
|
-
expect(err.context.stepName).toBe("step-A");
|
|
211
|
-
expect(err.context.cause).toBeInstanceOf(TypeError);
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
it("throws on failed ${...} expression", () => {
|
|
215
|
-
setMode("strict");
|
|
216
|
-
const ctx = createMockContext();
|
|
217
|
-
expect(() => mapper.replaceString("${ctx.req.body.bad.path}", ctx, {})).toThrow(MapperResolutionError);
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
it("does NOT throw when expression resolves successfully", () => {
|
|
221
|
-
setMode("strict");
|
|
222
|
-
const ctx = createMockContext();
|
|
223
|
-
expect(mapper.replaceString("js/1 + 2", ctx, {})).toBe(3);
|
|
224
|
-
expect(mapper.replaceString("${name}", ctx, { name: "ok" } as unknown as ParamsDictionary)).toBe("ok");
|
|
225
|
-
});
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
describe('mode = "silent" — full suppression (pre-v0.3.x behavior)', () => {
|
|
229
|
-
it("does not log via ctx.logger", () => {
|
|
230
|
-
setMode("silent");
|
|
231
|
-
const logLevel = vi.fn();
|
|
232
|
-
const log = vi.fn();
|
|
233
|
-
const ctx = createMockContext({
|
|
234
|
-
logger: { log, logLevel, error: vi.fn() } as unknown as Context["logger"],
|
|
235
|
-
});
|
|
236
|
-
const result = mapper.replaceString("js/ctx.req.body.bad.path", ctx, {});
|
|
237
|
-
expect(result).toBe("js/ctx.req.body.bad.path");
|
|
238
|
-
expect(logLevel).not.toHaveBeenCalled();
|
|
239
|
-
expect(log).not.toHaveBeenCalled();
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
it("does not log via console.warn", () => {
|
|
243
|
-
setMode("silent");
|
|
244
|
-
const consoleWarn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
245
|
-
const ctx = createMockContext({ logger: undefined as unknown as Context["logger"] });
|
|
246
|
-
mapper.replaceString("js/ctx.bad", ctx, {});
|
|
247
|
-
expect(consoleWarn).not.toHaveBeenCalled();
|
|
248
|
-
});
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
// =========================================================================
|
|
252
|
-
// MapperResolutionError diagnostic content
|
|
253
|
-
// =========================================================================
|
|
254
|
-
|
|
255
|
-
describe("MapperResolutionError — diagnostic message quality", () => {
|
|
256
|
-
it("includes a hint for 'Cannot read properties of undefined' errors", () => {
|
|
257
|
-
setMode("strict");
|
|
258
|
-
const ctx = createMockContext();
|
|
259
|
-
let thrown: MapperResolutionError | null = null;
|
|
260
|
-
try {
|
|
261
|
-
mapper.replaceString("js/ctx.req.body.deeply.nested.value", ctx, {});
|
|
262
|
-
} catch (e) {
|
|
263
|
-
thrown = e as MapperResolutionError;
|
|
264
|
-
}
|
|
265
|
-
expect(thrown?.message).toMatch(/hint: the path/);
|
|
266
|
-
expect(thrown?.message).toMatch(/check the trigger payload/i);
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
it("includes a hint for ReferenceError ('not defined')", () => {
|
|
270
|
-
setMode("strict");
|
|
271
|
-
const ctx = createMockContext();
|
|
272
|
-
let thrown: MapperResolutionError | null = null;
|
|
273
|
-
try {
|
|
274
|
-
mapper.replaceString("js/unknownIdentifier.foo", ctx, {});
|
|
275
|
-
} catch (e) {
|
|
276
|
-
thrown = e as MapperResolutionError;
|
|
277
|
-
}
|
|
278
|
-
expect(thrown?.message).toMatch(/`unknownIdentifier` is not in scope/);
|
|
279
|
-
expect(thrown?.message).toMatch(/ctx, data, func, vars/);
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
it("includes a hint for syntax errors", () => {
|
|
283
|
-
setMode("strict");
|
|
284
|
-
const ctx = createMockContext();
|
|
285
|
-
let thrown: MapperResolutionError | null = null;
|
|
286
|
-
try {
|
|
287
|
-
mapper.replaceString("js/ctx.req.body.+", ctx, {});
|
|
288
|
-
} catch (e) {
|
|
289
|
-
thrown = e as MapperResolutionError;
|
|
290
|
-
}
|
|
291
|
-
expect(thrown?.message).toMatch(/not valid JavaScript/);
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
it("works with `instanceof` after JSON round-trip preservation (Object.setPrototypeOf)", () => {
|
|
295
|
-
setMode("strict");
|
|
296
|
-
const ctx = createMockContext();
|
|
297
|
-
let thrown: unknown = null;
|
|
298
|
-
try {
|
|
299
|
-
mapper.replaceString("js/ctx.req.body.x.y", ctx, {});
|
|
300
|
-
} catch (e) {
|
|
301
|
-
thrown = e;
|
|
302
|
-
}
|
|
303
|
-
expect(thrown instanceof MapperResolutionError).toBe(true);
|
|
304
|
-
expect(thrown instanceof Error).toBe(true);
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
it("attaches Error.cause for native cause-chain support", () => {
|
|
308
|
-
setMode("strict");
|
|
309
|
-
const ctx = createMockContext();
|
|
310
|
-
let thrown: MapperResolutionError | null = null;
|
|
311
|
-
try {
|
|
312
|
-
mapper.replaceString("js/ctx.req.body.x.y", ctx, {});
|
|
313
|
-
} catch (e) {
|
|
314
|
-
thrown = e as MapperResolutionError;
|
|
315
|
-
}
|
|
316
|
-
const e = thrown as Error & { cause?: unknown };
|
|
317
|
-
expect(e.cause).toBeDefined();
|
|
318
|
-
expect(e.cause).toBe(thrown?.context.cause);
|
|
319
|
-
});
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
// =========================================================================
|
|
323
|
-
// replaceObjectStrings — recursion + mutation contract
|
|
324
|
-
// =========================================================================
|
|
325
|
-
|
|
326
|
-
describe("replaceObjectStrings()", () => {
|
|
327
|
-
it("replaces string values in flat object", () => {
|
|
328
|
-
const ctx = createMockContext();
|
|
329
|
-
const data = { greeting: "World" };
|
|
330
|
-
const obj: Record<string, unknown> = { msg: "Hello ${greeting}" };
|
|
331
|
-
mapper.replaceObjectStrings(obj as Record<string, string>, ctx, data);
|
|
332
|
-
expect(obj.msg).toBe("Hello World");
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
it("recursively replaces nested objects", () => {
|
|
336
|
-
const ctx = createMockContext();
|
|
337
|
-
const data = { val: "replaced" };
|
|
338
|
-
const obj: Record<string, unknown> = {
|
|
339
|
-
level1: { level2: "value is ${val}" },
|
|
340
|
-
};
|
|
341
|
-
mapper.replaceObjectStrings(obj as Record<string, string>, ctx, data);
|
|
342
|
-
expect((obj.level1 as Record<string, unknown>).level2).toBe("value is replaced");
|
|
343
|
-
});
|
|
344
|
-
|
|
345
|
-
it("skips non-string, non-object values (null, primitives untouched)", () => {
|
|
346
|
-
const ctx = createMockContext();
|
|
347
|
-
const obj: Record<string, unknown> = { num: 42, bool: true, str: "keep", nullish: null };
|
|
348
|
-
mapper.replaceObjectStrings(obj as Record<string, string>, ctx, {});
|
|
349
|
-
expect(obj.num).toBe(42);
|
|
350
|
-
expect(obj.bool).toBe(true);
|
|
351
|
-
expect(obj.str).toBe("keep");
|
|
352
|
-
expect(obj.nullish).toBe(null);
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
it("preserves the actual resolved type when assigning back to the dictionary slot", () => {
|
|
356
|
-
// `obj.count` ends up as the NUMBER 5, not the string "5",
|
|
357
|
-
// because js/ expressions return their actual evaluated type.
|
|
358
|
-
const ctx = createMockContext({ vars: { count: 5 } });
|
|
359
|
-
const obj: Record<string, unknown> = { count: "js/ctx.vars.count" };
|
|
360
|
-
mapper.replaceObjectStrings(obj as Record<string, string>, ctx, {});
|
|
361
|
-
expect(obj.count).toBe(5);
|
|
362
|
-
expect(typeof obj.count).toBe("number");
|
|
363
|
-
});
|
|
364
|
-
});
|
|
365
|
-
|
|
366
|
-
// =========================================================================
|
|
367
|
-
// jsMapper via replaceString
|
|
368
|
-
// =========================================================================
|
|
369
|
-
|
|
370
|
-
describe("jsMapper via replaceString", () => {
|
|
371
|
-
it("accesses ctx in js/ expressions", () => {
|
|
372
|
-
const ctx = createMockContext({ vars: { count: 5 } });
|
|
373
|
-
const result = mapper.replaceString("js/ctx.vars.count", ctx, {});
|
|
374
|
-
expect(result).toBe(5);
|
|
375
|
-
});
|
|
376
|
-
|
|
377
|
-
it("handles js/ errors in default mode (warn) by passing through the literal", () => {
|
|
378
|
-
const ctx = createMockContext({
|
|
379
|
-
logger: { log: vi.fn(), logLevel: vi.fn(), error: vi.fn() } as unknown as Context["logger"],
|
|
380
|
-
});
|
|
381
|
-
const result = mapper.replaceString('js/throw new Error("fail")', ctx, {});
|
|
382
|
-
// Original literal passes through (back-compat).
|
|
383
|
-
expect(result).toBe('js/throw new Error("fail")');
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
it("returns the actual evaluated type (number, object, array) — not a string", () => {
|
|
387
|
-
const ctx = createMockContext();
|
|
388
|
-
expect(mapper.replaceString("js/[1, 2, 3]", ctx, {})).toEqual([1, 2, 3]);
|
|
389
|
-
expect(mapper.replaceString('js/({hello: "world"})', ctx, {})).toEqual({ hello: "world" });
|
|
390
|
-
expect(mapper.replaceString("js/true", ctx, {})).toBe(true);
|
|
391
|
-
});
|
|
392
|
-
});
|
|
393
|
-
});
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { MapperResolutionError } from "../../../src/utils/MapperResolutionError";
|
|
3
|
-
|
|
4
|
-
describe("MapperResolutionError", () => {
|
|
5
|
-
it("constructs with name 'MapperResolutionError'", () => {
|
|
6
|
-
const e = new MapperResolutionError("msg", { expression: "x", syntax: "js" });
|
|
7
|
-
expect(e.name).toBe("MapperResolutionError");
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it("preserves the prototype chain across `instanceof` checks", () => {
|
|
11
|
-
const e = new MapperResolutionError("msg", { expression: "x", syntax: "js" });
|
|
12
|
-
expect(e instanceof MapperResolutionError).toBe(true);
|
|
13
|
-
expect(e instanceof Error).toBe(true);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it("carries the structured context object verbatim", () => {
|
|
17
|
-
const cause = new TypeError("boom");
|
|
18
|
-
const e = new MapperResolutionError("msg", {
|
|
19
|
-
expression: "ctx.req.body.id",
|
|
20
|
-
syntax: "js",
|
|
21
|
-
workflowName: "wf-1",
|
|
22
|
-
stepName: "step-2",
|
|
23
|
-
cause,
|
|
24
|
-
});
|
|
25
|
-
expect(e.context.expression).toBe("ctx.req.body.id");
|
|
26
|
-
expect(e.context.syntax).toBe("js");
|
|
27
|
-
expect(e.context.workflowName).toBe("wf-1");
|
|
28
|
-
expect(e.context.stepName).toBe("step-2");
|
|
29
|
-
expect(e.context.cause).toBe(cause);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it("attaches Error.cause when context.cause is provided (ES2022 cause-chain)", () => {
|
|
33
|
-
const cause = new Error("underlying");
|
|
34
|
-
const e = new MapperResolutionError("msg", { expression: "x", syntax: "js", cause });
|
|
35
|
-
// `cause` is set on the Error instance per spec.
|
|
36
|
-
expect((e as Error & { cause?: unknown }).cause).toBe(cause);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it("does NOT set Error.cause when context.cause is omitted", () => {
|
|
40
|
-
const e = new MapperResolutionError("msg", { expression: "x", syntax: "js" });
|
|
41
|
-
expect((e as Error & { cause?: unknown }).cause).toBeUndefined();
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it("supports both syntax discriminators (js + template)", () => {
|
|
45
|
-
const a = new MapperResolutionError("msg", { expression: "ctx.x", syntax: "js" });
|
|
46
|
-
const b = new MapperResolutionError("msg", { expression: "ctx.x", syntax: "template" });
|
|
47
|
-
expect(a.context.syntax).toBe("js");
|
|
48
|
-
expect(b.context.syntax).toBe("template");
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it("captures the original message verbatim (multi-line OK)", () => {
|
|
52
|
-
const msg = "[blok][mapper] Failed to resolve `js/x`\n underlying: bad\n hint: try this";
|
|
53
|
-
const e = new MapperResolutionError(msg, { expression: "x", syntax: "js" });
|
|
54
|
-
expect(e.message).toBe(msg);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it("makes context fields readonly at the type level (compile-time check)", () => {
|
|
58
|
-
// This test exists for documentation — TS rejects mutation on
|
|
59
|
-
// `readonly` fields at compile time. At runtime, the object
|
|
60
|
-
// is plain. We assert the shape, not enforcement.
|
|
61
|
-
const e = new MapperResolutionError("msg", { expression: "x", syntax: "js" });
|
|
62
|
-
expect(Object.isFrozen(e.context)).toBe(false); // readonly is type-level only
|
|
63
|
-
});
|
|
64
|
-
});
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import os from "node:os";
|
|
2
|
-
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
-
import MemoryUsage from "../../../src/utils/MemoryUsage";
|
|
4
|
-
|
|
5
|
-
describe("MemoryUsage", () => {
|
|
6
|
-
let memory: MemoryUsage;
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
memory = new MemoryUsage();
|
|
10
|
-
vi.restoreAllMocks();
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
describe("start()", () => {
|
|
14
|
-
it("should increment counter", () => {
|
|
15
|
-
vi.spyOn(process, "memoryUsage").mockReturnValue({
|
|
16
|
-
heapUsed: 50_000_000,
|
|
17
|
-
heapTotal: 100_000_000,
|
|
18
|
-
rss: 200_000_000,
|
|
19
|
-
external: 10_000_000,
|
|
20
|
-
arrayBuffers: 5_000_000,
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
memory.start();
|
|
24
|
-
memory.start();
|
|
25
|
-
|
|
26
|
-
const metrics = memory.getMetrics();
|
|
27
|
-
// total is average = total_val / counter, with 2 starts
|
|
28
|
-
expect(metrics.total).toBeTypeOf("number");
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("should track min value", () => {
|
|
32
|
-
vi.spyOn(process, "memoryUsage")
|
|
33
|
-
.mockReturnValueOnce({ heapUsed: 100_000_000, heapTotal: 0, rss: 0, external: 0, arrayBuffers: 0 })
|
|
34
|
-
.mockReturnValueOnce({ heapUsed: 50_000_000, heapTotal: 0, rss: 0, external: 0, arrayBuffers: 0 });
|
|
35
|
-
|
|
36
|
-
memory.start();
|
|
37
|
-
memory.start();
|
|
38
|
-
|
|
39
|
-
const metrics = memory.getMetrics();
|
|
40
|
-
expect(metrics.min).toBe(50); // 50_000_000 / 1_000_000
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it("should track max value", () => {
|
|
44
|
-
vi.spyOn(process, "memoryUsage")
|
|
45
|
-
.mockReturnValueOnce({ heapUsed: 50_000_000, heapTotal: 0, rss: 0, external: 0, arrayBuffers: 0 })
|
|
46
|
-
.mockReturnValueOnce({ heapUsed: 100_000_000, heapTotal: 0, rss: 0, external: 0, arrayBuffers: 0 });
|
|
47
|
-
|
|
48
|
-
memory.start();
|
|
49
|
-
memory.start();
|
|
50
|
-
|
|
51
|
-
const metrics = memory.getMetrics();
|
|
52
|
-
expect(metrics.max).toBe(100); // 100_000_000 / 1_000_000
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it("should set min_val on first call", () => {
|
|
56
|
-
vi.spyOn(process, "memoryUsage").mockReturnValue({
|
|
57
|
-
heapUsed: 75_000_000,
|
|
58
|
-
heapTotal: 0,
|
|
59
|
-
rss: 0,
|
|
60
|
-
external: 0,
|
|
61
|
-
arrayBuffers: 0,
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
memory.start();
|
|
65
|
-
|
|
66
|
-
const metrics = memory.getMetrics();
|
|
67
|
-
expect(metrics.min).toBe(75);
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
describe("stop()", () => {
|
|
72
|
-
it("should be a no-op", () => {
|
|
73
|
-
memory.stop();
|
|
74
|
-
// Should not throw or change state
|
|
75
|
-
const metrics = memory.getMetrics();
|
|
76
|
-
expect(metrics.total).toBeNaN(); // 0 / 0
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
describe("getMetrics()", () => {
|
|
81
|
-
it("should return average, min, max, global_memory, global_free_memory", () => {
|
|
82
|
-
vi.spyOn(process, "memoryUsage").mockReturnValue({
|
|
83
|
-
heapUsed: 50_000_000,
|
|
84
|
-
heapTotal: 0,
|
|
85
|
-
rss: 0,
|
|
86
|
-
external: 0,
|
|
87
|
-
arrayBuffers: 0,
|
|
88
|
-
});
|
|
89
|
-
vi.spyOn(os, "totalmem").mockReturnValue(16_000_000_000);
|
|
90
|
-
vi.spyOn(os, "freemem").mockReturnValue(8_000_000_000);
|
|
91
|
-
|
|
92
|
-
memory.start();
|
|
93
|
-
|
|
94
|
-
const metrics = memory.getMetrics();
|
|
95
|
-
expect(metrics).toHaveProperty("total");
|
|
96
|
-
expect(metrics).toHaveProperty("min");
|
|
97
|
-
expect(metrics).toHaveProperty("max");
|
|
98
|
-
expect(metrics.global_memory).toBe(16000);
|
|
99
|
-
expect(metrics.global_free_memory).toBe(8000);
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
describe("clear()", () => {
|
|
104
|
-
it("should reset all values to 0", () => {
|
|
105
|
-
vi.spyOn(process, "memoryUsage").mockReturnValue({
|
|
106
|
-
heapUsed: 50_000_000,
|
|
107
|
-
heapTotal: 0,
|
|
108
|
-
rss: 0,
|
|
109
|
-
external: 0,
|
|
110
|
-
arrayBuffers: 0,
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
memory.start();
|
|
114
|
-
memory.clear();
|
|
115
|
-
|
|
116
|
-
const metrics = memory.getMetrics();
|
|
117
|
-
expect(metrics.min).toBe(0);
|
|
118
|
-
expect(metrics.max).toBe(0);
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
});
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it } from "vitest";
|
|
2
|
-
import Time from "../../../src/utils/Time";
|
|
3
|
-
|
|
4
|
-
describe("Time", () => {
|
|
5
|
-
let time: Time;
|
|
6
|
-
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
time = new Time();
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
describe("start()", () => {
|
|
12
|
-
it("should record start time as dayjs format", () => {
|
|
13
|
-
time.start();
|
|
14
|
-
const metrics = time.getMetrics();
|
|
15
|
-
expect(metrics.startTime).toBeTypeOf("string");
|
|
16
|
-
expect(metrics.startTime).not.toBeNull();
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe("stop()", () => {
|
|
21
|
-
it("should record end time as dayjs format", () => {
|
|
22
|
-
time.start();
|
|
23
|
-
time.stop();
|
|
24
|
-
const metrics = time.getMetrics();
|
|
25
|
-
expect(metrics.endTime).toBeTypeOf("string");
|
|
26
|
-
expect(metrics.endTime).not.toBeNull();
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe("getMetrics()", () => {
|
|
31
|
-
it("should return startTime, endTime, duration", () => {
|
|
32
|
-
time.start();
|
|
33
|
-
time.stop();
|
|
34
|
-
|
|
35
|
-
const metrics = time.getMetrics();
|
|
36
|
-
expect(metrics).toHaveProperty("startTime");
|
|
37
|
-
expect(metrics).toHaveProperty("endTime");
|
|
38
|
-
expect(metrics).toHaveProperty("duration");
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("should have positive duration after start/stop", () => {
|
|
42
|
-
time.start();
|
|
43
|
-
// Small delay to ensure measurable duration
|
|
44
|
-
for (let i = 0; i < 1000; i++) {
|
|
45
|
-
/* spin */
|
|
46
|
-
}
|
|
47
|
-
time.stop();
|
|
48
|
-
|
|
49
|
-
const metrics = time.getMetrics();
|
|
50
|
-
expect(metrics.duration).toBeGreaterThanOrEqual(0);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("should return null times before start", () => {
|
|
54
|
-
const metrics = time.getMetrics();
|
|
55
|
-
expect(metrics.startTime).toBeNull();
|
|
56
|
-
expect(metrics.endTime).toBeNull();
|
|
57
|
-
expect(metrics.duration).toBe(0);
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
});
|
package/tsconfig.json
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "es2022",
|
|
4
|
-
"module": "es2022",
|
|
5
|
-
"moduleResolution": "bundler",
|
|
6
|
-
"rootDir": "./src",
|
|
7
|
-
"declaration": true,
|
|
8
|
-
"sourceMap": true,
|
|
9
|
-
"outDir": "./dist",
|
|
10
|
-
"esModuleInterop": true,
|
|
11
|
-
"forceConsistentCasingInFileNames": true,
|
|
12
|
-
"strict": true,
|
|
13
|
-
"noUnusedLocals": true,
|
|
14
|
-
"noImplicitReturns": true,
|
|
15
|
-
"skipLibCheck": true
|
|
16
|
-
},
|
|
17
|
-
"compileOnSave": true,
|
|
18
|
-
"include": ["./src"]
|
|
19
|
-
}
|
package/vitest.config.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import { defineConfig } from "vitest/config";
|
|
3
|
-
|
|
4
|
-
export default defineConfig({
|
|
5
|
-
test: {
|
|
6
|
-
globals: true,
|
|
7
|
-
environment: "node",
|
|
8
|
-
coverage: {
|
|
9
|
-
provider: "istanbul",
|
|
10
|
-
reporter: ["text", "json", "html", "lcov"],
|
|
11
|
-
exclude: ["node_modules/", "dist/", "**/*.d.ts", "**/*.config.ts", "__tests__/", "src/types/", "src/index.ts"],
|
|
12
|
-
thresholds: {
|
|
13
|
-
lines: 90,
|
|
14
|
-
functions: 90,
|
|
15
|
-
branches: 85,
|
|
16
|
-
statements: 90,
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
include: ["__tests__/**/*.test.ts"],
|
|
20
|
-
exclude: ["node_modules", "dist"],
|
|
21
|
-
testTimeout: 10000,
|
|
22
|
-
hookTimeout: 10000,
|
|
23
|
-
},
|
|
24
|
-
resolve: {
|
|
25
|
-
alias: {
|
|
26
|
-
"@": path.resolve(__dirname, "./src"),
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
});
|