use serde::{Deserialize, Serialize};
use crate::state::StoredMessage;
pub const CHAT_CHUNK_SIZE: u32 = 100;
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChatChunk {
pub messages: Vec<StoredMessage>,
pub next_index: Option<u32>,
}
#[cfg(test)]
mod tests {
use insta::assert_snapshot;
use opentalk_types_common::time::Timestamp;
use opentalk_types_signaling::ParticipantId;
use pretty_assertions::assert_eq;
use serde_json::json;
use crate::{
MessageId, Scope,
state::{ChatChunk, StoredMessage},
};
#[test]
fn serialize_chat_chunk() {
let chunk = ChatChunk {
messages: vec![StoredMessage {
id: MessageId::nil(),
source: ParticipantId::nil(),
timestamp: Timestamp::unix_epoch(),
content: "hello".into(),
scope: Scope::Global,
}],
next_index: None,
};
let produced = serde_json::to_string_pretty(&chunk).unwrap();
assert_snapshot!(produced, @r#"
{
"messages": [
{
"id": "00000000-0000-0000-0000-000000000000",
"source": "00000000-0000-0000-0000-000000000000",
"timestamp": "1970-01-01T00:00:00Z",
"content": "hello",
"scope": "global"
}
],
"next_index": null
}
"#);
}
#[test]
fn deserialize_chat_chunk() {
let json = json!({
"messages": [{
"id": "00000000-0000-0000-0000-000000000000",
"source": "00000000-0000-0000-0000-000000000000",
"timestamp": "1970-01-01T00:00:00Z",
"content": "hello",
"scope": "global",
}],
"next_index": null,
});
let chunk = serde_json::from_value(json).unwrap();
assert_eq!(
ChatChunk {
messages: vec![StoredMessage {
id: MessageId::nil(),
source: ParticipantId::nil(),
timestamp: Timestamp::unix_epoch(),
content: "hello".into(),
scope: Scope::Global,
}],
next_index: None,
},
chunk
);
}
}