sapphire_framework_session/
message.rs1use grain_id::GrainId;
4use sapphire_sync::{ContentHash, PathUpdate, ReplicaId, VersionVector};
5use serde::{Deserialize, Serialize};
6
7#[derive(Clone, Debug, Deserialize, Serialize)]
9pub enum Message {
10 Hello {
12 format: u32,
14 workspace_id: GrainId,
16 replica_id: ReplicaId,
18 vv: VersionVector,
20 max_file_size: u64,
23 #[serde(default)]
27 acks: bool,
28 },
29 Updates(Vec<PathUpdate>),
31 Done,
33 Want(ContentHash),
35 Missing(ContentHash),
37 Aborted(ContentHash),
42 Settled,
49 Live(Vec<PathUpdate>),
51 Refused(String),
53 Ack(VersionVector),
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 fn round_trip(msg: &Message) -> Message {
67 let json = serde_json::to_vec(msg).unwrap();
68 serde_json::from_slice(&json).unwrap()
69 }
70
71 #[test]
72 fn a_hello_round_trips() {
73 let mut vv = VersionVector::new();
74 vv.add_dot(&sapphire_sync::Dot {
75 replica: ReplicaId::new(),
76 counter: 3,
77 });
78 let msg = Message::Hello {
79 format: crate::SESSION_FORMAT_VERSION,
80 workspace_id: GrainId::random(),
81 replica_id: ReplicaId::new(),
82 vv,
83 max_file_size: 1024,
84 acks: true,
85 };
86 assert_eq!(format!("{:?}", round_trip(&msg)), format!("{msg:?}"));
87 }
88
89 #[test]
90 fn the_payload_free_messages_round_trip() {
91 assert!(matches!(round_trip(&Message::Done), Message::Done));
92 assert!(matches!(round_trip(&Message::Settled), Message::Settled));
93 }
94
95 #[test]
96 fn a_hash_carrying_message_round_trips_as_hex() {
97 let hash = ContentHash::of_bytes(b"content");
98 let json = serde_json::to_string(&Message::Want(hash)).unwrap();
99 assert!(json.contains(&hash.to_hex()), "got {json}");
101 assert_eq!(
102 format!("{:?}", round_trip(&Message::Want(hash))),
103 format!("{:?}", Message::Want(hash))
104 );
105 assert_eq!(
106 format!("{:?}", round_trip(&Message::Missing(hash))),
107 format!("{:?}", Message::Missing(hash))
108 );
109 }
110
111 #[test]
112 fn an_abort_round_trips() {
113 let hash = ContentHash::of_bytes(b"content");
114 match round_trip(&Message::Aborted(hash)) {
115 Message::Aborted(back) => assert_eq!(back, hash),
116 other => panic!("got {other:?}"),
117 }
118 }
119
120 #[test]
121 fn a_hello_without_acks_reads_as_not_acknowledging() {
122 let json = serde_json::json!({ "Hello": {
124 "format": crate::SESSION_FORMAT_VERSION,
125 "workspace_id": GrainId::random(),
126 "replica_id": ReplicaId::new(),
127 "vv": VersionVector::new(),
128 "max_file_size": 1024,
129 }});
130 match serde_json::from_value::<Message>(json).unwrap() {
131 Message::Hello { acks, .. } => assert!(!acks),
132 other => panic!("got {other:?}"),
133 }
134 }
135
136 #[test]
137 fn an_ack_round_trips() {
138 let mut vv = VersionVector::new();
139 vv.add_dot(&sapphire_sync::Dot {
140 replica: ReplicaId::new(),
141 counter: 7,
142 });
143 match round_trip(&Message::Ack(vv.clone())) {
144 Message::Ack(back) => assert!(back.covers(&vv) && vv.covers(&back)),
145 other => panic!("got {other:?}"),
146 }
147 }
148
149 #[test]
150 fn a_refusal_round_trips() {
151 let msg = Message::Refused("format 2".to_owned());
152 match round_trip(&msg) {
153 Message::Refused(why) => assert_eq!(why, "format 2"),
154 other => panic!("got {other:?}"),
155 }
156 }
157
158 #[test]
159 fn a_live_push_round_trips() {
160 let mut vv = VersionVector::new();
161 vv.add_dot(&sapphire_sync::Dot {
162 replica: ReplicaId::new(),
163 counter: 3,
164 });
165 let msg = Message::Live(vec![PathUpdate {
166 path: "notes/hello.md".to_owned(),
167 versions: vec![],
168 seen: vv,
169 }]);
170 match round_trip(&msg) {
171 Message::Live(updates) => {
172 assert_eq!(updates.len(), 1);
173 assert_eq!(updates[0].path, "notes/hello.md");
174 }
175 other => panic!("got {other:?}"),
176 }
177 }
178}