s3m-core 0.21.0

Reusable S3 client and streaming engine behind the s3m CLI
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
pub mod db;
pub mod iterator;
pub mod part;
pub mod state;
pub mod upload_compressed;
pub mod upload_compressed_encrypted;
pub mod upload_default;
pub mod upload_encrypted;
pub mod upload_multipart;
pub mod upload_stdin;
pub mod upload_stdin_compressed;
pub mod upload_stdin_compressed_encrypted;
pub mod upload_stdin_encrypted;

use crate::{
    progressbar::Bar,
    s3::RequestOptions,
    s3::{S3, actions, object_lock::ObjectLock, request::ProgressCallback},
};
use aead_stream::{DecryptorBE32, EncryptorBE32};
use anyhow::{Context as _, Result, anyhow};
use bytes::BytesMut;
use bytesize::ByteSize;
use chacha20poly1305::{ChaCha20Poly1305, Key, aead::KeyInit};
use indicatif::ProgressBar;
use rand::{RngExt, rng};
use ring::digest::{Context, SHA256};
use secrecy::ExposeSecret;
use std::{
    cmp::min,
    collections::BTreeMap,
    io::{Cursor, Read, Write},
    path::{Path, PathBuf},
    sync::Arc,
};
use tempfile::{Builder, NamedTempFile};
use tokio::time::{Duration, sleep};
use tokio::{
    io::Error,
    sync::mpsc::{UnboundedSender, unbounded_channel},
    task,
};
use zstd::stream::encode_all;

// 512MB  to upload 5TB (the current max object size)
const STDIN_BUFFER_SIZE: usize = 1_024 * 1_024 * 512;

pub struct Stream<'a> {
    tmp_file: NamedTempFile,
    count: usize,
    etags: Vec<String>,
    key: &'a str,
    part_number: u16,
    s3: &'a S3,
    upload_id: &'a str,
    sha: ring::digest::Context,
    md5: md5::Context,
    channel: Option<UnboundedSender<StreamProgressEvent>>,
    tmp_dir: PathBuf,
    throttle: Option<usize>,
    retries: u32,
}

struct InitialStreamParams<'a> {
    upload_id: &'a str,
    tmp_dir: &'a Path,
    key: &'a str,
    s3: &'a S3,
    progress_sender: Option<UnboundedSender<StreamProgressEvent>>,
    globals: &'a RequestOptions,
    header_data: Option<&'a [u8]>,
}

pub struct FileStreamUpload<'a> {
    pub s3: &'a S3,
    pub object_key: &'a str,
    pub acl: Option<String>,
    pub meta: Option<BTreeMap<String, String>>,
    pub quiet: bool,
    pub tmp_dir: PathBuf,
    pub globals: RequestOptions,
    pub file_path: &'a Path,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum StreamProgressEvent {
    Staged(u64),
    SendingStarted { part_number: u16, total_bytes: u64 },
    SendingProgress(u64),
    SendingStopped,
    Confirmed(u64),
    ResetStaging,
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
struct StreamProgressState {
    staged_current: u64,
    staged_total: u64,
    confirmed_total: u64,
    sending_current: u64,
    sending_total: u64,
    sending_part: Option<u16>,
}

// return the key with the .zst, .enc, or .zst.enc extension based on the flags
fn get_key(key: &str, compress: bool, encrypt: bool) -> String {
    let path = Path::new(key);
    let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");

    match (compress, encrypt) {
        (true, true) => {
            // If extension is already "zst.enc" add ".zst.enc" only if not present
            if ext == "zst.enc" {
                key.to_string()
            } else {
                format!("{key}.zst.enc")
            }
        }

        (true, false) => {
            // Only compress: add ".zst" if not already "zst"
            if ext == "zst" {
                key.to_string()
            } else {
                format!("{key}.zst")
            }
        }

        (false, true) => {
            // Only encrypt: add ".enc" if not already "enc"
            if ext == "enc" {
                key.to_string()
            } else {
                format!("{key}.enc")
            }
        }

        (false, false) => key.to_string(),
    }
}

async fn try_stream_part(part: &Stream<'_>) -> Result<String> {
    let mut etag = String::new();

    let digest_sha = part.sha.clone().finish();
    let digest_md5 = part.md5.clone().finalize();

    // Create globals only to pass the throttle
    let globals = RequestOptions {
        throttle: part.throttle,
        retries: part.retries,
        compress: false,
        encrypt: false,
        enc_key: None,
        object_lock: None,
    };

    for attempt in 1..=part.retries {
        // Exponential backoff: 1s, 2s, 4s, 8s, 16s, capped at 30s
        const MAX_BACKOFF_SECS: u64 = 30;
        let backoff_time = std::cmp::min(2u64.pow(attempt - 1), MAX_BACKOFF_SECS);

        if attempt > 1 {
            // Add jitter (0-1000ms) to prevent thundering herd
            let jitter_ms = rng().random_range(0_u64..1000);
            let total_backoff =
                Duration::from_secs(backoff_time) + Duration::from_millis(jitter_ms);

            log::warn!(
                "Error streaming part number {}, retrying in {:.1} seconds",
                part.part_number,
                total_backoff.as_secs_f64()
            );

            sleep(total_backoff).await;
        }

        emit_stream_progress(
            part.channel.as_ref(),
            StreamProgressEvent::SendingStarted {
                part_number: part.part_number,
                total_bytes: part.count as u64,
            },
        );

        let action = actions::StreamPart::new(
            part.key,
            part.tmp_file.path(),
            part.part_number,
            part.upload_id,
            part.count,
            (digest_sha.as_ref(), digest_md5.as_ref()),
            stream_sending_progress_callback(part.channel.as_ref()),
        );

        match action.request(part.s3, &globals).await {
            Ok(e) => {
                etag = e;
                emit_stream_progress(
                    part.channel.as_ref(),
                    StreamProgressEvent::Confirmed(part.count as u64),
                );

                log::info!("Uploaded part: {}, etag: {}", part.part_number, etag);

                break;
            }

            Err(e) => {
                emit_stream_progress(part.channel.as_ref(), StreamProgressEvent::SendingStopped);

                log::error!(
                    "Error uploading part number {}, attempt {}/{} failed: {}",
                    part.part_number,
                    attempt,
                    part.retries,
                    e
                );

                if attempt == part.retries {
                    return Err(anyhow::anyhow!(
                        "Error uploading part number {}, {}",
                        part.part_number,
                        e
                    ));
                }
            }
        }
    }

    Ok(etag)
}

/// Compresses a chunk of bytes using Zstandard (zstd), offloading the work to a blocking thread.
///
/// # Errors
/// Returns an error if compression fails or the thread panics.
pub async fn compress_chunk(bytes: BytesMut) -> Result<Vec<u8>> {
    let input = bytes.freeze(); // Safe to send across threads

    task::spawn_blocking(move || {
        encode_all(Cursor::new(input), 0).context("failed to compress with zstd")
    })
    .await
    .context("compression task panicked or was cancelled")?
}

/// Decompresses Zstandard (zstd) data, offloading the work to a blocking thread.
///
/// Counterpart of [`compress_chunk`]. Decodes the entire input, including
/// multiple concatenated zstd frames (as produced by compressing a stream chunk
/// by chunk), so it can restore a whole compressed object in one call.
///
/// The output is **bounded to `max_output` bytes**: decompression stops and
/// returns an error if the decompressed size would exceed it. This caps memory
/// use and guards against decompression bombs when restoring untrusted data.
///
/// # Errors
/// Returns an error if decompression fails, the output would exceed
/// `max_output`, or the thread panics.
pub async fn decompress_chunk(data: Vec<u8>, max_output: usize) -> Result<Vec<u8>> {
    task::spawn_blocking(move || {
        let decoder = zstd::stream::read::Decoder::new(Cursor::new(data))
            .context("failed to initialize zstd decoder")?;

        // Read at most max_output + 1 bytes; getting the extra byte means the
        // decompressed stream is larger than allowed.
        let limit = u64::try_from(max_output)
            .unwrap_or(u64::MAX)
            .saturating_add(1);
        let mut out = Vec::new();
        decoder
            .take(limit)
            .read_to_end(&mut out)
            .context("failed to decompress with zstd")?;

        if out.len() > max_output {
            return Err(anyhow!(
                "decompressed output exceeds the {max_output}-byte limit"
            ));
        }
        Ok(out)
    })
    .await
    .context("decompression task panicked or was cancelled")?
}

/// Build a `ChaCha20Poly1305` cipher from a 32-byte encryption key.
///
/// # Errors
/// Returns an error if the key is not exactly 32 bytes.
pub fn cipher_from_key(encryption_key: &secrecy::SecretString) -> Result<ChaCha20Poly1305> {
    let key = Key::try_from(encryption_key.expose_secret().as_bytes())
        .map_err(|_| anyhow!("encryption key must be exactly 32 bytes"))?;
    Ok(ChaCha20Poly1305::new(&key))
}

/// Initialize a streaming encryptor cipher and generate a fresh random 7-byte nonce.
///
/// # Errors
/// Returns an error if the key is not exactly 32 bytes.
pub fn init_encryption(
    encryption_key: &secrecy::SecretString,
) -> Result<(ChaCha20Poly1305, [u8; 7])> {
    let cipher = cipher_from_key(encryption_key)?;

    // Generate a random 7-byte nonce
    let nonce_bytes: [u8; 7] = rng().random();

    Ok((cipher, nonce_bytes))
}

/// Build a streaming decryptor from the encryption key and the 7-byte nonce.
///
/// Counterpart of [`init_encryption`]: the nonce is the one carried by
/// [`create_nonce_header`] and recovered with [`parse_nonce_header`].
///
/// # Errors
/// Returns an error if the key is not exactly 32 bytes.
pub fn init_decryption(
    encryption_key: &secrecy::SecretString,
    nonce: &[u8; 7],
) -> Result<DecryptorBE32<ChaCha20Poly1305>> {
    let cipher = cipher_from_key(encryption_key)?;
    Ok(DecryptorBE32::from_aead(cipher, nonce.into()))
}

/// Initialize multipart upload
async fn initiate_multipart_upload(
    s3: &S3,
    key: &str,
    acl: Option<String>,
    meta: BTreeMap<String, String>,
    object_lock: Option<ObjectLock>,
) -> Result<String> {
    let action = actions::CreateMultipartUpload::new(key, acl, Some(meta), None, object_lock);
    let response = action.request(s3).await?;
    Ok(response.upload_id)
}

async fn setup_progress(quiet: bool, size: u64) -> Option<UnboundedSender<usize>> {
    if quiet {
        return None;
    }

    let (sender, receiver) = unbounded_channel::<usize>();

    if let Some(pb) = Bar::new(size).progress {
        spawn_progress_task(receiver, pb, size);
        return Some(sender);
    }

    None
}

async fn setup_stream_progress(quiet: bool) -> Option<UnboundedSender<StreamProgressEvent>> {
    if quiet {
        return None;
    }

    let (sender, receiver) = unbounded_channel::<StreamProgressEvent>();

    if let Some(bars) = Bar::new_stream(STDIN_BUFFER_SIZE as u64) {
        spawn_stream_progress_task(receiver, bars.staging, bars.status);
        return Some(sender);
    }

    None
}

fn spawn_progress_task(
    mut receiver: tokio::sync::mpsc::UnboundedReceiver<usize>,
    pb: ProgressBar,
    size: u64,
) -> task::JoinHandle<u64> {
    tokio::spawn(async move {
        let mut bytes_tracked = 0;

        while let Some(bytes_count) = receiver.recv().await {
            let new = min(bytes_tracked + bytes_count as u64, size);
            bytes_tracked = new;
            pb.set_position(new);
        }

        log::debug!("Progress channel closed — total bytes tracked: {bytes_tracked}");

        pb.finish();
        bytes_tracked
    })
}

fn spawn_stream_progress_task(
    mut receiver: tokio::sync::mpsc::UnboundedReceiver<StreamProgressEvent>,
    staging_pb: ProgressBar,
    status_pb: ProgressBar,
) -> task::JoinHandle<StreamProgressState> {
    tokio::spawn(async move {
        let mut state = StreamProgressState::default();
        update_stream_status_message(&status_pb, &state);

        while let Some(event) = receiver.recv().await {
            match event {
                StreamProgressEvent::Staged(bytes_count) => {
                    state.staged_current += bytes_count;
                    state.staged_total += bytes_count;
                    staging_pb.set_position(min(state.staged_current, STDIN_BUFFER_SIZE as u64));
                }
                StreamProgressEvent::SendingStarted {
                    part_number,
                    total_bytes,
                } => {
                    state.sending_part = Some(part_number);
                    state.sending_current = 0;
                    state.sending_total = total_bytes;
                }
                StreamProgressEvent::SendingProgress(bytes_count) => {
                    state.sending_current =
                        min(state.sending_current + bytes_count, state.sending_total);
                }
                StreamProgressEvent::SendingStopped => {
                    state.sending_part = None;
                    state.sending_current = 0;
                    state.sending_total = 0;
                }
                StreamProgressEvent::Confirmed(bytes_count) => {
                    state.confirmed_total += bytes_count;
                    state.sending_part = None;
                    state.sending_current = 0;
                    state.sending_total = 0;
                }
                StreamProgressEvent::ResetStaging => {
                    state.staged_current = 0;
                    staging_pb.set_position(0);
                }
            }

            update_stream_status_message(&status_pb, &state);
        }

        log::debug!(
            "Stream progress channel closed - staged_current: {}, staged_total: {}, confirmed_total: {}, sending_current: {}, sending_total: {}, sending_part: {:?}",
            state.staged_current,
            state.staged_total,
            state.confirmed_total,
            state.sending_current,
            state.sending_total,
            state.sending_part
        );

        staging_pb.finish();
        status_pb.finish();
        state
    })
}

fn update_stream_status_message(pb: &ProgressBar, state: &StreamProgressState) {
    let message = if let Some(part_number) = state.sending_part {
        format!(
            "sending part {part_number} {}/{} | confirmed {}",
            ByteSize(state.sending_current),
            ByteSize(state.sending_total),
            ByteSize(state.confirmed_total)
        )
    } else {
        format!("confirmed {}", ByteSize(state.confirmed_total))
    };

    pb.set_message(message);
}

fn emit_stream_progress(
    channel: Option<&UnboundedSender<StreamProgressEvent>>,
    event: StreamProgressEvent,
) {
    if let Some(tx) = channel
        && tx.send(event).is_err()
    {
        log::trace!("Progress receiver dropped");
    }
}

fn stream_sending_progress_callback(
    channel: Option<&UnboundedSender<StreamProgressEvent>>,
) -> Option<ProgressCallback> {
    let sender = channel.cloned()?;
    let callback: ProgressCallback = Arc::new(move |bytes_count| {
        if sender
            .send(StreamProgressEvent::SendingProgress(bytes_count as u64))
            .is_err()
        {
            log::trace!("Progress receiver dropped");
        }
    });
    Some(callback)
}

/// Create the initial stream with nonce header
fn create_initial_stream(params: InitialStreamParams<'_>) -> Result<Stream<'_>> {
    let tmp_file = Builder::new()
        .prefix(params.upload_id)
        .suffix(".s3m")
        .tempfile_in(params.tmp_dir)?;

    let mut stream = Stream {
        tmp_file,
        count: 0,
        etags: Vec::new(),
        key: params.key,
        part_number: 1,
        s3: params.s3,
        upload_id: params.upload_id,
        sha: Context::new(&SHA256),
        md5: md5::Context::new(),
        channel: params.progress_sender,
        tmp_dir: params.tmp_dir.to_path_buf(),
        throttle: params.globals.throttle,
        retries: params.globals.retries,
    };

    if let Some(header) = params.header_data {
        write_to_stream(&mut stream, header)?;
    }

    Ok(stream)
}

/// Write data to stream and update counters/hashes
///
/// # Errors
/// Returns `Err` if writing to the temporary file fails
pub fn write_to_stream(stream: &mut Stream<'_>, data: &[u8]) -> Result<()> {
    stream.tmp_file.write_all(data)?;
    stream.sha.update(data);
    stream.md5.consume(data);
    stream.count += data.len();

    emit_stream_progress(
        stream.channel.as_ref(),
        StreamProgressEvent::Staged(data.len() as u64),
    );

    Ok(())
}

/// Create encryption nonce header
#[must_use]
pub fn create_nonce_header(nonce_bytes: &[u8; 7]) -> Vec<u8> {
    [&[7_u8], nonce_bytes.as_slice()].concat()
}

/// Parse the encryption nonce header written by [`create_nonce_header`].
///
/// Expects `[0x07, nonce0..=nonce6]` (8 bytes) at the start of an encrypted
/// stream and returns the 7-byte nonce.
///
/// # Errors
/// Returns an error if the header is too short or the length byte is not 7.
pub fn parse_nonce_header(header: &[u8]) -> Result<[u8; 7]> {
    let len = *header.first().context("missing nonce length byte")? as usize;
    if len != 7 {
        return Err(anyhow!("expected nonce length 7, got {len}"));
    }
    let nonce: [u8; 7] = header
        .get(1..8)
        .context("missing nonce bytes")?
        .try_into()
        .map_err(|_| anyhow!("invalid nonce length"))?;
    Ok(nonce)
}

/// Encrypt a chunk of data
///
/// # Errors
/// Returns `Err` if encryption fails or if the encrypted chunk exceeds 4GB
pub fn encrypt_chunk(
    encryptor: &mut EncryptorBE32<ChaCha20Poly1305>,
    chunk: &[u8],
) -> Result<Vec<u8>> {
    let mut encrypted_chunk = chunk.to_vec();
    encryptor
        .encrypt_next_in_place(&[], &mut encrypted_chunk)
        .map_err(|e| anyhow!("Encryption error: {e}"))?;

    // Format: [encrypted_data_length(4 bytes)][encrypted_data]
    let encrypted_len = u32::try_from(encrypted_chunk.len())
        .map_err(|_| anyhow!("Encrypted chunk size exceeds 4GB"))?;
    let mut result = Vec::with_capacity(4 + encrypted_chunk.len());
    result.extend_from_slice(&encrypted_len.to_be_bytes());
    result.extend_from_slice(&encrypted_chunk);
    Ok(result)
}

/// Decrypt one chunk produced by [`encrypt_chunk`].
///
/// `encrypt_chunk` frames its output as `[len(4 bytes)][ciphertext]`; the caller
/// reads the length, then passes exactly `len` ciphertext bytes here. Chunks
/// must be fed in the same order they were encrypted (the stream cipher is
/// stateful).
///
/// # Errors
/// Returns `Err` if decryption/authentication fails.
pub fn decrypt_chunk(
    decryptor: &mut DecryptorBE32<ChaCha20Poly1305>,
    data: &[u8],
) -> Result<Vec<u8>> {
    let mut chunk = data.to_vec();
    decryptor
        .decrypt_next_in_place(&[], &mut chunk)
        .map_err(|e| anyhow!("Decryption error: {e}"))?;
    Ok(chunk)
}

/// Check if we need to upload current part and start a new one
///
/// # Errors
/// Returns `Err` if streaming the part fails or if creating a new temporary file fails
pub async fn maybe_upload_part(stream: &mut Stream<'_>, buffer_size: usize) -> Result<(), Error> {
    if stream.count >= buffer_size {
        let etag = try_stream_part(stream)
            .await
            .map_err(|e| Error::other(format!("Error streaming part: {e}")))?;

        emit_stream_progress(stream.channel.as_ref(), StreamProgressEvent::ResetStaging);

        stream.etags.push(etag);

        log::debug!(
            "Part {} uploaded, total bytes: {}, etag: {}",
            stream.part_number,
            stream.count,
            stream.etags.last().unwrap_or(&String::new())
        );

        // Reset for next part
        stream.tmp_file = Builder::new()
            .prefix(stream.upload_id)
            .suffix(".s3m")
            .tempfile_in(&stream.tmp_dir)?;

        stream.count = 0;
        stream.part_number += 1;
        stream.sha = Context::new(&SHA256);
        stream.md5 = md5::Context::new();
    }
    Ok(())
}

/// Complete multipart upload
async fn complete_multipart_upload(
    s3: &S3,
    key: &str,
    upload_id: &str,
    etags: Vec<String>,
) -> Result<String> {
    // Validate part count before processing
    let part_count = etags.len();
    if part_count > 10_000 {
        log::error!(
            "Part count ({part_count}) exceeds S3's maximum of 10,000 parts. \
            This indicates a bug in part size calculation. \
            Upload ID: {upload_id}, Key: {key}"
        );

        return Err(anyhow!(
            "Upload failed: {part_count} parts created, but S3 allows maximum 10,000 parts. \
            This is a bug in s3m - please report at https://github.com/s3m/s3m/issues with file size and buffer settings. \
            The incomplete multipart upload (ID: {upload_id}) may need manual cleanup using: \
            s3m rm {key} --abort {upload_id}"
        ));
    }

    let parts: BTreeMap<u16, actions::Part> = etags
        .into_iter()
        .enumerate()
        .map(|(index, etag)| {
            // S3 supports max 10,000 parts. We validated above, so this should never fail.
            // Convert to Result to satisfy clippy's no-expect rule
            let part_number = u16::try_from(index + 1).map_err(|_| {
                anyhow!(
                    "BUG: Part number overflow after validation (index={index}). This should be impossible."
                )
            })?;
            Ok((
                part_number,
                actions::Part {
                    etag,
                    number: part_number,
                    checksum: None,
                },
            ))
        })
        .collect::<Result<BTreeMap<_, _>>>()?;

    let action = actions::CompleteMultipartUpload::new(key, upload_id, parts, None);

    let response = action.request(s3).await?;

    Ok(response.e_tag)
}

/// Upload the final part
async fn upload_final_part(
    stream: &mut Stream<'_>,
    key: &str,
    upload_id: &str,
    s3: &S3,
    globals: &RequestOptions,
) -> Result<String> {
    let digest_sha = stream.sha.clone().finish();
    let digest_md5 = stream.md5.clone().finalize();

    emit_stream_progress(
        stream.channel.as_ref(),
        StreamProgressEvent::SendingStarted {
            part_number: stream.part_number,
            total_bytes: stream.count as u64,
        },
    );

    let action = actions::StreamPart::new(
        key,
        stream.tmp_file.path(),
        stream.part_number,
        upload_id,
        stream.count,
        (digest_sha.as_ref(), digest_md5.as_ref()),
        stream_sending_progress_callback(stream.channel.as_ref()),
    );

    let etag = action.request(s3, globals).await.map_err(|e| {
        emit_stream_progress(stream.channel.as_ref(), StreamProgressEvent::SendingStopped);
        anyhow!("Failed to upload final part: {e}")
    })?;

    emit_stream_progress(
        stream.channel.as_ref(),
        StreamProgressEvent::Confirmed(stream.count as u64),
    );

    Ok(etag)
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::indexing_slicing,
    clippy::unnecessary_wraps
)]
mod tests {
    use super::*;
    use crate::s3::{Credentials, Region, S3};
    use aead_stream::EncryptorBE32;
    use indicatif::ProgressBar;
    use mockito::{Matcher, Server};
    use secrecy::SecretString;
    use tokio::{sync::mpsc::unbounded_channel, time::timeout};

    #[tokio::test]
    async fn test_compress_decompress_roundtrip() {
        let original = b"the quick brown fox ".repeat(64);
        let second = b"second chunk payload".to_vec();

        let c1 = compress_chunk(BytesMut::from(&original[..])).await.unwrap();
        let c2 = compress_chunk(BytesMut::from(&second[..])).await.unwrap();

        // Two independent zstd frames concatenated, as chunked compression produces.
        let mut blob = c1;
        blob.extend_from_slice(&c2);

        let out = decompress_chunk(blob, 1 << 20).await.unwrap();

        let mut expected = original.clone();
        expected.extend_from_slice(&second);
        assert_eq!(out, expected);
    }

    #[tokio::test]
    async fn test_decompress_chunk_respects_limit() {
        // ~100 KB of highly compressible data: small compressed, big decompressed.
        let original = b"A".repeat(100_000);
        let compressed = compress_chunk(BytesMut::from(&original[..])).await.unwrap();

        // A tight limit must reject the bomb...
        assert!(decompress_chunk(compressed.clone(), 1_000).await.is_err());

        // ...while a generous limit round-trips exactly.
        let ok = decompress_chunk(compressed, 1_000_000).await.unwrap();
        assert_eq!(ok, original);
    }

    #[test]
    fn test_encrypt_decrypt_roundtrip() {
        let key = SecretString::new("0123456789abcdef0123456789abcdef".into());

        // Encode: header + framed chunks, exactly as the upload paths produce.
        let (cipher, nonce) = init_encryption(&key).unwrap();
        let mut enc = EncryptorBE32::from_aead(cipher, (&nonce).into());
        let header = create_nonce_header(&nonce);
        let p1 = b"first plaintext chunk".to_vec();
        let p2 = b"second plaintext chunk".to_vec();
        let f1 = encrypt_chunk(&mut enc, &p1).unwrap(); // [len(4)][ciphertext]
        let f2 = encrypt_chunk(&mut enc, &p2).unwrap();

        let mut stream = header;
        stream.extend_from_slice(&f1);
        stream.extend_from_slice(&f2);

        // Decode: recover nonce, then unframe + decrypt each chunk in order.
        let nonce2 = parse_nonce_header(&stream[..8]).unwrap();
        assert_eq!(nonce2, nonce);
        let mut dec = init_decryption(&key, &nonce2).unwrap();

        let mut pos = 8;
        let len1 = u32::from_be_bytes(stream[pos..pos + 4].try_into().unwrap()) as usize;
        pos += 4;
        let d1 = decrypt_chunk(&mut dec, &stream[pos..pos + len1]).unwrap();
        pos += len1;
        let len2 = u32::from_be_bytes(stream[pos..pos + 4].try_into().unwrap()) as usize;
        pos += 4;
        let d2 = decrypt_chunk(&mut dec, &stream[pos..pos + len2]).unwrap();

        assert_eq!(d1, p1);
        assert_eq!(d2, p2);
    }

    #[test]
    fn test_parse_nonce_header() {
        let nonce = [1u8, 2, 3, 4, 5, 6, 7];
        assert_eq!(
            parse_nonce_header(&create_nonce_header(&nonce)).unwrap(),
            nonce
        );
        assert!(parse_nonce_header(&[6, 0, 0, 0, 0, 0, 0, 0]).is_err()); // wrong len byte
        assert!(parse_nonce_header(&[7, 1, 2, 3]).is_err()); // too short
    }

    #[test]
    fn test_get_key() {
        let test_cases = vec![
            ("test", false, false, "test"),
            ("test", true, false, "test.zst"),
            ("test", false, true, "test.enc"),
            ("test", true, true, "test.zst.enc"),
            ("test.txt", false, false, "test.txt"),
            ("test.txt", true, false, "test.txt.zst"),
            ("test.txt", false, true, "test.txt.enc"),
            ("test.txt", true, true, "test.txt.zst.enc"),
            ("test.ZST", false, false, "test.ZST"),
            ("test.ZST", true, false, "test.ZST.zst"),
            ("test.ZST", false, true, "test.ZST.enc"),
            ("test.ZST", true, true, "test.ZST.zst.enc"),
        ];
        for (key, compress, encrypt, expected) in test_cases {
            assert_eq!(get_key(key, compress, encrypt), expected);
        }
    }

    #[test]
    fn test_try_stream_part() {
        let s3 = S3::new(
            &Credentials::new(
                "AKIAIOSFODNN7EXAMPLE",
                &SecretString::new("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY".into()),
            ),
            &"us-west-1".parse::<Region>().unwrap(),
            Some("awsexamplebucket1".to_string()),
            false,
        );

        let part = Stream {
            tmp_file: NamedTempFile::new().unwrap(),
            count: 0,
            etags: Vec::new(),
            key: "test",
            part_number: 1,
            s3: &s3,
            upload_id: "test",
            sha: ring::digest::Context::new(&ring::digest::SHA256),
            md5: md5::Context::new(),
            channel: None,
            tmp_dir: PathBuf::new(),
            throttle: None,
            retries: 1,
        };

        let rt = tokio::runtime::Runtime::new().unwrap();
        let result = rt.block_on(try_stream_part(&part));
        assert!(result.is_err());
    }

    #[test]
    fn test_create_nonce_header() {
        let nonce = [1, 2, 3, 4, 5, 6, 7];
        let header = create_nonce_header(&nonce);
        assert_eq!(header.len(), 8);
        assert_eq!(header[0], 7);
        assert_eq!(&header[1..], &nonce);
    }

    #[test]
    fn test_encrypt_chunk() {
        let key = secrecy::SecretString::new("0123456789abcdef0123456789abcdef".into());
        let (cipher, nonce) = init_encryption(&key).unwrap();
        let mut encryptor: EncryptorBE32<ChaCha20Poly1305> =
            EncryptorBE32::from_aead(cipher, (&nonce).into());

        let data = b"Hello, world!";
        let encrypted = encrypt_chunk(&mut encryptor, data).unwrap();

        assert!(
            encrypted.len() > 4,
            "Encrypted output should include ciphertext after 4-byte prefix"
        );

        let prefix = u32::from_be_bytes(encrypted[0..4].try_into().unwrap()) as usize;
        let encrypted_payload = &encrypted[4..];

        // The prefix should match the actual length of the encrypted payload
        assert_eq!(
            prefix,
            encrypted_payload.len(),
            "Length prefix should match encrypted chunk length"
        );

        // The encrypted payload should differ from plaintext
        assert_ne!(
            encrypted_payload, data,
            "Encrypted payload should differ from original plaintext"
        );
    }

    #[test]
    fn test_write_to_stream() {
        let s3 = S3::new(
            &Credentials::new(
                "AKIAIOSFODNN7EXAMPLE",
                &SecretString::new("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY".into()),
            ),
            &"us-west-1".parse::<Region>().unwrap(),
            Some("awsexamplebucket1".to_string()),
            false,
        );

        let mut stream = Stream {
            tmp_file: NamedTempFile::new().unwrap(),
            count: 0,
            etags: Vec::new(),
            key: "test",
            part_number: 1,
            s3: &s3,
            upload_id: "test",
            sha: Context::new(&SHA256),
            md5: md5::Context::new(),
            channel: None,
            tmp_dir: PathBuf::new(),
            throttle: None,
            retries: 1,
        };

        let data = b"Hello, world!";
        write_to_stream(&mut stream, data).unwrap();

        assert_eq!(stream.count, data.len());
        assert!(stream.tmp_file.as_file().metadata().unwrap().len() > 0);
    }

    #[tokio::test]
    async fn test_write_to_stream_emits_staged_progress() {
        let s3 = create_test_s3();
        let (sender, receiver) = unbounded_channel();
        let handle =
            spawn_stream_progress_task(receiver, ProgressBar::hidden(), ProgressBar::hidden());

        let mut stream = Stream {
            tmp_file: NamedTempFile::new().unwrap(),
            count: 0,
            etags: Vec::new(),
            key: "test",
            part_number: 1,
            s3: &s3,
            upload_id: "test",
            sha: Context::new(&SHA256),
            md5: md5::Context::new(),
            channel: Some(sender),
            tmp_dir: PathBuf::new(),
            throttle: None,
            retries: 1,
        };

        let data = b"Hello, world!";
        write_to_stream(&mut stream, data).unwrap();
        drop(stream.channel.take());

        let progress = timeout(Duration::from_secs(1), handle)
            .await
            .unwrap()
            .unwrap();

        assert_eq!(progress.staged_current, data.len() as u64);
        assert_eq!(progress.staged_total, data.len() as u64);
        assert_eq!(progress.confirmed_total, 0);
        assert_eq!(progress.sending_current, 0);
        assert_eq!(progress.sending_total, 0);
        assert_eq!(progress.sending_part, None);
    }

    #[test]
    fn test_create_initial_stream() {
        let s3 = S3::new(
            &Credentials::new(
                "AKIAIOSFODNN7EXAMPLE",
                &SecretString::new("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY".into()),
            ),
            &"us-west-1".parse::<Region>().unwrap(),
            Some("awsexamplebucket1".to_string()),
            false,
        );

        let tmp_dir = PathBuf::new();
        let key = "test_key";
        let upload_id = "test_upload_id";
        let globals = RequestOptions {
            throttle: None,
            retries: 1,
            compress: false,
            encrypt: false,
            enc_key: None,
            object_lock: None,
        };

        let stream = create_initial_stream(InitialStreamParams {
            upload_id,
            tmp_dir: &tmp_dir,
            key,
            s3: &s3,
            progress_sender: None,
            globals: &globals,
            header_data: None,
        })
        .unwrap();

        assert_eq!(stream.key, key);
        assert_eq!(stream.upload_id, upload_id);
        assert_eq!(stream.part_number, 1);
        assert_eq!(stream.count, 0);
    }

    #[tokio::test]
    async fn test_create_initial_stream_header_counts_as_staged_progress() {
        let s3 = create_test_s3();
        let key = "test_key";
        let upload_id = "test_upload_id";
        let globals = RequestOptions {
            throttle: None,
            retries: 1,
            compress: false,
            encrypt: true,
            enc_key: None,
            object_lock: None,
        };
        let header = create_nonce_header(&[1, 2, 3, 4, 5, 6, 7]);
        let tmp_dir = PathBuf::new();
        let (sender, receiver) = unbounded_channel();
        let handle =
            spawn_stream_progress_task(receiver, ProgressBar::hidden(), ProgressBar::hidden());

        let mut stream = create_initial_stream(InitialStreamParams {
            upload_id,
            tmp_dir: &tmp_dir,
            key,
            s3: &s3,
            progress_sender: Some(sender),
            globals: &globals,
            header_data: Some(&header),
        })
        .unwrap();

        assert_eq!(stream.count, header.len());
        drop(stream.channel.take());

        let progress = timeout(Duration::from_secs(1), handle)
            .await
            .unwrap()
            .unwrap();

        assert_eq!(progress.staged_current, header.len() as u64);
        assert_eq!(progress.staged_total, header.len() as u64);
        assert_eq!(progress.confirmed_total, 0);
        assert_eq!(progress.sending_current, 0);
        assert_eq!(progress.sending_total, 0);
        assert_eq!(progress.sending_part, None);
    }

    #[test]
    fn test_compress_chunk() {
        let data = BytesMut::from("Hello, world!");
        let compressed = tokio::runtime::Runtime::new()
            .unwrap()
            .block_on(compress_chunk(data))
            .unwrap();

        assert!(
            !compressed.is_empty(),
            "Compressed data should not be empty"
        );

        assert_ne!(
            compressed, b"Hello, world!",
            "Compressed data should differ from original"
        );
    }

    #[tokio::test]
    async fn test_maybe_upload_part_1_no_upload() {
        let s3 = S3::new(
            &Credentials::new(
                "AKIAIOSFODNN7EXAMPLE",
                &SecretString::new("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY".into()),
            ),
            &"us-west-1".parse::<Region>().unwrap(),
            Some("awsexamplebucket1".to_string()),
            false,
        );

        let tmp_dir = PathBuf::new();
        let key = "test_key";
        let upload_id = "test_upload_id";
        let globals = RequestOptions {
            throttle: None,
            retries: 1,
            compress: false,
            encrypt: false,
            enc_key: None,
            object_lock: None,
        };

        let stream = create_initial_stream(InitialStreamParams {
            upload_id,
            tmp_dir: &tmp_dir,
            key,
            s3: &s3,
            progress_sender: None,
            globals: &globals,
            header_data: None,
        })
        .unwrap();

        let buffer_size = 1024; // 1KB for testing
        let mut stream = stream;
        let data = vec![0; 1023];

        write_to_stream(&mut stream, &data).unwrap();
        let result = maybe_upload_part(&mut stream, buffer_size).await;

        println!("Result: {result:?}");
    }

    #[tokio::test]
    async fn test_spawn_progress_task_finishes_when_sender_dropped() {
        let (sender, receiver) = unbounded_channel();
        let handle = spawn_progress_task(receiver, ProgressBar::hidden(), 10);

        sender.send(4).unwrap();
        sender.send(10).unwrap();
        drop(sender);

        let uploaded = timeout(Duration::from_secs(1), handle)
            .await
            .unwrap()
            .unwrap();

        assert_eq!(uploaded, 10);
    }

    #[tokio::test]
    async fn test_spawn_stream_progress_task_tracks_sending_and_confirmed_bytes() {
        let (sender, receiver) = unbounded_channel();
        let handle =
            spawn_stream_progress_task(receiver, ProgressBar::hidden(), ProgressBar::hidden());

        sender.send(StreamProgressEvent::Staged(4)).unwrap();
        sender.send(StreamProgressEvent::Staged(5)).unwrap();
        sender
            .send(StreamProgressEvent::SendingStarted {
                part_number: 2,
                total_bytes: 9,
            })
            .unwrap();
        sender
            .send(StreamProgressEvent::SendingProgress(4))
            .unwrap();
        sender.send(StreamProgressEvent::Confirmed(9)).unwrap();
        sender.send(StreamProgressEvent::ResetStaging).unwrap();
        drop(sender);

        let progress = timeout(Duration::from_secs(1), handle)
            .await
            .unwrap()
            .unwrap();

        assert_eq!(progress.staged_current, 0);
        assert_eq!(progress.staged_total, 9);
        assert_eq!(progress.confirmed_total, 9);
        assert_eq!(progress.sending_current, 0);
        assert_eq!(progress.sending_total, 0);
        assert_eq!(progress.sending_part, None);
    }

    // Comprehensive tests for complete_multipart_upload to prevent regressions

    fn create_mock_s3(endpoint: String, bucket: Option<&str>) -> S3 {
        S3::new(
            &Credentials::new(
                "minioadmin",
                &SecretString::new("minioadmin".to_string().into()),
            ),
            &Region::custom("us-east-1", endpoint),
            bucket.map(str::to_string),
            false,
        )
    }

    fn create_test_s3() -> S3 {
        S3::new(
            &Credentials::new(
                "AKIAIOSFODNN7EXAMPLE",
                &SecretString::new("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY".into()),
            ),
            &"us-west-1".parse::<Region>().unwrap(),
            Some("test-bucket".to_string()),
            false,
        )
    }

    #[tokio::test]
    async fn test_try_stream_part_retry_does_not_inflate_staged_progress() {
        let mut server = Server::new_async().await;
        let query = Matcher::AllOf(vec![
            Matcher::UrlEncoded("partNumber".into(), "1".into()),
            Matcher::UrlEncoded("uploadId".into(), "upload-id".into()),
        ]);

        let _first = server
            .mock("PUT", "/bucket/key")
            .match_query(query.clone())
            .with_status(503)
            .with_body("Slow Down")
            .expect(1)
            .create_async()
            .await;
        let _second = server
            .mock("PUT", "/bucket/key")
            .match_query(query)
            .with_status(200)
            .with_header("ETag", "\"retry-ok\"")
            .expect(1)
            .create_async()
            .await;

        let s3 = create_mock_s3(server.url(), Some("bucket"));
        let (sender, receiver) = unbounded_channel();
        let handle =
            spawn_stream_progress_task(receiver, ProgressBar::hidden(), ProgressBar::hidden());

        let mut stream = Stream {
            tmp_file: NamedTempFile::new().unwrap(),
            count: 0,
            etags: Vec::new(),
            key: "key",
            part_number: 1,
            s3: &s3,
            upload_id: "upload-id",
            sha: Context::new(&SHA256),
            md5: md5::Context::new(),
            channel: Some(sender),
            tmp_dir: PathBuf::new(),
            throttle: None,
            retries: 2,
        };

        let data = b"retry me once";
        write_to_stream(&mut stream, data).unwrap();

        let etag = try_stream_part(&stream).await.unwrap();
        assert_eq!(etag, "\"retry-ok\"");

        drop(stream.channel.take());

        let progress = timeout(Duration::from_secs(3), handle)
            .await
            .unwrap()
            .unwrap();

        assert_eq!(progress.staged_current, data.len() as u64);
        assert_eq!(progress.staged_total, data.len() as u64);
        assert_eq!(progress.confirmed_total, data.len() as u64);
        assert_eq!(progress.sending_current, 0);
        assert_eq!(progress.sending_total, 0);
        assert_eq!(progress.sending_part, None);
    }

    #[tokio::test]
    async fn test_maybe_upload_part_emits_confirmed_progress_and_resets_staging() {
        let mut server = Server::new_async().await;
        let query = Matcher::AllOf(vec![
            Matcher::UrlEncoded("partNumber".into(), "1".into()),
            Matcher::UrlEncoded("uploadId".into(), "upload-id".into()),
        ]);

        let _upload = server
            .mock("PUT", "/bucket/key")
            .match_query(query)
            .with_status(200)
            .with_header("ETag", "\"part-ok\"")
            .expect(1)
            .create_async()
            .await;

        let s3 = create_mock_s3(server.url(), Some("bucket"));
        let (sender, receiver) = unbounded_channel();
        let handle =
            spawn_stream_progress_task(receiver, ProgressBar::hidden(), ProgressBar::hidden());

        let mut stream = Stream {
            tmp_file: NamedTempFile::new().unwrap(),
            count: 0,
            etags: Vec::new(),
            key: "key",
            part_number: 1,
            s3: &s3,
            upload_id: "upload-id",
            sha: Context::new(&SHA256),
            md5: md5::Context::new(),
            channel: Some(sender),
            tmp_dir: PathBuf::new(),
            throttle: None,
            retries: 1,
        };

        let data = vec![0_u8; 32];
        write_to_stream(&mut stream, &data).unwrap();
        maybe_upload_part(&mut stream, 16).await.unwrap();
        drop(stream.channel.take());

        let progress = timeout(Duration::from_secs(1), handle)
            .await
            .unwrap()
            .unwrap();

        assert_eq!(progress.staged_current, 0);
        assert_eq!(progress.staged_total, data.len() as u64);
        assert_eq!(progress.confirmed_total, data.len() as u64);
        assert_eq!(progress.sending_current, 0);
        assert_eq!(progress.sending_total, 0);
        assert_eq!(progress.sending_part, None);
        assert_eq!(stream.count, 0);
        assert_eq!(stream.part_number, 2);
        assert_eq!(stream.etags.len(), 1);
    }

    #[tokio::test]
    async fn test_complete_multipart_upload_single_part() {
        let s3 = create_test_s3();
        let etags = vec!["etag1".to_string()];

        let result = complete_multipart_upload(&s3, "test-key", "upload-123", etags).await;

        // This will fail because we're not mocking the S3 API, but we're testing the part number conversion logic
        // The important part is that it doesn't panic and correctly creates the parts map
        assert!(result.is_err(), "Expected error due to unmocked S3 API");
    }

    #[tokio::test]
    async fn test_complete_multipart_upload_100_parts() {
        let s3 = create_test_s3();
        let etags: Vec<String> = (1..=100).map(|i| format!("etag{i}")).collect();

        let result = complete_multipart_upload(&s3, "test-key", "upload-123", etags).await;

        // Should not panic, will fail at API call
        assert!(result.is_err(), "Expected error due to unmocked S3 API");
    }

    #[tokio::test]
    async fn test_complete_multipart_upload_1000_parts() {
        let s3 = create_test_s3();
        let etags: Vec<String> = (1..=1000).map(|i| format!("etag{i}")).collect();

        let result = complete_multipart_upload(&s3, "test-key", "upload-123", etags).await;

        // Should not panic, will fail at API call
        assert!(result.is_err(), "Expected error due to unmocked S3 API");
    }

    #[tokio::test]
    async fn test_complete_multipart_upload_exactly_10000_parts() {
        let s3 = create_test_s3();
        let etags: Vec<String> = (1..=10_000).map(|i| format!("etag{i}")).collect();

        let result = complete_multipart_upload(&s3, "test-key", "upload-123", etags).await;

        // Should not panic with exactly 10,000 parts (the S3 maximum)
        // Will fail at API call but that's expected
        assert!(result.is_err(), "Expected error due to unmocked S3 API");
    }

    #[tokio::test]
    async fn test_complete_multipart_upload_10001_parts_returns_error() {
        let s3 = create_test_s3();
        let etags: Vec<String> = (1..=10_001).map(|i| format!("etag{i}")).collect();

        let result = complete_multipart_upload(&s3, "test-key", "upload-123", etags).await;

        // Should return error, not panic
        assert!(result.is_err(), "Expected error for exceeding 10,000 parts");

        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("10001 parts created"),
            "Error message should mention part count. Got: {err_msg}"
        );
        assert!(
            err_msg.contains("maximum 10,000 parts"),
            "Error message should mention the limit. Got: {err_msg}"
        );
        assert!(
            err_msg.contains("s3m rm"),
            "Error message should include cleanup command. Got: {err_msg}"
        );
        assert!(
            err_msg.contains("--abort"),
            "Error message should include abort flag. Got: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_complete_multipart_upload_20000_parts_returns_error() {
        let s3 = create_test_s3();
        let etags: Vec<String> = (1..=20_000).map(|i| format!("etag{i}")).collect();

        let result = complete_multipart_upload(&s3, "test-key", "upload-123", etags).await;

        // Should return error, not panic
        assert!(result.is_err(), "Expected error for exceeding 10,000 parts");

        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("20000 parts created"),
            "Error message should mention actual part count. Got: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_complete_multipart_upload_error_contains_context() {
        let s3 = create_test_s3();
        let etags: Vec<String> = (1..=15_000).map(|i| format!("etag{i}")).collect();

        let result =
            complete_multipart_upload(&s3, "my-large-file.dat", "ABC123DEF456", etags).await;

        assert!(result.is_err(), "Expected error for exceeding 10,000 parts");

        let err_msg = result.unwrap_err().to_string();

        // Verify all important context is in the error message
        assert!(
            err_msg.contains("my-large-file.dat"),
            "Error should contain the key. Got: {err_msg}"
        );
        assert!(
            err_msg.contains("ABC123DEF456"),
            "Error should contain the upload ID. Got: {err_msg}"
        );
        assert!(
            err_msg.contains("github.com/s3m/s3m/issues"),
            "Error should contain bug reporting URL. Got: {err_msg}"
        );
    }

    #[test]
    fn test_part_number_conversion_edge_cases() {
        // Test that our conversion logic handles edge cases correctly

        // Test index 0 -> part number 1
        let part_num = u16::try_from(1_usize).unwrap();
        assert_eq!(part_num, 1, "First part should be numbered 1");

        // Test index 9,999 -> part number 10,000
        let part_num = u16::try_from(10_000_usize).unwrap();
        assert_eq!(part_num, 10_000, "Part 10,000 should fit in u16");

        // Test that index 10,000 -> part number 10,001 would overflow our validation
        // (but our validation catches it before conversion)
        let etags_count = 10_001_usize;
        assert!(
            etags_count > 10_000,
            "Validation should catch this before conversion"
        );
    }

    #[test]
    fn test_u16_max_value_sufficient_for_s3_limit() {
        // Verify our assumption that u16 is sufficient for S3's 10,000 part limit
        // u16::MAX is 65535, which is much larger than 10,000
        use crate::s3::limits::MAX_PARTS_PER_UPLOAD;
        const _: () = assert!(u16::MAX as usize > MAX_PARTS_PER_UPLOAD);

        // Verify we can represent 10,000
        assert!(
            u16::try_from(10_000_usize).is_ok(),
            "Must be able to convert 10,000 to u16"
        );

        // Verify 10,001 also fits (for testing error path)
        assert!(
            u16::try_from(10_001_usize).is_ok(),
            "Must be able to convert 10,001 to u16 for testing"
        );
    }
}