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
//! Duplicate reply prevention.
//!
//! Provides exact-match deduplication (never reply to the same tweet twice)
//! and phrasing similarity detection (reject replies too similar to recent ones).

use crate::error::StorageError;
use crate::storage::DbPool;
use std::collections::HashSet;

/// Checks for duplicate and similar replies.
pub struct DedupChecker {
    pool: DbPool,
}

impl DedupChecker {
    /// Create a new dedup checker backed by the given database pool.
    pub fn new(pool: DbPool) -> Self {
        Self { pool }
    }

    /// Check if a reply has already been sent to the given tweet.
    ///
    /// Returns `true` if a reply exists in `replies_sent` for this tweet ID.
    pub async fn has_replied_to(&self, tweet_id: &str) -> Result<bool, StorageError> {
        crate::storage::replies::has_replied_to(&self.pool, tweet_id).await
    }

    /// Check if a proposed reply is too similar to recent replies.
    ///
    /// Compares against the last `limit` replies using Jaccard word similarity.
    /// Returns `true` if any recent reply has >= 0.8 similarity or is an exact match.
    /// Replies shorter than 5 words skip the similarity check (too short for meaningful comparison).
    pub async fn is_phrasing_similar(
        &self,
        new_reply: &str,
        limit: i64,
    ) -> Result<bool, StorageError> {
        if new_reply.is_empty() {
            return Ok(false);
        }

        let recent = crate::storage::replies::get_recent_reply_contents(&self.pool, limit).await?;
        let new_tokens = tokenize(new_reply);

        for recent_reply in &recent {
            // Exact match check
            if new_reply == recent_reply {
                return Ok(true);
            }

            // Skip similarity check for very short replies
            if new_tokens.len() < 5 {
                continue;
            }

            let recent_tokens = tokenize(recent_reply);
            if jaccard_similarity(&new_tokens, &recent_tokens) >= 0.8 {
                return Ok(true);
            }
        }

        Ok(false)
    }

    /// Get recent reply contents for testing and debugging.
    pub async fn get_recent_reply_phrases(&self, limit: i64) -> Result<Vec<String>, StorageError> {
        crate::storage::replies::get_recent_reply_contents(&self.pool, limit).await
    }
}

/// Tokenize text into a set of lowercase alphanumeric words.
fn tokenize(text: &str) -> HashSet<String> {
    text.to_lowercase()
        .split_whitespace()
        .map(|w| w.trim_matches(|c: char| !c.is_alphanumeric()).to_string())
        .filter(|w| !w.is_empty())
        .collect()
}

/// Calculate Jaccard similarity between two word sets.
///
/// Returns a value between 0.0 (no overlap) and 1.0 (identical sets).
/// Two empty sets are considered identical (returns 1.0).
fn jaccard_similarity(a: &HashSet<String>, b: &HashSet<String>) -> f64 {
    if a.is_empty() && b.is_empty() {
        return 1.0;
    }
    let intersection = a.intersection(b).count() as f64;
    let union = a.union(b).count() as f64;
    intersection / union
}

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

    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,
        }
    }

    #[test]
    fn tokenize_basic() {
        let tokens = tokenize("Hello, World! This is a test.");
        assert!(tokens.contains("hello"));
        assert!(tokens.contains("world"));
        assert!(tokens.contains("test"));
        assert!(!tokens.contains(""));
    }

    #[test]
    fn tokenize_strips_punctuation() {
        let tokens = tokenize("(great) [tool] {for} developers!");
        assert!(tokens.contains("great"));
        assert!(tokens.contains("tool"));
        assert!(tokens.contains("for"));
        assert!(tokens.contains("developers"));
    }

    #[test]
    fn tokenize_empty_string() {
        let tokens = tokenize("");
        assert!(tokens.is_empty());
    }

    #[test]
    fn jaccard_identical_sets() {
        let a: HashSet<String> = ["hello", "world"].iter().map(|s| s.to_string()).collect();
        let b = a.clone();
        assert!((jaccard_similarity(&a, &b) - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn jaccard_disjoint_sets() {
        let a: HashSet<String> = ["hello", "world"].iter().map(|s| s.to_string()).collect();
        let b: HashSet<String> = ["foo", "bar"].iter().map(|s| s.to_string()).collect();
        assert!((jaccard_similarity(&a, &b)).abs() < f64::EPSILON);
    }

    #[test]
    fn jaccard_partial_overlap() {
        let a: HashSet<String> = ["hello", "world", "foo"]
            .iter()
            .map(|s| s.to_string())
            .collect();
        let b: HashSet<String> = ["hello", "world", "bar"]
            .iter()
            .map(|s| s.to_string())
            .collect();
        // intersection=2, union=4 => 0.5
        let sim = jaccard_similarity(&a, &b);
        assert!((sim - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn jaccard_empty_sets() {
        let a: HashSet<String> = HashSet::new();
        let b: HashSet<String> = HashSet::new();
        assert!((jaccard_similarity(&a, &b) - 1.0).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn has_replied_to_works() {
        let pool = init_test_db().await.expect("init db");
        let checker = DedupChecker::new(pool.clone());

        assert!(!checker.has_replied_to("tweet_123").await.expect("check"));

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

        assert!(checker.has_replied_to("tweet_123").await.expect("check"));
        assert!(!checker.has_replied_to("tweet_456").await.expect("check"));
    }

    #[tokio::test]
    async fn is_phrasing_similar_exact_match() {
        let pool = init_test_db().await.expect("init db");
        let checker = DedupChecker::new(pool.clone());

        let reply = sample_reply("t1", "This is a great tool for developers");
        insert_reply(&pool, &reply).await.expect("insert");

        assert!(checker
            .is_phrasing_similar("This is a great tool for developers", 20)
            .await
            .expect("check"));
    }

    #[tokio::test]
    async fn is_phrasing_similar_high_overlap() {
        let pool = init_test_db().await.expect("init db");
        let checker = DedupChecker::new(pool.clone());

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

        // Very similar phrasing (most words overlap)
        assert!(checker
            .is_phrasing_similar("This is a great tool for developers and designers", 20)
            .await
            .expect("check"));
    }

    #[tokio::test]
    async fn is_phrasing_similar_no_overlap() {
        let pool = init_test_db().await.expect("init db");
        let checker = DedupChecker::new(pool.clone());

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

        assert!(!checker
            .is_phrasing_similar("I love cooking pasta with fresh basil and tomatoes", 20)
            .await
            .expect("check"));
    }

    #[tokio::test]
    async fn is_phrasing_similar_empty_string() {
        let pool = init_test_db().await.expect("init db");
        let checker = DedupChecker::new(pool.clone());

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

        assert!(!checker.is_phrasing_similar("", 20).await.expect("check"));
    }

    #[tokio::test]
    async fn is_phrasing_similar_short_reply_skips_similarity() {
        let pool = init_test_db().await.expect("init db");
        let checker = DedupChecker::new(pool.clone());

        let reply = sample_reply("t1", "Great point!");
        insert_reply(&pool, &reply).await.expect("insert");

        // Short reply (< 5 words) - exact match still works
        assert!(checker
            .is_phrasing_similar("Great point!", 20)
            .await
            .expect("check"));

        // But similar short phrases don't trigger (avoids false positives)
        assert!(!checker
            .is_phrasing_similar("Good point!", 20)
            .await
            .expect("check"));
    }

    #[tokio::test]
    async fn is_phrasing_similar_no_recent_replies() {
        let pool = init_test_db().await.expect("init db");
        let checker = DedupChecker::new(pool.clone());

        assert!(!checker
            .is_phrasing_similar("Any reply text here that is long enough to test", 20)
            .await
            .expect("check"));
    }

    #[tokio::test]
    async fn get_recent_reply_phrases_works() {
        let pool = init_test_db().await.expect("init db");
        let checker = DedupChecker::new(pool.clone());

        let r1 = sample_reply("t1", "Reply one");
        let r2 = sample_reply("t2", "Reply two");
        insert_reply(&pool, &r1).await.expect("ins1");
        insert_reply(&pool, &r2).await.expect("ins2");

        let phrases = checker.get_recent_reply_phrases(5).await.expect("get");
        assert_eq!(phrases.len(), 2);
    }

    // -----------------------------------------------------------------------
    // Additional dedup coverage tests
    // -----------------------------------------------------------------------

    #[test]
    fn tokenize_mixed_punctuation() {
        let tokens = tokenize("Hello! World? This... is (a) test.");
        assert!(tokens.contains("hello"));
        assert!(tokens.contains("world"));
        assert!(tokens.contains("this"));
        assert!(tokens.contains("a"));
        assert!(tokens.contains("test"));
    }

    #[test]
    fn tokenize_duplicate_words() {
        // HashSet deduplicates
        let tokens = tokenize("hello hello hello world");
        assert_eq!(tokens.len(), 2);
    }

    #[test]
    fn tokenize_only_punctuation() {
        let tokens = tokenize("!!! ... ???");
        assert!(tokens.is_empty());
    }

    #[test]
    fn tokenize_single_word() {
        let tokens = tokenize("hello");
        assert_eq!(tokens.len(), 1);
        assert!(tokens.contains("hello"));
    }

    #[test]
    fn tokenize_case_normalization() {
        let tokens = tokenize("Hello HELLO hello");
        assert_eq!(tokens.len(), 1);
        assert!(tokens.contains("hello"));
    }

    #[test]
    fn jaccard_one_empty_one_not() {
        let a: HashSet<String> = HashSet::new();
        let b: HashSet<String> = ["hello"].iter().map(|s| s.to_string()).collect();
        assert!((jaccard_similarity(&a, &b) - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn jaccard_single_element_match() {
        let a: HashSet<String> = ["hello"].iter().map(|s| s.to_string()).collect();
        let b: HashSet<String> = ["hello"].iter().map(|s| s.to_string()).collect();
        assert!((jaccard_similarity(&a, &b) - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn jaccard_superset() {
        let a: HashSet<String> = ["hello", "world", "foo"]
            .iter()
            .map(|s| s.to_string())
            .collect();
        let b: HashSet<String> = ["hello", "world"].iter().map(|s| s.to_string()).collect();
        // intersection=2, union=3 => 0.667
        let sim = jaccard_similarity(&a, &b);
        assert!((sim - 2.0 / 3.0).abs() < 0.001);
    }

    #[test]
    fn jaccard_high_overlap_threshold() {
        // 8 common words out of 10 total = 0.8
        let common: Vec<String> = (0..8).map(|i| format!("word{i}")).collect();
        let mut a: HashSet<String> = common.iter().cloned().collect();
        a.insert("unique_a".to_string());
        let mut b: HashSet<String> = common.iter().cloned().collect();
        b.insert("unique_b".to_string());
        let sim = jaccard_similarity(&a, &b);
        assert!(sim >= 0.8);
    }

    #[tokio::test]
    async fn has_replied_to_multiple_tweets() {
        let pool = init_test_db().await.expect("init db");
        let checker = DedupChecker::new(pool.clone());

        let r1 = sample_reply("tweet_100", "Reply 1");
        let r2 = sample_reply("tweet_200", "Reply 2");
        insert_reply(&pool, &r1).await.expect("ins1");
        insert_reply(&pool, &r2).await.expect("ins2");

        assert!(checker.has_replied_to("tweet_100").await.expect("check"));
        assert!(checker.has_replied_to("tweet_200").await.expect("check"));
        assert!(!checker.has_replied_to("tweet_300").await.expect("check"));
    }

    #[tokio::test]
    async fn get_recent_reply_phrases_respects_limit() {
        let pool = init_test_db().await.expect("init db");
        let checker = DedupChecker::new(pool.clone());

        for i in 0..5 {
            let reply = sample_reply(&format!("t{i}"), &format!("Reply content {i}"));
            insert_reply(&pool, &reply).await.expect("insert");
        }

        let phrases = checker.get_recent_reply_phrases(3).await.expect("get");
        assert_eq!(phrases.len(), 3);
    }

    #[tokio::test]
    async fn is_phrasing_similar_moderate_overlap_not_triggered() {
        let pool = init_test_db().await.expect("init db");
        let checker = DedupChecker::new(pool.clone());

        let reply = sample_reply(
            "t1",
            "Rust has an amazing type system with lifetimes and borrowing rules",
        );
        insert_reply(&pool, &reply).await.expect("insert");

        // Moderate overlap (some words shared) — should NOT trigger (< 0.8)
        assert!(!checker
            .is_phrasing_similar(
                "Python has dynamic typing with duck typing and runtime checks",
                20,
            )
            .await
            .expect("check"));
    }
}