mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-10-04 01:02:12 +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"),
|
cache_key("https://misskey.io/notes/aotihl10lqrs015s"),
|
||||||
Some("misskey:aotihl10lqrs015s".to_string())
|
Some("misskey:aotihl10lqrs015s".to_string())
|
||||||
);
|
);
|
||||||
assert_eq!(x_media_site_id("misskey:abc"), "misskey");
|
assert_eq!(crate::site::site_id_from_key("misskey:abc"), "misskey");
|
||||||
}
|
|
||||||
|
|
||||||
fn x_media_site_id(key: &str) -> &'static str {
|
|
||||||
crate::site::site_id_from_key(key)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ mod api;
|
|||||||
mod interface;
|
mod interface;
|
||||||
mod model;
|
mod model;
|
||||||
|
|
||||||
pub use api::{PixivAPI, PixivError, disable, fetch, validate};
|
pub use api::{PixivError, disable, fetch, validate};
|
||||||
pub use interface::{
|
pub use interface::{
|
||||||
Illustration, PATTERN, PixivSite, cache_key, enabled, fetch_from_url, is_retryable,
|
Illustration, PATTERN, PixivSite, cache_key, enabled, fetch_from_url, is_retryable,
|
||||||
media_headers,
|
media_headers,
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ pub static CONTEXT: LazyLock<AppContext<'static>> =
|
|||||||
pub(crate) mod test_support {
|
pub(crate) mod test_support {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::link_cache::{CachedMedia, CachedMediaKind, CachedPost};
|
use crate::link_cache::{CachedMedia, CachedMediaKind, CachedPost};
|
||||||
|
use crate::send::MediaItemPayload;
|
||||||
use crate::state::EditMessage;
|
use crate::state::EditMessage;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use teloxide::{ApiError, RequestError};
|
use teloxide::{ApiError, RequestError};
|
||||||
@@ -64,6 +65,18 @@ pub(crate) mod test_support {
|
|||||||
RequestError::Api(ApiError::Unknown(message.to_string()))
|
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
|
/// 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`).
|
/// photo with a Telegram file id at the canonical URL (key `twitter:1`).
|
||||||
/// Tests that need another field mutate the returned value.
|
/// 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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
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 crate::media_sender::test_support::{MockSender, Outcome};
|
||||||
use std::time::Duration;
|
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 {
|
fn permanent_error() -> RequestError {
|
||||||
RequestError::Api(ApiError::Unknown(
|
api_error("Bad Request: message is not modified")
|
||||||
"Bad Request: message is not modified".into(),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -1381,12 +1382,7 @@ mod tests {
|
|||||||
chat_id: 1,
|
chat_id: 1,
|
||||||
reply_to_message_id: 2,
|
reply_to_message_id: 2,
|
||||||
caption: "cap".into(),
|
caption: "cap".into(),
|
||||||
media_batches: vec![vec![MediaItemPayload::Photo {
|
media_batches: vec![vec![photo_item(media, false, false)]],
|
||||||
media: media.to_string(),
|
|
||||||
has_spoiler: false,
|
|
||||||
fallback_url: None,
|
|
||||||
file_id: false,
|
|
||||||
}]],
|
|
||||||
batch_index,
|
batch_index,
|
||||||
sent_message_ids: sent,
|
sent_message_ids: sent,
|
||||||
source_url: "https://x.com/u/status/1".into(),
|
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 task = queued_task("/nonexistent-ugoira.mp4", 0, vec![]);
|
||||||
let fresh = Refetched {
|
let fresh = Refetched {
|
||||||
caption: "fresh caption".into(),
|
caption: "fresh caption".into(),
|
||||||
items: vec![MediaItemPayload::Photo {
|
items: vec![photo_item("https://cdn/fresh.jpg", true, false)],
|
||||||
media: "https://cdn/fresh.jpg".into(),
|
|
||||||
has_spoiler: true,
|
|
||||||
fallback_url: None,
|
|
||||||
file_id: false,
|
|
||||||
}],
|
|
||||||
cache_data: None,
|
cache_data: None,
|
||||||
};
|
};
|
||||||
match apply_refresh(&task, &fresh).expect("a repairable task") {
|
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::post_send::{build_edit_markup, cache_sent_task};
|
||||||
use super::upload::sniff_ext;
|
use super::upload::sniff_ext;
|
||||||
use super::*;
|
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::collections::HashMap;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
@@ -1231,12 +1231,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn media_item_payload_serde_tags() {
|
fn media_item_payload_serde_tags() {
|
||||||
let photo = MediaItemPayload::Photo {
|
let photo = photo_item("https://a/b.jpg", false, false);
|
||||||
media: "https://a/b.jpg".into(),
|
|
||||||
has_spoiler: false,
|
|
||||||
fallback_url: None,
|
|
||||||
file_id: false,
|
|
||||||
};
|
|
||||||
let json = serde_json::to_value(&photo).unwrap();
|
let json = serde_json::to_value(&photo).unwrap();
|
||||||
assert_eq!(json["kind"], "photo");
|
assert_eq!(json["kind"], "photo");
|
||||||
}
|
}
|
||||||
@@ -1272,12 +1267,7 @@ mod tests {
|
|||||||
chat_id: 1,
|
chat_id: 1,
|
||||||
reply_to_message_id: 2,
|
reply_to_message_id: 2,
|
||||||
caption: caption.into(),
|
caption: caption.into(),
|
||||||
media_batches: vec![vec![MediaItemPayload::Photo {
|
media_batches: vec![vec![photo_item(media, false, false)]],
|
||||||
media: media.to_string(),
|
|
||||||
has_spoiler: false,
|
|
||||||
fallback_url: None,
|
|
||||||
file_id: false,
|
|
||||||
}]],
|
|
||||||
batch_index: 0,
|
batch_index: 0,
|
||||||
sent_message_ids: vec![],
|
sent_message_ids: vec![],
|
||||||
source_url: "https://x.com/u/status/1".into(),
|
source_url: "https://x.com/u/status/1".into(),
|
||||||
@@ -1670,12 +1660,7 @@ mod tests {
|
|||||||
chat_id: 1,
|
chat_id: 1,
|
||||||
reply_to_message_id: 2,
|
reply_to_message_id: 2,
|
||||||
caption: "cap".into(),
|
caption: "cap".into(),
|
||||||
media_batches: vec![vec![MediaItemPayload::Photo {
|
media_batches: vec![vec![photo_item("https://p/1.jpg", false, false)]],
|
||||||
media: "https://p/1.jpg".into(),
|
|
||||||
has_spoiler: false,
|
|
||||||
fallback_url: None,
|
|
||||||
file_id: false,
|
|
||||||
}]],
|
|
||||||
batch_index: 0,
|
batch_index: 0,
|
||||||
sent_message_ids: vec![],
|
sent_message_ids: vec![],
|
||||||
source_url: "https://x.com/u/status/1".into(),
|
source_url: "https://x.com/u/status/1".into(),
|
||||||
@@ -1694,12 +1679,7 @@ mod tests {
|
|||||||
chat_id: 1,
|
chat_id: 1,
|
||||||
reply_to_message_id: 2,
|
reply_to_message_id: 2,
|
||||||
caption: "cap".into(),
|
caption: "cap".into(),
|
||||||
media_batches: vec![vec![MediaItemPayload::Photo {
|
media_batches: vec![vec![photo_item("AgAC-file-id", false, true)]],
|
||||||
media: "AgAC-file-id".into(),
|
|
||||||
has_spoiler: false,
|
|
||||||
fallback_url: None,
|
|
||||||
file_id: true,
|
|
||||||
}]],
|
|
||||||
batch_index: 0,
|
batch_index: 0,
|
||||||
sent_message_ids: vec![],
|
sent_message_ids: vec![],
|
||||||
source_url: "https://x.com/u/status/1".into(),
|
source_url: "https://x.com/u/status/1".into(),
|
||||||
|
|||||||
Reference in New Issue
Block a user