BlueGit

ict rusty-teams

Files

rusty-teams / crates / store / tests / agents.rs

agents.rs

agents.rs 3.3 KiB Download Raw
1use chrono::{DateTime, TimeZone, Utc};
2use store::{AgentProposalRecord, MessageRecord, Store};
3
4fn at(minute: u32) -> DateTime<Utc> {
5 Utc.with_ymd_and_hms(2026, 10, 6, 9, minute, 0).unwrap()
6}
7
8fn message(conversation_id: &str, message_id: &str, minute: u32, html: &str) -> MessageRecord {
9 MessageRecord {
10 conversation_id: conversation_id.to_owned(),
11 message_id: message_id.to_owned(),
12 sender_id: Some("user-ada".to_owned()),
13 sender_name: Some("Ada Example".to_owned()),
14 created_at: at(minute),
15 body_html: html.to_owned(),
16 attachments_json: "[]".to_owned(),
17 reactions_json: "[]".to_owned(),
18 mentions_json: "[]".to_owned(),
19 links_json: "[]".to_owned(),
20 ..MessageRecord::default()
21 }
22}
23
24fn proposal(id: &str, conversation_id: &str, minute: u32) -> AgentProposalRecord {
25 AgentProposalRecord {
26 id: id.to_owned(),
27 conversation_id: conversation_id.to_owned(),
28 reply_to_message_id: None,
29 text: format!("text {id}"),
30 agent: Some("claude".to_owned()),
31 session_id: None,
32 created_at: at(minute),
33 state: "pending".to_owned(),
34 resolved_at: None,
35 }
36}
37
38#[test]
39fn search_in_filters_before_the_limit_applies() {
40 let store = Store::open_in_memory().unwrap();
41 store
42 .upsert_messages(&[
43 message("c1", "m1", 1, "<p>budget one</p>"),
44 message("c2", "m2", 2, "<p>budget two</p>"),
45 message("c2", "m3", 3, "<p>budget three</p>"),
46 ])
47 .unwrap();
48 let hits = store
49 .search_messages_in("budget", &["c1".to_owned()], 1)
50 .unwrap();
51 assert_eq!(hits.len(), 1);
52 assert_eq!(hits[0].message_id, "m1");
53 assert!(
54 store
55 .search_messages_in("budget", &[], 10)
56 .unwrap()
57 .is_empty()
58 );
59}
60
61#[test]
62fn messages_after_pages_forward_oldest_first() {
63 let store = Store::open_in_memory().unwrap();
64 store
65 .upsert_messages(&[
66 message("c1", "m1", 1, "<p>a</p>"),
67 message("c1", "m2", 2, "<p>b</p>"),
68 message("c1", "m3", 3, "<p>c</p>"),
69 message("c2", "m4", 4, "<p>d</p>"),
70 ])
71 .unwrap();
72 let page = store.messages_after("c1", at(1), 1).unwrap();
73 assert_eq!(page.len(), 1);
74 assert_eq!(page[0].message_id, "m2");
75 assert_eq!(store.messages_after("c1", at(1), 10).unwrap().len(), 2);
76}
77
78#[test]
79fn proposals_persist_and_only_pending_ones_are_listed() {
80 let store = Store::open_in_memory().unwrap();
81 store
82 .insert_agent_proposal(&proposal("p2", "c1", 2))
83 .unwrap();
84 store
85 .insert_agent_proposal(&proposal("p1", "c2", 1))
86 .unwrap();
87 let pending = store.pending_agent_proposals().unwrap();
88 assert_eq!(
89 pending
90 .iter()
91 .map(|record| record.id.as_str())
92 .collect::<Vec<_>>(),
93 ["p1", "p2"]
94 );
95 assert_eq!(pending[0], proposal("p1", "c2", 1));
96
97 assert!(
98 store
99 .set_agent_proposal_state("p1", "sent", Some(at(5)))
100 .unwrap()
101 );
102 assert!(
103 !store
104 .set_agent_proposal_state("nope", "sent", None)
105 .unwrap()
106 );
107 let pending = store.pending_agent_proposals().unwrap();
108 assert_eq!(pending.len(), 1);
109 assert_eq!(pending[0].id, "p2");
110}