autotel-message-contract 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,409 @@
1
+ /**
2
+ * The message contract DSL.
3
+ *
4
+ * Every check starts from {@link messageContract} and reads as a sentence:
5
+ *
6
+ * ```ts
7
+ * // Pin the serialized shape — fail when it drifts.
8
+ * await messageContract()
9
+ * .given(new OrderPlaced(orderId, 'Alice', placedAt))
10
+ * .whenSerialized()
11
+ * .thenContractIsUnchanged();
12
+ *
13
+ * // Prove a newer reader still reads what an older writer produced.
14
+ * await messageContract()
15
+ * .given(orderPlacedV1)
16
+ * .whenDeserializedAs(OrderPlacedV2) // a Zod schema or parse fn
17
+ * .thenBackwardCompatible((v2) => expect(v2.coupon).toBeUndefined());
18
+ * ```
19
+ *
20
+ * A **snapshot check** confirms a message still serializes exactly as approved,
21
+ * so nothing reading it downstream breaks. A **compatibility check** confirms an
22
+ * older and a newer version can still read each other's data. Both are ordinary
23
+ * unit tests — no broker, no registry, no running service.
24
+ */
25
+ import { lineDiff } from './diff.js';
26
+ import { read, type Reader } from './reader.js';
27
+ import {
28
+ defaultSerializer,
29
+ type MessageSerializer,
30
+ } from './serializer.js';
31
+ import {
32
+ isUpdateMode,
33
+ readSnapshot,
34
+ type SnapshotLocation,
35
+ writeSnapshot,
36
+ } from './snapshot-storage.js';
37
+
38
+ /** Thrown when a contract check fails. Message is pre-formatted for a test runner. */
39
+ export class ContractViolationError extends Error {
40
+ override readonly name = 'ContractViolationError';
41
+ constructor(message: string) {
42
+ super(message);
43
+ }
44
+ }
45
+
46
+ export interface MessageContractOptions {
47
+ /** Serializer producing the bytes you ship. Defaults to deterministic JSON. */
48
+ serializer?: MessageSerializer<string>;
49
+ /**
50
+ * Where approved files live and what they are named. A bare string is used as
51
+ * the logical name; an object gives full control over `dir`/`path`.
52
+ */
53
+ snapshot?: string | SnapshotLocation;
54
+ /** Override update-mode detection (default reads env, e.g. AUTOTEL_CONTRACT_UPDATE=1). */
55
+ update?: boolean;
56
+ }
57
+
58
+ const SNAPSHOT_SOURCE = Symbol('autotel-message-contract.snapshot-source');
59
+
60
+ export interface ApprovedSnapshotSource {
61
+ readonly [SNAPSHOT_SOURCE]: true;
62
+ readonly location?: string | SnapshotLocation;
63
+ }
64
+
65
+ /**
66
+ * Point a compatibility check at a previously approved snapshot instead of a
67
+ * live in-memory message instance.
68
+ */
69
+ export function approvedSnapshot(
70
+ location?: string | SnapshotLocation,
71
+ ): ApprovedSnapshotSource {
72
+ return {
73
+ [SNAPSHOT_SOURCE]: true,
74
+ location,
75
+ };
76
+ }
77
+
78
+ function isApprovedSnapshotSource(value: unknown): value is ApprovedSnapshotSource {
79
+ return (
80
+ typeof value === 'object' &&
81
+ value !== null &&
82
+ SNAPSHOT_SOURCE in value &&
83
+ (value as ApprovedSnapshotSource)[SNAPSHOT_SOURCE] === true
84
+ );
85
+ }
86
+
87
+ /** Start a contract check. */
88
+ export function messageContract(options: MessageContractOptions = {}): GivenStep {
89
+ return new GivenStep(options);
90
+ }
91
+
92
+ class GivenStep {
93
+ constructor(private readonly options: MessageContractOptions) {}
94
+
95
+ /** The message under contract. */
96
+ given<T>(message: T | ApprovedSnapshotSource): WhenStep<T> {
97
+ return new WhenStep(message, this.options);
98
+ }
99
+ }
100
+
101
+ class WhenStep<T> {
102
+ constructor(
103
+ private readonly message: T | ApprovedSnapshotSource,
104
+ private readonly options: MessageContractOptions,
105
+ ) {}
106
+
107
+ /** Serialize the message; the next step pins or inspects the result. */
108
+ whenSerialized(): SnapshotStep {
109
+ if (isApprovedSnapshotSource(this.message)) {
110
+ throw new ContractViolationError(
111
+ 'Cannot serialize an approved snapshot source. ' +
112
+ 'Use .whenDeserializedAs(...) to run a compatibility check, or ' +
113
+ 'pass a live message instance to .given(...).',
114
+ );
115
+ }
116
+ const serializer = this.options.serializer ?? defaultSerializer;
117
+ const serialized = serializer.serialize(this.message);
118
+ return new SnapshotStep(serialized, serializer, this.options);
119
+ }
120
+
121
+ /**
122
+ * Round-trip the message through a reader that models a *different version*
123
+ * (a Standard Schema such as Zod/Valibot, or a parse function). The next step
124
+ * asserts the versions stay compatible.
125
+ */
126
+ whenDeserializedAs<Output>(reader: Reader<Output>): CompatibilityStep<Output> {
127
+ const serializer = this.options.serializer ?? defaultSerializer;
128
+ return new CompatibilityStep(this.message, reader, serializer, this.options);
129
+ }
130
+ }
131
+
132
+ class SnapshotStep {
133
+ constructor(
134
+ private readonly serialized: string,
135
+ private readonly serializer: MessageSerializer<string>,
136
+ private readonly options: MessageContractOptions,
137
+ ) {}
138
+
139
+ /** The serialized bytes, for ad-hoc assertions outside the snapshot flow. */
140
+ get output(): string {
141
+ return this.serialized;
142
+ }
143
+
144
+ /**
145
+ * Compare the serialized output against the approved snapshot. On first run
146
+ * (or in update mode) it writes the approved file and passes; afterwards it
147
+ * fails with a diff when the shape drifts.
148
+ */
149
+ thenContractIsUnchanged(snapshotName?: string): void {
150
+ const location = this.resolveLocation(snapshotName);
151
+ const existing = readSnapshot(location);
152
+ const update = this.options.update ?? isUpdateMode();
153
+
154
+ if (!existing.exists || update) {
155
+ const path = writeSnapshot(location, this.serialized);
156
+ if (!existing.exists) {
157
+ // First run: record and pass, leaving the file to be reviewed/committed.
158
+ return;
159
+ }
160
+ // Update mode: rewrite and pass even if it changed.
161
+ void path;
162
+ return;
163
+ }
164
+
165
+ if (existing.content !== this.serialized) {
166
+ throw new ContractViolationError(
167
+ `Message contract drifted from its approved snapshot.\n` +
168
+ ` serializer: ${this.serializer.name}\n` +
169
+ ` snapshot: ${existing.path}\n\n` +
170
+ `${lineDiff(existing.content ?? '', this.serialized)}\n\n` +
171
+ `If this change is intentional, re-run with AUTOTEL_CONTRACT_UPDATE=1 ` +
172
+ `to update the approved file, then review and commit it.`,
173
+ );
174
+ }
175
+ }
176
+
177
+ private resolveLocation(snapshotName?: string): SnapshotLocation {
178
+ if (snapshotName) return { name: snapshotName };
179
+ const configured = this.options.snapshot;
180
+ if (typeof configured === 'string') return { name: configured };
181
+ if (configured) return configured;
182
+ throw new ContractViolationError(
183
+ `A snapshot name is required. Pass one to messageContract({ snapshot: 'OrderPlaced' }) ` +
184
+ `or to thenContractIsUnchanged('OrderPlaced').`,
185
+ );
186
+ }
187
+ }
188
+
189
+ class CompatibilityStep<Output> {
190
+ constructor(
191
+ private readonly source: unknown,
192
+ private readonly reader: Reader<Output>,
193
+ private readonly serializer: MessageSerializer<string>,
194
+ private readonly options: MessageContractOptions,
195
+ ) {}
196
+
197
+ /**
198
+ * The reader models a **newer** version; confirm it still reads what an older
199
+ * writer produced (stored events, in-flight messages). Optionally assert on
200
+ * the upgraded value — e.g. that a newly-added field defaults sensibly.
201
+ */
202
+ async thenBackwardCompatible(
203
+ assert?: (value: Output) => void | Promise<void>,
204
+ ): Promise<Output> {
205
+ return this.check('backward', assert);
206
+ }
207
+
208
+ /**
209
+ * The reader models an **older** version; confirm a consumer that has not
210
+ * upgraded yet still reads what the newer writer produces, so you can ship the
211
+ * new shape before every reader has caught up.
212
+ */
213
+ async thenForwardCompatible(
214
+ assert?: (value: Output) => void | Promise<void>,
215
+ ): Promise<Output> {
216
+ return this.check('forward', assert);
217
+ }
218
+
219
+ private async check(
220
+ direction: 'backward' | 'forward',
221
+ assert?: (value: Output) => void | Promise<void>,
222
+ ): Promise<Output> {
223
+ const serialized = this.resolveSourceSerialized();
224
+ const deserializedSource = this.serializer.deserialize(serialized);
225
+ const outcome = await read(this.reader, deserializedSource);
226
+
227
+ if (!outcome.ok) {
228
+ const writer = direction === 'backward' ? 'an older writer' : 'a newer writer';
229
+ const readerLabel =
230
+ direction === 'backward' ? 'the newer reader' : 'the older reader';
231
+ throw new ContractViolationError(
232
+ `Not ${direction}-compatible: ${readerLabel} rejected a message ${writer} produced.\n` +
233
+ ` serializer: ${this.serializer.name}\n` +
234
+ ` serialized: ${truncate(serialized)}\n` +
235
+ ` issues:\n${outcome.issues.map((m) => ` - ${m}`).join('\n')}`,
236
+ );
237
+ }
238
+
239
+ this.assertStructuralCompatibility(
240
+ deserializedSource,
241
+ this.serializer.deserialize(this.serializer.serialize(outcome.value)),
242
+ direction,
243
+ serialized,
244
+ );
245
+
246
+ if (assert) await assert(outcome.value as Output);
247
+ return outcome.value as Output;
248
+ }
249
+
250
+ private resolveSourceSerialized(): string {
251
+ if (isApprovedSnapshotSource(this.source)) {
252
+ const location = this.resolveSnapshotLocation(this.source.location);
253
+ const existing = readSnapshot(location);
254
+ if (!existing.exists || existing.content === undefined) {
255
+ throw new ContractViolationError(
256
+ `Cannot read approved snapshot for compatibility check.\n` +
257
+ ` snapshot: ${existing.path}\n\n` +
258
+ `Record it first with .whenSerialized().thenContractIsUnchanged(), ` +
259
+ `or point approvedSnapshot(...) at an existing file.`,
260
+ );
261
+ }
262
+ return existing.content;
263
+ }
264
+ return this.serializer.serialize(this.source);
265
+ }
266
+
267
+ private resolveSnapshotLocation(
268
+ location?: string | SnapshotLocation,
269
+ ): SnapshotLocation {
270
+ if (typeof location === 'string') return { name: location };
271
+ if (location) return location;
272
+
273
+ const configured = this.options.snapshot;
274
+ if (typeof configured === 'string') return { name: configured };
275
+ if (configured) return configured;
276
+
277
+ throw new ContractViolationError(
278
+ 'A snapshot location is required for approvedSnapshot(). ' +
279
+ `Pass approvedSnapshot('OrderPlaced_v1') or configure messageContract({ snapshot: 'OrderPlaced_v1' }).`,
280
+ );
281
+ }
282
+
283
+ private assertStructuralCompatibility(
284
+ sourceValue: unknown,
285
+ targetValue: unknown,
286
+ direction: 'backward' | 'forward',
287
+ serialized: string,
288
+ ): void {
289
+ const mismatches: string[] = [];
290
+ compareSharedStructure(sourceValue, targetValue, '$', mismatches);
291
+
292
+ if (mismatches.length === 0) return;
293
+
294
+ throw new ContractViolationError(
295
+ `Not ${direction}-compatible: shared fields changed meaning across versions.\n` +
296
+ ` serializer: ${this.serializer.name}\n` +
297
+ ` serialized: ${truncate(serialized)}\n` +
298
+ ` mismatches:\n${mismatches.map((issue) => ` - ${issue}`).join('\n')}`,
299
+ );
300
+ }
301
+ }
302
+
303
+ function truncate(value: string, max = 400): string {
304
+ return value.length > max ? `${value.slice(0, max)}… (${value.length} chars)` : value;
305
+ }
306
+
307
+ function compareSharedStructure(
308
+ sourceValue: unknown,
309
+ targetValue: unknown,
310
+ path: string,
311
+ mismatches: string[],
312
+ ): void {
313
+ if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {
314
+ const sourceKeys = Object.keys(sourceValue);
315
+ const targetKeys = Object.keys(targetValue);
316
+ const sourceOnly = sourceKeys.filter((key) => !(key in targetValue));
317
+ const targetOnly = targetKeys.filter((key) => !(key in sourceValue));
318
+
319
+ if (sourceOnly.length > 0 && targetOnly.length > 0) {
320
+ mismatches.push(
321
+ `${path}: structural incompatibility ` +
322
+ `[source-only: ${sourceOnly.join(', ')}, target-only: ${targetOnly.join(', ')}]`,
323
+ );
324
+ return;
325
+ }
326
+
327
+ for (const key of sourceKeys.filter((candidate) => candidate in targetValue).toSorted()) {
328
+ compareSharedStructure(
329
+ sourceValue[key],
330
+ targetValue[key],
331
+ joinPath(path, key),
332
+ mismatches,
333
+ );
334
+ }
335
+ return;
336
+ }
337
+
338
+ if (Array.isArray(sourceValue) && Array.isArray(targetValue)) {
339
+ if (sourceValue.length !== targetValue.length) {
340
+ mismatches.push(
341
+ `${path}: array length differs (${sourceValue.length} vs ${targetValue.length})`,
342
+ );
343
+ return;
344
+ }
345
+
346
+ for (const [index, sourceItem] of sourceValue.entries()) {
347
+ compareSharedStructure(sourceItem, targetValue[index], `${path}[${index}]`, mismatches);
348
+ }
349
+ return;
350
+ }
351
+
352
+ if (!deepEqual(sourceValue, targetValue)) {
353
+ mismatches.push(
354
+ `${path}: value differs (${formatValue(sourceValue)} vs ${formatValue(targetValue)})`,
355
+ );
356
+ }
357
+ }
358
+
359
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
360
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
361
+ const proto = Object.getPrototypeOf(value);
362
+ return proto === Object.prototype || proto === null;
363
+ }
364
+
365
+ function deepEqual(left: unknown, right: unknown): boolean {
366
+ if (Object.is(left, right)) return true;
367
+
368
+ if (Array.isArray(left) && Array.isArray(right)) {
369
+ return (
370
+ left.length === right.length &&
371
+ left.every((value, index) => deepEqual(value, right[index]))
372
+ );
373
+ }
374
+
375
+ if (isPlainObject(left) && isPlainObject(right)) {
376
+ const leftKeys = Object.keys(left);
377
+ const rightKeys = Object.keys(right);
378
+ return (
379
+ leftKeys.length === rightKeys.length &&
380
+ leftKeys.every((key) => key in right && deepEqual(left[key], right[key]))
381
+ );
382
+ }
383
+
384
+ return false;
385
+ }
386
+
387
+ function formatValue(value: unknown): string {
388
+ if (typeof value === 'string') return JSON.stringify(value);
389
+ if (
390
+ value === null ||
391
+ typeof value === 'number' ||
392
+ typeof value === 'boolean' ||
393
+ value === undefined
394
+ ) {
395
+ return String(value);
396
+ }
397
+ return JSON.stringify(value);
398
+ }
399
+
400
+ function joinPath(path: string, key: string): string {
401
+ return path === '$' ? `$.${key}` : `${path}.${key}`;
402
+ }
403
+
404
+ export type {
405
+ GivenStep,
406
+ WhenStep,
407
+ SnapshotStep,
408
+ CompatibilityStep,
409
+ };
package/src/diff.ts ADDED
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Minimal line diff for snapshot failures. The goal is a message that points
3
+ * at *what moved* — a renamed field, a switched date format, a dropped value —
4
+ * not a full diff engine. Lines only in the approved file are marked `-`, lines
5
+ * only in the actual output are marked `+`, and a little surrounding context is
6
+ * kept so the change is readable in a terminal.
7
+ */
8
+ export function lineDiff(approved: string, actual: string): string {
9
+ const a = approved.split('\n');
10
+ const b = actual.split('\n');
11
+ const lcs = longestCommonSubsequence(a, b);
12
+
13
+ const out: string[] = [];
14
+ let i = 0;
15
+ let j = 0;
16
+ for (const [ai, bj] of lcs) {
17
+ while (i < ai) out.push(`- ${a[i++]}`);
18
+ while (j < bj) out.push(`+ ${b[j++]}`);
19
+ out.push(` ${a[i++]}`);
20
+ j++;
21
+ }
22
+ while (i < a.length) out.push(`- ${a[i++]}`);
23
+ while (j < b.length) out.push(`+ ${b[j++]}`);
24
+
25
+ return out.join('\n');
26
+ }
27
+
28
+ /** Indices `[i, j]` of lines common to both, longest such subsequence. */
29
+ function longestCommonSubsequence(
30
+ a: string[],
31
+ b: string[],
32
+ ): Array<[number, number]> {
33
+ const n = a.length;
34
+ const m = b.length;
35
+ const table: number[][] = Array.from({ length: n + 1 }, () =>
36
+ Array.from({ length: m + 1 }, () => 0),
37
+ );
38
+ for (let i = n - 1; i >= 0; i--) {
39
+ for (let j = m - 1; j >= 0; j--) {
40
+ table[i][j] =
41
+ a[i] === b[j]
42
+ ? table[i + 1][j + 1] + 1
43
+ : Math.max(table[i + 1][j], table[i][j + 1]);
44
+ }
45
+ }
46
+ const pairs: Array<[number, number]> = [];
47
+ let i = 0;
48
+ let j = 0;
49
+ while (i < n && j < m) {
50
+ if (a[i] === b[j]) {
51
+ pairs.push([i, j]);
52
+ i++;
53
+ j++;
54
+ } else if (table[i + 1][j] >= table[i][j + 1]) {
55
+ i++;
56
+ } else {
57
+ j++;
58
+ }
59
+ }
60
+ return pairs;
61
+ }
package/src/index.ts ADDED
@@ -0,0 +1,53 @@
1
+ /**
2
+ * autotel-contract — brokerless message contract testing.
3
+ *
4
+ * Pin the serialized shape of the messages your code sends and stores (events,
5
+ * commands, queue payloads, HTTP bodies) and prove old and new versions stay
6
+ * compatible — as ordinary unit tests, with the contract committed as a file
7
+ * beside the test. No broker, no schema registry, nothing to run in Docker.
8
+ *
9
+ * @see {@link messageContract} for the snapshot + compatibility DSL.
10
+ */
11
+ export {
12
+ messageContract,
13
+ ContractViolationError,
14
+ approvedSnapshot,
15
+ } from './contract.js';
16
+ export type {
17
+ ApprovedSnapshotSource,
18
+ MessageContractOptions,
19
+ GivenStep,
20
+ WhenStep,
21
+ SnapshotStep,
22
+ CompatibilityStep,
23
+ } from './contract.js';
24
+
25
+ export {
26
+ defaultSerializer,
27
+ jsonSerializer,
28
+ } from './serializer.js';
29
+ export type {
30
+ MessageSerializer,
31
+ JsonSerializerOptions,
32
+ } from './serializer.js';
33
+
34
+ export { read } from './reader.js';
35
+ export type {
36
+ Reader,
37
+ ParseFn,
38
+ StandardSchemaLike,
39
+ ReadOutcome,
40
+ } from './reader.js';
41
+
42
+ export {
43
+ isUpdateMode,
44
+ resolveSnapshotPath,
45
+ readSnapshot,
46
+ writeSnapshot,
47
+ } from './snapshot-storage.js';
48
+ export type {
49
+ SnapshotLocation,
50
+ ReadSnapshotResult,
51
+ } from './snapshot-storage.js';
52
+
53
+ export { lineDiff } from './diff.js';
package/src/reader.ts ADDED
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Readers — the "as this version" side of a compatibility check.
3
+ *
4
+ * In a JVM contract library you write `whenDeserializedAs(NewType.class)` and
5
+ * reflection does the rest. TypeScript types are erased at runtime, so there is
6
+ * no class to hand over. Instead you describe the *reader*: the thing that
7
+ * accepts a deserialized value and either produces a typed result or rejects
8
+ * it. Two shapes are accepted, in order of how most TS codebases already model
9
+ * a message version:
10
+ *
11
+ * 1. A **Standard Schema** (Zod ≥3.24, Valibot, ArkType, …) — anything exposing
12
+ * the `~standard` interface. This is the recommended form: the schema is the
13
+ * version, and it already lives next to your message type.
14
+ * 2. A plain **parse function** `(value) => T` that throws on incompatible input.
15
+ *
16
+ * A reader that accepts the value proves compatibility; a reader that throws or
17
+ * reports issues proves the versions have drifted apart.
18
+ */
19
+
20
+ /** The subset of the Standard Schema v1 interface we rely on. */
21
+ export interface StandardSchemaLike<Output = unknown> {
22
+ readonly '~standard': {
23
+ readonly version: 1;
24
+ readonly vendor: string;
25
+ readonly validate: (value: unknown) =>
26
+ | StandardResult<Output>
27
+ | Promise<StandardResult<Output>>;
28
+ };
29
+ }
30
+
31
+ interface StandardResult<Output> {
32
+ value?: Output;
33
+ issues?: ReadonlyArray<{ readonly message: string; readonly path?: unknown }>;
34
+ }
35
+
36
+ /** A bare parse function: returns the typed value or throws. */
37
+ export type ParseFn<Output = unknown> = (value: unknown) => Output;
38
+
39
+ /** Either accepted reader form. */
40
+ export type Reader<Output = unknown> =
41
+ | StandardSchemaLike<Output>
42
+ | ParseFn<Output>;
43
+
44
+ function isStandardSchema(reader: Reader): reader is StandardSchemaLike {
45
+ return (
46
+ typeof reader === 'object' &&
47
+ reader !== null &&
48
+ '~standard' in reader &&
49
+ typeof (reader as StandardSchemaLike)['~standard']?.validate === 'function'
50
+ );
51
+ }
52
+
53
+ export interface ReadOutcome<Output = unknown> {
54
+ ok: boolean;
55
+ /** Present when `ok`. */
56
+ value?: Output;
57
+ /** Human-readable reasons the reader rejected the value. */
58
+ issues: string[];
59
+ }
60
+
61
+ /**
62
+ * Run a reader against a deserialized value. Never throws — a thrown parse
63
+ * error or reported issues become `{ ok: false, issues }` so the caller can
64
+ * build a single, legible failure message.
65
+ */
66
+ export async function read<Output>(
67
+ reader: Reader<Output>,
68
+ value: unknown,
69
+ ): Promise<ReadOutcome<Output>> {
70
+ if (isStandardSchema(reader)) {
71
+ try {
72
+ const result = await reader['~standard'].validate(value);
73
+ if (result.issues && result.issues.length > 0) {
74
+ return {
75
+ ok: false,
76
+ issues: result.issues.map(
77
+ (issue) => formatIssue(issue.message, issue.path),
78
+ ),
79
+ };
80
+ }
81
+ return { ok: true, value: result.value as Output, issues: [] };
82
+ } catch (error) {
83
+ return { ok: false, issues: [errorMessage(error)] };
84
+ }
85
+ }
86
+
87
+ try {
88
+ const parsed = (reader as ParseFn<Output>)(value);
89
+ return { ok: true, value: parsed, issues: [] };
90
+ } catch (error) {
91
+ return { ok: false, issues: [errorMessage(error)] };
92
+ }
93
+ }
94
+
95
+ function formatIssue(message: string, path: unknown): string {
96
+ if (Array.isArray(path) && path.length > 0) {
97
+ return `${path.map(String).join('.')}: ${message}`;
98
+ }
99
+ return message;
100
+ }
101
+
102
+ function errorMessage(error: unknown): string {
103
+ if (error instanceof Error) return error.message;
104
+ return String(error);
105
+ }
@@ -0,0 +1,32 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { jsonSerializer } from './serializer.js';
4
+
5
+ describe('jsonSerializer', () => {
6
+ it('sorts object keys deterministically by default', () => {
7
+ const s = jsonSerializer({ indent: 0 });
8
+ expect(s.serialize({ b: 1, a: 2 })).toBe(s.serialize({ a: 2, b: 1 }));
9
+ expect(s.serialize({ b: 1, a: 2 })).toBe('{"a":2,"b":1}');
10
+ });
11
+
12
+ it('sorts keys recursively', () => {
13
+ const s = jsonSerializer({ indent: 0 });
14
+ expect(s.serialize({ z: { y: 1, x: 2 } })).toBe('{"z":{"x":2,"y":1}}');
15
+ });
16
+
17
+ it('preserves array order', () => {
18
+ const s = jsonSerializer({ indent: 0 });
19
+ expect(s.serialize([3, 1, 2])).toBe('[3,1,2]');
20
+ });
21
+
22
+ it('can preserve key order when asked', () => {
23
+ const s = jsonSerializer({ indent: 0, sortKeys: false });
24
+ expect(s.serialize({ b: 1, a: 2 })).toBe('{"b":1,"a":2}');
25
+ });
26
+
27
+ it('round-trips through deserialize', () => {
28
+ const s = jsonSerializer();
29
+ const value = { orderId: 'ord-1', items: [{ sku: 'x', qty: 2 }] };
30
+ expect(s.deserialize(s.serialize(value))).toEqual(value);
31
+ });
32
+ });