ringline-momento 0.3.0

Momento cache client for the ringline io_uring runtime
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
//! Minimal protobuf encoding/decoding for Momento cache messages.
//!
//! This implements just enough protobuf wire format to encode/decode
//! the protosocket cache API messages without requiring prost or other heavy deps.

use bytes::Bytes;

/// Wire type for varint (int32, int64, uint32, uint64, bool, enum).
const WIRE_TYPE_VARINT: u8 = 0;
/// Wire type for length-delimited (string, bytes, embedded messages).
const WIRE_TYPE_LEN: u8 = 2;

/// Encode a varint.
pub fn encode_varint(mut value: u64, buf: &mut Vec<u8>) {
    loop {
        let mut byte = (value & 0x7F) as u8;
        value >>= 7;
        if value != 0 {
            byte |= 0x80;
        }
        buf.push(byte);
        if value == 0 {
            break;
        }
    }
}

/// Decode a varint from a buffer.
pub fn decode_varint(buf: &mut &[u8]) -> Option<u64> {
    let mut result: u64 = 0;
    let mut shift = 0;

    loop {
        if buf.is_empty() {
            return None;
        }
        let byte = buf[0];
        *buf = &buf[1..];

        result |= ((byte & 0x7F) as u64) << shift;
        if byte & 0x80 == 0 {
            return Some(result);
        }
        shift += 7;
        if shift >= 64 {
            return None; // Overflow
        }
    }
}

/// Encode a field tag.
pub fn encode_tag(field_number: u32, wire_type: u8, buf: &mut Vec<u8>) {
    encode_varint(((field_number as u64) << 3) | (wire_type as u64), buf);
}

/// Decode a field tag, returning (field_number, wire_type).
pub fn decode_tag(buf: &mut &[u8]) -> Option<(u32, u8)> {
    let tag = decode_varint(buf)?;
    let field_number = (tag >> 3) as u32;
    let wire_type = (tag & 0x07) as u8;
    Some((field_number, wire_type))
}

/// Encode a bytes field.
pub fn encode_bytes(field_number: u32, data: &[u8], buf: &mut Vec<u8>) {
    encode_tag(field_number, WIRE_TYPE_LEN, buf);
    encode_varint(data.len() as u64, buf);
    buf.extend_from_slice(data);
}

/// Encode a string field (same as bytes in protobuf).
pub fn encode_string(field_number: u32, s: &str, buf: &mut Vec<u8>) {
    encode_bytes(field_number, s.as_bytes(), buf);
}

/// Encode a uint64 field.
pub fn encode_uint64(field_number: u32, value: u64, buf: &mut Vec<u8>) {
    encode_tag(field_number, WIRE_TYPE_VARINT, buf);
    encode_varint(value, buf);
}

/// Encode an embedded message field.
pub fn encode_message(field_number: u32, message: &[u8], buf: &mut Vec<u8>) {
    encode_tag(field_number, WIRE_TYPE_LEN, buf);
    encode_varint(message.len() as u64, buf);
    buf.extend_from_slice(message);
}

/// Compute the encoded size of a varint.
pub fn varint_size(mut value: u64) -> usize {
    let mut size = 1;
    while value >= 0x80 {
        value >>= 7;
        size += 1;
    }
    size
}

/// Compute the encoded size of a field tag.
fn tag_size(field_number: u32) -> usize {
    varint_size(((field_number as u64) << 3) | (WIRE_TYPE_LEN as u64))
}

/// Compute the encoded size of a bytes/string field (tag + length varint + data).
fn field_size_bytes(field_number: u32, data: &[u8]) -> usize {
    tag_size(field_number) + varint_size(data.len() as u64) + data.len()
}

/// Compute the encoded size of a string field.
fn field_size_string(field_number: u32, s: &str) -> usize {
    field_size_bytes(field_number, s.as_bytes())
}

/// Compute the encoded size of a uint64 field (tag + varint value).
fn field_size_uint64(field_number: u32, value: u64) -> usize {
    varint_size(((field_number as u64) << 3) | (WIRE_TYPE_VARINT as u64)) + varint_size(value)
}

/// Decode a length-delimited field, returning the bytes.
pub fn decode_length_delimited<'a>(buf: &mut &'a [u8]) -> Option<&'a [u8]> {
    let len = decode_varint(buf)? as usize;
    if buf.len() < len {
        return None;
    }
    let data = &buf[..len];
    *buf = &buf[len..];
    Some(data)
}

/// Skip a field based on its wire type.
pub fn skip_field(wire_type: u8, buf: &mut &[u8]) -> Option<()> {
    match wire_type {
        WIRE_TYPE_VARINT => {
            decode_varint(buf)?;
        }
        WIRE_TYPE_LEN => {
            decode_length_delimited(buf)?;
        }
        1 => {
            // 64-bit fixed
            if buf.len() < 8 {
                return None;
            }
            *buf = &buf[8..];
        }
        5 => {
            // 32-bit fixed
            if buf.len() < 4 {
                return None;
            }
            *buf = &buf[4..];
        }
        _ => return None,
    }
    Some(())
}

// ============================================================================
// Protosocket Wire Format Messages
// ============================================================================

/// Control codes for protosocket messages.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u32)]
pub enum ControlCode {
    /// Normal message.
    #[default]
    Normal = 0,
    /// Cancel the RPC.
    Cancel = 1,
    /// End of stream (for streaming RPCs).
    End = 2,
}

impl ControlCode {
    pub fn from_u32(value: u32) -> Self {
        match value {
            0 => ControlCode::Normal,
            1 => ControlCode::Cancel,
            2 => ControlCode::End,
            _ => ControlCode::Normal,
        }
    }
}

/// Status codes for protosocket responses (matches gRPC codes).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u32)]
pub enum StatusCode {
    #[default]
    Ok = 0,
    Cancelled = 1,
    Unknown = 2,
    InvalidArgument = 3,
    DeadlineExceeded = 4,
    NotFound = 5,
    AlreadyExists = 6,
    PermissionDenied = 7,
    ResourceExhausted = 8,
    FailedPrecondition = 9,
    Aborted = 10,
    OutOfRange = 11,
    Unimplemented = 12,
    Internal = 13,
    Unavailable = 14,
    DataLoss = 15,
    Unauthenticated = 16,
}

impl StatusCode {
    pub fn from_u32(value: u32) -> Self {
        match value {
            0 => StatusCode::Ok,
            1 => StatusCode::Cancelled,
            2 => StatusCode::Unknown,
            3 => StatusCode::InvalidArgument,
            4 => StatusCode::DeadlineExceeded,
            5 => StatusCode::NotFound,
            6 => StatusCode::AlreadyExists,
            7 => StatusCode::PermissionDenied,
            8 => StatusCode::ResourceExhausted,
            9 => StatusCode::FailedPrecondition,
            10 => StatusCode::Aborted,
            11 => StatusCode::OutOfRange,
            12 => StatusCode::Unimplemented,
            13 => StatusCode::Internal,
            14 => StatusCode::Unavailable,
            15 => StatusCode::DataLoss,
            16 => StatusCode::Unauthenticated,
            _ => StatusCode::Unknown,
        }
    }
}

/// Error from a protosocket command.
#[derive(Debug, Clone)]
pub struct CommandError {
    pub code: StatusCode,
    pub message: String,
}

impl CommandError {
    pub fn encode(&self) -> Vec<u8> {
        let mut buf = Vec::with_capacity(32);
        self.encode_into(&mut buf);
        buf
    }

    pub fn encode_into(&self, buf: &mut Vec<u8>) {
        encode_uint64(1, self.code as u64, buf);
        if !self.message.is_empty() {
            encode_string(2, &self.message, buf);
        }
    }

    pub fn encoded_size(&self) -> usize {
        let mut size = field_size_uint64(1, self.code as u64);
        if !self.message.is_empty() {
            size += field_size_string(2, &self.message);
        }
        size
    }

    pub fn decode(data: &[u8]) -> Option<Self> {
        let mut buf = data;
        let mut code = StatusCode::Unknown;
        let mut message = String::new();

        while !buf.is_empty() {
            let (field_number, wire_type) = decode_tag(&mut buf)?;
            match field_number {
                1 => code = StatusCode::from_u32(decode_varint(&mut buf)? as u32),
                2 => {
                    let bytes = decode_length_delimited(&mut buf)?;
                    message = String::from_utf8_lossy(bytes).into_owned();
                }
                _ => skip_field(wire_type, &mut buf)?,
            }
        }

        Some(Self { code, message })
    }
}

/// Unary command types for protosocket.
#[derive(Debug, Clone)]
pub enum UnaryCommand {
    Authenticate {
        auth_token: String,
    },
    Get {
        namespace: Bytes,
        key: Bytes,
    },
    Set {
        namespace: Bytes,
        key: Bytes,
        value: Bytes,
        ttl_millis: u64,
    },
    Delete {
        namespace: Bytes,
        key: Bytes,
    },
}

impl UnaryCommand {
    /// Encode the inner command fields (without the Unary wrapper) into `buf`.
    fn encode_inner_into(&self, buf: &mut Vec<u8>) {
        match self {
            UnaryCommand::Authenticate { auth_token } => {
                encode_string(1, auth_token, buf);
            }
            UnaryCommand::Get { namespace, key } => {
                encode_bytes(1, namespace, buf);
                encode_bytes(2, key, buf);
            }
            UnaryCommand::Set {
                namespace,
                key,
                value,
                ttl_millis,
            } => {
                encode_bytes(1, namespace, buf);
                encode_bytes(2, key, buf);
                encode_bytes(3, value, buf);
                encode_uint64(4, *ttl_millis, buf);
            }
            UnaryCommand::Delete { namespace, key } => {
                encode_bytes(1, namespace, buf);
                encode_bytes(2, key, buf);
            }
        }
    }

    /// Compute the encoded size of the inner command fields (without tags or length prefixes).
    fn inner_encoded_size(&self) -> usize {
        match self {
            UnaryCommand::Authenticate { auth_token } => field_size_string(1, auth_token),
            UnaryCommand::Get { namespace, key } => {
                field_size_bytes(1, namespace) + field_size_bytes(2, key)
            }
            UnaryCommand::Set {
                namespace,
                key,
                value,
                ttl_millis,
            } => {
                field_size_bytes(1, namespace)
                    + field_size_bytes(2, key)
                    + field_size_bytes(3, value)
                    + field_size_uint64(4, *ttl_millis)
            }
            UnaryCommand::Delete { namespace, key } => {
                field_size_bytes(1, namespace) + field_size_bytes(2, key)
            }
        }
    }

    /// The protobuf field number for this command variant within the Unary message.
    fn field_number(&self) -> u32 {
        match self {
            UnaryCommand::Authenticate { .. } => 1,
            UnaryCommand::Get { .. } => 2,
            UnaryCommand::Set { .. } => 3,
            UnaryCommand::Delete { .. } => 4,
        }
    }

    /// Encode as a Unary message with the command in the appropriate field.
    pub fn encode(&self) -> Vec<u8> {
        let mut buf = Vec::with_capacity(256);
        self.encode_into(&mut buf);
        buf
    }

    /// Compute the total encoded size of the Unary message (tag + length prefix + inner).
    pub fn encoded_size(&self) -> usize {
        let inner_size = self.inner_encoded_size();
        tag_size(self.field_number()) + varint_size(inner_size as u64) + inner_size
    }

    /// Encode as a Unary message directly into `buf` (single-pass, no intermediate allocations).
    pub fn encode_into(&self, buf: &mut Vec<u8>) {
        let inner_size = self.inner_encoded_size();
        // Unary wrapper: tag + length prefix + inner fields
        encode_tag(self.field_number(), WIRE_TYPE_LEN, buf);
        encode_varint(inner_size as u64, buf);
        self.encode_inner_into(buf);
    }

    /// Decode a Unary message.
    pub fn decode(data: &[u8]) -> Option<Self> {
        let mut buf = data;

        while !buf.is_empty() {
            let (field_number, wire_type) = decode_tag(&mut buf)?;
            if wire_type != WIRE_TYPE_LEN {
                skip_field(wire_type, &mut buf)?;
                continue;
            }

            let inner = decode_length_delimited(&mut buf)?;
            return match field_number {
                1 => Self::decode_authenticate(inner),
                2 => Self::decode_get(inner),
                3 => Self::decode_set(inner),
                4 => Self::decode_delete(inner),
                _ => None,
            };
        }

        None
    }

    fn decode_authenticate(data: &[u8]) -> Option<Self> {
        let mut buf = data;
        let mut auth_token = String::new();

        while !buf.is_empty() {
            let (field_number, wire_type) = decode_tag(&mut buf)?;
            match field_number {
                1 => {
                    let bytes = decode_length_delimited(&mut buf)?;
                    auth_token = String::from_utf8_lossy(bytes).into_owned();
                }
                _ => skip_field(wire_type, &mut buf)?,
            }
        }

        Some(UnaryCommand::Authenticate { auth_token })
    }

    fn decode_get(data: &[u8]) -> Option<Self> {
        let mut buf = data;
        let mut namespace = Bytes::new();
        let mut key = Bytes::new();

        while !buf.is_empty() {
            let (field_number, wire_type) = decode_tag(&mut buf)?;
            match field_number {
                1 => {
                    let bytes = decode_length_delimited(&mut buf)?;
                    namespace = Bytes::copy_from_slice(bytes);
                }
                2 => {
                    let bytes = decode_length_delimited(&mut buf)?;
                    key = Bytes::copy_from_slice(bytes);
                }
                _ => skip_field(wire_type, &mut buf)?,
            }
        }

        Some(UnaryCommand::Get { namespace, key })
    }

    fn decode_set(data: &[u8]) -> Option<Self> {
        let mut buf = data;
        let mut namespace = Bytes::new();
        let mut key = Bytes::new();
        let mut value = Bytes::new();
        let mut ttl_millis = 0u64;

        while !buf.is_empty() {
            let (field_number, wire_type) = decode_tag(&mut buf)?;
            match field_number {
                1 => {
                    let bytes = decode_length_delimited(&mut buf)?;
                    namespace = Bytes::copy_from_slice(bytes);
                }
                2 => {
                    let bytes = decode_length_delimited(&mut buf)?;
                    key = Bytes::copy_from_slice(bytes);
                }
                3 => {
                    let bytes = decode_length_delimited(&mut buf)?;
                    value = Bytes::copy_from_slice(bytes);
                }
                4 => {
                    ttl_millis = decode_varint(&mut buf)?;
                }
                _ => skip_field(wire_type, &mut buf)?,
            }
        }

        Some(UnaryCommand::Set {
            namespace,
            key,
            value,
            ttl_millis,
        })
    }

    fn decode_delete(data: &[u8]) -> Option<Self> {
        let mut buf = data;
        let mut namespace = Bytes::new();
        let mut key = Bytes::new();

        while !buf.is_empty() {
            let (field_number, wire_type) = decode_tag(&mut buf)?;
            match field_number {
                1 => {
                    let bytes = decode_length_delimited(&mut buf)?;
                    namespace = Bytes::copy_from_slice(bytes);
                }
                2 => {
                    let bytes = decode_length_delimited(&mut buf)?;
                    key = Bytes::copy_from_slice(bytes);
                }
                _ => skip_field(wire_type, &mut buf)?,
            }
        }

        Some(UnaryCommand::Delete { namespace, key })
    }
}

/// A command sent from client to server over protosocket.
#[derive(Debug, Clone)]
pub struct CacheCommand {
    pub message_id: u64,
    pub control_code: ControlCode,
    pub command: Option<UnaryCommand>,
}

impl CacheCommand {
    /// Create a new command with the given message ID.
    pub fn new(message_id: u64, command: UnaryCommand) -> Self {
        Self {
            message_id,
            control_code: ControlCode::Normal,
            command: Some(command),
        }
    }

    /// Create a cancel command.
    pub fn cancel(message_id: u64) -> Self {
        Self {
            message_id,
            control_code: ControlCode::Cancel,
            command: None,
        }
    }

    /// Encode the command to bytes.
    pub fn encode(&self) -> Vec<u8> {
        let mut buf = Vec::with_capacity(256);
        self.encode_into(&mut buf);
        buf
    }

    /// Encode directly into `buf` (single-pass, no intermediate allocations).
    pub fn encode_into(&self, buf: &mut Vec<u8>) {
        // Field 2: message_id (per official proto)
        encode_uint64(2, self.message_id, buf);

        // Field 3: control_code (per official proto)
        encode_uint64(3, self.control_code as u64, buf);

        // Field 10: unary command (per official proto)
        if let Some(ref cmd) = self.command {
            // Write the field 10 tag + length prefix, then encode inner fields directly.
            let unary_size = cmd.encoded_size();
            encode_tag(10, WIRE_TYPE_LEN, buf);
            encode_varint(unary_size as u64, buf);
            cmd.encode_into(buf);
        }
    }

    /// Compute the encoded size of the command (without length-delimited prefix).
    fn encoded_size(&self) -> usize {
        let mut size =
            field_size_uint64(2, self.message_id) + field_size_uint64(3, self.control_code as u64);
        if let Some(ref cmd) = self.command {
            let unary_size = cmd.encoded_size();
            size += tag_size(10) + varint_size(unary_size as u64) + unary_size;
        }
        size
    }

    /// Encode with length prefix for protosocket wire format (single allocation).
    pub fn encode_length_delimited(&self) -> Vec<u8> {
        let msg_size = self.encoded_size();
        let mut buf = Vec::with_capacity(varint_size(msg_size as u64) + msg_size);
        self.encode_length_delimited_into(&mut buf);
        buf
    }

    /// Encode with length prefix directly into `buf` (zero intermediate allocations).
    pub fn encode_length_delimited_into(&self, buf: &mut Vec<u8>) {
        let msg_size = self.encoded_size();
        encode_varint(msg_size as u64, buf);
        self.encode_into(buf);
    }

    /// Decode a command from bytes.
    pub fn decode(data: &[u8]) -> Option<Self> {
        let mut buf = data;
        let mut message_id = 0u64;
        let mut control_code = ControlCode::Normal;
        let mut command = None;

        while !buf.is_empty() {
            let (field_number, wire_type) = decode_tag(&mut buf)?;
            match field_number {
                2 => message_id = decode_varint(&mut buf)?,
                3 => control_code = ControlCode::from_u32(decode_varint(&mut buf)? as u32),
                10 => {
                    let unary_data = decode_length_delimited(&mut buf)?;
                    command = UnaryCommand::decode(unary_data);
                }
                _ => skip_field(wire_type, &mut buf)?,
            }
        }

        Some(Self {
            message_id,
            control_code,
            command,
        })
    }
}

/// A response sent from server to client over protosocket.
#[derive(Debug, Clone)]
pub struct CacheResponse {
    pub message_id: u64,
    pub control_code: ControlCode,
    pub result: CacheResponseResult,
}

/// The result portion of a CacheResponse.
#[derive(Debug, Clone)]
pub enum CacheResponseResult {
    Authenticate,
    Get { value: Option<Bytes> },
    Set,
    Delete,
    Error(CommandError),
}

impl CacheResponse {
    /// Create a successful authenticate response.
    pub fn authenticate(message_id: u64) -> Self {
        Self {
            message_id,
            control_code: ControlCode::Normal,
            result: CacheResponseResult::Authenticate,
        }
    }

    /// Create a successful get response (hit).
    pub fn get_hit(message_id: u64, value: Bytes) -> Self {
        Self {
            message_id,
            control_code: ControlCode::Normal,
            result: CacheResponseResult::Get { value: Some(value) },
        }
    }

    /// Create a get miss response.
    pub fn get_miss(message_id: u64) -> Self {
        Self {
            message_id,
            control_code: ControlCode::Normal,
            result: CacheResponseResult::Get { value: None },
        }
    }

    /// Create a successful set response.
    pub fn set_ok(message_id: u64) -> Self {
        Self {
            message_id,
            control_code: ControlCode::Normal,
            result: CacheResponseResult::Set,
        }
    }

    /// Create a successful delete response.
    pub fn delete_ok(message_id: u64) -> Self {
        Self {
            message_id,
            control_code: ControlCode::Normal,
            result: CacheResponseResult::Delete,
        }
    }

    /// Create an error response.
    pub fn error(message_id: u64, code: StatusCode, message: impl Into<String>) -> Self {
        Self {
            message_id,
            control_code: ControlCode::Normal,
            result: CacheResponseResult::Error(CommandError {
                code,
                message: message.into(),
            }),
        }
    }

    /// Encode the response to bytes.
    pub fn encode(&self) -> Vec<u8> {
        let mut buf = Vec::with_capacity(256);
        self.encode_into(&mut buf);
        buf
    }

    /// Encode directly into `buf` (single-pass, no intermediate allocations).
    pub fn encode_into(&self, buf: &mut Vec<u8>) {
        // Field 1: message_id
        encode_uint64(1, self.message_id, buf);

        // Field 2: control_code
        encode_uint64(2, self.control_code as u64, buf);

        // Response kind (per official proto field numbers)
        match &self.result {
            CacheResponseResult::Error(err) => {
                // Field 9: error — encode inline
                let inner_size = err.encoded_size();
                encode_tag(9, WIRE_TYPE_LEN, buf);
                encode_varint(inner_size as u64, buf);
                err.encode_into(buf);
            }
            CacheResponseResult::Authenticate => {
                // Field 10: authenticate response (empty)
                encode_message(10, &[], buf);
            }
            CacheResponseResult::Get { value } => {
                // Field 11: get response — encode inline
                let inner_size = match value {
                    Some(v) => field_size_bytes(1, v),
                    None => 0,
                };
                encode_tag(11, WIRE_TYPE_LEN, buf);
                encode_varint(inner_size as u64, buf);
                if let Some(v) = value {
                    encode_bytes(1, v, buf);
                }
            }
            CacheResponseResult::Set => {
                // Field 12: set response (empty)
                encode_message(12, &[], buf);
            }
            CacheResponseResult::Delete => {
                // Field 13: delete response (empty)
                encode_message(13, &[], buf);
            }
        }
    }

    /// Compute the encoded size of the response (without length-delimited prefix).
    fn encoded_size(&self) -> usize {
        let mut size =
            field_size_uint64(1, self.message_id) + field_size_uint64(2, self.control_code as u64);
        match &self.result {
            CacheResponseResult::Error(err) => {
                let inner = err.encoded_size();
                size += tag_size(9) + varint_size(inner as u64) + inner;
            }
            CacheResponseResult::Authenticate => {
                size += tag_size(10) + varint_size(0);
            }
            CacheResponseResult::Get { value } => {
                let inner = match value {
                    Some(v) => field_size_bytes(1, v),
                    None => 0,
                };
                size += tag_size(11) + varint_size(inner as u64) + inner;
            }
            CacheResponseResult::Set => {
                size += tag_size(12) + varint_size(0);
            }
            CacheResponseResult::Delete => {
                size += tag_size(13) + varint_size(0);
            }
        }
        size
    }

    /// Encode with length prefix for protosocket wire format (single allocation).
    pub fn encode_length_delimited(&self) -> Vec<u8> {
        let msg_size = self.encoded_size();
        let mut buf = Vec::with_capacity(varint_size(msg_size as u64) + msg_size);
        encode_varint(msg_size as u64, &mut buf);
        self.encode_into(&mut buf);
        buf
    }

    /// Decode a response from bytes.
    pub fn decode(data: &[u8]) -> Option<Self> {
        let mut buf = data;
        let mut message_id = 0u64;
        let mut control_code = ControlCode::Normal;
        let mut result = None;

        while !buf.is_empty() {
            let (field_number, wire_type) = decode_tag(&mut buf)?;
            match field_number {
                1 if wire_type == WIRE_TYPE_VARINT => {
                    message_id = decode_varint(&mut buf)?;
                }
                2 if wire_type == WIRE_TYPE_VARINT => {
                    control_code = ControlCode::from_u32(decode_varint(&mut buf)? as u32);
                }
                // Response kinds (per official proto field numbers)
                9 if wire_type == WIRE_TYPE_LEN => {
                    let inner = decode_length_delimited(&mut buf)?;
                    let err = CommandError::decode(inner)?;
                    result = Some(CacheResponseResult::Error(err));
                }
                10 if wire_type == WIRE_TYPE_LEN => {
                    let _inner = decode_length_delimited(&mut buf)?;
                    result = Some(CacheResponseResult::Authenticate);
                }
                11 if wire_type == WIRE_TYPE_LEN => {
                    let inner = decode_length_delimited(&mut buf)?;
                    result = Some(Self::decode_get_response(inner));
                }
                12 if wire_type == WIRE_TYPE_LEN => {
                    let _inner = decode_length_delimited(&mut buf)?;
                    result = Some(CacheResponseResult::Set);
                }
                13 if wire_type == WIRE_TYPE_LEN => {
                    let _inner = decode_length_delimited(&mut buf)?;
                    result = Some(CacheResponseResult::Delete);
                }
                _ => skip_field(wire_type, &mut buf)?,
            }
        }

        Some(Self {
            message_id,
            control_code,
            result: result.unwrap_or(CacheResponseResult::Error(CommandError {
                code: StatusCode::Internal,
                message: "missing response".to_string(),
            })),
        })
    }

    fn decode_get_response(data: &[u8]) -> CacheResponseResult {
        let mut buf = data;
        let mut value = None;

        while !buf.is_empty() {
            if let Some((field_number, wire_type)) = decode_tag(&mut buf) {
                match field_number {
                    1 => {
                        if let Some(bytes) = decode_length_delimited(&mut buf) {
                            value = Some(Bytes::copy_from_slice(bytes));
                        }
                    }
                    _ => {
                        if skip_field(wire_type, &mut buf).is_none() {
                            break;
                        }
                    }
                }
            } else {
                break;
            }
        }

        CacheResponseResult::Get { value }
    }
}

/// Decode a length-delimited message from a buffer.
/// Returns (bytes_consumed, message) or None if incomplete.
pub fn decode_length_delimited_message(buf: &[u8]) -> Option<(usize, &[u8])> {
    let mut cursor = buf;
    let original_len = buf.len();

    // Decode varint length
    let len = decode_varint(&mut cursor)? as usize;
    let header_len = original_len - cursor.len();

    // Check if we have enough data
    if cursor.len() < len {
        return None;
    }

    let message = &cursor[..len];
    Some((header_len + len, message))
}

// ============================================================================
// Zero-copy Bytes-aware decoders
// ============================================================================
//
// These decode functions work with `Bytes` handles, returning `Bytes::slice()`
// sub-references instead of copying. Used by the recv path to avoid copies when
// extracting values from the accumulator.

/// Decode a length-delimited field from a `Bytes` handle, returning a zero-copy slice.
///
/// Advances `offset` past the consumed bytes. Returns `None` if incomplete.
fn decode_length_delimited_bytes(data: &Bytes, offset: &mut usize) -> Option<Bytes> {
    let buf = &data[*offset..];
    let mut cursor = buf;
    let len = decode_varint(&mut cursor)? as usize;
    let header_len = buf.len() - cursor.len();
    if cursor.len() < len {
        return None;
    }
    let start = *offset + header_len;
    *offset = start + len;
    Some(data.slice(start..start + len))
}

/// Skip a field in a `Bytes` buffer by advancing `offset`.
fn skip_field_bytes(wire_type: u8, data: &[u8], offset: &mut usize) -> Option<()> {
    let buf = &data[*offset..];
    let mut cursor = buf;
    skip_field(wire_type, &mut cursor)?;
    *offset += buf.len() - cursor.len();
    Some(())
}

/// Decode a varint from a `Bytes` buffer, advancing `offset`.
fn decode_varint_bytes(data: &[u8], offset: &mut usize) -> Option<u64> {
    let buf = &data[*offset..];
    let mut cursor = buf;
    let value = decode_varint(&mut cursor)?;
    *offset += buf.len() - cursor.len();
    Some(value)
}

/// Decode a tag from a `Bytes` buffer, advancing `offset`.
fn decode_tag_bytes(data: &[u8], offset: &mut usize) -> Option<(u32, u8)> {
    let buf = &data[*offset..];
    let mut cursor = buf;
    let tag = decode_tag(&mut cursor)?;
    *offset += buf.len() - cursor.len();
    Some(tag)
}

impl CacheResponse {
    /// Decode a response from a `Bytes` handle using zero-copy slicing.
    ///
    /// Extracted values (e.g., GET response bodies) are `Bytes::slice()` references
    /// into the original buffer — no allocation or memcpy.
    pub fn decode_bytes(data: Bytes) -> Option<Self> {
        let mut offset = 0;
        let mut message_id = 0u64;
        let mut control_code = ControlCode::Normal;
        let mut result = None;

        while offset < data.len() {
            let (field_number, wire_type) = decode_tag_bytes(&data, &mut offset)?;
            match field_number {
                1 if wire_type == WIRE_TYPE_VARINT => {
                    message_id = decode_varint_bytes(&data, &mut offset)?;
                }
                2 if wire_type == WIRE_TYPE_VARINT => {
                    control_code =
                        ControlCode::from_u32(decode_varint_bytes(&data, &mut offset)? as u32);
                }
                9 if wire_type == WIRE_TYPE_LEN => {
                    let inner = decode_length_delimited_bytes(&data, &mut offset)?;
                    let err = CommandError::decode(&inner)?;
                    result = Some(CacheResponseResult::Error(err));
                }
                10 if wire_type == WIRE_TYPE_LEN => {
                    let _inner = decode_length_delimited_bytes(&data, &mut offset)?;
                    result = Some(CacheResponseResult::Authenticate);
                }
                11 if wire_type == WIRE_TYPE_LEN => {
                    // GET response — decode inline with zero-copy value extraction
                    let inner = decode_length_delimited_bytes(&data, &mut offset)?;
                    result = Some(Self::decode_get_response_bytes(inner));
                }
                12 if wire_type == WIRE_TYPE_LEN => {
                    let _inner = decode_length_delimited_bytes(&data, &mut offset)?;
                    result = Some(CacheResponseResult::Set);
                }
                13 if wire_type == WIRE_TYPE_LEN => {
                    let _inner = decode_length_delimited_bytes(&data, &mut offset)?;
                    result = Some(CacheResponseResult::Delete);
                }
                _ => skip_field_bytes(wire_type, &data, &mut offset)?,
            }
        }

        Some(Self {
            message_id,
            control_code,
            result: result.unwrap_or(CacheResponseResult::Error(CommandError {
                code: StatusCode::Internal,
                message: "missing response".to_string(),
            })),
        })
    }

    /// Decode a GET response from a `Bytes` handle — zero-copy value extraction.
    fn decode_get_response_bytes(data: Bytes) -> CacheResponseResult {
        let mut offset = 0;
        let mut value = None;

        while offset < data.len() {
            if let Some((field_number, wire_type)) = decode_tag_bytes(&data, &mut offset) {
                match field_number {
                    1 if wire_type == WIRE_TYPE_LEN => {
                        value = decode_length_delimited_bytes(&data, &mut offset);
                    }
                    _ => {
                        if skip_field_bytes(wire_type, &data, &mut offset).is_none() {
                            break;
                        }
                    }
                }
            } else {
                break;
            }
        }

        CacheResponseResult::Get { value }
    }
}

/// Upper bound on a single length-delimited protobuf message. Without this
/// cap, an adversarial peer (or a buggy server) sending a varint length of
/// e.g. `2^60` would park the recv accumulator indefinitely while it tries
/// to gather that many bytes, growing unboundedly. 16 MiB comfortably fits
/// realistic Momento responses while bounding the worst case.
pub const MAX_MESSAGE_SIZE: u64 = 16 * 1024 * 1024;

/// Result of `decode_length_delimited_message_bytes`.
#[derive(Debug)]
pub enum DecodedMessage {
    /// One full message decoded; `(bytes_consumed, message_bytes)`.
    Message(usize, Bytes),
    /// Buffer doesn't yet contain a full message — wait for more.
    Incomplete,
    /// The declared length is larger than [`MAX_MESSAGE_SIZE`]; the peer
    /// is misbehaving or hostile. Callers should close the connection.
    Oversize,
}

/// Decode a length-delimited message from a `Bytes` handle. Returns
/// `Incomplete` if the buffer isn't full yet, `Oversize` if the declared
/// length exceeds [`MAX_MESSAGE_SIZE`], or `Message(consumed, bytes)`.
pub fn decode_length_delimited_message_bytes(buf: &Bytes) -> DecodedMessage {
    let mut offset = 0;
    let len = match decode_varint_bytes(buf, &mut offset) {
        Some(v) => v,
        None => return DecodedMessage::Incomplete,
    };
    if len > MAX_MESSAGE_SIZE {
        return DecodedMessage::Oversize;
    }
    let len = len as usize;
    if buf.len() - offset < len {
        return DecodedMessage::Incomplete;
    }
    let message = buf.slice(offset..offset + len);
    DecodedMessage::Message(offset + len, message)
}

#[cfg(test)]
mod tests {
    use super::*;

    // Varint tests

    #[test]
    fn test_varint_roundtrip() {
        let values = [0, 1, 127, 128, 300, 16383, 16384, u64::MAX];

        for &value in &values {
            let mut buf = Vec::new();
            encode_varint(value, &mut buf);

            let mut slice = &buf[..];
            let decoded = decode_varint(&mut slice).unwrap();
            assert_eq!(decoded, value);
            assert!(slice.is_empty());
        }
    }

    #[test]
    fn test_decode_varint_empty() {
        let mut buf: &[u8] = &[];
        assert!(decode_varint(&mut buf).is_none());
    }

    #[test]
    fn test_decode_varint_overflow() {
        let mut buf: &[u8] = &[0x80; 11];
        assert!(decode_varint(&mut buf).is_none());
    }

    // Tag tests

    #[test]
    fn test_encode_decode_tag() {
        let test_cases = [(1, 0), (1, 2), (15, 0), (100, 2), (1000, 0)];

        for (field_number, wire_type) in test_cases {
            let mut buf = Vec::new();
            encode_tag(field_number, wire_type, &mut buf);

            let mut slice = &buf[..];
            let (decoded_field, decoded_wire) = decode_tag(&mut slice).unwrap();
            assert_eq!(decoded_field, field_number);
            assert_eq!(decoded_wire, wire_type);
        }
    }

    #[test]
    fn test_decode_tag_empty() {
        let mut buf: &[u8] = &[];
        assert!(decode_tag(&mut buf).is_none());
    }

    // Length-delimited tests

    #[test]
    fn test_decode_length_delimited() {
        let mut buf = Vec::new();
        encode_varint(5, &mut buf); // length = 5
        buf.extend_from_slice(b"hello");

        let mut slice = &buf[..];
        let data = decode_length_delimited(&mut slice).unwrap();
        assert_eq!(data, b"hello");
        assert!(slice.is_empty());
    }

    #[test]
    fn test_decode_length_delimited_empty() {
        let mut buf = Vec::new();
        encode_varint(0, &mut buf); // length = 0

        let mut slice = &buf[..];
        let data = decode_length_delimited(&mut slice).unwrap();
        assert!(data.is_empty());
    }

    #[test]
    fn test_decode_length_delimited_insufficient() {
        let mut buf = Vec::new();
        encode_varint(10, &mut buf); // length = 10
        buf.extend_from_slice(b"short"); // only 5 bytes

        let mut slice = &buf[..];
        assert!(decode_length_delimited(&mut slice).is_none());
    }

    // Skip field tests

    #[test]
    fn test_skip_field_varint() {
        let mut buf = Vec::new();
        encode_varint(12345, &mut buf);

        let mut slice = &buf[..];
        assert!(skip_field(WIRE_TYPE_VARINT, &mut slice).is_some());
        assert!(slice.is_empty());
    }

    #[test]
    fn test_skip_field_length_delimited() {
        let mut buf = Vec::new();
        encode_varint(3, &mut buf);
        buf.extend_from_slice(b"abc");

        let mut slice = &buf[..];
        assert!(skip_field(WIRE_TYPE_LEN, &mut slice).is_some());
        assert!(slice.is_empty());
    }

    #[test]
    fn test_skip_field_64bit() {
        let buf = [0u8; 8];
        let mut slice = &buf[..];
        assert!(skip_field(1, &mut slice).is_some());
        assert!(slice.is_empty());
    }

    #[test]
    fn test_skip_field_64bit_insufficient() {
        let buf = [0u8; 4];
        let mut slice = &buf[..];
        assert!(skip_field(1, &mut slice).is_none());
    }

    #[test]
    fn test_skip_field_32bit() {
        let buf = [0u8; 4];
        let mut slice = &buf[..];
        assert!(skip_field(5, &mut slice).is_some());
        assert!(slice.is_empty());
    }

    #[test]
    fn test_skip_field_32bit_insufficient() {
        let buf = [0u8; 2];
        let mut slice = &buf[..];
        assert!(skip_field(5, &mut slice).is_none());
    }

    #[test]
    fn test_skip_field_unknown_wire_type() {
        let buf = [0u8; 8];
        let mut slice = &buf[..];
        assert!(skip_field(6, &mut slice).is_none());
    }

    // encode_string and encode_message tests

    #[test]
    fn test_encode_string() {
        let mut buf = Vec::new();
        encode_string(1, "hello", &mut buf);

        let mut expected = Vec::new();
        encode_bytes(1, b"hello", &mut expected);

        assert_eq!(buf, expected);
    }

    #[test]
    fn test_encode_message() {
        let inner_msg = b"inner message";
        let mut buf = Vec::new();
        encode_message(1, inner_msg, &mut buf);

        let mut expected = Vec::new();
        encode_bytes(1, inner_msg, &mut expected);

        assert_eq!(buf, expected);
    }

    // ControlCode tests

    #[test]
    fn test_control_code_from_u32() {
        assert_eq!(ControlCode::from_u32(0), ControlCode::Normal);
        assert_eq!(ControlCode::from_u32(1), ControlCode::Cancel);
        assert_eq!(ControlCode::from_u32(2), ControlCode::End);
        assert_eq!(ControlCode::from_u32(99), ControlCode::Normal);
    }

    #[test]
    fn test_control_code_default() {
        assert_eq!(ControlCode::default(), ControlCode::Normal);
    }

    // StatusCode tests

    #[test]
    fn test_status_code_from_u32() {
        assert_eq!(StatusCode::from_u32(0), StatusCode::Ok);
        assert_eq!(StatusCode::from_u32(5), StatusCode::NotFound);
        assert_eq!(StatusCode::from_u32(14), StatusCode::Unavailable);
        assert_eq!(StatusCode::from_u32(16), StatusCode::Unauthenticated);
        assert_eq!(StatusCode::from_u32(99), StatusCode::Unknown);
    }

    #[test]
    fn test_status_code_default() {
        assert_eq!(StatusCode::default(), StatusCode::Ok);
    }

    // CommandError tests

    #[test]
    fn test_command_error_roundtrip() {
        let err = CommandError {
            code: StatusCode::NotFound,
            message: "key not found".to_string(),
        };
        let encoded = err.encode();
        let decoded = CommandError::decode(&encoded).unwrap();
        assert_eq!(decoded.code, StatusCode::NotFound);
        assert_eq!(decoded.message, "key not found");
    }

    #[test]
    fn test_command_error_empty_message() {
        let err = CommandError {
            code: StatusCode::Internal,
            message: String::new(),
        };
        let encoded = err.encode();
        let decoded = CommandError::decode(&encoded).unwrap();
        assert_eq!(decoded.code, StatusCode::Internal);
        assert!(decoded.message.is_empty());
    }

    // UnaryCommand tests

    #[test]
    fn test_unary_command_authenticate_roundtrip() {
        let cmd = UnaryCommand::Authenticate {
            auth_token: "test-token-123".to_string(),
        };
        let encoded = cmd.encode();
        let decoded = UnaryCommand::decode(&encoded).unwrap();
        match decoded {
            UnaryCommand::Authenticate { auth_token } => {
                assert_eq!(auth_token, "test-token-123");
            }
            _ => panic!("expected Authenticate"),
        }
    }

    #[test]
    fn test_unary_command_get_roundtrip() {
        let cmd = UnaryCommand::Get {
            namespace: Bytes::from_static(b"my-cache"),
            key: Bytes::from_static(b"my-key"),
        };
        let encoded = cmd.encode();
        let decoded = UnaryCommand::decode(&encoded).unwrap();
        match decoded {
            UnaryCommand::Get { namespace, key } => {
                assert_eq!(namespace.as_ref(), b"my-cache");
                assert_eq!(key.as_ref(), b"my-key");
            }
            _ => panic!("expected Get"),
        }
    }

    #[test]
    fn test_unary_command_set_roundtrip() {
        let cmd = UnaryCommand::Set {
            namespace: Bytes::from_static(b"cache"),
            key: Bytes::from_static(b"key"),
            value: Bytes::from_static(b"value"),
            ttl_millis: 60000,
        };
        let encoded = cmd.encode();
        let decoded = UnaryCommand::decode(&encoded).unwrap();
        match decoded {
            UnaryCommand::Set {
                namespace,
                key,
                value,
                ttl_millis,
            } => {
                assert_eq!(namespace.as_ref(), b"cache");
                assert_eq!(key.as_ref(), b"key");
                assert_eq!(value.as_ref(), b"value");
                assert_eq!(ttl_millis, 60000);
            }
            _ => panic!("expected Set"),
        }
    }

    #[test]
    fn test_unary_command_delete_roundtrip() {
        let cmd = UnaryCommand::Delete {
            namespace: Bytes::from_static(b"cache"),
            key: Bytes::from_static(b"delete-me"),
        };
        let encoded = cmd.encode();
        let decoded = UnaryCommand::decode(&encoded).unwrap();
        match decoded {
            UnaryCommand::Delete { namespace, key } => {
                assert_eq!(namespace.as_ref(), b"cache");
                assert_eq!(key.as_ref(), b"delete-me");
            }
            _ => panic!("expected Delete"),
        }
    }

    // CacheCommand tests

    #[test]
    fn test_cache_command_roundtrip() {
        let cmd = CacheCommand::new(
            42,
            UnaryCommand::Get {
                namespace: Bytes::from_static(b"test"),
                key: Bytes::from_static(b"key"),
            },
        );
        let encoded = cmd.encode();
        let decoded = CacheCommand::decode(&encoded).unwrap();
        assert_eq!(decoded.message_id, 42);
        assert_eq!(decoded.control_code, ControlCode::Normal);
        assert!(decoded.command.is_some());
    }

    #[test]
    fn test_cache_command_cancel() {
        let cmd = CacheCommand::cancel(99);
        let encoded = cmd.encode();
        let decoded = CacheCommand::decode(&encoded).unwrap();
        assert_eq!(decoded.message_id, 99);
        assert_eq!(decoded.control_code, ControlCode::Cancel);
        assert!(decoded.command.is_none());
    }

    #[test]
    fn test_cache_command_length_delimited() {
        let cmd = CacheCommand::new(
            1,
            UnaryCommand::Authenticate {
                auth_token: "token".to_string(),
            },
        );
        let encoded = cmd.encode_length_delimited();
        let (consumed, message) = decode_length_delimited_message(&encoded).unwrap();
        assert_eq!(consumed, encoded.len());
        let decoded = CacheCommand::decode(message).unwrap();
        assert_eq!(decoded.message_id, 1);
    }

    // CacheResponse tests

    #[test]
    fn test_cache_response_authenticate() {
        let resp = CacheResponse::authenticate(1);
        let encoded = resp.encode();
        let decoded = CacheResponse::decode(&encoded).unwrap();
        assert_eq!(decoded.message_id, 1);
        assert!(matches!(decoded.result, CacheResponseResult::Authenticate));
    }

    #[test]
    fn test_cache_response_get_hit() {
        let resp = CacheResponse::get_hit(2, Bytes::from_static(b"value"));
        let encoded = resp.encode();
        let decoded = CacheResponse::decode(&encoded).unwrap();
        assert_eq!(decoded.message_id, 2);
        match decoded.result {
            CacheResponseResult::Get { value } => {
                assert_eq!(value, Some(Bytes::from_static(b"value")));
            }
            _ => panic!("expected Get"),
        }
    }

    #[test]
    fn test_cache_response_get_miss() {
        let resp = CacheResponse::get_miss(3);
        let encoded = resp.encode();
        let decoded = CacheResponse::decode(&encoded).unwrap();
        assert_eq!(decoded.message_id, 3);
        match decoded.result {
            CacheResponseResult::Get { value } => {
                assert!(value.is_none());
            }
            _ => panic!("expected Get"),
        }
    }

    #[test]
    fn test_cache_response_set_ok() {
        let resp = CacheResponse::set_ok(4);
        let encoded = resp.encode();
        let decoded = CacheResponse::decode(&encoded).unwrap();
        assert_eq!(decoded.message_id, 4);
        assert!(matches!(decoded.result, CacheResponseResult::Set));
    }

    #[test]
    fn test_cache_response_delete_ok() {
        let resp = CacheResponse::delete_ok(5);
        let encoded = resp.encode();
        let decoded = CacheResponse::decode(&encoded).unwrap();
        assert_eq!(decoded.message_id, 5);
        assert!(matches!(decoded.result, CacheResponseResult::Delete));
    }

    #[test]
    fn test_cache_response_error() {
        let resp = CacheResponse::error(6, StatusCode::NotFound, "not found");
        let encoded = resp.encode();
        let decoded = CacheResponse::decode(&encoded).unwrap();
        assert_eq!(decoded.message_id, 6);
        match decoded.result {
            CacheResponseResult::Error(err) => {
                assert_eq!(err.code, StatusCode::NotFound);
                assert_eq!(err.message, "not found");
            }
            _ => panic!("expected Error"),
        }
    }

    #[test]
    fn test_cache_response_length_delimited() {
        let resp = CacheResponse::set_ok(7);
        let encoded = resp.encode_length_delimited();
        let (consumed, message) = decode_length_delimited_message(&encoded).unwrap();
        assert_eq!(consumed, encoded.len());
        let decoded = CacheResponse::decode(message).unwrap();
        assert_eq!(decoded.message_id, 7);
    }

    // decode_length_delimited_message tests

    #[test]
    fn test_decode_length_delimited_message_complete() {
        let data = b"hello";
        let mut buf = Vec::new();
        encode_varint(data.len() as u64, &mut buf);
        buf.extend_from_slice(data);

        let (consumed, message) = decode_length_delimited_message(&buf).unwrap();
        assert_eq!(consumed, buf.len());
        assert_eq!(message, data);
    }

    #[test]
    fn test_decode_length_delimited_message_incomplete() {
        let mut buf = Vec::new();
        encode_varint(100, &mut buf); // Says 100 bytes
        buf.extend_from_slice(b"short"); // Only 5 bytes

        let result = decode_length_delimited_message(&buf);
        assert!(result.is_none());
    }

    #[test]
    fn test_decode_length_delimited_message_empty() {
        let result = decode_length_delimited_message(&[]);
        assert!(result.is_none());
    }

    // Bytes-handle (zero-copy) variant + size cap

    #[test]
    fn decode_length_delimited_bytes_message() {
        let payload = b"hello world";
        let mut buf = Vec::new();
        encode_varint(payload.len() as u64, &mut buf);
        buf.extend_from_slice(payload);
        let bytes = Bytes::from(buf.clone());

        match decode_length_delimited_message_bytes(&bytes) {
            DecodedMessage::Message(consumed, msg) => {
                assert_eq!(consumed, buf.len());
                assert_eq!(&msg[..], payload);
            }
            other => panic!("expected Message, got {other:?}"),
        }
    }

    #[test]
    fn decode_length_delimited_bytes_incomplete() {
        let mut buf = Vec::new();
        encode_varint(100, &mut buf);
        buf.extend_from_slice(b"only-5");
        let bytes = Bytes::from(buf);
        assert!(matches!(
            decode_length_delimited_message_bytes(&bytes),
            DecodedMessage::Incomplete
        ));
    }

    #[test]
    fn decode_length_delimited_bytes_oversize() {
        // Declared length above MAX_MESSAGE_SIZE — must report Oversize so
        // the caller can poison the connection instead of waiting forever.
        let mut buf = Vec::new();
        encode_varint(MAX_MESSAGE_SIZE + 1, &mut buf);
        let bytes = Bytes::from(buf);
        assert!(matches!(
            decode_length_delimited_message_bytes(&bytes),
            DecodedMessage::Oversize
        ));

        // A pathological 2^62 varint must also report Oversize, not panic.
        let mut buf = Vec::new();
        encode_varint(1u64 << 62, &mut buf);
        let bytes = Bytes::from(buf);
        assert!(matches!(
            decode_length_delimited_message_bytes(&bytes),
            DecodedMessage::Oversize
        ));
    }
}