notedthat_api_http/
state.rs1use notedthat_core::{KbSlug, Storage};
4use notedthat_indexer::Searcher;
5use std::collections::BTreeMap;
6use std::sync::Arc;
7
8#[derive(Clone)]
12pub struct AppState {
13 pub storage: Arc<dyn Storage>,
15 pub declared_kbs: Arc<BTreeMap<String, KbSlug>>,
17 pub bearer_token: Arc<String>,
19 pub max_body_size: u64,
21 pub max_patchable_size: u64,
23 pub indexer_tx: tokio::sync::mpsc::Sender<notedthat_indexer::IndexEvent>,
25 pub searcher: Arc<dyn Searcher>,
27}
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32 use crate::testing::InMemoryStorage;
33 use std::collections::BTreeMap;
34 use std::sync::Arc;
35
36 fn minimal_state(tx: tokio::sync::mpsc::Sender<notedthat_indexer::IndexEvent>) -> AppState {
37 AppState {
38 storage: Arc::new(InMemoryStorage::default()),
39 declared_kbs: Arc::new(BTreeMap::new()),
40 bearer_token: Arc::new("token".to_string()),
41 max_body_size: 1024,
42 max_patchable_size: 1024,
43 indexer_tx: tx,
44 searcher: Arc::new(crate::testing::NoopSearcher),
45 }
46 }
47
48 #[tokio::test]
49 async fn clone_shares_same_channel() {
50 let (tx, mut rx) = tokio::sync::mpsc::channel(10);
51 let state = minimal_state(tx);
52 let cloned = state.clone();
53
54 let event = notedthat_indexer::IndexEvent::Tombstone {
55 kb: notedthat_core::KbSlug::try_new("test").expect("valid kb slug"),
56 object_key: notedthat_core::ObjectPath::try_from("a.md").expect("valid path"),
57 };
58 cloned
59 .indexer_tx
60 .send(event.clone())
61 .await
62 .expect("send on cloned tx");
63
64 let received = rx.recv().await.expect("receive on original rx");
65 assert_eq!(
66 received, event,
67 "cloned Sender must share the same underlying channel"
68 );
69 }
70}