autotel-message-contract 2.0.3 → 2.0.5
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/LICENSE +191 -21
- package/README.md +1 -1
- package/dist/index.cjs +2 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/LICENSE
CHANGED
|
@@ -1,21 +1,191 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the
|
|
13
|
+
copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other
|
|
16
|
+
entities that control, are controlled by, or are under common control with
|
|
17
|
+
that entity. For the purposes of this definition, "control" means (i) the
|
|
18
|
+
power, direct or indirect, to cause the direction or management of such
|
|
19
|
+
entity, whether by contract or otherwise, or (ii) ownership of fifty
|
|
20
|
+
percent (50%) or more of the outstanding shares, or (iii) beneficial
|
|
21
|
+
ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
24
|
+
permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation source,
|
|
28
|
+
and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical transformation
|
|
31
|
+
or translation of a Source form, including but not limited to compiled
|
|
32
|
+
object code, generated documentation, and conversions to other media types.
|
|
33
|
+
|
|
34
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
35
|
+
made available under the License, as indicated by a copyright notice that
|
|
36
|
+
is included in or attached to the work (an example is provided in the
|
|
37
|
+
Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object form,
|
|
40
|
+
that is based on (or derived from) the Work and for which the editorial
|
|
41
|
+
revisions, annotations, elaborations, or other modifications represent, as
|
|
42
|
+
a whole, an original work of authorship. For the purposes of this License,
|
|
43
|
+
Derivative Works shall not include works that remain separable from, or
|
|
44
|
+
merely link (or bind by name) to the interfaces of, the Work and Derivative
|
|
45
|
+
Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean any work of authorship, including the original
|
|
48
|
+
version of the Work and any modifications or additions to that Work or
|
|
49
|
+
Derivative Works thereof, that is intentionally submitted to Licensor for
|
|
50
|
+
inclusion in the Work by the copyright owner or by an individual or Legal
|
|
51
|
+
Entity authorized to submit on behalf of the copyright owner. For the
|
|
52
|
+
purposes of this definition, "submitted" means any form of electronic,
|
|
53
|
+
verbal, or written communication sent to the Licensor or its
|
|
54
|
+
representatives, including but not limited to communication on electronic
|
|
55
|
+
mailing lists, source code control systems, and issue tracking systems that
|
|
56
|
+
are managed by, or on behalf of, the Licensor for the purpose of discussing
|
|
57
|
+
and improving the Work, but excluding communication that is conspicuously
|
|
58
|
+
marked or otherwise designated in writing by the copyright owner as "Not a
|
|
59
|
+
Contribution."
|
|
60
|
+
|
|
61
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on
|
|
62
|
+
behalf of whom a Contribution has been received by Licensor and
|
|
63
|
+
subsequently incorporated within the Work.
|
|
64
|
+
|
|
65
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
66
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
67
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
68
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
69
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
70
|
+
Object form.
|
|
71
|
+
|
|
72
|
+
3. Grant of Patent License. Subject to the terms and conditions of this
|
|
73
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
74
|
+
non-exclusive, no-charge, royalty-free, irrevocable (except as stated in
|
|
75
|
+
this section) patent license to make, have made, use, offer to sell, sell,
|
|
76
|
+
import, and otherwise transfer the Work, where such license applies only to
|
|
77
|
+
those patent claims licensable by such Contributor that are necessarily
|
|
78
|
+
infringed by their Contribution(s) alone or by combination of their
|
|
79
|
+
Contribution(s) with the Work to which such Contribution(s) was submitted.
|
|
80
|
+
If You institute patent litigation against any entity (including a
|
|
81
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
|
82
|
+
Contribution incorporated within the Work constitutes direct or
|
|
83
|
+
contributory patent infringement, then any patent licenses granted to You
|
|
84
|
+
under this License for that Work shall terminate as of the date such
|
|
85
|
+
litigation is filed.
|
|
86
|
+
|
|
87
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
88
|
+
Derivative Works thereof in any medium, with or without modifications, and
|
|
89
|
+
in Source or Object form, provided that You meet the following conditions:
|
|
90
|
+
|
|
91
|
+
(a) You must give any other recipients of the Work or Derivative Works
|
|
92
|
+
a copy of this License; and
|
|
93
|
+
|
|
94
|
+
(b) You must cause any modified files to carry prominent notices
|
|
95
|
+
stating that You changed the files; and
|
|
96
|
+
|
|
97
|
+
(c) You must retain, in the Source form of any Derivative Works that
|
|
98
|
+
You distribute, all copyright, patent, trademark, and attribution notices
|
|
99
|
+
from the Source form of the Work, excluding those notices that do not
|
|
100
|
+
pertain to any part of the Derivative Works; and
|
|
101
|
+
|
|
102
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
103
|
+
distribution, then any Derivative Works that You distribute must include a
|
|
104
|
+
readable copy of the attribution notices contained within such NOTICE file,
|
|
105
|
+
excluding those notices that do not pertain to any part of the Derivative
|
|
106
|
+
Works, in at least one of the following places: within a NOTICE text file
|
|
107
|
+
distributed as part of the Derivative Works; within the Source form or
|
|
108
|
+
documentation, if provided along with the Derivative Works; or, within a
|
|
109
|
+
display generated by the Derivative Works, if and wherever such third-party
|
|
110
|
+
notices normally appear. The contents of the NOTICE file are for
|
|
111
|
+
informational purposes only and do not modify the License. You may add Your
|
|
112
|
+
own attribution notices within Derivative Works that You distribute,
|
|
113
|
+
alongside or as an addendum to the NOTICE text from the Work, provided that
|
|
114
|
+
such additional attribution notices cannot be construed as modifying the
|
|
115
|
+
License.
|
|
116
|
+
|
|
117
|
+
You may add Your own copyright statement to Your modifications and may
|
|
118
|
+
provide additional or different license terms and conditions for use,
|
|
119
|
+
reproduction, or distribution of Your modifications, or for any such
|
|
120
|
+
Derivative Works as a whole, provided Your use, reproduction, and
|
|
121
|
+
distribution of the Work otherwise complies with the conditions stated in
|
|
122
|
+
this License.
|
|
123
|
+
|
|
124
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
125
|
+
Contribution intentionally submitted for inclusion in the Work by You to
|
|
126
|
+
the Licensor shall be under the terms and conditions of this License,
|
|
127
|
+
without any additional terms or conditions. Notwithstanding the above,
|
|
128
|
+
nothing herein shall supersede or modify the terms of any separate license
|
|
129
|
+
agreement you may have executed with Licensor regarding such Contributions.
|
|
130
|
+
|
|
131
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
132
|
+
names, trademarks, service marks, or product names of the Licensor, except
|
|
133
|
+
as required for reasonable and customary use in describing the origin of
|
|
134
|
+
the Work and reproducing the content of the NOTICE file.
|
|
135
|
+
|
|
136
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to
|
|
137
|
+
in writing, Licensor provides the Work (and each Contributor provides its
|
|
138
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
139
|
+
KIND, either express or implied, including, without limitation, any
|
|
140
|
+
warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or
|
|
141
|
+
FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for
|
|
142
|
+
determining the appropriateness of using or redistributing the Work and
|
|
143
|
+
assume any risks associated with Your exercise of permissions under this
|
|
144
|
+
License.
|
|
145
|
+
|
|
146
|
+
8. Limitation of Liability. In no event and under no legal theory, whether
|
|
147
|
+
in tort (including negligence), contract, or otherwise, unless required by
|
|
148
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to
|
|
149
|
+
in writing, shall any Contributor be liable to You for damages, including
|
|
150
|
+
any direct, indirect, special, incidental, or consequential damages of any
|
|
151
|
+
character arising as a result of this License or out of the use or
|
|
152
|
+
inability to use the Work (including but not limited to damages for loss of
|
|
153
|
+
goodwill, work stoppage, computer failure or malfunction, or any and all
|
|
154
|
+
other commercial damages or losses), even if such Contributor has been
|
|
155
|
+
advised of the possibility of such damages.
|
|
156
|
+
|
|
157
|
+
9. Accepting Warranty or Additional Liability. While redistributing the
|
|
158
|
+
Work or Derivative Works thereof, You may choose to offer, and charge a fee
|
|
159
|
+
for, acceptance of support, warranty, indemnity, or other liability
|
|
160
|
+
obligations and/or rights consistent with this License. However, in
|
|
161
|
+
accepting such obligations, You may act only on Your own behalf and on Your
|
|
162
|
+
sole responsibility, not on behalf of any other Contributor, and only if
|
|
163
|
+
You agree to indemnify, defend, and hold each Contributor harmless for any
|
|
164
|
+
liability incurred by, or claims asserted against, such Contributor by
|
|
165
|
+
reason of your accepting any such warranty or additional liability.
|
|
166
|
+
|
|
167
|
+
END OF TERMS AND CONDITIONS
|
|
168
|
+
|
|
169
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
170
|
+
|
|
171
|
+
To apply the Apache License to your work, attach the following boilerplate
|
|
172
|
+
notice, with the fields enclosed by brackets "[]" replaced with your own
|
|
173
|
+
identifying information. (Don't include the brackets!) The text should be
|
|
174
|
+
enclosed in the appropriate comment syntax for the file format. We also
|
|
175
|
+
recommend that a file or class name and description of purpose be included
|
|
176
|
+
on the same "printed page" as the copyright notice for easier
|
|
177
|
+
identification within third-party archives.
|
|
178
|
+
|
|
179
|
+
Copyright [yyyy] [name of copyright owner]
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -280,7 +280,8 @@ var WhenStep = class {
|
|
|
280
280
|
whenSerialized() {
|
|
281
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
282
|
const serializer = this.options.serializer ?? require_serializer.defaultSerializer;
|
|
283
|
-
|
|
283
|
+
const serialized = serializer.serialize(this.message);
|
|
284
|
+
return new SnapshotStep(serialized, serializer, this.options);
|
|
284
285
|
}
|
|
285
286
|
/**
|
|
286
287
|
* Round-trip the message through a reader that models a *different version*
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["path","defaultSerializer"],"sources":["../src/diff.ts","../src/reader.ts","../src/snapshot-storage.ts","../src/contract.ts"],"sourcesContent":["/**\n * Minimal line diff for snapshot failures. The goal is a message that points\n * at *what moved* — a renamed field, a switched date format, a dropped value —\n * not a full diff engine. Lines only in the approved file are marked `-`, lines\n * only in the actual output are marked `+`, and a little surrounding context is\n * kept so the change is readable in a terminal.\n */\nexport function lineDiff(approved: string, actual: string): string {\n const a = approved.split('\\n');\n const b = actual.split('\\n');\n const lcs = longestCommonSubsequence(a, b);\n\n const out: string[] = [];\n let i = 0;\n let j = 0;\n for (const [ai, bj] of lcs) {\n while (i < ai) out.push(`- ${a[i++]}`);\n while (j < bj) out.push(`+ ${b[j++]}`);\n out.push(` ${a[i++]}`);\n j++;\n }\n while (i < a.length) out.push(`- ${a[i++]}`);\n while (j < b.length) out.push(`+ ${b[j++]}`);\n\n return out.join('\\n');\n}\n\n/** Indices `[i, j]` of lines common to both, longest such subsequence. */\nfunction longestCommonSubsequence(\n a: string[],\n b: string[],\n): Array<[number, number]> {\n const n = a.length;\n const m = b.length;\n const table: number[][] = Array.from({ length: n + 1 }, () =>\n Array.from({ length: m + 1 }, () => 0),\n );\n for (let i = n - 1; i >= 0; i--) {\n for (let j = m - 1; j >= 0; j--) {\n table[i][j] =\n a[i] === b[j]\n ? table[i + 1][j + 1] + 1\n : Math.max(table[i + 1][j], table[i][j + 1]);\n }\n }\n const pairs: Array<[number, number]> = [];\n let i = 0;\n let j = 0;\n while (i < n && j < m) {\n if (a[i] === b[j]) {\n pairs.push([i, j]);\n i++;\n j++;\n } else if (table[i + 1][j] >= table[i][j + 1]) {\n i++;\n } else {\n j++;\n }\n }\n return pairs;\n}\n","/**\n * Readers — the \"as this version\" side of a compatibility check.\n *\n * In a JVM contract library you write `whenDeserializedAs(NewType.class)` and\n * reflection does the rest. TypeScript types are erased at runtime, so there is\n * no class to hand over. Instead you describe the *reader*: the thing that\n * accepts a deserialized value and either produces a typed result or rejects\n * it. Two shapes are accepted, in order of how most TS codebases already model\n * a message version:\n *\n * 1. A **Standard Schema** (Zod ≥3.24, Valibot, ArkType, …) — anything exposing\n * the `~standard` interface. This is the recommended form: the schema is the\n * version, and it already lives next to your message type.\n * 2. A plain **parse function** `(value) => T` that throws on incompatible input.\n *\n * A reader that accepts the value proves compatibility; a reader that throws or\n * reports issues proves the versions have drifted apart.\n */\n\n/** The subset of the Standard Schema v1 interface we rely on. */\nexport interface StandardSchemaLike<Output = unknown> {\n readonly '~standard': {\n readonly version: 1;\n readonly vendor: string;\n readonly validate: (value: unknown) =>\n | StandardResult<Output>\n | Promise<StandardResult<Output>>;\n };\n}\n\ninterface StandardResult<Output> {\n value?: Output;\n issues?: ReadonlyArray<{ readonly message: string; readonly path?: unknown }>;\n}\n\n/** A bare parse function: returns the typed value or throws. */\nexport type ParseFn<Output = unknown> = (value: unknown) => Output;\n\n/** Either accepted reader form. */\nexport type Reader<Output = unknown> =\n | StandardSchemaLike<Output>\n | ParseFn<Output>;\n\nfunction isStandardSchema(reader: Reader): reader is StandardSchemaLike {\n return (\n typeof reader === 'object' &&\n reader !== null &&\n '~standard' in reader &&\n typeof (reader as StandardSchemaLike)['~standard']?.validate === 'function'\n );\n}\n\nexport interface ReadOutcome<Output = unknown> {\n ok: boolean;\n /** Present when `ok`. */\n value?: Output;\n /** Human-readable reasons the reader rejected the value. */\n issues: string[];\n}\n\n/**\n * Run a reader against a deserialized value. Never throws — a thrown parse\n * error or reported issues become `{ ok: false, issues }` so the caller can\n * build a single, legible failure message.\n */\nexport async function read<Output>(\n reader: Reader<Output>,\n value: unknown,\n): Promise<ReadOutcome<Output>> {\n if (isStandardSchema(reader)) {\n try {\n const result = await reader['~standard'].validate(value);\n if (result.issues && result.issues.length > 0) {\n return {\n ok: false,\n issues: result.issues.map(\n (issue) => formatIssue(issue.message, issue.path),\n ),\n };\n }\n return { ok: true, value: result.value as Output, issues: [] };\n } catch (error) {\n return { ok: false, issues: [errorMessage(error)] };\n }\n }\n\n try {\n const parsed = (reader as ParseFn<Output>)(value);\n return { ok: true, value: parsed, issues: [] };\n } catch (error) {\n return { ok: false, issues: [errorMessage(error)] };\n }\n}\n\nfunction formatIssue(message: string, path: unknown): string {\n if (Array.isArray(path) && path.length > 0) {\n return `${path.map(String).join('.')}: ${message}`;\n }\n return message;\n}\n\nfunction errorMessage(error: unknown): string {\n if (error instanceof Error) return error.message;\n return String(error);\n}\n","import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';\nimport path from 'node:path';\n\n/**\n * Snapshot storage.\n *\n * A snapshot is just a file you commit: the serialized shape of a message,\n * reviewed once and from then on compared on every run. There is no broker, no\n * registry, no service to start — a format change shows up in a normal diff,\n * reviewed like any other code. This mirrors the \"approved file\" convention\n * (`<name>.approved.txt`) familiar from approval testing.\n *\n * By default the file lives in a `__contracts__` directory beside the test that\n * created it, resolved from the call stack so you do not have to thread paths\n * around. Override `dir` / `path` when you keep snapshots elsewhere.\n */\n\n/** Set any of these (e.g. `AUTOTEL_CONTRACT_UPDATE=1`) to (re)write approved files. */\nconst UPDATE_ENV_VARS = [\n 'AUTOTEL_CONTRACT_UPDATE',\n 'UPDATE_CONTRACTS',\n 'UPDATE_SNAPSHOTS',\n] as const;\n\nexport function isUpdateMode(env: NodeJS.ProcessEnv = process.env): boolean {\n return UPDATE_ENV_VARS.some((name) => {\n const value = env[name];\n return value === '1' || value === 'true';\n });\n}\n\nexport interface SnapshotLocation {\n /** Directory holding the approved file. */\n dir?: string;\n /** Logical name; the file becomes `<name>.approved.txt`. */\n name: string;\n /** Fully-qualified path; when set, `dir`/`name` are ignored for placement. */\n path?: string;\n}\n\nconst DEFAULT_DIR_NAME = '__contracts__';\nconst APPROVED_SUFFIX = '.approved.txt';\n\n/** Resolve the absolute file path for a snapshot. */\nexport function resolveSnapshotPath(location: SnapshotLocation): string {\n if (location.path) {\n return path.isAbsolute(location.path)\n ? location.path\n : path.resolve(process.cwd(), location.path);\n }\n const dir = location.dir ?? defaultSnapshotDir();\n return path.join(dir, `${sanitize(location.name)}${APPROVED_SUFFIX}`);\n}\n\nfunction sanitize(name: string): string {\n // Keep it filesystem-safe while preserving readable type names.\n return name.replaceAll(/[^\\w.@-]+/g, '_');\n}\n\nexport interface ReadSnapshotResult {\n exists: boolean;\n content?: string;\n path: string;\n}\n\nexport function readSnapshot(location: SnapshotLocation): ReadSnapshotResult {\n const filePath = resolveSnapshotPath(location);\n if (!existsSync(filePath)) return { exists: false, path: filePath };\n return { exists: true, content: readFileSync(filePath, 'utf8'), path: filePath };\n}\n\nexport function writeSnapshot(\n location: SnapshotLocation,\n content: string,\n): string {\n const filePath = resolveSnapshotPath(location);\n mkdirSync(path.dirname(filePath), { recursive: true });\n writeFileSync(filePath, content, 'utf8');\n return filePath;\n}\n\n/**\n * Best-effort `__contracts__` directory beside the calling test file, found by\n * walking the stack past this module and the contract internals. Falls back to\n * `<cwd>/__contracts__` when the caller cannot be determined (e.g. bundled).\n */\nfunction defaultSnapshotDir(): string {\n const callerFile = callerOutsidePackage();\n const base = callerFile ? path.dirname(callerFile) : process.cwd();\n return path.join(base, DEFAULT_DIR_NAME);\n}\n\nfunction callerOutsidePackage(): string | undefined {\n const stack = new Error('stack probe').stack;\n if (!stack) return undefined;\n const lines = stack.split('\\n').slice(1);\n for (const line of lines) {\n const match = line.match(/\\((.*?):\\d+:\\d+\\)/) ?? line.match(/at (.*?):\\d+:\\d+/);\n const file = match?.[1];\n if (!file) continue;\n if (file.includes('node:')) continue;\n // Skip frames inside this package's own source/dist.\n if (file.includes(`${path.join('autotel-message-contract', 'src')}`)) continue;\n if (file.includes(`${path.join('autotel-message-contract', 'dist')}`)) continue;\n if (file.includes('node_modules')) continue;\n return file.startsWith('file://') ? fileUrlToPath(file) : file;\n }\n return undefined;\n}\n\nfunction fileUrlToPath(url: string): string {\n return decodeURIComponent(url.replace(/^file:\\/\\//, ''));\n}\n","/**\n * The message contract DSL.\n *\n * Every check starts from {@link messageContract} and reads as a sentence:\n *\n * ```ts\n * // Pin the serialized shape — fail when it drifts.\n * await messageContract()\n * .given(new OrderPlaced(orderId, 'Alice', placedAt))\n * .whenSerialized()\n * .thenContractIsUnchanged();\n *\n * // Prove a newer reader still reads what an older writer produced.\n * await messageContract()\n * .given(orderPlacedV1)\n * .whenDeserializedAs(OrderPlacedV2) // a Zod schema or parse fn\n * .thenBackwardCompatible((v2) => expect(v2.coupon).toBeUndefined());\n * ```\n *\n * A **snapshot check** confirms a message still serializes exactly as approved,\n * so nothing reading it downstream breaks. A **compatibility check** confirms an\n * older and a newer version can still read each other's data. Both are ordinary\n * unit tests — no broker, no registry, no running service.\n */\nimport { lineDiff } from './diff.js';\nimport { read, type Reader } from './reader.js';\nimport {\n defaultSerializer,\n type MessageSerializer,\n} from './serializer.js';\nimport {\n isUpdateMode,\n readSnapshot,\n type SnapshotLocation,\n writeSnapshot,\n} from './snapshot-storage.js';\n\n/** Thrown when a contract check fails. Message is pre-formatted for a test runner. */\nexport class ContractViolationError extends Error {\n override readonly name = 'ContractViolationError';\n constructor(message: string) {\n super(message);\n }\n}\n\nexport interface MessageContractOptions {\n /** Serializer producing the bytes you ship. Defaults to deterministic JSON. */\n serializer?: MessageSerializer<string>;\n /**\n * Where approved files live and what they are named. A bare string is used as\n * the logical name; an object gives full control over `dir`/`path`.\n */\n snapshot?: string | SnapshotLocation;\n /** Override update-mode detection (default reads env, e.g. AUTOTEL_CONTRACT_UPDATE=1). */\n update?: boolean;\n}\n\nconst SNAPSHOT_SOURCE = Symbol('autotel-message-contract.snapshot-source');\n\nexport interface ApprovedSnapshotSource {\n readonly [SNAPSHOT_SOURCE]: true;\n readonly location?: string | SnapshotLocation;\n}\n\n/**\n * Point a compatibility check at a previously approved snapshot instead of a\n * live in-memory message instance.\n */\nexport function approvedSnapshot(\n location?: string | SnapshotLocation,\n): ApprovedSnapshotSource {\n return {\n [SNAPSHOT_SOURCE]: true,\n location,\n };\n}\n\nfunction isApprovedSnapshotSource(value: unknown): value is ApprovedSnapshotSource {\n return (\n typeof value === 'object' &&\n value !== null &&\n SNAPSHOT_SOURCE in value &&\n (value as ApprovedSnapshotSource)[SNAPSHOT_SOURCE] === true\n );\n}\n\n/** Start a contract check. */\nexport function messageContract(options: MessageContractOptions = {}): GivenStep {\n return new GivenStep(options);\n}\n\nclass GivenStep {\n constructor(private readonly options: MessageContractOptions) {}\n\n /** The message under contract. */\n given<T>(message: T | ApprovedSnapshotSource): WhenStep<T> {\n return new WhenStep(message, this.options);\n }\n}\n\nclass WhenStep<T> {\n constructor(\n private readonly message: T | ApprovedSnapshotSource,\n private readonly options: MessageContractOptions,\n ) {}\n\n /** Serialize the message; the next step pins or inspects the result. */\n whenSerialized(): SnapshotStep {\n if (isApprovedSnapshotSource(this.message)) {\n throw new ContractViolationError(\n 'Cannot serialize an approved snapshot source. ' +\n 'Use .whenDeserializedAs(...) to run a compatibility check, or ' +\n 'pass a live message instance to .given(...).',\n );\n }\n const serializer = this.options.serializer ?? defaultSerializer;\n const serialized = serializer.serialize(this.message);\n return new SnapshotStep(serialized, serializer, this.options);\n }\n\n /**\n * Round-trip the message through a reader that models a *different version*\n * (a Standard Schema such as Zod/Valibot, or a parse function). The next step\n * asserts the versions stay compatible.\n */\n whenDeserializedAs<Output>(reader: Reader<Output>): CompatibilityStep<Output> {\n const serializer = this.options.serializer ?? defaultSerializer;\n return new CompatibilityStep(this.message, reader, serializer, this.options);\n }\n}\n\nclass SnapshotStep {\n constructor(\n private readonly serialized: string,\n private readonly serializer: MessageSerializer<string>,\n private readonly options: MessageContractOptions,\n ) {}\n\n /** The serialized bytes, for ad-hoc assertions outside the snapshot flow. */\n get output(): string {\n return this.serialized;\n }\n\n /**\n * Compare the serialized output against the approved snapshot. On first run\n * (or in update mode) it writes the approved file and passes; afterwards it\n * fails with a diff when the shape drifts.\n */\n thenContractIsUnchanged(snapshotName?: string): void {\n const location = this.resolveLocation(snapshotName);\n const existing = readSnapshot(location);\n const update = this.options.update ?? isUpdateMode();\n\n if (!existing.exists || update) {\n const path = writeSnapshot(location, this.serialized);\n if (!existing.exists) {\n // First run: record and pass, leaving the file to be reviewed/committed.\n return;\n }\n // Update mode: rewrite and pass even if it changed.\n void path;\n return;\n }\n\n if (existing.content !== this.serialized) {\n throw new ContractViolationError(\n `Message contract drifted from its approved snapshot.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` snapshot: ${existing.path}\\n\\n` +\n `${lineDiff(existing.content ?? '', this.serialized)}\\n\\n` +\n `If this change is intentional, re-run with AUTOTEL_CONTRACT_UPDATE=1 ` +\n `to update the approved file, then review and commit it.`,\n );\n }\n }\n\n private resolveLocation(snapshotName?: string): SnapshotLocation {\n if (snapshotName) return { name: snapshotName };\n const configured = this.options.snapshot;\n if (typeof configured === 'string') return { name: configured };\n if (configured) return configured;\n throw new ContractViolationError(\n `A snapshot name is required. Pass one to messageContract({ snapshot: 'OrderPlaced' }) ` +\n `or to thenContractIsUnchanged('OrderPlaced').`,\n );\n }\n}\n\nclass CompatibilityStep<Output> {\n constructor(\n private readonly source: unknown,\n private readonly reader: Reader<Output>,\n private readonly serializer: MessageSerializer<string>,\n private readonly options: MessageContractOptions,\n ) {}\n\n /**\n * The reader models a **newer** version; confirm it still reads what an older\n * writer produced (stored events, in-flight messages). Optionally assert on\n * the upgraded value — e.g. that a newly-added field defaults sensibly.\n */\n async thenBackwardCompatible(\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n return this.check('backward', assert);\n }\n\n /**\n * The reader models an **older** version; confirm a consumer that has not\n * upgraded yet still reads what the newer writer produces, so you can ship the\n * new shape before every reader has caught up.\n */\n async thenForwardCompatible(\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n return this.check('forward', assert);\n }\n\n private async check(\n direction: 'backward' | 'forward',\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n const serialized = this.resolveSourceSerialized();\n const deserializedSource = this.serializer.deserialize(serialized);\n const outcome = await read(this.reader, deserializedSource);\n\n if (!outcome.ok) {\n const writer = direction === 'backward' ? 'an older writer' : 'a newer writer';\n const readerLabel =\n direction === 'backward' ? 'the newer reader' : 'the older reader';\n throw new ContractViolationError(\n `Not ${direction}-compatible: ${readerLabel} rejected a message ${writer} produced.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` serialized: ${truncate(serialized)}\\n` +\n ` issues:\\n${outcome.issues.map((m) => ` - ${m}`).join('\\n')}`,\n );\n }\n\n this.assertStructuralCompatibility(\n deserializedSource,\n this.serializer.deserialize(this.serializer.serialize(outcome.value)),\n direction,\n serialized,\n );\n\n if (assert) await assert(outcome.value as Output);\n return outcome.value as Output;\n }\n\n private resolveSourceSerialized(): string {\n if (isApprovedSnapshotSource(this.source)) {\n const location = this.resolveSnapshotLocation(this.source.location);\n const existing = readSnapshot(location);\n if (!existing.exists || existing.content === undefined) {\n throw new ContractViolationError(\n `Cannot read approved snapshot for compatibility check.\\n` +\n ` snapshot: ${existing.path}\\n\\n` +\n `Record it first with .whenSerialized().thenContractIsUnchanged(), ` +\n `or point approvedSnapshot(...) at an existing file.`,\n );\n }\n return existing.content;\n }\n return this.serializer.serialize(this.source);\n }\n\n private resolveSnapshotLocation(\n location?: string | SnapshotLocation,\n ): SnapshotLocation {\n if (typeof location === 'string') return { name: location };\n if (location) return location;\n\n const configured = this.options.snapshot;\n if (typeof configured === 'string') return { name: configured };\n if (configured) return configured;\n\n throw new ContractViolationError(\n 'A snapshot location is required for approvedSnapshot(). ' +\n `Pass approvedSnapshot('OrderPlaced_v1') or configure messageContract({ snapshot: 'OrderPlaced_v1' }).`,\n );\n }\n\n private assertStructuralCompatibility(\n sourceValue: unknown,\n targetValue: unknown,\n direction: 'backward' | 'forward',\n serialized: string,\n ): void {\n const mismatches: string[] = [];\n compareSharedStructure(sourceValue, targetValue, '$', mismatches);\n\n if (mismatches.length === 0) return;\n\n throw new ContractViolationError(\n `Not ${direction}-compatible: shared fields changed meaning across versions.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` serialized: ${truncate(serialized)}\\n` +\n ` mismatches:\\n${mismatches.map((issue) => ` - ${issue}`).join('\\n')}`,\n );\n }\n}\n\nfunction truncate(value: string, max = 400): string {\n return value.length > max ? `${value.slice(0, max)}… (${value.length} chars)` : value;\n}\n\nfunction compareSharedStructure(\n sourceValue: unknown,\n targetValue: unknown,\n path: string,\n mismatches: string[],\n): void {\n if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {\n const sourceKeys = Object.keys(sourceValue);\n const targetKeys = Object.keys(targetValue);\n const sourceOnly = sourceKeys.filter((key) => !(key in targetValue));\n const targetOnly = targetKeys.filter((key) => !(key in sourceValue));\n\n if (sourceOnly.length > 0 && targetOnly.length > 0) {\n mismatches.push(\n `${path}: structural incompatibility ` +\n `[source-only: ${sourceOnly.join(', ')}, target-only: ${targetOnly.join(', ')}]`,\n );\n return;\n }\n\n for (const key of sourceKeys.filter((candidate) => candidate in targetValue).toSorted()) {\n compareSharedStructure(\n sourceValue[key],\n targetValue[key],\n joinPath(path, key),\n mismatches,\n );\n }\n return;\n }\n\n if (Array.isArray(sourceValue) && Array.isArray(targetValue)) {\n if (sourceValue.length !== targetValue.length) {\n mismatches.push(\n `${path}: array length differs (${sourceValue.length} vs ${targetValue.length})`,\n );\n return;\n }\n\n for (const [index, sourceItem] of sourceValue.entries()) {\n compareSharedStructure(sourceItem, targetValue[index], `${path}[${index}]`, mismatches);\n }\n return;\n }\n\n if (!deepEqual(sourceValue, targetValue)) {\n mismatches.push(\n `${path}: value differs (${formatValue(sourceValue)} vs ${formatValue(targetValue)})`,\n );\n }\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;\n const proto = Object.getPrototypeOf(value);\n return proto === Object.prototype || proto === null;\n}\n\nfunction deepEqual(left: unknown, right: unknown): boolean {\n if (Object.is(left, right)) return true;\n\n if (Array.isArray(left) && Array.isArray(right)) {\n return (\n left.length === right.length &&\n left.every((value, index) => deepEqual(value, right[index]))\n );\n }\n\n if (isPlainObject(left) && isPlainObject(right)) {\n const leftKeys = Object.keys(left);\n const rightKeys = Object.keys(right);\n return (\n leftKeys.length === rightKeys.length &&\n leftKeys.every((key) => key in right && deepEqual(left[key], right[key]))\n );\n }\n\n return false;\n}\n\nfunction formatValue(value: unknown): string {\n if (typeof value === 'string') return JSON.stringify(value);\n if (\n value === null ||\n typeof value === 'number' ||\n typeof value === 'boolean' ||\n value === undefined\n ) {\n return String(value);\n }\n return JSON.stringify(value);\n}\n\nfunction joinPath(path: string, key: string): string {\n return path === '$' ? `$.${key}` : `${path}.${key}`;\n}\n\nexport type {\n GivenStep,\n WhenStep,\n SnapshotStep,\n CompatibilityStep,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,SAAgB,SAAS,UAAkB,QAAwB;CACjE,MAAM,IAAI,SAAS,MAAM,IAAI;CAC7B,MAAM,IAAI,OAAO,MAAM,IAAI;CAC3B,MAAM,MAAM,yBAAyB,GAAG,CAAC;CAEzC,MAAM,MAAgB,CAAC;CACvB,IAAI,IAAI;CACR,IAAI,IAAI;CACR,KAAK,MAAM,CAAC,IAAI,OAAO,KAAK;EAC1B,OAAO,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE,MAAM;EACrC,OAAO,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE,MAAM;EACrC,IAAI,KAAK,KAAK,EAAE,MAAM;EACtB;CACF;CACA,OAAO,IAAI,EAAE,QAAQ,IAAI,KAAK,KAAK,EAAE,MAAM;CAC3C,OAAO,IAAI,EAAE,QAAQ,IAAI,KAAK,KAAK,EAAE,MAAM;CAE3C,OAAO,IAAI,KAAK,IAAI;AACtB;;AAGA,SAAS,yBACP,GACA,GACyB;CACzB,MAAM,IAAI,EAAE;CACZ,MAAM,IAAI,EAAE;CACZ,MAAM,QAAoB,MAAM,KAAK,EAAE,QAAQ,IAAI,EAAE,SACnD,MAAM,KAAK,EAAE,QAAQ,IAAI,EAAE,SAAS,CAAC,CACvC;CACA,KAAK,IAAI,IAAI,IAAI,GAAG,KAAK,GAAG,KAC1B,KAA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;AAEA,SAAS,YAAY,OAAwB;CAC3C,IAAI,OAAO,UAAU,UAAU,OAAO,KAAK,UAAU,KAAK;CAC1D,IACE,UAAU,QACV,OAAO,UAAU,YACjB,OAAO,UAAU,aACjB,UAAU,QAEV,OAAO,OAAO,KAAK;CAErB,OAAO,KAAK,UAAU,KAAK;AAC7B;AAEA,SAAS,SAAS,MAAc,KAAqB;CACnD,OAAO,SAAS,MAAM,KAAK,QAAQ,GAAG,KAAK,GAAG;AAChD"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["path","defaultSerializer"],"sources":["../src/diff.ts","../src/reader.ts","../src/snapshot-storage.ts","../src/contract.ts"],"sourcesContent":["/**\n * Minimal line diff for snapshot failures. The goal is a message that points\n * at *what moved* — a renamed field, a switched date format, a dropped value —\n * not a full diff engine. Lines only in the approved file are marked `-`, lines\n * only in the actual output are marked `+`, and a little surrounding context is\n * kept so the change is readable in a terminal.\n */\nexport function lineDiff(approved: string, actual: string): string {\n const a = approved.split('\\n');\n const b = actual.split('\\n');\n const lcs = longestCommonSubsequence(a, b);\n\n const out: string[] = [];\n let i = 0;\n let j = 0;\n for (const [ai, bj] of lcs) {\n while (i < ai) out.push(`- ${a[i++]}`);\n while (j < bj) out.push(`+ ${b[j++]}`);\n out.push(` ${a[i++]}`);\n j++;\n }\n while (i < a.length) out.push(`- ${a[i++]}`);\n while (j < b.length) out.push(`+ ${b[j++]}`);\n\n return out.join('\\n');\n}\n\n/** Indices `[i, j]` of lines common to both, longest such subsequence. */\nfunction longestCommonSubsequence(\n a: string[],\n b: string[],\n): Array<[number, number]> {\n const n = a.length;\n const m = b.length;\n const table: number[][] = Array.from({ length: n + 1 }, () =>\n Array.from({ length: m + 1 }, () => 0),\n );\n for (let i = n - 1; i >= 0; i--) {\n for (let j = m - 1; j >= 0; j--) {\n table[i][j] =\n a[i] === b[j]\n ? table[i + 1][j + 1] + 1\n : Math.max(table[i + 1][j], table[i][j + 1]);\n }\n }\n const pairs: Array<[number, number]> = [];\n let i = 0;\n let j = 0;\n while (i < n && j < m) {\n if (a[i] === b[j]) {\n pairs.push([i, j]);\n i++;\n j++;\n } else if (table[i + 1][j] >= table[i][j + 1]) {\n i++;\n } else {\n j++;\n }\n }\n return pairs;\n}\n","/**\n * Readers — the \"as this version\" side of a compatibility check.\n *\n * In a JVM contract library you write `whenDeserializedAs(NewType.class)` and\n * reflection does the rest. TypeScript types are erased at runtime, so there is\n * no class to hand over. Instead you describe the *reader*: the thing that\n * accepts a deserialized value and either produces a typed result or rejects\n * it. Two shapes are accepted, in order of how most TS codebases already model\n * a message version:\n *\n * 1. A **Standard Schema** (Zod ≥3.24, Valibot, ArkType, …) — anything exposing\n * the `~standard` interface. This is the recommended form: the schema is the\n * version, and it already lives next to your message type.\n * 2. A plain **parse function** `(value) => T` that throws on incompatible input.\n *\n * A reader that accepts the value proves compatibility; a reader that throws or\n * reports issues proves the versions have drifted apart.\n */\n\n/** The subset of the Standard Schema v1 interface we rely on. */\nexport interface StandardSchemaLike<Output = unknown> {\n readonly '~standard': {\n readonly version: 1;\n readonly vendor: string;\n readonly validate: (value: unknown) =>\n | StandardResult<Output>\n | Promise<StandardResult<Output>>;\n };\n}\n\ninterface StandardResult<Output> {\n value?: Output;\n issues?: ReadonlyArray<{ readonly message: string; readonly path?: unknown }>;\n}\n\n/** A bare parse function: returns the typed value or throws. */\nexport type ParseFn<Output = unknown> = (value: unknown) => Output;\n\n/** Either accepted reader form. */\nexport type Reader<Output = unknown> =\n | StandardSchemaLike<Output>\n | ParseFn<Output>;\n\nfunction isStandardSchema(reader: Reader): reader is StandardSchemaLike {\n return (\n typeof reader === 'object' &&\n reader !== null &&\n '~standard' in reader &&\n typeof (reader as StandardSchemaLike)['~standard']?.validate === 'function'\n );\n}\n\nexport interface ReadOutcome<Output = unknown> {\n ok: boolean;\n /** Present when `ok`. */\n value?: Output;\n /** Human-readable reasons the reader rejected the value. */\n issues: string[];\n}\n\n/**\n * Run a reader against a deserialized value. Never throws — a thrown parse\n * error or reported issues become `{ ok: false, issues }` so the caller can\n * build a single, legible failure message.\n */\nexport async function read<Output>(\n reader: Reader<Output>,\n value: unknown,\n): Promise<ReadOutcome<Output>> {\n if (isStandardSchema(reader)) {\n try {\n const result = await reader['~standard'].validate(value);\n if (result.issues && result.issues.length > 0) {\n return {\n ok: false,\n issues: result.issues.map(\n (issue) => formatIssue(issue.message, issue.path),\n ),\n };\n }\n return { ok: true, value: result.value as Output, issues: [] };\n } catch (error) {\n return { ok: false, issues: [errorMessage(error)] };\n }\n }\n\n try {\n const parsed = (reader as ParseFn<Output>)(value);\n return { ok: true, value: parsed, issues: [] };\n } catch (error) {\n return { ok: false, issues: [errorMessage(error)] };\n }\n}\n\nfunction formatIssue(message: string, path: unknown): string {\n if (Array.isArray(path) && path.length > 0) {\n return `${path.map(String).join('.')}: ${message}`;\n }\n return message;\n}\n\nfunction errorMessage(error: unknown): string {\n if (error instanceof Error) return error.message;\n return String(error);\n}\n","import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';\nimport path from 'node:path';\n\n/**\n * Snapshot storage.\n *\n * A snapshot is just a file you commit: the serialized shape of a message,\n * reviewed once and from then on compared on every run. There is no broker, no\n * registry, no service to start — a format change shows up in a normal diff,\n * reviewed like any other code. This mirrors the \"approved file\" convention\n * (`<name>.approved.txt`) familiar from approval testing.\n *\n * By default the file lives in a `__contracts__` directory beside the test that\n * created it, resolved from the call stack so you do not have to thread paths\n * around. Override `dir` / `path` when you keep snapshots elsewhere.\n */\n\n/** Set any of these (e.g. `AUTOTEL_CONTRACT_UPDATE=1`) to (re)write approved files. */\nconst UPDATE_ENV_VARS = [\n 'AUTOTEL_CONTRACT_UPDATE',\n 'UPDATE_CONTRACTS',\n 'UPDATE_SNAPSHOTS',\n] as const;\n\nexport function isUpdateMode(env: NodeJS.ProcessEnv = process.env): boolean {\n return UPDATE_ENV_VARS.some((name) => {\n const value = env[name];\n return value === '1' || value === 'true';\n });\n}\n\nexport interface SnapshotLocation {\n /** Directory holding the approved file. */\n dir?: string;\n /** Logical name; the file becomes `<name>.approved.txt`. */\n name: string;\n /** Fully-qualified path; when set, `dir`/`name` are ignored for placement. */\n path?: string;\n}\n\nconst DEFAULT_DIR_NAME = '__contracts__';\nconst APPROVED_SUFFIX = '.approved.txt';\n\n/** Resolve the absolute file path for a snapshot. */\nexport function resolveSnapshotPath(location: SnapshotLocation): string {\n if (location.path) {\n return path.isAbsolute(location.path)\n ? location.path\n : path.resolve(process.cwd(), location.path);\n }\n const dir = location.dir ?? defaultSnapshotDir();\n return path.join(dir, `${sanitize(location.name)}${APPROVED_SUFFIX}`);\n}\n\nfunction sanitize(name: string): string {\n // Keep it filesystem-safe while preserving readable type names.\n return name.replaceAll(/[^\\w.@-]+/g, '_');\n}\n\nexport interface ReadSnapshotResult {\n exists: boolean;\n content?: string;\n path: string;\n}\n\nexport function readSnapshot(location: SnapshotLocation): ReadSnapshotResult {\n const filePath = resolveSnapshotPath(location);\n if (!existsSync(filePath)) return { exists: false, path: filePath };\n return { exists: true, content: readFileSync(filePath, 'utf8'), path: filePath };\n}\n\nexport function writeSnapshot(\n location: SnapshotLocation,\n content: string,\n): string {\n const filePath = resolveSnapshotPath(location);\n mkdirSync(path.dirname(filePath), { recursive: true });\n writeFileSync(filePath, content, 'utf8');\n return filePath;\n}\n\n/**\n * Best-effort `__contracts__` directory beside the calling test file, found by\n * walking the stack past this module and the contract internals. Falls back to\n * `<cwd>/__contracts__` when the caller cannot be determined (e.g. bundled).\n */\nfunction defaultSnapshotDir(): string {\n const callerFile = callerOutsidePackage();\n const base = callerFile ? path.dirname(callerFile) : process.cwd();\n return path.join(base, DEFAULT_DIR_NAME);\n}\n\nfunction callerOutsidePackage(): string | undefined {\n const stack = new Error('stack probe').stack;\n if (!stack) return undefined;\n const lines = stack.split('\\n').slice(1);\n for (const line of lines) {\n const match = line.match(/\\((.*?):\\d+:\\d+\\)/) ?? line.match(/at (.*?):\\d+:\\d+/);\n const file = match?.[1];\n if (!file) continue;\n if (file.includes('node:')) continue;\n // Skip frames inside this package's own source/dist.\n if (file.includes(`${path.join('autotel-message-contract', 'src')}`)) continue;\n if (file.includes(`${path.join('autotel-message-contract', 'dist')}`)) continue;\n if (file.includes('node_modules')) continue;\n return file.startsWith('file://') ? fileUrlToPath(file) : file;\n }\n return undefined;\n}\n\nfunction fileUrlToPath(url: string): string {\n return decodeURIComponent(url.replace(/^file:\\/\\//, ''));\n}\n","/**\n * The message contract DSL.\n *\n * Every check starts from {@link messageContract} and reads as a sentence:\n *\n * ```ts\n * // Pin the serialized shape — fail when it drifts.\n * await messageContract()\n * .given(new OrderPlaced(orderId, 'Alice', placedAt))\n * .whenSerialized()\n * .thenContractIsUnchanged();\n *\n * // Prove a newer reader still reads what an older writer produced.\n * await messageContract()\n * .given(orderPlacedV1)\n * .whenDeserializedAs(OrderPlacedV2) // a Zod schema or parse fn\n * .thenBackwardCompatible((v2) => expect(v2.coupon).toBeUndefined());\n * ```\n *\n * A **snapshot check** confirms a message still serializes exactly as approved,\n * so nothing reading it downstream breaks. A **compatibility check** confirms an\n * older and a newer version can still read each other's data. Both are ordinary\n * unit tests — no broker, no registry, no running service.\n */\nimport { lineDiff } from './diff.js';\nimport { read, type Reader } from './reader.js';\nimport {\n defaultSerializer,\n type MessageSerializer,\n} from './serializer.js';\nimport {\n isUpdateMode,\n readSnapshot,\n type SnapshotLocation,\n writeSnapshot,\n} from './snapshot-storage.js';\n\n/** Thrown when a contract check fails. Message is pre-formatted for a test runner. */\nexport class ContractViolationError extends Error {\n override readonly name = 'ContractViolationError';\n constructor(message: string) {\n super(message);\n }\n}\n\nexport interface MessageContractOptions {\n /** Serializer producing the bytes you ship. Defaults to deterministic JSON. */\n serializer?: MessageSerializer<string>;\n /**\n * Where approved files live and what they are named. A bare string is used as\n * the logical name; an object gives full control over `dir`/`path`.\n */\n snapshot?: string | SnapshotLocation;\n /** Override update-mode detection (default reads env, e.g. AUTOTEL_CONTRACT_UPDATE=1). */\n update?: boolean;\n}\n\nconst SNAPSHOT_SOURCE = Symbol('autotel-message-contract.snapshot-source');\n\nexport interface ApprovedSnapshotSource {\n readonly [SNAPSHOT_SOURCE]: true;\n readonly location?: string | SnapshotLocation;\n}\n\n/**\n * Point a compatibility check at a previously approved snapshot instead of a\n * live in-memory message instance.\n */\nexport function approvedSnapshot(\n location?: string | SnapshotLocation,\n): ApprovedSnapshotSource {\n return {\n [SNAPSHOT_SOURCE]: true,\n location,\n };\n}\n\nfunction isApprovedSnapshotSource(value: unknown): value is ApprovedSnapshotSource {\n return (\n typeof value === 'object' &&\n value !== null &&\n SNAPSHOT_SOURCE in value &&\n (value as ApprovedSnapshotSource)[SNAPSHOT_SOURCE] === true\n );\n}\n\n/** Start a contract check. */\nexport function messageContract(options: MessageContractOptions = {}): GivenStep {\n return new GivenStep(options);\n}\n\nclass GivenStep {\n constructor(private readonly options: MessageContractOptions) {}\n\n /** The message under contract. */\n given<T>(message: T | ApprovedSnapshotSource): WhenStep<T> {\n return new WhenStep(message, this.options);\n }\n}\n\nclass WhenStep<T> {\n constructor(\n private readonly message: T | ApprovedSnapshotSource,\n private readonly options: MessageContractOptions,\n ) {}\n\n /** Serialize the message; the next step pins or inspects the result. */\n whenSerialized(): SnapshotStep {\n if (isApprovedSnapshotSource(this.message)) {\n throw new ContractViolationError(\n 'Cannot serialize an approved snapshot source. ' +\n 'Use .whenDeserializedAs(...) to run a compatibility check, or ' +\n 'pass a live message instance to .given(...).',\n );\n }\n const serializer = this.options.serializer ?? defaultSerializer;\n const serialized = serializer.serialize(this.message);\n return new SnapshotStep(serialized, serializer, this.options);\n }\n\n /**\n * Round-trip the message through a reader that models a *different version*\n * (a Standard Schema such as Zod/Valibot, or a parse function). The next step\n * asserts the versions stay compatible.\n */\n whenDeserializedAs<Output>(reader: Reader<Output>): CompatibilityStep<Output> {\n const serializer = this.options.serializer ?? defaultSerializer;\n return new CompatibilityStep(this.message, reader, serializer, this.options);\n }\n}\n\nclass SnapshotStep {\n constructor(\n private readonly serialized: string,\n private readonly serializer: MessageSerializer<string>,\n private readonly options: MessageContractOptions,\n ) {}\n\n /** The serialized bytes, for ad-hoc assertions outside the snapshot flow. */\n get output(): string {\n return this.serialized;\n }\n\n /**\n * Compare the serialized output against the approved snapshot. On first run\n * (or in update mode) it writes the approved file and passes; afterwards it\n * fails with a diff when the shape drifts.\n */\n thenContractIsUnchanged(snapshotName?: string): void {\n const location = this.resolveLocation(snapshotName);\n const existing = readSnapshot(location);\n const update = this.options.update ?? isUpdateMode();\n\n if (!existing.exists || update) {\n const path = writeSnapshot(location, this.serialized);\n if (!existing.exists) {\n // First run: record and pass, leaving the file to be reviewed/committed.\n return;\n }\n // Update mode: rewrite and pass even if it changed.\n void path;\n return;\n }\n\n if (existing.content !== this.serialized) {\n throw new ContractViolationError(\n `Message contract drifted from its approved snapshot.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` snapshot: ${existing.path}\\n\\n` +\n `${lineDiff(existing.content ?? '', this.serialized)}\\n\\n` +\n `If this change is intentional, re-run with AUTOTEL_CONTRACT_UPDATE=1 ` +\n `to update the approved file, then review and commit it.`,\n );\n }\n }\n\n private resolveLocation(snapshotName?: string): SnapshotLocation {\n if (snapshotName) return { name: snapshotName };\n const configured = this.options.snapshot;\n if (typeof configured === 'string') return { name: configured };\n if (configured) return configured;\n throw new ContractViolationError(\n `A snapshot name is required. Pass one to messageContract({ snapshot: 'OrderPlaced' }) ` +\n `or to thenContractIsUnchanged('OrderPlaced').`,\n );\n }\n}\n\nclass CompatibilityStep<Output> {\n constructor(\n private readonly source: unknown,\n private readonly reader: Reader<Output>,\n private readonly serializer: MessageSerializer<string>,\n private readonly options: MessageContractOptions,\n ) {}\n\n /**\n * The reader models a **newer** version; confirm it still reads what an older\n * writer produced (stored events, in-flight messages). Optionally assert on\n * the upgraded value — e.g. that a newly-added field defaults sensibly.\n */\n async thenBackwardCompatible(\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n return this.check('backward', assert);\n }\n\n /**\n * The reader models an **older** version; confirm a consumer that has not\n * upgraded yet still reads what the newer writer produces, so you can ship the\n * new shape before every reader has caught up.\n */\n async thenForwardCompatible(\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n return this.check('forward', assert);\n }\n\n private async check(\n direction: 'backward' | 'forward',\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n const serialized = this.resolveSourceSerialized();\n const deserializedSource = this.serializer.deserialize(serialized);\n const outcome = await read(this.reader, deserializedSource);\n\n if (!outcome.ok) {\n const writer = direction === 'backward' ? 'an older writer' : 'a newer writer';\n const readerLabel =\n direction === 'backward' ? 'the newer reader' : 'the older reader';\n throw new ContractViolationError(\n `Not ${direction}-compatible: ${readerLabel} rejected a message ${writer} produced.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` serialized: ${truncate(serialized)}\\n` +\n ` issues:\\n${outcome.issues.map((m) => ` - ${m}`).join('\\n')}`,\n );\n }\n\n this.assertStructuralCompatibility(\n deserializedSource,\n this.serializer.deserialize(this.serializer.serialize(outcome.value)),\n direction,\n serialized,\n );\n\n if (assert) await assert(outcome.value as Output);\n return outcome.value as Output;\n }\n\n private resolveSourceSerialized(): string {\n if (isApprovedSnapshotSource(this.source)) {\n const location = this.resolveSnapshotLocation(this.source.location);\n const existing = readSnapshot(location);\n if (!existing.exists || existing.content === undefined) {\n throw new ContractViolationError(\n `Cannot read approved snapshot for compatibility check.\\n` +\n ` snapshot: ${existing.path}\\n\\n` +\n `Record it first with .whenSerialized().thenContractIsUnchanged(), ` +\n `or point approvedSnapshot(...) at an existing file.`,\n );\n }\n return existing.content;\n }\n return this.serializer.serialize(this.source);\n }\n\n private resolveSnapshotLocation(\n location?: string | SnapshotLocation,\n ): SnapshotLocation {\n if (typeof location === 'string') return { name: location };\n if (location) return location;\n\n const configured = this.options.snapshot;\n if (typeof configured === 'string') return { name: configured };\n if (configured) return configured;\n\n throw new ContractViolationError(\n 'A snapshot location is required for approvedSnapshot(). ' +\n `Pass approvedSnapshot('OrderPlaced_v1') or configure messageContract({ snapshot: 'OrderPlaced_v1' }).`,\n );\n }\n\n private assertStructuralCompatibility(\n sourceValue: unknown,\n targetValue: unknown,\n direction: 'backward' | 'forward',\n serialized: string,\n ): void {\n const mismatches: string[] = [];\n compareSharedStructure(sourceValue, targetValue, '$', mismatches);\n\n if (mismatches.length === 0) return;\n\n throw new ContractViolationError(\n `Not ${direction}-compatible: shared fields changed meaning across versions.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` serialized: ${truncate(serialized)}\\n` +\n ` mismatches:\\n${mismatches.map((issue) => ` - ${issue}`).join('\\n')}`,\n );\n }\n}\n\nfunction truncate(value: string, max = 400): string {\n return value.length > max ? `${value.slice(0, max)}… (${value.length} chars)` : value;\n}\n\nfunction compareSharedStructure(\n sourceValue: unknown,\n targetValue: unknown,\n path: string,\n mismatches: string[],\n): void {\n if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {\n const sourceKeys = Object.keys(sourceValue);\n const targetKeys = Object.keys(targetValue);\n const sourceOnly = sourceKeys.filter((key) => !(key in targetValue));\n const targetOnly = targetKeys.filter((key) => !(key in sourceValue));\n\n if (sourceOnly.length > 0 && targetOnly.length > 0) {\n mismatches.push(\n `${path}: structural incompatibility ` +\n `[source-only: ${sourceOnly.join(', ')}, target-only: ${targetOnly.join(', ')}]`,\n );\n return;\n }\n\n for (const key of sourceKeys.filter((candidate) => candidate in targetValue).toSorted()) {\n compareSharedStructure(\n sourceValue[key],\n targetValue[key],\n joinPath(path, key),\n mismatches,\n );\n }\n return;\n }\n\n if (Array.isArray(sourceValue) && Array.isArray(targetValue)) {\n if (sourceValue.length !== targetValue.length) {\n mismatches.push(\n `${path}: array length differs (${sourceValue.length} vs ${targetValue.length})`,\n );\n return;\n }\n\n for (const [index, sourceItem] of sourceValue.entries()) {\n compareSharedStructure(sourceItem, targetValue[index], `${path}[${index}]`, mismatches);\n }\n return;\n }\n\n if (!deepEqual(sourceValue, targetValue)) {\n mismatches.push(\n `${path}: value differs (${formatValue(sourceValue)} vs ${formatValue(targetValue)})`,\n );\n }\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;\n const proto = Object.getPrototypeOf(value);\n return proto === Object.prototype || proto === null;\n}\n\nfunction deepEqual(left: unknown, right: unknown): boolean {\n if (Object.is(left, right)) return true;\n\n if (Array.isArray(left) && Array.isArray(right)) {\n return (\n left.length === right.length &&\n left.every((value, index) => deepEqual(value, right[index]))\n );\n }\n\n if (isPlainObject(left) && isPlainObject(right)) {\n const leftKeys = Object.keys(left);\n const rightKeys = Object.keys(right);\n return (\n leftKeys.length === rightKeys.length &&\n leftKeys.every((key) => key in right && deepEqual(left[key], right[key]))\n );\n }\n\n return false;\n}\n\nfunction formatValue(value: unknown): string {\n if (typeof value === 'string') return JSON.stringify(value);\n if (\n value === null ||\n typeof value === 'number' ||\n typeof value === 'boolean' ||\n value === undefined\n ) {\n return String(value);\n }\n return JSON.stringify(value);\n}\n\nfunction joinPath(path: string, key: string): string {\n return path === '$' ? `$.${key}` : `${path}.${key}`;\n}\n\nexport type {\n GivenStep,\n WhenStep,\n SnapshotStep,\n CompatibilityStep,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,SAAgB,SAAS,UAAkB,QAAwB;CACjE,MAAM,IAAI,SAAS,MAAM,IAAI;CAC7B,MAAM,IAAI,OAAO,MAAM,IAAI;CAC3B,MAAM,MAAM,yBAAyB,GAAG,CAAC;CAEzC,MAAM,MAAgB,CAAC;CACvB,IAAI,IAAI;CACR,IAAI,IAAI;CACR,KAAK,MAAM,CAAC,IAAI,OAAO,KAAK;EAC1B,OAAO,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE,MAAM;EACrC,OAAO,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE,MAAM;EACrC,IAAI,KAAK,KAAK,EAAE,MAAM;EACtB;CACF;CACA,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GAAG,MAAM,KAAK,GAAG,OAAO;CAEnC,IAAI,MAAM,QAAQ,IAAI,KAAK,MAAM,QAAQ,KAAK,GAC5C,OACE,KAAK,WAAW,MAAM,UACtB,KAAK,OAAO,OAAO,UAAU,UAAU,OAAO,MAAM,MAAM,CAAC;CAI/D,IAAI,cAAc,IAAI,KAAK,cAAc,KAAK,GAAG;EAC/C,MAAM,WAAW,OAAO,KAAK,IAAI;EACjC,MAAM,YAAY,OAAO,KAAK,KAAK;EACnC,OACE,SAAS,WAAW,UAAU,UAC9B,SAAS,OAAO,QAAQ,OAAO,SAAS,UAAU,KAAK,MAAM,MAAM,IAAI,CAAC;CAE5E;CAEA,OAAO;AACT;AAEA,SAAS,YAAY,OAAwB;CAC3C,IAAI,OAAO,UAAU,UAAU,OAAO,KAAK,UAAU,KAAK;CAC1D,IACE,UAAU,QACV,OAAO,UAAU,YACjB,OAAO,UAAU,aACjB,UAAU,QAEV,OAAO,OAAO,KAAK;CAErB,OAAO,KAAK,UAAU,KAAK;AAC7B;AAEA,SAAS,SAAS,MAAc,KAAqB;CACnD,OAAO,SAAS,MAAM,KAAK,QAAQ,GAAG,KAAK,GAAG;AAChD"}
|
package/dist/index.js
CHANGED
|
@@ -251,7 +251,8 @@ var WhenStep = class {
|
|
|
251
251
|
whenSerialized() {
|
|
252
252
|
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(...).");
|
|
253
253
|
const serializer = this.options.serializer ?? defaultSerializer;
|
|
254
|
-
|
|
254
|
+
const serialized = serializer.serialize(this.message);
|
|
255
|
+
return new SnapshotStep(serialized, serializer, this.options);
|
|
255
256
|
}
|
|
256
257
|
/**
|
|
257
258
|
* Round-trip the message through a reader that models a *different version*
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/diff.ts","../src/reader.ts","../src/snapshot-storage.ts","../src/contract.ts"],"sourcesContent":["/**\n * Minimal line diff for snapshot failures. The goal is a message that points\n * at *what moved* — a renamed field, a switched date format, a dropped value —\n * not a full diff engine. Lines only in the approved file are marked `-`, lines\n * only in the actual output are marked `+`, and a little surrounding context is\n * kept so the change is readable in a terminal.\n */\nexport function lineDiff(approved: string, actual: string): string {\n const a = approved.split('\\n');\n const b = actual.split('\\n');\n const lcs = longestCommonSubsequence(a, b);\n\n const out: string[] = [];\n let i = 0;\n let j = 0;\n for (const [ai, bj] of lcs) {\n while (i < ai) out.push(`- ${a[i++]}`);\n while (j < bj) out.push(`+ ${b[j++]}`);\n out.push(` ${a[i++]}`);\n j++;\n }\n while (i < a.length) out.push(`- ${a[i++]}`);\n while (j < b.length) out.push(`+ ${b[j++]}`);\n\n return out.join('\\n');\n}\n\n/** Indices `[i, j]` of lines common to both, longest such subsequence. */\nfunction longestCommonSubsequence(\n a: string[],\n b: string[],\n): Array<[number, number]> {\n const n = a.length;\n const m = b.length;\n const table: number[][] = Array.from({ length: n + 1 }, () =>\n Array.from({ length: m + 1 }, () => 0),\n );\n for (let i = n - 1; i >= 0; i--) {\n for (let j = m - 1; j >= 0; j--) {\n table[i][j] =\n a[i] === b[j]\n ? table[i + 1][j + 1] + 1\n : Math.max(table[i + 1][j], table[i][j + 1]);\n }\n }\n const pairs: Array<[number, number]> = [];\n let i = 0;\n let j = 0;\n while (i < n && j < m) {\n if (a[i] === b[j]) {\n pairs.push([i, j]);\n i++;\n j++;\n } else if (table[i + 1][j] >= table[i][j + 1]) {\n i++;\n } else {\n j++;\n }\n }\n return pairs;\n}\n","/**\n * Readers — the \"as this version\" side of a compatibility check.\n *\n * In a JVM contract library you write `whenDeserializedAs(NewType.class)` and\n * reflection does the rest. TypeScript types are erased at runtime, so there is\n * no class to hand over. Instead you describe the *reader*: the thing that\n * accepts a deserialized value and either produces a typed result or rejects\n * it. Two shapes are accepted, in order of how most TS codebases already model\n * a message version:\n *\n * 1. A **Standard Schema** (Zod ≥3.24, Valibot, ArkType, …) — anything exposing\n * the `~standard` interface. This is the recommended form: the schema is the\n * version, and it already lives next to your message type.\n * 2. A plain **parse function** `(value) => T` that throws on incompatible input.\n *\n * A reader that accepts the value proves compatibility; a reader that throws or\n * reports issues proves the versions have drifted apart.\n */\n\n/** The subset of the Standard Schema v1 interface we rely on. */\nexport interface StandardSchemaLike<Output = unknown> {\n readonly '~standard': {\n readonly version: 1;\n readonly vendor: string;\n readonly validate: (value: unknown) =>\n | StandardResult<Output>\n | Promise<StandardResult<Output>>;\n };\n}\n\ninterface StandardResult<Output> {\n value?: Output;\n issues?: ReadonlyArray<{ readonly message: string; readonly path?: unknown }>;\n}\n\n/** A bare parse function: returns the typed value or throws. */\nexport type ParseFn<Output = unknown> = (value: unknown) => Output;\n\n/** Either accepted reader form. */\nexport type Reader<Output = unknown> =\n | StandardSchemaLike<Output>\n | ParseFn<Output>;\n\nfunction isStandardSchema(reader: Reader): reader is StandardSchemaLike {\n return (\n typeof reader === 'object' &&\n reader !== null &&\n '~standard' in reader &&\n typeof (reader as StandardSchemaLike)['~standard']?.validate === 'function'\n );\n}\n\nexport interface ReadOutcome<Output = unknown> {\n ok: boolean;\n /** Present when `ok`. */\n value?: Output;\n /** Human-readable reasons the reader rejected the value. */\n issues: string[];\n}\n\n/**\n * Run a reader against a deserialized value. Never throws — a thrown parse\n * error or reported issues become `{ ok: false, issues }` so the caller can\n * build a single, legible failure message.\n */\nexport async function read<Output>(\n reader: Reader<Output>,\n value: unknown,\n): Promise<ReadOutcome<Output>> {\n if (isStandardSchema(reader)) {\n try {\n const result = await reader['~standard'].validate(value);\n if (result.issues && result.issues.length > 0) {\n return {\n ok: false,\n issues: result.issues.map(\n (issue) => formatIssue(issue.message, issue.path),\n ),\n };\n }\n return { ok: true, value: result.value as Output, issues: [] };\n } catch (error) {\n return { ok: false, issues: [errorMessage(error)] };\n }\n }\n\n try {\n const parsed = (reader as ParseFn<Output>)(value);\n return { ok: true, value: parsed, issues: [] };\n } catch (error) {\n return { ok: false, issues: [errorMessage(error)] };\n }\n}\n\nfunction formatIssue(message: string, path: unknown): string {\n if (Array.isArray(path) && path.length > 0) {\n return `${path.map(String).join('.')}: ${message}`;\n }\n return message;\n}\n\nfunction errorMessage(error: unknown): string {\n if (error instanceof Error) return error.message;\n return String(error);\n}\n","import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';\nimport path from 'node:path';\n\n/**\n * Snapshot storage.\n *\n * A snapshot is just a file you commit: the serialized shape of a message,\n * reviewed once and from then on compared on every run. There is no broker, no\n * registry, no service to start — a format change shows up in a normal diff,\n * reviewed like any other code. This mirrors the \"approved file\" convention\n * (`<name>.approved.txt`) familiar from approval testing.\n *\n * By default the file lives in a `__contracts__` directory beside the test that\n * created it, resolved from the call stack so you do not have to thread paths\n * around. Override `dir` / `path` when you keep snapshots elsewhere.\n */\n\n/** Set any of these (e.g. `AUTOTEL_CONTRACT_UPDATE=1`) to (re)write approved files. */\nconst UPDATE_ENV_VARS = [\n 'AUTOTEL_CONTRACT_UPDATE',\n 'UPDATE_CONTRACTS',\n 'UPDATE_SNAPSHOTS',\n] as const;\n\nexport function isUpdateMode(env: NodeJS.ProcessEnv = process.env): boolean {\n return UPDATE_ENV_VARS.some((name) => {\n const value = env[name];\n return value === '1' || value === 'true';\n });\n}\n\nexport interface SnapshotLocation {\n /** Directory holding the approved file. */\n dir?: string;\n /** Logical name; the file becomes `<name>.approved.txt`. */\n name: string;\n /** Fully-qualified path; when set, `dir`/`name` are ignored for placement. */\n path?: string;\n}\n\nconst DEFAULT_DIR_NAME = '__contracts__';\nconst APPROVED_SUFFIX = '.approved.txt';\n\n/** Resolve the absolute file path for a snapshot. */\nexport function resolveSnapshotPath(location: SnapshotLocation): string {\n if (location.path) {\n return path.isAbsolute(location.path)\n ? location.path\n : path.resolve(process.cwd(), location.path);\n }\n const dir = location.dir ?? defaultSnapshotDir();\n return path.join(dir, `${sanitize(location.name)}${APPROVED_SUFFIX}`);\n}\n\nfunction sanitize(name: string): string {\n // Keep it filesystem-safe while preserving readable type names.\n return name.replaceAll(/[^\\w.@-]+/g, '_');\n}\n\nexport interface ReadSnapshotResult {\n exists: boolean;\n content?: string;\n path: string;\n}\n\nexport function readSnapshot(location: SnapshotLocation): ReadSnapshotResult {\n const filePath = resolveSnapshotPath(location);\n if (!existsSync(filePath)) return { exists: false, path: filePath };\n return { exists: true, content: readFileSync(filePath, 'utf8'), path: filePath };\n}\n\nexport function writeSnapshot(\n location: SnapshotLocation,\n content: string,\n): string {\n const filePath = resolveSnapshotPath(location);\n mkdirSync(path.dirname(filePath), { recursive: true });\n writeFileSync(filePath, content, 'utf8');\n return filePath;\n}\n\n/**\n * Best-effort `__contracts__` directory beside the calling test file, found by\n * walking the stack past this module and the contract internals. Falls back to\n * `<cwd>/__contracts__` when the caller cannot be determined (e.g. bundled).\n */\nfunction defaultSnapshotDir(): string {\n const callerFile = callerOutsidePackage();\n const base = callerFile ? path.dirname(callerFile) : process.cwd();\n return path.join(base, DEFAULT_DIR_NAME);\n}\n\nfunction callerOutsidePackage(): string | undefined {\n const stack = new Error('stack probe').stack;\n if (!stack) return undefined;\n const lines = stack.split('\\n').slice(1);\n for (const line of lines) {\n const match = line.match(/\\((.*?):\\d+:\\d+\\)/) ?? line.match(/at (.*?):\\d+:\\d+/);\n const file = match?.[1];\n if (!file) continue;\n if (file.includes('node:')) continue;\n // Skip frames inside this package's own source/dist.\n if (file.includes(`${path.join('autotel-message-contract', 'src')}`)) continue;\n if (file.includes(`${path.join('autotel-message-contract', 'dist')}`)) continue;\n if (file.includes('node_modules')) continue;\n return file.startsWith('file://') ? fileUrlToPath(file) : file;\n }\n return undefined;\n}\n\nfunction fileUrlToPath(url: string): string {\n return decodeURIComponent(url.replace(/^file:\\/\\//, ''));\n}\n","/**\n * The message contract DSL.\n *\n * Every check starts from {@link messageContract} and reads as a sentence:\n *\n * ```ts\n * // Pin the serialized shape — fail when it drifts.\n * await messageContract()\n * .given(new OrderPlaced(orderId, 'Alice', placedAt))\n * .whenSerialized()\n * .thenContractIsUnchanged();\n *\n * // Prove a newer reader still reads what an older writer produced.\n * await messageContract()\n * .given(orderPlacedV1)\n * .whenDeserializedAs(OrderPlacedV2) // a Zod schema or parse fn\n * .thenBackwardCompatible((v2) => expect(v2.coupon).toBeUndefined());\n * ```\n *\n * A **snapshot check** confirms a message still serializes exactly as approved,\n * so nothing reading it downstream breaks. A **compatibility check** confirms an\n * older and a newer version can still read each other's data. Both are ordinary\n * unit tests — no broker, no registry, no running service.\n */\nimport { lineDiff } from './diff.js';\nimport { read, type Reader } from './reader.js';\nimport {\n defaultSerializer,\n type MessageSerializer,\n} from './serializer.js';\nimport {\n isUpdateMode,\n readSnapshot,\n type SnapshotLocation,\n writeSnapshot,\n} from './snapshot-storage.js';\n\n/** Thrown when a contract check fails. Message is pre-formatted for a test runner. */\nexport class ContractViolationError extends Error {\n override readonly name = 'ContractViolationError';\n constructor(message: string) {\n super(message);\n }\n}\n\nexport interface MessageContractOptions {\n /** Serializer producing the bytes you ship. Defaults to deterministic JSON. */\n serializer?: MessageSerializer<string>;\n /**\n * Where approved files live and what they are named. A bare string is used as\n * the logical name; an object gives full control over `dir`/`path`.\n */\n snapshot?: string | SnapshotLocation;\n /** Override update-mode detection (default reads env, e.g. AUTOTEL_CONTRACT_UPDATE=1). */\n update?: boolean;\n}\n\nconst SNAPSHOT_SOURCE = Symbol('autotel-message-contract.snapshot-source');\n\nexport interface ApprovedSnapshotSource {\n readonly [SNAPSHOT_SOURCE]: true;\n readonly location?: string | SnapshotLocation;\n}\n\n/**\n * Point a compatibility check at a previously approved snapshot instead of a\n * live in-memory message instance.\n */\nexport function approvedSnapshot(\n location?: string | SnapshotLocation,\n): ApprovedSnapshotSource {\n return {\n [SNAPSHOT_SOURCE]: true,\n location,\n };\n}\n\nfunction isApprovedSnapshotSource(value: unknown): value is ApprovedSnapshotSource {\n return (\n typeof value === 'object' &&\n value !== null &&\n SNAPSHOT_SOURCE in value &&\n (value as ApprovedSnapshotSource)[SNAPSHOT_SOURCE] === true\n );\n}\n\n/** Start a contract check. */\nexport function messageContract(options: MessageContractOptions = {}): GivenStep {\n return new GivenStep(options);\n}\n\nclass GivenStep {\n constructor(private readonly options: MessageContractOptions) {}\n\n /** The message under contract. */\n given<T>(message: T | ApprovedSnapshotSource): WhenStep<T> {\n return new WhenStep(message, this.options);\n }\n}\n\nclass WhenStep<T> {\n constructor(\n private readonly message: T | ApprovedSnapshotSource,\n private readonly options: MessageContractOptions,\n ) {}\n\n /** Serialize the message; the next step pins or inspects the result. */\n whenSerialized(): SnapshotStep {\n if (isApprovedSnapshotSource(this.message)) {\n throw new ContractViolationError(\n 'Cannot serialize an approved snapshot source. ' +\n 'Use .whenDeserializedAs(...) to run a compatibility check, or ' +\n 'pass a live message instance to .given(...).',\n );\n }\n const serializer = this.options.serializer ?? defaultSerializer;\n const serialized = serializer.serialize(this.message);\n return new SnapshotStep(serialized, serializer, this.options);\n }\n\n /**\n * Round-trip the message through a reader that models a *different version*\n * (a Standard Schema such as Zod/Valibot, or a parse function). The next step\n * asserts the versions stay compatible.\n */\n whenDeserializedAs<Output>(reader: Reader<Output>): CompatibilityStep<Output> {\n const serializer = this.options.serializer ?? defaultSerializer;\n return new CompatibilityStep(this.message, reader, serializer, this.options);\n }\n}\n\nclass SnapshotStep {\n constructor(\n private readonly serialized: string,\n private readonly serializer: MessageSerializer<string>,\n private readonly options: MessageContractOptions,\n ) {}\n\n /** The serialized bytes, for ad-hoc assertions outside the snapshot flow. */\n get output(): string {\n return this.serialized;\n }\n\n /**\n * Compare the serialized output against the approved snapshot. On first run\n * (or in update mode) it writes the approved file and passes; afterwards it\n * fails with a diff when the shape drifts.\n */\n thenContractIsUnchanged(snapshotName?: string): void {\n const location = this.resolveLocation(snapshotName);\n const existing = readSnapshot(location);\n const update = this.options.update ?? isUpdateMode();\n\n if (!existing.exists || update) {\n const path = writeSnapshot(location, this.serialized);\n if (!existing.exists) {\n // First run: record and pass, leaving the file to be reviewed/committed.\n return;\n }\n // Update mode: rewrite and pass even if it changed.\n void path;\n return;\n }\n\n if (existing.content !== this.serialized) {\n throw new ContractViolationError(\n `Message contract drifted from its approved snapshot.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` snapshot: ${existing.path}\\n\\n` +\n `${lineDiff(existing.content ?? '', this.serialized)}\\n\\n` +\n `If this change is intentional, re-run with AUTOTEL_CONTRACT_UPDATE=1 ` +\n `to update the approved file, then review and commit it.`,\n );\n }\n }\n\n private resolveLocation(snapshotName?: string): SnapshotLocation {\n if (snapshotName) return { name: snapshotName };\n const configured = this.options.snapshot;\n if (typeof configured === 'string') return { name: configured };\n if (configured) return configured;\n throw new ContractViolationError(\n `A snapshot name is required. Pass one to messageContract({ snapshot: 'OrderPlaced' }) ` +\n `or to thenContractIsUnchanged('OrderPlaced').`,\n );\n }\n}\n\nclass CompatibilityStep<Output> {\n constructor(\n private readonly source: unknown,\n private readonly reader: Reader<Output>,\n private readonly serializer: MessageSerializer<string>,\n private readonly options: MessageContractOptions,\n ) {}\n\n /**\n * The reader models a **newer** version; confirm it still reads what an older\n * writer produced (stored events, in-flight messages). Optionally assert on\n * the upgraded value — e.g. that a newly-added field defaults sensibly.\n */\n async thenBackwardCompatible(\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n return this.check('backward', assert);\n }\n\n /**\n * The reader models an **older** version; confirm a consumer that has not\n * upgraded yet still reads what the newer writer produces, so you can ship the\n * new shape before every reader has caught up.\n */\n async thenForwardCompatible(\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n return this.check('forward', assert);\n }\n\n private async check(\n direction: 'backward' | 'forward',\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n const serialized = this.resolveSourceSerialized();\n const deserializedSource = this.serializer.deserialize(serialized);\n const outcome = await read(this.reader, deserializedSource);\n\n if (!outcome.ok) {\n const writer = direction === 'backward' ? 'an older writer' : 'a newer writer';\n const readerLabel =\n direction === 'backward' ? 'the newer reader' : 'the older reader';\n throw new ContractViolationError(\n `Not ${direction}-compatible: ${readerLabel} rejected a message ${writer} produced.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` serialized: ${truncate(serialized)}\\n` +\n ` issues:\\n${outcome.issues.map((m) => ` - ${m}`).join('\\n')}`,\n );\n }\n\n this.assertStructuralCompatibility(\n deserializedSource,\n this.serializer.deserialize(this.serializer.serialize(outcome.value)),\n direction,\n serialized,\n );\n\n if (assert) await assert(outcome.value as Output);\n return outcome.value as Output;\n }\n\n private resolveSourceSerialized(): string {\n if (isApprovedSnapshotSource(this.source)) {\n const location = this.resolveSnapshotLocation(this.source.location);\n const existing = readSnapshot(location);\n if (!existing.exists || existing.content === undefined) {\n throw new ContractViolationError(\n `Cannot read approved snapshot for compatibility check.\\n` +\n ` snapshot: ${existing.path}\\n\\n` +\n `Record it first with .whenSerialized().thenContractIsUnchanged(), ` +\n `or point approvedSnapshot(...) at an existing file.`,\n );\n }\n return existing.content;\n }\n return this.serializer.serialize(this.source);\n }\n\n private resolveSnapshotLocation(\n location?: string | SnapshotLocation,\n ): SnapshotLocation {\n if (typeof location === 'string') return { name: location };\n if (location) return location;\n\n const configured = this.options.snapshot;\n if (typeof configured === 'string') return { name: configured };\n if (configured) return configured;\n\n throw new ContractViolationError(\n 'A snapshot location is required for approvedSnapshot(). ' +\n `Pass approvedSnapshot('OrderPlaced_v1') or configure messageContract({ snapshot: 'OrderPlaced_v1' }).`,\n );\n }\n\n private assertStructuralCompatibility(\n sourceValue: unknown,\n targetValue: unknown,\n direction: 'backward' | 'forward',\n serialized: string,\n ): void {\n const mismatches: string[] = [];\n compareSharedStructure(sourceValue, targetValue, '$', mismatches);\n\n if (mismatches.length === 0) return;\n\n throw new ContractViolationError(\n `Not ${direction}-compatible: shared fields changed meaning across versions.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` serialized: ${truncate(serialized)}\\n` +\n ` mismatches:\\n${mismatches.map((issue) => ` - ${issue}`).join('\\n')}`,\n );\n }\n}\n\nfunction truncate(value: string, max = 400): string {\n return value.length > max ? `${value.slice(0, max)}… (${value.length} chars)` : value;\n}\n\nfunction compareSharedStructure(\n sourceValue: unknown,\n targetValue: unknown,\n path: string,\n mismatches: string[],\n): void {\n if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {\n const sourceKeys = Object.keys(sourceValue);\n const targetKeys = Object.keys(targetValue);\n const sourceOnly = sourceKeys.filter((key) => !(key in targetValue));\n const targetOnly = targetKeys.filter((key) => !(key in sourceValue));\n\n if (sourceOnly.length > 0 && targetOnly.length > 0) {\n mismatches.push(\n `${path}: structural incompatibility ` +\n `[source-only: ${sourceOnly.join(', ')}, target-only: ${targetOnly.join(', ')}]`,\n );\n return;\n }\n\n for (const key of sourceKeys.filter((candidate) => candidate in targetValue).toSorted()) {\n compareSharedStructure(\n sourceValue[key],\n targetValue[key],\n joinPath(path, key),\n mismatches,\n );\n }\n return;\n }\n\n if (Array.isArray(sourceValue) && Array.isArray(targetValue)) {\n if (sourceValue.length !== targetValue.length) {\n mismatches.push(\n `${path}: array length differs (${sourceValue.length} vs ${targetValue.length})`,\n );\n return;\n }\n\n for (const [index, sourceItem] of sourceValue.entries()) {\n compareSharedStructure(sourceItem, targetValue[index], `${path}[${index}]`, mismatches);\n }\n return;\n }\n\n if (!deepEqual(sourceValue, targetValue)) {\n mismatches.push(\n `${path}: value differs (${formatValue(sourceValue)} vs ${formatValue(targetValue)})`,\n );\n }\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;\n const proto = Object.getPrototypeOf(value);\n return proto === Object.prototype || proto === null;\n}\n\nfunction deepEqual(left: unknown, right: unknown): boolean {\n if (Object.is(left, right)) return true;\n\n if (Array.isArray(left) && Array.isArray(right)) {\n return (\n left.length === right.length &&\n left.every((value, index) => deepEqual(value, right[index]))\n );\n }\n\n if (isPlainObject(left) && isPlainObject(right)) {\n const leftKeys = Object.keys(left);\n const rightKeys = Object.keys(right);\n return (\n leftKeys.length === rightKeys.length &&\n leftKeys.every((key) => key in right && deepEqual(left[key], right[key]))\n );\n }\n\n return false;\n}\n\nfunction formatValue(value: unknown): string {\n if (typeof value === 'string') return JSON.stringify(value);\n if (\n value === null ||\n typeof value === 'number' ||\n typeof value === 'boolean' ||\n value === undefined\n ) {\n return String(value);\n }\n return JSON.stringify(value);\n}\n\nfunction joinPath(path: string, key: string): string {\n return path === '$' ? `$.${key}` : `${path}.${key}`;\n}\n\nexport type {\n GivenStep,\n WhenStep,\n SnapshotStep,\n CompatibilityStep,\n};\n"],"mappings":";;;;;;;;;;;;AAOA,SAAgB,SAAS,UAAkB,QAAwB;CACjE,MAAM,IAAI,SAAS,MAAM,IAAI;CAC7B,MAAM,IAAI,OAAO,MAAM,IAAI;CAC3B,MAAM,MAAM,yBAAyB,GAAG,CAAC;CAEzC,MAAM,MAAgB,CAAC;CACvB,IAAI,IAAI;CACR,IAAI,IAAI;CACR,KAAK,MAAM,CAAC,IAAI,OAAO,KAAK;EAC1B,OAAO,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE,MAAM;EACrC,OAAO,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE,MAAM;EACrC,IAAI,KAAK,KAAK,EAAE,MAAM;EACtB;CACF;CACA,OAAO,IAAI,EAAE,QAAQ,IAAI,KAAK,KAAK,EAAE,MAAM;CAC3C,OAAO,IAAI,EAAE,QAAQ,IAAI,KAAK,KAAK,EAAE,MAAM;CAE3C,OAAO,IAAI,KAAK,IAAI;AACtB;;AAGA,SAAS,yBACP,GACA,GACyB;CACzB,MAAM,IAAI,EAAE;CACZ,MAAM,IAAI,EAAE;CACZ,MAAM,QAAoB,MAAM,KAAK,EAAE,QAAQ,IAAI,EAAE,SACnD,MAAM,KAAK,EAAE,QAAQ,IAAI,EAAE,SAAS,CAAC,CACvC;CACA,KAAK,IAAI,IAAI,IAAI,GAAG,KAAK,GAAG,KAC1B,KAAK,IAAI,IAAI,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3C,IAAI,OAAO,UAAU,UAAU,OAAO,KAAK,UAAU,KAAK;CAC1D,IACE,UAAU,QACV,OAAO,UAAU,YACjB,OAAO,UAAU,aACjB,UAAU,QAEV,OAAO,OAAO,KAAK;CAErB,OAAO,KAAK,UAAU,KAAK;AAC7B;AAEA,SAAS,SAAS,MAAc,KAAqB;CACnD,OAAO,SAAS,MAAM,KAAK,QAAQ,GAAG,KAAK,GAAG;AAChD"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/diff.ts","../src/reader.ts","../src/snapshot-storage.ts","../src/contract.ts"],"sourcesContent":["/**\n * Minimal line diff for snapshot failures. The goal is a message that points\n * at *what moved* — a renamed field, a switched date format, a dropped value —\n * not a full diff engine. Lines only in the approved file are marked `-`, lines\n * only in the actual output are marked `+`, and a little surrounding context is\n * kept so the change is readable in a terminal.\n */\nexport function lineDiff(approved: string, actual: string): string {\n const a = approved.split('\\n');\n const b = actual.split('\\n');\n const lcs = longestCommonSubsequence(a, b);\n\n const out: string[] = [];\n let i = 0;\n let j = 0;\n for (const [ai, bj] of lcs) {\n while (i < ai) out.push(`- ${a[i++]}`);\n while (j < bj) out.push(`+ ${b[j++]}`);\n out.push(` ${a[i++]}`);\n j++;\n }\n while (i < a.length) out.push(`- ${a[i++]}`);\n while (j < b.length) out.push(`+ ${b[j++]}`);\n\n return out.join('\\n');\n}\n\n/** Indices `[i, j]` of lines common to both, longest such subsequence. */\nfunction longestCommonSubsequence(\n a: string[],\n b: string[],\n): Array<[number, number]> {\n const n = a.length;\n const m = b.length;\n const table: number[][] = Array.from({ length: n + 1 }, () =>\n Array.from({ length: m + 1 }, () => 0),\n );\n for (let i = n - 1; i >= 0; i--) {\n for (let j = m - 1; j >= 0; j--) {\n table[i][j] =\n a[i] === b[j]\n ? table[i + 1][j + 1] + 1\n : Math.max(table[i + 1][j], table[i][j + 1]);\n }\n }\n const pairs: Array<[number, number]> = [];\n let i = 0;\n let j = 0;\n while (i < n && j < m) {\n if (a[i] === b[j]) {\n pairs.push([i, j]);\n i++;\n j++;\n } else if (table[i + 1][j] >= table[i][j + 1]) {\n i++;\n } else {\n j++;\n }\n }\n return pairs;\n}\n","/**\n * Readers — the \"as this version\" side of a compatibility check.\n *\n * In a JVM contract library you write `whenDeserializedAs(NewType.class)` and\n * reflection does the rest. TypeScript types are erased at runtime, so there is\n * no class to hand over. Instead you describe the *reader*: the thing that\n * accepts a deserialized value and either produces a typed result or rejects\n * it. Two shapes are accepted, in order of how most TS codebases already model\n * a message version:\n *\n * 1. A **Standard Schema** (Zod ≥3.24, Valibot, ArkType, …) — anything exposing\n * the `~standard` interface. This is the recommended form: the schema is the\n * version, and it already lives next to your message type.\n * 2. A plain **parse function** `(value) => T` that throws on incompatible input.\n *\n * A reader that accepts the value proves compatibility; a reader that throws or\n * reports issues proves the versions have drifted apart.\n */\n\n/** The subset of the Standard Schema v1 interface we rely on. */\nexport interface StandardSchemaLike<Output = unknown> {\n readonly '~standard': {\n readonly version: 1;\n readonly vendor: string;\n readonly validate: (value: unknown) =>\n | StandardResult<Output>\n | Promise<StandardResult<Output>>;\n };\n}\n\ninterface StandardResult<Output> {\n value?: Output;\n issues?: ReadonlyArray<{ readonly message: string; readonly path?: unknown }>;\n}\n\n/** A bare parse function: returns the typed value or throws. */\nexport type ParseFn<Output = unknown> = (value: unknown) => Output;\n\n/** Either accepted reader form. */\nexport type Reader<Output = unknown> =\n | StandardSchemaLike<Output>\n | ParseFn<Output>;\n\nfunction isStandardSchema(reader: Reader): reader is StandardSchemaLike {\n return (\n typeof reader === 'object' &&\n reader !== null &&\n '~standard' in reader &&\n typeof (reader as StandardSchemaLike)['~standard']?.validate === 'function'\n );\n}\n\nexport interface ReadOutcome<Output = unknown> {\n ok: boolean;\n /** Present when `ok`. */\n value?: Output;\n /** Human-readable reasons the reader rejected the value. */\n issues: string[];\n}\n\n/**\n * Run a reader against a deserialized value. Never throws — a thrown parse\n * error or reported issues become `{ ok: false, issues }` so the caller can\n * build a single, legible failure message.\n */\nexport async function read<Output>(\n reader: Reader<Output>,\n value: unknown,\n): Promise<ReadOutcome<Output>> {\n if (isStandardSchema(reader)) {\n try {\n const result = await reader['~standard'].validate(value);\n if (result.issues && result.issues.length > 0) {\n return {\n ok: false,\n issues: result.issues.map(\n (issue) => formatIssue(issue.message, issue.path),\n ),\n };\n }\n return { ok: true, value: result.value as Output, issues: [] };\n } catch (error) {\n return { ok: false, issues: [errorMessage(error)] };\n }\n }\n\n try {\n const parsed = (reader as ParseFn<Output>)(value);\n return { ok: true, value: parsed, issues: [] };\n } catch (error) {\n return { ok: false, issues: [errorMessage(error)] };\n }\n}\n\nfunction formatIssue(message: string, path: unknown): string {\n if (Array.isArray(path) && path.length > 0) {\n return `${path.map(String).join('.')}: ${message}`;\n }\n return message;\n}\n\nfunction errorMessage(error: unknown): string {\n if (error instanceof Error) return error.message;\n return String(error);\n}\n","import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';\nimport path from 'node:path';\n\n/**\n * Snapshot storage.\n *\n * A snapshot is just a file you commit: the serialized shape of a message,\n * reviewed once and from then on compared on every run. There is no broker, no\n * registry, no service to start — a format change shows up in a normal diff,\n * reviewed like any other code. This mirrors the \"approved file\" convention\n * (`<name>.approved.txt`) familiar from approval testing.\n *\n * By default the file lives in a `__contracts__` directory beside the test that\n * created it, resolved from the call stack so you do not have to thread paths\n * around. Override `dir` / `path` when you keep snapshots elsewhere.\n */\n\n/** Set any of these (e.g. `AUTOTEL_CONTRACT_UPDATE=1`) to (re)write approved files. */\nconst UPDATE_ENV_VARS = [\n 'AUTOTEL_CONTRACT_UPDATE',\n 'UPDATE_CONTRACTS',\n 'UPDATE_SNAPSHOTS',\n] as const;\n\nexport function isUpdateMode(env: NodeJS.ProcessEnv = process.env): boolean {\n return UPDATE_ENV_VARS.some((name) => {\n const value = env[name];\n return value === '1' || value === 'true';\n });\n}\n\nexport interface SnapshotLocation {\n /** Directory holding the approved file. */\n dir?: string;\n /** Logical name; the file becomes `<name>.approved.txt`. */\n name: string;\n /** Fully-qualified path; when set, `dir`/`name` are ignored for placement. */\n path?: string;\n}\n\nconst DEFAULT_DIR_NAME = '__contracts__';\nconst APPROVED_SUFFIX = '.approved.txt';\n\n/** Resolve the absolute file path for a snapshot. */\nexport function resolveSnapshotPath(location: SnapshotLocation): string {\n if (location.path) {\n return path.isAbsolute(location.path)\n ? location.path\n : path.resolve(process.cwd(), location.path);\n }\n const dir = location.dir ?? defaultSnapshotDir();\n return path.join(dir, `${sanitize(location.name)}${APPROVED_SUFFIX}`);\n}\n\nfunction sanitize(name: string): string {\n // Keep it filesystem-safe while preserving readable type names.\n return name.replaceAll(/[^\\w.@-]+/g, '_');\n}\n\nexport interface ReadSnapshotResult {\n exists: boolean;\n content?: string;\n path: string;\n}\n\nexport function readSnapshot(location: SnapshotLocation): ReadSnapshotResult {\n const filePath = resolveSnapshotPath(location);\n if (!existsSync(filePath)) return { exists: false, path: filePath };\n return { exists: true, content: readFileSync(filePath, 'utf8'), path: filePath };\n}\n\nexport function writeSnapshot(\n location: SnapshotLocation,\n content: string,\n): string {\n const filePath = resolveSnapshotPath(location);\n mkdirSync(path.dirname(filePath), { recursive: true });\n writeFileSync(filePath, content, 'utf8');\n return filePath;\n}\n\n/**\n * Best-effort `__contracts__` directory beside the calling test file, found by\n * walking the stack past this module and the contract internals. Falls back to\n * `<cwd>/__contracts__` when the caller cannot be determined (e.g. bundled).\n */\nfunction defaultSnapshotDir(): string {\n const callerFile = callerOutsidePackage();\n const base = callerFile ? path.dirname(callerFile) : process.cwd();\n return path.join(base, DEFAULT_DIR_NAME);\n}\n\nfunction callerOutsidePackage(): string | undefined {\n const stack = new Error('stack probe').stack;\n if (!stack) return undefined;\n const lines = stack.split('\\n').slice(1);\n for (const line of lines) {\n const match = line.match(/\\((.*?):\\d+:\\d+\\)/) ?? line.match(/at (.*?):\\d+:\\d+/);\n const file = match?.[1];\n if (!file) continue;\n if (file.includes('node:')) continue;\n // Skip frames inside this package's own source/dist.\n if (file.includes(`${path.join('autotel-message-contract', 'src')}`)) continue;\n if (file.includes(`${path.join('autotel-message-contract', 'dist')}`)) continue;\n if (file.includes('node_modules')) continue;\n return file.startsWith('file://') ? fileUrlToPath(file) : file;\n }\n return undefined;\n}\n\nfunction fileUrlToPath(url: string): string {\n return decodeURIComponent(url.replace(/^file:\\/\\//, ''));\n}\n","/**\n * The message contract DSL.\n *\n * Every check starts from {@link messageContract} and reads as a sentence:\n *\n * ```ts\n * // Pin the serialized shape — fail when it drifts.\n * await messageContract()\n * .given(new OrderPlaced(orderId, 'Alice', placedAt))\n * .whenSerialized()\n * .thenContractIsUnchanged();\n *\n * // Prove a newer reader still reads what an older writer produced.\n * await messageContract()\n * .given(orderPlacedV1)\n * .whenDeserializedAs(OrderPlacedV2) // a Zod schema or parse fn\n * .thenBackwardCompatible((v2) => expect(v2.coupon).toBeUndefined());\n * ```\n *\n * A **snapshot check** confirms a message still serializes exactly as approved,\n * so nothing reading it downstream breaks. A **compatibility check** confirms an\n * older and a newer version can still read each other's data. Both are ordinary\n * unit tests — no broker, no registry, no running service.\n */\nimport { lineDiff } from './diff.js';\nimport { read, type Reader } from './reader.js';\nimport {\n defaultSerializer,\n type MessageSerializer,\n} from './serializer.js';\nimport {\n isUpdateMode,\n readSnapshot,\n type SnapshotLocation,\n writeSnapshot,\n} from './snapshot-storage.js';\n\n/** Thrown when a contract check fails. Message is pre-formatted for a test runner. */\nexport class ContractViolationError extends Error {\n override readonly name = 'ContractViolationError';\n constructor(message: string) {\n super(message);\n }\n}\n\nexport interface MessageContractOptions {\n /** Serializer producing the bytes you ship. Defaults to deterministic JSON. */\n serializer?: MessageSerializer<string>;\n /**\n * Where approved files live and what they are named. A bare string is used as\n * the logical name; an object gives full control over `dir`/`path`.\n */\n snapshot?: string | SnapshotLocation;\n /** Override update-mode detection (default reads env, e.g. AUTOTEL_CONTRACT_UPDATE=1). */\n update?: boolean;\n}\n\nconst SNAPSHOT_SOURCE = Symbol('autotel-message-contract.snapshot-source');\n\nexport interface ApprovedSnapshotSource {\n readonly [SNAPSHOT_SOURCE]: true;\n readonly location?: string | SnapshotLocation;\n}\n\n/**\n * Point a compatibility check at a previously approved snapshot instead of a\n * live in-memory message instance.\n */\nexport function approvedSnapshot(\n location?: string | SnapshotLocation,\n): ApprovedSnapshotSource {\n return {\n [SNAPSHOT_SOURCE]: true,\n location,\n };\n}\n\nfunction isApprovedSnapshotSource(value: unknown): value is ApprovedSnapshotSource {\n return (\n typeof value === 'object' &&\n value !== null &&\n SNAPSHOT_SOURCE in value &&\n (value as ApprovedSnapshotSource)[SNAPSHOT_SOURCE] === true\n );\n}\n\n/** Start a contract check. */\nexport function messageContract(options: MessageContractOptions = {}): GivenStep {\n return new GivenStep(options);\n}\n\nclass GivenStep {\n constructor(private readonly options: MessageContractOptions) {}\n\n /** The message under contract. */\n given<T>(message: T | ApprovedSnapshotSource): WhenStep<T> {\n return new WhenStep(message, this.options);\n }\n}\n\nclass WhenStep<T> {\n constructor(\n private readonly message: T | ApprovedSnapshotSource,\n private readonly options: MessageContractOptions,\n ) {}\n\n /** Serialize the message; the next step pins or inspects the result. */\n whenSerialized(): SnapshotStep {\n if (isApprovedSnapshotSource(this.message)) {\n throw new ContractViolationError(\n 'Cannot serialize an approved snapshot source. ' +\n 'Use .whenDeserializedAs(...) to run a compatibility check, or ' +\n 'pass a live message instance to .given(...).',\n );\n }\n const serializer = this.options.serializer ?? defaultSerializer;\n const serialized = serializer.serialize(this.message);\n return new SnapshotStep(serialized, serializer, this.options);\n }\n\n /**\n * Round-trip the message through a reader that models a *different version*\n * (a Standard Schema such as Zod/Valibot, or a parse function). The next step\n * asserts the versions stay compatible.\n */\n whenDeserializedAs<Output>(reader: Reader<Output>): CompatibilityStep<Output> {\n const serializer = this.options.serializer ?? defaultSerializer;\n return new CompatibilityStep(this.message, reader, serializer, this.options);\n }\n}\n\nclass SnapshotStep {\n constructor(\n private readonly serialized: string,\n private readonly serializer: MessageSerializer<string>,\n private readonly options: MessageContractOptions,\n ) {}\n\n /** The serialized bytes, for ad-hoc assertions outside the snapshot flow. */\n get output(): string {\n return this.serialized;\n }\n\n /**\n * Compare the serialized output against the approved snapshot. On first run\n * (or in update mode) it writes the approved file and passes; afterwards it\n * fails with a diff when the shape drifts.\n */\n thenContractIsUnchanged(snapshotName?: string): void {\n const location = this.resolveLocation(snapshotName);\n const existing = readSnapshot(location);\n const update = this.options.update ?? isUpdateMode();\n\n if (!existing.exists || update) {\n const path = writeSnapshot(location, this.serialized);\n if (!existing.exists) {\n // First run: record and pass, leaving the file to be reviewed/committed.\n return;\n }\n // Update mode: rewrite and pass even if it changed.\n void path;\n return;\n }\n\n if (existing.content !== this.serialized) {\n throw new ContractViolationError(\n `Message contract drifted from its approved snapshot.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` snapshot: ${existing.path}\\n\\n` +\n `${lineDiff(existing.content ?? '', this.serialized)}\\n\\n` +\n `If this change is intentional, re-run with AUTOTEL_CONTRACT_UPDATE=1 ` +\n `to update the approved file, then review and commit it.`,\n );\n }\n }\n\n private resolveLocation(snapshotName?: string): SnapshotLocation {\n if (snapshotName) return { name: snapshotName };\n const configured = this.options.snapshot;\n if (typeof configured === 'string') return { name: configured };\n if (configured) return configured;\n throw new ContractViolationError(\n `A snapshot name is required. Pass one to messageContract({ snapshot: 'OrderPlaced' }) ` +\n `or to thenContractIsUnchanged('OrderPlaced').`,\n );\n }\n}\n\nclass CompatibilityStep<Output> {\n constructor(\n private readonly source: unknown,\n private readonly reader: Reader<Output>,\n private readonly serializer: MessageSerializer<string>,\n private readonly options: MessageContractOptions,\n ) {}\n\n /**\n * The reader models a **newer** version; confirm it still reads what an older\n * writer produced (stored events, in-flight messages). Optionally assert on\n * the upgraded value — e.g. that a newly-added field defaults sensibly.\n */\n async thenBackwardCompatible(\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n return this.check('backward', assert);\n }\n\n /**\n * The reader models an **older** version; confirm a consumer that has not\n * upgraded yet still reads what the newer writer produces, so you can ship the\n * new shape before every reader has caught up.\n */\n async thenForwardCompatible(\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n return this.check('forward', assert);\n }\n\n private async check(\n direction: 'backward' | 'forward',\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n const serialized = this.resolveSourceSerialized();\n const deserializedSource = this.serializer.deserialize(serialized);\n const outcome = await read(this.reader, deserializedSource);\n\n if (!outcome.ok) {\n const writer = direction === 'backward' ? 'an older writer' : 'a newer writer';\n const readerLabel =\n direction === 'backward' ? 'the newer reader' : 'the older reader';\n throw new ContractViolationError(\n `Not ${direction}-compatible: ${readerLabel} rejected a message ${writer} produced.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` serialized: ${truncate(serialized)}\\n` +\n ` issues:\\n${outcome.issues.map((m) => ` - ${m}`).join('\\n')}`,\n );\n }\n\n this.assertStructuralCompatibility(\n deserializedSource,\n this.serializer.deserialize(this.serializer.serialize(outcome.value)),\n direction,\n serialized,\n );\n\n if (assert) await assert(outcome.value as Output);\n return outcome.value as Output;\n }\n\n private resolveSourceSerialized(): string {\n if (isApprovedSnapshotSource(this.source)) {\n const location = this.resolveSnapshotLocation(this.source.location);\n const existing = readSnapshot(location);\n if (!existing.exists || existing.content === undefined) {\n throw new ContractViolationError(\n `Cannot read approved snapshot for compatibility check.\\n` +\n ` snapshot: ${existing.path}\\n\\n` +\n `Record it first with .whenSerialized().thenContractIsUnchanged(), ` +\n `or point approvedSnapshot(...) at an existing file.`,\n );\n }\n return existing.content;\n }\n return this.serializer.serialize(this.source);\n }\n\n private resolveSnapshotLocation(\n location?: string | SnapshotLocation,\n ): SnapshotLocation {\n if (typeof location === 'string') return { name: location };\n if (location) return location;\n\n const configured = this.options.snapshot;\n if (typeof configured === 'string') return { name: configured };\n if (configured) return configured;\n\n throw new ContractViolationError(\n 'A snapshot location is required for approvedSnapshot(). ' +\n `Pass approvedSnapshot('OrderPlaced_v1') or configure messageContract({ snapshot: 'OrderPlaced_v1' }).`,\n );\n }\n\n private assertStructuralCompatibility(\n sourceValue: unknown,\n targetValue: unknown,\n direction: 'backward' | 'forward',\n serialized: string,\n ): void {\n const mismatches: string[] = [];\n compareSharedStructure(sourceValue, targetValue, '$', mismatches);\n\n if (mismatches.length === 0) return;\n\n throw new ContractViolationError(\n `Not ${direction}-compatible: shared fields changed meaning across versions.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` serialized: ${truncate(serialized)}\\n` +\n ` mismatches:\\n${mismatches.map((issue) => ` - ${issue}`).join('\\n')}`,\n );\n }\n}\n\nfunction truncate(value: string, max = 400): string {\n return value.length > max ? `${value.slice(0, max)}… (${value.length} chars)` : value;\n}\n\nfunction compareSharedStructure(\n sourceValue: unknown,\n targetValue: unknown,\n path: string,\n mismatches: string[],\n): void {\n if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {\n const sourceKeys = Object.keys(sourceValue);\n const targetKeys = Object.keys(targetValue);\n const sourceOnly = sourceKeys.filter((key) => !(key in targetValue));\n const targetOnly = targetKeys.filter((key) => !(key in sourceValue));\n\n if (sourceOnly.length > 0 && targetOnly.length > 0) {\n mismatches.push(\n `${path}: structural incompatibility ` +\n `[source-only: ${sourceOnly.join(', ')}, target-only: ${targetOnly.join(', ')}]`,\n );\n return;\n }\n\n for (const key of sourceKeys.filter((candidate) => candidate in targetValue).toSorted()) {\n compareSharedStructure(\n sourceValue[key],\n targetValue[key],\n joinPath(path, key),\n mismatches,\n );\n }\n return;\n }\n\n if (Array.isArray(sourceValue) && Array.isArray(targetValue)) {\n if (sourceValue.length !== targetValue.length) {\n mismatches.push(\n `${path}: array length differs (${sourceValue.length} vs ${targetValue.length})`,\n );\n return;\n }\n\n for (const [index, sourceItem] of sourceValue.entries()) {\n compareSharedStructure(sourceItem, targetValue[index], `${path}[${index}]`, mismatches);\n }\n return;\n }\n\n if (!deepEqual(sourceValue, targetValue)) {\n mismatches.push(\n `${path}: value differs (${formatValue(sourceValue)} vs ${formatValue(targetValue)})`,\n );\n }\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;\n const proto = Object.getPrototypeOf(value);\n return proto === Object.prototype || proto === null;\n}\n\nfunction deepEqual(left: unknown, right: unknown): boolean {\n if (Object.is(left, right)) return true;\n\n if (Array.isArray(left) && Array.isArray(right)) {\n return (\n left.length === right.length &&\n left.every((value, index) => deepEqual(value, right[index]))\n );\n }\n\n if (isPlainObject(left) && isPlainObject(right)) {\n const leftKeys = Object.keys(left);\n const rightKeys = Object.keys(right);\n return (\n leftKeys.length === rightKeys.length &&\n leftKeys.every((key) => key in right && deepEqual(left[key], right[key]))\n );\n }\n\n return false;\n}\n\nfunction formatValue(value: unknown): string {\n if (typeof value === 'string') return JSON.stringify(value);\n if (\n value === null ||\n typeof value === 'number' ||\n typeof value === 'boolean' ||\n value === undefined\n ) {\n return String(value);\n }\n return JSON.stringify(value);\n}\n\nfunction joinPath(path: string, key: string): string {\n return path === '$' ? `$.${key}` : `${path}.${key}`;\n}\n\nexport type {\n GivenStep,\n WhenStep,\n SnapshotStep,\n CompatibilityStep,\n};\n"],"mappings":";;;;;;;;;;;;AAOA,SAAgB,SAAS,UAAkB,QAAwB;CACjE,MAAM,IAAI,SAAS,MAAM,IAAI;CAC7B,MAAM,IAAI,OAAO,MAAM,IAAI;CAC3B,MAAM,MAAM,yBAAyB,GAAG,CAAC;CAEzC,MAAM,MAAgB,CAAC;CACvB,IAAI,IAAI;CACR,IAAI,IAAI;CACR,KAAK,MAAM,CAAC,IAAI,OAAO,KAAK;EAC1B,OAAO,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE,MAAM;EACrC,OAAO,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE,MAAM;EACrC,IAAI,KAAK,KAAK,EAAE,MAAM;EACtB;CACF;CACA,OAAO,IAAI,EAAE,QAAQ,IAAI,KAAK,KAAK,EAAE,MAAM;CAC3C,OAAO,IAAI,EAAE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OACE,KAAK,WAAW,MAAM,UACtB,KAAK,OAAO,OAAO,UAAU,UAAU,OAAO,MAAM,MAAM,CAAC;CAI/D,IAAI,cAAc,IAAI,KAAK,cAAc,KAAK,GAAG;EAC/C,MAAM,WAAW,OAAO,KAAK,IAAI;EACjC,MAAM,YAAY,OAAO,KAAK,KAAK;EACnC,OACE,SAAS,WAAW,UAAU,UAC9B,SAAS,OAAO,QAAQ,OAAO,SAAS,UAAU,KAAK,MAAM,MAAM,IAAI,CAAC;CAE5E;CAEA,OAAO;AACT;AAEA,SAAS,YAAY,OAAwB;CAC3C,IAAI,OAAO,UAAU,UAAU,OAAO,KAAK,UAAU,KAAK;CAC1D,IACE,UAAU,QACV,OAAO,UAAU,YACjB,OAAO,UAAU,aACjB,UAAU,QAEV,OAAO,OAAO,KAAK;CAErB,OAAO,KAAK,UAAU,KAAK;AAC7B;AAEA,SAAS,SAAS,MAAc,KAAqB;CACnD,OAAO,SAAS,MAAM,KAAK,QAAQ,GAAG,KAAK,GAAG;AAChD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "autotel-message-contract",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.5",
|
|
4
4
|
"description": "Brokerless message contract testing for autotel — pin the serialized shape of your events/commands/messages and prove old and new versions stay compatible, as ordinary unit tests.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -34,9 +34,9 @@
|
|
|
34
34
|
"messages"
|
|
35
35
|
],
|
|
36
36
|
"author": "Jag Reehal <jag@jagreehal.com> (https://jagreehal.com)",
|
|
37
|
-
"license": "
|
|
37
|
+
"license": "Apache-2.0",
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"autotel": "4.2.
|
|
39
|
+
"autotel": "4.2.5"
|
|
40
40
|
},
|
|
41
41
|
"peerDependenciesMeta": {
|
|
42
42
|
"autotel": {
|
|
@@ -44,11 +44,11 @@
|
|
|
44
44
|
}
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@types/node": "^
|
|
47
|
+
"@types/node": "^26.1.0",
|
|
48
48
|
"rimraf": "^6.1.3",
|
|
49
|
-
"tsdown": "^0.22.
|
|
49
|
+
"tsdown": "^0.22.3",
|
|
50
50
|
"typescript": "^6.0.3",
|
|
51
|
-
"vitest": "^4.1.
|
|
51
|
+
"vitest": "^4.1.10"
|
|
52
52
|
},
|
|
53
53
|
"repository": {
|
|
54
54
|
"type": "git",
|