use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct UserInfo {
pub uid: i64,
pub base: UserBaseInfo,
pub medal: UserMedalInfo,
pub wealth: Option<serde_json::Value>,
pub title: Option<serde_json::Value>,
pub guard: UserGuardInfo,
pub uhead_frame: Option<serde_json::Value>,
pub guard_leader: Option<serde_json::Value>,
}
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct GuardTabInfo {
pub num: i32,
pub page: i32,
pub now: i32,
pub achievement_level: i32,
pub anchor_guard_achieve_level: i32,
pub achievement_icon_src: String,
pub buy_guard_icon_src: String,
pub rule_doc_src: String,
pub ex_background_src: String,
pub color_start: String,
pub color_end: String,
pub tab_color: Vec<String>,
pub title_color: Vec<String>,
}
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct UserOriginInfo {
pub name: String,
pub face: String,
}
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct UserOfficialInfo {
pub role: i32,
pub title: String,
pub desc: String,
pub r#type: i32,
}
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct UserBaseInfo {
pub name: String,
pub face: String,
pub name_color: i32,
pub is_mystery: bool,
pub risk_ctrl_info: Option<serde_json::Value>,
pub origin_info: UserOriginInfo,
pub official_info: UserOfficialInfo,
pub name_color_str: String,
}
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct UserMedalInfo {
pub name: String,
pub level: i32,
pub color_start: i32,
pub color_end: i32,
pub color_border: i32,
pub color: i32,
pub id: i32,
pub typ: i32,
pub is_light: i32,
pub ruid: i64,
pub guard_level: i32,
pub score: i32,
pub guard_icon: String,
pub honor_icon: String,
pub v2_medal_color_start: String,
pub v2_medal_color_end: String,
pub v2_medal_color_border: String,
pub v2_medal_color_text: String,
pub v2_medal_color_level: String,
pub user_receive_count: i32,
}
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct UserGuardInfo {
pub level: i32,
pub expired_str: String,
}
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct GuardMember {
pub ruid: i64,
pub rank: i32,
pub accompany: i32,
pub uinfo: UserInfo,
pub score: i32,
}
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct GuardListData {
pub info: GuardTabInfo,
pub top3: Vec<GuardMember>,
pub list: Vec<GuardMember>,
}
#[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/guard-read/guard-list/contract.json"
))
}
#[ignore = "legacy live API test; requires explicit BPI_LIVE_TEST review"]
#[tokio::test]
async fn test_get_guard_list() -> Result<(), Box<BpiError>> {
let bpi = BpiClient::new().expect("client should build");
let data = bpi
.live()
.guard_list(23174842, 504140200, None, None, None)
.await?;
assert!(!data.list.is_empty());
Ok(())
}
#[test]
fn live_guard_list_contract_matches_endpoint_request() -> BpiResult<()> {
let contract = contract()?;
let params: Vec<(&str, String)> = vec![
("roomid", 23174842_i64.to_string()),
("ruid", 504140200_i64.to_string()),
("page", 1_i32.to_string()),
("page_size", 20_i32.to_string()),
("typ", 5_i32.to_string()),
];
assert_eq!(contract.name, "live.guard_list");
assert_eq!(contract.request.method, HttpMethod::Get);
assert_eq!(
contract.request.url.as_str(),
"https://api.live.bilibili.com/xlive/app-room/v2/guardTab/topListNew"
);
assert_eq!(
contract.request.query.get("roomid").map(String::as_str),
Some("23174842")
);
assert_eq!(
contract.request.query.get("ruid").map(String::as_str),
Some("504140200")
);
assert_eq!(
params,
vec![
("roomid", "23174842".to_string()),
("ruid", "504140200".to_string()),
("page", "1".to_string()),
("page_size", "20".to_string()),
("typ", "5".to_string()),
]
);
assert_eq!(contract.cases.len(), 3);
assert_eq!(
contract.cases[0].response.rust_model.as_deref(),
Some("GuardListData")
);
Ok(())
}
#[test]
fn live_guard_list_response_fixture_parses_declared_model() -> BpiResult<()> {
let payload = ApiEnvelope::<GuardListData>::from_slice(include_bytes!(
"../../tests/contracts/live/guard-read/guard-list/responses/success.json"
))?
.into_payload()?;
assert_eq!(payload.info.now, 1);
assert_eq!(payload.top3.len(), 1);
assert_eq!(payload.list.len(), 1);
Ok(())
}
fn local_probe_body(profile: &str) -> Option<serde_json::Value> {
let path =
format!("target/bpi-probe-runs/live/guard-read/guard-list/{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_guard_list_model_matches_local_probe_outputs_when_available() -> BpiResult<()> {
for profile in ["anonymous", "normal", "vip"] {
let Some(body) = local_probe_body(profile) else {
continue;
};
let payload =
serde_json::from_value::<ApiEnvelope<GuardListData>>(body)?.into_payload()?;
assert!(!payload.list.is_empty() || !payload.top3.is_empty());
}
Ok(())
}
}