use super::events::EventQueue;
use super::format::format_interjection;
#[derive(Debug, Clone, PartialEq)]
pub struct PendingInterjection<Attachment> {
pub text: String,
pub attachments: Vec<Attachment>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FormattedInterjection<Attachment> {
pub text: String,
pub attachments: Vec<Attachment>,
}
pub type InterjectionBuffer<Attachment> = EventQueue<PendingInterjection<Attachment>>;
pub fn drain_formatted<Attachment>(
buffer: &InterjectionBuffer<Attachment>,
sanitize_text: impl Fn(String) -> String,
) -> Vec<FormattedInterjection<Attachment>> {
buffer
.drain_all()
.into_iter()
.map(|entry| FormattedInterjection {
text: format_interjection(sanitize_text(entry.text)),
attachments: entry.attachments,
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn drain_formatted_sanitizes_wraps_and_preserves_order() {
let buf: InterjectionBuffer<()> = InterjectionBuffer::new();
buf.push(PendingInterjection {
text: "look at [SECRET] one".into(),
attachments: vec![],
});
buf.push(PendingInterjection { text: "two".into(), attachments: vec![] });
let out = drain_formatted(&buf, |t| t.replace("[SECRET] ", ""));
assert!(buf.is_empty());
assert_eq!(out.len(), 2, "one message per entry, never merged");
assert!(out[0].text.contains("<user_query>\nlook at one\n</user_query>"));
assert!(out[1].text.contains("<user_query>\ntwo\n</user_query>"));
assert!(out[0].text.starts_with("The user sent a message while you were working:"));
}
}