mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-10-06 01:22:16 +00:00
test: share the handler/cache fixtures from ctx::test_support
The same fixtures were rebuilt in five test modules: a `CachedPost` literal in `link_cache.rs`, `handlers/urls.rs` and twice in `send/mod.rs`, the edit-before-forward prompt in `handlers/mod.rs` and `handlers/callback.rs`, and a scripted API error in both handler modules. They now live in `ctx::test_support` next to `TestStores`: - `cached_photo()` — the canonical cached post (photo + file id at `https://x.com/u/status/1`, key `twitter:1`); tests mutate the fields they care about, as the caption-quote test already did. - `seed_prompt(template, created_at)` + `PROMPT_ID`/`FORWARDED_ID` — the prompt record, the chat template and the bound forward channel. The two former copies differed only in which knob the caller set (the callback tests backdate it for the expiry cases, the reply tests pick the template), so the union is one helper. - `api_error(message)` — construction only; each test module keeps its own message constant, because the wording is what that module's path answers with (`chat not found` vs `message not found`). `send/mod.rs`'s `cached_sequence_cache_data()` (which re-extracted the post out of the task it had just built) is gone: the two settle tests seed the cache from the same builder the task uses. No behaviour change: the values are the ones the tests used except `file_id` (`AgAC-file-id` everywhere, asserted in the link-cache round-trip) and `sensitive` (the unasserted `true` in the link-cache fixture), and every test still passes unchanged. Verified: `cargo fmt`, `cargo clippy --workspace --all-targets --locked -- -D warnings` and `cargo test --workspace --locked` (180 passed, 14 ignored).
This commit is contained in:
@@ -49,7 +49,66 @@ pub static CONTEXT: LazyLock<AppContext<'static>> =
|
||||
#[cfg(test)]
|
||||
pub(crate) mod test_support {
|
||||
use super::*;
|
||||
use crate::link_cache::{CachedMedia, CachedMediaKind, CachedPost};
|
||||
use crate::state::EditMessage;
|
||||
use std::sync::Arc;
|
||||
use teloxide::{ApiError, RequestError};
|
||||
|
||||
/// The edit-before-forward prompt's message id, and the message the prompt
|
||||
/// refers to (the one whose caption a reply swaps).
|
||||
pub(crate) const PROMPT_ID: i64 = 7;
|
||||
pub(crate) const FORWARDED_ID: i64 = 9;
|
||||
|
||||
/// A Telegram API error, for the tests that script a failure.
|
||||
pub(crate) fn api_error(message: &str) -> RequestError {
|
||||
RequestError::Api(ApiError::Unknown(message.to_string()))
|
||||
}
|
||||
|
||||
/// The cached post every test that touches the link cache starts from: one
|
||||
/// photo with a Telegram file id at the canonical URL (key `twitter:1`).
|
||||
/// Tests that need another field mutate the returned value.
|
||||
pub(crate) fn cached_photo() -> CachedPost {
|
||||
CachedPost {
|
||||
url: "https://x.com/u/status/1".into(),
|
||||
caption: "cap".into(),
|
||||
title: "t".into(),
|
||||
content: "c".into(),
|
||||
author: "a".into(),
|
||||
author_url: "au".into(),
|
||||
tags: String::new(),
|
||||
sensitive: false,
|
||||
media: vec![CachedMedia {
|
||||
kind: CachedMediaKind::Photo,
|
||||
file_id: "AgAC-file-id".into(),
|
||||
}],
|
||||
}
|
||||
}
|
||||
|
||||
/// Seeds the live prompt a post-send leaves behind in chat 1: the chat's
|
||||
/// template, a bound forward channel (the prompt's "forward" button
|
||||
/// branches on it) and the record for [`PROMPT_ID`] pointing at
|
||||
/// [`FORWARDED_ID`]. `template` is the record's template — what a reply
|
||||
/// swaps the caption through, `""` for none — and `created_at` backdates
|
||||
/// the record for the expiry cases.
|
||||
pub(crate) async fn seed_prompt(ctx: &AppContext<'_>, template: &str, created_at: i64) {
|
||||
ctx.chat_store
|
||||
.update(1, |data| {
|
||||
data.forward_channel_id = Some(2);
|
||||
data.template
|
||||
.insert("tpl".to_string(), "<b>[]</b>".to_string());
|
||||
data.edit_message.insert(
|
||||
PROMPT_ID,
|
||||
EditMessage {
|
||||
url: "https://x.com/u/status/1".into(),
|
||||
chat_id: 1,
|
||||
forward_message_ids: vec![FORWARDED_ID],
|
||||
template: template.to_string(),
|
||||
created_at,
|
||||
},
|
||||
);
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
pub(crate) struct TestStores {
|
||||
_dir: tempfile::TempDir,
|
||||
|
||||
Reference in New Issue
Block a user