mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-09-24 23:42:18 +00:00
test: share the photo payload fixture, drop two test-only aliases
The one-photo MediaItemPayload literal was written out at six sites across the urls and send test modules; ctx::test_support::photo_item holds it once. permanent_error now delegates to api_error (it stays a fn pointer because that is what MockSender::scripted takes), misskey's x_media_site_id wrapper is gone in favour of the function it renamed, and PixivAPI is no longer re-exported -- nothing outside pixiv/api.rs names it.
This commit is contained in:
@@ -220,11 +220,7 @@ mod tests {
|
||||
cache_key("https://misskey.io/notes/aotihl10lqrs015s"),
|
||||
Some("misskey:aotihl10lqrs015s".to_string())
|
||||
);
|
||||
assert_eq!(x_media_site_id("misskey:abc"), "misskey");
|
||||
}
|
||||
|
||||
fn x_media_site_id(key: &str) -> &'static str {
|
||||
crate::site::site_id_from_key(key)
|
||||
assert_eq!(crate::site::site_id_from_key("misskey:abc"), "misskey");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -2,7 +2,7 @@ mod api;
|
||||
mod interface;
|
||||
mod model;
|
||||
|
||||
pub use api::{PixivAPI, PixivError, disable, fetch, validate};
|
||||
pub use api::{PixivError, disable, fetch, validate};
|
||||
pub use interface::{
|
||||
Illustration, PATTERN, PixivSite, cache_key, enabled, fetch_from_url, is_retryable,
|
||||
media_headers,
|
||||
|
||||
@@ -50,6 +50,7 @@ pub static CONTEXT: LazyLock<AppContext<'static>> =
|
||||
pub(crate) mod test_support {
|
||||
use super::*;
|
||||
use crate::link_cache::{CachedMedia, CachedMediaKind, CachedPost};
|
||||
use crate::send::MediaItemPayload;
|
||||
use crate::state::EditMessage;
|
||||
use std::sync::Arc;
|
||||
use teloxide::{ApiError, RequestError};
|
||||
@@ -64,6 +65,18 @@ pub(crate) mod test_support {
|
||||
RequestError::Api(ApiError::Unknown(message.to_string()))
|
||||
}
|
||||
|
||||
/// One photo payload item: `media` in the two flags the tests vary (no
|
||||
/// smaller variant, since that is the field most tests leave alone —
|
||||
/// `send`'s own tests build that case directly).
|
||||
pub(crate) fn photo_item(media: &str, has_spoiler: bool, file_id: bool) -> MediaItemPayload {
|
||||
MediaItemPayload::Photo {
|
||||
media: media.to_string(),
|
||||
has_spoiler,
|
||||
fallback_url: None,
|
||||
file_id,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
|
||||
@@ -960,15 +960,16 @@ pub(crate) async fn repair_lost_local_media(ctx: &AppContext<'_>) -> usize {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ctx::test_support::{TestStores, cached_photo};
|
||||
use crate::ctx::test_support::{TestStores, api_error, cached_photo, photo_item};
|
||||
use crate::media_sender::test_support::{MockSender, Outcome};
|
||||
use std::time::Duration;
|
||||
use teloxide::{ApiError, RequestError};
|
||||
use teloxide::RequestError;
|
||||
|
||||
/// The API error a caption edit that changes nothing answers with — what
|
||||
/// the mocks script for a permanent send failure. A `fn` pointer, so it
|
||||
/// can be handed to `MockSender::scripted` as-is.
|
||||
fn permanent_error() -> RequestError {
|
||||
RequestError::Api(ApiError::Unknown(
|
||||
"Bad Request: message is not modified".into(),
|
||||
))
|
||||
api_error("Bad Request: message is not modified")
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -1381,12 +1382,7 @@ mod tests {
|
||||
chat_id: 1,
|
||||
reply_to_message_id: 2,
|
||||
caption: "cap".into(),
|
||||
media_batches: vec![vec![MediaItemPayload::Photo {
|
||||
media: media.to_string(),
|
||||
has_spoiler: false,
|
||||
fallback_url: None,
|
||||
file_id: false,
|
||||
}]],
|
||||
media_batches: vec![vec![photo_item(media, false, false)]],
|
||||
batch_index,
|
||||
sent_message_ids: sent,
|
||||
source_url: "https://x.com/u/status/1".into(),
|
||||
@@ -1443,12 +1439,7 @@ mod tests {
|
||||
let task = queued_task("/nonexistent-ugoira.mp4", 0, vec![]);
|
||||
let fresh = Refetched {
|
||||
caption: "fresh caption".into(),
|
||||
items: vec![MediaItemPayload::Photo {
|
||||
media: "https://cdn/fresh.jpg".into(),
|
||||
has_spoiler: true,
|
||||
fallback_url: None,
|
||||
file_id: false,
|
||||
}],
|
||||
items: vec![photo_item("https://cdn/fresh.jpg", true, false)],
|
||||
cache_data: None,
|
||||
};
|
||||
match apply_refresh(&task, &fresh).expect("a repairable task") {
|
||||
|
||||
@@ -856,7 +856,7 @@ mod tests {
|
||||
use super::post_send::{build_edit_markup, cache_sent_task};
|
||||
use super::upload::sniff_ext;
|
||||
use super::*;
|
||||
use crate::ctx::test_support::{TestStores, cached_photo};
|
||||
use crate::ctx::test_support::{TestStores, cached_photo, photo_item};
|
||||
use std::collections::HashMap;
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -1231,12 +1231,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn media_item_payload_serde_tags() {
|
||||
let photo = MediaItemPayload::Photo {
|
||||
media: "https://a/b.jpg".into(),
|
||||
has_spoiler: false,
|
||||
fallback_url: None,
|
||||
file_id: false,
|
||||
};
|
||||
let photo = photo_item("https://a/b.jpg", false, false);
|
||||
let json = serde_json::to_value(&photo).unwrap();
|
||||
assert_eq!(json["kind"], "photo");
|
||||
}
|
||||
@@ -1272,12 +1267,7 @@ mod tests {
|
||||
chat_id: 1,
|
||||
reply_to_message_id: 2,
|
||||
caption: caption.into(),
|
||||
media_batches: vec![vec![MediaItemPayload::Photo {
|
||||
media: media.to_string(),
|
||||
has_spoiler: false,
|
||||
fallback_url: None,
|
||||
file_id: false,
|
||||
}]],
|
||||
media_batches: vec![vec![photo_item(media, false, false)]],
|
||||
batch_index: 0,
|
||||
sent_message_ids: vec![],
|
||||
source_url: "https://x.com/u/status/1".into(),
|
||||
@@ -1670,12 +1660,7 @@ mod tests {
|
||||
chat_id: 1,
|
||||
reply_to_message_id: 2,
|
||||
caption: "cap".into(),
|
||||
media_batches: vec![vec![MediaItemPayload::Photo {
|
||||
media: "https://p/1.jpg".into(),
|
||||
has_spoiler: false,
|
||||
fallback_url: None,
|
||||
file_id: false,
|
||||
}]],
|
||||
media_batches: vec![vec![photo_item("https://p/1.jpg", false, false)]],
|
||||
batch_index: 0,
|
||||
sent_message_ids: vec![],
|
||||
source_url: "https://x.com/u/status/1".into(),
|
||||
@@ -1694,12 +1679,7 @@ mod tests {
|
||||
chat_id: 1,
|
||||
reply_to_message_id: 2,
|
||||
caption: "cap".into(),
|
||||
media_batches: vec![vec![MediaItemPayload::Photo {
|
||||
media: "AgAC-file-id".into(),
|
||||
has_spoiler: false,
|
||||
fallback_url: None,
|
||||
file_id: true,
|
||||
}]],
|
||||
media_batches: vec![vec![photo_item("AgAC-file-id", false, true)]],
|
||||
batch_index: 0,
|
||||
sent_message_ids: vec![],
|
||||
source_url: "https://x.com/u/status/1".into(),
|
||||
|
||||
Reference in New Issue
Block a user