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
#[cfg(test)]
mod tests {
    use super::super::*;
    use std::time::Duration;

    // ── randomized_delay ────────────────────────────────────────────

    #[test]
    fn delay_returns_min_when_min_equals_max() {
        let d = queue::randomized_delay(Duration::from_secs(5), Duration::from_secs(5));
        assert_eq!(d, Duration::from_secs(5));
    }

    #[test]
    fn delay_returns_min_when_min_greater_than_max() {
        let d = queue::randomized_delay(Duration::from_secs(10), Duration::from_secs(5));
        assert_eq!(d, Duration::from_secs(10));
    }

    #[test]
    fn delay_returns_zero_when_both_zero() {
        let d = queue::randomized_delay(Duration::ZERO, Duration::ZERO);
        assert_eq!(d, Duration::ZERO);
    }

    #[test]
    fn delay_within_range() {
        let min = Duration::from_millis(100);
        let max = Duration::from_millis(500);
        for _ in 0..50 {
            let d = queue::randomized_delay(min, max);
            assert!(d >= min, "delay {d:?} should be >= {min:?}");
            assert!(d <= max, "delay {d:?} should be <= {max:?}");
        }
    }

    #[test]
    fn delay_zero_min_nonzero_max() {
        let min = Duration::ZERO;
        let max = Duration::from_millis(100);
        for _ in 0..20 {
            let d = queue::randomized_delay(min, max);
            assert!(d <= max);
        }
    }

    #[test]
    fn delay_narrow_range_produces_deterministic_ish_result() {
        let min = Duration::from_millis(50);
        let max = Duration::from_millis(51);
        for _ in 0..20 {
            let d = queue::randomized_delay(min, max);
            assert!(d >= min && d <= max);
        }
    }

    // ── media_paths JSON parsing (mirrors inline logic) ─────────────

    #[test]
    fn media_paths_parses_valid_json_array() {
        let json = r#"["/tmp/img1.png", "/tmp/img2.jpg"]"#;
        let paths: Vec<String> = serde_json::from_str(json).unwrap_or_default();
        assert_eq!(paths.len(), 2);
        assert_eq!(paths[0], "/tmp/img1.png");
    }

    #[test]
    fn media_paths_parses_empty_array() {
        let json = "[]";
        let paths: Vec<String> = serde_json::from_str(json).unwrap_or_default();
        assert!(paths.is_empty());
    }

    #[test]
    fn media_paths_invalid_json_returns_empty() {
        let json = "not valid json";
        let paths: Vec<String> = serde_json::from_str(json).unwrap_or_default();
        assert!(paths.is_empty());
    }

    #[test]
    fn media_paths_empty_string_returns_empty() {
        let json = "";
        let paths: Vec<String> = serde_json::from_str(json).unwrap_or_default();
        assert!(paths.is_empty());
    }

    // ── action_type routing logic ───────────────────────────────────

    #[test]
    fn action_type_reply_with_target_routes_to_reply() {
        let action_type = "reply";
        let target_tweet_id = "12345";
        let is_reply = action_type == "reply" && !target_tweet_id.is_empty();
        assert!(is_reply);
    }

    #[test]
    fn action_type_reply_without_target_routes_to_tweet() {
        let action_type = "reply";
        let target_tweet_id = "";
        let is_reply = action_type == "reply" && !target_tweet_id.is_empty();
        assert!(!is_reply);
    }

    #[test]
    fn action_type_tweet_routes_to_tweet() {
        let action_type = "tweet";
        let target_tweet_id = "";
        let is_reply = action_type == "reply" && !target_tweet_id.is_empty();
        assert!(!is_reply);
    }

    #[test]
    fn action_type_thread_tweet_routes_to_tweet() {
        let action_type = "thread_tweet";
        let target_tweet_id = "some_id";
        let is_reply = action_type == "reply" && !target_tweet_id.is_empty();
        assert!(!is_reply);
    }

    // ── action log format string ────────────────────────────────────

    #[test]
    fn action_log_format_for_reply() {
        let action_type = "reply";
        let log_action = format!("{action_type}_posted");
        assert_eq!(log_action, "reply_posted");
    }

    #[test]
    fn action_log_format_for_tweet() {
        let action_type = "tweet";
        let log_action = format!("{action_type}_posted");
        assert_eq!(log_action, "tweet_posted");
    }

    // ── post_reply / post_tweet helper logic ─────────────────────

    #[test]
    fn media_ids_empty_gives_none() {
        let media_ids: Vec<String> = vec![];
        let media: Option<&[String]> = if media_ids.is_empty() {
            None
        } else {
            Some(&media_ids)
        };
        assert!(media.is_none());
    }

    #[test]
    fn media_ids_nonempty_gives_some() {
        let media_ids = vec!["m1".to_string()];
        let media: Option<&[String]> = if media_ids.is_empty() {
            None
        } else {
            Some(&media_ids)
        };
        assert!(media.is_some());
        assert_eq!(media.unwrap().len(), 1);
    }

    // ── propagate_provenance conditional logic ───────────────────

    #[test]
    fn propagate_condition_both_none_skips() {
        let source_node_id: Option<i64> = None;
        let source_seed_id: Option<i64> = None;
        let should_propagate = source_node_id.is_some() || source_seed_id.is_some();
        assert!(!should_propagate);
    }

    #[test]
    fn propagate_condition_node_id_triggers() {
        let source_node_id: Option<i64> = Some(42);
        let source_seed_id: Option<i64> = None;
        let should_propagate = source_node_id.is_some() || source_seed_id.is_some();
        assert!(should_propagate);
    }

    #[test]
    fn propagate_condition_seed_id_triggers() {
        let source_node_id: Option<i64> = None;
        let source_seed_id: Option<i64> = Some(99);
        let should_propagate = source_node_id.is_some() || source_seed_id.is_some();
        assert!(should_propagate);
    }

    #[test]
    fn propagate_condition_both_set_triggers() {
        let source_node_id: Option<i64> = Some(1);
        let source_seed_id: Option<i64> = Some(2);
        let should_propagate = source_node_id.is_some() || source_seed_id.is_some();
        assert!(should_propagate);
    }

    // ── topic to Option conversion ───────────────────────────────

    #[test]
    fn empty_topic_becomes_none() {
        let topic = "";
        let opt: Option<String> = if topic.is_empty() {
            None
        } else {
            Some(topic.to_string())
        };
        assert!(opt.is_none());
    }

    #[test]
    fn nonempty_topic_becomes_some() {
        let topic = "rust programming";
        let opt: Option<String> = if topic.is_empty() {
            None
        } else {
            Some(topic.to_string())
        };
        assert_eq!(opt, Some("rust programming".to_string()));
    }

    // ── loopback URL construction ────────────────────────────────

    #[test]
    fn loopback_url_format() {
        let tweet_id = "1234567890";
        let url = format!("https://x.com/i/status/{tweet_id}");
        assert_eq!(url, "https://x.com/i/status/1234567890");
    }

    // ── delay edge cases ─────────────────────────────────────────

    #[test]
    fn delay_large_values() {
        let min = Duration::from_secs(60);
        let max = Duration::from_secs(300);
        for _ in 0..20 {
            let d = queue::randomized_delay(min, max);
            assert!(d >= min);
            assert!(d <= max);
        }
    }

    #[test]
    fn delay_subsecond() {
        let min = Duration::from_millis(1);
        let max = Duration::from_millis(10);
        for _ in 0..20 {
            let d = queue::randomized_delay(min, max);
            assert!(d >= min);
            assert!(d <= max);
        }
    }

    #[test]
    fn delay_is_zero_returns_true() {
        assert!(Duration::ZERO.is_zero());
        assert!(!Duration::from_millis(1).is_zero());
    }

    // ── action_type exhaustive routing ────────────────────────────

    #[test]
    fn action_type_all_variants() {
        for (action_type, target, expected_reply) in [
            ("reply", "12345", true),
            ("reply", "", false),
            ("tweet", "", false),
            ("tweet", "12345", false),
            ("thread_tweet", "12345", false),
            ("thread_tweet", "", false),
        ] {
            let is_reply = action_type == "reply" && !target.is_empty();
            assert_eq!(
                is_reply, expected_reply,
                "action={action_type}, target={target}"
            );
        }
    }

    // ── action log format all types ───────────────────────────────

    #[test]
    fn action_log_format_thread() {
        assert_eq!(format!("{}_posted", "thread_tweet"), "thread_tweet_posted");
    }

    // ── media_paths JSON edge cases ──────────────────────────────

    #[test]
    fn media_paths_nested_arrays_treated_as_invalid() {
        let json = r#"[["nested"]]"#;
        let paths: Vec<String> = serde_json::from_str(json).unwrap_or_default();
        assert!(paths.is_empty());
    }

    #[test]
    fn media_paths_single_item() {
        let json = r#"["/path/to/image.jpg"]"#;
        let paths: Vec<String> = serde_json::from_str(json).unwrap_or_default();
        assert_eq!(paths.len(), 1);
        assert_eq!(paths[0], "/path/to/image.jpg");
    }

    #[test]
    fn media_paths_many_items() {
        let json = r#"["/a.jpg", "/b.png", "/c.gif", "/d.mp4"]"#;
        let paths: Vec<String> = serde_json::from_str(json).unwrap_or_default();
        assert_eq!(paths.len(), 4);
    }

    // ── parse_thread_content ─────────────────────────────────────

    #[test]
    fn parse_thread_content_block_json() {
        use crate::content::{serialize_blocks_for_storage, ThreadBlock};

        let blocks = vec![
            ThreadBlock {
                id: "a".to_string(),
                text: "First tweet".to_string(),
                media_paths: vec![],
                order: 0,
            },
            ThreadBlock {
                id: "b".to_string(),
                text: "Second tweet".to_string(),
                media_paths: vec![],
                order: 1,
            },
        ];
        let content = serialize_blocks_for_storage(&blocks);
        let parsed = poster::parse_thread_content(&content).unwrap();
        assert_eq!(parsed, vec!["First tweet", "Second tweet"]);
    }

    #[test]
    fn parse_thread_content_legacy_string_array() {
        let content = r#"["Tweet one","Tweet two","Tweet three"]"#;
        let parsed = poster::parse_thread_content(content).unwrap();
        assert_eq!(parsed.len(), 3);
        assert_eq!(parsed[0], "Tweet one");
    }

    #[test]
    fn parse_thread_content_invalid_format() {
        let result = poster::parse_thread_content("just plain text");
        assert!(result.is_err());
    }

    #[test]
    fn parse_thread_content_empty_array() {
        let result = poster::parse_thread_content("[]");
        assert!(result.is_err());
    }

    #[test]
    fn parse_thread_content_blocks_sorted_by_order() {
        use crate::content::{serialize_blocks_for_storage, ThreadBlock};

        // Blocks with reversed order
        let blocks = vec![
            ThreadBlock {
                id: "b".to_string(),
                text: "Second".to_string(),
                media_paths: vec![],
                order: 1,
            },
            ThreadBlock {
                id: "a".to_string(),
                text: "First".to_string(),
                media_paths: vec![],
                order: 0,
            },
        ];
        let content = serialize_blocks_for_storage(&blocks);
        let parsed = poster::parse_thread_content(&content).unwrap();
        assert_eq!(parsed, vec!["First", "Second"]);
    }

    // ── action_type thread routing ───────────────────────────────

    #[test]
    fn action_type_thread_is_routed_separately() {
        // Thread items are handled by the thread-specific branch,
        // not the reply/tweet match.
        let action_type = "thread";
        let is_thread = action_type == "thread";
        assert!(is_thread);
    }

    // ── parse_thread_content additional edge cases ─────────────────

    #[test]
    fn parse_thread_content_single_tweet_array() {
        let content = r#"["Only tweet"]"#;
        let parsed = poster::parse_thread_content(content).unwrap();
        assert_eq!(parsed.len(), 1);
        assert_eq!(parsed[0], "Only tweet");
    }

    #[test]
    fn parse_thread_content_numeric_array_is_invalid() {
        let content = "[1, 2, 3]";
        let result = poster::parse_thread_content(content);
        assert!(result.is_err());
    }

    #[test]
    fn parse_thread_content_nested_json_is_invalid() {
        let content = r#"{"key": "value"}"#;
        let result = poster::parse_thread_content(content);
        assert!(result.is_err());
    }

    #[test]
    fn parse_thread_content_preserves_tweet_order() {
        let content = r#"["First","Second","Third","Fourth"]"#;
        let parsed = poster::parse_thread_content(content).unwrap();
        assert_eq!(parsed, vec!["First", "Second", "Third", "Fourth"]);
    }

    #[test]
    fn parse_thread_content_empty_string() {
        let result = poster::parse_thread_content("");
        assert!(result.is_err());
    }

    #[test]
    fn parse_thread_content_whitespace_only() {
        let result = poster::parse_thread_content("   ");
        assert!(result.is_err());
    }

    // ── tweet URL construction ────────────────────────────────────

    #[test]
    fn loopback_url_format_long_id() {
        let tweet_id = "1234567890123456789";
        let url = format!("https://x.com/i/status/{tweet_id}");
        assert_eq!(url, "https://x.com/i/status/1234567890123456789");
    }

    // ── child_tweet_ids extraction ────────────────────────────────

    #[test]
    fn child_ids_from_posted_ids() {
        let posted_ids = vec![
            "root".to_string(),
            "child1".to_string(),
            "child2".to_string(),
        ];
        let child_ids: Vec<String> = posted_ids.iter().skip(1).cloned().collect();
        assert_eq!(child_ids, vec!["child1", "child2"]);
    }

    #[test]
    fn child_ids_single_tweet_no_children() {
        let posted_ids = vec!["root".to_string()];
        let child_ids: Vec<String> = posted_ids.iter().skip(1).cloned().collect();
        assert!(child_ids.is_empty());
    }

    // ── topic normalization ───────────────────────────────────────

    #[test]
    fn empty_topic_uses_fallback() {
        let topic = "";
        let effective = if topic.is_empty() { "" } else { topic };
        assert_eq!(effective, "");
    }

    #[test]
    fn nonempty_topic_used_directly() {
        let topic = "rust async";
        let effective = if topic.is_empty() { "" } else { topic };
        assert_eq!(effective, "rust async");
    }

    // ── parse_thread_content additional edge cases ─────────────────

    #[test]
    fn parse_thread_content_single_tweet_array_v2() {
        let content = r#"["Only tweet"]"#;
        let parsed = poster::parse_thread_content(content).unwrap();
        assert_eq!(parsed.len(), 1);
        assert_eq!(parsed[0], "Only tweet");
    }
}