autotel-message-contract 0.1.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/README.md +166 -0
- package/dist/index.cjs +453 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +173 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +173 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +414 -0
- package/dist/index.js.map +1 -0
- package/dist/serializer.cjs +47 -0
- package/dist/serializer.cjs.map +1 -0
- package/dist/serializer.d.cts +50 -0
- package/dist/serializer.d.cts.map +1 -0
- package/dist/serializer.d.ts +50 -0
- package/dist/serializer.d.ts.map +1 -0
- package/dist/serializer.js +44 -0
- package/dist/serializer.js.map +1 -0
- package/package.json +73 -0
- package/src/contract.test.ts +279 -0
- package/src/contract.ts +409 -0
- package/src/diff.ts +61 -0
- package/src/index.ts +53 -0
- package/src/reader.ts +105 -0
- package/src/serializer.test.ts +32 -0
- package/src/serializer.ts +89 -0
- package/src/snapshot-storage.ts +113 -0
package/README.md
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# autotel-message-contract
|
|
2
|
+
|
|
3
|
+
> Pin the serialized shape of your messages and prove old and new versions stay compatible, as ordinary unit tests with the contract committed beside the test.
|
|
4
|
+
|
|
5
|
+
`autotel-message-contract` is contract testing for the messages your code sends and stores: events, commands, queue payloads, HTTP request/response bodies, and anything else you serialize for someone else to read.
|
|
6
|
+
|
|
7
|
+
You write a small unit test that locks down a message's serialized format. Later you rename a field or change a type. The code still compiles and your other tests pass, but this one fails and points at what changed. You fix it in the same pull request, before a consumer or a stored event has hit the old format in production.
|
|
8
|
+
|
|
9
|
+
It is the test-time companion to [`autotel-pact`](../autotel-pact) (runtime evidence that contracted interactions actually fired) and [`autotel-schema`](../autotel-schema) (your telemetry surface as a contract). Where those answer *did it run?* and *is my trace surface stable?*, this answers *does my message serialization stay stable and stay compatible across versions?*
|
|
10
|
+
|
|
11
|
+
## Why
|
|
12
|
+
|
|
13
|
+
When you change how a message serializes, the change is easy to miss. The code compiles and the tests pass, because they write and read the message with the same code. The mismatch surfaces later, when something holding the old format reads it: a stored event, a message waiting on a queue, or another service.
|
|
14
|
+
|
|
15
|
+
`autotel-message-contract` takes the small, brokerless approach:
|
|
16
|
+
|
|
17
|
+
- **The checks are ordinary unit tests** in your existing suite, with no broker, schema registry, or mock service to run, and nothing to start in Docker.
|
|
18
|
+
- **The contract is the serialized output committed next to the test**, so a format change appears in a normal diff and is reviewed like any other code.
|
|
19
|
+
- **The check uses your application's own serializer**, so the snapshot is the exact bytes you ship.
|
|
20
|
+
|
|
21
|
+
It checks the serialized shape of a message and whether its versions stay compatible. It doesn't exercise a live exchange between running services, so it complements that kind of tooling (Pact, `autotel-pact`) rather than replacing it.
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pnpm add -D autotel-message-contract
|
|
27
|
+
# autotel is an optional peer dependency; this package works standalone
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Snapshot check: pin the serialized shape
|
|
31
|
+
|
|
32
|
+
A snapshot check confirms a message still serializes to the bytes you approved, so nothing reading it downstream breaks. The first run writes the approved file and passes; you review and commit it. From then on the check compares against it.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { messageContract } from 'autotel-message-contract';
|
|
36
|
+
import { OrderPlaced } from './events';
|
|
37
|
+
|
|
38
|
+
it('OrderPlaced serialization is unchanged', () => {
|
|
39
|
+
messageContract({ snapshot: 'OrderPlaced' })
|
|
40
|
+
.given(new OrderPlaced('ord-1', 'Alice', placedAt))
|
|
41
|
+
.whenSerialized()
|
|
42
|
+
.thenContractIsUnchanged();
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The approved file lands in a `__contracts__/` directory beside the test (`OrderPlaced.approved.txt`). When the format drifts, the failure shows you what moved:
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
Message contract drifted from its approved snapshot.
|
|
50
|
+
serializer: json
|
|
51
|
+
snapshot: .../__contracts__/OrderPlaced.approved.txt
|
|
52
|
+
|
|
53
|
+
{
|
|
54
|
+
- "customer": "Alice",
|
|
55
|
+
+ "customerName": "Alice",
|
|
56
|
+
"orderId": "ord-1"
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
If this change is intentional, re-run with AUTOTEL_CONTRACT_UPDATE=1 to update the
|
|
60
|
+
approved file, then review and commit it.
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Use your application's serializer
|
|
64
|
+
|
|
65
|
+
The default serializer is JSON with deterministic key ordering, good enough to pin most events. The snapshot is only meaningful if it matches the shape your consumers see. Pass your app's real serializer so the snapshot records the exact bytes you ship (snake_case, custom date formats, omitted nulls, `superjson`, `devalue`, protobuf):
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { messageContract } from 'autotel-message-contract';
|
|
69
|
+
|
|
70
|
+
messageContract({ serializer: mySnakeCaseSerializer, snapshot: 'OrderPlaced' })
|
|
71
|
+
.given(new OrderPlaced('ord-1', 'Alice', placedAt))
|
|
72
|
+
.whenSerialized()
|
|
73
|
+
.thenContractIsUnchanged();
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
A `MessageSerializer` is `{ name, serialize, deserialize }`. `jsonSerializer({ indent: 0, sortKeys: false })` gives you the compact, order-preserving bytes you put on the wire.
|
|
77
|
+
|
|
78
|
+
## Compatibility check: prove versions still read each other
|
|
79
|
+
|
|
80
|
+
A compatibility check is for the version you evolve on purpose, so changing a message doesn't strand the ones already in your store or on the wire. TypeScript erases types at runtime, so instead of a class you hand over a **reader**: a [Standard Schema](https://standardschema.dev) (Zod ≥3.24, Valibot, ArkType) or a plain parse function.
|
|
81
|
+
|
|
82
|
+
**Backward compatible**: confirm a newer reader still reads what an older writer produced (events you stored last year, a request already sent):
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { messageContract } from 'autotel-message-contract';
|
|
86
|
+
import { OrderPlacedV2 } from './events'; // a Zod schema
|
|
87
|
+
|
|
88
|
+
await messageContract()
|
|
89
|
+
.given(orderPlacedV1) // bytes an old version wrote
|
|
90
|
+
.whenDeserializedAs(OrderPlacedV2)
|
|
91
|
+
.thenBackwardCompatible((v2) => {
|
|
92
|
+
expect(v2.coupon).toBeUndefined(); // newly-added field defaults sensibly
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
**Forward compatible**: confirm a consumer that hasn't upgraded yet still reads what the newer writer produces, so you can ship the new shape before the readers have caught up:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
await messageContract()
|
|
100
|
+
.given(orderPlacedV2) // bytes the new version writes
|
|
101
|
+
.whenDeserializedAs(OrderPlacedV1)
|
|
102
|
+
.thenForwardCompatible();
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Compatibility checks are stricter than simple parse-success: after the target
|
|
106
|
+
reader accepts the payload, the package re-serializes the parsed value and
|
|
107
|
+
checks that shared fields still mean the same thing. Silent field renames or
|
|
108
|
+
lossy transforms fail even if the reader does not throw.
|
|
109
|
+
|
|
110
|
+
### Replay a saved snapshot as the source version
|
|
111
|
+
|
|
112
|
+
When you want to prove that today's reader still accepts a payload you approved
|
|
113
|
+
months ago, point the check at the approved file directly:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
import { approvedSnapshot, messageContract } from 'autotel-message-contract';
|
|
117
|
+
|
|
118
|
+
await messageContract({ snapshot: 'OrderPlaced_v1' })
|
|
119
|
+
.given(approvedSnapshot())
|
|
120
|
+
.whenDeserializedAs(OrderPlacedV2)
|
|
121
|
+
.thenBackwardCompatible();
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
You can also pass an explicit location: `approvedSnapshot({ dir, name })` or
|
|
125
|
+
`approvedSnapshot({ path })`.
|
|
126
|
+
|
|
127
|
+
When the versions have drifted apart, the failure names the issues:
|
|
128
|
+
|
|
129
|
+
```
|
|
130
|
+
Not backward-compatible: the newer reader rejected a message an older writer produced.
|
|
131
|
+
serializer: json
|
|
132
|
+
serialized: {"customerName":"Alice","orderId":"ord-1"}
|
|
133
|
+
issues:
|
|
134
|
+
- customer: Required
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Updating snapshots
|
|
138
|
+
|
|
139
|
+
When a change is intentional, re-run with any of these set, review the diff, and commit:
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
AUTOTEL_CONTRACT_UPDATE=1 pnpm test
|
|
143
|
+
# UPDATE_CONTRACTS / UPDATE_SNAPSHOTS also work
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Or per-check: `messageContract({ snapshot: 'X', update: true })`.
|
|
147
|
+
|
|
148
|
+
## What this package does NOT do
|
|
149
|
+
|
|
150
|
+
- **Does not replace Pact / `autotel-pact`.** It checks serialized shape and version compatibility, not a live exchange between running services.
|
|
151
|
+
- **Does not infer your serializer.** Pass your app's serializer to pin the bytes you ship; the default is deterministic JSON.
|
|
152
|
+
- **Does not pin API/type surface.** Single-purpose by design. For module or type-surface pinning use a dedicated tool like [`@microsoft/api-extractor`](https://api-extractor.com) or [`@arethetypeswrong/cli`](https://github.com/arethetypeswrong/arethetypeswrong.github.io).
|
|
153
|
+
|
|
154
|
+
## API
|
|
155
|
+
|
|
156
|
+
| Export | Purpose |
|
|
157
|
+
|--------|---------|
|
|
158
|
+
| `messageContract(options?)` | Start a check: `.given(msg).whenSerialized()` / `.whenDeserializedAs(reader)`. |
|
|
159
|
+
| `approvedSnapshot(location?)` | Use a committed approved file as the source version in a compatibility check. |
|
|
160
|
+
| `jsonSerializer(options?)` | Deterministic JSON serializer; `defaultSerializer` is `jsonSerializer()`. |
|
|
161
|
+
| `read(reader, value)` | Run a reader (schema or parse fn) against a value; never throws. |
|
|
162
|
+
| `lineDiff`, `resolveSnapshotPath`, `readSnapshot`, `writeSnapshot`, `isUpdateMode` | Lower-level building blocks. |
|
|
163
|
+
|
|
164
|
+
## License
|
|
165
|
+
|
|
166
|
+
MIT © Jag Reehal
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
+
key = keys[i];
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
14
|
+
__defProp(to, key, {
|
|
15
|
+
get: ((k) => from[k]).bind(null, key),
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
24
|
+
value: mod,
|
|
25
|
+
enumerable: true
|
|
26
|
+
}) : target, mod));
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
const require_serializer = require('./serializer.cjs');
|
|
30
|
+
let node_fs = require("node:fs");
|
|
31
|
+
let node_path = require("node:path");
|
|
32
|
+
node_path = __toESM(node_path, 1);
|
|
33
|
+
|
|
34
|
+
//#region src/diff.ts
|
|
35
|
+
/**
|
|
36
|
+
* Minimal line diff for snapshot failures. The goal is a message that points
|
|
37
|
+
* at *what moved* — a renamed field, a switched date format, a dropped value —
|
|
38
|
+
* not a full diff engine. Lines only in the approved file are marked `-`, lines
|
|
39
|
+
* only in the actual output are marked `+`, and a little surrounding context is
|
|
40
|
+
* kept so the change is readable in a terminal.
|
|
41
|
+
*/
|
|
42
|
+
function lineDiff(approved, actual) {
|
|
43
|
+
const a = approved.split("\n");
|
|
44
|
+
const b = actual.split("\n");
|
|
45
|
+
const lcs = longestCommonSubsequence(a, b);
|
|
46
|
+
const out = [];
|
|
47
|
+
let i = 0;
|
|
48
|
+
let j = 0;
|
|
49
|
+
for (const [ai, bj] of lcs) {
|
|
50
|
+
while (i < ai) out.push(`- ${a[i++]}`);
|
|
51
|
+
while (j < bj) out.push(`+ ${b[j++]}`);
|
|
52
|
+
out.push(` ${a[i++]}`);
|
|
53
|
+
j++;
|
|
54
|
+
}
|
|
55
|
+
while (i < a.length) out.push(`- ${a[i++]}`);
|
|
56
|
+
while (j < b.length) out.push(`+ ${b[j++]}`);
|
|
57
|
+
return out.join("\n");
|
|
58
|
+
}
|
|
59
|
+
/** Indices `[i, j]` of lines common to both, longest such subsequence. */
|
|
60
|
+
function longestCommonSubsequence(a, b) {
|
|
61
|
+
const n = a.length;
|
|
62
|
+
const m = b.length;
|
|
63
|
+
const table = Array.from({ length: n + 1 }, () => Array.from({ length: m + 1 }, () => 0));
|
|
64
|
+
for (let i = n - 1; i >= 0; i--) for (let j = m - 1; j >= 0; j--) table[i][j] = a[i] === b[j] ? table[i + 1][j + 1] + 1 : Math.max(table[i + 1][j], table[i][j + 1]);
|
|
65
|
+
const pairs = [];
|
|
66
|
+
let i = 0;
|
|
67
|
+
let j = 0;
|
|
68
|
+
while (i < n && j < m) if (a[i] === b[j]) {
|
|
69
|
+
pairs.push([i, j]);
|
|
70
|
+
i++;
|
|
71
|
+
j++;
|
|
72
|
+
} else if (table[i + 1][j] >= table[i][j + 1]) i++;
|
|
73
|
+
else j++;
|
|
74
|
+
return pairs;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/reader.ts
|
|
79
|
+
function isStandardSchema(reader) {
|
|
80
|
+
return typeof reader === "object" && reader !== null && "~standard" in reader && typeof reader["~standard"]?.validate === "function";
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Run a reader against a deserialized value. Never throws — a thrown parse
|
|
84
|
+
* error or reported issues become `{ ok: false, issues }` so the caller can
|
|
85
|
+
* build a single, legible failure message.
|
|
86
|
+
*/
|
|
87
|
+
async function read(reader, value) {
|
|
88
|
+
if (isStandardSchema(reader)) try {
|
|
89
|
+
const result = await reader["~standard"].validate(value);
|
|
90
|
+
if (result.issues && result.issues.length > 0) return {
|
|
91
|
+
ok: false,
|
|
92
|
+
issues: result.issues.map((issue) => formatIssue(issue.message, issue.path))
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
ok: true,
|
|
96
|
+
value: result.value,
|
|
97
|
+
issues: []
|
|
98
|
+
};
|
|
99
|
+
} catch (error) {
|
|
100
|
+
return {
|
|
101
|
+
ok: false,
|
|
102
|
+
issues: [errorMessage(error)]
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
return {
|
|
107
|
+
ok: true,
|
|
108
|
+
value: reader(value),
|
|
109
|
+
issues: []
|
|
110
|
+
};
|
|
111
|
+
} catch (error) {
|
|
112
|
+
return {
|
|
113
|
+
ok: false,
|
|
114
|
+
issues: [errorMessage(error)]
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function formatIssue(message, path) {
|
|
119
|
+
if (Array.isArray(path) && path.length > 0) return `${path.map(String).join(".")}: ${message}`;
|
|
120
|
+
return message;
|
|
121
|
+
}
|
|
122
|
+
function errorMessage(error) {
|
|
123
|
+
if (error instanceof Error) return error.message;
|
|
124
|
+
return String(error);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/snapshot-storage.ts
|
|
129
|
+
/**
|
|
130
|
+
* Snapshot storage.
|
|
131
|
+
*
|
|
132
|
+
* A snapshot is just a file you commit: the serialized shape of a message,
|
|
133
|
+
* reviewed once and from then on compared on every run. There is no broker, no
|
|
134
|
+
* registry, no service to start — a format change shows up in a normal diff,
|
|
135
|
+
* reviewed like any other code. This mirrors the "approved file" convention
|
|
136
|
+
* (`<name>.approved.txt`) familiar from approval testing.
|
|
137
|
+
*
|
|
138
|
+
* By default the file lives in a `__contracts__` directory beside the test that
|
|
139
|
+
* created it, resolved from the call stack so you do not have to thread paths
|
|
140
|
+
* around. Override `dir` / `path` when you keep snapshots elsewhere.
|
|
141
|
+
*/
|
|
142
|
+
/** Set any of these (e.g. `AUTOTEL_CONTRACT_UPDATE=1`) to (re)write approved files. */
|
|
143
|
+
const UPDATE_ENV_VARS = [
|
|
144
|
+
"AUTOTEL_CONTRACT_UPDATE",
|
|
145
|
+
"UPDATE_CONTRACTS",
|
|
146
|
+
"UPDATE_SNAPSHOTS"
|
|
147
|
+
];
|
|
148
|
+
function isUpdateMode(env = process.env) {
|
|
149
|
+
return UPDATE_ENV_VARS.some((name) => {
|
|
150
|
+
const value = env[name];
|
|
151
|
+
return value === "1" || value === "true";
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
const DEFAULT_DIR_NAME = "__contracts__";
|
|
155
|
+
const APPROVED_SUFFIX = ".approved.txt";
|
|
156
|
+
/** Resolve the absolute file path for a snapshot. */
|
|
157
|
+
function resolveSnapshotPath(location) {
|
|
158
|
+
if (location.path) return node_path.default.isAbsolute(location.path) ? location.path : node_path.default.resolve(process.cwd(), location.path);
|
|
159
|
+
const dir = location.dir ?? defaultSnapshotDir();
|
|
160
|
+
return node_path.default.join(dir, `${sanitize(location.name)}${APPROVED_SUFFIX}`);
|
|
161
|
+
}
|
|
162
|
+
function sanitize(name) {
|
|
163
|
+
return name.replaceAll(/[^\w.@-]+/g, "_");
|
|
164
|
+
}
|
|
165
|
+
function readSnapshot(location) {
|
|
166
|
+
const filePath = resolveSnapshotPath(location);
|
|
167
|
+
if (!(0, node_fs.existsSync)(filePath)) return {
|
|
168
|
+
exists: false,
|
|
169
|
+
path: filePath
|
|
170
|
+
};
|
|
171
|
+
return {
|
|
172
|
+
exists: true,
|
|
173
|
+
content: (0, node_fs.readFileSync)(filePath, "utf8"),
|
|
174
|
+
path: filePath
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function writeSnapshot(location, content) {
|
|
178
|
+
const filePath = resolveSnapshotPath(location);
|
|
179
|
+
(0, node_fs.mkdirSync)(node_path.default.dirname(filePath), { recursive: true });
|
|
180
|
+
(0, node_fs.writeFileSync)(filePath, content, "utf8");
|
|
181
|
+
return filePath;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Best-effort `__contracts__` directory beside the calling test file, found by
|
|
185
|
+
* walking the stack past this module and the contract internals. Falls back to
|
|
186
|
+
* `<cwd>/__contracts__` when the caller cannot be determined (e.g. bundled).
|
|
187
|
+
*/
|
|
188
|
+
function defaultSnapshotDir() {
|
|
189
|
+
const callerFile = callerOutsidePackage();
|
|
190
|
+
const base = callerFile ? node_path.default.dirname(callerFile) : process.cwd();
|
|
191
|
+
return node_path.default.join(base, DEFAULT_DIR_NAME);
|
|
192
|
+
}
|
|
193
|
+
function callerOutsidePackage() {
|
|
194
|
+
const stack = (/* @__PURE__ */ new Error("stack probe")).stack;
|
|
195
|
+
if (!stack) return void 0;
|
|
196
|
+
const lines = stack.split("\n").slice(1);
|
|
197
|
+
for (const line of lines) {
|
|
198
|
+
const file = (line.match(/\((.*?):\d+:\d+\)/) ?? line.match(/at (.*?):\d+:\d+/))?.[1];
|
|
199
|
+
if (!file) continue;
|
|
200
|
+
if (file.includes("node:")) continue;
|
|
201
|
+
if (file.includes(`${node_path.default.join("autotel-message-contract", "src")}`)) continue;
|
|
202
|
+
if (file.includes(`${node_path.default.join("autotel-message-contract", "dist")}`)) continue;
|
|
203
|
+
if (file.includes("node_modules")) continue;
|
|
204
|
+
return file.startsWith("file://") ? fileUrlToPath(file) : file;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function fileUrlToPath(url) {
|
|
208
|
+
return decodeURIComponent(url.replace(/^file:\/\//, ""));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
//#endregion
|
|
212
|
+
//#region src/contract.ts
|
|
213
|
+
/**
|
|
214
|
+
* The message contract DSL.
|
|
215
|
+
*
|
|
216
|
+
* Every check starts from {@link messageContract} and reads as a sentence:
|
|
217
|
+
*
|
|
218
|
+
* ```ts
|
|
219
|
+
* // Pin the serialized shape — fail when it drifts.
|
|
220
|
+
* await messageContract()
|
|
221
|
+
* .given(new OrderPlaced(orderId, 'Alice', placedAt))
|
|
222
|
+
* .whenSerialized()
|
|
223
|
+
* .thenContractIsUnchanged();
|
|
224
|
+
*
|
|
225
|
+
* // Prove a newer reader still reads what an older writer produced.
|
|
226
|
+
* await messageContract()
|
|
227
|
+
* .given(orderPlacedV1)
|
|
228
|
+
* .whenDeserializedAs(OrderPlacedV2) // a Zod schema or parse fn
|
|
229
|
+
* .thenBackwardCompatible((v2) => expect(v2.coupon).toBeUndefined());
|
|
230
|
+
* ```
|
|
231
|
+
*
|
|
232
|
+
* A **snapshot check** confirms a message still serializes exactly as approved,
|
|
233
|
+
* so nothing reading it downstream breaks. A **compatibility check** confirms an
|
|
234
|
+
* older and a newer version can still read each other's data. Both are ordinary
|
|
235
|
+
* unit tests — no broker, no registry, no running service.
|
|
236
|
+
*/
|
|
237
|
+
/** Thrown when a contract check fails. Message is pre-formatted for a test runner. */
|
|
238
|
+
var ContractViolationError = class extends Error {
|
|
239
|
+
name = "ContractViolationError";
|
|
240
|
+
constructor(message) {
|
|
241
|
+
super(message);
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
const SNAPSHOT_SOURCE = Symbol("autotel-message-contract.snapshot-source");
|
|
245
|
+
/**
|
|
246
|
+
* Point a compatibility check at a previously approved snapshot instead of a
|
|
247
|
+
* live in-memory message instance.
|
|
248
|
+
*/
|
|
249
|
+
function approvedSnapshot(location) {
|
|
250
|
+
return {
|
|
251
|
+
[SNAPSHOT_SOURCE]: true,
|
|
252
|
+
location
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
function isApprovedSnapshotSource(value) {
|
|
256
|
+
return typeof value === "object" && value !== null && SNAPSHOT_SOURCE in value && value[SNAPSHOT_SOURCE] === true;
|
|
257
|
+
}
|
|
258
|
+
/** Start a contract check. */
|
|
259
|
+
function messageContract(options = {}) {
|
|
260
|
+
return new GivenStep(options);
|
|
261
|
+
}
|
|
262
|
+
var GivenStep = class {
|
|
263
|
+
options;
|
|
264
|
+
constructor(options) {
|
|
265
|
+
this.options = options;
|
|
266
|
+
}
|
|
267
|
+
/** The message under contract. */
|
|
268
|
+
given(message) {
|
|
269
|
+
return new WhenStep(message, this.options);
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
var WhenStep = class {
|
|
273
|
+
message;
|
|
274
|
+
options;
|
|
275
|
+
constructor(message, options) {
|
|
276
|
+
this.message = message;
|
|
277
|
+
this.options = options;
|
|
278
|
+
}
|
|
279
|
+
/** Serialize the message; the next step pins or inspects the result. */
|
|
280
|
+
whenSerialized() {
|
|
281
|
+
if (isApprovedSnapshotSource(this.message)) throw new ContractViolationError("Cannot serialize an approved snapshot source. Use .whenDeserializedAs(...) to run a compatibility check, or pass a live message instance to .given(...).");
|
|
282
|
+
const serializer = this.options.serializer ?? require_serializer.defaultSerializer;
|
|
283
|
+
return new SnapshotStep(serializer.serialize(this.message), serializer, this.options);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Round-trip the message through a reader that models a *different version*
|
|
287
|
+
* (a Standard Schema such as Zod/Valibot, or a parse function). The next step
|
|
288
|
+
* asserts the versions stay compatible.
|
|
289
|
+
*/
|
|
290
|
+
whenDeserializedAs(reader) {
|
|
291
|
+
const serializer = this.options.serializer ?? require_serializer.defaultSerializer;
|
|
292
|
+
return new CompatibilityStep(this.message, reader, serializer, this.options);
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
var SnapshotStep = class {
|
|
296
|
+
serialized;
|
|
297
|
+
serializer;
|
|
298
|
+
options;
|
|
299
|
+
constructor(serialized, serializer, options) {
|
|
300
|
+
this.serialized = serialized;
|
|
301
|
+
this.serializer = serializer;
|
|
302
|
+
this.options = options;
|
|
303
|
+
}
|
|
304
|
+
/** The serialized bytes, for ad-hoc assertions outside the snapshot flow. */
|
|
305
|
+
get output() {
|
|
306
|
+
return this.serialized;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Compare the serialized output against the approved snapshot. On first run
|
|
310
|
+
* (or in update mode) it writes the approved file and passes; afterwards it
|
|
311
|
+
* fails with a diff when the shape drifts.
|
|
312
|
+
*/
|
|
313
|
+
thenContractIsUnchanged(snapshotName) {
|
|
314
|
+
const location = this.resolveLocation(snapshotName);
|
|
315
|
+
const existing = readSnapshot(location);
|
|
316
|
+
const update = this.options.update ?? isUpdateMode();
|
|
317
|
+
if (!existing.exists || update) {
|
|
318
|
+
writeSnapshot(location, this.serialized);
|
|
319
|
+
if (!existing.exists) return;
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (existing.content !== this.serialized) throw new ContractViolationError(`Message contract drifted from its approved snapshot.\n serializer: ${this.serializer.name}\n snapshot: ${existing.path}\n\n${lineDiff(existing.content ?? "", this.serialized)}\n\nIf this change is intentional, re-run with AUTOTEL_CONTRACT_UPDATE=1 to update the approved file, then review and commit it.`);
|
|
323
|
+
}
|
|
324
|
+
resolveLocation(snapshotName) {
|
|
325
|
+
if (snapshotName) return { name: snapshotName };
|
|
326
|
+
const configured = this.options.snapshot;
|
|
327
|
+
if (typeof configured === "string") return { name: configured };
|
|
328
|
+
if (configured) return configured;
|
|
329
|
+
throw new ContractViolationError("A snapshot name is required. Pass one to messageContract({ snapshot: 'OrderPlaced' }) or to thenContractIsUnchanged('OrderPlaced').");
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
var CompatibilityStep = class {
|
|
333
|
+
source;
|
|
334
|
+
reader;
|
|
335
|
+
serializer;
|
|
336
|
+
options;
|
|
337
|
+
constructor(source, reader, serializer, options) {
|
|
338
|
+
this.source = source;
|
|
339
|
+
this.reader = reader;
|
|
340
|
+
this.serializer = serializer;
|
|
341
|
+
this.options = options;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* The reader models a **newer** version; confirm it still reads what an older
|
|
345
|
+
* writer produced (stored events, in-flight messages). Optionally assert on
|
|
346
|
+
* the upgraded value — e.g. that a newly-added field defaults sensibly.
|
|
347
|
+
*/
|
|
348
|
+
async thenBackwardCompatible(assert) {
|
|
349
|
+
return this.check("backward", assert);
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* The reader models an **older** version; confirm a consumer that has not
|
|
353
|
+
* upgraded yet still reads what the newer writer produces, so you can ship the
|
|
354
|
+
* new shape before every reader has caught up.
|
|
355
|
+
*/
|
|
356
|
+
async thenForwardCompatible(assert) {
|
|
357
|
+
return this.check("forward", assert);
|
|
358
|
+
}
|
|
359
|
+
async check(direction, assert) {
|
|
360
|
+
const serialized = this.resolveSourceSerialized();
|
|
361
|
+
const deserializedSource = this.serializer.deserialize(serialized);
|
|
362
|
+
const outcome = await read(this.reader, deserializedSource);
|
|
363
|
+
if (!outcome.ok) throw new ContractViolationError(`Not ${direction}-compatible: ${direction === "backward" ? "the newer reader" : "the older reader"} rejected a message ${direction === "backward" ? "an older writer" : "a newer writer"} produced.\n serializer: ${this.serializer.name}\n serialized: ${truncate(serialized)}\n issues:\n${outcome.issues.map((m) => ` - ${m}`).join("\n")}`);
|
|
364
|
+
this.assertStructuralCompatibility(deserializedSource, this.serializer.deserialize(this.serializer.serialize(outcome.value)), direction, serialized);
|
|
365
|
+
if (assert) await assert(outcome.value);
|
|
366
|
+
return outcome.value;
|
|
367
|
+
}
|
|
368
|
+
resolveSourceSerialized() {
|
|
369
|
+
if (isApprovedSnapshotSource(this.source)) {
|
|
370
|
+
const existing = readSnapshot(this.resolveSnapshotLocation(this.source.location));
|
|
371
|
+
if (!existing.exists || existing.content === void 0) throw new ContractViolationError(`Cannot read approved snapshot for compatibility check.\n snapshot: ${existing.path}\n\nRecord it first with .whenSerialized().thenContractIsUnchanged(), or point approvedSnapshot(...) at an existing file.`);
|
|
372
|
+
return existing.content;
|
|
373
|
+
}
|
|
374
|
+
return this.serializer.serialize(this.source);
|
|
375
|
+
}
|
|
376
|
+
resolveSnapshotLocation(location) {
|
|
377
|
+
if (typeof location === "string") return { name: location };
|
|
378
|
+
if (location) return location;
|
|
379
|
+
const configured = this.options.snapshot;
|
|
380
|
+
if (typeof configured === "string") return { name: configured };
|
|
381
|
+
if (configured) return configured;
|
|
382
|
+
throw new ContractViolationError("A snapshot location is required for approvedSnapshot(). Pass approvedSnapshot('OrderPlaced_v1') or configure messageContract({ snapshot: 'OrderPlaced_v1' }).");
|
|
383
|
+
}
|
|
384
|
+
assertStructuralCompatibility(sourceValue, targetValue, direction, serialized) {
|
|
385
|
+
const mismatches = [];
|
|
386
|
+
compareSharedStructure(sourceValue, targetValue, "$", mismatches);
|
|
387
|
+
if (mismatches.length === 0) return;
|
|
388
|
+
throw new ContractViolationError(`Not ${direction}-compatible: shared fields changed meaning across versions.\n serializer: ${this.serializer.name}\n serialized: ${truncate(serialized)}\n mismatches:\n${mismatches.map((issue) => ` - ${issue}`).join("\n")}`);
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
function truncate(value, max = 400) {
|
|
392
|
+
return value.length > max ? `${value.slice(0, max)}… (${value.length} chars)` : value;
|
|
393
|
+
}
|
|
394
|
+
function compareSharedStructure(sourceValue, targetValue, path, mismatches) {
|
|
395
|
+
if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {
|
|
396
|
+
const sourceKeys = Object.keys(sourceValue);
|
|
397
|
+
const targetKeys = Object.keys(targetValue);
|
|
398
|
+
const sourceOnly = sourceKeys.filter((key) => !(key in targetValue));
|
|
399
|
+
const targetOnly = targetKeys.filter((key) => !(key in sourceValue));
|
|
400
|
+
if (sourceOnly.length > 0 && targetOnly.length > 0) {
|
|
401
|
+
mismatches.push(`${path}: structural incompatibility [source-only: ${sourceOnly.join(", ")}, target-only: ${targetOnly.join(", ")}]`);
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
for (const key of sourceKeys.filter((candidate) => candidate in targetValue).toSorted()) compareSharedStructure(sourceValue[key], targetValue[key], joinPath(path, key), mismatches);
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (Array.isArray(sourceValue) && Array.isArray(targetValue)) {
|
|
408
|
+
if (sourceValue.length !== targetValue.length) {
|
|
409
|
+
mismatches.push(`${path}: array length differs (${sourceValue.length} vs ${targetValue.length})`);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
for (const [index, sourceItem] of sourceValue.entries()) compareSharedStructure(sourceItem, targetValue[index], `${path}[${index}]`, mismatches);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
if (!deepEqual(sourceValue, targetValue)) mismatches.push(`${path}: value differs (${formatValue(sourceValue)} vs ${formatValue(targetValue)})`);
|
|
416
|
+
}
|
|
417
|
+
function isPlainObject(value) {
|
|
418
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
419
|
+
const proto = Object.getPrototypeOf(value);
|
|
420
|
+
return proto === Object.prototype || proto === null;
|
|
421
|
+
}
|
|
422
|
+
function deepEqual(left, right) {
|
|
423
|
+
if (Object.is(left, right)) return true;
|
|
424
|
+
if (Array.isArray(left) && Array.isArray(right)) return left.length === right.length && left.every((value, index) => deepEqual(value, right[index]));
|
|
425
|
+
if (isPlainObject(left) && isPlainObject(right)) {
|
|
426
|
+
const leftKeys = Object.keys(left);
|
|
427
|
+
const rightKeys = Object.keys(right);
|
|
428
|
+
return leftKeys.length === rightKeys.length && leftKeys.every((key) => key in right && deepEqual(left[key], right[key]));
|
|
429
|
+
}
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
function formatValue(value) {
|
|
433
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
434
|
+
if (value === null || typeof value === "number" || typeof value === "boolean" || value === void 0) return String(value);
|
|
435
|
+
return JSON.stringify(value);
|
|
436
|
+
}
|
|
437
|
+
function joinPath(path, key) {
|
|
438
|
+
return path === "$" ? `$.${key}` : `${path}.${key}`;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
//#endregion
|
|
442
|
+
exports.ContractViolationError = ContractViolationError;
|
|
443
|
+
exports.approvedSnapshot = approvedSnapshot;
|
|
444
|
+
exports.defaultSerializer = require_serializer.defaultSerializer;
|
|
445
|
+
exports.isUpdateMode = isUpdateMode;
|
|
446
|
+
exports.jsonSerializer = require_serializer.jsonSerializer;
|
|
447
|
+
exports.lineDiff = lineDiff;
|
|
448
|
+
exports.messageContract = messageContract;
|
|
449
|
+
exports.read = read;
|
|
450
|
+
exports.readSnapshot = readSnapshot;
|
|
451
|
+
exports.resolveSnapshotPath = resolveSnapshotPath;
|
|
452
|
+
exports.writeSnapshot = writeSnapshot;
|
|
453
|
+
//# sourceMappingURL=index.cjs.map
|