use crate::channels::discord::DiscordState;
use crate::channels::discord::interactions::FormSpec;
use crate::channels::discord::pending_interactions::PENDING_INTERACTION_CAP;
const REGISTRATIONS: usize = 100;
#[tokio::test]
async fn a_hundred_unclicked_menus_leave_a_bounded_map() {
let state = DiscordState::new();
for i in 0..REGISTRATIONS {
state
.register_select(format!("select-{i}"), vec![format!("option-{i}")])
.await;
}
assert!(
state.take_select("select-0", 24.0).await.is_none(),
"the oldest unclicked menu must be evicted once the cap is passed"
);
let newest = format!("select-{}", REGISTRATIONS - 1);
assert!(
state.take_select(&newest, 24.0).await.is_some(),
"the newest menu must survive the prune"
);
let boundary = format!("select-{}", REGISTRATIONS - PENDING_INTERACTION_CAP);
assert!(
state.take_select(&boundary, 24.0).await.is_some(),
"the entry at the cap boundary must survive, got None for {boundary}"
);
}
#[tokio::test]
async fn a_hundred_unclicked_forms_leave_a_bounded_map() {
let state = DiscordState::new();
for i in 0..REGISTRATIONS {
state
.register_form(
format!("form-{i}"),
FormSpec {
title: format!("Form {i}"),
fields: vec![("answer".to_string(), false)],
},
)
.await;
}
assert!(
state.get_form("form-0", 24.0).await.is_none(),
"the oldest unclicked form must be evicted once the cap is passed"
);
let newest = format!("form-{}", REGISTRATIONS - 1);
assert!(
state.get_form(&newest, 24.0).await.is_some(),
"the newest form must survive the prune"
);
}
#[test]
fn both_registrations_prune_before_inserting() {
const SRC: &str = include_str!("../channels/discord/pending_interactions.rs");
for (name, anchor) in [
("register_select", "pub(crate) async fn register_select"),
("register_form", "pub(crate) async fn register_form"),
] {
let at = SRC
.find(anchor)
.unwrap_or_else(|| panic!("{name} is gone from the module"));
let body = &SRC[at..];
let body = &body[..body.find("\n }").unwrap_or(body.len())];
assert!(
body.contains("make_room(&mut map)"),
"{name} must prune before inserting, or the map is unbounded again"
);
}
assert!(
!SRC.contains("Bounded by lazy TTL expiry"),
"the module must not claim the TTL bounds the map"
);
}
#[test]
fn the_followup_select_is_registered_only_after_a_successful_send() {
const SRC: &str = include_str!("../channels/discord/suggest_options.rs");
let send = SRC
.find("super::governor::send_content(")
.expect("the send is gone from suggest_options");
let register = SRC
.find("state.register_select(")
.expect("the select registration is gone from suggest_options");
assert!(
send < register,
"registration must follow the send, or a failed send orphans an entry"
);
assert!(
SRC.contains("Ok(_) => state.register_select(followup_id, options).await"),
"the registration must be the success arm of the send"
);
}