notedthat_api_http/
state.rs1use notedthat_core::{AccessPolicy, Authenticator, EventPublisher, EventSource, 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 pub events: Option<Arc<dyn EventPublisher>>,
36}
37
38impl AppState {
39 pub(crate) fn sinks(&self, source: EventSource) -> notedthat_write::WriteSinks<'_> {
42 notedthat_write::WriteSinks::new(&self.indexer_tx, self.events.as_deref(), source)
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49 use crate::testing::InMemoryStorage;
50 use std::collections::BTreeMap;
51 use std::sync::Arc;
52
53 fn minimal_state(tx: tokio::sync::mpsc::Sender<notedthat_indexer::IndexEvent>) -> AppState {
54 AppState {
55 storage: Arc::new(InMemoryStorage::default()),
56 declared_kbs: Arc::new(BTreeMap::new()),
57 access_policies: Arc::new(BTreeMap::new()),
58 authenticator: Arc::new(Authenticator::new("token")),
59 max_body_size: 1024,
60 max_patchable_size: 1024,
61 indexer_tx: tx,
62 searcher: Arc::new(crate::testing::NoopSearcher),
63 events: None,
64 }
65 }
66
67 #[tokio::test]
68 async fn clone_shares_same_channel() {
69 let (tx, mut rx) = tokio::sync::mpsc::channel(10);
70 let state = minimal_state(tx);
71 let cloned = state.clone();
72
73 let event = notedthat_indexer::IndexEvent::Tombstone {
74 kb: notedthat_core::KbSlug::try_new("test").expect("valid kb slug"),
75 object_key: notedthat_core::ObjectPath::try_from("a.md").expect("valid path"),
76 };
77 cloned
78 .indexer_tx
79 .send(event.clone())
80 .await
81 .expect("send on cloned tx");
82
83 let received = rx.recv().await.expect("receive on original rx");
84 assert_eq!(
85 received, event,
86 "cloned Sender must share the same underlying channel"
87 );
88 }
89}