@relaybus/testing 0.4.0 → 0.5.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 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"codec-contract.cjs"}
@@ -0,0 +1,178 @@
1
+ import { RelayBusError, EventEnvelope } from '@relaybus/core';
2
+
3
+ /** How message bodies are compared: byte for byte, or as JSON regardless of key order and spacing. */
4
+ type BodyComparison = 'bytes' | 'json';
5
+ /** Bytes as text or bytes, as captured or encoded. */
6
+ type WireBytes = string | Uint8Array;
7
+
8
+ /** The two functions of a test framework the codec contract registers its tests with. */
9
+ interface TestFramework {
10
+ readonly describe: (name: string, body: () => void) => void;
11
+ readonly it: (name: string, body: () => Promise<void>) => void;
12
+ }
13
+ /** Thrown by a codec contract assertion: one line per mismatch between the codec and the case. */
14
+ declare class CodecContractError extends RelayBusError {
15
+ name: string;
16
+ readonly code: string;
17
+ readonly mismatches: readonly string[];
18
+ constructor(message: string, mismatches?: readonly string[]);
19
+ }
20
+
21
+ /** A record as captured from a topic: what an existing producer wrote. */
22
+ interface KafkaCapturedRecord {
23
+ readonly topic: string;
24
+ /** Default 0. */
25
+ readonly partition?: number;
26
+ /** Default `'0'`. */
27
+ readonly offset?: string;
28
+ readonly key: WireBytes | null;
29
+ readonly value: WireBytes | null;
30
+ /** Default none. */
31
+ readonly headers?: Readonly<Record<string, WireBytes>>;
32
+ /** Epoch milliseconds in a string; compared only when the codec sets a timestamp. */
33
+ readonly timestamp?: string;
34
+ }
35
+ /** What a codec's `encode` returns: a `KafkaCodec` record. */
36
+ interface KafkaWrittenRecord {
37
+ readonly key: WireBytes | null;
38
+ readonly value: WireBytes | null;
39
+ readonly headers: Readonly<Record<string, WireBytes | readonly WireBytes[]>>;
40
+ readonly timestamp?: string;
41
+ }
42
+ /** A record as a consumer hands it over: bytes for the key, the value and every header. */
43
+ interface KafkaDeliveredRecord {
44
+ readonly topic: string;
45
+ readonly partition: number;
46
+ readonly offset: string;
47
+ readonly key: Buffer | null;
48
+ readonly value: Buffer | null;
49
+ readonly headers: Readonly<Record<string, Buffer>>;
50
+ readonly timestamp?: string;
51
+ }
52
+ /** One message of the format: the event and the record that carries it. */
53
+ interface KafkaCodecCase {
54
+ readonly name: string;
55
+ readonly envelope: EventEnvelope;
56
+ readonly record: KafkaCapturedRecord;
57
+ /**
58
+ * The event decoding the record gives, when the wire does not carry all of `envelope` (for
59
+ * example a record written without a key). Default `envelope`.
60
+ */
61
+ readonly decoded?: EventEnvelope;
62
+ /**
63
+ * Checks one direction only: `'decode'` for a message only other producers write (for
64
+ * example one without an id, which relaybus would add), `'encode'` for one only other
65
+ * consumers read. Default both.
66
+ */
67
+ readonly only?: 'encode' | 'decode';
68
+ }
69
+ interface KafkaCodecContractOptions {
70
+ /** Writes an event: a codec's `encode`, or a transport's publish captured by an injected producer. */
71
+ readonly encode: (envelope: EventEnvelope) => KafkaWrittenRecord | Promise<KafkaWrittenRecord>;
72
+ /** Reads a record the way the transport does: `(record) => decodeKafkaRecord(codec, record)`. */
73
+ readonly decode: (record: KafkaDeliveredRecord) => EventEnvelope | Promise<EventEnvelope>;
74
+ readonly cases: readonly KafkaCodecCase[];
75
+ /** How values are compared. Default `'bytes'`. */
76
+ readonly compare?: BodyComparison;
77
+ /** Headers left out of the comparison, such as ones relaybus adds and other consumers ignore. */
78
+ readonly ignoreHeaders?: readonly string[];
79
+ }
80
+ /**
81
+ * What differs between `testCase` and what the codec writes and reads, one line each: the key,
82
+ * the value, the headers (a repeated header by its last value) and the timestamp of
83
+ * `encode(envelope)`, and `decode(record)` against the expected event. Empty when they agree.
84
+ */
85
+ declare function kafkaCodecMismatches(options: KafkaCodecContractOptions, testCase: KafkaCodecCase): Promise<string[]>;
86
+ /**
87
+ * Throws a {@link CodecContractError} listing what {@link kafkaCodecMismatches} finds, so any test framework
88
+ * (or a plain script) can check one case.
89
+ */
90
+ declare function assertKafkaCodecCase(options: KafkaCodecContractOptions, testCase: KafkaCodecCase): Promise<void>;
91
+ /**
92
+ * Registers one test per case through `framework` (the `describe` and `it` of Jest, Vitest,
93
+ * Mocha or `node:test`), each asserting {@link assertKafkaCodecCase}: a format of existing
94
+ * producers and consumers is adopted only once relaybus writes and reads it exactly.
95
+ */
96
+ declare function registerKafkaCodecContract(framework: TestFramework, { name, options }: {
97
+ readonly name: string;
98
+ readonly options: KafkaCodecContractOptions;
99
+ }): void;
100
+
101
+ /** A message as captured from a queue: what an existing publisher wrote. */
102
+ interface RabbitMqCapturedMessage {
103
+ readonly routingKey: string;
104
+ /**
105
+ * The message properties as publish options name them (`persistent`, `messageId`,
106
+ * `contentType`, `timestamp`, ...), without `headers`. Default none.
107
+ */
108
+ readonly properties?: Readonly<Record<string, unknown>>;
109
+ /** Default none. */
110
+ readonly headers?: Readonly<Record<string, unknown>>;
111
+ readonly body: WireBytes;
112
+ }
113
+ /** What a codec's `encode` returns: a `RabbitMqCodec` message. */
114
+ interface RabbitMqWrittenMessage {
115
+ readonly content: Uint8Array;
116
+ /** The publish options: properties, and the headers under `headers`. */
117
+ readonly options: object;
118
+ }
119
+ /** A message as a consumer receives it. */
120
+ interface RabbitMqDeliveredMessage {
121
+ readonly content: Buffer;
122
+ readonly fields: {
123
+ readonly routingKey: string;
124
+ };
125
+ readonly properties: Readonly<Record<string, unknown>> & {
126
+ readonly headers: Readonly<Record<string, unknown>>;
127
+ };
128
+ }
129
+ /** One message of the format: the event and the message that carries it. */
130
+ interface RabbitMqCodecCase {
131
+ readonly name: string;
132
+ readonly envelope: EventEnvelope;
133
+ readonly message: RabbitMqCapturedMessage;
134
+ /** The event decoding the message gives, when the wire does not carry all of `envelope`. */
135
+ readonly decoded?: EventEnvelope;
136
+ /**
137
+ * Checks one direction only: `'decode'` for a message only other producers write (for
138
+ * example one without an id, which relaybus would add), `'encode'` for one only other
139
+ * consumers read. Default both.
140
+ */
141
+ readonly only?: 'encode' | 'decode';
142
+ }
143
+ interface RabbitMqCodecContractOptions {
144
+ /** Writes an event: a codec's `encode`. The routing key is the route's destination, not the codec's. */
145
+ readonly encode: (envelope: EventEnvelope) => RabbitMqWrittenMessage | Promise<RabbitMqWrittenMessage>;
146
+ /** Reads a message the way the transport does: `(message) => decodeRabbitMqMessage(codec, message)`. */
147
+ readonly decode: (message: RabbitMqDeliveredMessage) => EventEnvelope | Promise<EventEnvelope>;
148
+ readonly cases: readonly RabbitMqCodecCase[];
149
+ /** How bodies are compared. Default `'bytes'`. */
150
+ readonly compare?: BodyComparison;
151
+ /** Properties left out of the comparison, such as a `timestamp` that differs per send. */
152
+ readonly ignoreProperties?: readonly string[];
153
+ /** Headers left out of the comparison. */
154
+ readonly ignoreHeaders?: readonly string[];
155
+ }
156
+ /**
157
+ * What differs between `testCase` and what the codec writes and reads, one line each: the
158
+ * properties, headers and body of `encode(envelope)`, and `decode(message)` against the expected
159
+ * event. Empty when they agree.
160
+ */
161
+ declare function rabbitMqCodecMismatches(options: RabbitMqCodecContractOptions, testCase: RabbitMqCodecCase): Promise<string[]>;
162
+ /**
163
+ * Throws a {@link CodecContractError} listing what {@link rabbitMqCodecMismatches} finds, so any test framework
164
+ * (or a plain script) can check one case.
165
+ */
166
+ declare function assertRabbitMqCodecCase(options: RabbitMqCodecContractOptions, testCase: RabbitMqCodecCase): Promise<void>;
167
+ /**
168
+ * Registers one test per case through `framework` (the `describe` and `it` of Jest, Vitest,
169
+ * Mocha or `node:test`), each asserting {@link assertRabbitMqCodecCase}: a format of existing
170
+ * producers and consumers is adopted only once relaybus writes and reads it exactly.
171
+ */
172
+ declare function registerRabbitMqCodecContract(framework: TestFramework, { name, options }: {
173
+ readonly name: string;
174
+ readonly options: RabbitMqCodecContractOptions;
175
+ }): void;
176
+
177
+ export { CodecContractError, assertKafkaCodecCase, assertRabbitMqCodecCase, kafkaCodecMismatches, rabbitMqCodecMismatches, registerKafkaCodecContract, registerRabbitMqCodecContract };
178
+ export type { BodyComparison, KafkaCapturedRecord, KafkaCodecCase, KafkaCodecContractOptions, KafkaDeliveredRecord, KafkaWrittenRecord, RabbitMqCapturedMessage, RabbitMqCodecCase, RabbitMqCodecContractOptions, RabbitMqDeliveredMessage, RabbitMqWrittenMessage, TestFramework, WireBytes };
@@ -0,0 +1,178 @@
1
+ import { RelayBusError, EventEnvelope } from '@relaybus/core';
2
+
3
+ /** How message bodies are compared: byte for byte, or as JSON regardless of key order and spacing. */
4
+ type BodyComparison = 'bytes' | 'json';
5
+ /** Bytes as text or bytes, as captured or encoded. */
6
+ type WireBytes = string | Uint8Array;
7
+
8
+ /** The two functions of a test framework the codec contract registers its tests with. */
9
+ interface TestFramework {
10
+ readonly describe: (name: string, body: () => void) => void;
11
+ readonly it: (name: string, body: () => Promise<void>) => void;
12
+ }
13
+ /** Thrown by a codec contract assertion: one line per mismatch between the codec and the case. */
14
+ declare class CodecContractError extends RelayBusError {
15
+ name: string;
16
+ readonly code: string;
17
+ readonly mismatches: readonly string[];
18
+ constructor(message: string, mismatches?: readonly string[]);
19
+ }
20
+
21
+ /** A record as captured from a topic: what an existing producer wrote. */
22
+ interface KafkaCapturedRecord {
23
+ readonly topic: string;
24
+ /** Default 0. */
25
+ readonly partition?: number;
26
+ /** Default `'0'`. */
27
+ readonly offset?: string;
28
+ readonly key: WireBytes | null;
29
+ readonly value: WireBytes | null;
30
+ /** Default none. */
31
+ readonly headers?: Readonly<Record<string, WireBytes>>;
32
+ /** Epoch milliseconds in a string; compared only when the codec sets a timestamp. */
33
+ readonly timestamp?: string;
34
+ }
35
+ /** What a codec's `encode` returns: a `KafkaCodec` record. */
36
+ interface KafkaWrittenRecord {
37
+ readonly key: WireBytes | null;
38
+ readonly value: WireBytes | null;
39
+ readonly headers: Readonly<Record<string, WireBytes | readonly WireBytes[]>>;
40
+ readonly timestamp?: string;
41
+ }
42
+ /** A record as a consumer hands it over: bytes for the key, the value and every header. */
43
+ interface KafkaDeliveredRecord {
44
+ readonly topic: string;
45
+ readonly partition: number;
46
+ readonly offset: string;
47
+ readonly key: Buffer | null;
48
+ readonly value: Buffer | null;
49
+ readonly headers: Readonly<Record<string, Buffer>>;
50
+ readonly timestamp?: string;
51
+ }
52
+ /** One message of the format: the event and the record that carries it. */
53
+ interface KafkaCodecCase {
54
+ readonly name: string;
55
+ readonly envelope: EventEnvelope;
56
+ readonly record: KafkaCapturedRecord;
57
+ /**
58
+ * The event decoding the record gives, when the wire does not carry all of `envelope` (for
59
+ * example a record written without a key). Default `envelope`.
60
+ */
61
+ readonly decoded?: EventEnvelope;
62
+ /**
63
+ * Checks one direction only: `'decode'` for a message only other producers write (for
64
+ * example one without an id, which relaybus would add), `'encode'` for one only other
65
+ * consumers read. Default both.
66
+ */
67
+ readonly only?: 'encode' | 'decode';
68
+ }
69
+ interface KafkaCodecContractOptions {
70
+ /** Writes an event: a codec's `encode`, or a transport's publish captured by an injected producer. */
71
+ readonly encode: (envelope: EventEnvelope) => KafkaWrittenRecord | Promise<KafkaWrittenRecord>;
72
+ /** Reads a record the way the transport does: `(record) => decodeKafkaRecord(codec, record)`. */
73
+ readonly decode: (record: KafkaDeliveredRecord) => EventEnvelope | Promise<EventEnvelope>;
74
+ readonly cases: readonly KafkaCodecCase[];
75
+ /** How values are compared. Default `'bytes'`. */
76
+ readonly compare?: BodyComparison;
77
+ /** Headers left out of the comparison, such as ones relaybus adds and other consumers ignore. */
78
+ readonly ignoreHeaders?: readonly string[];
79
+ }
80
+ /**
81
+ * What differs between `testCase` and what the codec writes and reads, one line each: the key,
82
+ * the value, the headers (a repeated header by its last value) and the timestamp of
83
+ * `encode(envelope)`, and `decode(record)` against the expected event. Empty when they agree.
84
+ */
85
+ declare function kafkaCodecMismatches(options: KafkaCodecContractOptions, testCase: KafkaCodecCase): Promise<string[]>;
86
+ /**
87
+ * Throws a {@link CodecContractError} listing what {@link kafkaCodecMismatches} finds, so any test framework
88
+ * (or a plain script) can check one case.
89
+ */
90
+ declare function assertKafkaCodecCase(options: KafkaCodecContractOptions, testCase: KafkaCodecCase): Promise<void>;
91
+ /**
92
+ * Registers one test per case through `framework` (the `describe` and `it` of Jest, Vitest,
93
+ * Mocha or `node:test`), each asserting {@link assertKafkaCodecCase}: a format of existing
94
+ * producers and consumers is adopted only once relaybus writes and reads it exactly.
95
+ */
96
+ declare function registerKafkaCodecContract(framework: TestFramework, { name, options }: {
97
+ readonly name: string;
98
+ readonly options: KafkaCodecContractOptions;
99
+ }): void;
100
+
101
+ /** A message as captured from a queue: what an existing publisher wrote. */
102
+ interface RabbitMqCapturedMessage {
103
+ readonly routingKey: string;
104
+ /**
105
+ * The message properties as publish options name them (`persistent`, `messageId`,
106
+ * `contentType`, `timestamp`, ...), without `headers`. Default none.
107
+ */
108
+ readonly properties?: Readonly<Record<string, unknown>>;
109
+ /** Default none. */
110
+ readonly headers?: Readonly<Record<string, unknown>>;
111
+ readonly body: WireBytes;
112
+ }
113
+ /** What a codec's `encode` returns: a `RabbitMqCodec` message. */
114
+ interface RabbitMqWrittenMessage {
115
+ readonly content: Uint8Array;
116
+ /** The publish options: properties, and the headers under `headers`. */
117
+ readonly options: object;
118
+ }
119
+ /** A message as a consumer receives it. */
120
+ interface RabbitMqDeliveredMessage {
121
+ readonly content: Buffer;
122
+ readonly fields: {
123
+ readonly routingKey: string;
124
+ };
125
+ readonly properties: Readonly<Record<string, unknown>> & {
126
+ readonly headers: Readonly<Record<string, unknown>>;
127
+ };
128
+ }
129
+ /** One message of the format: the event and the message that carries it. */
130
+ interface RabbitMqCodecCase {
131
+ readonly name: string;
132
+ readonly envelope: EventEnvelope;
133
+ readonly message: RabbitMqCapturedMessage;
134
+ /** The event decoding the message gives, when the wire does not carry all of `envelope`. */
135
+ readonly decoded?: EventEnvelope;
136
+ /**
137
+ * Checks one direction only: `'decode'` for a message only other producers write (for
138
+ * example one without an id, which relaybus would add), `'encode'` for one only other
139
+ * consumers read. Default both.
140
+ */
141
+ readonly only?: 'encode' | 'decode';
142
+ }
143
+ interface RabbitMqCodecContractOptions {
144
+ /** Writes an event: a codec's `encode`. The routing key is the route's destination, not the codec's. */
145
+ readonly encode: (envelope: EventEnvelope) => RabbitMqWrittenMessage | Promise<RabbitMqWrittenMessage>;
146
+ /** Reads a message the way the transport does: `(message) => decodeRabbitMqMessage(codec, message)`. */
147
+ readonly decode: (message: RabbitMqDeliveredMessage) => EventEnvelope | Promise<EventEnvelope>;
148
+ readonly cases: readonly RabbitMqCodecCase[];
149
+ /** How bodies are compared. Default `'bytes'`. */
150
+ readonly compare?: BodyComparison;
151
+ /** Properties left out of the comparison, such as a `timestamp` that differs per send. */
152
+ readonly ignoreProperties?: readonly string[];
153
+ /** Headers left out of the comparison. */
154
+ readonly ignoreHeaders?: readonly string[];
155
+ }
156
+ /**
157
+ * What differs between `testCase` and what the codec writes and reads, one line each: the
158
+ * properties, headers and body of `encode(envelope)`, and `decode(message)` against the expected
159
+ * event. Empty when they agree.
160
+ */
161
+ declare function rabbitMqCodecMismatches(options: RabbitMqCodecContractOptions, testCase: RabbitMqCodecCase): Promise<string[]>;
162
+ /**
163
+ * Throws a {@link CodecContractError} listing what {@link rabbitMqCodecMismatches} finds, so any test framework
164
+ * (or a plain script) can check one case.
165
+ */
166
+ declare function assertRabbitMqCodecCase(options: RabbitMqCodecContractOptions, testCase: RabbitMqCodecCase): Promise<void>;
167
+ /**
168
+ * Registers one test per case through `framework` (the `describe` and `it` of Jest, Vitest,
169
+ * Mocha or `node:test`), each asserting {@link assertRabbitMqCodecCase}: a format of existing
170
+ * producers and consumers is adopted only once relaybus writes and reads it exactly.
171
+ */
172
+ declare function registerRabbitMqCodecContract(framework: TestFramework, { name, options }: {
173
+ readonly name: string;
174
+ readonly options: RabbitMqCodecContractOptions;
175
+ }): void;
176
+
177
+ export { CodecContractError, assertKafkaCodecCase, assertRabbitMqCodecCase, kafkaCodecMismatches, rabbitMqCodecMismatches, registerKafkaCodecContract, registerRabbitMqCodecContract };
178
+ export type { BodyComparison, KafkaCapturedRecord, KafkaCodecCase, KafkaCodecContractOptions, KafkaDeliveredRecord, KafkaWrittenRecord, RabbitMqCapturedMessage, RabbitMqCodecCase, RabbitMqCodecContractOptions, RabbitMqDeliveredMessage, RabbitMqWrittenMessage, TestFramework, WireBytes };
@@ -0,0 +1,3 @@
1
+ export { CodecContractError, assertKafkaCodecCase, assertRabbitMqCodecCase, kafkaCodecMismatches, rabbitMqCodecMismatches, registerKafkaCodecContract, registerRabbitMqCodecContract } from './chunk-ZZZXYZJ3.js';
2
+ //# sourceMappingURL=codec-contract.js.map
3
+ //# sourceMappingURL=codec-contract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"codec-contract.js"}