use std::pin::Pin;
use std::sync::Arc;
use std::time::{Duration, Instant, UNIX_EPOCH};
use futures_core::Stream;
use serde::{Deserialize, Serialize};
use crate::aio::Store;
use crate::{Options, TestClock};
pub(super) const SECOND: Duration = Duration::from_secs(1);
pub(super) const MINUTE: Duration = Duration::from_secs(60);
pub(super) const HOUR: Duration = Duration::from_secs(60 * 60);
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub(super) struct Draft {
pub(super) chat: i64,
pub(super) text: String,
}
pub(super) fn draft(chat: i64) -> Draft {
Draft { chat, text: format!("a draft for chat {chat}") }
}
pub(super) struct Fixture {
pub(super) _dir: tempfile::TempDir,
pub(super) store: Store,
pub(super) clock: Arc<TestClock>,
}
pub(super) fn fixture() -> Fixture {
let dir = tempfile::tempdir().unwrap();
let clock = Arc::new(TestClock::new(UNIX_EPOCH + Duration::from_secs(1_791_547_200)));
let options = Options { clock: Some(clock.clone()), background: false, ..Options::default() };
let store = Store::open(dir.path(), options).unwrap();
Fixture { _dir: dir, store, clock }
}
impl Fixture {
pub(super) fn advance(&self, by: Duration) {
self.clock.advance(by);
self.store.blocking().clock_moved();
}
}
pub(super) async fn next<S: Stream + Unpin>(stream: &mut S) -> Option<S::Item> {
std::future::poll_fn(|context| Pin::new(&mut *stream).poll_next(context)).await
}
pub(super) async fn eventually(what: &str, mut holds: impl FnMut() -> bool) {
let deadline = Instant::now() + crate::testing::PATIENCE;
while !holds() {
assert!(Instant::now() < deadline, "{what}: not within {:?}", crate::testing::PATIENCE);
tokio::time::sleep(Duration::from_millis(2)).await;
}
}