@node-llm/testing 0.1.0 → 0.2.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/CHANGELOG.md +22 -1
- package/LICENSE +21 -0
- package/README.md +42 -3
- package/dist/Mocker.d.ts +27 -2
- package/dist/Mocker.d.ts.map +1 -1
- package/dist/Mocker.js +43 -3
- package/dist/Scrubber.d.ts.map +1 -1
- package/dist/Scrubber.js +25 -0
- package/dist/Serializer.d.ts +16 -0
- package/dist/Serializer.d.ts.map +1 -0
- package/dist/Serializer.js +107 -0
- package/dist/vcr.d.ts +8 -6
- package/dist/vcr.d.ts.map +1 -1
- package/dist/vcr.js +11 -11
- package/package.json +8 -8
- package/src/Mocker.ts +67 -3
- package/src/Scrubber.ts +30 -0
- package/src/Serializer.ts +113 -0
- package/src/vcr.ts +37 -27
- package/test/cassettes/handles-rich-types.json +38 -0
- package/test/unit/__snapshots__/mocker_snapshots.test.ts.snap +12 -0
- package/test/unit/dx.test.ts +1 -1
- package/test/unit/mocker_history.test.ts +115 -0
- package/test/unit/mocker_snapshots.test.ts +48 -0
- package/test/unit/scoping.test.ts +8 -4
- package/test/unit/scrubbing.test.ts +9 -2
- package/test/unit/serializer.test.ts +81 -0
- package/test/unit/vcr-global-config.test.ts +15 -3
- package/test/unit/vcr-mismatch.test.ts +2 -1
- package/test/unit/vcr_advanced_types.test.ts +72 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## 0.
|
|
3
|
+
## [0.2.0] - 2026-01-26
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- **Call Verification**: Added `mocker.history`, `mocker.getCalls()`, and `mocker.getLastCall()` for spy-style assertions.
|
|
8
|
+
- **Prompt Snapshots**: Added `mocker.getLastCall().prompt` convenience accessor for snapshot testing of request structures.
|
|
9
|
+
- **Rich Type Persistence**: VCR now faithfully records and replays `Date`, `Map`, `Set`, `Buffer`, `RegExp`, `Infinity`, `NaN`, and `Error` objects using a new Smart Serializer.
|
|
10
|
+
- **Deep Scrubbing**: `Scrubber` now recurses into `Map` and `Set` collections to redact sensitive data.
|
|
11
|
+
- **Modern Cloning**: Switched to `structuredClone` for high-performance in-memory operations.
|
|
12
|
+
|
|
13
|
+
### Security
|
|
14
|
+
|
|
15
|
+
- **Hardened Scrubbing**: Fixed potential leak where secrets inside nested Maps/Sets were skipped by the scrubber.
|
|
16
|
+
|
|
17
|
+
### Improvements
|
|
18
|
+
|
|
19
|
+
- **Documentation**: Added comprehensive "Testing" section to project documentation and `llms.txt`.
|
|
20
|
+
- **Reliability**: VCR no longer corrupts binary data (Buffers) during recording.
|
|
21
|
+
|
|
22
|
+
## [0.1.0]
|
|
23
|
+
|
|
24
|
+
### Added
|
|
4
25
|
|
|
5
26
|
- Initial release
|
|
6
27
|
- VCR record/replay
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NodeLLM contributors
|
|
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.
|
package/README.md
CHANGED
|
@@ -116,6 +116,30 @@ mocker.paint(/a cat/i).respond({ url: "https://mock.com/cat.png" });
|
|
|
116
116
|
mocker.embed("text").respond({ vectors: [[0.1, 0.2, 0.3]] });
|
|
117
117
|
```
|
|
118
118
|
|
|
119
|
+
### Call Verification & History 🕵️♀️
|
|
120
|
+
|
|
121
|
+
Inspect what requests were sent to your mock, enabling "spy" style assertions.
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
// 1. Check full history
|
|
125
|
+
const history = mocker.history;
|
|
126
|
+
expect(history.length).toBe(1);
|
|
127
|
+
|
|
128
|
+
// 2. Filter by method
|
|
129
|
+
const chats = mocker.getCalls("chat");
|
|
130
|
+
expect(chats[0].args[0].messages[0].content).toContain("Hello");
|
|
131
|
+
|
|
132
|
+
// 3. Get the most recent call
|
|
133
|
+
const lastEmbed = mocker.getLastCall("embed");
|
|
134
|
+
expect(lastEmbed.args[0].input).toBe("text to embed");
|
|
135
|
+
|
|
136
|
+
// 4. Reset history (keep mocks)
|
|
137
|
+
mocker.resetHistory();
|
|
138
|
+
|
|
139
|
+
// 5. Snapshot valid request structures
|
|
140
|
+
expect(mocker.getLastCall().prompt).toMatchSnapshot();
|
|
141
|
+
```
|
|
142
|
+
|
|
119
143
|
---
|
|
120
144
|
|
|
121
145
|
## 🛣️ Decision Tree: VCR vs Mocker
|
|
@@ -438,9 +462,6 @@ interface VCROptions {
|
|
|
438
462
|
interface MockerOptions {
|
|
439
463
|
// Enforce exact matching
|
|
440
464
|
strict?: boolean;
|
|
441
|
-
|
|
442
|
-
// Enable verbose logging
|
|
443
|
-
debug?: boolean;
|
|
444
465
|
}
|
|
445
466
|
```
|
|
446
467
|
|
|
@@ -481,6 +502,24 @@ interface MockerDebugInfo {
|
|
|
481
502
|
}
|
|
482
503
|
```
|
|
483
504
|
|
|
505
|
+
### MockCall
|
|
506
|
+
|
|
507
|
+
```typescript
|
|
508
|
+
interface MockCall {
|
|
509
|
+
// The method name ("chat", "stream", etc.)
|
|
510
|
+
method: string;
|
|
511
|
+
|
|
512
|
+
// The arguments passed to the method
|
|
513
|
+
args: unknown[];
|
|
514
|
+
|
|
515
|
+
// Timestamp of the call
|
|
516
|
+
timestamp: number;
|
|
517
|
+
|
|
518
|
+
// Convenience prompt accessor (e.g. messages, input text)
|
|
519
|
+
prompt?: unknown;
|
|
520
|
+
}
|
|
521
|
+
```
|
|
522
|
+
|
|
484
523
|
---
|
|
485
524
|
|
|
486
525
|
## 🏛️ Integration with @node-llm/orm
|
package/dist/Mocker.d.ts
CHANGED
|
@@ -31,10 +31,35 @@ export interface MockerDebugInfo {
|
|
|
31
31
|
totalMocks: number;
|
|
32
32
|
methods: string[];
|
|
33
33
|
}
|
|
34
|
+
export interface MockerOptions {
|
|
35
|
+
/**
|
|
36
|
+
* Enforce that every LLM call must have a corresponding mock.
|
|
37
|
+
* If true, unmocked calls throw an error.
|
|
38
|
+
*/
|
|
39
|
+
strict?: boolean;
|
|
40
|
+
}
|
|
41
|
+
export interface MockCall {
|
|
42
|
+
method: string;
|
|
43
|
+
args: unknown[];
|
|
44
|
+
timestamp: number;
|
|
45
|
+
/**
|
|
46
|
+
* Convenience accessor for the primary input "prompt" of the call.
|
|
47
|
+
* - chat/stream: `messages`
|
|
48
|
+
* - embed/moderate: `input`
|
|
49
|
+
* - paint: `prompt`
|
|
50
|
+
* - transcribe: `file`
|
|
51
|
+
*/
|
|
52
|
+
prompt?: unknown;
|
|
53
|
+
}
|
|
34
54
|
export declare class Mocker {
|
|
35
55
|
private mocks;
|
|
56
|
+
private _history;
|
|
36
57
|
strict: boolean;
|
|
37
|
-
constructor();
|
|
58
|
+
constructor(options?: MockerOptions);
|
|
59
|
+
get history(): MockCall[];
|
|
60
|
+
getCalls(method?: string): MockCall[];
|
|
61
|
+
getLastCall(method?: string): MockCall | undefined;
|
|
62
|
+
resetHistory(): void;
|
|
38
63
|
chat(query?: string | RegExp): this;
|
|
39
64
|
stream(chunks: string[] | ChatChunk[]): this;
|
|
40
65
|
placeholder(query: string | RegExp): this;
|
|
@@ -54,5 +79,5 @@ export declare class Mocker {
|
|
|
54
79
|
private getContentString;
|
|
55
80
|
private setupInterceptor;
|
|
56
81
|
}
|
|
57
|
-
export declare function mockLLM(): Mocker;
|
|
82
|
+
export declare function mockLLM(options?: MockerOptions): Mocker;
|
|
58
83
|
//# sourceMappingURL=Mocker.d.ts.map
|
package/dist/Mocker.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Mocker.d.ts","sourceRoot":"","sources":["../src/Mocker.ts"],"names":[],"mappings":"AAAA,OAAO,EAaL,SAAS,EACT,QAAQ,EACR,gBAAgB,EAEjB,MAAM,gBAAgB,CAAC;AAExB,MAAM,WAAW,YAAY;IAC3B,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,UAAU,CAAC,EAAE,QAAQ,EAAE,CAAC;IACxB,KAAK,CAAC,EAAE;QACN,YAAY,EAAE,MAAM,CAAC;QACrB,aAAa,EAAE,MAAM,CAAC;QACtB,YAAY,EAAE,MAAM,CAAC;KACtB,CAAC;IACF,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,MAAM,CAAC,EAAE,MAAM,EAAE,GAAG,SAAS,EAAE,CAAC;IAChC,OAAO,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC;IACrB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAA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AAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAWzC,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,GAAG,IAAI;IAmBjE,KAAK,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI;IAQtC,KAAK,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IASrC,UAAU,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IASxC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,GAAG,IAAI;IAUlD,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,YAAY,GAAG,CAAC,CAAC,GAAG,EAAE,OAAO,KAAK,YAAY,CAAC,GAAG,IAAI;IAWxF;;;OAGG;IACI,YAAY,IAAI,eAAe;IAQ/B,KAAK,IAAI,IAAI;IAMpB,OAAO,CAAC,OAAO;IAKf,OAAO,CAAC,gBAAgB;IASxB,OAAO,CAAC,gBAAgB;CAkIzB;AAED,wBAAgB,OAAO,CAAC,OAAO,GAAE,aAAkB,UAElD"}
|
package/dist/Mocker.js
CHANGED
|
@@ -2,10 +2,27 @@ import { providerRegistry } from "@node-llm/core";
|
|
|
2
2
|
const EXECUTION_METHODS = ["chat", "stream", "paint", "transcribe", "moderate", "embed"];
|
|
3
3
|
export class Mocker {
|
|
4
4
|
mocks = [];
|
|
5
|
+
_history = [];
|
|
5
6
|
strict = false;
|
|
6
|
-
constructor() {
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
this.strict = !!options.strict;
|
|
7
9
|
this.setupInterceptor();
|
|
8
10
|
}
|
|
11
|
+
get history() {
|
|
12
|
+
return [...this._history];
|
|
13
|
+
}
|
|
14
|
+
getCalls(method) {
|
|
15
|
+
if (!method)
|
|
16
|
+
return this.history;
|
|
17
|
+
return this._history.filter((c) => c.method === method);
|
|
18
|
+
}
|
|
19
|
+
getLastCall(method) {
|
|
20
|
+
const calls = this.getCalls(method);
|
|
21
|
+
return calls[calls.length - 1];
|
|
22
|
+
}
|
|
23
|
+
resetHistory() {
|
|
24
|
+
this._history = [];
|
|
25
|
+
}
|
|
9
26
|
chat(query) {
|
|
10
27
|
return this.addMock("chat", (req) => {
|
|
11
28
|
const chatReq = req;
|
|
@@ -124,6 +141,7 @@ export class Mocker {
|
|
|
124
141
|
}
|
|
125
142
|
clear() {
|
|
126
143
|
this.mocks = [];
|
|
144
|
+
this._history = [];
|
|
127
145
|
providerRegistry.setInterceptor(undefined);
|
|
128
146
|
}
|
|
129
147
|
addMock(method, matcher) {
|
|
@@ -151,6 +169,12 @@ export class Mocker {
|
|
|
151
169
|
if (EXECUTION_METHODS.includes(methodName)) {
|
|
152
170
|
if (methodName === "stream") {
|
|
153
171
|
return async function* (request) {
|
|
172
|
+
this._history.push({
|
|
173
|
+
method: methodName,
|
|
174
|
+
args: [request],
|
|
175
|
+
timestamp: Date.now(),
|
|
176
|
+
prompt: request.messages
|
|
177
|
+
});
|
|
154
178
|
const matchingMocks = this.mocks.filter((m) => m.method === methodName && m.match(request));
|
|
155
179
|
const mock = matchingMocks[matchingMocks.length - 1];
|
|
156
180
|
if (mock) {
|
|
@@ -177,6 +201,22 @@ export class Mocker {
|
|
|
177
201
|
}
|
|
178
202
|
// Promise-based methods
|
|
179
203
|
return (async (request) => {
|
|
204
|
+
const req = request;
|
|
205
|
+
let promptAttr;
|
|
206
|
+
if (methodName === "chat")
|
|
207
|
+
promptAttr = req.messages;
|
|
208
|
+
else if (methodName === "embed" || methodName === "moderate")
|
|
209
|
+
promptAttr = req.input;
|
|
210
|
+
else if (methodName === "paint")
|
|
211
|
+
promptAttr = req.prompt;
|
|
212
|
+
else if (methodName === "transcribe")
|
|
213
|
+
promptAttr = req.file;
|
|
214
|
+
this._history.push({
|
|
215
|
+
method: methodName,
|
|
216
|
+
args: [request],
|
|
217
|
+
timestamp: Date.now(),
|
|
218
|
+
prompt: promptAttr
|
|
219
|
+
});
|
|
180
220
|
const matchingMocks = this.mocks.filter((m) => m.method === methodName && m.match(request));
|
|
181
221
|
const mock = matchingMocks[matchingMocks.length - 1];
|
|
182
222
|
if (mock) {
|
|
@@ -242,6 +282,6 @@ export class Mocker {
|
|
|
242
282
|
});
|
|
243
283
|
}
|
|
244
284
|
}
|
|
245
|
-
export function mockLLM() {
|
|
246
|
-
return new Mocker();
|
|
285
|
+
export function mockLLM(options = {}) {
|
|
286
|
+
return new Mocker(options);
|
|
247
287
|
}
|
package/dist/Scrubber.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Scrubber.d.ts","sourceRoot":"","sources":["../src/Scrubber.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,uBAAuB,UAInC,CAAC;AAEF,MAAM,WAAW,eAAe;IAC9B,cAAc,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC;IAC5C,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC7B,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B;AAED,qBAAa,QAAQ;IACnB,OAAO,CAAC,cAAc,CAAC,CAA6B;IACpD,OAAO,CAAC,iBAAiB,CAAW;IACpC,OAAO,CAAC,aAAa,CAAc;gBAEvB,OAAO,GAAE,eAAoB;IAazC;;OAEG;IACI,KAAK,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO;IAYpC,OAAO,CAAC,SAAS;
|
|
1
|
+
{"version":3,"file":"Scrubber.d.ts","sourceRoot":"","sources":["../src/Scrubber.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,uBAAuB,UAInC,CAAC;AAEF,MAAM,WAAW,eAAe;IAC9B,cAAc,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC;IAC5C,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC7B,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B;AAED,qBAAa,QAAQ;IACnB,OAAO,CAAC,cAAc,CAAC,CAA6B;IACpD,OAAO,CAAC,iBAAiB,CAAW;IACpC,OAAO,CAAC,aAAa,CAAc;gBAEvB,OAAO,GAAE,eAAoB;IAazC;;OAEG;IACI,KAAK,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO;IAYpC,OAAO,CAAC,SAAS;CAqElB"}
|
package/dist/Scrubber.js
CHANGED
|
@@ -42,6 +42,31 @@ export class Scrubber {
|
|
|
42
42
|
if (Array.isArray(val)) {
|
|
43
43
|
return val.map((v) => this.deepScrub(v));
|
|
44
44
|
}
|
|
45
|
+
if (val instanceof Map) {
|
|
46
|
+
const newMap = new Map();
|
|
47
|
+
for (const [k, v] of val.entries()) {
|
|
48
|
+
const scrubbedKey = typeof k === "string" ? this.deepScrub(k) : k;
|
|
49
|
+
const scrubbedValue = this.deepScrub(v);
|
|
50
|
+
newMap.set(scrubbedKey, scrubbedValue);
|
|
51
|
+
}
|
|
52
|
+
return newMap;
|
|
53
|
+
}
|
|
54
|
+
if (val instanceof Set) {
|
|
55
|
+
const newSet = new Set();
|
|
56
|
+
for (const v of val.values()) {
|
|
57
|
+
newSet.add(this.deepScrub(v));
|
|
58
|
+
}
|
|
59
|
+
return newSet;
|
|
60
|
+
}
|
|
61
|
+
if (val instanceof Date) {
|
|
62
|
+
return new Date(val);
|
|
63
|
+
}
|
|
64
|
+
if (val instanceof RegExp) {
|
|
65
|
+
return new RegExp(val);
|
|
66
|
+
}
|
|
67
|
+
if (typeof Buffer !== "undefined" && Buffer.isBuffer(val)) {
|
|
68
|
+
return Buffer.from(val);
|
|
69
|
+
}
|
|
45
70
|
if (val !== null && typeof val === "object") {
|
|
46
71
|
const obj = val;
|
|
47
72
|
const newObj = {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handles serialization and deserialization of complex types that JSON.stringify/parse
|
|
3
|
+
* natively lose (Date, Map, Set, RegExp, Error, Infinity, NaN, Buffer).
|
|
4
|
+
*/
|
|
5
|
+
export declare class Serializer {
|
|
6
|
+
static serialize(data: unknown, space?: string | number): string;
|
|
7
|
+
static deserialize<T = unknown>(json: string): T;
|
|
8
|
+
/**
|
|
9
|
+
* Deep clone that preserves types better than JSON.parse(JSON.stringify(x))
|
|
10
|
+
* Uses structuredClone if available (Node 17+), otherwise falls back to serialization.
|
|
11
|
+
*/
|
|
12
|
+
static clone<T>(data: T): T;
|
|
13
|
+
private static replacer;
|
|
14
|
+
private static reviver;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=Serializer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Serializer.d.ts","sourceRoot":"","sources":["../src/Serializer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,qBAAa,UAAU;WACP,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;WAIzD,WAAW,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC;IAIvD;;;OAGG;WACW,KAAK,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC;IAWlC,OAAO,CAAC,MAAM,CAAC,QAAQ;IA0CvB,OAAO,CAAC,MAAM,CAAC,OAAO;CA0CvB"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handles serialization and deserialization of complex types that JSON.stringify/parse
|
|
3
|
+
* natively lose (Date, Map, Set, RegExp, Error, Infinity, NaN, Buffer).
|
|
4
|
+
*/
|
|
5
|
+
export class Serializer {
|
|
6
|
+
static serialize(data, space) {
|
|
7
|
+
return JSON.stringify(data, Serializer.replacer, space);
|
|
8
|
+
}
|
|
9
|
+
static deserialize(json) {
|
|
10
|
+
return JSON.parse(json, Serializer.reviver);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Deep clone that preserves types better than JSON.parse(JSON.stringify(x))
|
|
14
|
+
* Uses structuredClone if available (Node 17+), otherwise falls back to serialization.
|
|
15
|
+
*/
|
|
16
|
+
static clone(data) {
|
|
17
|
+
if (typeof globalThis.structuredClone === "function") {
|
|
18
|
+
try {
|
|
19
|
+
return globalThis.structuredClone(data);
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
// Fallback for types structuredClone might not handle or implementation quirks
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return Serializer.deserialize(Serializer.serialize(data));
|
|
26
|
+
}
|
|
27
|
+
static replacer(key, value) {
|
|
28
|
+
const originalValue = this[key];
|
|
29
|
+
if (originalValue === Infinity)
|
|
30
|
+
return { $type: "Infinity" };
|
|
31
|
+
if (originalValue === -Infinity)
|
|
32
|
+
return { $type: "-Infinity" };
|
|
33
|
+
if (Number.isNaN(originalValue))
|
|
34
|
+
return { $type: "NaN" };
|
|
35
|
+
if (originalValue instanceof Date) {
|
|
36
|
+
return { $type: "Date", value: originalValue.toISOString() };
|
|
37
|
+
}
|
|
38
|
+
if (originalValue instanceof RegExp) {
|
|
39
|
+
return { $type: "RegExp", source: originalValue.source, flags: originalValue.flags };
|
|
40
|
+
}
|
|
41
|
+
if (originalValue instanceof Map) {
|
|
42
|
+
return { $type: "Map", value: Array.from(originalValue.entries()) };
|
|
43
|
+
}
|
|
44
|
+
if (originalValue instanceof Set) {
|
|
45
|
+
return { $type: "Set", value: Array.from(originalValue.values()) };
|
|
46
|
+
}
|
|
47
|
+
if (originalValue instanceof Error) {
|
|
48
|
+
return {
|
|
49
|
+
...originalValue, // Capture literal properties attached to the error
|
|
50
|
+
$type: "Error",
|
|
51
|
+
name: originalValue.name,
|
|
52
|
+
message: originalValue.message,
|
|
53
|
+
stack: originalValue.stack,
|
|
54
|
+
cause: originalValue.cause
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
// Handle Buffers (Node.js)
|
|
58
|
+
if (typeof Buffer !== "undefined" && Buffer.isBuffer(originalValue)) {
|
|
59
|
+
return { $type: "Buffer", value: originalValue.toString("base64") };
|
|
60
|
+
}
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
static reviver(key, value) {
|
|
64
|
+
if (value && typeof value === "object" && "$type" in value) {
|
|
65
|
+
const typedValue = value;
|
|
66
|
+
switch (typedValue.$type) {
|
|
67
|
+
case "Date":
|
|
68
|
+
return new Date(typedValue.value);
|
|
69
|
+
case "RegExp":
|
|
70
|
+
return new RegExp(typedValue.source, typedValue.flags);
|
|
71
|
+
case "Map":
|
|
72
|
+
return new Map(typedValue.value);
|
|
73
|
+
case "Set":
|
|
74
|
+
return new Set(typedValue.value);
|
|
75
|
+
case "Infinity":
|
|
76
|
+
return Infinity;
|
|
77
|
+
case "-Infinity":
|
|
78
|
+
return -Infinity;
|
|
79
|
+
case "NaN":
|
|
80
|
+
return NaN;
|
|
81
|
+
case "Error": {
|
|
82
|
+
const err = new Error(typedValue.message);
|
|
83
|
+
err.name = typedValue.name;
|
|
84
|
+
if (typedValue.stack)
|
|
85
|
+
err.stack = typedValue.stack;
|
|
86
|
+
if (typedValue.cause)
|
|
87
|
+
err.cause = typedValue.cause;
|
|
88
|
+
// Restore other properties
|
|
89
|
+
for (const k in typedValue) {
|
|
90
|
+
if (["$type", "name", "message", "stack", "cause"].includes(k))
|
|
91
|
+
continue;
|
|
92
|
+
err[k] = typedValue[k];
|
|
93
|
+
}
|
|
94
|
+
return err;
|
|
95
|
+
}
|
|
96
|
+
case "Buffer":
|
|
97
|
+
if (typeof Buffer !== "undefined") {
|
|
98
|
+
return Buffer.from(typedValue.value, "base64");
|
|
99
|
+
}
|
|
100
|
+
return typedValue.value;
|
|
101
|
+
default:
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
}
|
package/dist/vcr.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export type VCRMode = "record" | "replay" | "auto" | "passthrough";
|
|
2
2
|
export interface VCRInteraction {
|
|
3
3
|
method: string;
|
|
4
|
-
request:
|
|
5
|
-
response:
|
|
6
|
-
chunks?:
|
|
4
|
+
request: unknown;
|
|
5
|
+
response: unknown;
|
|
6
|
+
chunks?: unknown[];
|
|
7
7
|
}
|
|
8
8
|
export interface VCRCassette {
|
|
9
9
|
name: string;
|
|
@@ -18,11 +18,13 @@ export interface VCRCassette {
|
|
|
18
18
|
}
|
|
19
19
|
export interface VCROptions {
|
|
20
20
|
mode?: VCRMode;
|
|
21
|
-
scrub?: (data:
|
|
21
|
+
scrub?: (data: unknown) => unknown;
|
|
22
22
|
cassettesDir?: string;
|
|
23
23
|
scope?: string | string[];
|
|
24
24
|
sensitivePatterns?: RegExp[];
|
|
25
25
|
sensitiveKeys?: string[];
|
|
26
|
+
/** @internal - For testing VCR library itself. Bypasses CI recording check. */
|
|
27
|
+
_allowRecordingInCI?: boolean;
|
|
26
28
|
}
|
|
27
29
|
export declare class VCR {
|
|
28
30
|
private cassette;
|
|
@@ -35,8 +37,8 @@ export declare class VCR {
|
|
|
35
37
|
get currentMode(): VCRMode;
|
|
36
38
|
stop(): Promise<void>;
|
|
37
39
|
private get interactionsCount();
|
|
38
|
-
execute(method: string, originalMethod: (...args:
|
|
39
|
-
executeStream(method: string, originalMethod: (...args:
|
|
40
|
+
execute(method: string, originalMethod: (...args: unknown[]) => Promise<unknown>, request: unknown): Promise<unknown>;
|
|
41
|
+
executeStream(method: string, originalMethod: (...args: unknown[]) => AsyncIterable<unknown>, request: unknown): AsyncIterable<unknown>;
|
|
40
42
|
private clone;
|
|
41
43
|
private slugify;
|
|
42
44
|
}
|
package/dist/vcr.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vcr.d.ts","sourceRoot":"","sources":["../src/vcr.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"vcr.d.ts","sourceRoot":"","sources":["../src/vcr.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AE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAkB9F"}
|
package/dist/vcr.js
CHANGED
|
@@ -2,6 +2,7 @@ import { providerRegistry } from "@node-llm/core";
|
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { Scrubber } from "./Scrubber.js";
|
|
5
|
+
import { Serializer } from "./Serializer.js";
|
|
5
6
|
// Internal state for nested scoping (Feature 12)
|
|
6
7
|
const currentVCRScopes = [];
|
|
7
8
|
// Try to import Vitest's expect to get test state
|
|
@@ -60,8 +61,9 @@ export class VCR {
|
|
|
60
61
|
const initialMode = mergedOptions.mode || process.env.VCR_MODE || "auto";
|
|
61
62
|
const isCI = !!process.env.CI;
|
|
62
63
|
const exists = fs.existsSync(this.filePath);
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
const allowRecordingInCI = mergedOptions._allowRecordingInCI === true;
|
|
65
|
+
// CI Enforcement (can be bypassed for VCR library's own unit tests)
|
|
66
|
+
if (isCI && !allowRecordingInCI) {
|
|
65
67
|
if (initialMode === "record") {
|
|
66
68
|
throw new Error(`VCR[${name}]: Recording cassettes is not allowed in CI.`);
|
|
67
69
|
}
|
|
@@ -95,7 +97,7 @@ export class VCR {
|
|
|
95
97
|
if (!exists) {
|
|
96
98
|
throw new Error(`VCR[${name}]: Cassette not found at ${this.filePath}`);
|
|
97
99
|
}
|
|
98
|
-
this.cassette =
|
|
100
|
+
this.cassette = Serializer.deserialize(fs.readFileSync(this.filePath, "utf-8"));
|
|
99
101
|
}
|
|
100
102
|
else {
|
|
101
103
|
this.cassette = {
|
|
@@ -123,7 +125,7 @@ export class VCR {
|
|
|
123
125
|
if (this.cassette.metadata) {
|
|
124
126
|
this.cassette.metadata.duration = duration;
|
|
125
127
|
}
|
|
126
|
-
fs.writeFileSync(this.filePath,
|
|
128
|
+
fs.writeFileSync(this.filePath, Serializer.serialize(this.cassette, 2));
|
|
127
129
|
}
|
|
128
130
|
providerRegistry.setInterceptor(undefined);
|
|
129
131
|
}
|
|
@@ -178,12 +180,7 @@ export class VCR {
|
|
|
178
180
|
}
|
|
179
181
|
}
|
|
180
182
|
clone(obj) {
|
|
181
|
-
|
|
182
|
-
return JSON.parse(JSON.stringify(obj));
|
|
183
|
-
}
|
|
184
|
-
catch {
|
|
185
|
-
return obj;
|
|
186
|
-
}
|
|
183
|
+
return Serializer.clone(obj);
|
|
187
184
|
}
|
|
188
185
|
slugify(text) {
|
|
189
186
|
return text
|
|
@@ -241,11 +238,14 @@ export function withVCR(...args) {
|
|
|
241
238
|
options = args[0] || {};
|
|
242
239
|
fn = args[1];
|
|
243
240
|
}
|
|
241
|
+
if (!fn) {
|
|
242
|
+
throw new Error("VCR: No test function provided.");
|
|
243
|
+
}
|
|
244
244
|
// Pass captured inherited scopes if not explicitly overridden
|
|
245
245
|
if (capturedScopes.length > 0 && !options.scope) {
|
|
246
246
|
options.scope = capturedScopes;
|
|
247
247
|
}
|
|
248
|
-
if (!name && vitestExpect) {
|
|
248
|
+
if (!name && vitestExpect?.getState) {
|
|
249
249
|
const state = vitestExpect.getState();
|
|
250
250
|
name = state.currentTestName || "unnamed-test";
|
|
251
251
|
}
|
package/package.json
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@node-llm/testing",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Deterministic testing for NodeLLM powered AI systems",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
7
|
+
"dependencies": {},
|
|
8
|
+
"devDependencies": {
|
|
9
|
+
"vitest": "^1.0.0",
|
|
10
|
+
"typescript": "^5.0.0",
|
|
11
|
+
"@node-llm/core": "1.8.0"
|
|
12
|
+
},
|
|
7
13
|
"scripts": {
|
|
8
14
|
"build": "tsc",
|
|
9
15
|
"test": "vitest run",
|
|
10
16
|
"test:record": "VCR_MODE=record vitest run",
|
|
11
17
|
"test:vitest": "vitest run"
|
|
12
|
-
},
|
|
13
|
-
"dependencies": {},
|
|
14
|
-
"devDependencies": {
|
|
15
|
-
"@node-llm/core": "workspace:*",
|
|
16
|
-
"vitest": "^1.0.0",
|
|
17
|
-
"typescript": "^5.0.0"
|
|
18
18
|
}
|
|
19
|
-
}
|
|
19
|
+
}
|