oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1use docs_core::{fragment_html, fragment_json, fragment_text, initial::initialize, snapshot_bytes};
2use yrs::{Doc, Options};
3
4fn doc() -> Doc {
5 Doc::with_options(Options::with_guid_and_client_id("initial-test".into(), 71))
6}
7fn block(level: u8, text: &[u8]) -> Vec<u8> {
8 let mut out = vec![level];
9 out.extend_from_slice(&(text.len() as u32).to_be_bytes());
10 out.extend_from_slice(text);
11 out
12}
13#[test]
14fn initial_content_is_real_structured_literal_text() {
15 let d = doc();
16 let mut input = block(2, "Agenda 😀".as_bytes());
17 input.extend(block(0, "<script>& Café\n二".as_bytes()));
18 initialize(&d, &input).unwrap();
19 assert!(fragment_html(&d).contains("<h2>Agenda 😀</h2>"));
20 assert!(fragment_html(&d).contains("&lt;script&gt;&amp; Café"));
21 assert!(fragment_text(&d).contains("<script>& Café\n二"));
22 let json: serde_json::Value = serde_json::from_str(&fragment_json(&d)).unwrap();
23 assert_eq!(json["content"][0]["attrs"]["level"], 2);
24 assert_eq!(json["content"][1]["type"], "paragraph");
25 let before = snapshot_bytes(&d);
26 assert!(initialize(&d, &block(0, b"replacement")).is_err());
27 assert_eq!(before, snapshot_bytes(&d));
28}
29#[test]
30fn initial_rejection_never_leaves_partial_content() {
31 let mut cases = vec![
32 block(7, b"bad"),
33 block(0, &[0xff]),
34 vec![0, 0, 0],
35 block(0, &vec![b'x'; 128 * 1024 + 1]),
36 ];
37 let mut many = Vec::new();
38 for _ in 0..201 {
39 many.extend(block(0, b""));
40 }
41 cases.push(many);
42 for invalid in cases {
43 let d = doc();
44 let before = snapshot_bytes(&d);
45 let mut input = block(0, b"valid prefix");
46 input.extend(invalid);
47 assert!(initialize(&d, &input).is_err());
48 assert_eq!(snapshot_bytes(&d), before);
49 }
50}
51
52#[test]
53fn initial_rejects_client_ids_outside_yjs_range() {
54 for client_id in [u32::MAX as u64 + 1, (1u64 << 63) - 1] {
55 let d = Doc::with_options(Options::with_guid_and_client_id(
56 "unsafe-id".into(),
57 client_id,
58 ));
59 assert!(initialize(&d, &block(0, b"unchanged")).is_err());
60 assert!(fragment_text(&d).is_empty());
61 }
62}
63