krafka 0.7.0

A pure Rust, async-native Apache Kafka client
Documentation
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
use bytes::{Buf, BufMut, Bytes};

use super::{VersionedDecode, VersionedEncode, non_nullable_bytes, non_nullable_string};
use crate::error::{ErrorCode, KrafkaError, Result};
use crate::protocol::api::ApiKey;
use crate::protocol::primitives::{
    Decode, Encode, KafkaBytes, KafkaString, TaggedFields, TryEncode,
};
use crate::protocol::{array_len_i32, check_compact_array_len, check_decode_array_len};

// ============================================================================
// DescribeGroups API (Key 15)
// ============================================================================

/// DescribeGroups request.
#[derive(Debug, Clone)]
pub struct DescribeGroupsRequest {
    /// Group IDs to describe.
    pub groups: Vec<String>,
    /// Whether to include authorized operations (v3+).
    pub include_authorized_operations: bool,
}

impl DescribeGroupsRequest {
    /// Get the API key.
    pub fn api_key() -> ApiKey {
        ApiKey::DescribeGroups
    }

    /// Encode for version 1–2 (groups array only, no authorized ops).
    pub fn encode_v1(&self, buf: &mut impl BufMut) -> Result<()> {
        buf.put_i32(array_len_i32(self.groups.len())?);
        for group in &self.groups {
            KafkaString::new(group).try_encode(buf)?;
        }
        Ok(())
    }

    /// Encode for version 3–4 (adds include_authorized_operations).
    pub fn encode_v3(&self, buf: &mut impl BufMut) -> Result<()> {
        buf.put_i32(array_len_i32(self.groups.len())?);
        for group in &self.groups {
            KafkaString::new(group).try_encode(buf)?;
        }
        self.include_authorized_operations.encode(buf);
        Ok(())
    }

    /// Encode for version 5–6 (flexible: compact strings + tagged fields).
    pub fn encode_v5(&self, buf: &mut impl BufMut) -> Result<()> {
        let len = u32::try_from(self.groups.len().saturating_add(1))
            .map_err(|_| KrafkaError::protocol("groups array too large"))?;
        crate::util::varint::encode_unsigned_varint(len, buf);
        for group in &self.groups {
            KafkaString::new(group).try_encode_compact(buf)?;
        }
        self.include_authorized_operations.encode(buf);
        TaggedFields::default().try_encode(buf)?;
        Ok(())
    }
}

/// A member in a described group.
#[derive(Debug, Clone)]
pub struct DescribeGroupMember {
    /// Member ID.
    pub member_id: String,
    /// Group instance ID (static membership).
    pub group_instance_id: Option<String>,
    /// Client ID.
    pub client_id: String,
    /// Client host.
    pub client_host: String,
    /// Member metadata.
    pub member_metadata: Bytes,
    /// Member assignment.
    pub member_assignment: Bytes,
}

/// A described group.
#[derive(Debug, Clone)]
pub struct DescribedGroup {
    /// Error code.
    pub error_code: ErrorCode,
    /// Error message (v6+, KIP-1043).
    pub error_message: Option<String>,
    /// Group ID.
    pub group_id: String,
    /// Group state (e.g., "Stable", "Empty", "Dead").
    pub group_state: String,
    /// Protocol type (e.g., "consumer").
    pub protocol_type: String,
    /// Protocol data (assignor name).
    pub protocol_data: String,
    /// Group members.
    pub members: Vec<DescribeGroupMember>,
    /// Authorized operations bitfield (v3+, -2147483648 when not requested).
    pub authorized_operations: i32,
}

/// DescribeGroups response.
#[derive(Debug, Clone)]
pub struct DescribeGroupsResponse {
    /// Throttle time (v1+).
    pub throttle_time_ms: i32,
    /// Described groups.
    pub groups: Vec<DescribedGroup>,
}

impl DescribeGroupsResponse {
    /// Decode from version 1–2 (non-flexible, no authorized_operations, no instance_id).
    pub fn decode_v1(buf: &mut impl Buf) -> Result<Self> {
        let throttle_time_ms = i32::decode(buf)?;
        let group_count = check_decode_array_len(i32::decode(buf)?)?;
        let mut groups = Vec::with_capacity(group_count);

        for _ in 0..group_count {
            let error_code = ErrorCode::from_i16(i16::decode(buf)?);
            let group_id = non_nullable_string("group_id", KafkaString::decode(buf)?.0)?;
            let group_state = non_nullable_string("group_state", KafkaString::decode(buf)?.0)?;
            let protocol_type = non_nullable_string("protocol_type", KafkaString::decode(buf)?.0)?;
            let protocol_data = non_nullable_string("protocol_data", KafkaString::decode(buf)?.0)?;

            let member_count = check_decode_array_len(i32::decode(buf)?)?;
            let mut members = Vec::with_capacity(member_count);
            for _ in 0..member_count {
                let member_id = non_nullable_string("member_id", KafkaString::decode(buf)?.0)?;
                let client_id = non_nullable_string("client_id", KafkaString::decode(buf)?.0)?;
                let client_host = non_nullable_string("client_host", KafkaString::decode(buf)?.0)?;
                let member_metadata =
                    non_nullable_bytes("member_metadata", KafkaBytes::decode(buf)?.0)?;
                let member_assignment =
                    non_nullable_bytes("member_assignment", KafkaBytes::decode(buf)?.0)?;
                members.push(DescribeGroupMember {
                    member_id,
                    group_instance_id: None,
                    client_id,
                    client_host,
                    member_metadata,
                    member_assignment,
                });
            }

            groups.push(DescribedGroup {
                error_code,
                error_message: None,
                group_id,
                group_state,
                protocol_type,
                protocol_data,
                members,
                authorized_operations: i32::MIN,
            });
        }

        Ok(Self {
            throttle_time_ms,
            groups,
        })
    }

    /// Decode from version 3 (adds authorized_operations per group).
    pub fn decode_v3(buf: &mut impl Buf) -> Result<Self> {
        let throttle_time_ms = i32::decode(buf)?;
        let group_count = check_decode_array_len(i32::decode(buf)?)?;
        let mut groups = Vec::with_capacity(group_count);

        for _ in 0..group_count {
            let error_code = ErrorCode::from_i16(i16::decode(buf)?);
            let group_id = non_nullable_string("group_id", KafkaString::decode(buf)?.0)?;
            let group_state = non_nullable_string("group_state", KafkaString::decode(buf)?.0)?;
            let protocol_type = non_nullable_string("protocol_type", KafkaString::decode(buf)?.0)?;
            let protocol_data = non_nullable_string("protocol_data", KafkaString::decode(buf)?.0)?;

            let member_count = check_decode_array_len(i32::decode(buf)?)?;
            let mut members = Vec::with_capacity(member_count);
            for _ in 0..member_count {
                let member_id = non_nullable_string("member_id", KafkaString::decode(buf)?.0)?;
                let client_id = non_nullable_string("client_id", KafkaString::decode(buf)?.0)?;
                let client_host = non_nullable_string("client_host", KafkaString::decode(buf)?.0)?;
                let member_metadata =
                    non_nullable_bytes("member_metadata", KafkaBytes::decode(buf)?.0)?;
                let member_assignment =
                    non_nullable_bytes("member_assignment", KafkaBytes::decode(buf)?.0)?;
                members.push(DescribeGroupMember {
                    member_id,
                    group_instance_id: None,
                    client_id,
                    client_host,
                    member_metadata,
                    member_assignment,
                });
            }

            let authorized_operations = i32::decode(buf)?;

            groups.push(DescribedGroup {
                error_code,
                error_message: None,
                group_id,
                group_state,
                protocol_type,
                protocol_data,
                members,
                authorized_operations,
            });
        }

        Ok(Self {
            throttle_time_ms,
            groups,
        })
    }

    /// Decode from version 4 (adds group_instance_id per member).
    pub fn decode_v4(buf: &mut impl Buf) -> Result<Self> {
        let throttle_time_ms = i32::decode(buf)?;
        let group_count = check_decode_array_len(i32::decode(buf)?)?;
        let mut groups = Vec::with_capacity(group_count);

        for _ in 0..group_count {
            let error_code = ErrorCode::from_i16(i16::decode(buf)?);
            let group_id = non_nullable_string("group_id", KafkaString::decode(buf)?.0)?;
            let group_state = non_nullable_string("group_state", KafkaString::decode(buf)?.0)?;
            let protocol_type = non_nullable_string("protocol_type", KafkaString::decode(buf)?.0)?;
            let protocol_data = non_nullable_string("protocol_data", KafkaString::decode(buf)?.0)?;

            let member_count = check_decode_array_len(i32::decode(buf)?)?;
            let mut members = Vec::with_capacity(member_count);
            for _ in 0..member_count {
                let member_id = non_nullable_string("member_id", KafkaString::decode(buf)?.0)?;
                let group_instance_id = KafkaString::decode(buf)?.0;
                let client_id = non_nullable_string("client_id", KafkaString::decode(buf)?.0)?;
                let client_host = non_nullable_string("client_host", KafkaString::decode(buf)?.0)?;
                let member_metadata =
                    non_nullable_bytes("member_metadata", KafkaBytes::decode(buf)?.0)?;
                let member_assignment =
                    non_nullable_bytes("member_assignment", KafkaBytes::decode(buf)?.0)?;
                members.push(DescribeGroupMember {
                    member_id,
                    group_instance_id,
                    client_id,
                    client_host,
                    member_metadata,
                    member_assignment,
                });
            }

            let authorized_operations = i32::decode(buf)?;

            groups.push(DescribedGroup {
                error_code,
                error_message: None,
                group_id,
                group_state,
                protocol_type,
                protocol_data,
                members,
                authorized_operations,
            });
        }

        Ok(Self {
            throttle_time_ms,
            groups,
        })
    }

    /// Decode from version 5 (flexible: compact strings, tagged fields, group_instance_id).
    pub fn decode_v5(buf: &mut impl Buf) -> Result<Self> {
        let throttle_time_ms = i32::decode(buf)?;
        let group_count =
            check_compact_array_len(crate::util::varint::decode_unsigned_varint(buf)?)?;
        let mut groups = Vec::with_capacity(group_count);

        for _ in 0..group_count {
            let error_code = ErrorCode::from_i16(i16::decode(buf)?);
            let group_id = non_nullable_string("group_id", KafkaString::decode_compact(buf)?.0)?;
            let group_state =
                non_nullable_string("group_state", KafkaString::decode_compact(buf)?.0)?;
            let protocol_type =
                non_nullable_string("protocol_type", KafkaString::decode_compact(buf)?.0)?;
            let protocol_data =
                non_nullable_string("protocol_data", KafkaString::decode_compact(buf)?.0)?;

            let member_count =
                check_compact_array_len(crate::util::varint::decode_unsigned_varint(buf)?)?;
            let mut members = Vec::with_capacity(member_count);
            for _ in 0..member_count {
                let member_id =
                    non_nullable_string("member_id", KafkaString::decode_compact(buf)?.0)?;
                let group_instance_id = KafkaString::decode_compact(buf)?.0;
                let client_id =
                    non_nullable_string("client_id", KafkaString::decode_compact(buf)?.0)?;
                let client_host =
                    non_nullable_string("client_host", KafkaString::decode_compact(buf)?.0)?;
                let member_metadata =
                    non_nullable_bytes("member_metadata", KafkaBytes::decode_compact(buf)?.0)?;
                let member_assignment =
                    non_nullable_bytes("member_assignment", KafkaBytes::decode_compact(buf)?.0)?;
                let _ = TaggedFields::decode(buf)?;
                members.push(DescribeGroupMember {
                    member_id,
                    group_instance_id,
                    client_id,
                    client_host,
                    member_metadata,
                    member_assignment,
                });
            }

            let authorized_operations = i32::decode(buf)?;
            let _ = TaggedFields::decode(buf)?;

            groups.push(DescribedGroup {
                error_code,
                error_message: None,
                group_id,
                group_state,
                protocol_type,
                protocol_data,
                members,
                authorized_operations,
            });
        }

        let _ = TaggedFields::decode(buf)?;

        Ok(Self {
            throttle_time_ms,
            groups,
        })
    }

    /// Decode from version 6 (adds error_message per group, KIP-1043).
    pub fn decode_v6(buf: &mut impl Buf) -> Result<Self> {
        let throttle_time_ms = i32::decode(buf)?;
        let group_count =
            check_compact_array_len(crate::util::varint::decode_unsigned_varint(buf)?)?;
        let mut groups = Vec::with_capacity(group_count);

        for _ in 0..group_count {
            let error_code = ErrorCode::from_i16(i16::decode(buf)?);
            let error_message = KafkaString::decode_compact(buf)?.0;
            let group_id = non_nullable_string("group_id", KafkaString::decode_compact(buf)?.0)?;
            let group_state =
                non_nullable_string("group_state", KafkaString::decode_compact(buf)?.0)?;
            let protocol_type =
                non_nullable_string("protocol_type", KafkaString::decode_compact(buf)?.0)?;
            let protocol_data =
                non_nullable_string("protocol_data", KafkaString::decode_compact(buf)?.0)?;

            let member_count =
                check_compact_array_len(crate::util::varint::decode_unsigned_varint(buf)?)?;
            let mut members = Vec::with_capacity(member_count);
            for _ in 0..member_count {
                let member_id =
                    non_nullable_string("member_id", KafkaString::decode_compact(buf)?.0)?;
                let group_instance_id = KafkaString::decode_compact(buf)?.0;
                let client_id =
                    non_nullable_string("client_id", KafkaString::decode_compact(buf)?.0)?;
                let client_host =
                    non_nullable_string("client_host", KafkaString::decode_compact(buf)?.0)?;
                let member_metadata =
                    non_nullable_bytes("member_metadata", KafkaBytes::decode_compact(buf)?.0)?;
                let member_assignment =
                    non_nullable_bytes("member_assignment", KafkaBytes::decode_compact(buf)?.0)?;
                let _ = TaggedFields::decode(buf)?;
                members.push(DescribeGroupMember {
                    member_id,
                    group_instance_id,
                    client_id,
                    client_host,
                    member_metadata,
                    member_assignment,
                });
            }

            let authorized_operations = i32::decode(buf)?;
            let _ = TaggedFields::decode(buf)?;

            groups.push(DescribedGroup {
                error_code,
                error_message,
                group_id,
                group_state,
                protocol_type,
                protocol_data,
                members,
                authorized_operations,
            });
        }

        let _ = TaggedFields::decode(buf)?;

        Ok(Self {
            throttle_time_ms,
            groups,
        })
    }
}

impl VersionedEncode for DescribeGroupsRequest {
    fn encode_versioned(&self, version: i16, buf: &mut impl BufMut) -> Result<()> {
        match version {
            1 | 2 => self.encode_v1(buf)?,
            3 | 4 => self.encode_v3(buf)?,
            5 | 6 => self.encode_v5(buf)?,
            _ => return unsupported_encode!("DescribeGroupsRequest", version),
        }
        Ok(())
    }
}

impl VersionedDecode for DescribeGroupsResponse {
    fn decode_versioned(version: i16, buf: &mut impl Buf) -> Result<Self> {
        match version {
            1 | 2 => Self::decode_v1(buf),
            3 => Self::decode_v3(buf),
            4 => Self::decode_v4(buf),
            5 => Self::decode_v5(buf),
            6 => Self::decode_v6(buf),
            _ => unsupported_decode!("DescribeGroupsResponse", version),
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;

    use bytes::BytesMut;

    #[test]
    fn test_describe_groups_request() {
        let request = DescribeGroupsRequest {
            groups: vec!["group-1".to_string(), "group-2".to_string()],
            include_authorized_operations: false,
        };
        assert_eq!(request.groups.len(), 2);
        assert_eq!(DescribeGroupsRequest::api_key(), ApiKey::DescribeGroups);

        let mut buf = BytesMut::new();
        request.encode_v1(&mut buf).unwrap();
        assert!(!buf.is_empty());
    }

    #[test]
    fn test_describe_groups_response_decode_v1() {
        // Build a minimal v1 response: throttle_time_ms + 1 group with 0 members
        let mut buf = BytesMut::new();
        // throttle_time_ms
        buf.put_i32(0);
        // group count
        buf.put_i32(1);
        // error_code
        buf.put_i16(0);
        // group_id
        let group_id = "test-group";
        buf.put_i16(group_id.len() as i16);
        buf.put_slice(group_id.as_bytes());
        // group_state
        let state = "Stable";
        buf.put_i16(state.len() as i16);
        buf.put_slice(state.as_bytes());
        // protocol_type
        let ptype = "consumer";
        buf.put_i16(ptype.len() as i16);
        buf.put_slice(ptype.as_bytes());
        // protocol_data
        let pdata = "range";
        buf.put_i16(pdata.len() as i16);
        buf.put_slice(pdata.as_bytes());
        // members count
        buf.put_i32(0);

        let response = DescribeGroupsResponse::decode_v1(&mut buf.freeze()).unwrap();
        assert_eq!(response.throttle_time_ms, 0);
        assert_eq!(response.groups.len(), 1);
        assert_eq!(response.groups[0].group_id, "test-group");
        assert_eq!(response.groups[0].group_state, "Stable");
        assert_eq!(response.groups[0].protocol_type, "consumer");
        assert_eq!(response.groups[0].protocol_data, "range");
        assert!(response.groups[0].error_code.is_ok());
        assert!(response.groups[0].members.is_empty());
    }

    #[test]
    fn test_describe_groups_request_encode_v3_authorized_ops() {
        let request = DescribeGroupsRequest {
            groups: vec!["my-group".to_string()],
            include_authorized_operations: true,
        };

        let mut buf_v1 = BytesMut::new();
        request.encode_v1(&mut buf_v1).unwrap();

        let mut buf_v3 = BytesMut::new();
        request.encode_v3(&mut buf_v3).unwrap();

        // v3 adds the include_authorized_operations bool (1 byte)
        assert_eq!(buf_v3.len(), buf_v1.len() + 1);
    }

    #[test]
    fn test_describe_groups_request_encode_v5_flexible() {
        let request = DescribeGroupsRequest {
            groups: vec!["my-group".to_string()],
            include_authorized_operations: true,
        };

        let mut buf_v3 = BytesMut::new();
        request.encode_v3(&mut buf_v3).unwrap();

        let mut buf_v5 = BytesMut::new();
        request.encode_v5(&mut buf_v5).unwrap();

        // v5 uses compact encoding (varint lengths instead of i32/i16)
        assert!(!buf_v5.is_empty());
        assert_ne!(buf_v3.len(), buf_v5.len());
    }

    #[test]
    fn test_describe_groups_response_decode_v3_authorized_ops() {
        use bytes::BufMut;
        let mut buf = BytesMut::new();

        buf.put_i32(10); // throttle_time_ms
        buf.put_i32(1); // group count

        buf.put_i16(0); // error_code
        let gid = b"test-group";
        buf.put_i16(gid.len() as i16);
        buf.put_slice(gid);
        let state = b"Stable";
        buf.put_i16(state.len() as i16);
        buf.put_slice(state);
        let ptype = b"consumer";
        buf.put_i16(ptype.len() as i16);
        buf.put_slice(ptype);
        let pdata = b"range";
        buf.put_i16(pdata.len() as i16);
        buf.put_slice(pdata);
        buf.put_i32(0); // member count
        buf.put_i32(0x0000_001F); // authorized_operations

        let mut data = buf.freeze();
        let resp = DescribeGroupsResponse::decode_v3(&mut data).unwrap();

        assert_eq!(resp.throttle_time_ms, 10);
        assert_eq!(resp.groups.len(), 1);
        assert_eq!(resp.groups[0].group_id, "test-group");
        assert_eq!(resp.groups[0].authorized_operations, 0x0000_001F);
        assert_eq!(resp.groups[0].error_message, None);
    }

    #[test]
    fn test_describe_groups_response_decode_v4_instance_id() {
        use bytes::BufMut;
        let mut buf = BytesMut::new();

        buf.put_i32(0); // throttle_time_ms
        buf.put_i32(1); // group count

        buf.put_i16(0); // error_code
        let gid = b"grp";
        buf.put_i16(gid.len() as i16);
        buf.put_slice(gid);
        let state = b"Stable";
        buf.put_i16(state.len() as i16);
        buf.put_slice(state);
        let ptype = b"consumer";
        buf.put_i16(ptype.len() as i16);
        buf.put_slice(ptype);
        let pdata = b"range";
        buf.put_i16(pdata.len() as i16);
        buf.put_slice(pdata);

        // 1 member
        buf.put_i32(1);
        let mid = b"member-1";
        buf.put_i16(mid.len() as i16);
        buf.put_slice(mid);
        // group_instance_id (v4+): "inst-1"
        let inst = b"inst-1";
        buf.put_i16(inst.len() as i16);
        buf.put_slice(inst);
        let cid = b"client-1";
        buf.put_i16(cid.len() as i16);
        buf.put_slice(cid);
        let host = b"/10.0.0.1";
        buf.put_i16(host.len() as i16);
        buf.put_slice(host);
        buf.put_i32(0); // member_metadata (empty bytes)
        buf.put_i32(0); // member_assignment (empty bytes)

        buf.put_i32(i32::MIN); // authorized_operations (not requested)

        let mut data = buf.freeze();
        let resp = DescribeGroupsResponse::decode_v4(&mut data).unwrap();

        assert_eq!(resp.groups.len(), 1);
        assert_eq!(resp.groups[0].members.len(), 1);
        assert_eq!(
            resp.groups[0].members[0].group_instance_id,
            Some("inst-1".to_string())
        );
        assert_eq!(resp.groups[0].members[0].client_id, "client-1");
        assert_eq!(resp.groups[0].members[0].client_host, "/10.0.0.1");
        assert_eq!(resp.groups[0].authorized_operations, i32::MIN);
    }

    #[test]
    fn test_describe_groups_response_decode_v5_flexible() {
        use bytes::BufMut;
        let mut buf = BytesMut::new();

        buf.put_i32(5); // throttle_time_ms

        // groups compact array: 1 group → varint(2)
        crate::util::varint::encode_unsigned_varint(2, &mut buf);
        {
            buf.put_i16(0); // error_code

            // group_id (compact): "grp"
            crate::util::varint::encode_unsigned_varint(4, &mut buf);
            buf.put_slice(b"grp");
            // group_state (compact): "Empty"
            crate::util::varint::encode_unsigned_varint(6, &mut buf);
            buf.put_slice(b"Empty");
            // protocol_type (compact): "consumer"
            crate::util::varint::encode_unsigned_varint(9, &mut buf);
            buf.put_slice(b"consumer");
            // protocol_data (compact): ""
            crate::util::varint::encode_unsigned_varint(1, &mut buf);

            // members compact array: 0 → varint(1)
            crate::util::varint::encode_unsigned_varint(1, &mut buf);

            buf.put_i32(0x0F); // authorized_operations
            // group tagged fields
            crate::util::varint::encode_unsigned_varint(0, &mut buf);
        }

        // top-level tagged fields
        crate::util::varint::encode_unsigned_varint(0, &mut buf);

        let mut data = buf.freeze();
        let resp = DescribeGroupsResponse::decode_v5(&mut data).unwrap();

        assert_eq!(resp.throttle_time_ms, 5);
        assert_eq!(resp.groups.len(), 1);
        assert_eq!(resp.groups[0].group_id, "grp");
        assert_eq!(resp.groups[0].group_state, "Empty");
        assert_eq!(resp.groups[0].error_message, None);
        assert_eq!(resp.groups[0].authorized_operations, 0x0F);
        assert!(resp.groups[0].members.is_empty());
    }

    #[test]
    fn test_describe_groups_response_decode_v6_error_message() {
        use bytes::BufMut;
        let mut buf = BytesMut::new();

        buf.put_i32(0); // throttle_time_ms

        // groups compact array: 1 group → varint(2)
        crate::util::varint::encode_unsigned_varint(2, &mut buf);
        {
            buf.put_i16(69); // error_code (GROUP_ID_NOT_FOUND)

            // error_message (compact nullable): "group not found"
            let msg = b"group not found";
            crate::util::varint::encode_unsigned_varint((msg.len() + 1) as u32, &mut buf);
            buf.put_slice(msg);

            // group_id (compact): "missing-grp"
            let gid = b"missing-grp";
            crate::util::varint::encode_unsigned_varint((gid.len() + 1) as u32, &mut buf);
            buf.put_slice(gid);
            // group_state (compact): ""
            crate::util::varint::encode_unsigned_varint(1, &mut buf);
            // protocol_type (compact): ""
            crate::util::varint::encode_unsigned_varint(1, &mut buf);
            // protocol_data (compact): ""
            crate::util::varint::encode_unsigned_varint(1, &mut buf);

            // members compact array: 0 → varint(1)
            crate::util::varint::encode_unsigned_varint(1, &mut buf);

            buf.put_i32(i32::MIN); // authorized_operations
            // group tagged fields
            crate::util::varint::encode_unsigned_varint(0, &mut buf);
        }

        // top-level tagged fields
        crate::util::varint::encode_unsigned_varint(0, &mut buf);

        let mut data = buf.freeze();
        let resp = DescribeGroupsResponse::decode_v6(&mut data).unwrap();

        assert_eq!(resp.groups.len(), 1);
        assert!(!resp.groups[0].error_code.is_ok());
        assert_eq!(
            resp.groups[0].error_message,
            Some("group not found".to_string())
        );
        assert_eq!(resp.groups[0].group_id, "missing-grp");
    }
}