@contractspec/lib.testing 1.57.0 → 1.58.0
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/adapters/jest-adapter.d.ts +7 -11
- package/dist/adapters/jest-adapter.d.ts.map +1 -1
- package/dist/adapters/vitest-adapter.d.ts +7 -11
- package/dist/adapters/vitest-adapter.d.ts.map +1 -1
- package/dist/generator/assertion-builder.d.ts +6 -10
- package/dist/generator/assertion-builder.d.ts.map +1 -1
- package/dist/generator/golden-test-generator.d.ts +19 -23
- package/dist/generator/golden-test-generator.d.ts.map +1 -1
- package/dist/generator/golden-test-generator.test.d.ts +2 -0
- package/dist/generator/golden-test-generator.test.d.ts.map +1 -0
- package/dist/index.d.ts +7 -7
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +223 -6
- package/dist/node/index.js +223 -0
- package/dist/recorder/traffic-recorder.d.ts +30 -34
- package/dist/recorder/traffic-recorder.d.ts.map +1 -1
- package/dist/types.d.ts +36 -40
- package/dist/types.d.ts.map +1 -1
- package/package.json +20 -16
- package/dist/adapters/jest-adapter.js +0 -28
- package/dist/adapters/jest-adapter.js.map +0 -1
- package/dist/adapters/vitest-adapter.js +0 -27
- package/dist/adapters/vitest-adapter.js.map +0 -1
- package/dist/generator/assertion-builder.js +0 -16
- package/dist/generator/assertion-builder.js.map +0 -1
- package/dist/generator/golden-test-generator.js +0 -87
- package/dist/generator/golden-test-generator.js.map +0 -1
- package/dist/recorder/traffic-recorder.js +0 -60
- package/dist/recorder/traffic-recorder.js.map +0 -1
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
import { GoldenTestCase } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
runnerImport: string;
|
|
8
|
-
runnerFunction: string;
|
|
1
|
+
import type { GoldenTestCase } from '../types';
|
|
2
|
+
export interface JestAdapterOptions {
|
|
3
|
+
suiteName: string;
|
|
4
|
+
cases: GoldenTestCase[];
|
|
5
|
+
runnerImport: string;
|
|
6
|
+
runnerFunction: string;
|
|
9
7
|
}
|
|
10
|
-
declare function generateJestSuite(options: JestAdapterOptions): string;
|
|
11
|
-
//#endregion
|
|
12
|
-
export { JestAdapterOptions, generateJestSuite };
|
|
8
|
+
export declare function generateJestSuite(options: JestAdapterOptions): string;
|
|
13
9
|
//# sourceMappingURL=jest-adapter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jest-adapter.d.ts","
|
|
1
|
+
{"version":3,"file":"jest-adapter.d.ts","sourceRoot":"","sources":["../../src/adapters/jest-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAG/C,MAAM,WAAW,kBAAkB;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,cAAc,EAAE,CAAC;IACxB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,UA0B5D"}
|
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
import { GoldenTestCase } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
runnerImport: string;
|
|
8
|
-
runnerFunction: string;
|
|
1
|
+
import type { GoldenTestCase } from '../types';
|
|
2
|
+
export interface VitestAdapterOptions {
|
|
3
|
+
suiteName: string;
|
|
4
|
+
cases: GoldenTestCase[];
|
|
5
|
+
runnerImport: string;
|
|
6
|
+
runnerFunction: string;
|
|
9
7
|
}
|
|
10
|
-
declare function generateVitestSuite(options: VitestAdapterOptions): string;
|
|
11
|
-
//#endregion
|
|
12
|
-
export { VitestAdapterOptions, generateVitestSuite };
|
|
8
|
+
export declare function generateVitestSuite(options: VitestAdapterOptions): string;
|
|
13
9
|
//# sourceMappingURL=vitest-adapter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vitest-adapter.d.ts","
|
|
1
|
+
{"version":3,"file":"vitest-adapter.d.ts","sourceRoot":"","sources":["../../src/adapters/vitest-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAG/C,MAAM,WAAW,oBAAoB;IACnC,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,cAAc,EAAE,CAAC;IACxB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,oBAAoB,UA8BhE"}
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
import { GoldenTestCase } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
runnerCall: string;
|
|
6
|
-
caseRef: string;
|
|
1
|
+
import type { GoldenTestCase } from '../types';
|
|
2
|
+
export interface AssertionContext {
|
|
3
|
+
runnerCall: string;
|
|
4
|
+
caseRef: string;
|
|
7
5
|
}
|
|
8
|
-
declare function buildAssertions(testCase: GoldenTestCase, ctx: AssertionContext): string;
|
|
9
|
-
declare function serialize(value: unknown): string;
|
|
10
|
-
//#endregion
|
|
11
|
-
export { AssertionContext, buildAssertions, serialize };
|
|
6
|
+
export declare function buildAssertions(testCase: GoldenTestCase, ctx: AssertionContext): string;
|
|
7
|
+
export declare function serialize(value: unknown): string;
|
|
12
8
|
//# sourceMappingURL=assertion-builder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assertion-builder.d.ts","
|
|
1
|
+
{"version":3,"file":"assertion-builder.d.ts","sourceRoot":"","sources":["../../src/generator/assertion-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE/C,MAAM,WAAW,gBAAgB;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,cAAc,EACxB,GAAG,EAAE,gBAAgB,UAWtB;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,UAUvC"}
|
|
@@ -1,27 +1,23 @@
|
|
|
1
|
-
import { GoldenTestCase, TrafficSnapshot } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
framework?: 'vitest' | 'jest';
|
|
9
|
-
serializeMetadata?: (snapshot: TrafficSnapshot) => Record<string, unknown>;
|
|
1
|
+
import type { GoldenTestCase, TrafficSnapshot } from '../types';
|
|
2
|
+
export interface GoldenTestGeneratorOptions {
|
|
3
|
+
suiteName: string;
|
|
4
|
+
runnerImport: string;
|
|
5
|
+
runnerFunction: string;
|
|
6
|
+
framework?: 'vitest' | 'jest';
|
|
7
|
+
serializeMetadata?: (snapshot: TrafficSnapshot) => Record<string, unknown>;
|
|
10
8
|
}
|
|
11
|
-
declare class GoldenTestGenerator {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
export declare class GoldenTestGenerator {
|
|
10
|
+
private readonly serializeMetadata;
|
|
11
|
+
constructor(serializeMetadata?: GoldenTestGeneratorOptions['serializeMetadata']);
|
|
12
|
+
createCases(snapshots: TrafficSnapshot[]): GoldenTestCase[];
|
|
13
|
+
generate(snapshots: TrafficSnapshot[], options: GoldenTestGeneratorOptions): string;
|
|
16
14
|
}
|
|
17
|
-
type GoldenTestRunner = (input: unknown, metadata?: Record<string, unknown>) => Promise<unknown>;
|
|
18
|
-
interface GoldenTestRunResult {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
export type GoldenTestRunner = (input: unknown, metadata?: Record<string, unknown>) => Promise<unknown>;
|
|
16
|
+
export interface GoldenTestRunResult {
|
|
17
|
+
caseId: string;
|
|
18
|
+
passed: boolean;
|
|
19
|
+
durationMs: number;
|
|
20
|
+
error?: unknown;
|
|
23
21
|
}
|
|
24
|
-
declare function runGoldenTests(cases: GoldenTestCase[], runner: GoldenTestRunner): Promise<GoldenTestRunResult[]>;
|
|
25
|
-
//#endregion
|
|
26
|
-
export { GoldenTestGenerator, GoldenTestGeneratorOptions, GoldenTestRunResult, GoldenTestRunner, runGoldenTests };
|
|
22
|
+
export declare function runGoldenTests(cases: GoldenTestCase[], runner: GoldenTestRunner): Promise<GoldenTestRunResult[]>;
|
|
27
23
|
//# sourceMappingURL=golden-test-generator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"golden-test-generator.d.ts","
|
|
1
|
+
{"version":3,"file":"golden-test-generator.d.ts","sourceRoot":"","sources":["../../src/generator/golden-test-generator.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAIhE,MAAM,WAAW,0BAA0B;IACzC,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC;IAC9B,iBAAiB,CAAC,EAAE,CAAC,QAAQ,EAAE,eAAe,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC5E;AAED,qBAAa,mBAAmB;IAE5B,OAAO,CAAC,QAAQ,CAAC,iBAAiB;gBAAjB,iBAAiB,GAAE,0BAA0B,CAAC,mBAAmB,CAOnD;IAGjC,WAAW,CAAC,SAAS,EAAE,eAAe,EAAE,GAAG,cAAc,EAAE;IAc3D,QAAQ,CACN,SAAS,EAAE,eAAe,EAAE,EAC5B,OAAO,EAAE,0BAA0B,GAClC,MAAM;CAiBV;AAED,MAAM,MAAM,gBAAgB,GAAG,CAC7B,KAAK,EAAE,OAAO,EACd,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC/B,OAAO,CAAC,OAAO,CAAC,CAAC;AAEtB,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,OAAO,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,wBAAsB,cAAc,CAClC,KAAK,EAAE,cAAc,EAAE,EACvB,MAAM,EAAE,gBAAgB,GACvB,OAAO,CAAC,mBAAmB,EAAE,CAAC,CAoChC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"golden-test-generator.test.d.ts","sourceRoot":"","sources":["../../src/generator/golden-test-generator.test.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
export * from './types';
|
|
2
|
+
export * from './recorder/traffic-recorder';
|
|
3
|
+
export * from './generator/golden-test-generator';
|
|
4
|
+
export * from './generator/assertion-builder';
|
|
5
|
+
export * from './adapters/vitest-adapter';
|
|
6
|
+
export * from './adapters/jest-adapter';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mCAAmC,CAAC;AAClD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,224 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import { generateJestSuite } from "./adapters/jest-adapter.js";
|
|
5
|
-
import { GoldenTestGenerator, runGoldenTests } from "./generator/golden-test-generator.js";
|
|
1
|
+
// @bun
|
|
2
|
+
// src/recorder/traffic-recorder.ts
|
|
3
|
+
import { randomUUID } from "crypto";
|
|
6
4
|
|
|
7
|
-
|
|
5
|
+
class InMemoryTrafficStore {
|
|
6
|
+
items = [];
|
|
7
|
+
async save(snapshot) {
|
|
8
|
+
this.items.push(snapshot);
|
|
9
|
+
}
|
|
10
|
+
async list(operation) {
|
|
11
|
+
if (!operation)
|
|
12
|
+
return [...this.items];
|
|
13
|
+
return this.items.filter((item) => item.operation.name === operation);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class TrafficRecorder {
|
|
18
|
+
store;
|
|
19
|
+
sampleRate;
|
|
20
|
+
sanitize;
|
|
21
|
+
constructor(options) {
|
|
22
|
+
this.store = options.store;
|
|
23
|
+
this.sampleRate = options.sampleRate ?? 1;
|
|
24
|
+
this.sanitize = options.sanitize;
|
|
25
|
+
}
|
|
26
|
+
async record(input) {
|
|
27
|
+
if (!this.shouldSample())
|
|
28
|
+
return;
|
|
29
|
+
const snapshot = {
|
|
30
|
+
id: randomUUID(),
|
|
31
|
+
operation: input.operation,
|
|
32
|
+
input: structuredCloneSafe(input.input),
|
|
33
|
+
output: structuredCloneSafe(input.output),
|
|
34
|
+
error: input.error ? structuredCloneSafe(input.error) : undefined,
|
|
35
|
+
success: input.success,
|
|
36
|
+
timestamp: new Date,
|
|
37
|
+
durationMs: input.durationMs,
|
|
38
|
+
tenantId: input.tenantId,
|
|
39
|
+
userId: input.userId,
|
|
40
|
+
channel: input.channel,
|
|
41
|
+
metadata: input.metadata
|
|
42
|
+
};
|
|
43
|
+
const sanitized = this.sanitize ? this.sanitize(snapshot) : snapshot;
|
|
44
|
+
await this.store.save(sanitized);
|
|
45
|
+
}
|
|
46
|
+
shouldSample() {
|
|
47
|
+
if (this.sampleRate >= 1)
|
|
48
|
+
return true;
|
|
49
|
+
return Math.random() <= this.sampleRate;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function structuredCloneSafe(value) {
|
|
53
|
+
if (value == null)
|
|
54
|
+
return value ?? undefined;
|
|
55
|
+
try {
|
|
56
|
+
const clone = globalThis.structuredClone;
|
|
57
|
+
if (typeof clone === "function") {
|
|
58
|
+
return clone(value);
|
|
59
|
+
}
|
|
60
|
+
return JSON.parse(JSON.stringify(value));
|
|
61
|
+
} catch {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// src/generator/golden-test-generator.ts
|
|
66
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
67
|
+
import { performance } from "perf_hooks";
|
|
68
|
+
|
|
69
|
+
// src/generator/assertion-builder.ts
|
|
70
|
+
function buildAssertions(testCase, ctx) {
|
|
71
|
+
if (testCase.success) {
|
|
72
|
+
return [
|
|
73
|
+
`const result = await ${ctx.runnerCall};`,
|
|
74
|
+
`expect(result).toEqual(${serialize(testCase.expectedOutput ?? null)});`
|
|
75
|
+
].join(`
|
|
76
|
+
`);
|
|
77
|
+
}
|
|
78
|
+
return `await expect(${ctx.runnerCall}).rejects.toMatchObject(${serialize(testCase.expectedError ?? { message: "expected failure" })});`;
|
|
79
|
+
}
|
|
80
|
+
function serialize(value) {
|
|
81
|
+
return JSON.stringify(value, (_key, val) => {
|
|
82
|
+
if (val instanceof Date)
|
|
83
|
+
return val.toISOString();
|
|
84
|
+
if (typeof val === "undefined")
|
|
85
|
+
return null;
|
|
86
|
+
return val;
|
|
87
|
+
}, 2);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/adapters/vitest-adapter.ts
|
|
91
|
+
function generateVitestSuite(options) {
|
|
92
|
+
const caseBlocks = options.cases.map((testCase) => {
|
|
93
|
+
const inputConst = serialize(testCase.input);
|
|
94
|
+
const metadataConst = serialize(testCase.metadata ?? {});
|
|
95
|
+
const assertions = testCase.success ? [
|
|
96
|
+
`const result = await ${options.runnerFunction}(input${testCase.id}, metadata${testCase.id});`,
|
|
97
|
+
`expect(result).toEqual(${serialize(testCase.expectedOutput ?? null)});`
|
|
98
|
+
] : [
|
|
99
|
+
`await expect(${options.runnerFunction}(input${testCase.id}, metadata${testCase.id})).rejects.toMatchObject(${serialize(testCase.expectedError ?? { message: "expected failure" })});`
|
|
100
|
+
];
|
|
101
|
+
return `
|
|
102
|
+
it('${testCase.name}', async () => {
|
|
103
|
+
const input${testCase.id} = ${inputConst};
|
|
104
|
+
const metadata${testCase.id} = ${metadataConst};
|
|
105
|
+
${assertions.join(`
|
|
106
|
+
`)}
|
|
107
|
+
});`;
|
|
108
|
+
}).join(`
|
|
109
|
+
`);
|
|
110
|
+
return `
|
|
111
|
+
import { describe, it, expect } from 'bun:test';
|
|
112
|
+
import { ${options.runnerFunction} } from '${options.runnerImport}';
|
|
113
|
+
|
|
114
|
+
describe('${options.suiteName}', () => {${caseBlocks}
|
|
115
|
+
});
|
|
116
|
+
`.trim();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/adapters/jest-adapter.ts
|
|
120
|
+
function generateJestSuite(options) {
|
|
121
|
+
const caseBlocks = options.cases.map((testCase) => {
|
|
122
|
+
const inputConst = serialize(testCase.input);
|
|
123
|
+
const metadataConst = serialize(testCase.metadata ?? {});
|
|
124
|
+
const successBlock = `const result = await ${options.runnerFunction}(input${testCase.id}, metadata${testCase.id});
|
|
125
|
+
expect(result).toEqual(${serialize(testCase.expectedOutput ?? null)});`;
|
|
126
|
+
const failureBlock = `await expect(${options.runnerFunction}(input${testCase.id}, metadata${testCase.id})).rejects.toMatchObject(${serialize(testCase.expectedError ?? { message: "expected failure" })});`;
|
|
127
|
+
return `
|
|
128
|
+
test('${testCase.name}', async () => {
|
|
129
|
+
const input${testCase.id} = ${inputConst};
|
|
130
|
+
const metadata${testCase.id} = ${metadataConst};
|
|
131
|
+
${testCase.success ? successBlock : failureBlock}
|
|
132
|
+
});`;
|
|
133
|
+
}).join(`
|
|
134
|
+
`);
|
|
135
|
+
return `
|
|
136
|
+
import { ${options.runnerFunction} } from '${options.runnerImport}';
|
|
137
|
+
|
|
138
|
+
describe('${options.suiteName}', () => {${caseBlocks}
|
|
139
|
+
});
|
|
140
|
+
`.trim();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// src/generator/golden-test-generator.ts
|
|
144
|
+
class GoldenTestGenerator {
|
|
145
|
+
serializeMetadata;
|
|
146
|
+
constructor(serializeMetadata = (snapshot) => ({
|
|
147
|
+
tenantId: snapshot.tenantId,
|
|
148
|
+
userId: snapshot.userId,
|
|
149
|
+
channel: snapshot.channel
|
|
150
|
+
})) {
|
|
151
|
+
this.serializeMetadata = serializeMetadata;
|
|
152
|
+
}
|
|
153
|
+
createCases(snapshots) {
|
|
154
|
+
return snapshots.map((snapshot, index) => ({
|
|
155
|
+
id: snapshot.id ?? randomUUID2(),
|
|
156
|
+
name: snapshot.success ? `case-${index + 1}-success` : `case-${index + 1}-failure`,
|
|
157
|
+
input: snapshot.input,
|
|
158
|
+
expectedOutput: snapshot.output,
|
|
159
|
+
expectedError: snapshot.error,
|
|
160
|
+
success: snapshot.success,
|
|
161
|
+
metadata: this.serializeMetadata?.(snapshot)
|
|
162
|
+
}));
|
|
163
|
+
}
|
|
164
|
+
generate(snapshots, options) {
|
|
165
|
+
const cases = this.createCases(snapshots);
|
|
166
|
+
if (options.framework === "jest") {
|
|
167
|
+
return generateJestSuite({
|
|
168
|
+
suiteName: options.suiteName,
|
|
169
|
+
cases,
|
|
170
|
+
runnerImport: options.runnerImport,
|
|
171
|
+
runnerFunction: options.runnerFunction
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
return generateVitestSuite({
|
|
175
|
+
suiteName: options.suiteName,
|
|
176
|
+
cases,
|
|
177
|
+
runnerImport: options.runnerImport,
|
|
178
|
+
runnerFunction: options.runnerFunction
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async function runGoldenTests(cases, runner) {
|
|
183
|
+
const results = [];
|
|
184
|
+
for (const testCase of cases) {
|
|
185
|
+
const startedAt = performance.now();
|
|
186
|
+
try {
|
|
187
|
+
const output = await runner(testCase.input, testCase.metadata);
|
|
188
|
+
if (!testCase.success) {
|
|
189
|
+
results.push({
|
|
190
|
+
caseId: testCase.id,
|
|
191
|
+
passed: false,
|
|
192
|
+
durationMs: performance.now() - startedAt,
|
|
193
|
+
error: new Error("Expected failure but runner resolved")
|
|
194
|
+
});
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
const matches = JSON.stringify(output) === JSON.stringify(testCase.expectedOutput ?? null);
|
|
198
|
+
results.push({
|
|
199
|
+
caseId: testCase.id,
|
|
200
|
+
passed: matches,
|
|
201
|
+
durationMs: performance.now() - startedAt,
|
|
202
|
+
error: matches ? undefined : { expected: testCase.expectedOutput, received: output }
|
|
203
|
+
});
|
|
204
|
+
} catch (error) {
|
|
205
|
+
const durationMs = performance.now() - startedAt;
|
|
206
|
+
if (!testCase.success) {
|
|
207
|
+
results.push({ caseId: testCase.id, passed: true, durationMs });
|
|
208
|
+
} else {
|
|
209
|
+
results.push({ caseId: testCase.id, passed: false, durationMs, error });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return results;
|
|
214
|
+
}
|
|
215
|
+
export {
|
|
216
|
+
serialize,
|
|
217
|
+
runGoldenTests,
|
|
218
|
+
generateVitestSuite,
|
|
219
|
+
generateJestSuite,
|
|
220
|
+
buildAssertions,
|
|
221
|
+
TrafficRecorder,
|
|
222
|
+
InMemoryTrafficStore,
|
|
223
|
+
GoldenTestGenerator
|
|
224
|
+
};
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// src/recorder/traffic-recorder.ts
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
|
|
4
|
+
class InMemoryTrafficStore {
|
|
5
|
+
items = [];
|
|
6
|
+
async save(snapshot) {
|
|
7
|
+
this.items.push(snapshot);
|
|
8
|
+
}
|
|
9
|
+
async list(operation) {
|
|
10
|
+
if (!operation)
|
|
11
|
+
return [...this.items];
|
|
12
|
+
return this.items.filter((item) => item.operation.name === operation);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
class TrafficRecorder {
|
|
17
|
+
store;
|
|
18
|
+
sampleRate;
|
|
19
|
+
sanitize;
|
|
20
|
+
constructor(options) {
|
|
21
|
+
this.store = options.store;
|
|
22
|
+
this.sampleRate = options.sampleRate ?? 1;
|
|
23
|
+
this.sanitize = options.sanitize;
|
|
24
|
+
}
|
|
25
|
+
async record(input) {
|
|
26
|
+
if (!this.shouldSample())
|
|
27
|
+
return;
|
|
28
|
+
const snapshot = {
|
|
29
|
+
id: randomUUID(),
|
|
30
|
+
operation: input.operation,
|
|
31
|
+
input: structuredCloneSafe(input.input),
|
|
32
|
+
output: structuredCloneSafe(input.output),
|
|
33
|
+
error: input.error ? structuredCloneSafe(input.error) : undefined,
|
|
34
|
+
success: input.success,
|
|
35
|
+
timestamp: new Date,
|
|
36
|
+
durationMs: input.durationMs,
|
|
37
|
+
tenantId: input.tenantId,
|
|
38
|
+
userId: input.userId,
|
|
39
|
+
channel: input.channel,
|
|
40
|
+
metadata: input.metadata
|
|
41
|
+
};
|
|
42
|
+
const sanitized = this.sanitize ? this.sanitize(snapshot) : snapshot;
|
|
43
|
+
await this.store.save(sanitized);
|
|
44
|
+
}
|
|
45
|
+
shouldSample() {
|
|
46
|
+
if (this.sampleRate >= 1)
|
|
47
|
+
return true;
|
|
48
|
+
return Math.random() <= this.sampleRate;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function structuredCloneSafe(value) {
|
|
52
|
+
if (value == null)
|
|
53
|
+
return value ?? undefined;
|
|
54
|
+
try {
|
|
55
|
+
const clone = globalThis.structuredClone;
|
|
56
|
+
if (typeof clone === "function") {
|
|
57
|
+
return clone(value);
|
|
58
|
+
}
|
|
59
|
+
return JSON.parse(JSON.stringify(value));
|
|
60
|
+
} catch {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// src/generator/golden-test-generator.ts
|
|
65
|
+
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
66
|
+
import { performance } from "node:perf_hooks";
|
|
67
|
+
|
|
68
|
+
// src/generator/assertion-builder.ts
|
|
69
|
+
function buildAssertions(testCase, ctx) {
|
|
70
|
+
if (testCase.success) {
|
|
71
|
+
return [
|
|
72
|
+
`const result = await ${ctx.runnerCall};`,
|
|
73
|
+
`expect(result).toEqual(${serialize(testCase.expectedOutput ?? null)});`
|
|
74
|
+
].join(`
|
|
75
|
+
`);
|
|
76
|
+
}
|
|
77
|
+
return `await expect(${ctx.runnerCall}).rejects.toMatchObject(${serialize(testCase.expectedError ?? { message: "expected failure" })});`;
|
|
78
|
+
}
|
|
79
|
+
function serialize(value) {
|
|
80
|
+
return JSON.stringify(value, (_key, val) => {
|
|
81
|
+
if (val instanceof Date)
|
|
82
|
+
return val.toISOString();
|
|
83
|
+
if (typeof val === "undefined")
|
|
84
|
+
return null;
|
|
85
|
+
return val;
|
|
86
|
+
}, 2);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/adapters/vitest-adapter.ts
|
|
90
|
+
function generateVitestSuite(options) {
|
|
91
|
+
const caseBlocks = options.cases.map((testCase) => {
|
|
92
|
+
const inputConst = serialize(testCase.input);
|
|
93
|
+
const metadataConst = serialize(testCase.metadata ?? {});
|
|
94
|
+
const assertions = testCase.success ? [
|
|
95
|
+
`const result = await ${options.runnerFunction}(input${testCase.id}, metadata${testCase.id});`,
|
|
96
|
+
`expect(result).toEqual(${serialize(testCase.expectedOutput ?? null)});`
|
|
97
|
+
] : [
|
|
98
|
+
`await expect(${options.runnerFunction}(input${testCase.id}, metadata${testCase.id})).rejects.toMatchObject(${serialize(testCase.expectedError ?? { message: "expected failure" })});`
|
|
99
|
+
];
|
|
100
|
+
return `
|
|
101
|
+
it('${testCase.name}', async () => {
|
|
102
|
+
const input${testCase.id} = ${inputConst};
|
|
103
|
+
const metadata${testCase.id} = ${metadataConst};
|
|
104
|
+
${assertions.join(`
|
|
105
|
+
`)}
|
|
106
|
+
});`;
|
|
107
|
+
}).join(`
|
|
108
|
+
`);
|
|
109
|
+
return `
|
|
110
|
+
import { describe, it, expect } from 'bun:test';
|
|
111
|
+
import { ${options.runnerFunction} } from '${options.runnerImport}';
|
|
112
|
+
|
|
113
|
+
describe('${options.suiteName}', () => {${caseBlocks}
|
|
114
|
+
});
|
|
115
|
+
`.trim();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/adapters/jest-adapter.ts
|
|
119
|
+
function generateJestSuite(options) {
|
|
120
|
+
const caseBlocks = options.cases.map((testCase) => {
|
|
121
|
+
const inputConst = serialize(testCase.input);
|
|
122
|
+
const metadataConst = serialize(testCase.metadata ?? {});
|
|
123
|
+
const successBlock = `const result = await ${options.runnerFunction}(input${testCase.id}, metadata${testCase.id});
|
|
124
|
+
expect(result).toEqual(${serialize(testCase.expectedOutput ?? null)});`;
|
|
125
|
+
const failureBlock = `await expect(${options.runnerFunction}(input${testCase.id}, metadata${testCase.id})).rejects.toMatchObject(${serialize(testCase.expectedError ?? { message: "expected failure" })});`;
|
|
126
|
+
return `
|
|
127
|
+
test('${testCase.name}', async () => {
|
|
128
|
+
const input${testCase.id} = ${inputConst};
|
|
129
|
+
const metadata${testCase.id} = ${metadataConst};
|
|
130
|
+
${testCase.success ? successBlock : failureBlock}
|
|
131
|
+
});`;
|
|
132
|
+
}).join(`
|
|
133
|
+
`);
|
|
134
|
+
return `
|
|
135
|
+
import { ${options.runnerFunction} } from '${options.runnerImport}';
|
|
136
|
+
|
|
137
|
+
describe('${options.suiteName}', () => {${caseBlocks}
|
|
138
|
+
});
|
|
139
|
+
`.trim();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/generator/golden-test-generator.ts
|
|
143
|
+
class GoldenTestGenerator {
|
|
144
|
+
serializeMetadata;
|
|
145
|
+
constructor(serializeMetadata = (snapshot) => ({
|
|
146
|
+
tenantId: snapshot.tenantId,
|
|
147
|
+
userId: snapshot.userId,
|
|
148
|
+
channel: snapshot.channel
|
|
149
|
+
})) {
|
|
150
|
+
this.serializeMetadata = serializeMetadata;
|
|
151
|
+
}
|
|
152
|
+
createCases(snapshots) {
|
|
153
|
+
return snapshots.map((snapshot, index) => ({
|
|
154
|
+
id: snapshot.id ?? randomUUID2(),
|
|
155
|
+
name: snapshot.success ? `case-${index + 1}-success` : `case-${index + 1}-failure`,
|
|
156
|
+
input: snapshot.input,
|
|
157
|
+
expectedOutput: snapshot.output,
|
|
158
|
+
expectedError: snapshot.error,
|
|
159
|
+
success: snapshot.success,
|
|
160
|
+
metadata: this.serializeMetadata?.(snapshot)
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
generate(snapshots, options) {
|
|
164
|
+
const cases = this.createCases(snapshots);
|
|
165
|
+
if (options.framework === "jest") {
|
|
166
|
+
return generateJestSuite({
|
|
167
|
+
suiteName: options.suiteName,
|
|
168
|
+
cases,
|
|
169
|
+
runnerImport: options.runnerImport,
|
|
170
|
+
runnerFunction: options.runnerFunction
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return generateVitestSuite({
|
|
174
|
+
suiteName: options.suiteName,
|
|
175
|
+
cases,
|
|
176
|
+
runnerImport: options.runnerImport,
|
|
177
|
+
runnerFunction: options.runnerFunction
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
async function runGoldenTests(cases, runner) {
|
|
182
|
+
const results = [];
|
|
183
|
+
for (const testCase of cases) {
|
|
184
|
+
const startedAt = performance.now();
|
|
185
|
+
try {
|
|
186
|
+
const output = await runner(testCase.input, testCase.metadata);
|
|
187
|
+
if (!testCase.success) {
|
|
188
|
+
results.push({
|
|
189
|
+
caseId: testCase.id,
|
|
190
|
+
passed: false,
|
|
191
|
+
durationMs: performance.now() - startedAt,
|
|
192
|
+
error: new Error("Expected failure but runner resolved")
|
|
193
|
+
});
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
const matches = JSON.stringify(output) === JSON.stringify(testCase.expectedOutput ?? null);
|
|
197
|
+
results.push({
|
|
198
|
+
caseId: testCase.id,
|
|
199
|
+
passed: matches,
|
|
200
|
+
durationMs: performance.now() - startedAt,
|
|
201
|
+
error: matches ? undefined : { expected: testCase.expectedOutput, received: output }
|
|
202
|
+
});
|
|
203
|
+
} catch (error) {
|
|
204
|
+
const durationMs = performance.now() - startedAt;
|
|
205
|
+
if (!testCase.success) {
|
|
206
|
+
results.push({ caseId: testCase.id, passed: true, durationMs });
|
|
207
|
+
} else {
|
|
208
|
+
results.push({ caseId: testCase.id, passed: false, durationMs, error });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return results;
|
|
213
|
+
}
|
|
214
|
+
export {
|
|
215
|
+
serialize,
|
|
216
|
+
runGoldenTests,
|
|
217
|
+
generateVitestSuite,
|
|
218
|
+
generateJestSuite,
|
|
219
|
+
buildAssertions,
|
|
220
|
+
TrafficRecorder,
|
|
221
|
+
InMemoryTrafficStore,
|
|
222
|
+
GoldenTestGenerator
|
|
223
|
+
};
|
|
@@ -1,40 +1,36 @@
|
|
|
1
|
-
import { TrafficSnapshot } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
save(snapshot: TrafficSnapshot): Promise<void>;
|
|
6
|
-
list(operation?: TrafficSnapshot['operation']['name']): Promise<TrafficSnapshot[]>;
|
|
1
|
+
import type { TrafficSnapshot } from '../types';
|
|
2
|
+
export interface TrafficStore {
|
|
3
|
+
save(snapshot: TrafficSnapshot): Promise<void>;
|
|
4
|
+
list(operation?: TrafficSnapshot['operation']['name']): Promise<TrafficSnapshot[]>;
|
|
7
5
|
}
|
|
8
|
-
declare class InMemoryTrafficStore implements TrafficStore {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
export declare class InMemoryTrafficStore implements TrafficStore {
|
|
7
|
+
private readonly items;
|
|
8
|
+
save(snapshot: TrafficSnapshot): Promise<void>;
|
|
9
|
+
list(operation?: string): Promise<TrafficSnapshot[]>;
|
|
12
10
|
}
|
|
13
|
-
interface TrafficRecorderOptions {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
export interface TrafficRecorderOptions {
|
|
12
|
+
store: TrafficStore;
|
|
13
|
+
sampleRate?: number;
|
|
14
|
+
sanitize?: (snapshot: TrafficSnapshot) => TrafficSnapshot;
|
|
17
15
|
}
|
|
18
|
-
interface RecordOperationInput {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
16
|
+
export interface RecordOperationInput {
|
|
17
|
+
operation: TrafficSnapshot['operation'];
|
|
18
|
+
input: unknown;
|
|
19
|
+
output?: unknown;
|
|
20
|
+
error?: TrafficSnapshot['error'];
|
|
21
|
+
success: boolean;
|
|
22
|
+
durationMs?: number;
|
|
23
|
+
tenantId?: string;
|
|
24
|
+
userId?: string;
|
|
25
|
+
channel?: string;
|
|
26
|
+
metadata?: Record<string, unknown>;
|
|
29
27
|
}
|
|
30
|
-
declare class TrafficRecorder {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
28
|
+
export declare class TrafficRecorder {
|
|
29
|
+
private readonly store;
|
|
30
|
+
private readonly sampleRate;
|
|
31
|
+
private readonly sanitize?;
|
|
32
|
+
constructor(options: TrafficRecorderOptions);
|
|
33
|
+
record(input: RecordOperationInput): Promise<void>;
|
|
34
|
+
private shouldSample;
|
|
37
35
|
}
|
|
38
|
-
//#endregion
|
|
39
|
-
export { InMemoryTrafficStore, RecordOperationInput, TrafficRecorder, TrafficRecorderOptions, TrafficStore };
|
|
40
36
|
//# sourceMappingURL=traffic-recorder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"traffic-recorder.d.ts","
|
|
1
|
+
{"version":3,"file":"traffic-recorder.d.ts","sourceRoot":"","sources":["../../src/recorder/traffic-recorder.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEhD,MAAM,WAAW,YAAY;IAC3B,IAAI,CAAC,QAAQ,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/C,IAAI,CACF,SAAS,CAAC,EAAE,eAAe,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,GAC/C,OAAO,CAAC,eAAe,EAAE,CAAC,CAAC;CAC/B;AAED,qBAAa,oBAAqB,YAAW,YAAY;IACvD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAyB;IAEzC,IAAI,CAAC,QAAQ,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC;IAI9C,IAAI,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;CAI3D;AAED,MAAM,WAAW,sBAAsB;IACrC,KAAK,EAAE,YAAY,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,CAAC,QAAQ,EAAE,eAAe,KAAK,eAAe,CAAC;CAC3D;AAED,MAAM,WAAW,oBAAoB;IACnC,SAAS,EAAE,eAAe,CAAC,WAAW,CAAC,CAAC;IACxC,KAAK,EAAE,OAAO,CAAC;IACf,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,KAAK,CAAC,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC;IACjC,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,qBAAa,eAAe;IAC1B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAe;IACrC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAiD;gBAE/D,OAAO,EAAE,sBAAsB;IAMrC,MAAM,CAAC,KAAK,EAAE,oBAAoB;IAoBxC,OAAO,CAAC,YAAY;CAIrB"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,43 +1,39 @@
|
|
|
1
|
-
import { OperationSpec, ResourceRefDescriptor } from
|
|
2
|
-
import { AnySchemaModel } from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
channel?: string;
|
|
25
|
-
metadata?: Record<string, unknown>;
|
|
1
|
+
import type { OperationSpec, ResourceRefDescriptor } from '@contractspec/lib.contracts';
|
|
2
|
+
import type { AnySchemaModel } from '@contractspec/lib.schema';
|
|
3
|
+
export interface TrafficSnapshot {
|
|
4
|
+
id: string;
|
|
5
|
+
operation: {
|
|
6
|
+
name: string;
|
|
7
|
+
version: string;
|
|
8
|
+
};
|
|
9
|
+
input: unknown;
|
|
10
|
+
output?: unknown;
|
|
11
|
+
error?: {
|
|
12
|
+
name?: string;
|
|
13
|
+
message?: string;
|
|
14
|
+
stack?: string;
|
|
15
|
+
code?: string;
|
|
16
|
+
};
|
|
17
|
+
success: boolean;
|
|
18
|
+
timestamp: Date;
|
|
19
|
+
durationMs?: number;
|
|
20
|
+
tenantId?: string;
|
|
21
|
+
userId?: string;
|
|
22
|
+
channel?: string;
|
|
23
|
+
metadata?: Record<string, unknown>;
|
|
26
24
|
}
|
|
27
|
-
interface GoldenTestCase {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
25
|
+
export interface GoldenTestCase {
|
|
26
|
+
id: string;
|
|
27
|
+
name: string;
|
|
28
|
+
input: unknown;
|
|
29
|
+
expectedOutput?: unknown;
|
|
30
|
+
expectedError?: {
|
|
31
|
+
name?: string;
|
|
32
|
+
message?: string;
|
|
33
|
+
code?: string;
|
|
34
|
+
};
|
|
35
|
+
success: boolean;
|
|
36
|
+
metadata?: Record<string, unknown>;
|
|
39
37
|
}
|
|
40
|
-
type RuntimeContract = OperationSpec<AnySchemaModel, AnySchemaModel | ResourceRefDescriptor<boolean>>;
|
|
41
|
-
//#endregion
|
|
42
|
-
export { GoldenTestCase, RuntimeContract, TrafficSnapshot };
|
|
38
|
+
export type RuntimeContract = OperationSpec<AnySchemaModel, AnySchemaModel | ResourceRefDescriptor<boolean>>;
|
|
43
39
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,qBAAqB,EACtB,MAAM,6BAA6B,CAAC;AACrC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAE/D,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IAC7C,KAAK,EAAE,OAAO,CAAC;IACf,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,KAAK,CAAC,EAAE;QACN,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,IAAI,CAAC,EAAE,MAAM,CAAC;KACf,CAAC;IACF,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,IAAI,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,OAAO,CAAC;IACf,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,aAAa,CAAC,EAAE;QACd,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,IAAI,CAAC,EAAE,MAAM,CAAC;KACf,CAAC;IACF,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,MAAM,MAAM,eAAe,GAAG,aAAa,CACzC,cAAc,EACd,cAAc,GAAG,qBAAqB,CAAC,OAAO,CAAC,CAChD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contractspec/lib.testing",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.58.0",
|
|
4
4
|
"description": "Contract-aware testing utilities and runners",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"contractspec",
|
|
@@ -17,35 +17,39 @@
|
|
|
17
17
|
"scripts": {
|
|
18
18
|
"publish:pkg": "bun publish --tolerate-republish --ignore-scripts --verbose",
|
|
19
19
|
"publish:pkg:canary": "bun publish:pkg --tag canary",
|
|
20
|
-
"build": "bun build:
|
|
21
|
-
"build:bundle": "
|
|
22
|
-
"build:types": "
|
|
23
|
-
"dev": "bun
|
|
20
|
+
"build": "bun run prebuild && bun run build:bundle && bun run build:types",
|
|
21
|
+
"build:bundle": "contractspec-bun-build transpile",
|
|
22
|
+
"build:types": "contractspec-bun-build types",
|
|
23
|
+
"dev": "contractspec-bun-build dev",
|
|
24
24
|
"clean": "rimraf dist .turbo",
|
|
25
25
|
"lint": "bun lint:fix",
|
|
26
26
|
"lint:fix": "eslint src --fix",
|
|
27
27
|
"lint:check": "eslint src",
|
|
28
|
-
"test": "bun test"
|
|
28
|
+
"test": "bun test",
|
|
29
|
+
"prebuild": "contractspec-bun-build prebuild",
|
|
30
|
+
"typecheck": "tsc --noEmit"
|
|
29
31
|
},
|
|
30
32
|
"dependencies": {
|
|
31
|
-
"@contractspec/lib.schema": "1.
|
|
32
|
-
"@contractspec/lib.contracts": "1.
|
|
33
|
+
"@contractspec/lib.schema": "1.58.0",
|
|
34
|
+
"@contractspec/lib.contracts": "1.58.0"
|
|
33
35
|
},
|
|
34
36
|
"devDependencies": {
|
|
35
|
-
"@contractspec/tool.
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"typescript": "^5.9.3"
|
|
37
|
+
"@contractspec/tool.typescript": "1.58.0",
|
|
38
|
+
"typescript": "^5.9.3",
|
|
39
|
+
"@contractspec/tool.bun": "1.57.0"
|
|
39
40
|
},
|
|
40
41
|
"exports": {
|
|
41
|
-
".": "./
|
|
42
|
-
"./*": "./*"
|
|
42
|
+
".": "./src/index.ts"
|
|
43
43
|
},
|
|
44
44
|
"publishConfig": {
|
|
45
45
|
"access": "public",
|
|
46
46
|
"exports": {
|
|
47
|
-
".":
|
|
48
|
-
|
|
47
|
+
".": {
|
|
48
|
+
"types": "./dist/index.d.ts",
|
|
49
|
+
"bun": "./dist/index.js",
|
|
50
|
+
"node": "./dist/node/index.mjs",
|
|
51
|
+
"default": "./dist/index.js"
|
|
52
|
+
}
|
|
49
53
|
},
|
|
50
54
|
"registry": "https://registry.npmjs.org/"
|
|
51
55
|
},
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { serialize } from "../generator/assertion-builder.js";
|
|
2
|
-
|
|
3
|
-
//#region src/adapters/jest-adapter.ts
|
|
4
|
-
function generateJestSuite(options) {
|
|
5
|
-
const caseBlocks = options.cases.map((testCase) => {
|
|
6
|
-
const inputConst = serialize(testCase.input);
|
|
7
|
-
const metadataConst = serialize(testCase.metadata ?? {});
|
|
8
|
-
const successBlock = `const result = await ${options.runnerFunction}(input${testCase.id}, metadata${testCase.id});
|
|
9
|
-
expect(result).toEqual(${serialize(testCase.expectedOutput ?? null)});`;
|
|
10
|
-
const failureBlock = `await expect(${options.runnerFunction}(input${testCase.id}, metadata${testCase.id})).rejects.toMatchObject(${serialize(testCase.expectedError ?? { message: "expected failure" })});`;
|
|
11
|
-
return `
|
|
12
|
-
test('${testCase.name}', async () => {
|
|
13
|
-
const input${testCase.id} = ${inputConst};
|
|
14
|
-
const metadata${testCase.id} = ${metadataConst};
|
|
15
|
-
${testCase.success ? successBlock : failureBlock}
|
|
16
|
-
});`;
|
|
17
|
-
}).join("\n");
|
|
18
|
-
return `
|
|
19
|
-
import { ${options.runnerFunction} } from '${options.runnerImport}';
|
|
20
|
-
|
|
21
|
-
describe('${options.suiteName}', () => {${caseBlocks}
|
|
22
|
-
});
|
|
23
|
-
`.trim();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
//#endregion
|
|
27
|
-
export { generateJestSuite };
|
|
28
|
-
//# sourceMappingURL=jest-adapter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"jest-adapter.js","names":[],"sources":["../../src/adapters/jest-adapter.ts"],"sourcesContent":["import type { GoldenTestCase } from '../types';\nimport { serialize } from '../generator/assertion-builder';\n\nexport interface JestAdapterOptions {\n suiteName: string;\n cases: GoldenTestCase[];\n runnerImport: string;\n runnerFunction: string;\n}\n\nexport function generateJestSuite(options: JestAdapterOptions) {\n const caseBlocks = options.cases\n .map((testCase) => {\n const inputConst = serialize(testCase.input);\n const metadataConst = serialize(testCase.metadata ?? {});\n const successBlock = `const result = await ${options.runnerFunction}(input${testCase.id}, metadata${testCase.id});\n expect(result).toEqual(${serialize(testCase.expectedOutput ?? null)});`;\n const failureBlock = `await expect(${options.runnerFunction}(input${testCase.id}, metadata${testCase.id})).rejects.toMatchObject(${serialize(\n testCase.expectedError ?? { message: 'expected failure' }\n )});`;\n\n return `\n test('${testCase.name}', async () => {\n const input${testCase.id} = ${inputConst};\n const metadata${testCase.id} = ${metadataConst};\n ${testCase.success ? successBlock : failureBlock}\n });`;\n })\n .join('\\n');\n\n return `\nimport { ${options.runnerFunction} } from '${options.runnerImport}';\n\ndescribe('${options.suiteName}', () => {${caseBlocks}\n});\n`.trim();\n}\n"],"mappings":";;;AAUA,SAAgB,kBAAkB,SAA6B;CAC7D,MAAM,aAAa,QAAQ,MACxB,KAAK,aAAa;EACjB,MAAM,aAAa,UAAU,SAAS,MAAM;EAC5C,MAAM,gBAAgB,UAAU,SAAS,YAAY,EAAE,CAAC;EACxD,MAAM,eAAe,wBAAwB,QAAQ,eAAe,QAAQ,SAAS,GAAG,YAAY,SAAS,GAAG;6BACzF,UAAU,SAAS,kBAAkB,KAAK,CAAC;EAClE,MAAM,eAAe,gBAAgB,QAAQ,eAAe,QAAQ,SAAS,GAAG,YAAY,SAAS,GAAG,2BAA2B,UACjI,SAAS,iBAAiB,EAAE,SAAS,oBAAoB,CAC1D,CAAC;AAEF,SAAO;UACH,SAAS,KAAK;iBACP,SAAS,GAAG,KAAK,WAAW;oBACzB,SAAS,GAAG,KAAK,cAAc;MAC7C,SAAS,UAAU,eAAe,aAAa;;GAE/C,CACD,KAAK,KAAK;AAEb,QAAO;WACE,QAAQ,eAAe,WAAW,QAAQ,aAAa;;YAEtD,QAAQ,UAAU,YAAY,WAAW;;EAEnD,MAAM"}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { serialize } from "../generator/assertion-builder.js";
|
|
2
|
-
|
|
3
|
-
//#region src/adapters/vitest-adapter.ts
|
|
4
|
-
function generateVitestSuite(options) {
|
|
5
|
-
const caseBlocks = options.cases.map((testCase) => {
|
|
6
|
-
const inputConst = serialize(testCase.input);
|
|
7
|
-
const metadataConst = serialize(testCase.metadata ?? {});
|
|
8
|
-
const assertions = testCase.success ? [`const result = await ${options.runnerFunction}(input${testCase.id}, metadata${testCase.id});`, `expect(result).toEqual(${serialize(testCase.expectedOutput ?? null)});`] : [`await expect(${options.runnerFunction}(input${testCase.id}, metadata${testCase.id})).rejects.toMatchObject(${serialize(testCase.expectedError ?? { message: "expected failure" })});`];
|
|
9
|
-
return `
|
|
10
|
-
it('${testCase.name}', async () => {
|
|
11
|
-
const input${testCase.id} = ${inputConst};
|
|
12
|
-
const metadata${testCase.id} = ${metadataConst};
|
|
13
|
-
${assertions.join("\n ")}
|
|
14
|
-
});`;
|
|
15
|
-
}).join("\n");
|
|
16
|
-
return `
|
|
17
|
-
import { describe, it, expect } from 'bun:test';
|
|
18
|
-
import { ${options.runnerFunction} } from '${options.runnerImport}';
|
|
19
|
-
|
|
20
|
-
describe('${options.suiteName}', () => {${caseBlocks}
|
|
21
|
-
});
|
|
22
|
-
`.trim();
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
//#endregion
|
|
26
|
-
export { generateVitestSuite };
|
|
27
|
-
//# sourceMappingURL=vitest-adapter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"vitest-adapter.js","names":[],"sources":["../../src/adapters/vitest-adapter.ts"],"sourcesContent":["import type { GoldenTestCase } from '../types';\nimport { serialize } from '../generator/assertion-builder';\n\nexport interface VitestAdapterOptions {\n suiteName: string;\n cases: GoldenTestCase[];\n runnerImport: string;\n runnerFunction: string;\n}\n\nexport function generateVitestSuite(options: VitestAdapterOptions) {\n const caseBlocks = options.cases\n .map((testCase) => {\n const inputConst = serialize(testCase.input);\n const metadataConst = serialize(testCase.metadata ?? {});\n const assertions = testCase.success\n ? [\n `const result = await ${options.runnerFunction}(input${testCase.id}, metadata${testCase.id});`,\n `expect(result).toEqual(${serialize(testCase.expectedOutput ?? null)});`,\n ]\n : [\n `await expect(${options.runnerFunction}(input${testCase.id}, metadata${testCase.id})).rejects.toMatchObject(${serialize(testCase.expectedError ?? { message: 'expected failure' })});`,\n ];\n\n return `\n it('${testCase.name}', async () => {\n const input${testCase.id} = ${inputConst};\n const metadata${testCase.id} = ${metadataConst};\n ${assertions.join('\\n ')}\n });`;\n })\n .join('\\n');\n\n return `\nimport { describe, it, expect } from 'bun:test';\nimport { ${options.runnerFunction} } from '${options.runnerImport}';\n\ndescribe('${options.suiteName}', () => {${caseBlocks}\n});\n`.trim();\n}\n"],"mappings":";;;AAUA,SAAgB,oBAAoB,SAA+B;CACjE,MAAM,aAAa,QAAQ,MACxB,KAAK,aAAa;EACjB,MAAM,aAAa,UAAU,SAAS,MAAM;EAC5C,MAAM,gBAAgB,UAAU,SAAS,YAAY,EAAE,CAAC;EACxD,MAAM,aAAa,SAAS,UACxB,CACE,wBAAwB,QAAQ,eAAe,QAAQ,SAAS,GAAG,YAAY,SAAS,GAAG,KAC3F,0BAA0B,UAAU,SAAS,kBAAkB,KAAK,CAAC,IACtE,GACD,CACE,gBAAgB,QAAQ,eAAe,QAAQ,SAAS,GAAG,YAAY,SAAS,GAAG,2BAA2B,UAAU,SAAS,iBAAiB,EAAE,SAAS,oBAAoB,CAAC,CAAC,IACpL;AAEL,SAAO;QACL,SAAS,KAAK;iBACL,SAAS,GAAG,KAAK,WAAW;oBACzB,SAAS,GAAG,KAAK,cAAc;MAC7C,WAAW,KAAK,SAAS,CAAC;;GAE1B,CACD,KAAK,KAAK;AAEb,QAAO;;WAEE,QAAQ,eAAe,WAAW,QAAQ,aAAa;;YAEtD,QAAQ,UAAU,YAAY,WAAW;;EAEnD,MAAM"}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
//#region src/generator/assertion-builder.ts
|
|
2
|
-
function buildAssertions(testCase, ctx) {
|
|
3
|
-
if (testCase.success) return [`const result = await ${ctx.runnerCall};`, `expect(result).toEqual(${serialize(testCase.expectedOutput ?? null)});`].join("\n ");
|
|
4
|
-
return `await expect(${ctx.runnerCall}).rejects.toMatchObject(${serialize(testCase.expectedError ?? { message: "expected failure" })});`;
|
|
5
|
-
}
|
|
6
|
-
function serialize(value) {
|
|
7
|
-
return JSON.stringify(value, (_key, val) => {
|
|
8
|
-
if (val instanceof Date) return val.toISOString();
|
|
9
|
-
if (typeof val === "undefined") return null;
|
|
10
|
-
return val;
|
|
11
|
-
}, 2);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
//#endregion
|
|
15
|
-
export { buildAssertions, serialize };
|
|
16
|
-
//# sourceMappingURL=assertion-builder.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"assertion-builder.js","names":[],"sources":["../../src/generator/assertion-builder.ts"],"sourcesContent":["import type { GoldenTestCase } from '../types';\n\nexport interface AssertionContext {\n runnerCall: string;\n caseRef: string;\n}\n\nexport function buildAssertions(\n testCase: GoldenTestCase,\n ctx: AssertionContext\n) {\n if (testCase.success) {\n return [\n `const result = await ${ctx.runnerCall};`,\n `expect(result).toEqual(${serialize(testCase.expectedOutput ?? null)});`,\n ].join('\\n ');\n }\n return `await expect(${ctx.runnerCall}).rejects.toMatchObject(${serialize(\n testCase.expectedError ?? { message: 'expected failure' }\n )});`;\n}\n\nexport function serialize(value: unknown) {\n return JSON.stringify(\n value,\n (_key, val) => {\n if (val instanceof Date) return val.toISOString();\n if (typeof val === 'undefined') return null;\n return val;\n },\n 2\n );\n}\n"],"mappings":";AAOA,SAAgB,gBACd,UACA,KACA;AACA,KAAI,SAAS,QACX,QAAO,CACL,wBAAwB,IAAI,WAAW,IACvC,0BAA0B,UAAU,SAAS,kBAAkB,KAAK,CAAC,IACtE,CAAC,KAAK,WAAW;AAEpB,QAAO,gBAAgB,IAAI,WAAW,0BAA0B,UAC9D,SAAS,iBAAiB,EAAE,SAAS,oBAAoB,CAC1D,CAAC;;AAGJ,SAAgB,UAAU,OAAgB;AACxC,QAAO,KAAK,UACV,QACC,MAAM,QAAQ;AACb,MAAI,eAAe,KAAM,QAAO,IAAI,aAAa;AACjD,MAAI,OAAO,QAAQ,YAAa,QAAO;AACvC,SAAO;IAET,EACD"}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { generateVitestSuite } from "../adapters/vitest-adapter.js";
|
|
2
|
-
import { generateJestSuite } from "../adapters/jest-adapter.js";
|
|
3
|
-
import { randomUUID } from "node:crypto";
|
|
4
|
-
import { performance } from "node:perf_hooks";
|
|
5
|
-
|
|
6
|
-
//#region src/generator/golden-test-generator.ts
|
|
7
|
-
var GoldenTestGenerator = class {
|
|
8
|
-
constructor(serializeMetadata = (snapshot) => ({
|
|
9
|
-
tenantId: snapshot.tenantId,
|
|
10
|
-
userId: snapshot.userId,
|
|
11
|
-
channel: snapshot.channel
|
|
12
|
-
})) {
|
|
13
|
-
this.serializeMetadata = serializeMetadata;
|
|
14
|
-
}
|
|
15
|
-
createCases(snapshots) {
|
|
16
|
-
return snapshots.map((snapshot, index) => ({
|
|
17
|
-
id: snapshot.id ?? randomUUID(),
|
|
18
|
-
name: snapshot.success ? `case-${index + 1}-success` : `case-${index + 1}-failure`,
|
|
19
|
-
input: snapshot.input,
|
|
20
|
-
expectedOutput: snapshot.output,
|
|
21
|
-
expectedError: snapshot.error,
|
|
22
|
-
success: snapshot.success,
|
|
23
|
-
metadata: this.serializeMetadata?.(snapshot)
|
|
24
|
-
}));
|
|
25
|
-
}
|
|
26
|
-
generate(snapshots, options) {
|
|
27
|
-
const cases = this.createCases(snapshots);
|
|
28
|
-
if (options.framework === "jest") return generateJestSuite({
|
|
29
|
-
suiteName: options.suiteName,
|
|
30
|
-
cases,
|
|
31
|
-
runnerImport: options.runnerImport,
|
|
32
|
-
runnerFunction: options.runnerFunction
|
|
33
|
-
});
|
|
34
|
-
return generateVitestSuite({
|
|
35
|
-
suiteName: options.suiteName,
|
|
36
|
-
cases,
|
|
37
|
-
runnerImport: options.runnerImport,
|
|
38
|
-
runnerFunction: options.runnerFunction
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
async function runGoldenTests(cases, runner) {
|
|
43
|
-
const results = [];
|
|
44
|
-
for (const testCase of cases) {
|
|
45
|
-
const startedAt = performance.now();
|
|
46
|
-
try {
|
|
47
|
-
const output = await runner(testCase.input, testCase.metadata);
|
|
48
|
-
if (!testCase.success) {
|
|
49
|
-
results.push({
|
|
50
|
-
caseId: testCase.id,
|
|
51
|
-
passed: false,
|
|
52
|
-
durationMs: performance.now() - startedAt,
|
|
53
|
-
error: /* @__PURE__ */ new Error("Expected failure but runner resolved")
|
|
54
|
-
});
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
const matches = JSON.stringify(output) === JSON.stringify(testCase.expectedOutput ?? null);
|
|
58
|
-
results.push({
|
|
59
|
-
caseId: testCase.id,
|
|
60
|
-
passed: matches,
|
|
61
|
-
durationMs: performance.now() - startedAt,
|
|
62
|
-
error: matches ? void 0 : {
|
|
63
|
-
expected: testCase.expectedOutput,
|
|
64
|
-
received: output
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
} catch (error) {
|
|
68
|
-
const durationMs = performance.now() - startedAt;
|
|
69
|
-
if (!testCase.success) results.push({
|
|
70
|
-
caseId: testCase.id,
|
|
71
|
-
passed: true,
|
|
72
|
-
durationMs
|
|
73
|
-
});
|
|
74
|
-
else results.push({
|
|
75
|
-
caseId: testCase.id,
|
|
76
|
-
passed: false,
|
|
77
|
-
durationMs,
|
|
78
|
-
error
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return results;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
//#endregion
|
|
86
|
-
export { GoldenTestGenerator, runGoldenTests };
|
|
87
|
-
//# sourceMappingURL=golden-test-generator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"golden-test-generator.js","names":[],"sources":["../../src/generator/golden-test-generator.ts"],"sourcesContent":["import { randomUUID } from 'node:crypto';\nimport { performance } from 'node:perf_hooks';\nimport type { GoldenTestCase, TrafficSnapshot } from '../types';\nimport { generateVitestSuite } from '../adapters/vitest-adapter';\nimport { generateJestSuite } from '../adapters/jest-adapter';\n\nexport interface GoldenTestGeneratorOptions {\n suiteName: string;\n runnerImport: string;\n runnerFunction: string;\n framework?: 'vitest' | 'jest';\n serializeMetadata?: (snapshot: TrafficSnapshot) => Record<string, unknown>;\n}\n\nexport class GoldenTestGenerator {\n constructor(\n private readonly serializeMetadata: GoldenTestGeneratorOptions['serializeMetadata'] = (\n snapshot\n ) =>\n ({\n tenantId: snapshot.tenantId,\n userId: snapshot.userId,\n channel: snapshot.channel,\n }) as Record<string, unknown>\n ) {}\n\n createCases(snapshots: TrafficSnapshot[]): GoldenTestCase[] {\n return snapshots.map((snapshot, index) => ({\n id: snapshot.id ?? randomUUID(),\n name: snapshot.success\n ? `case-${index + 1}-success`\n : `case-${index + 1}-failure`,\n input: snapshot.input,\n expectedOutput: snapshot.output,\n expectedError: snapshot.error,\n success: snapshot.success,\n metadata: this.serializeMetadata?.(snapshot),\n }));\n }\n\n generate(\n snapshots: TrafficSnapshot[],\n options: GoldenTestGeneratorOptions\n ): string {\n const cases = this.createCases(snapshots);\n if (options.framework === 'jest') {\n return generateJestSuite({\n suiteName: options.suiteName,\n cases,\n runnerImport: options.runnerImport,\n runnerFunction: options.runnerFunction,\n });\n }\n return generateVitestSuite({\n suiteName: options.suiteName,\n cases,\n runnerImport: options.runnerImport,\n runnerFunction: options.runnerFunction,\n });\n }\n}\n\nexport type GoldenTestRunner = (\n input: unknown,\n metadata?: Record<string, unknown>\n) => Promise<unknown>;\n\nexport interface GoldenTestRunResult {\n caseId: string;\n passed: boolean;\n durationMs: number;\n error?: unknown;\n}\n\nexport async function runGoldenTests(\n cases: GoldenTestCase[],\n runner: GoldenTestRunner\n): Promise<GoldenTestRunResult[]> {\n const results: GoldenTestRunResult[] = [];\n for (const testCase of cases) {\n const startedAt = performance.now();\n try {\n const output = await runner(testCase.input, testCase.metadata);\n if (!testCase.success) {\n results.push({\n caseId: testCase.id,\n passed: false,\n durationMs: performance.now() - startedAt,\n error: new Error('Expected failure but runner resolved'),\n });\n continue;\n }\n const matches =\n JSON.stringify(output) ===\n JSON.stringify(testCase.expectedOutput ?? null);\n results.push({\n caseId: testCase.id,\n passed: matches,\n durationMs: performance.now() - startedAt,\n error: matches\n ? undefined\n : { expected: testCase.expectedOutput, received: output },\n });\n } catch (error) {\n const durationMs = performance.now() - startedAt;\n if (!testCase.success) {\n results.push({ caseId: testCase.id, passed: true, durationMs });\n } else {\n results.push({ caseId: testCase.id, passed: false, durationMs, error });\n }\n }\n }\n return results;\n}\n"],"mappings":";;;;;;AAcA,IAAa,sBAAb,MAAiC;CAC/B,YACE,AAAiB,qBACf,cAEC;EACC,UAAU,SAAS;EACnB,QAAQ,SAAS;EACjB,SAAS,SAAS;EACnB,GACH;EARiB;;CAUnB,YAAY,WAAgD;AAC1D,SAAO,UAAU,KAAK,UAAU,WAAW;GACzC,IAAI,SAAS,MAAM,YAAY;GAC/B,MAAM,SAAS,UACX,QAAQ,QAAQ,EAAE,YAClB,QAAQ,QAAQ,EAAE;GACtB,OAAO,SAAS;GAChB,gBAAgB,SAAS;GACzB,eAAe,SAAS;GACxB,SAAS,SAAS;GAClB,UAAU,KAAK,oBAAoB,SAAS;GAC7C,EAAE;;CAGL,SACE,WACA,SACQ;EACR,MAAM,QAAQ,KAAK,YAAY,UAAU;AACzC,MAAI,QAAQ,cAAc,OACxB,QAAO,kBAAkB;GACvB,WAAW,QAAQ;GACnB;GACA,cAAc,QAAQ;GACtB,gBAAgB,QAAQ;GACzB,CAAC;AAEJ,SAAO,oBAAoB;GACzB,WAAW,QAAQ;GACnB;GACA,cAAc,QAAQ;GACtB,gBAAgB,QAAQ;GACzB,CAAC;;;AAgBN,eAAsB,eACpB,OACA,QACgC;CAChC,MAAM,UAAiC,EAAE;AACzC,MAAK,MAAM,YAAY,OAAO;EAC5B,MAAM,YAAY,YAAY,KAAK;AACnC,MAAI;GACF,MAAM,SAAS,MAAM,OAAO,SAAS,OAAO,SAAS,SAAS;AAC9D,OAAI,CAAC,SAAS,SAAS;AACrB,YAAQ,KAAK;KACX,QAAQ,SAAS;KACjB,QAAQ;KACR,YAAY,YAAY,KAAK,GAAG;KAChC,uBAAO,IAAI,MAAM,uCAAuC;KACzD,CAAC;AACF;;GAEF,MAAM,UACJ,KAAK,UAAU,OAAO,KACtB,KAAK,UAAU,SAAS,kBAAkB,KAAK;AACjD,WAAQ,KAAK;IACX,QAAQ,SAAS;IACjB,QAAQ;IACR,YAAY,YAAY,KAAK,GAAG;IAChC,OAAO,UACH,SACA;KAAE,UAAU,SAAS;KAAgB,UAAU;KAAQ;IAC5D,CAAC;WACK,OAAO;GACd,MAAM,aAAa,YAAY,KAAK,GAAG;AACvC,OAAI,CAAC,SAAS,QACZ,SAAQ,KAAK;IAAE,QAAQ,SAAS;IAAI,QAAQ;IAAM;IAAY,CAAC;OAE/D,SAAQ,KAAK;IAAE,QAAQ,SAAS;IAAI,QAAQ;IAAO;IAAY;IAAO,CAAC;;;AAI7E,QAAO"}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
|
|
3
|
-
//#region src/recorder/traffic-recorder.ts
|
|
4
|
-
var InMemoryTrafficStore = class {
|
|
5
|
-
items = [];
|
|
6
|
-
async save(snapshot) {
|
|
7
|
-
this.items.push(snapshot);
|
|
8
|
-
}
|
|
9
|
-
async list(operation) {
|
|
10
|
-
if (!operation) return [...this.items];
|
|
11
|
-
return this.items.filter((item) => item.operation.name === operation);
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
var TrafficRecorder = class {
|
|
15
|
-
store;
|
|
16
|
-
sampleRate;
|
|
17
|
-
sanitize;
|
|
18
|
-
constructor(options) {
|
|
19
|
-
this.store = options.store;
|
|
20
|
-
this.sampleRate = options.sampleRate ?? 1;
|
|
21
|
-
this.sanitize = options.sanitize;
|
|
22
|
-
}
|
|
23
|
-
async record(input) {
|
|
24
|
-
if (!this.shouldSample()) return;
|
|
25
|
-
const snapshot = {
|
|
26
|
-
id: randomUUID(),
|
|
27
|
-
operation: input.operation,
|
|
28
|
-
input: structuredCloneSafe(input.input),
|
|
29
|
-
output: structuredCloneSafe(input.output),
|
|
30
|
-
error: input.error ? structuredCloneSafe(input.error) : void 0,
|
|
31
|
-
success: input.success,
|
|
32
|
-
timestamp: /* @__PURE__ */ new Date(),
|
|
33
|
-
durationMs: input.durationMs,
|
|
34
|
-
tenantId: input.tenantId,
|
|
35
|
-
userId: input.userId,
|
|
36
|
-
channel: input.channel,
|
|
37
|
-
metadata: input.metadata
|
|
38
|
-
};
|
|
39
|
-
const sanitized = this.sanitize ? this.sanitize(snapshot) : snapshot;
|
|
40
|
-
await this.store.save(sanitized);
|
|
41
|
-
}
|
|
42
|
-
shouldSample() {
|
|
43
|
-
if (this.sampleRate >= 1) return true;
|
|
44
|
-
return Math.random() <= this.sampleRate;
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
function structuredCloneSafe(value) {
|
|
48
|
-
if (value == null) return value ?? void 0;
|
|
49
|
-
try {
|
|
50
|
-
const clone = globalThis.structuredClone;
|
|
51
|
-
if (typeof clone === "function") return clone(value);
|
|
52
|
-
return JSON.parse(JSON.stringify(value));
|
|
53
|
-
} catch {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
//#endregion
|
|
59
|
-
export { InMemoryTrafficStore, TrafficRecorder };
|
|
60
|
-
//# sourceMappingURL=traffic-recorder.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"traffic-recorder.js","names":[],"sources":["../../src/recorder/traffic-recorder.ts"],"sourcesContent":["import { randomUUID } from 'node:crypto';\nimport type { TrafficSnapshot } from '../types';\n\nexport interface TrafficStore {\n save(snapshot: TrafficSnapshot): Promise<void>;\n list(\n operation?: TrafficSnapshot['operation']['name']\n ): Promise<TrafficSnapshot[]>;\n}\n\nexport class InMemoryTrafficStore implements TrafficStore {\n private readonly items: TrafficSnapshot[] = [];\n\n async save(snapshot: TrafficSnapshot): Promise<void> {\n this.items.push(snapshot);\n }\n\n async list(operation?: string): Promise<TrafficSnapshot[]> {\n if (!operation) return [...this.items];\n return this.items.filter((item) => item.operation.name === operation);\n }\n}\n\nexport interface TrafficRecorderOptions {\n store: TrafficStore;\n sampleRate?: number;\n sanitize?: (snapshot: TrafficSnapshot) => TrafficSnapshot;\n}\n\nexport interface RecordOperationInput {\n operation: TrafficSnapshot['operation'];\n input: unknown;\n output?: unknown;\n error?: TrafficSnapshot['error'];\n success: boolean;\n durationMs?: number;\n tenantId?: string;\n userId?: string;\n channel?: string;\n metadata?: Record<string, unknown>;\n}\n\nexport class TrafficRecorder {\n private readonly store: TrafficStore;\n private readonly sampleRate: number;\n private readonly sanitize?: (snapshot: TrafficSnapshot) => TrafficSnapshot;\n\n constructor(options: TrafficRecorderOptions) {\n this.store = options.store;\n this.sampleRate = options.sampleRate ?? 1;\n this.sanitize = options.sanitize;\n }\n\n async record(input: RecordOperationInput) {\n if (!this.shouldSample()) return;\n const snapshot: TrafficSnapshot = {\n id: randomUUID(),\n operation: input.operation,\n input: structuredCloneSafe(input.input),\n output: structuredCloneSafe(input.output),\n error: input.error ? structuredCloneSafe(input.error) : undefined,\n success: input.success,\n timestamp: new Date(),\n durationMs: input.durationMs,\n tenantId: input.tenantId,\n userId: input.userId,\n channel: input.channel,\n metadata: input.metadata,\n };\n const sanitized = this.sanitize ? this.sanitize(snapshot) : snapshot;\n await this.store.save(sanitized);\n }\n\n private shouldSample() {\n if (this.sampleRate >= 1) return true;\n return Math.random() <= this.sampleRate;\n }\n}\n\nfunction structuredCloneSafe<T>(value: T): T | undefined {\n if (value == null) return value ?? undefined;\n try {\n const clone = (globalThis as { structuredClone?: <R>(input: R) => R })\n .structuredClone;\n if (typeof clone === 'function') {\n return clone(value);\n }\n return JSON.parse(JSON.stringify(value));\n } catch {\n return undefined;\n }\n}\n"],"mappings":";;;AAUA,IAAa,uBAAb,MAA0D;CACxD,AAAiB,QAA2B,EAAE;CAE9C,MAAM,KAAK,UAA0C;AACnD,OAAK,MAAM,KAAK,SAAS;;CAG3B,MAAM,KAAK,WAAgD;AACzD,MAAI,CAAC,UAAW,QAAO,CAAC,GAAG,KAAK,MAAM;AACtC,SAAO,KAAK,MAAM,QAAQ,SAAS,KAAK,UAAU,SAAS,UAAU;;;AAuBzE,IAAa,kBAAb,MAA6B;CAC3B,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,SAAiC;AAC3C,OAAK,QAAQ,QAAQ;AACrB,OAAK,aAAa,QAAQ,cAAc;AACxC,OAAK,WAAW,QAAQ;;CAG1B,MAAM,OAAO,OAA6B;AACxC,MAAI,CAAC,KAAK,cAAc,CAAE;EAC1B,MAAM,WAA4B;GAChC,IAAI,YAAY;GAChB,WAAW,MAAM;GACjB,OAAO,oBAAoB,MAAM,MAAM;GACvC,QAAQ,oBAAoB,MAAM,OAAO;GACzC,OAAO,MAAM,QAAQ,oBAAoB,MAAM,MAAM,GAAG;GACxD,SAAS,MAAM;GACf,2BAAW,IAAI,MAAM;GACrB,YAAY,MAAM;GAClB,UAAU,MAAM;GAChB,QAAQ,MAAM;GACd,SAAS,MAAM;GACf,UAAU,MAAM;GACjB;EACD,MAAM,YAAY,KAAK,WAAW,KAAK,SAAS,SAAS,GAAG;AAC5D,QAAM,KAAK,MAAM,KAAK,UAAU;;CAGlC,AAAQ,eAAe;AACrB,MAAI,KAAK,cAAc,EAAG,QAAO;AACjC,SAAO,KAAK,QAAQ,IAAI,KAAK;;;AAIjC,SAAS,oBAAuB,OAAyB;AACvD,KAAI,SAAS,KAAM,QAAO,SAAS;AACnC,KAAI;EACF,MAAM,QAAS,WACZ;AACH,MAAI,OAAO,UAAU,WACnB,QAAO,MAAM,MAAM;AAErB,SAAO,KAAK,MAAM,KAAK,UAAU,MAAM,CAAC;SAClC;AACN"}
|