chore: add tests

This commit is contained in:
kaichaosun 2026-01-06 15:45:48 +08:00
parent 39474393c0
commit 403029a541
No known key found for this signature in database
GPG Key ID: 223E0F992F4F03BF
3 changed files with 83 additions and 0 deletions

1
gen/rust/Cargo.lock generated
View File

@ -18,6 +18,7 @@ checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3"
name = "chat-proto"
version = "0.1.0"
dependencies = [
"bytes",
"prost",
]

View File

@ -5,3 +5,6 @@ edition = "2021"
[dependencies]
prost = "0.14"
[dev-dependencies]
bytes = "1.3"

View File

@ -28,3 +28,82 @@ pub mod wap {
include!("wap/reliability/wap.reliability.rs");
}
}
#[cfg(test)]
mod tests {
use super::wap::{
encryption::{encrypted_payload::Encryption, EncryptedPayload, Plaintext},
inbox::{inbox_v1_frame::FrameType, InboxV1Frame, Note},
invite::InvitePrivateV1,
};
use bytes::Bytes;
use prost::Message;
#[test]
fn test_encrypted_payload_roundtrip() {
let payload = EncryptedPayload {
encryption: Some(Encryption::Plaintext(Plaintext {
payload: Bytes::from_static(b"hello world"),
})),
};
// Encode to bytes
let mut buf = Vec::new();
payload.encode(&mut buf).expect("Encoding failed");
// Decode back
let decoded = EncryptedPayload::decode(&buf[..]).expect("Decoding failed");
match decoded.encryption {
Some(Encryption::Plaintext(p)) => {
assert_eq!(p.payload, Bytes::from_static(b"hello world"));
}
_ => panic!("Expected plaintext variant"),
}
}
#[test]
fn test_inbox_frame_roundtrip() {
let note = Note {
text: "This is a test note".to_string(),
};
let frame = InboxV1Frame {
recipient: "alice".to_string(),
frame_type: Some(FrameType::Note(note.clone())),
};
let mut buf = Vec::new();
frame.encode(&mut buf).expect("Encoding failed");
let decoded = InboxV1Frame::decode(&buf[..]).expect("Decoding failed");
match decoded.frame_type {
Some(FrameType::Note(n)) => {
assert_eq!(n.text, note.text);
}
_ => panic!("Expected Note variant"),
}
}
#[test]
fn test_invite_private_roundtrip() {
let invite = InvitePrivateV1 {
initiator: Bytes::from_static(b"initiator"),
initiator_ephemeral: Bytes::from_static(b"ephemeral"),
participant: Bytes::from_static(b"participant"),
participant_ephemeral_id: 42,
discriminator: "test_discriminator".to_string(),
initial_message: None, // skipping encrypted payload for simplicity
};
let mut buf = Vec::new();
invite.encode(&mut buf).expect("Encoding failed");
let decoded = InvitePrivateV1::decode(&buf[..]).expect("Decoding failed");
assert_eq!(decoded.initiator, Bytes::from_static(b"initiator"));
assert_eq!(decoded.participant_ephemeral_id, 42);
assert_eq!(decoded.discriminator, "test_discriminator");
}
}