smugmug-cli 0.5.1

A command-line tool for uploading photos to SmugMug with deduplication
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
use anyhow::{Context as AnyhowContext, Result};
use chrono::Utc;
use md5::Context as Md5Context;
use sha2::{Digest, Sha256};
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;
use std::sync::Arc;
use tokio::sync::Mutex;

use crate::api::SmugMugClient;
use crate::api::images::AlbumImage;
use crate::api::upload::{UploadPayload, replace_image, upload_image};
use crate::cache::hash_store::{HashStore, UploadedFile};
use crate::uploader::album_series::{AlbumSeries, ClientAlbumSeries};

pub struct UploadWorkerContext {
    pub client: Arc<SmugMugClient>,
    /// Target album for new images, unless `series` is set.
    pub album_uri: String,
    pub album_key: String,
    /// When set, each new image claims room in this album series instead of
    /// going to `album_uri` (see `album_series`).
    pub series: Option<Arc<AlbumSeries<ClientAlbumSeries>>>,
    pub hash_store: Arc<Mutex<HashStore>>,
    pub remote_md5s: Option<Arc<std::collections::HashMap<String, String>>>,
    /// Existing images already in the target album, keyed by filename. Used
    /// to decide, per file, whether to skip (content unchanged), replace
    /// (same filename, different content) or create (new filename).
    pub remote_images: Option<Arc<std::collections::HashMap<String, AlbumImage>>>,
    pub dry_run: bool,
    pub no_cache: bool,
    pub retry_attempts: u32,
    /// Upload RAW files as JPEGs rendered from them (see `crate::raw`)
    /// instead of as originals.
    pub render_raw: bool,
    pub skip_raw_files: Arc<std::sync::atomic::AtomicBool>,
    /// SHA-256 of files already hashed before the upload (see `collect`).
    pub known_hashes: Option<Arc<std::collections::HashMap<std::path::PathBuf, String>>>,
}

/// Determines if an error is retryable (transient) or permanent
fn is_retryable_error(error: &anyhow::Error) -> bool {
    let error_msg = error.to_string().to_lowercase();

    // Retry on network errors, timeouts, and 5xx server errors
    error_msg.contains("timeout")
        || error_msg.contains("connection")
        || error_msg.contains("network")
        || error_msg.contains("500")
        || error_msg.contains("502")
        || error_msg.contains("503")
        || error_msg.contains("504")
        || error_msg.contains("429") // Rate limiting
}

pub async fn upload_worker(
    file_path: &Path,
    context: Arc<UploadWorkerContext>,
) -> Result<UploadStatus> {
    let is_raw = crate::scanner::is_raw_file(file_path);
    let render = is_raw && context.render_raw;

    // Check if this is a RAW original and RAW uploads were found not to work
    if is_raw
        && !render
        && context
            .skip_raw_files
            .load(std::sync::atomic::Ordering::Relaxed)
    {
        return Ok(UploadStatus::Skipped);
    }

    // Calculate file hash (SHA256 for local cache). For a rendered RAW this
    // is the RAW file's hash, so re-runs skip it without rendering again.
    let known_hash = context
        .known_hashes
        .as_ref()
        .and_then(|hashes| hashes.get(file_path).cloned());
    let hash = match known_hash {
        Some(hash) => hash,
        None => calculate_file_hash(file_path).with_context(|| "Failed to calculate file hash")?,
    };

    // Check local cache first (unless no_cache is enabled)
    if !context.no_cache {
        let store = context.hash_store.lock().await;
        if let Some(_cached) = store.get(&hash)? {
            return Ok(UploadStatus::Skipped);
        }
    }

    if render {
        return upload_rendered_raw(file_path, &hash, &context).await;
    }

    let filename = file_path
        .file_name()
        .and_then(|n| n.to_str())
        .map(|s| s.to_string());

    // MD5 is needed for both the optional --check-remote scan and the
    // default same-filename skip/replace comparison below.
    let needs_md5 = context.remote_md5s.is_some() || context.remote_images.is_some();
    let md5_hash = if needs_md5 {
        Some(calculate_md5_hash(file_path).with_context(|| "Failed to calculate MD5 hash")?)
    } else {
        None
    };

    // Check remote MD5s if --check-remote is enabled (matches content
    // anywhere in the album, regardless of filename)
    if let (Some(remote_md5s), Some(md5_hash)) = (&context.remote_md5s, &md5_hash) {
        if remote_md5s.contains_key(&md5_hash.to_lowercase()) {
            return Ok(UploadStatus::Skipped);
        }
    }

    // If an image with this filename already exists in the album, either
    // skip (content is unchanged) or replace it in place (content differs)
    // instead of attempting a fresh create, which SmugMug would reject with
    // 409 Conflict for a duplicate filename.
    let mut replace_target: Option<String> = None;
    if let (Some(remote_images), Some(filename), Some(md5_hash)) =
        (&context.remote_images, &filename, &md5_hash)
    {
        if let Some(remote_image) = remote_images.get(filename) {
            let remote_md5_matches = remote_image
                .archived_md5
                .as_deref()
                .map(|m| m.eq_ignore_ascii_case(md5_hash))
                .unwrap_or(false);

            if remote_md5_matches {
                return Ok(UploadStatus::Skipped);
            }

            replace_target = Some(remote_image.uri.clone());
        }
    }

    // Get file size
    let file_size = std::fs::metadata(file_path)
        .with_context(|| "Failed to get file metadata")?
        .len();

    let (album_uri, album_key, claimed) = claim_album(&context, replace_target.is_some()).await?;

    // Skip actual upload if dry run
    if context.dry_run {
        return Ok(UploadStatus::DryRun { file_size });
    }

    let payload = UploadPayload::from_path(file_path)
        .await
        .with_context(|| "Failed to read file")?;
    let result = upload_with_retries(
        &context,
        file_path,
        &hash,
        &payload,
        is_raw,
        replace_target.as_deref(),
        &album_uri,
        &album_key,
    )
    .await;

    release_on_failure(&context, &result, claimed).await;
    result
}

/// Upload a JPEG rendered from the RAW file at `file_path` (whose SHA256 is
/// `hash`) under the RAW's name with a `.jpg` extension.
///
/// An image of that name already in the album counts as this file, whatever
/// its content: it may have been rendered by an older version of this tool,
/// and replacing it would just churn the same photo.
async fn upload_rendered_raw(
    file_path: &Path,
    hash: &str,
    context: &UploadWorkerContext,
) -> Result<UploadStatus> {
    let file_name = crate::raw::rendered_file_name(file_path);
    if let Some(remote_images) = &context.remote_images
        && remote_images.contains_key(&file_name)
    {
        return Ok(UploadStatus::Skipped);
    }

    // Rendering reads the whole RAW file and scans it, so keep it off the
    // async runtime's threads.
    let path = file_path.to_path_buf();
    let rendered = tokio::task::spawn_blocking(move || crate::raw::render_jpeg(&path))
        .await
        .with_context(|| "RAW rendering task failed")?
        .with_context(|| "Failed to render RAW file to JPEG")?;

    if let Some(remote_md5s) = &context.remote_md5s
        && remote_md5s.contains_key(&format!("{:x}", md5::compute(&rendered.data)))
    {
        return Ok(UploadStatus::Skipped);
    }

    let payload = UploadPayload {
        data: rendered.data.into(),
        file_name,
        mime_type: "image/jpeg".to_string(),
    };
    let file_size = payload.data.len() as u64;

    let (album_uri, album_key, claimed) = claim_album(context, false).await?;

    if context.dry_run {
        return Ok(UploadStatus::DryRun { file_size });
    }

    let result = upload_with_retries(
        context, file_path, hash, &payload, false, None, &album_uri, &album_key,
    )
    .await;

    release_on_failure(context, &result, claimed).await;
    result
}

/// The album a file goes into: `context`'s album, or, for a new image in an
/// album series, room claimed in the first album of the series that has
/// some, which may create that album. Claims happen only once a file is
/// known to need uploading, so skipped files never use up space. On a dry
/// run the claim is still made (without creating anything) so the summary
/// shows where files would go.
async fn claim_album(
    context: &UploadWorkerContext,
    replacing: bool,
) -> Result<(String, String, Option<crate::api::albums::Album>)> {
    Ok(match (&context.series, replacing) {
        (Some(series), false) => {
            let album = series.claim().await?;
            (album.uri.clone(), album.album_key.clone(), Some(album))
        }
        _ => (context.album_uri.clone(), context.album_key.clone(), None),
    })
}

/// A failed upload gives its claimed room back.
async fn release_on_failure(
    context: &UploadWorkerContext,
    result: &Result<UploadStatus>,
    claimed: Option<crate::api::albums::Album>,
) {
    if let (Err(_), Some(album), Some(series)) = (result, &claimed, &context.series) {
        series.release(album).await;
    }
}

#[allow(clippy::too_many_arguments)]
async fn upload_with_retries(
    context: &UploadWorkerContext,
    file_path: &Path,
    hash: &str,
    payload: &UploadPayload,
    is_raw: bool,
    replace_target: Option<&str>,
    album_uri: &str,
    album_key: &str,
) -> Result<UploadStatus> {
    // Upload file with retry logic
    let mut last_error = None;
    let max_attempts = context.retry_attempts.max(1); // At least 1 attempt
    let file_size = payload.data.len() as u64;

    for attempt in 1..=max_attempts {
        let upload_attempt = match replace_target {
            Some(image_uri) => replace_image(&context.client, image_uri, payload).await,
            None => upload_image(&context.client, album_uri, payload).await,
        };

        match upload_attempt {
            Ok(upload_result) => {
                // Success! Update cache and return
                let uploaded_file = UploadedFile {
                    smugmug_uri: upload_result.image_uri.clone(),
                    album_key: album_key.to_string(),
                    image_key: upload_result.image_key.clone(),
                    uploaded_at: Utc::now(),
                    file_size,
                    original_path: file_path.to_string_lossy().to_string(),
                };

                {
                    let store = context.hash_store.lock().await;
                    store.insert(hash, uploaded_file)?;
                }

                return Ok(if replace_target.is_some() {
                    UploadStatus::Replaced { file_size }
                } else {
                    UploadStatus::Uploaded { file_size }
                });
            }
            Err(e) => {
                let error = e.context("Failed to upload image");

                // If this is a RAW file and upload failed, disable future RAW uploads
                if is_raw {
                    let was_already_set = context
                        .skip_raw_files
                        .swap(true, std::sync::atomic::Ordering::Relaxed);
                    if !was_already_set {
                        eprintln!("\n⚠️  RAW file upload failed. Skipping remaining RAW files.");
                        eprintln!("   (RAW files require a SmugMug Source subscription)");
                    }
                }

                // Check if we should retry
                if attempt < max_attempts && is_retryable_error(&error) {
                    // Calculate exponential backoff: 1s, 2s, 4s, 8s...
                    let delay_secs = 2u64.pow(attempt - 1);
                    tokio::time::sleep(tokio::time::Duration::from_secs(delay_secs)).await;
                    last_error = Some(error);
                } else {
                    // Don't retry - either last attempt or non-retryable error
                    return Err(error);
                }
            }
        }
    }

    // If we get here, all retries failed
    Err(last_error.unwrap_or_else(|| anyhow::anyhow!("Upload failed after retries")))
}

pub enum UploadStatus {
    Uploaded {
        file_size: u64,
    },
    /// An existing image with the same filename was updated in place
    /// because its content had changed.
    Replaced {
        file_size: u64,
    },
    Skipped,
    DryRun {
        file_size: u64,
    },
}

pub fn calculate_file_hash(file_path: &Path) -> Result<String> {
    let file = File::open(file_path)?;
    let mut reader = BufReader::new(file);
    let mut hasher = Sha256::new();
    let mut buffer = [0; 8192];

    loop {
        let count = reader.read(&mut buffer)?;
        if count == 0 {
            break;
        }
        hasher.update(&buffer[..count]);
    }

    Ok(hex::encode(hasher.finalize()))
}

fn calculate_md5_hash(file_path: &Path) -> Result<String> {
    let file = File::open(file_path)?;
    let mut reader = BufReader::new(file);
    let mut context = Md5Context::new();
    let mut buffer = [0; 8192];

    loop {
        let count = reader.read(&mut buffer)?;
        if count == 0 {
            break;
        }
        context.consume(&buffer[..count]);
    }

    Ok(format!("{:x}", context.finalize()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::io::Write;
    use tempfile::NamedTempFile;

    // Tests for worker functions: hash calculation, deduplication, and upload status
    // All tests use temporary files and avoid actual API calls

    // Helper to create a temporary test file with content
    fn create_test_file(content: &[u8]) -> NamedTempFile {
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(content).unwrap();
        file.flush().unwrap();
        file
    }

    #[test]
    fn test_calculate_file_hash() {
        let content = b"test content for hashing";
        let file = create_test_file(content);

        let hash = calculate_file_hash(file.path()).unwrap();

        // Verify hash is valid SHA256 format (64 hex characters)
        assert_eq!(hash.len(), 64);
        assert!(hash.chars().all(|c| c.is_ascii_hexdigit()));

        // Verify hash is consistent
        let hash2 = calculate_file_hash(file.path()).unwrap();
        assert_eq!(hash, hash2);
    }

    #[test]
    fn test_calculate_file_hash_empty_file() {
        let file = create_test_file(b"");
        let hash = calculate_file_hash(file.path()).unwrap();

        // SHA256 of empty file
        assert_eq!(
            hash,
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
        );
    }

    #[test]
    fn test_calculate_file_hash_large_file() {
        // Create a file larger than buffer size (8192 bytes)
        let content: Vec<u8> = (0..20000).map(|i| (i % 256) as u8).collect();
        let file = create_test_file(&content);

        let hash = calculate_file_hash(file.path()).unwrap();
        assert_eq!(hash.len(), 64);
    }

    #[test]
    fn test_calculate_md5_hash() {
        let content = b"test content for md5";
        let file = create_test_file(content);

        let hash = calculate_md5_hash(file.path()).unwrap();

        // Verify hash is valid MD5 format (32 hex characters)
        assert_eq!(hash.len(), 32);
        assert!(hash.chars().all(|c| c.is_ascii_hexdigit()));

        // Verify hash is consistent
        let hash2 = calculate_md5_hash(file.path()).unwrap();
        assert_eq!(hash, hash2);
    }

    #[test]
    fn test_calculate_md5_hash_empty_file() {
        let file = create_test_file(b"");
        let hash = calculate_md5_hash(file.path()).unwrap();

        // MD5 of empty file
        assert_eq!(hash, "d41d8cd98f00b204e9800998ecf8427e");
    }

    #[test]
    fn test_different_content_different_hashes() {
        let file1 = create_test_file(b"content1");
        let file2 = create_test_file(b"content2");

        let hash1 = calculate_file_hash(file1.path()).unwrap();
        let hash2 = calculate_file_hash(file2.path()).unwrap();

        assert_ne!(hash1, hash2);

        let md5_1 = calculate_md5_hash(file1.path()).unwrap();
        let md5_2 = calculate_md5_hash(file2.path()).unwrap();

        assert_ne!(md5_1, md5_2);
    }

    #[tokio::test]
    async fn test_upload_worker_dry_run() {
        let file = create_test_file(b"test image content");
        let temp_dir = tempfile::tempdir().unwrap();

        let hash_store = Arc::new(Mutex::new(
            HashStore::new(temp_dir.path().to_str().unwrap()).unwrap(),
        ));

        let client = Arc::new(SmugMugClient::new(
            "test_key".to_string(),
            "test_secret".to_string(),
            "test_token".to_string(),
            "test_token_secret".to_string(),
        ));

        let context = Arc::new(UploadWorkerContext {
            client,
            album_uri: "/api/v2/album/test".to_string(),
            album_key: "test_album".to_string(),
            series: None,
            hash_store,
            remote_md5s: None,
            remote_images: None,
            dry_run: true,
            no_cache: false,
            retry_attempts: 3,
            render_raw: false,
            skip_raw_files: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            known_hashes: None,
        });

        let result = upload_worker(file.path(), context).await.unwrap();

        match result {
            UploadStatus::DryRun { file_size } => {
                assert!(file_size > 0);
            }
            _ => panic!("Expected DryRun status"),
        }
    }

    #[tokio::test]
    async fn test_upload_worker_skipped_local_cache() {
        let file = create_test_file(b"cached content");
        let temp_dir = tempfile::tempdir().unwrap();

        let hash_store = Arc::new(Mutex::new(
            HashStore::new(temp_dir.path().to_str().unwrap()).unwrap(),
        ));

        // Pre-populate cache
        let hash = calculate_file_hash(file.path()).unwrap();
        let uploaded_file = UploadedFile {
            smugmug_uri: "/api/v2/image/test".to_string(),
            album_key: "test_album".to_string(),
            image_key: "test_key".to_string(),
            uploaded_at: Utc::now(),
            file_size: 100,
            original_path: file.path().to_string_lossy().to_string(),
        };
        hash_store
            .lock()
            .await
            .insert(&hash, uploaded_file)
            .unwrap();

        let client = Arc::new(SmugMugClient::new(
            "test_key".to_string(),
            "test_secret".to_string(),
            "test_token".to_string(),
            "test_token_secret".to_string(),
        ));

        let context = Arc::new(UploadWorkerContext {
            client,
            album_uri: "/api/v2/album/test".to_string(),
            album_key: "test_album".to_string(),
            series: None,
            hash_store,
            remote_md5s: None,
            remote_images: None,
            dry_run: false,
            no_cache: false,
            retry_attempts: 3,
            render_raw: false,
            skip_raw_files: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            known_hashes: None,
        });

        let result = upload_worker(file.path(), context).await.unwrap();

        match result {
            UploadStatus::Skipped => {
                // Success - file was skipped due to local cache
            }
            _ => panic!("Expected Skipped status due to local cache"),
        }
    }

    #[tokio::test]
    async fn test_upload_worker_skipped_remote_md5() {
        let file = create_test_file(b"remote content");
        let temp_dir = tempfile::tempdir().unwrap();

        let hash_store = Arc::new(Mutex::new(
            HashStore::new(temp_dir.path().to_str().unwrap()).unwrap(),
        ));

        // Calculate MD5 and add to remote map
        let md5 = calculate_md5_hash(file.path()).unwrap();
        let mut remote_md5s = HashMap::new();
        remote_md5s.insert(md5.to_lowercase(), "remote_image_key".to_string());

        let client = Arc::new(SmugMugClient::new(
            "test_key".to_string(),
            "test_secret".to_string(),
            "test_token".to_string(),
            "test_token_secret".to_string(),
        ));

        let context = Arc::new(UploadWorkerContext {
            client,
            album_uri: "/api/v2/album/test".to_string(),
            album_key: "test_album".to_string(),
            series: None,
            hash_store,
            remote_md5s: Some(Arc::new(remote_md5s)),
            remote_images: None,
            dry_run: false,
            no_cache: false,
            retry_attempts: 3,
            render_raw: false,
            skip_raw_files: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            known_hashes: None,
        });

        let result = upload_worker(file.path(), context).await.unwrap();

        match result {
            UploadStatus::Skipped => {
                // Success - file was skipped due to remote MD5 match
            }
            _ => panic!("Expected Skipped status due to remote MD5"),
        }
    }

    #[tokio::test]
    async fn test_upload_worker_skipped_same_filename_matching_md5() {
        let file = create_test_file(b"unchanged content");
        let temp_dir = tempfile::tempdir().unwrap();

        let hash_store = Arc::new(Mutex::new(
            HashStore::new(temp_dir.path().to_str().unwrap()).unwrap(),
        ));

        let filename = file
            .path()
            .file_name()
            .unwrap()
            .to_str()
            .unwrap()
            .to_string();
        let md5 = calculate_md5_hash(file.path()).unwrap();

        let mut remote_images = HashMap::new();
        remote_images.insert(
            filename,
            AlbumImage {
                image_key: "EXISTING123".to_string(),
                file_name: file
                    .path()
                    .file_name()
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .to_string(),
                archived_uri: "https://example.com/existing.jpg".to_string(),
                file_size: 100,
                format: "JPG".to_string(),
                uri: "/api/v2/image/EXISTING123".to_string(),
                title: None,
                archived_md5: Some(md5),
            },
        );

        let client = Arc::new(SmugMugClient::new(
            "test_key".to_string(),
            "test_secret".to_string(),
            "test_token".to_string(),
            "test_token_secret".to_string(),
        ));

        let context = Arc::new(UploadWorkerContext {
            client,
            album_uri: "/api/v2/album/test".to_string(),
            album_key: "test_album".to_string(),
            series: None,
            hash_store,
            remote_md5s: None,
            remote_images: Some(Arc::new(remote_images)),
            dry_run: false,
            no_cache: false,
            retry_attempts: 3,
            render_raw: false,
            skip_raw_files: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            known_hashes: None,
        });

        // No local cache entry, but the remote image with the same filename
        // has matching content, so no upload/replace call should be made.
        let result = upload_worker(file.path(), context).await.unwrap();

        match result {
            UploadStatus::Skipped => {}
            _ => panic!("Expected Skipped status because remote content is unchanged"),
        }
    }

    #[tokio::test]
    async fn test_upload_worker_no_cache_flag() {
        let file = create_test_file(b"no cache test");
        let temp_dir = tempfile::tempdir().unwrap();

        let hash_store = Arc::new(Mutex::new(
            HashStore::new(temp_dir.path().to_str().unwrap()).unwrap(),
        ));

        // Pre-populate cache
        let hash = calculate_file_hash(file.path()).unwrap();
        let uploaded_file = UploadedFile {
            smugmug_uri: "/api/v2/image/test".to_string(),
            album_key: "test_album".to_string(),
            image_key: "test_key".to_string(),
            uploaded_at: Utc::now(),
            file_size: 100,
            original_path: file.path().to_string_lossy().to_string(),
        };
        hash_store
            .lock()
            .await
            .insert(&hash, uploaded_file)
            .unwrap();

        let client = Arc::new(SmugMugClient::new(
            "test_key".to_string(),
            "test_secret".to_string(),
            "test_token".to_string(),
            "test_token_secret".to_string(),
        ));

        let context = Arc::new(UploadWorkerContext {
            client,
            album_uri: "/api/v2/album/test".to_string(),
            album_key: "test_album".to_string(),
            series: None,
            hash_store,
            remote_md5s: None,
            remote_images: None,
            dry_run: true,  // Use dry run to avoid actual upload
            no_cache: true, // This should bypass local cache
            retry_attempts: 3,
            render_raw: false,
            skip_raw_files: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            known_hashes: None,
        });

        let result = upload_worker(file.path(), context).await.unwrap();

        // With no_cache=true, should not be skipped even though in cache
        match result {
            UploadStatus::DryRun { .. } => {
                // Success - cache was bypassed
            }
            _ => panic!("Expected DryRun status (cache should be bypassed)"),
        }
    }

    /// A dry-run context that renders RAW files, with `remote_names` as the
    /// images already in the album.
    fn render_context(remote_names: &[&str]) -> (Arc<UploadWorkerContext>, tempfile::TempDir) {
        let temp_dir = tempfile::tempdir().unwrap();
        let hash_store = Arc::new(Mutex::new(
            HashStore::new(temp_dir.path().join("cache").to_str().unwrap()).unwrap(),
        ));
        let remote_images = remote_names
            .iter()
            .map(|name| {
                let image = AlbumImage {
                    image_key: "K".to_string(),
                    file_name: name.to_string(),
                    archived_uri: String::new(),
                    file_size: 1,
                    format: "JPG".to_string(),
                    uri: "/api/v2/image/K-0".to_string(),
                    title: None,
                    archived_md5: Some("0".repeat(32)),
                };
                (name.to_string(), image)
            })
            .collect();
        let context = Arc::new(UploadWorkerContext {
            client: Arc::new(SmugMugClient::new(
                "k".to_string(),
                "s".to_string(),
                "t".to_string(),
                "ts".to_string(),
            )),
            album_uri: "/api/v2/album/test".to_string(),
            album_key: "test".to_string(),
            series: None,
            hash_store,
            remote_md5s: None,
            remote_images: Some(Arc::new(remote_images)),
            dry_run: true,
            no_cache: false,
            retry_attempts: 1,
            render_raw: true,
            skip_raw_files: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            known_hashes: None,
        });
        (context, temp_dir)
    }

    fn write_fake_raw(dir: &Path, name: &str) -> std::path::PathBuf {
        let mut raw = crate::raw::exif::tests::fake_tiff_raw_be();
        raw.extend(crate::raw::jpeg::tests::fake_jpeg(0xC0, 6000, 4000, None));
        let path = dir.join(name);
        std::fs::write(&path, raw).unwrap();
        path
    }

    #[tokio::test]
    async fn test_rendered_raw_dry_run_reports_jpeg_size() {
        let (context, dir) = render_context(&[]);
        let path = write_fake_raw(dir.path(), "IMG_1.NEF");
        let expected = crate::raw::render_jpeg(&path).unwrap().data.len() as u64;

        match upload_worker(&path, context).await.unwrap() {
            UploadStatus::DryRun { file_size } => assert_eq!(file_size, expected),
            _ => panic!("Expected DryRun status"),
        }
    }

    #[tokio::test]
    async fn test_rendered_raw_skips_existing_jpeg_name() {
        // Any image named like the rendered JPEG counts as this file, even
        // with different content, so it is skipped rather than replaced.
        let (context, dir) = render_context(&["IMG_1.jpg"]);
        let path = write_fake_raw(dir.path(), "IMG_1.NEF");

        assert!(matches!(
            upload_worker(&path, context).await.unwrap(),
            UploadStatus::Skipped
        ));
    }

    #[tokio::test]
    async fn test_rendered_raw_without_preview_fails() {
        let (context, dir) = render_context(&[]);
        let path = dir.path().join("IMG_1.CR2");
        std::fs::write(&path, crate::raw::exif::tests::fake_tiff_raw_be()).unwrap();

        let err = upload_worker(&path, context).await.err().unwrap();
        assert!(format!("{err:#}").contains("no embedded JPEG preview"));
    }

    #[test]
    fn test_upload_status_variants() {
        let uploaded = UploadStatus::Uploaded { file_size: 1024 };
        let skipped = UploadStatus::Skipped;
        let dry_run = UploadStatus::DryRun { file_size: 2048 };

        match uploaded {
            UploadStatus::Uploaded { file_size } => assert_eq!(file_size, 1024),
            _ => panic!("Wrong variant"),
        }

        match skipped {
            UploadStatus::Skipped => {}
            _ => panic!("Wrong variant"),
        }

        match dry_run {
            UploadStatus::DryRun { file_size } => assert_eq!(file_size, 2048),
            _ => panic!("Wrong variant"),
        }
    }

    #[test]
    fn test_is_retryable_error_timeout() {
        let error = anyhow::anyhow!("Connection timeout occurred");
        assert!(is_retryable_error(&error));
    }

    #[test]
    fn test_is_retryable_error_connection() {
        let error = anyhow::anyhow!("Connection refused");
        assert!(is_retryable_error(&error));
    }

    #[test]
    fn test_is_retryable_error_network() {
        let error = anyhow::anyhow!("Network error encountered");
        assert!(is_retryable_error(&error));
    }

    #[test]
    fn test_is_retryable_error_500() {
        let error = anyhow::anyhow!("Server returned 500 Internal Server Error");
        assert!(is_retryable_error(&error));
    }

    #[test]
    fn test_is_retryable_error_502() {
        let error = anyhow::anyhow!("502 Bad Gateway");
        assert!(is_retryable_error(&error));
    }

    #[test]
    fn test_is_retryable_error_503() {
        let error = anyhow::anyhow!("503 Service Unavailable");
        assert!(is_retryable_error(&error));
    }

    #[test]
    fn test_is_retryable_error_504() {
        let error = anyhow::anyhow!("504 Gateway Timeout");
        assert!(is_retryable_error(&error));
    }

    #[test]
    fn test_is_retryable_error_rate_limit() {
        let error = anyhow::anyhow!("429 Too Many Requests");
        assert!(is_retryable_error(&error));
    }

    #[test]
    fn test_is_not_retryable_error_auth() {
        let error = anyhow::anyhow!("401 Unauthorized");
        assert!(!is_retryable_error(&error));
    }

    #[test]
    fn test_is_not_retryable_error_not_found() {
        let error = anyhow::anyhow!("404 Not Found");
        assert!(!is_retryable_error(&error));
    }

    #[test]
    fn test_is_not_retryable_error_validation() {
        let error = anyhow::anyhow!("Invalid input: file too large");
        assert!(!is_retryable_error(&error));
    }

    #[test]
    fn test_is_not_retryable_error_forbidden() {
        let error = anyhow::anyhow!("403 Forbidden");
        assert!(!is_retryable_error(&error));
    }

    #[tokio::test]
    async fn test_upload_worker_context_creation() {
        let temp_dir = tempfile::tempdir().unwrap();
        let hash_store = Arc::new(Mutex::new(
            HashStore::new(temp_dir.path().to_str().unwrap()).unwrap(),
        ));

        let client = Arc::new(SmugMugClient::new(
            "key".to_string(),
            "secret".to_string(),
            "token".to_string(),
            "token_secret".to_string(),
        ));

        let remote_md5s = HashMap::new();

        let context = UploadWorkerContext {
            client: client.clone(),
            album_uri: "/api/v2/album/ABC123".to_string(),
            album_key: "ABC123".to_string(),
            series: None,
            hash_store: hash_store.clone(),
            remote_md5s: Some(Arc::new(remote_md5s)),
            remote_images: None,
            dry_run: false,
            no_cache: true,
            retry_attempts: 3,
            render_raw: false,
            skip_raw_files: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            known_hashes: None,
        };

        assert_eq!(context.album_uri, "/api/v2/album/ABC123");
        assert_eq!(context.album_key, "ABC123");
        assert!(!context.dry_run);
        assert!(context.no_cache);
        assert!(context.remote_md5s.is_some());
    }
}