use serde::Deserialize;
use crate::{Group, Torrent};
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GroupResponse {
pub group: Group,
pub torrents: Vec<Torrent>,
}
#[cfg(feature = "mock")]
impl GroupResponse {
#[must_use]
pub fn mock() -> Self {
Self {
group: Group::mock(),
torrents: vec![Torrent::mock()],
}
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::indexing_slicing)]
mod tests {
use super::*;
const OPS_RESPONSE: &str = include_str!("../tests/fixtures/group_response_ops.json");
const RED_RESPONSE: &str = include_str!("../tests/fixtures/group_response_red.json");
#[test]
fn deserialize_ops_group_response() {
let response: GroupResponse = serde_json::from_str(OPS_RESPONSE).unwrap();
assert!(response.group.bb_body.is_none());
assert_eq!(response.group.id, 1_161_539);
assert_eq!(response.group.name, "Test Album");
assert_eq!(response.group.year, 2024);
assert!(!response.group.vanity_house);
assert_eq!(response.torrents[0].trumpable, Some(false));
assert!(response.torrents[0].lossy_web_approved.is_none());
assert!(response.torrents[0].lossy_master_approved.is_none());
assert!(response.torrents[0].is_neutralleech.is_none());
assert!(response.torrents[0].is_freeload.is_none());
}
#[test]
fn deserialize_red_group_response() {
let response: GroupResponse = serde_json::from_str(RED_RESPONSE).unwrap();
assert!(response.group.bb_body.is_some());
assert_eq!(response.group.id, 629);
assert_eq!(response.group.name, "Test Album");
assert_eq!(response.group.year, 1982);
assert!(!response.group.vanity_house);
assert_eq!(response.torrents[0].trumpable, Some(false));
assert_eq!(response.torrents[0].lossy_web_approved, Some(false));
assert_eq!(response.torrents[2].trumpable, Some(true));
assert_eq!(response.torrents[2].lossy_web_approved, Some(true));
assert_eq!(response.torrents[2].is_neutralleech, Some(true));
}
#[test]
fn deserialize_group_with_multiple_torrents() {
let response: GroupResponse = serde_json::from_str(OPS_RESPONSE).unwrap();
assert_eq!(response.torrents.len(), 2);
let first_torrent = &response.torrents[0];
assert_eq!(first_torrent.id, 2_492_578);
assert_eq!(first_torrent.media, "WEB");
assert_eq!(first_torrent.encoding, "24bit Lossless");
let second_torrent = &response.torrents[1];
assert_eq!(second_torrent.id, 1_520_678);
assert_eq!(second_torrent.encoding, "Lossless");
}
#[test]
fn deserialize_ops_group_music_info() {
let response: GroupResponse = serde_json::from_str(OPS_RESPONSE).unwrap();
let music_info = response.group.music_info.expect("music_info should exist");
assert_eq!(music_info.artists.len(), 1);
assert_eq!(music_info.artists[0].name, "Test Artist");
assert!(music_info.arranger.is_some());
assert!(music_info.arranger.as_ref().unwrap().is_empty());
}
#[test]
fn deserialize_red_group_music_info() {
let response: GroupResponse = serde_json::from_str(RED_RESPONSE).unwrap();
let music_info = response.group.music_info.expect("music_info should exist");
assert_eq!(music_info.artists.len(), 1);
assert_eq!(music_info.artists[0].name, "Test Artist");
assert!(music_info.arranger.is_none());
}
#[test]
fn deserialize_group_tags() {
let ops_response: GroupResponse = serde_json::from_str(OPS_RESPONSE).unwrap();
let red_response: GroupResponse = serde_json::from_str(RED_RESPONSE).unwrap();
assert_eq!(ops_response.group.tags.len(), 1);
assert!(ops_response.group.tags.contains(&"electronic".to_owned()));
assert_eq!(red_response.group.tags.len(), 3);
assert!(red_response.group.tags.contains(&"rock".to_owned()));
assert!(red_response.group.tags.contains(&"punk".to_owned()));
}
#[test]
fn deserialize_group_torrent_uploaders() {
let response: GroupResponse = serde_json::from_str(RED_RESPONSE).unwrap();
assert_eq!(response.torrents[0].user_id, 2001);
assert_eq!(response.torrents[0].username, "user_a");
assert_eq!(response.torrents[1].user_id, 2002);
assert_eq!(response.torrents[1].username, "user_b");
}
#[test]
fn deserialize_red_group_with_three_editions() {
let response: GroupResponse = serde_json::from_str(RED_RESPONSE).unwrap();
assert_eq!(response.torrents.len(), 3);
assert_eq!(response.torrents[0].media, "Vinyl");
assert_eq!(response.torrents[1].media, "CD");
assert_eq!(response.torrents[2].media, "WEB");
}
}