tuitbot-core 0.1.47

Core library for Tuitbot autonomous X growth assistant
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
//! Safety module for rate limiting and duplicate prevention.
//!
//! Provides the `SafetyGuard` as the primary pre-flight check interface
//! for all automation loops. Combines rate limiting with deduplication
//! to prevent API abuse and duplicate content.

pub mod dedup;
pub mod qa;
pub mod redact;

use crate::error::StorageError;
use crate::storage::rate_limits;
use crate::storage::{author_interactions, DbPool};

pub use dedup::DedupChecker;

/// Wraps rate limit database operations with a clean API.
pub struct RateLimiter {
    pool: DbPool,
}

impl RateLimiter {
    /// Create a new rate limiter backed by the given database pool.
    pub fn new(pool: DbPool) -> Self {
        Self { pool }
    }

    /// Check if a reply action is allowed under the current rate limit.
    pub async fn can_reply(&self) -> Result<bool, StorageError> {
        rate_limits::check_rate_limit(&self.pool, "reply").await
    }

    /// Check if a tweet action is allowed under the current rate limit.
    pub async fn can_tweet(&self) -> Result<bool, StorageError> {
        rate_limits::check_rate_limit(&self.pool, "tweet").await
    }

    /// Check if a thread action is allowed under the current rate limit.
    pub async fn can_thread(&self) -> Result<bool, StorageError> {
        rate_limits::check_rate_limit(&self.pool, "thread").await
    }

    /// Check if a search action is allowed under the current rate limit.
    pub async fn can_search(&self) -> Result<bool, StorageError> {
        rate_limits::check_rate_limit(&self.pool, "search").await
    }

    /// Record a successful reply action (increments counter).
    pub async fn record_reply(&self) -> Result<(), StorageError> {
        rate_limits::increment_rate_limit(&self.pool, "reply").await
    }

    /// Record a successful tweet action (increments counter).
    pub async fn record_tweet(&self) -> Result<(), StorageError> {
        rate_limits::increment_rate_limit(&self.pool, "tweet").await
    }

    /// Record a successful thread action (increments counter).
    pub async fn record_thread(&self) -> Result<(), StorageError> {
        rate_limits::increment_rate_limit(&self.pool, "thread").await
    }

    /// Record a successful search action (increments counter).
    pub async fn record_search(&self) -> Result<(), StorageError> {
        rate_limits::increment_rate_limit(&self.pool, "search").await
    }

    /// Atomically check and claim a rate limit slot.
    ///
    /// Returns `Ok(true)` if permitted (counter incremented),
    /// `Ok(false)` if the rate limit is reached.
    /// Preferred over separate check + record for posting actions.
    pub async fn acquire_posting_permit(&self, action_type: &str) -> Result<bool, StorageError> {
        rate_limits::check_and_increment_rate_limit(&self.pool, action_type).await
    }
}

/// Reason an action was denied by the safety guard.
#[derive(Debug, Clone, PartialEq)]
pub enum DenialReason {
    /// Action blocked by rate limiting.
    RateLimited {
        /// Which action type hit the limit.
        action_type: String,
        /// Current request count.
        current: i64,
        /// Maximum allowed requests.
        max: i64,
    },
    /// Already replied to this tweet.
    AlreadyReplied {
        /// The tweet ID that was already replied to.
        tweet_id: String,
    },
    /// Proposed reply is too similar to a recent reply.
    SimilarPhrasing,
    /// Reply contains a banned phrase.
    BannedPhrase {
        /// The banned phrase that was found.
        phrase: String,
    },
    /// Already reached the per-author daily reply limit.
    AuthorLimitReached,
    /// Replying to own tweet.
    SelfReply,
}

impl std::fmt::Display for DenialReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::RateLimited {
                action_type,
                current,
                max,
            } => write!(f, "Rate limited: {action_type} ({current}/{max})"),
            Self::AlreadyReplied { tweet_id } => {
                write!(f, "Already replied to tweet {tweet_id}")
            }
            Self::SimilarPhrasing => {
                write!(f, "Reply phrasing too similar to recent replies")
            }
            Self::BannedPhrase { phrase } => {
                write!(f, "Reply contains banned phrase: \"{phrase}\"")
            }
            Self::AuthorLimitReached => {
                write!(f, "Already reached daily reply limit for this author")
            }
            Self::SelfReply => {
                write!(f, "Cannot reply to own tweets")
            }
        }
    }
}

/// Check if any banned phrase appears in the text (case-insensitive).
///
/// Returns the first matching banned phrase, or `None` if clean.
pub fn contains_banned_phrase(text: &str, banned: &[String]) -> Option<String> {
    let text_lower = text.to_lowercase();
    for phrase in banned {
        if text_lower.contains(&phrase.to_lowercase()) {
            return Some(phrase.clone());
        }
    }
    None
}

/// Check if the tweet author is the bot's own user ID.
pub fn is_self_reply(tweet_author_id: &str, own_user_id: &str) -> bool {
    !tweet_author_id.is_empty() && !own_user_id.is_empty() && tweet_author_id == own_user_id
}

/// Combined safety guard for all automation loops.
///
/// Provides pre-flight checks that combine rate limiting with deduplication.
/// All automation loops should call `SafetyGuard` methods before taking actions.
pub struct SafetyGuard {
    rate_limiter: RateLimiter,
    dedup_checker: DedupChecker,
    pool: DbPool,
}

impl SafetyGuard {
    /// Create a new safety guard backed by the given database pool.
    pub fn new(pool: DbPool) -> Self {
        Self {
            rate_limiter: RateLimiter::new(pool.clone()),
            dedup_checker: DedupChecker::new(pool.clone()),
            pool,
        }
    }

    /// Check whether replying to a tweet is permitted.
    ///
    /// Checks rate limits, exact dedup, and optionally phrasing similarity.
    /// Returns `Ok(Ok(()))` if allowed, `Ok(Err(DenialReason))` if blocked,
    /// or `Err(StorageError)` on infrastructure failure.
    pub async fn can_reply_to(
        &self,
        tweet_id: &str,
        proposed_reply: Option<&str>,
    ) -> Result<Result<(), DenialReason>, StorageError> {
        // Check rate limit
        if !self.rate_limiter.can_reply().await? {
            let limits = rate_limits::get_all_rate_limits(&self.rate_limiter.pool).await?;
            let reply_limit = limits.iter().find(|l| l.action_type == "reply");
            let (current, max) = reply_limit
                .map(|l| (l.request_count, l.max_requests))
                .unwrap_or((0, 0));

            tracing::debug!(
                action = "reply",
                current,
                max,
                "Action denied: rate limited"
            );

            return Ok(Err(DenialReason::RateLimited {
                action_type: "reply".to_string(),
                current,
                max,
            }));
        }

        // Check exact dedup
        if self.dedup_checker.has_replied_to(tweet_id).await? {
            tracing::debug!(tweet_id, "Action denied: already replied");
            return Ok(Err(DenialReason::AlreadyReplied {
                tweet_id: tweet_id.to_string(),
            }));
        }

        // Check phrasing similarity
        if let Some(reply_text) = proposed_reply {
            if self
                .dedup_checker
                .is_phrasing_similar(reply_text, 20)
                .await?
            {
                tracing::debug!("Action denied: similar phrasing");
                return Ok(Err(DenialReason::SimilarPhrasing));
            }
        }

        Ok(Ok(()))
    }

    /// Check whether posting an original tweet is permitted.
    ///
    /// Only checks rate limits (no dedup for original tweets).
    pub async fn can_post_tweet(&self) -> Result<Result<(), DenialReason>, StorageError> {
        if !self.rate_limiter.can_tweet().await? {
            let limits = rate_limits::get_all_rate_limits(&self.rate_limiter.pool).await?;
            let tweet_limit = limits.iter().find(|l| l.action_type == "tweet");
            let (current, max) = tweet_limit
                .map(|l| (l.request_count, l.max_requests))
                .unwrap_or((0, 0));

            tracing::debug!(
                action = "tweet",
                current,
                max,
                "Action denied: rate limited"
            );

            return Ok(Err(DenialReason::RateLimited {
                action_type: "tweet".to_string(),
                current,
                max,
            }));
        }

        Ok(Ok(()))
    }

    /// Check whether posting a thread is permitted.
    ///
    /// Only checks rate limits (no dedup for threads).
    pub async fn can_post_thread(&self) -> Result<Result<(), DenialReason>, StorageError> {
        if !self.rate_limiter.can_thread().await? {
            let limits = rate_limits::get_all_rate_limits(&self.rate_limiter.pool).await?;
            let thread_limit = limits.iter().find(|l| l.action_type == "thread");
            let (current, max) = thread_limit
                .map(|l| (l.request_count, l.max_requests))
                .unwrap_or((0, 0));

            tracing::debug!(
                action = "thread",
                current,
                max,
                "Action denied: rate limited"
            );

            return Ok(Err(DenialReason::RateLimited {
                action_type: "thread".to_string(),
                current,
                max,
            }));
        }

        Ok(Ok(()))
    }

    /// Check if replying to this author is permitted (per-author daily limit).
    pub async fn check_author_limit(
        &self,
        author_id: &str,
        max_per_day: u32,
    ) -> Result<Result<(), DenialReason>, StorageError> {
        let count =
            author_interactions::get_author_reply_count_today(&self.pool, author_id).await?;
        if count >= max_per_day as i64 {
            tracing::debug!(
                author_id,
                count,
                max = max_per_day,
                "Action denied: author daily limit reached"
            );
            return Ok(Err(DenialReason::AuthorLimitReached));
        }
        Ok(Ok(()))
    }

    /// Check if a generated reply contains a banned phrase.
    pub fn check_banned_phrases(reply_text: &str, banned: &[String]) -> Result<(), DenialReason> {
        if let Some(phrase) = contains_banned_phrase(reply_text, banned) {
            tracing::debug!(phrase = %phrase, "Action denied: banned phrase");
            return Err(DenialReason::BannedPhrase { phrase });
        }
        Ok(())
    }

    /// Record a reply for an author interaction.
    pub async fn record_author_interaction(
        &self,
        author_id: &str,
        author_username: &str,
    ) -> Result<(), StorageError> {
        author_interactions::increment_author_interaction(&self.pool, author_id, author_username)
            .await
    }

    /// Record a successful reply action.
    pub async fn record_reply(&self) -> Result<(), StorageError> {
        self.rate_limiter.record_reply().await
    }

    /// Record a successful tweet action.
    pub async fn record_tweet(&self) -> Result<(), StorageError> {
        self.rate_limiter.record_tweet().await
    }

    /// Record a successful thread action.
    pub async fn record_thread(&self) -> Result<(), StorageError> {
        self.rate_limiter.record_thread().await
    }

    /// Get a reference to the underlying rate limiter.
    pub fn rate_limiter(&self) -> &RateLimiter {
        &self.rate_limiter
    }

    /// Get a reference to the underlying dedup checker.
    pub fn dedup_checker(&self) -> &DedupChecker {
        &self.dedup_checker
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{IntervalsConfig, LimitsConfig};
    use crate::storage::init_test_db;
    use crate::storage::replies::{insert_reply, ReplySent};

    fn test_limits() -> LimitsConfig {
        LimitsConfig {
            max_replies_per_day: 3,
            max_tweets_per_day: 2,
            max_threads_per_week: 1,
            min_action_delay_seconds: 30,
            max_action_delay_seconds: 120,
            max_replies_per_author_per_day: 1,
            banned_phrases: vec!["check out".to_string(), "you should try".to_string()],
            product_mention_ratio: 0.2,
        }
    }

    fn test_intervals() -> IntervalsConfig {
        IntervalsConfig {
            mentions_check_seconds: 300,
            discovery_search_seconds: 600,
            content_post_window_seconds: 14400,
            thread_interval_seconds: 604800,
        }
    }

    async fn setup_guard() -> (DbPool, SafetyGuard) {
        let pool = init_test_db().await.expect("init db");
        rate_limits::init_rate_limits(&pool, &test_limits(), &test_intervals())
            .await
            .expect("init rate limits");
        let guard = SafetyGuard::new(pool.clone());
        (pool, guard)
    }

    fn sample_reply(target_id: &str, content: &str) -> ReplySent {
        ReplySent {
            id: 0,
            target_tweet_id: target_id.to_string(),
            reply_tweet_id: Some("r_123".to_string()),
            reply_content: content.to_string(),
            llm_provider: None,
            llm_model: None,
            created_at: chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string(),
            status: "sent".to_string(),
            error_message: None,
        }
    }

    #[tokio::test]
    async fn rate_limiter_can_reply_and_record() {
        let pool = init_test_db().await.expect("init db");
        rate_limits::init_rate_limits(&pool, &test_limits(), &test_intervals())
            .await
            .expect("init");

        let limiter = RateLimiter::new(pool);

        assert!(limiter.can_reply().await.expect("check"));
        limiter.record_reply().await.expect("record");
        limiter.record_reply().await.expect("record");
        limiter.record_reply().await.expect("record");
        assert!(!limiter.can_reply().await.expect("check"));
    }

    #[tokio::test]
    async fn rate_limiter_acquire_posting_permit() {
        let pool = init_test_db().await.expect("init db");
        rate_limits::init_rate_limits(&pool, &test_limits(), &test_intervals())
            .await
            .expect("init");

        let limiter = RateLimiter::new(pool);

        assert!(limiter.acquire_posting_permit("tweet").await.expect("1"));
        assert!(limiter.acquire_posting_permit("tweet").await.expect("2"));
        assert!(!limiter.acquire_posting_permit("tweet").await.expect("3"));
    }

    #[tokio::test]
    async fn safety_guard_allows_new_reply() {
        let (_pool, guard) = setup_guard().await;

        let result = guard.can_reply_to("tweet_1", None).await.expect("check");
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn safety_guard_blocks_already_replied() {
        let (pool, guard) = setup_guard().await;

        let reply = sample_reply("tweet_1", "Some reply content");
        insert_reply(&pool, &reply).await.expect("insert");

        let result = guard.can_reply_to("tweet_1", None).await.expect("check");
        assert_eq!(
            result,
            Err(DenialReason::AlreadyReplied {
                tweet_id: "tweet_1".to_string()
            })
        );
    }

    #[tokio::test]
    async fn safety_guard_blocks_rate_limited() {
        let (_pool, guard) = setup_guard().await;

        // Exhaust the reply limit (max = 3)
        for _ in 0..3 {
            guard.record_reply().await.expect("record");
        }

        let result = guard.can_reply_to("tweet_new", None).await.expect("check");
        match result {
            Err(DenialReason::RateLimited {
                action_type,
                current,
                max,
            }) => {
                assert_eq!(action_type, "reply");
                assert_eq!(current, 3);
                assert_eq!(max, 3);
            }
            other => panic!("expected RateLimited, got: {other:?}"),
        }
    }

    #[tokio::test]
    async fn safety_guard_blocks_similar_phrasing() {
        let (pool, guard) = setup_guard().await;

        let reply = sample_reply(
            "tweet_1",
            "This is a great tool for developers and engineers to use daily",
        );
        insert_reply(&pool, &reply).await.expect("insert");

        let result = guard
            .can_reply_to(
                "tweet_2",
                Some("This is a great tool for developers and engineers to use often"),
            )
            .await
            .expect("check");

        assert_eq!(result, Err(DenialReason::SimilarPhrasing));
    }

    #[tokio::test]
    async fn safety_guard_allows_different_phrasing() {
        let (pool, guard) = setup_guard().await;

        let reply = sample_reply(
            "tweet_1",
            "This is a great tool for developers and engineers to use daily",
        );
        insert_reply(&pool, &reply).await.expect("insert");

        let result = guard
            .can_reply_to(
                "tweet_2",
                Some("I love cooking pasta with fresh basil and tomatoes every day"),
            )
            .await
            .expect("check");

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn safety_guard_can_post_tweet_allowed() {
        let (_pool, guard) = setup_guard().await;

        let result = guard.can_post_tweet().await.expect("check");
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn safety_guard_can_post_tweet_blocked() {
        let (_pool, guard) = setup_guard().await;

        // Exhaust tweet limit (max = 2)
        guard.record_tweet().await.expect("record");
        guard.record_tweet().await.expect("record");

        let result = guard.can_post_tweet().await.expect("check");
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn safety_guard_can_post_thread_allowed() {
        let (_pool, guard) = setup_guard().await;

        let result = guard.can_post_thread().await.expect("check");
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn safety_guard_can_post_thread_blocked() {
        let (_pool, guard) = setup_guard().await;

        // Exhaust thread limit (max = 1)
        guard.record_thread().await.expect("record");

        let result = guard.can_post_thread().await.expect("check");
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn denial_reason_display() {
        let rate = DenialReason::RateLimited {
            action_type: "reply".to_string(),
            current: 20,
            max: 20,
        };
        assert_eq!(rate.to_string(), "Rate limited: reply (20/20)");

        let replied = DenialReason::AlreadyReplied {
            tweet_id: "abc123".to_string(),
        };
        assert_eq!(replied.to_string(), "Already replied to tweet abc123");

        let similar = DenialReason::SimilarPhrasing;
        assert_eq!(
            similar.to_string(),
            "Reply phrasing too similar to recent replies"
        );

        let banned = DenialReason::BannedPhrase {
            phrase: "check out".to_string(),
        };
        assert_eq!(
            banned.to_string(),
            "Reply contains banned phrase: \"check out\""
        );

        let author = DenialReason::AuthorLimitReached;
        assert_eq!(
            author.to_string(),
            "Already reached daily reply limit for this author"
        );

        let self_reply = DenialReason::SelfReply;
        assert_eq!(self_reply.to_string(), "Cannot reply to own tweets");
    }

    #[test]
    fn contains_banned_phrase_detects_match() {
        let banned = vec!["check out".to_string(), "link in bio".to_string()];
        assert_eq!(
            contains_banned_phrase("You should check out this tool!", &banned),
            Some("check out".to_string())
        );
    }

    #[test]
    fn contains_banned_phrase_case_insensitive() {
        let banned = vec!["Check Out".to_string()];
        assert_eq!(
            contains_banned_phrase("check out this thing", &banned),
            Some("Check Out".to_string())
        );
    }

    #[test]
    fn contains_banned_phrase_no_match() {
        let banned = vec!["check out".to_string()];
        assert_eq!(
            contains_banned_phrase("This is a helpful reply", &banned),
            None
        );
    }

    #[test]
    fn is_self_reply_detects_self() {
        assert!(is_self_reply("user_123", "user_123"));
    }

    #[test]
    fn is_self_reply_different_users() {
        assert!(!is_self_reply("user_123", "user_456"));
    }

    #[test]
    fn is_self_reply_empty_ids() {
        assert!(!is_self_reply("", "user_123"));
        assert!(!is_self_reply("user_123", ""));
        assert!(!is_self_reply("", ""));
    }

    #[tokio::test]
    async fn safety_guard_check_author_limit_allows_first() {
        let (_pool, guard) = setup_guard().await;
        let result = guard
            .check_author_limit("author_1", 1)
            .await
            .expect("check");
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn safety_guard_check_author_limit_blocks_over_limit() {
        let (_pool, guard) = setup_guard().await;
        guard
            .record_author_interaction("author_1", "alice")
            .await
            .expect("record");

        let result = guard
            .check_author_limit("author_1", 1)
            .await
            .expect("check");
        assert_eq!(result, Err(DenialReason::AuthorLimitReached));
    }

    #[test]
    fn check_banned_phrases_blocks_banned() {
        let banned = vec!["check out".to_string(), "I recommend".to_string()];
        let result = SafetyGuard::check_banned_phrases("You should check out this tool!", &banned);
        assert_eq!(
            result,
            Err(DenialReason::BannedPhrase {
                phrase: "check out".to_string()
            })
        );
    }

    #[test]
    fn check_banned_phrases_allows_clean() {
        let banned = vec!["check out".to_string()];
        let result = SafetyGuard::check_banned_phrases("Great insight on testing!", &banned);
        assert!(result.is_ok());
    }

    #[test]
    fn contains_banned_phrase_empty_list() {
        assert_eq!(contains_banned_phrase("anything", &[]), None);
    }

    #[test]
    fn contains_banned_phrase_empty_text() {
        let banned = vec!["check out".to_string()];
        assert_eq!(contains_banned_phrase("", &banned), None);
    }

    #[test]
    fn denial_reason_display_all_variants() {
        // Verify all variants produce non-empty display strings
        let variants = vec![
            DenialReason::RateLimited {
                action_type: "search".to_string(),
                current: 5,
                max: 5,
            },
            DenialReason::AlreadyReplied {
                tweet_id: "t1".to_string(),
            },
            DenialReason::SimilarPhrasing,
            DenialReason::BannedPhrase {
                phrase: "buy now".to_string(),
            },
            DenialReason::AuthorLimitReached,
            DenialReason::SelfReply,
        ];
        for variant in &variants {
            assert!(!variant.to_string().is_empty());
        }
    }

    #[test]
    fn denial_reason_equality() {
        assert_eq!(DenialReason::SelfReply, DenialReason::SelfReply);
        assert_eq!(
            DenialReason::AuthorLimitReached,
            DenialReason::AuthorLimitReached
        );
        assert_ne!(DenialReason::SelfReply, DenialReason::SimilarPhrasing);
    }

    #[tokio::test]
    async fn safety_guard_exposes_rate_limiter_and_dedup() {
        let (_pool, guard) = setup_guard().await;

        // Verify accessors work without panicking
        assert!(guard.rate_limiter().can_search().await.expect("search"));
        let phrases = guard
            .dedup_checker()
            .get_recent_reply_phrases(5)
            .await
            .expect("phrases");
        assert!(phrases.is_empty());
    }

    // -----------------------------------------------------------------------
    // Additional safety coverage tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn rate_limiter_can_tweet_and_record() {
        let pool = init_test_db().await.expect("init db");
        rate_limits::init_rate_limits(&pool, &test_limits(), &test_intervals())
            .await
            .expect("init");

        let limiter = RateLimiter::new(pool);
        assert!(limiter.can_tweet().await.expect("check"));
        limiter.record_tweet().await.expect("record");
        limiter.record_tweet().await.expect("record");
        // max_tweets_per_day = 2
        assert!(!limiter.can_tweet().await.expect("check"));
    }

    #[tokio::test]
    async fn rate_limiter_can_thread_and_record() {
        let pool = init_test_db().await.expect("init db");
        rate_limits::init_rate_limits(&pool, &test_limits(), &test_intervals())
            .await
            .expect("init");

        let limiter = RateLimiter::new(pool);
        assert!(limiter.can_thread().await.expect("check"));
        limiter.record_thread().await.expect("record");
        // max_threads_per_week = 1
        assert!(!limiter.can_thread().await.expect("check"));
    }

    #[tokio::test]
    async fn rate_limiter_can_search_and_record() {
        let pool = init_test_db().await.expect("init db");
        rate_limits::init_rate_limits(&pool, &test_limits(), &test_intervals())
            .await
            .expect("init");

        let limiter = RateLimiter::new(pool);
        assert!(limiter.can_search().await.expect("check"));
        limiter.record_search().await.expect("record");
    }

    #[test]
    fn contains_banned_phrase_multiple_matches_returns_first() {
        let banned = vec!["check out".to_string(), "link in bio".to_string()];
        let result = contains_banned_phrase("check out the link in bio", &banned);
        assert_eq!(result, Some("check out".to_string()));
    }

    #[test]
    fn contains_banned_phrase_substring_match() {
        let banned = vec!["buy now".to_string()];
        // "buy now" appears as substring
        assert_eq!(
            contains_banned_phrase("Go buy now and save!", &banned),
            Some("buy now".to_string())
        );
    }

    #[test]
    fn is_self_reply_whitespace_ids() {
        // Whitespace strings are non-empty and equal, so this is a self-reply
        assert!(is_self_reply(" ", " "));
        // Different whitespace strings: not a self-reply
        assert!(!is_self_reply("user_123", " "));
    }

    #[test]
    fn denial_reason_clone_and_debug() {
        let reason = DenialReason::BannedPhrase {
            phrase: "test".to_string(),
        };
        let cloned = reason.clone();
        assert_eq!(reason, cloned);
        let debug = format!("{:?}", reason);
        assert!(debug.contains("BannedPhrase"));
    }

    #[test]
    fn check_banned_phrases_empty_list_allows() {
        let result = SafetyGuard::check_banned_phrases("anything goes here", &[]);
        assert!(result.is_ok());
    }

    #[test]
    fn check_banned_phrases_case_insensitive() {
        let banned = vec!["CHECK OUT".to_string()];
        let result = SafetyGuard::check_banned_phrases("check out this", &banned);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn safety_guard_record_tweet_works() {
        let (_pool, guard) = setup_guard().await;
        guard.record_tweet().await.expect("record tweet");
    }

    #[tokio::test]
    async fn safety_guard_record_thread_works() {
        let (_pool, guard) = setup_guard().await;
        guard.record_thread().await.expect("record thread");
    }

    #[tokio::test]
    async fn safety_guard_can_reply_to_with_unique_reply() {
        let (_pool, guard) = setup_guard().await;
        let result = guard
            .can_reply_to("unique_tweet", Some("A completely unique reply text here"))
            .await
            .expect("check");
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn safety_guard_multiple_author_interactions() {
        let (_pool, guard) = setup_guard().await;
        // First interaction OK
        let result = guard.check_author_limit("a1", 2).await.expect("check");
        assert!(result.is_ok());

        // Record two interactions
        guard
            .record_author_interaction("a1", "alice")
            .await
            .expect("record 1");
        guard
            .record_author_interaction("a1", "alice")
            .await
            .expect("record 2");

        // Now should be blocked (limit=2)
        let result = guard.check_author_limit("a1", 2).await.expect("check");
        assert_eq!(result, Err(DenialReason::AuthorLimitReached));
    }

    #[tokio::test]
    async fn safety_guard_different_authors_independent() {
        let (_pool, guard) = setup_guard().await;
        guard
            .record_author_interaction("a1", "alice")
            .await
            .expect("record");

        // Different author should still be allowed
        let result = guard.check_author_limit("a2", 1).await.expect("check");
        assert!(result.is_ok());
    }
}