scroll.server/crates/service-rpc/tests/postcard_roundtrip.rs
WiseDev a509529aef carry rpc over a binary frame, not json+base64
the inter-service payload is already raw bytes; postcard sends it as
length-prefixed binary instead of base64 stuffed into a json envelope.
drops the base64 module and the per-field serde shims.
2026-08-28 23:26:38 +03:00

64 lines
1.8 KiB
Rust

use service_rpc::{
AccountRef, AuthRequest, AuthResponse, DeviceInfo, GameRequest, GameResponse, HomeRequestKind,
LoginOutcome, Session, WireMessage,
};
fn roundtrip<T>(value: T)
where
T: serde::Serialize + serde::de::DeserializeOwned + PartialEq + std::fmt::Debug,
{
let bytes = postcard::to_allocvec(&value).expect("encode");
let back: T = postcard::from_bytes(&bytes).expect("decode");
assert_eq!(value, back);
}
fn session() -> Session {
Session {
account: AccountRef::new(0, 5),
pass_token: "tok".to_owned(),
session_count: 3,
play_time_seconds: 42,
days_since_started_playing: 1,
account_created_date: "2026-01-01".to_owned(),
server_time: "2026-08-28".to_owned(),
}
}
#[test]
fn game_request_survives_the_wire() {
roundtrip(GameRequest::BattleTick {
account: AccountRef::new(0, 5),
});
roundtrip(GameRequest::SectorCommand {
account: AccountRef::new(0, 5),
payload: vec![0, 1, 2, 253, 254, 255],
});
roundtrip(GameRequest::LoadHome {
account: AccountRef::new(1, 2),
kind: HomeRequestKind::Login,
});
}
#[test]
fn game_response_survives_the_wire() {
roundtrip(GameResponse::Empty);
roundtrip(GameResponse::messages(vec![
WireMessage::new(20104, 0, vec![9, 8, 7, 0, 255]),
WireMessage::new(24101, 1, Vec::new()),
]));
}
#[test]
fn auth_exchange_survives_the_wire() {
roundtrip(AuthRequest::Login {
account: AccountRef::new(0, 5),
pass_token: Some("tok".to_owned()),
device: DeviceInfo::default(),
});
roundtrip(AuthResponse::Login(LoginOutcome::Accepted(session())));
roundtrip(AuthResponse::Login(LoginOutcome::Rejected {
error_code: 11,
message: Some("banned".to_owned()),
}));
roundtrip(AuthResponse::Resolved { known: true });
}