@relaybus/testing 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -0
- package/dist/index.cjs +175 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +142 -1
- package/dist/index.d.ts +142 -1
- package/dist/index.js +173 -5
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.cts
CHANGED
|
@@ -83,4 +83,145 @@ declare class InMemoryTransport implements Transport {
|
|
|
83
83
|
close(): Promise<void>;
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
|
|
86
|
+
/** How message bodies are compared: byte for byte, or as JSON regardless of key order and spacing. */
|
|
87
|
+
type BodyComparison = 'bytes' | 'json';
|
|
88
|
+
/** Bytes as text or bytes, as captured or encoded. */
|
|
89
|
+
type WireBytes = string | Uint8Array;
|
|
90
|
+
|
|
91
|
+
/** A record as captured from a topic: what an existing producer wrote. */
|
|
92
|
+
interface KafkaCapturedRecord {
|
|
93
|
+
readonly topic: string;
|
|
94
|
+
/** Default 0. */
|
|
95
|
+
readonly partition?: number;
|
|
96
|
+
/** Default `'0'`. */
|
|
97
|
+
readonly offset?: string;
|
|
98
|
+
readonly key: WireBytes | null;
|
|
99
|
+
readonly value: WireBytes | null;
|
|
100
|
+
/** Default none. */
|
|
101
|
+
readonly headers?: Readonly<Record<string, WireBytes>>;
|
|
102
|
+
/** Epoch milliseconds in a string; compared only when the codec sets a timestamp. */
|
|
103
|
+
readonly timestamp?: string;
|
|
104
|
+
}
|
|
105
|
+
/** What a codec's `encode` returns: a `KafkaCodec` record. */
|
|
106
|
+
interface KafkaWrittenRecord {
|
|
107
|
+
readonly key: WireBytes | null;
|
|
108
|
+
readonly value: WireBytes | null;
|
|
109
|
+
readonly headers: Readonly<Record<string, WireBytes | readonly WireBytes[]>>;
|
|
110
|
+
readonly timestamp?: string;
|
|
111
|
+
}
|
|
112
|
+
/** A record as a consumer hands it over: bytes for the key, the value and every header. */
|
|
113
|
+
interface KafkaDeliveredRecord {
|
|
114
|
+
readonly topic: string;
|
|
115
|
+
readonly partition: number;
|
|
116
|
+
readonly offset: string;
|
|
117
|
+
readonly key: Buffer | null;
|
|
118
|
+
readonly value: Buffer | null;
|
|
119
|
+
readonly headers: Readonly<Record<string, Buffer>>;
|
|
120
|
+
readonly timestamp?: string;
|
|
121
|
+
}
|
|
122
|
+
/** One message of the format: the event and the record that carries it. */
|
|
123
|
+
interface KafkaCodecCase {
|
|
124
|
+
readonly name: string;
|
|
125
|
+
readonly envelope: EventEnvelope;
|
|
126
|
+
readonly record: KafkaCapturedRecord;
|
|
127
|
+
/**
|
|
128
|
+
* The event decoding the record gives, when the wire does not carry all of `envelope` (for
|
|
129
|
+
* example a record written without a key). Default `envelope`.
|
|
130
|
+
*/
|
|
131
|
+
readonly decoded?: EventEnvelope;
|
|
132
|
+
/**
|
|
133
|
+
* Checks one direction only: `'decode'` for a message only other producers write (for
|
|
134
|
+
* example one without an id, which relaybus would add), `'encode'` for one only other
|
|
135
|
+
* consumers read. Default both.
|
|
136
|
+
*/
|
|
137
|
+
readonly only?: 'encode' | 'decode';
|
|
138
|
+
}
|
|
139
|
+
interface KafkaCodecContractOptions {
|
|
140
|
+
/** Writes an event: a codec's `encode`, or a transport's publish captured by an injected producer. */
|
|
141
|
+
readonly encode: (envelope: EventEnvelope) => KafkaWrittenRecord | Promise<KafkaWrittenRecord>;
|
|
142
|
+
/** Reads a record the way the transport does: `(record) => decodeKafkaRecord(codec, record)`. */
|
|
143
|
+
readonly decode: (record: KafkaDeliveredRecord) => EventEnvelope | Promise<EventEnvelope>;
|
|
144
|
+
readonly cases: readonly KafkaCodecCase[];
|
|
145
|
+
/** How values are compared. Default `'bytes'`. */
|
|
146
|
+
readonly compare?: BodyComparison;
|
|
147
|
+
/** Headers left out of the comparison, such as ones relaybus adds and other consumers ignore. */
|
|
148
|
+
readonly ignoreHeaders?: readonly string[];
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* What differs between `testCase` and what the codec writes and reads, one line each: the key,
|
|
152
|
+
* the value, the headers (a repeated header by its last value) and the timestamp of
|
|
153
|
+
* `encode(envelope)`, and `decode(record)` against the expected event. Empty when they agree.
|
|
154
|
+
*/
|
|
155
|
+
declare function kafkaCodecMismatches(options: KafkaCodecContractOptions, testCase: KafkaCodecCase): Promise<string[]>;
|
|
156
|
+
/**
|
|
157
|
+
* Registers one test per case asserting {@link kafkaCodecMismatches} finds nothing: a format of
|
|
158
|
+
* existing producers and consumers is adopted only once relaybus writes and reads it exactly.
|
|
159
|
+
*/
|
|
160
|
+
declare function kafkaCodecContract(name: string, options: KafkaCodecContractOptions): void;
|
|
161
|
+
|
|
162
|
+
/** A message as captured from a queue: what an existing publisher wrote. */
|
|
163
|
+
interface RabbitMqCapturedMessage {
|
|
164
|
+
readonly routingKey: string;
|
|
165
|
+
/**
|
|
166
|
+
* The message properties as publish options name them (`persistent`, `messageId`,
|
|
167
|
+
* `contentType`, `timestamp`, ...), without `headers`. Default none.
|
|
168
|
+
*/
|
|
169
|
+
readonly properties?: Readonly<Record<string, unknown>>;
|
|
170
|
+
/** Default none. */
|
|
171
|
+
readonly headers?: Readonly<Record<string, unknown>>;
|
|
172
|
+
readonly body: WireBytes;
|
|
173
|
+
}
|
|
174
|
+
/** What a codec's `encode` returns: a `RabbitMqCodec` message. */
|
|
175
|
+
interface RabbitMqWrittenMessage {
|
|
176
|
+
readonly content: Uint8Array;
|
|
177
|
+
/** The publish options: properties, and the headers under `headers`. */
|
|
178
|
+
readonly options: object;
|
|
179
|
+
}
|
|
180
|
+
/** A message as a consumer receives it. */
|
|
181
|
+
interface RabbitMqDeliveredMessage {
|
|
182
|
+
readonly content: Buffer;
|
|
183
|
+
readonly fields: {
|
|
184
|
+
readonly routingKey: string;
|
|
185
|
+
};
|
|
186
|
+
readonly properties: Readonly<Record<string, unknown>> & {
|
|
187
|
+
readonly headers: Readonly<Record<string, unknown>>;
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
/** One message of the format: the event and the message that carries it. */
|
|
191
|
+
interface RabbitMqCodecCase {
|
|
192
|
+
readonly name: string;
|
|
193
|
+
readonly envelope: EventEnvelope;
|
|
194
|
+
readonly message: RabbitMqCapturedMessage;
|
|
195
|
+
/** The event decoding the message gives, when the wire does not carry all of `envelope`. */
|
|
196
|
+
readonly decoded?: EventEnvelope;
|
|
197
|
+
/**
|
|
198
|
+
* Checks one direction only: `'decode'` for a message only other producers write (for
|
|
199
|
+
* example one without an id, which relaybus would add), `'encode'` for one only other
|
|
200
|
+
* consumers read. Default both.
|
|
201
|
+
*/
|
|
202
|
+
readonly only?: 'encode' | 'decode';
|
|
203
|
+
}
|
|
204
|
+
interface RabbitMqCodecContractOptions {
|
|
205
|
+
/** Writes an event: a codec's `encode`. The routing key is the route's destination, not the codec's. */
|
|
206
|
+
readonly encode: (envelope: EventEnvelope) => RabbitMqWrittenMessage | Promise<RabbitMqWrittenMessage>;
|
|
207
|
+
/** Reads a message the way the transport does: `(message) => decodeRabbitMqMessage(codec, message)`. */
|
|
208
|
+
readonly decode: (message: RabbitMqDeliveredMessage) => EventEnvelope | Promise<EventEnvelope>;
|
|
209
|
+
readonly cases: readonly RabbitMqCodecCase[];
|
|
210
|
+
/** How bodies are compared. Default `'bytes'`. */
|
|
211
|
+
readonly compare?: BodyComparison;
|
|
212
|
+
/** Properties left out of the comparison, such as a `timestamp` that differs per send. */
|
|
213
|
+
readonly ignoreProperties?: readonly string[];
|
|
214
|
+
/** Headers left out of the comparison. */
|
|
215
|
+
readonly ignoreHeaders?: readonly string[];
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* What differs between `testCase` and what the codec writes and reads, one line each: the
|
|
219
|
+
* properties, headers and body of `encode(envelope)`, and `decode(message)` against the expected
|
|
220
|
+
* event. Empty when they agree.
|
|
221
|
+
*/
|
|
222
|
+
declare function rabbitMqCodecMismatches(options: RabbitMqCodecContractOptions, testCase: RabbitMqCodecCase): Promise<string[]>;
|
|
223
|
+
/** Registers one test per case asserting {@link rabbitMqCodecMismatches} finds nothing. */
|
|
224
|
+
declare function rabbitMqCodecContract(name: string, options: RabbitMqCodecContractOptions): void;
|
|
225
|
+
|
|
226
|
+
export { FakeClock, InMemoryTransport, at, createDelivery, createEnvelope, eventStoreContract, kafkaCodecContract, kafkaCodecMismatches, rabbitMqCodecContract, rabbitMqCodecMismatches };
|
|
227
|
+
export type { BodyComparison, EventStoreContractOptions, KafkaCapturedRecord, KafkaCodecCase, KafkaCodecContractOptions, KafkaDeliveredRecord, KafkaWrittenRecord, PublishedMessage, RabbitMqCapturedMessage, RabbitMqCodecCase, RabbitMqCodecContractOptions, RabbitMqDeliveredMessage, RabbitMqWrittenMessage, WireBytes };
|
package/dist/index.d.ts
CHANGED
|
@@ -83,4 +83,145 @@ declare class InMemoryTransport implements Transport {
|
|
|
83
83
|
close(): Promise<void>;
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
|
|
86
|
+
/** How message bodies are compared: byte for byte, or as JSON regardless of key order and spacing. */
|
|
87
|
+
type BodyComparison = 'bytes' | 'json';
|
|
88
|
+
/** Bytes as text or bytes, as captured or encoded. */
|
|
89
|
+
type WireBytes = string | Uint8Array;
|
|
90
|
+
|
|
91
|
+
/** A record as captured from a topic: what an existing producer wrote. */
|
|
92
|
+
interface KafkaCapturedRecord {
|
|
93
|
+
readonly topic: string;
|
|
94
|
+
/** Default 0. */
|
|
95
|
+
readonly partition?: number;
|
|
96
|
+
/** Default `'0'`. */
|
|
97
|
+
readonly offset?: string;
|
|
98
|
+
readonly key: WireBytes | null;
|
|
99
|
+
readonly value: WireBytes | null;
|
|
100
|
+
/** Default none. */
|
|
101
|
+
readonly headers?: Readonly<Record<string, WireBytes>>;
|
|
102
|
+
/** Epoch milliseconds in a string; compared only when the codec sets a timestamp. */
|
|
103
|
+
readonly timestamp?: string;
|
|
104
|
+
}
|
|
105
|
+
/** What a codec's `encode` returns: a `KafkaCodec` record. */
|
|
106
|
+
interface KafkaWrittenRecord {
|
|
107
|
+
readonly key: WireBytes | null;
|
|
108
|
+
readonly value: WireBytes | null;
|
|
109
|
+
readonly headers: Readonly<Record<string, WireBytes | readonly WireBytes[]>>;
|
|
110
|
+
readonly timestamp?: string;
|
|
111
|
+
}
|
|
112
|
+
/** A record as a consumer hands it over: bytes for the key, the value and every header. */
|
|
113
|
+
interface KafkaDeliveredRecord {
|
|
114
|
+
readonly topic: string;
|
|
115
|
+
readonly partition: number;
|
|
116
|
+
readonly offset: string;
|
|
117
|
+
readonly key: Buffer | null;
|
|
118
|
+
readonly value: Buffer | null;
|
|
119
|
+
readonly headers: Readonly<Record<string, Buffer>>;
|
|
120
|
+
readonly timestamp?: string;
|
|
121
|
+
}
|
|
122
|
+
/** One message of the format: the event and the record that carries it. */
|
|
123
|
+
interface KafkaCodecCase {
|
|
124
|
+
readonly name: string;
|
|
125
|
+
readonly envelope: EventEnvelope;
|
|
126
|
+
readonly record: KafkaCapturedRecord;
|
|
127
|
+
/**
|
|
128
|
+
* The event decoding the record gives, when the wire does not carry all of `envelope` (for
|
|
129
|
+
* example a record written without a key). Default `envelope`.
|
|
130
|
+
*/
|
|
131
|
+
readonly decoded?: EventEnvelope;
|
|
132
|
+
/**
|
|
133
|
+
* Checks one direction only: `'decode'` for a message only other producers write (for
|
|
134
|
+
* example one without an id, which relaybus would add), `'encode'` for one only other
|
|
135
|
+
* consumers read. Default both.
|
|
136
|
+
*/
|
|
137
|
+
readonly only?: 'encode' | 'decode';
|
|
138
|
+
}
|
|
139
|
+
interface KafkaCodecContractOptions {
|
|
140
|
+
/** Writes an event: a codec's `encode`, or a transport's publish captured by an injected producer. */
|
|
141
|
+
readonly encode: (envelope: EventEnvelope) => KafkaWrittenRecord | Promise<KafkaWrittenRecord>;
|
|
142
|
+
/** Reads a record the way the transport does: `(record) => decodeKafkaRecord(codec, record)`. */
|
|
143
|
+
readonly decode: (record: KafkaDeliveredRecord) => EventEnvelope | Promise<EventEnvelope>;
|
|
144
|
+
readonly cases: readonly KafkaCodecCase[];
|
|
145
|
+
/** How values are compared. Default `'bytes'`. */
|
|
146
|
+
readonly compare?: BodyComparison;
|
|
147
|
+
/** Headers left out of the comparison, such as ones relaybus adds and other consumers ignore. */
|
|
148
|
+
readonly ignoreHeaders?: readonly string[];
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* What differs between `testCase` and what the codec writes and reads, one line each: the key,
|
|
152
|
+
* the value, the headers (a repeated header by its last value) and the timestamp of
|
|
153
|
+
* `encode(envelope)`, and `decode(record)` against the expected event. Empty when they agree.
|
|
154
|
+
*/
|
|
155
|
+
declare function kafkaCodecMismatches(options: KafkaCodecContractOptions, testCase: KafkaCodecCase): Promise<string[]>;
|
|
156
|
+
/**
|
|
157
|
+
* Registers one test per case asserting {@link kafkaCodecMismatches} finds nothing: a format of
|
|
158
|
+
* existing producers and consumers is adopted only once relaybus writes and reads it exactly.
|
|
159
|
+
*/
|
|
160
|
+
declare function kafkaCodecContract(name: string, options: KafkaCodecContractOptions): void;
|
|
161
|
+
|
|
162
|
+
/** A message as captured from a queue: what an existing publisher wrote. */
|
|
163
|
+
interface RabbitMqCapturedMessage {
|
|
164
|
+
readonly routingKey: string;
|
|
165
|
+
/**
|
|
166
|
+
* The message properties as publish options name them (`persistent`, `messageId`,
|
|
167
|
+
* `contentType`, `timestamp`, ...), without `headers`. Default none.
|
|
168
|
+
*/
|
|
169
|
+
readonly properties?: Readonly<Record<string, unknown>>;
|
|
170
|
+
/** Default none. */
|
|
171
|
+
readonly headers?: Readonly<Record<string, unknown>>;
|
|
172
|
+
readonly body: WireBytes;
|
|
173
|
+
}
|
|
174
|
+
/** What a codec's `encode` returns: a `RabbitMqCodec` message. */
|
|
175
|
+
interface RabbitMqWrittenMessage {
|
|
176
|
+
readonly content: Uint8Array;
|
|
177
|
+
/** The publish options: properties, and the headers under `headers`. */
|
|
178
|
+
readonly options: object;
|
|
179
|
+
}
|
|
180
|
+
/** A message as a consumer receives it. */
|
|
181
|
+
interface RabbitMqDeliveredMessage {
|
|
182
|
+
readonly content: Buffer;
|
|
183
|
+
readonly fields: {
|
|
184
|
+
readonly routingKey: string;
|
|
185
|
+
};
|
|
186
|
+
readonly properties: Readonly<Record<string, unknown>> & {
|
|
187
|
+
readonly headers: Readonly<Record<string, unknown>>;
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
/** One message of the format: the event and the message that carries it. */
|
|
191
|
+
interface RabbitMqCodecCase {
|
|
192
|
+
readonly name: string;
|
|
193
|
+
readonly envelope: EventEnvelope;
|
|
194
|
+
readonly message: RabbitMqCapturedMessage;
|
|
195
|
+
/** The event decoding the message gives, when the wire does not carry all of `envelope`. */
|
|
196
|
+
readonly decoded?: EventEnvelope;
|
|
197
|
+
/**
|
|
198
|
+
* Checks one direction only: `'decode'` for a message only other producers write (for
|
|
199
|
+
* example one without an id, which relaybus would add), `'encode'` for one only other
|
|
200
|
+
* consumers read. Default both.
|
|
201
|
+
*/
|
|
202
|
+
readonly only?: 'encode' | 'decode';
|
|
203
|
+
}
|
|
204
|
+
interface RabbitMqCodecContractOptions {
|
|
205
|
+
/** Writes an event: a codec's `encode`. The routing key is the route's destination, not the codec's. */
|
|
206
|
+
readonly encode: (envelope: EventEnvelope) => RabbitMqWrittenMessage | Promise<RabbitMqWrittenMessage>;
|
|
207
|
+
/** Reads a message the way the transport does: `(message) => decodeRabbitMqMessage(codec, message)`. */
|
|
208
|
+
readonly decode: (message: RabbitMqDeliveredMessage) => EventEnvelope | Promise<EventEnvelope>;
|
|
209
|
+
readonly cases: readonly RabbitMqCodecCase[];
|
|
210
|
+
/** How bodies are compared. Default `'bytes'`. */
|
|
211
|
+
readonly compare?: BodyComparison;
|
|
212
|
+
/** Properties left out of the comparison, such as a `timestamp` that differs per send. */
|
|
213
|
+
readonly ignoreProperties?: readonly string[];
|
|
214
|
+
/** Headers left out of the comparison. */
|
|
215
|
+
readonly ignoreHeaders?: readonly string[];
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* What differs between `testCase` and what the codec writes and reads, one line each: the
|
|
219
|
+
* properties, headers and body of `encode(envelope)`, and `decode(message)` against the expected
|
|
220
|
+
* event. Empty when they agree.
|
|
221
|
+
*/
|
|
222
|
+
declare function rabbitMqCodecMismatches(options: RabbitMqCodecContractOptions, testCase: RabbitMqCodecCase): Promise<string[]>;
|
|
223
|
+
/** Registers one test per case asserting {@link rabbitMqCodecMismatches} finds nothing. */
|
|
224
|
+
declare function rabbitMqCodecContract(name: string, options: RabbitMqCodecContractOptions): void;
|
|
225
|
+
|
|
226
|
+
export { FakeClock, InMemoryTransport, at, createDelivery, createEnvelope, eventStoreContract, kafkaCodecContract, kafkaCodecMismatches, rabbitMqCodecContract, rabbitMqCodecMismatches };
|
|
227
|
+
export type { BodyComparison, EventStoreContractOptions, KafkaCapturedRecord, KafkaCodecCase, KafkaCodecContractOptions, KafkaDeliveredRecord, KafkaWrittenRecord, PublishedMessage, RabbitMqCapturedMessage, RabbitMqCodecCase, RabbitMqCodecContractOptions, RabbitMqDeliveredMessage, RabbitMqWrittenMessage, WireBytes };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, beforeEach, afterEach, it, expect } from 'vitest';
|
|
2
|
-
import { createUuidV7 } from '@relaybus/core';
|
|
2
|
+
import { createUuidV7, toErrorInfo } from '@relaybus/core';
|
|
3
3
|
|
|
4
4
|
// src/event-store-contract.ts
|
|
5
5
|
function createDeferred() {
|
|
@@ -1404,18 +1404,186 @@ var InMemoryTransport = class {
|
|
|
1404
1404
|
}
|
|
1405
1405
|
};
|
|
1406
1406
|
async function deliverWithRedeliveries(handler, delivery) {
|
|
1407
|
-
for (let
|
|
1407
|
+
for (let attempt2 = 0; ; attempt2 += 1) {
|
|
1408
1408
|
try {
|
|
1409
1409
|
await handler(delivery.envelope, { destination: delivery.destination });
|
|
1410
|
-
return
|
|
1410
|
+
return attempt2 + 1;
|
|
1411
1411
|
} catch (error) {
|
|
1412
|
-
if (
|
|
1412
|
+
if (attempt2 >= delivery.maxRedeliveries) {
|
|
1413
1413
|
throw error;
|
|
1414
1414
|
}
|
|
1415
1415
|
}
|
|
1416
1416
|
}
|
|
1417
1417
|
}
|
|
1418
|
+
function toBuffer(bytes) {
|
|
1419
|
+
return typeof bytes === "string" ? Buffer.from(bytes, "utf8") : Buffer.from(bytes);
|
|
1420
|
+
}
|
|
1421
|
+
function canonicalJson(value) {
|
|
1422
|
+
return JSON.stringify(sortKeys(JSON.parse(JSON.stringify(value) ?? "null")));
|
|
1423
|
+
}
|
|
1424
|
+
function sortKeys(value) {
|
|
1425
|
+
if (Array.isArray(value)) {
|
|
1426
|
+
return value.map(sortKeys);
|
|
1427
|
+
}
|
|
1428
|
+
if (typeof value !== "object" || value === null) {
|
|
1429
|
+
return value;
|
|
1430
|
+
}
|
|
1431
|
+
return Object.fromEntries(
|
|
1432
|
+
Object.keys(value).sort().map((name) => [name, sortKeys(value[name])])
|
|
1433
|
+
);
|
|
1434
|
+
}
|
|
1435
|
+
function compareBodies(label, {
|
|
1436
|
+
expected,
|
|
1437
|
+
encoded,
|
|
1438
|
+
comparison
|
|
1439
|
+
}) {
|
|
1440
|
+
const text = (bytes) => bytes === null ? "null" : toBuffer(bytes).toString("utf8");
|
|
1441
|
+
const same = expected === null || encoded === null ? expected === encoded : comparison === "json" ? canonicalJson(JSON.parse(text(expected))) === canonicalJson(JSON.parse(text(encoded))) : toBuffer(expected).equals(toBuffer(encoded));
|
|
1442
|
+
return same ? [] : [`${label}: expected ${text(expected)}, encoded ${text(encoded)}`];
|
|
1443
|
+
}
|
|
1444
|
+
function compareObjects(label, {
|
|
1445
|
+
expected,
|
|
1446
|
+
encoded,
|
|
1447
|
+
ignored
|
|
1448
|
+
}) {
|
|
1449
|
+
const kept = (object) => canonicalJson(
|
|
1450
|
+
Object.fromEntries(Object.entries(object).filter(([name]) => !ignored.includes(name)))
|
|
1451
|
+
);
|
|
1452
|
+
const [left, right] = [kept(expected), kept(encoded)];
|
|
1453
|
+
return left === right ? [] : [`${label}: expected ${left}, encoded ${right}`];
|
|
1454
|
+
}
|
|
1455
|
+
function compareDecoded(expected, decoded) {
|
|
1456
|
+
const [left, right] = [canonicalJson(expected), canonicalJson(decoded)];
|
|
1457
|
+
return left === right ? [] : [`decoded: expected ${left}, decoded ${right}`];
|
|
1458
|
+
}
|
|
1459
|
+
async function attempt(label, step) {
|
|
1460
|
+
try {
|
|
1461
|
+
return { result: await step(), mismatches: [] };
|
|
1462
|
+
} catch (error) {
|
|
1463
|
+
return { mismatches: [`${label} threw: ${toErrorInfo(error).message}`] };
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
// src/codec-contract/kafka.ts
|
|
1468
|
+
async function kafkaCodecMismatches(options, testCase) {
|
|
1469
|
+
const { record } = testCase;
|
|
1470
|
+
const encoding = testCase.only === "decode" ? { mismatches: [] } : await attempt("encode", () => options.encode(testCase.envelope));
|
|
1471
|
+
const encoded = encoding.result;
|
|
1472
|
+
const decoding = testCase.only === "encode" ? { mismatches: [] } : await attempt("decode", () => options.decode(delivered(record)));
|
|
1473
|
+
return [
|
|
1474
|
+
...encoding.mismatches,
|
|
1475
|
+
...encoded === void 0 ? [] : compareRecords(options, { record, encoded }),
|
|
1476
|
+
...decoding.mismatches,
|
|
1477
|
+
...decoding.result === void 0 ? [] : compareDecoded(testCase.decoded ?? testCase.envelope, decoding.result)
|
|
1478
|
+
];
|
|
1479
|
+
}
|
|
1480
|
+
function kafkaCodecContract(name, options) {
|
|
1481
|
+
describe(`${name} writes and reads the captured Kafka records`, () => {
|
|
1482
|
+
it.each(options.cases.map((testCase) => [testCase.name, testCase]))(
|
|
1483
|
+
"%s",
|
|
1484
|
+
async (_name, testCase) => {
|
|
1485
|
+
expect(await kafkaCodecMismatches(options, testCase)).toEqual([]);
|
|
1486
|
+
}
|
|
1487
|
+
);
|
|
1488
|
+
});
|
|
1489
|
+
}
|
|
1490
|
+
function compareRecords(options, {
|
|
1491
|
+
record,
|
|
1492
|
+
encoded
|
|
1493
|
+
}) {
|
|
1494
|
+
const text = (bytes) => bytes === null ? null : toBuffer(bytes).toString("utf8");
|
|
1495
|
+
const timestamp = encoded.timestamp;
|
|
1496
|
+
return [
|
|
1497
|
+
...text(record.key) === text(encoded.key) ? [] : [`key: expected ${text(record.key)}, encoded ${text(encoded.key)}`],
|
|
1498
|
+
...compareBodies("value", {
|
|
1499
|
+
expected: record.value,
|
|
1500
|
+
encoded: encoded.value,
|
|
1501
|
+
comparison: options.compare ?? "bytes"
|
|
1502
|
+
}),
|
|
1503
|
+
...compareObjects("headers", {
|
|
1504
|
+
expected: textHeaders(record.headers ?? {}),
|
|
1505
|
+
encoded: textHeaders(encoded.headers),
|
|
1506
|
+
ignored: options.ignoreHeaders ?? []
|
|
1507
|
+
}),
|
|
1508
|
+
...timestamp === void 0 || timestamp === record.timestamp ? [] : [`timestamp: expected ${record.timestamp}, encoded ${timestamp}`]
|
|
1509
|
+
];
|
|
1510
|
+
}
|
|
1511
|
+
function textHeaders(headers) {
|
|
1512
|
+
return Object.fromEntries(
|
|
1513
|
+
Object.entries(headers).map(([name, value]) => [
|
|
1514
|
+
name,
|
|
1515
|
+
toBuffer(Array.isArray(value) ? value.at(-1) : value).toString(
|
|
1516
|
+
"utf8"
|
|
1517
|
+
)
|
|
1518
|
+
])
|
|
1519
|
+
);
|
|
1520
|
+
}
|
|
1521
|
+
function delivered(record) {
|
|
1522
|
+
return {
|
|
1523
|
+
topic: record.topic,
|
|
1524
|
+
partition: record.partition ?? 0,
|
|
1525
|
+
offset: record.offset ?? "0",
|
|
1526
|
+
key: record.key === null ? null : toBuffer(record.key),
|
|
1527
|
+
value: record.value === null ? null : toBuffer(record.value),
|
|
1528
|
+
headers: Object.fromEntries(
|
|
1529
|
+
Object.entries(record.headers ?? {}).map(([name, value]) => [name, toBuffer(value)])
|
|
1530
|
+
),
|
|
1531
|
+
...record.timestamp === void 0 ? {} : { timestamp: record.timestamp }
|
|
1532
|
+
};
|
|
1533
|
+
}
|
|
1534
|
+
async function rabbitMqCodecMismatches(options, testCase) {
|
|
1535
|
+
const { message } = testCase;
|
|
1536
|
+
const encoding = testCase.only === "decode" ? { mismatches: [] } : await attempt("encode", () => options.encode(testCase.envelope));
|
|
1537
|
+
const encoded = encoding.result;
|
|
1538
|
+
const decoding = testCase.only === "encode" ? { mismatches: [] } : await attempt("decode", () => options.decode(delivered2(message)));
|
|
1539
|
+
return [
|
|
1540
|
+
...encoding.mismatches,
|
|
1541
|
+
...encoded === void 0 ? [] : compareMessages(options, { message, encoded }),
|
|
1542
|
+
...decoding.mismatches,
|
|
1543
|
+
...decoding.result === void 0 ? [] : compareDecoded(testCase.decoded ?? testCase.envelope, decoding.result)
|
|
1544
|
+
];
|
|
1545
|
+
}
|
|
1546
|
+
function rabbitMqCodecContract(name, options) {
|
|
1547
|
+
describe(`${name} writes and reads the captured RabbitMQ messages`, () => {
|
|
1548
|
+
it.each(options.cases.map((testCase) => [testCase.name, testCase]))(
|
|
1549
|
+
"%s",
|
|
1550
|
+
async (_name, testCase) => {
|
|
1551
|
+
expect(await rabbitMqCodecMismatches(options, testCase)).toEqual([]);
|
|
1552
|
+
}
|
|
1553
|
+
);
|
|
1554
|
+
});
|
|
1555
|
+
}
|
|
1556
|
+
function compareMessages(options, {
|
|
1557
|
+
message,
|
|
1558
|
+
encoded
|
|
1559
|
+
}) {
|
|
1560
|
+
const { headers, ...properties } = encoded.options;
|
|
1561
|
+
return [
|
|
1562
|
+
...compareObjects("properties", {
|
|
1563
|
+
expected: message.properties ?? {},
|
|
1564
|
+
encoded: properties,
|
|
1565
|
+
ignored: options.ignoreProperties ?? []
|
|
1566
|
+
}),
|
|
1567
|
+
...compareObjects("headers", {
|
|
1568
|
+
expected: message.headers ?? {},
|
|
1569
|
+
encoded: headers ?? {},
|
|
1570
|
+
ignored: options.ignoreHeaders ?? []
|
|
1571
|
+
}),
|
|
1572
|
+
...compareBodies("body", {
|
|
1573
|
+
expected: message.body,
|
|
1574
|
+
encoded: encoded.content,
|
|
1575
|
+
comparison: options.compare ?? "bytes"
|
|
1576
|
+
})
|
|
1577
|
+
];
|
|
1578
|
+
}
|
|
1579
|
+
function delivered2(message) {
|
|
1580
|
+
return {
|
|
1581
|
+
content: toBuffer(message.body),
|
|
1582
|
+
fields: { routingKey: message.routingKey },
|
|
1583
|
+
properties: { ...message.properties, headers: { ...message.headers } }
|
|
1584
|
+
};
|
|
1585
|
+
}
|
|
1418
1586
|
|
|
1419
|
-
export { FakeClock, InMemoryTransport, at, createDelivery, createEnvelope, eventStoreContract };
|
|
1587
|
+
export { FakeClock, InMemoryTransport, at, createDelivery, createEnvelope, eventStoreContract, kafkaCodecContract, kafkaCodecMismatches, rabbitMqCodecContract, rabbitMqCodecMismatches };
|
|
1420
1588
|
//# sourceMappingURL=index.js.map
|
|
1421
1589
|
//# sourceMappingURL=index.js.map
|