1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! Codec fixture functions for generating valid and invalid Hotline-framed
//! wire bytes.
//!
//! These helpers produce raw byte sequences suitable for feeding to
//! [`decode_frames_with_codec`](super::decode_frames_with_codec) or directly
//! to a `HotlineAdapter` decoder. Four categories of fixtures are provided:
//!
//! - **Valid frames** — well-formed wire bytes that decode cleanly.
//! - **Invalid frames** — wire bytes triggering decoder errors (oversized payloads, mismatched
//! sizes).
//! - **Incomplete frames** — truncated data that causes trailing-byte errors.
//! - **Correlation metadata** — multi-frame sequences with specific transaction IDs for correlation
//! testing.
//!
//! Fixtures construct raw bytes directly rather than using the tokio-util
//! encoder, ensuring they are independent of the encoder implementation and
//! can represent malformed data that the encoder would reject.
use Bytes;
use ;
/// A transaction identifier for Hotline protocol frames.
;
/// Maximum permitted frame length for a Hotline codec.
;
/// Payload length in bytes for a Hotline frame fixture.
;
/// Hotline header length in bytes: `data_size` (4) + `total_size` (4) +
/// `transaction_id` (4) + reserved (8).
const HEADER_LEN: usize = 20;
/// Build a single valid Hotline frame as raw wire bytes.
///
/// Writes the 20-byte Hotline header (`data_size`, `total_size`,
/// `transaction_id`, 8 reserved zero bytes) followed by `payload`.
///
/// # Examples
///
/// ```rust
/// use wireframe::codec::examples::HotlineFrameCodec;
/// use wireframe_testing::{decode_frames_with_codec, valid_hotline_wire};
///
/// let wire = valid_hotline_wire(b"hello", 7);
/// let codec = HotlineFrameCodec::new(4096);
/// let frames = decode_frames_with_codec(&codec, wire).expect("valid fixture should decode");
/// assert_eq!(frames.len(), 1);
/// ```
/// Return a typed [`HotlineFrame`] with the given payload and transaction ID.
///
/// Useful when a test needs to inspect frame metadata without going through
/// the wire-encode/decode cycle.
///
/// # Examples
///
/// ```rust
/// use wireframe_testing::valid_hotline_frame;
///
/// let frame = valid_hotline_frame(b"data", 42);
/// assert_eq!(frame.transaction_id, 42);
/// assert_eq!(&*frame.payload, b"data");
/// ```
/// Build a Hotline frame whose `data_size` exceeds `max_frame_length` by one
/// byte.
///
/// The Hotline decoder rejects frames where `data_size > max_frame_length`
/// with an `InvalidData("payload too large")` error.
///
/// # Examples
///
/// ```rust
/// use wireframe::codec::examples::HotlineFrameCodec;
/// use wireframe_testing::{decode_frames_with_codec, oversized_hotline_wire};
///
/// let wire = oversized_hotline_wire(64);
/// let codec = HotlineFrameCodec::new(64);
/// let err =
/// decode_frames_with_codec(&codec, wire).expect_err("oversized frame should be rejected");
/// assert!(err.to_string().contains("payload too large"));
/// ```
/// Build a Hotline frame with a mismatched `total_size` field.
///
/// The header's `total_size` is set to `data_size + 21` (one byte more than
/// the correct value of `data_size + 20`). The Hotline decoder rejects this
/// with an `InvalidData("invalid total size")` error.
///
/// # Examples
///
/// ```rust
/// use wireframe::codec::examples::HotlineFrameCodec;
/// use wireframe_testing::{decode_frames_with_codec, mismatched_total_size_wire};
///
/// let wire = mismatched_total_size_wire(b"test");
/// let codec = HotlineFrameCodec::new(4096);
/// let err = decode_frames_with_codec(&codec, wire)
/// .expect_err("mismatched total_size should be rejected");
/// assert!(err.to_string().contains("invalid total size"));
/// ```
/// Return fewer than 20 bytes — a truncated Hotline header.
///
/// The Hotline decoder returns `Ok(None)` for this input (not enough bytes
/// to parse the header). When passed to
/// [`decode_frames_with_codec`](super::decode_frames_with_codec), the
/// `decode_eof` call detects unconsumed bytes and produces an
/// `InvalidData` error containing "bytes remaining".
///
/// # Examples
///
/// ```rust
/// use wireframe::codec::examples::HotlineFrameCodec;
/// use wireframe_testing::{decode_frames_with_codec, truncated_hotline_header};
///
/// let wire = truncated_hotline_header();
/// let codec = HotlineFrameCodec::new(4096);
/// let err = decode_frames_with_codec(&codec, wire)
/// .expect_err("truncated header should cause a decode error");
/// assert!(err.to_string().contains("bytes remaining"));
/// ```
/// Return a valid Hotline header claiming `payload_len` bytes of payload,
/// but provide only half the payload bytes.
///
/// The Hotline decoder returns `Ok(None)` because the buffer is shorter than
/// `total_size`. When passed to
/// [`decode_frames_with_codec`](super::decode_frames_with_codec), the
/// `decode_eof` call detects unconsumed bytes and produces an
/// `InvalidData` error containing "bytes remaining".
///
/// # Examples
///
/// ```rust
/// use wireframe::codec::examples::HotlineFrameCodec;
/// use wireframe_testing::{decode_frames_with_codec, truncated_hotline_payload};
///
/// let wire = truncated_hotline_payload(100);
/// let codec = HotlineFrameCodec::new(4096);
/// let err = decode_frames_with_codec(&codec, wire)
/// .expect_err("truncated payload should cause a decode error");
/// assert!(err.to_string().contains("bytes remaining"));
/// ```
/// Encode multiple Hotline frames sharing the same `transaction_id`.
///
/// Suitable for verifying that correlation ID propagation works across frame
/// sequences. All frames in the returned byte vector carry the same
/// transaction identifier.
///
/// # Examples
///
/// ```rust
/// use wireframe::codec::examples::HotlineFrameCodec;
/// use wireframe_testing::{correlated_hotline_wire, decode_frames_with_codec};
///
/// let wire = correlated_hotline_wire(42, &[b"a", b"b"]);
/// let codec = HotlineFrameCodec::new(4096);
/// let frames = decode_frames_with_codec(&codec, wire).expect("correlated fixtures should decode");
/// assert_eq!(frames.len(), 2);
/// assert!(frames.iter().all(|f| f.transaction_id == 42));
/// ```
/// Encode multiple Hotline frames with incrementing transaction IDs.
///
/// The first frame carries `base_transaction_id`, the second
/// `base_transaction_id + 1`, and so on. Suitable for verifying frame
/// ordering or sequential correlation.
///
/// # Examples
///
/// ```rust
/// use wireframe::codec::examples::HotlineFrameCodec;
/// use wireframe_testing::{decode_frames_with_codec, sequential_hotline_wire};
///
/// let wire = sequential_hotline_wire(10, &[b"x", b"y", b"z"]);
/// let codec = HotlineFrameCodec::new(4096);
/// let frames = decode_frames_with_codec(&codec, wire).expect("sequential fixtures should decode");
/// assert_eq!(frames.len(), 3);
/// ```
// ── Internal helpers ────────────────────────────────────────────────────
/// Construct a Hotline wire frame with explicit control over header fields.
///
/// When `correct_total_size` is `true`, `total_size` is set to
/// `data_size + HEADER_LEN`. When `false`, `total_size` is set to
/// `data_size + HEADER_LEN + 1` (deliberately wrong).
/// Convert a `usize` to `u32`, saturating at `u32::MAX`.
///
/// Fixture payloads are always small enough to fit in `u32`, but we avoid a
/// truncating cast to satisfy Clippy.