Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

style(notifications): reformat received_ms insert

Steven Enamakel877f5b7d05eb
diff --git a/crates/openhuman-core/src/desktop/notifications/store_documents.rs b/crates/openhuman-core/src/desktop/notifications/store_documents.rs
index 7b70652b96..f31eef78ca 100644
--- a/crates/openhuman-core/src/desktop/notifications/store_documents.rs
+++ b/crates/openhuman-core/src/desktop/notifications/store_documents.rs
@@ -88,7 +88,10 @@ fn to_doc(n: &IntegrationNotification) -> Value {
doc.insert("raw_payload".into(), json!(n.raw_payload.to_string()));
doc.insert("status".into(), json!(n.status.as_str()));
doc.insert("received_at".into(), json!(n.received_at.to_rfc3339()));
- doc.insert("received_ms".into(), json!(n.received_at.timestamp_millis()));
+ doc.insert(
+ "received_ms".into(),
+ json!(n.received_at.timestamp_millis()),
+ );
let optional = [
("account_id", n.account_id.as_ref().map(|v| json!(v))),
("importance_score", n.importance_score.map(|v| json!(v))),
diff --git a/crates/openhuman-core/src/desktop/notifications/store_documents_tests.rs b/crates/openhuman-core/src/desktop/notifications/store_documents_tests.rs
new file mode 100644
index 0000000000..e7911b61bb
--- /dev/null
+++ b/crates/openhuman-core/src/desktop/notifications/store_documents_tests.rs
@@ -0,0 +1,254 @@
+use super::*;
+use crate::desktop::notifications::types::CoreNotificationCategory;
+use crate::storage::{MemoryStorage, Scope, StorageBackend};
+
+fn docs_in(storage: &MemoryStorage, scope: &str) -> Docs {
+ Docs::over(&storage.for_scope(&Scope::new(scope).unwrap()).unwrap())
+}
+
+fn docs() -> Docs {
+ docs_in(&MemoryStorage::new(), "local")
+}
+
+fn note(id: &str, provider: &str, body: &str, secs_ago: i64) -> IntegrationNotification {
+ IntegrationNotification {
+ id: id.to_string(),
+ provider: provider.to_string(),
+ account_id: Some("acct".to_string()),
+ title: "New message".to_string(),
+ body: body.to_string(),
+ raw_payload: json!({ "k": 1 }),
+ importance_score: None,
+ triage_action: None,
+ triage_reason: None,
+ status: NotificationStatus::Unread,
+ received_at: Utc::now() - Duration::seconds(secs_ago),
+ scored_at: None,
+ }
+}
+
+fn event(id: &str, ts: u64) -> CoreNotificationEvent {
+ CoreNotificationEvent {
+ id: id.to_string(),
+ category: CoreNotificationCategory::Agents,
+ title: "Cron job completed".to_string(),
+ body: "done".to_string(),
+ deep_link: None,
+ timestamp_ms: ts,
+ actions: None,
+ workspace: None,
+ workspace_revision: None,
+ }
+}
+
+fn ids(list: &[IntegrationNotification]) -> Vec<&str> {
+ list.iter().map(|n| n.id.as_str()).collect()
+}
+
+#[test]
+fn notifications_round_trip_newest_first() {
+ let store = docs();
+ assert!(store.insert(&note("old", "slack", "a", 30), false).unwrap());
+ assert!(store.insert(&note("new", "gmail", "b", 0), false).unwrap());
+ let listed = store.list(10, 0, None, None).unwrap();
+ assert_eq!(ids(&listed), ["new", "old"]);
+ assert_eq!(listed[1].raw_payload, json!({ "k": 1 }));
+ assert_eq!(listed[1].account_id.as_deref(), Some("acct"));
+ assert!(listed[1].importance_score.is_none());
+ assert!(
+ store.insert(&note("old", "slack", "z", 0), false).is_err(),
+ "an id is inserted once"
+ );
+ assert_eq!(ids(&store.list(1, 1, None, None).unwrap()), ["old"]);
+ assert!(store.list(0, 0, None, None).unwrap().is_empty());
+ assert_eq!(
+ ids(&store.list(10, 0, Some("slack"), None).unwrap()),
+ ["old"]
+ );
+}
+
+#[test]
+fn identical_content_within_a_minute_is_skipped() {
+ let store = docs();
+ assert!(store.insert(&note("a", "slack", "hi", 0), true).unwrap());
+ assert!(!store.insert(&note("b", "slack", "hi", 0), true).unwrap());
+ assert!(store.insert(&note("c", "slack", "other", 0), true).unwrap());
+ assert!(store
+ .exists_recent("slack", Some("acct"), "New message", "hi")
+ .unwrap());
+ assert!(!store
+ .exists_recent("slack", None, "New message", "hi")
+ .unwrap());
+ assert!(!store
+ .exists_recent("gmail", Some("acct"), "New message", "hi")
+ .unwrap());
+ assert_eq!(ids(&store.list(10, 0, None, None).unwrap()).len(), 2);
+}
+
+#[test]
+fn content_older_than_the_window_is_not_a_duplicate() {
+ let store = docs();
+ store.insert(&note("a", "slack", "hi", 120), false).unwrap();
+ assert!(!store
+ .exists_recent("slack", Some("acct"), "New message", "hi")
+ .unwrap());
+ assert!(store.insert(&note("b", "slack", "hi", 0), true).unwrap());
+}
+
+#[test]
+fn concurrent_duplicates_insert_once() {
+ let store = docs();
+ let inserted: usize = (0..8)
+ .map(|i| {
+ let store = store.clone();
+ std::thread::spawn(move || {
+ store
+ .insert(&note(&format!("n{i}"), "slack", "same", 0), true)
+ .unwrap()
+ })
+ })
+ .collect::<Vec<_>>()
+ .into_iter()
+ .map(|handle| usize::from(handle.join().unwrap()))
+ .sum();
+ assert_eq!(inserted, 1);
+}
+
+#[test]
+fn triage_status_and_score_filter() {
+ let store = docs();
+ store.insert(&note("low", "slack", "a", 2), false).unwrap();
+ store.insert(&note("high", "slack", "b", 1), false).unwrap();
+ store
+ .insert(&note("unscored", "slack", "c", 0), false)
+ .unwrap();
+ assert!(store.update_triage("low", 0.2, "drop", "noise").unwrap());
+ assert!(store.update_triage("high", 0.9, "react", "urgent").unwrap());
+ assert!(!store.update_triage("missing", 0.5, "drop", "x").unwrap());
+ let filtered = store.list(10, 0, None, Some(0.5)).unwrap();
+ assert_eq!(ids(&filtered), ["unscored", "high"]);
+ assert_eq!(filtered[1].triage_action.as_deref(), Some("react"));
+ assert!(filtered[1].scored_at.is_some());
+
+ assert_eq!(store.unread_count().unwrap(), 3);
+ assert!(store.set_status("low", NotificationStatus::Read).unwrap());
+ assert!(store.set_status("high", NotificationStatus::Acted).unwrap());
+ assert!(!store
+ .set_status("missing", NotificationStatus::Dismissed)
+ .unwrap());
+ assert_eq!(store.unread_count().unwrap(), 1);
+
+ let stats = store.stats().unwrap();
+ assert_eq!((stats.total, stats.unread, stats.unscored), (3, 1, 1));
+ assert_eq!(stats.by_provider.get("slack"), Some(&3));
+ assert_eq!(stats.by_action.get("drop"), Some(&1));
+ assert_eq!(stats.by_action.get("react"), Some(&1));
+}
+
+#[test]
+fn settings_default_then_upsert() {
+ let store = docs();
+ let defaults = store.get_settings("slack").unwrap();
+ assert_eq!(defaults.provider, "slack");
+ let mut settings = defaults.clone();
+ settings.enabled = !defaults.enabled;
+ settings.importance_threshold = 0.25;
+ settings.route_to_orchestrator = !defaults.route_to_orchestrator;
+ store.upsert_settings(&settings).unwrap();
+ let read = store.get_settings("slack").unwrap();
+ assert_eq!(read.enabled, settings.enabled);
+ assert_eq!(read.importance_threshold, 0.25);
+ assert_eq!(read.route_to_orchestrator, settings.route_to_orchestrator);
+ assert_eq!(
+ store.get_settings("gmail").unwrap().enabled,
+ defaults.enabled
+ );
+}
+
+#[test]
+fn core_notifications_persist_once_and_mark_read() {
+ let store = docs();
+ assert!(store.insert_core_notification(&event("a", 1)).unwrap());
+ assert!(!store.insert_core_notification(&event("a", 1)).unwrap());
+ assert!(store.insert_core_notification(&event("b", 2)).unwrap());
+ let all = store.list_core_notifications(false, 10).unwrap();
+ assert_eq!(
+ all.iter().map(|e| e.id.as_str()).collect::<Vec<_>>(),
+ ["b", "a"]
+ );
+ assert_eq!(all[0], event("b", 2));
+ assert_eq!(store.unread_core_notification_count().unwrap(), 2);
+ assert!(store.mark_core_notification_read("b").unwrap());
+ assert!(
+ store.mark_core_notification_read("b").unwrap(),
+ "still exists"
+ );
+ assert!(!store.mark_core_notification_read("missing").unwrap());
+ let unread = store.list_core_notifications(true, 10).unwrap();
+ assert_eq!(
+ unread.iter().map(|e| e.id.as_str()).collect::<Vec<_>>(),
+ ["a"]
+ );
+ assert_eq!(store.unread_core_notification_count().unwrap(), 1);
+ assert!(store.list_core_notifications(false, 0).unwrap().is_empty());
+}
+
+#[test]
+fn a_corrupt_core_payload_is_skipped() {
+ let store = docs();
+ store.insert_core_notification(&event("good", 1)).unwrap();
+ store
+ .0
+ .run(|docs| async move {
+ docs.put(
+ CORE,
+ "bad",
+ json!({ "payload": "not json", "timestamp_ms": 2, "read": false }),
+ Precondition::Absent,
+ )
+ .await
+ .map(|_| ())
+ })
+ .unwrap();
+ let listed = store.list_core_notifications(false, 10).unwrap();
+ assert_eq!(listed.len(), 1);
+ assert_eq!(listed[0].id, "good");
+}
+
+#[test]
+fn an_unparseable_raw_payload_reads_as_a_string() {
+ let stored = Versioned {
+ id: "x".to_string(),
+ version: Default::default(),
+ doc: json!({ "raw_payload": "plain text", "status": "weird" }),
+ };
+ let read = to_notification(&stored);
+ assert_eq!(read.raw_payload, json!("plain text"));
+ assert_eq!(read.status, NotificationStatus::Unread);
+}
+
+#[test]
+fn scopes_keep_notifications_apart() {
+ let storage = MemoryStorage::new();
+ docs_in(&storage, "alice")
+ .insert(&note("a", "slack", "hi", 0), true)
+ .unwrap();
+ let bob = docs_in(&storage, "bob");
+ assert!(bob.list(10, 0, None, None).unwrap().is_empty());
+ assert!(!bob
+ .exists_recent("slack", Some("acct"), "New message", "hi")
+ .unwrap());
+ assert!(bob.insert(&note("a", "slack", "hi", 0), true).unwrap());
+}
+
+#[test]
+fn dedup_ids_do_not_collide_across_fields() {
+ assert_ne!(
+ dedup_id("a", None, "b", "c"),
+ dedup_id("a", Some(""), "b", "c")
+ );
+ assert_ne!(
+ dedup_id("ab", None, "c", "d"),
+ dedup_id("a", None, "bc", "d")
+ );
+}