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
//! Tests for target account CRUD and analytics.

use super::*;
use crate::storage::init_test_db;

#[tokio::test]
async fn upsert_and_get_target_account() {
    let pool = init_test_db().await.expect("init db");

    upsert_target_account(&pool, "acc_1", "alice")
        .await
        .expect("upsert");

    let account = get_target_account(&pool, "acc_1")
        .await
        .expect("get")
        .expect("found");
    assert_eq!(account.username, "alice");
    assert_eq!(account.total_replies_sent, 0);
    assert_eq!(account.status, "active");
}

#[tokio::test]
async fn get_active_target_accounts_works() {
    let pool = init_test_db().await.expect("init db");

    upsert_target_account(&pool, "acc_1", "alice")
        .await
        .expect("upsert");
    upsert_target_account(&pool, "acc_2", "bob")
        .await
        .expect("upsert");

    let accounts = get_active_target_accounts(&pool).await.expect("get all");
    assert_eq!(accounts.len(), 2);
}

#[tokio::test]
async fn record_target_reply_increments() {
    let pool = init_test_db().await.expect("init db");

    upsert_target_account(&pool, "acc_1", "alice")
        .await
        .expect("upsert");
    record_target_reply(&pool, "acc_1").await.expect("reply");
    record_target_reply(&pool, "acc_1").await.expect("reply");

    let account = get_target_account(&pool, "acc_1")
        .await
        .expect("get")
        .expect("found");
    assert_eq!(account.total_replies_sent, 2);
    assert!(account.first_engagement_at.is_some());
    assert!(account.last_reply_at.is_some());
}

#[tokio::test]
async fn store_and_check_target_tweet() {
    let pool = init_test_db().await.expect("init db");

    upsert_target_account(&pool, "acc_1", "alice")
        .await
        .expect("upsert");

    assert!(!target_tweet_exists(&pool, "tw_1").await.expect("check"));

    store_target_tweet(&pool, "tw_1", "acc_1", "hello", "2026-01-01", 0, 5, 80.0)
        .await
        .expect("store");

    assert!(target_tweet_exists(&pool, "tw_1").await.expect("check"));
}

#[tokio::test]
async fn mark_replied_updates_flag() {
    let pool = init_test_db().await.expect("init db");

    upsert_target_account(&pool, "acc_1", "alice")
        .await
        .expect("upsert");
    store_target_tweet(&pool, "tw_1", "acc_1", "hello", "2026-01-01", 0, 5, 80.0)
        .await
        .expect("store");

    mark_target_tweet_replied(&pool, "tw_1")
        .await
        .expect("mark");

    // Verify by checking the count of replied tweets
    let count = count_target_replies_today(&pool).await.expect("count");
    assert!(count >= 0); // May or may not be today depending on test timing
}

#[tokio::test]
async fn get_enriched_includes_interactions_today() {
    let pool = init_test_db().await.expect("init db");

    upsert_target_account(&pool, "acc_1", "alice")
        .await
        .expect("upsert");
    upsert_target_account(&pool, "acc_2", "bob")
        .await
        .expect("upsert");

    // Store a tweet for alice discovered today and mark as replied
    let today = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string();
    store_target_tweet(&pool, "tw_1", "acc_1", "hello", &today, 0, 5, 80.0)
        .await
        .expect("store");
    mark_target_tweet_replied(&pool, "tw_1")
        .await
        .expect("mark");

    let enriched = get_enriched_target_accounts(&pool).await.expect("enriched");
    assert_eq!(enriched.len(), 2);

    let alice = enriched.iter().find(|a| a.username == "alice").unwrap();
    assert_eq!(alice.interactions_today, 1);

    let bob = enriched.iter().find(|a| a.username == "bob").unwrap();
    assert_eq!(bob.interactions_today, 0);
}

#[tokio::test]
async fn get_target_timeline_returns_tweets_with_replies() {
    let pool = init_test_db().await.expect("init db");

    upsert_target_account(&pool, "acc_1", "alice")
        .await
        .expect("upsert");

    store_target_tweet(
        &pool,
        "tw_1",
        "acc_1",
        "First tweet",
        "2026-02-20T10:00:00Z",
        2,
        10,
        75.0,
    )
    .await
    .expect("store");
    store_target_tweet(
        &pool,
        "tw_2",
        "acc_1",
        "Second tweet",
        "2026-02-21T10:00:00Z",
        1,
        5,
        60.0,
    )
    .await
    .expect("store");
    mark_target_tweet_replied(&pool, "tw_1")
        .await
        .expect("mark");

    // Insert a reply for tw_1
    sqlx::query(
        "INSERT INTO replies_sent (target_tweet_id, reply_content, created_at, status) \
         VALUES ('tw_1', 'Great point!', '2026-02-20T11:00:00Z', 'sent')",
    )
    .execute(&pool)
    .await
    .expect("insert reply");

    let timeline = get_target_timeline(&pool, "alice", 50)
        .await
        .expect("timeline");
    assert_eq!(timeline.len(), 2);

    // Most recent first
    assert_eq!(timeline[0].tweet_id, "tw_2");
    assert!(!timeline[0].replied_to);
    assert!(timeline[0].reply_content.is_none());

    assert_eq!(timeline[1].tweet_id, "tw_1");
    assert!(timeline[1].replied_to);
    assert_eq!(timeline[1].reply_content.as_deref(), Some("Great point!"));
}

#[tokio::test]
async fn get_target_stats_returns_aggregates() {
    let pool = init_test_db().await.expect("init db");

    upsert_target_account(&pool, "acc_1", "alice")
        .await
        .expect("upsert");

    // Record some replies to set first/last engagement
    record_target_reply(&pool, "acc_1").await.expect("reply");
    record_target_reply(&pool, "acc_1").await.expect("reply");

    // Store tweets with scores and mark as replied
    store_target_tweet(
        &pool,
        "tw_1",
        "acc_1",
        "Tweet one",
        "2026-02-20T10:00:00Z",
        0,
        5,
        70.0,
    )
    .await
    .expect("store");
    mark_target_tweet_replied(&pool, "tw_1")
        .await
        .expect("mark");

    store_target_tweet(
        &pool,
        "tw_2",
        "acc_1",
        "Tweet two",
        "2026-02-22T10:00:00Z",
        0,
        3,
        90.0,
    )
    .await
    .expect("store");
    mark_target_tweet_replied(&pool, "tw_2")
        .await
        .expect("mark");

    // Insert replies for both
    sqlx::query(
        "INSERT INTO replies_sent (target_tweet_id, reply_content, created_at, status) \
         VALUES ('tw_1', 'Reply one', '2026-02-20T11:00:00Z', 'sent')",
    )
    .execute(&pool)
    .await
    .expect("insert reply");
    sqlx::query(
        "INSERT INTO replies_sent (target_tweet_id, reply_content, created_at, status) \
         VALUES ('tw_2', 'Reply two', '2026-02-22T11:00:00Z', 'sent')",
    )
    .execute(&pool)
    .await
    .expect("insert reply");

    let stats = get_target_stats(&pool, "alice")
        .await
        .expect("stats")
        .expect("found");
    assert_eq!(stats.total_replies, 2);
    assert!((stats.avg_score - 80.0).abs() < 0.01); // (70+90)/2
    assert!(stats.best_reply_content.is_some());
    assert!((stats.best_reply_score.unwrap() - 90.0).abs() < 0.01);
}

#[tokio::test]
async fn get_target_stats_returns_none_for_missing() {
    let pool = init_test_db().await.expect("init db");

    let stats = get_target_stats(&pool, "nobody").await.expect("stats");
    assert!(stats.is_none());
}

#[test]
fn compute_frequency_less_than_two_replies() {
    let result = compute_frequency(
        &Some("2026-01-01T00:00:00".to_string()),
        &Some("2026-01-10T00:00:00".to_string()),
        1,
    );
    assert!(result.is_none());
}

#[test]
fn compute_frequency_none_dates() {
    assert!(compute_frequency(&None, &Some("2026-01-10T00:00:00".to_string()), 5).is_none());
    assert!(compute_frequency(&Some("2026-01-01T00:00:00".to_string()), &None, 5).is_none());
}

#[test]
fn compute_frequency_valid_dates() {
    let first = Some("2026-01-01T00:00:00".to_string());
    let last = Some("2026-01-11T00:00:00".to_string());
    let result = compute_frequency(&first, &last, 6);
    assert!(result.is_some());
    // 10 days / (6-1) = 2.0 days per interaction
    assert!((result.unwrap() - 2.0).abs() < 0.01);
}

#[test]
fn compute_frequency_same_date_returns_none() {
    let date = Some("2026-01-01T00:00:00".to_string());
    let result = compute_frequency(&date, &date, 3);
    assert!(result.is_none()); // span = 0
}

#[test]
fn compute_frequency_invalid_dates() {
    let result = compute_frequency(
        &Some("not-a-date".to_string()),
        &Some("2026-01-10T00:00:00".to_string()),
        5,
    );
    assert!(result.is_none());
}

#[tokio::test]
async fn deactivate_target_account_works() {
    let pool = init_test_db().await.expect("init db");

    upsert_target_account(&pool, "acc_1", "alice")
        .await
        .expect("upsert");

    let deactivated = deactivate_target_account(&pool, "alice")
        .await
        .expect("deactivate");
    assert!(deactivated);

    // Should not appear in active accounts
    let active = get_active_target_accounts(&pool).await.expect("get");
    assert!(active.is_empty());
}

#[tokio::test]
async fn deactivate_nonexistent_account_returns_false() {
    let pool = init_test_db().await.expect("init db");

    let deactivated = deactivate_target_account(&pool, "nobody")
        .await
        .expect("deactivate");
    assert!(!deactivated);
}

#[tokio::test]
async fn deactivate_already_inactive_returns_false() {
    let pool = init_test_db().await.expect("init db");

    upsert_target_account(&pool, "acc_1", "alice")
        .await
        .expect("upsert");
    deactivate_target_account(&pool, "alice")
        .await
        .expect("deactivate");

    // Second deactivate should return false
    let second = deactivate_target_account(&pool, "alice")
        .await
        .expect("deactivate");
    assert!(!second);
}

#[tokio::test]
async fn get_target_account_by_username_works() {
    let pool = init_test_db().await.expect("init db");

    upsert_target_account(&pool, "acc_1", "alice")
        .await
        .expect("upsert");

    let account = get_target_account_by_username(&pool, "alice")
        .await
        .expect("get")
        .expect("found");
    assert_eq!(account.account_id, "acc_1");
    assert_eq!(account.username, "alice");
}

#[tokio::test]
async fn get_target_account_by_username_not_found() {
    let pool = init_test_db().await.expect("init db");

    let account = get_target_account_by_username(&pool, "nobody")
        .await
        .expect("get");
    assert!(account.is_none());
}

#[tokio::test]
async fn upsert_updates_username_on_conflict() {
    let pool = init_test_db().await.expect("init db");

    upsert_target_account(&pool, "acc_1", "alice")
        .await
        .expect("upsert");
    upsert_target_account(&pool, "acc_1", "alice_new")
        .await
        .expect("upsert again");

    let account = get_target_account(&pool, "acc_1")
        .await
        .expect("get")
        .expect("found");
    assert_eq!(account.username, "alice_new");
}

#[tokio::test]
async fn get_target_account_not_found() {
    let pool = init_test_db().await.expect("init db");

    let account = get_target_account(&pool, "nonexistent").await.expect("get");
    assert!(account.is_none());
}

#[tokio::test]
async fn store_target_tweet_ignore_duplicate() {
    let pool = init_test_db().await.expect("init db");

    upsert_target_account(&pool, "acc_1", "alice")
        .await
        .expect("upsert");

    store_target_tweet(&pool, "tw_1", "acc_1", "hello", "2026-01-01", 0, 5, 80.0)
        .await
        .expect("store first");

    // Should not error (INSERT OR IGNORE)
    store_target_tweet(&pool, "tw_1", "acc_1", "updated", "2026-01-01", 0, 5, 90.0)
        .await
        .expect("store duplicate");
}

#[tokio::test]
async fn count_target_replies_today_zero() {
    let pool = init_test_db().await.expect("init db");
    let count = count_target_replies_today(&pool).await.expect("count");
    assert_eq!(count, 0);
}

#[tokio::test]
async fn get_target_stats_returns_zero_avg_for_target_without_replies() {
    let pool = init_test_db().await.expect("init db");

    upsert_target_account(&pool, "acc_1", "alice")
        .await
        .expect("upsert");

    let stats = get_target_stats(&pool, "alice")
        .await
        .expect("stats")
        .expect("found");
    assert_eq!(stats.total_replies, 0);
    assert_eq!(stats.avg_score, 0.0);
    assert!(stats.best_reply_content.is_none());
    assert!(stats.best_reply_score.is_none());
    assert!(stats.first_interaction.is_none());
    assert!(stats.interaction_frequency_days.is_none());
}