mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-09-23 23:32:05 +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:
@@ -622,8 +622,7 @@ async fn url_media_inner(
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ctx::test_support::TestStores;
|
||||
use crate::link_cache::CachedMedia;
|
||||
use crate::ctx::test_support::{TestStores, cached_photo};
|
||||
use crate::media_sender::test_support::{MockSender, Outcome};
|
||||
use std::time::Duration;
|
||||
use teloxide::{ApiError, RequestError};
|
||||
@@ -634,23 +633,6 @@ mod tests {
|
||||
))
|
||||
}
|
||||
|
||||
fn cached_photo_entry() -> 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: "".into(),
|
||||
sensitive: false,
|
||||
media: vec![CachedMedia {
|
||||
kind: CachedMediaKind::Photo,
|
||||
file_id: "file-1".into(),
|
||||
}],
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cache_hit_sends_file_ids_and_invalidates_on_permanent_failure() {
|
||||
let stores = TestStores::new();
|
||||
@@ -659,10 +641,7 @@ mod tests {
|
||||
permanent_error,
|
||||
);
|
||||
let ctx = stores.ctx(&sender);
|
||||
stores
|
||||
.link_cache()
|
||||
.put("twitter:1", &cached_photo_entry())
|
||||
.await;
|
||||
stores.link_cache().put("twitter:1", &cached_photo()).await;
|
||||
|
||||
url_media(&ctx, 1, 2, "https://x.com/u/status/1", PostSend::FromChat).await;
|
||||
|
||||
@@ -699,7 +678,7 @@ mod tests {
|
||||
] {
|
||||
let sender = MockSender::scripted(vec![Outcome::GroupOk], permanent_error);
|
||||
let ctx = stores.ctx(&sender);
|
||||
let mut entry = cached_photo_entry();
|
||||
let mut entry = cached_photo();
|
||||
entry.caption = format!("{prefix}{text}");
|
||||
entry.title = String::new();
|
||||
entry.content = text.into();
|
||||
@@ -748,10 +727,7 @@ mod tests {
|
||||
let sender =
|
||||
MockSender::scripted(vec![Outcome::GroupOk, Outcome::MessageOk], permanent_error);
|
||||
let ctx = stores.ctx(&sender);
|
||||
stores
|
||||
.link_cache()
|
||||
.put("twitter:1", &cached_photo_entry())
|
||||
.await;
|
||||
stores.link_cache().put("twitter:1", &cached_photo()).await;
|
||||
seed_post_send_settings(&ctx).await;
|
||||
|
||||
url_media(&ctx, 1, 2, "https://x.com/u/status/1", PostSend::FromChat).await;
|
||||
@@ -771,10 +747,7 @@ mod tests {
|
||||
// prompt (send_message) would panic with "unexpected outcome".
|
||||
let sender = MockSender::scripted(vec![Outcome::GroupOk], permanent_error);
|
||||
let ctx = stores.ctx(&sender);
|
||||
stores
|
||||
.link_cache()
|
||||
.put("twitter:1", &cached_photo_entry())
|
||||
.await;
|
||||
stores.link_cache().put("twitter:1", &cached_photo()).await;
|
||||
seed_post_send_settings(&ctx).await;
|
||||
|
||||
url_media(&ctx, 1, 2, "https://x.com/u/status/1", PostSend::Suppressed).await;
|
||||
|
||||
Reference in New Issue
Block a user