use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct WatchedShow {
pub switch: bool,
pub num: i32,
pub text_small: String,
pub text_large: String,
pub icon: String,
pub icon_location: i32,
pub icon_web: String,
}
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct RecommendRoom {
pub head_box: Option<serde_json::Value>,
pub area_v2_id: i32,
pub area_v2_parent_id: i32,
pub area_v2_name: String,
pub area_v2_parent_name: String,
pub broadcast_type: i32,
pub cover: String,
pub link: String,
pub online: i32,
#[serde(rename = "pendant_Info")]
pub pendant_info: serde_json::Value,
pub roomid: i64,
pub title: String,
pub uname: String,
pub face: String,
pub verify: serde_json::Value,
pub uid: i64,
pub keyframe: String,
pub is_auto_play: i32,
pub head_box_type: i32,
pub flag: i32,
pub session_id: String,
pub show_callback: String,
pub click_callback: String,
pub special_id: i32,
pub watched_show: WatchedShow,
pub is_nft: i32,
pub nft_dmark: String,
pub is_ad: bool,
pub ad_transparent_content: Option<serde_json::Value>,
pub show_ad_icon: bool,
pub status: bool,
pub followers: i32,
}
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct RecommendData {
pub recommend_room_list: Vec<RecommendRoom>,
pub top_room_id: i64,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::probe::contract::HttpMethod;
use crate::probe::endpoint_contract::EndpointContract;
use crate::{ApiEnvelope, BpiClient, BpiError, BpiResult};
fn contract() -> BpiResult<EndpointContract> {
EndpointContract::from_slice(include_bytes!(
"../../tests/contracts/live/public-core/recommend/contract.json"
))
}
#[ignore = "legacy live API test; requires explicit BPI_LIVE_TEST review"]
#[tokio::test]
async fn test_get_live_recommend() -> Result<(), Box<BpiError>> {
let bpi = BpiClient::new().expect("client should build");
let data = bpi.live().recommend().await?;
assert!(!data.recommend_room_list.is_empty());
Ok(())
}
#[test]
fn live_recommend_contract_matches_endpoint_request() -> BpiResult<()> {
let contract = contract()?;
assert_eq!(contract.name, "live.recommend");
assert_eq!(contract.request.method, HttpMethod::Get);
assert_eq!(
contract.request.url.as_str(),
"https://api.live.bilibili.com/xlive/web-interface/v1/webMain/getMoreRecList"
);
assert_eq!(
contract.request.query.get("platform").map(String::as_str),
Some("web")
);
assert_eq!(
contract
.request
.query
.get("web_location")
.map(String::as_str),
Some("333.1007")
);
assert_eq!(contract.cases.len(), 3);
assert_eq!(
contract.cases[0].response.rust_model.as_deref(),
Some("RecommendData")
);
Ok(())
}
#[test]
fn live_recommend_response_fixture_parses_declared_model() -> BpiResult<()> {
let payload = ApiEnvelope::<RecommendData>::from_slice(include_bytes!(
"../../tests/contracts/live/public-core/recommend/responses/success.json"
))?
.into_payload()?;
assert_eq!(payload.recommend_room_list.len(), 1);
assert_eq!(payload.recommend_room_list[0].roomid, 1);
Ok(())
}
fn local_probe_body(profile: &str) -> Option<serde_json::Value> {
let path =
format!("target/bpi-probe-runs/live/public-core/recommend/{profile}.response.json");
let bytes = std::fs::read(path).ok()?;
let value: serde_json::Value = serde_json::from_slice(&bytes).ok()?;
value
.get("response")
.and_then(|response| response.get("body"))
.cloned()
}
#[test]
fn live_recommend_model_matches_local_probe_outputs_when_available() -> BpiResult<()> {
for profile in ["anonymous", "normal", "vip"] {
if let Some(body) = local_probe_body(profile) {
let payload =
serde_json::from_value::<ApiEnvelope<RecommendData>>(body)?.into_payload()?;
assert!(!payload.recommend_room_list.is_empty());
}
}
Ok(())
}
}