notedthat_api_http/
state.rs1use notedthat_core::{AccessPolicy, Authenticator, 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 access_policies: Arc<BTreeMap<String, Arc<AccessPolicy>>>,
22 pub authenticator: Arc<Authenticator>,
24 pub max_body_size: u64,
26 pub max_patchable_size: u64,
28 pub indexer_tx: tokio::sync::mpsc::Sender<notedthat_indexer::IndexEvent>,
30 pub searcher: Arc<dyn Searcher>,
32}
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37 use crate::testing::InMemoryStorage;
38 use std::collections::BTreeMap;
39 use std::sync::Arc;
40
41 fn minimal_state(tx: tokio::sync::mpsc::Sender<notedthat_indexer::IndexEvent>) -> AppState {
42 AppState {
43 storage: Arc::new(InMemoryStorage::default()),
44 declared_kbs: Arc::new(BTreeMap::new()),
45 access_policies: Arc::new(BTreeMap::new()),
46 authenticator: Arc::new(Authenticator::new("token")),
47 max_body_size: 1024,
48 max_patchable_size: 1024,
49 indexer_tx: tx,
50 searcher: Arc::new(crate::testing::NoopSearcher),
51 }
52 }
53
54 #[tokio::test]
55 async fn clone_shares_same_channel() {
56 let (tx, mut rx) = tokio::sync::mpsc::channel(10);
57 let state = minimal_state(tx);
58 let cloned = state.clone();
59
60 let event = notedthat_indexer::IndexEvent::Tombstone {
61 kb: notedthat_core::KbSlug::try_new("test").expect("valid kb slug"),
62 object_key: notedthat_core::ObjectPath::try_from("a.md").expect("valid path"),
63 };
64 cloned
65 .indexer_tx
66 .send(event.clone())
67 .await
68 .expect("send on cloned tx");
69
70 let received = rx.recv().await.expect("receive on original rx");
71 assert_eq!(
72 received, event,
73 "cloned Sender must share the same underlying channel"
74 );
75 }
76}