force 0.2.0

Production-ready Salesforce Platform API client with REST and Bulk API 2.0 support
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
//! Token storage and management logic.
//!
//! This module provides the `TokenManager` which handles secure storage,
//! automatic refresh, and concurrent access for OAuth tokens.

use crate::auth::authenticator::Authenticator;
use crate::auth::token::AccessToken;
use crate::error::Result;
use std::sync::Arc;
use tokio::sync::{Mutex, RwLock};

/// Internal state for token management.
#[derive(Debug)]
struct TokenState {
    /// The current access token (if any).
    token: Option<Arc<AccessToken>>,
}

/// Thread-safe token manager with automatic refresh.
#[derive(Debug)]
pub struct TokenManager<A: Authenticator> {
    /// The authenticator for obtaining tokens.
    authenticator: A,

    /// Thread-safe token state.
    state: Arc<RwLock<TokenState>>,

    /// Mutex to serialize refresh operations without blocking readers.
    refresh_lock: Mutex<()>,
}

impl<A: Authenticator> TokenManager<A> {
    /// Creates a new token manager with the given authenticator.
    ///
    /// # Arguments
    ///
    /// * `authenticator` - The authenticator to use for obtaining tokens
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let manager = TokenManager::new(my_authenticator);
    /// ```
    #[must_use]
    pub fn new(authenticator: A) -> Self {
        Self {
            authenticator,
            state: Arc::new(RwLock::new(TokenState { token: None })),
            refresh_lock: Mutex::new(()),
        }
    }

    /// Helper to update the token state in a thread-safe manner, returning the most up-to-date token.
    async fn update_token_state(
        &self,
        arc_token: Arc<AccessToken>,
        is_initial_auth: bool,
    ) -> Result<Arc<AccessToken>> {
        let mut state = self.state.write().await;

        if let Some(current) = &state.token {
            if current.issued_at() > arc_token.issued_at() || Arc::ptr_eq(current, &arc_token) {
                return Ok(current.clone());
            }
            state.token = Some(arc_token.clone());
        } else if is_initial_auth {
            state.token = Some(arc_token.clone());
        } else {
            // The state was cleared, we shouldn't revive the session!
            return Err(crate::error::ForceError::Authentication(
                crate::error::AuthenticationError::InvalidToken,
            ));
        }

        Ok(arc_token)
    }

    /// Returns the currently stored token when it is at least as new as `fallback`.
    async fn latest_token_or(&self, fallback: Arc<AccessToken>) -> Arc<AccessToken> {
        let state = self.state.read().await;

        match &state.token {
            Some(current) if current.issued_at() >= fallback.issued_at() => current.clone(),
            _ => fallback,
        }
    }

    /// Returns the current access token as an Arc reference, refreshing if necessary.
    ///
    /// This is an internal method to avoid cloning the token for internal use.
    pub(crate) async fn get_token_arc(&self) -> Result<Arc<AccessToken>> {
        let (is_soft_expired, is_hard_expired_actual, current_token) =
            self.evaluate_token_state().await;

        if let Some(token) = current_token.as_ref() {
            if !is_soft_expired && !is_hard_expired_actual {
                return Ok(token.clone());
            }
        }

        if is_hard_expired_actual {
            self.handle_hard_refresh().await
        } else if let Some(valid_token) = current_token {
            self.handle_soft_refresh(valid_token).await
        } else {
            Err(crate::error::ForceError::Authentication(
                crate::error::AuthenticationError::InvalidToken,
            ))
        }
    }

    async fn evaluate_token_state(&self) -> (bool, bool, Option<Arc<AccessToken>>) {
        let state = self.state.read().await;
        if let Some(token) = &state.token {
            (
                token.is_soft_expired(),
                token.is_hard_expired(),
                Some(token.clone()),
            )
        } else {
            (false, true, None)
        }
    }

    async fn handle_hard_refresh(&self) -> Result<Arc<AccessToken>> {
        let _lock = self.refresh_lock.lock().await;

        {
            let state = self.state.read().await;
            if let Some(token) = &state.token {
                if !token.is_hard_expired() {
                    return Ok(token.clone());
                }
            }
        }

        let has_token = {
            let state = self.state.read().await;
            state.token.is_some()
        };

        let new_token = if has_token {
            self.authenticator.refresh().await?
        } else {
            self.authenticator.authenticate().await?
        };

        // âš¡ Bolt: Moving `new_token` directly into `Arc` avoids an unnecessary `.clone()` allocation
        // when transferring ownership, saving one heap allocation per token refresh/auth.
        let arc_token = Arc::new(new_token);
        self.update_token_state(arc_token, !has_token).await
    }

    async fn handle_soft_refresh(&self, valid_token: Arc<AccessToken>) -> Result<Arc<AccessToken>> {
        let Ok(_lock) = self.refresh_lock.try_lock() else {
            return Ok(self.latest_token_or(valid_token).await);
        };

        {
            let state = self.state.read().await;
            if let Some(token) = &state.token {
                if !token.is_soft_expired() && !token.is_hard_expired() {
                    return Ok(token.clone());
                }
            }
        }

        let refresh_result = self.authenticator.refresh().await;

        match refresh_result {
            Ok(new_token) => {
                // âš¡ Bolt: Moving `new_token` directly into `Arc` avoids an unnecessary `.clone()` allocation
                // when transferring ownership, saving one heap allocation per token refresh.
                let arc_token = Arc::new(new_token);
                self.update_token_state(arc_token, false).await
            }
            Err(_) => Ok(self.latest_token_or(valid_token).await),
        }
    }

    /// Returns the current access token, refreshing if necessary.
    ///
    /// This method:
    /// 1. Checks if a token exists and is still valid
    /// 2. Refreshes the token if expired
    /// 3. Authenticates if no token exists
    ///
    /// # Errors
    ///
    /// Returns an error if authentication or refresh fails.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let token = manager.token().await?;
    /// println!("Instance URL: {}", token.instance_url());
    /// ```
    pub async fn token(&self) -> Result<AccessToken> {
        let arc_token = self.get_token_arc().await?;
        Ok((*arc_token).clone())
    }

    /// Forces a token refresh regardless of expiration status.
    ///
    /// This is useful for handling 401 responses where the server has invalidated
    /// the token but the client doesn't know it yet.
    ///
    /// # Errors
    ///
    /// Returns an error if the refresh attempt fails.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// // Got a 401 response, force refresh the token
    /// let new_token = manager.force_refresh().await?;
    /// ```
    pub async fn force_refresh(&self) -> Result<AccessToken> {
        // Capture the current token's Arc pointer (if any)
        let current_arc = {
            let state = self.state.read().await;
            state.token.clone()
        };

        // Acquire refresh lock to serialize force_refresh calls
        let _lock = self.refresh_lock.lock().await;

        // Double check: Did another thread already refresh the token while we were waiting?
        {
            let state = self.state.read().await;
            if let Some(token) = &state.token {
                // If the token in state is a different allocation (Arc::ptr_eq is false) than what we captured,
                // another thread just refreshed it. Return that one!
                let is_same = match &current_arc {
                    Some(arc) => Arc::ptr_eq(token, arc),
                    None => false,
                };
                if !is_same {
                    return Ok((*token.clone()).clone());
                }
            }
        }

        let has_token = {
            let state = self.state.read().await;
            state.token.is_some()
        };

        let new_token = if has_token {
            self.authenticator.refresh().await?
        } else {
            self.authenticator.authenticate().await?
        };

        // âš¡ Bolt: Moving `new_token` directly into `Arc` avoids an unnecessary `.clone()` allocation
        // when transferring ownership, saving one heap allocation per force refresh.
        let arc_token = Arc::new(new_token);
        let final_token = self.update_token_state(arc_token, !has_token).await?;
        Ok((*final_token).clone())
    }

    /// Clears the current token, forcing re-authentication on next access.
    ///
    /// This is useful for explicit logout or when you know the token is invalid.
    pub async fn clear(&self) {
        let mut state = self.state.write().await;
        state.token = None;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::auth::authenticator::Authenticator;
    use crate::test_support::Must;
    use async_trait::async_trait;
    use chrono::{Duration, Utc};
    use std::sync::Arc as StdArc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    // Mock authenticator for testing TokenManager
    #[derive(Debug)]
    struct MockAuthenticator {
        auth_count: StdArc<AtomicUsize>,
        refresh_count: StdArc<AtomicUsize>,
        should_fail: bool,
        refresh_delay: Option<std::time::Duration>,
    }

    impl MockAuthenticator {
        fn new() -> Self {
            Self {
                auth_count: StdArc::new(AtomicUsize::new(0)),
                refresh_count: StdArc::new(AtomicUsize::new(0)),
                should_fail: false,
                refresh_delay: None,
            }
        }

        fn with_failure() -> Self {
            Self {
                auth_count: StdArc::new(AtomicUsize::new(0)),
                refresh_count: StdArc::new(AtomicUsize::new(0)),
                should_fail: true,
                refresh_delay: None,
            }
        }

        fn with_delay(mut self, delay: std::time::Duration) -> Self {
            self.refresh_delay = Some(delay);
            self
        }

        fn auth_count(&self) -> usize {
            self.auth_count.load(Ordering::SeqCst)
        }

        fn refresh_count(&self) -> usize {
            self.refresh_count.load(Ordering::SeqCst)
        }
    }

    #[async_trait]
    impl Authenticator for MockAuthenticator {
        async fn authenticate(&self) -> Result<AccessToken> {
            if let Some(delay) = self.refresh_delay {
                tokio::time::sleep(delay).await;
            }

            self.auth_count.fetch_add(1, Ordering::SeqCst);

            if self.should_fail {
                return Err(crate::error::ForceError::Authentication(
                    crate::error::AuthenticationError::InvalidCredentials(
                        "mock auth failed".to_string(),
                    ),
                ));
            }

            Ok(AccessToken::new(
                format!("auth_token_{}", self.auth_count()),
                "https://test.salesforce.com".to_string(),
                Some(Utc::now() + Duration::hours(2)),
            ))
        }

        async fn refresh(&self) -> Result<AccessToken> {
            if let Some(delay) = self.refresh_delay {
                tokio::time::sleep(delay).await;
            }

            self.refresh_count.fetch_add(1, Ordering::SeqCst);

            if self.should_fail {
                return Err(crate::error::ForceError::Authentication(
                    crate::error::AuthenticationError::TokenRefreshFailed(
                        "mock refresh failed".to_string(),
                    ),
                ));
            }

            Ok(AccessToken::new(
                format!("refresh_token_{}", self.refresh_count()),
                "https://test.salesforce.com".to_string(),
                Some(Utc::now() + Duration::hours(2)),
            ))
        }
    }

    #[tokio::test]
    async fn test_token_manager_initial_auth() {
        let auth = MockAuthenticator::new();
        let manager = TokenManager::new(auth);

        let token = manager.token().await.must();
        assert_eq!(token.as_str(), "auth_token_1");
        assert_eq!(manager.authenticator.auth_count(), 1);
        assert_eq!(manager.authenticator.refresh_count(), 0);
    }

    #[tokio::test]
    async fn test_token_manager_reuses_valid_token() {
        let auth = MockAuthenticator::new();
        let manager = TokenManager::new(auth);

        // First call authenticates
        let token1 = manager.token().await.must();
        assert_eq!(manager.authenticator.auth_count(), 1);

        // Second call reuses token
        let token2 = manager.token().await.must();
        assert_eq!(manager.authenticator.auth_count(), 1); // No new auth
        assert_eq!(token1.as_str(), token2.as_str());
    }

    #[tokio::test]
    async fn test_token_manager_refreshes_expired_token() {
        let auth = MockAuthenticator::new();
        let manager = TokenManager::new(auth);

        // Get initial token
        let _token1 = manager.token().await.must();
        assert_eq!(manager.authenticator.auth_count(), 1);

        // Manually expire the token
        {
            let mut state = manager.state.write().await;
            if let Some(token) = &mut state.token {
                // Create an expired token
                *token = Arc::new(AccessToken::new(
                    "expired_token".to_string(),
                    "https://test.salesforce.com".to_string(),
                    Some(Utc::now() - Duration::hours(1)),
                ));
            }
        }

        // Next call should refresh
        let token2 = manager.token().await.must();
        assert_eq!(manager.authenticator.auth_count(), 1); // No new auth
        assert_eq!(manager.authenticator.refresh_count(), 1); // Refreshed once
        assert_eq!(token2.as_str(), "refresh_token_1");
    }

    #[tokio::test]
    async fn test_token_manager_force_refresh() {
        let auth = MockAuthenticator::new();
        let manager = TokenManager::new(auth);

        // Get initial token
        let token1 = manager.token().await.must();
        assert_eq!(token1.as_str(), "auth_token_1");

        // Force refresh even though token is valid
        let token2 = manager.force_refresh().await.must();
        assert_eq!(token2.as_str(), "refresh_token_1");
        assert_eq!(manager.authenticator.refresh_count(), 1);
    }

    #[tokio::test]
    async fn test_token_manager_clear() {
        let auth = MockAuthenticator::new();
        let manager = TokenManager::new(auth);

        // Get initial token
        let _token1 = manager.token().await.must();
        assert_eq!(manager.authenticator.auth_count(), 1);

        // Clear the token
        manager.clear().await;

        // Next call should authenticate again
        let _token2 = manager.token().await.must();
        assert_eq!(manager.authenticator.auth_count(), 2); // Auth called again
    }

    #[tokio::test]
    async fn test_token_manager_concurrent_access() {
        let auth = MockAuthenticator::new();
        let manager = StdArc::new(TokenManager::new(auth));

        // Spawn multiple tasks trying to get tokens concurrently
        let mut handles = vec![];
        for _ in 0..10 {
            let manager_clone = StdArc::clone(&manager);
            let handle = tokio::spawn(async move { manager_clone.token().await });
            handles.push(handle);
        }

        // Wait for all tasks to complete
        for handle in handles {
            let result = handle.await.must();
            assert!(result.is_ok());
        }

        // Should only authenticate once despite concurrent requests
        assert_eq!(manager.authenticator.auth_count(), 1);
    }

    #[tokio::test]
    async fn test_token_manager_auth_failure() {
        let auth = MockAuthenticator::with_failure();
        let manager = TokenManager::new(auth);

        let result = manager.token().await;

        if let Err(crate::error::ForceError::Authentication(
            crate::error::AuthenticationError::InvalidCredentials(msg),
        )) = result
        {
            assert_eq!(msg, "mock auth failed");
        } else {
            panic!("Expected InvalidCredentials error");
        }
    }

    #[tokio::test]
    async fn test_token_manager_refresh_failure() {
        let auth = MockAuthenticator::with_failure();
        let manager = TokenManager::new(auth);

        // Manually set an expired token
        {
            let mut state = manager.state.write().await;
            state.token = Some(Arc::new(AccessToken::new(
                "expired".to_string(),
                "https://test.salesforce.com".to_string(),
                Some(Utc::now() - Duration::hours(1)),
            )));
        }

        let result = manager.token().await;

        if let Err(crate::error::ForceError::Authentication(
            crate::error::AuthenticationError::TokenRefreshFailed(msg),
        )) = result
        {
            assert_eq!(msg, "mock refresh failed");
        } else {
            panic!("Expected TokenRefreshFailed error");
        }
    }

    #[tokio::test]
    async fn test_token_manager_concurrent_refresh_only_one_request() {
        let auth = MockAuthenticator::new().with_delay(std::time::Duration::from_millis(50));
        let manager = StdArc::new(TokenManager::new(auth));

        // 1. Initial auth to set a token
        let _ = manager.token().await.must();
        assert_eq!(manager.authenticator.auth_count(), 1);

        // 2. Manually expire the token
        {
            let mut state = manager.state.write().await;
            if let Some(token) = &mut state.token {
                *token = Arc::new(AccessToken::new(
                    "expired_token".to_string(),
                    "https://test.salesforce.com".to_string(),
                    Some(Utc::now() - Duration::hours(1)),
                ));
            }
        }

        // 3. Spawn concurrent tasks requesting token
        let mut handles = vec![];
        for _ in 0..50 {
            let manager_clone = StdArc::clone(&manager);
            handles.push(tokio::spawn(
                async move { manager_clone.token().await.must() },
            ));
        }

        // 4. Verify results
        for handle in handles {
            let token = handle.await.must();
            // Should get the refreshed token
            assert_eq!(token.as_str(), "refresh_token_1");
        }

        // 5. Assert refresh was called EXACTLY once
        assert_eq!(
            manager.authenticator.refresh_count(),
            1,
            "Should have refreshed exactly once despite concurrent load"
        );
        // Auth count should remain 1 (from initial setup)
        assert_eq!(manager.authenticator.auth_count(), 1);
    }

    #[tokio::test]
    async fn test_token_manager_force_refresh_protects_against_overwrite() {
        let auth = MockAuthenticator::new();
        let manager = TokenManager::new(auth);

        // 1. Manually set a token with a FUTURE issued_at to simulate a concurrent refresh finishing later
        let future_ts = (Utc::now() + Duration::hours(1)).timestamp_millis();
        let response = crate::auth::token::TokenResponse {
            access_token: "future_token".to_string(),
            instance_url: "https://test.salesforce.com".to_string(),
            token_type: "Bearer".to_string(),
            issued_at: future_ts.to_string(),
            signature: String::new(),
            expires_in: None,
            refresh_token: None,
        };
        let future_token = AccessToken::from_response(response);

        {
            let mut state = manager.state.write().await;
            state.token = Some(StdArc::new(future_token));
        }

        // 2. Call force_refresh
        // The mock authenticator returns a token with `issued_at` roughly NOW (older than future_token).
        let result = manager.force_refresh().await.must();

        // 3. Assert that the result matches the FUTURE token, proving we kept the newer one
        assert_eq!(result.as_str(), "future_token");

        // 4. Verify state also has the future token
        let state_token = manager.token().await.must();
        assert_eq!(state_token.as_str(), "future_token");
    }

    #[tokio::test]
    async fn test_token_manager_hard_refresh_protects_against_overwrite() {
        let auth = MockAuthenticator::new().with_delay(std::time::Duration::from_millis(50));
        let manager = StdArc::new(TokenManager::new(auth));

        // Let the manager start fetching the initial token (hard refresh path)
        let manager_clone = manager.clone();
        let handle = tokio::spawn(async move { manager_clone.token().await.must() });

        // Sleep to ensure the task has acquired the refresh lock and is awaiting `authenticator.authenticate()`
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;

        // Concurrently inject a FUTURE token
        let future_ts = (Utc::now() + Duration::hours(1)).timestamp_millis();
        let response = crate::auth::token::TokenResponse {
            access_token: "future_token".to_string(),
            instance_url: "https://test.salesforce.com".to_string(),
            token_type: "Bearer".to_string(),
            issued_at: future_ts.to_string(),
            signature: String::new(),
            expires_in: None,
            refresh_token: None,
        };
        let future_token = AccessToken::from_response(response);

        {
            let mut state = manager.state.write().await;
            state.token = Some(StdArc::new(future_token));
        }

        // Wait for the task to finish
        let result = handle.await.must();

        // The manager's task should have realized a newer token was injected and returned it
        // instead of its own "newly generated" one.
        assert_eq!(result.as_str(), "future_token");

        // Verify state also has the future token
        let final_token = manager.token().await.must();
        assert_eq!(final_token.as_str(), "future_token");
    }

    #[tokio::test]
    async fn test_token_manager_soft_refresh_protects_against_overwrite() {
        let auth = MockAuthenticator::new().with_delay(std::time::Duration::from_millis(50));
        let manager = StdArc::new(TokenManager::new(auth));

        // 1. Manually inject a SOFT EXPIRED token
        // It expires in 30 seconds, which is less than the 60s buffer, so it's soft expired.
        let soft_token = AccessToken::new(
            "soft_token".to_string(),
            "https://test.salesforce.com".to_string(),
            Some(Utc::now() + Duration::seconds(30)),
        );

        {
            let mut state = manager.state.write().await;
            state.token = Some(StdArc::new(soft_token));
        }

        // 2. Trigger token() which will see it's soft expired and call refresh(), taking 50ms
        let manager_clone = manager.clone();
        let handle = tokio::spawn(async move { manager_clone.token().await.must() });

        // 3. Wait 10ms to ensure the spawn starts and begins sleeping
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;

        // 4. Concurrently inject a FUTURE token
        let future_ts = (Utc::now() + Duration::hours(1)).timestamp_millis();
        let future_response = crate::auth::token::TokenResponse {
            access_token: "future_token".to_string(),
            instance_url: "https://test.salesforce.com".to_string(),
            token_type: "Bearer".to_string(),
            issued_at: future_ts.to_string(),
            signature: String::new(),
            expires_in: None,
            refresh_token: None,
        };
        let future_token = AccessToken::from_response(future_response);

        {
            let mut state = manager.state.write().await;
            state.token = Some(StdArc::new(future_token));
        }

        // 5. Await result
        let result = handle.await.must();

        // Should return the injected future token, not the newly refreshed one
        assert_eq!(result.as_str(), "future_token");

        // Verify state also has the future token
        let final_token = manager.token().await.must();
        assert_eq!(final_token.as_str(), "future_token");
    }

    #[tokio::test]
    async fn test_token_manager_update_token_state_cleared_token_rejects_non_initial() {
        // Tests the branch at line 68-73: state cleared, is_initial_auth=false -> InvalidToken
        // We can test this by calling update_token_state directly with is_initial_auth=false
        let auth = MockAuthenticator::new();
        let manager = TokenManager::new(auth);

        // 1. Set a valid token
        let _token1 = manager.token().await.must();

        // 2. Clear it
        manager.clear().await;

        // 3. Directly call update_token_state with is_initial_auth=false
        // Since the token was cleared, update_token_state should return InvalidToken
        let dummy_token = AccessToken::new(
            "dummy".to_string(),
            "https://test.salesforce.com".to_string(),
            Some(Utc::now() + Duration::hours(1)),
        );
        let result = manager
            .update_token_state(StdArc::new(dummy_token), false)
            .await;
        assert!(
            matches!(
                result,
                Err(crate::error::ForceError::Authentication(
                    crate::error::AuthenticationError::InvalidToken
                ))
            ),
            "Expected InvalidToken error after clearing and update_token_state, got: {result:?}"
        );
    }

    #[tokio::test]
    async fn test_token_manager_soft_expired_refresh_failure_returns_valid_token() {
        // Tests the branch at line 170-174: soft refresh fails, return old valid token
        let auth = MockAuthenticator::with_failure();
        let manager = TokenManager::new(auth);

        // Manually inject a SOFT expired token (still valid but should be refreshed)
        let soft_token = AccessToken::new(
            "still_valid_token".to_string(),
            "https://test.salesforce.com".to_string(),
            Some(Utc::now() + Duration::seconds(30)), // 30s < 60s buffer = soft expired
        );

        {
            let mut state = manager.state.write().await;
            state.token = Some(StdArc::new(soft_token));
        }

        // token() should try to refresh (and fail), but return the old valid token
        let token = manager.token().await.must();
        assert_eq!(token.as_str(), "still_valid_token");
    }

    #[tokio::test]
    async fn test_token_manager_soft_expired_concurrent_returns_latest_token() {
        // Tests the branch at line 176-179: someone else is refreshing, return latest token
        let auth = MockAuthenticator::new().with_delay(std::time::Duration::from_millis(200));
        let manager = StdArc::new(TokenManager::new(auth));

        // Manually inject a SOFT expired token
        let soft_token = AccessToken::new(
            "soft_valid_token".to_string(),
            "https://test.salesforce.com".to_string(),
            Some(Utc::now() + Duration::seconds(30)),
        );

        {
            let mut state = manager.state.write().await;
            state.token = Some(StdArc::new(soft_token));
        }

        // Spawn first task that will acquire refresh lock and sleep 200ms
        let manager_clone = manager.clone();
        let handle1 = tokio::spawn(async move { manager_clone.token().await.must() });

        // Wait a bit so the first task acquires the lock
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;

        // Second task should fail to acquire lock and return the latest token
        let token2 = manager.token().await.must();
        assert_eq!(token2.as_str(), "soft_valid_token");

        // First task completes with the refreshed token
        let _token1 = handle1.await.must();
    }

    #[tokio::test]
    async fn test_token_manager_equality_overwrites() {
        // We need the mock to generate a token with a specific timestamp to ensure equality.
        // Wait, MockAuthenticator just uses `Utc::now()`.
        // If we inject a token that has a timestamp slightly in the past, the new token will be newer (>),
        // which tests the overwrite. But we specifically want to test EQUALITY (==).
        // Since `TokenResponse` parses `issued_at` exactly, let's force the authenticator to use
        // a known timestamp.

        // Actually, we can just let `force_refresh` return its token,
        // and before the `force_refresh` writes to state, we inject a token with the EXACT SAME TIMESTAMP.
        // But doing it concurrently is hard because we don't control the exact MS the Mock uses.
        // Let's create a special `EqualityAuthenticator` for this specific test.
        #[derive(Debug)]
        struct EqAuth(i64);
        #[async_trait]
        impl Authenticator for EqAuth {
            async fn authenticate(&self) -> Result<AccessToken> {
                let response = crate::auth::token::TokenResponse {
                    access_token: "new_token".to_string(),
                    instance_url: "https://test.salesforce.com".to_string(),
                    token_type: "Bearer".to_string(),
                    issued_at: self.0.to_string(),
                    signature: String::new(),
                    expires_in: None,
                    refresh_token: None,
                };
                Ok(AccessToken::from_response(response))
            }
            async fn refresh(&self) -> Result<AccessToken> {
                self.authenticate().await
            }
        }

        let fixed_ts = Utc::now().timestamp_millis();
        let eq_auth = EqAuth(fixed_ts);
        let eq_manager = TokenManager::new(eq_auth);

        // Inject a token with the EXACT SAME timestamp
        let response = crate::auth::token::TokenResponse {
            access_token: "old_token".to_string(),
            instance_url: "https://test.salesforce.com".to_string(),
            token_type: "Bearer".to_string(),
            issued_at: fixed_ts.to_string(),
            signature: String::new(),
            expires_in: None,
            refresh_token: None,
        };
        let old_token = AccessToken::from_response(response);

        {
            let mut state = eq_manager.state.write().await;
            state.token = Some(StdArc::new(old_token));
        }

        // Call force_refresh. The new token will have the same `issued_at`.
        // Since `old.issued_at >= new.issued_at` is true, it SHOULD NOT overwrite
        // and return the old token ("old_token").
        let result = eq_manager.force_refresh().await.must();
        assert_eq!(
            result.as_str(),
            "new_token",
            "Equality should trigger an overwrite in force_refresh"
        );

        // Now let's test equality overwrite for hard expiration (line 114)
        let hard_eq_manager = TokenManager::new(EqAuth(fixed_ts));
        // Inject hard expired token with same timestamp
        // The mock will return a token with this exact timestamp
        let response = crate::auth::token::TokenResponse {
            access_token: "hard_old_token".to_string(),
            instance_url: "https://test.salesforce.com".to_string(),
            token_type: "Bearer".to_string(),
            issued_at: fixed_ts.to_string(),
            signature: String::new(),
            expires_in: Some(0), // Hard expired immediately
            refresh_token: None,
        };
        let hard_old_token = AccessToken::from_response(response);
        {
            let mut state = hard_eq_manager.state.write().await;
            state.token = Some(StdArc::new(hard_old_token));
        }

        let result = hard_eq_manager.token().await.must();
        assert_eq!(
            result.as_str(),
            "new_token",
            "Equality should trigger an overwrite in hard refresh"
        );

        // Now let's test equality overwrite for soft expiration (line 146)
        let soft_eq_manager = TokenManager::new(EqAuth(fixed_ts));
        // Inject soft expired token with same timestamp
        let response = crate::auth::token::TokenResponse {
            access_token: "soft_old_token".to_string(),
            instance_url: "https://test.salesforce.com".to_string(),
            token_type: "Bearer".to_string(),
            issued_at: fixed_ts.to_string(),
            signature: String::new(),
            expires_in: Some(30), // Expires in 30s -> Soft expired
            refresh_token: None,
        };
        let soft_old_token = AccessToken::from_response(response);
        {
            let mut state = soft_eq_manager.state.write().await;
            state.token = Some(StdArc::new(soft_old_token));
        }

        let result = soft_eq_manager.token().await.must();
        assert_eq!(
            result.as_str(),
            "new_token",
            "Equality should trigger an overwrite in soft refresh"
        );
    }

    #[tokio::test]
    async fn test_token_manager_force_refresh_stampede() {
        let auth = MockAuthenticator::new().with_delay(std::time::Duration::from_millis(50));
        let manager = StdArc::new(TokenManager::new(auth));

        // Let the manager fetch the initial token
        let _ = manager.token().await.must();

        // Spawn 100 concurrent tasks calling force_refresh
        let mut handles = Vec::new();
        for _ in 0..100 {
            let manager_clone = manager.clone();
            handles.push(tokio::spawn(async move {
                manager_clone.force_refresh().await.must()
            }));
        }

        for handle in handles {
            let _ = handle.await.must();
        }

        // The initial token() call triggers 1 auth.
        // The 100 force_refresh() calls should trigger EXACTLY 1 refresh, not 100.
        let refresh_count = manager.authenticator.refresh_count();
        assert_eq!(
            refresh_count, 1,
            "👺 Havoc: force_refresh triggered a stampede!"
        );
    }
}