Skip to main content

max / balanced_breakfast

2.8 KB · 91 lines History Blame Raw
1 //! Shared helpers for integration tests.
2
3 #![allow(dead_code)]
4
5 use bb_core::orchestrator::{Orchestrator, OrchestratorConfig};
6 use bb_db::{BusserId, CreateFeedItem, CreateFeed, Database};
7 use chrono::{Duration, Utc};
8 use std::env::temp_dir;
9
10 /// Build an `OrchestratorConfig` that uses in-memory SQLite and an empty
11 /// temp directory for plugins.
12 pub fn test_config(suffix: &str) -> OrchestratorConfig {
13 let dir = temp_dir().join(format!("bb_cmd_test_{}", suffix));
14 let _ = std::fs::create_dir_all(&dir);
15 OrchestratorConfig {
16 database_url: "sqlite::memory:".to_string(),
17 plugins_dir: dir.to_string_lossy().to_string(),
18 fetch_interval_secs: 300,
19 }
20 }
21
22 /// Create an Orchestrator with in-memory DB and run migrations.
23 pub async fn setup(suffix: &str) -> Orchestrator {
24 let config = test_config(suffix);
25 let orchestrator = Orchestrator::new(config).await.unwrap();
26 orchestrator.migrate().await.unwrap();
27 orchestrator
28 }
29
30 /// Create a standalone in-memory Database and run migrations.
31 pub async fn test_db() -> Database {
32 let db = Database::connect("sqlite::memory:").await.unwrap();
33 db.migrate().await.unwrap();
34 db
35 }
36
37 /// Insert an RSS feed with the given name and URL config.
38 pub async fn create_rss_feed(db: &Database, name: &str, feed_url: &str) -> bb_db::DbFeed {
39 db.feeds()
40 .create(CreateFeed {
41 busser_id: BusserId::new("rss"),
42 name: name.to_string(),
43 config: serde_json::json!({ "feed_url": feed_url }),
44 })
45 .await
46 .unwrap()
47 }
48
49 /// Insert a non-RSS feed (e.g. "hn").
50 pub async fn create_other_feed(db: &Database, busser_id: &str, name: &str) -> bb_db::DbFeed {
51 db.feeds()
52 .create(CreateFeed {
53 busser_id: BusserId::new(busser_id),
54 name: name.to_string(),
55 config: serde_json::json!({}),
56 })
57 .await
58 .unwrap()
59 }
60
61 /// Insert a feed item attached to a feed.
62 pub async fn insert_item(
63 db: &Database,
64 feed: &bb_db::DbFeed,
65 external_id: &str,
66 title: &str,
67 hours_ago: i64,
68 ) -> bb_db::DbFeedItem {
69 db.items()
70 .upsert(CreateFeedItem {
71 external_id: external_id.to_string(),
72 feed_id: feed.id,
73 busser_id: feed.busser_id.clone(),
74 bite_author: "author".to_string(),
75 bite_text: format!("Item {external_id}"),
76 bite_secondary: None,
77 bite_indicator: None,
78 title: Some(title.to_string()),
79 body: Some(format!("Body of {title}")),
80 url: Some(format!("https://example.com/{external_id}")),
81 media: vec![],
82 published_at: Utc::now() - Duration::hours(hours_ago),
83 source_name: "test".to_string(),
84 score: None,
85 tags: vec![],
86 actions: vec![],
87 })
88 .await
89 .unwrap()
90 }
91