lb-rs 26.8.4

The rust library for interacting with your lockbook.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use itertools::Itertools;
use lb_rs::model::file::ShareMode;
use test_utils::*;

/// Uncategorized tests.

#[tokio::test]
async fn test_path_conflict() {
    let db1 = test_core_with_account().await;
    let db2 = test_core_from(&db1).await;

    db1.create_at_path("new.md").await.unwrap();
    db1.sync().await.unwrap();
    db2.create_at_path("new.md").await.unwrap();
    db2.sync().await.unwrap();

    assert_eq!(
        db2.list_metadatas()
            .await
            .unwrap()
            .iter()
            .filter(|file| file.id != file.parent)
            .map(|file| file.name.clone())
            .sorted()
            .collect::<Vec<String>>(),
        ["new-1.md", "new.md"]
    )
}

#[tokio::test]
async fn test_path_conflict2() {
    let db1 = test_core_with_account().await;
    let db2 = test_core_from(&db1).await;

    db1.create_at_path("new-1.md").await.unwrap();
    db1.sync().await.unwrap();
    db2.create_at_path("new-1.md").await.unwrap();
    db2.sync().await.unwrap();

    assert_eq!(
        db2.list_metadatas()
            .await
            .unwrap()
            .iter()
            .filter(|file| file.id != file.parent)
            .map(|file| file.name.clone())
            .sorted()
            .collect::<Vec<String>>(),
        ["new-1.md", "new-2.md"]
    )
}

#[tokio::test]
async fn deleted_path_is_released() {
    let db1 = test_core_with_account().await;
    let file1 = db1.create_at_path("file1.md").await.unwrap();
    db1.sync().await.unwrap();
    db1.delete(&file1.id).await.unwrap();
    db1.sync().await.unwrap();
    db1.create_at_path("file1.md").await.unwrap();
    db1.sync().await.unwrap();

    let db2 = test_core_from(&db1).await;
    db2.sync().await.unwrap();
}

// this case did not actually get the fuzzer stuck and was written while reproducing the issue
#[tokio::test]
async fn fuzzer_stuck_test_1() {
    let db1 = test_core_with_account().await;
    let b = db1.create_at_path("/b").await.unwrap();
    let c = db1.create_at_path("/c/").await.unwrap();
    let d = db1.create_at_path("/c/d/").await.unwrap();
    db1.move_file(&b.id, &d.id).await.unwrap();
    db1.move_file(&c.id, &d.id).await.unwrap_err();
}

// this case did not actually get the fuzzer stuck and was written while reproducing the issue
#[tokio::test]
async fn fuzzer_stuck_test_2() {
    let db1 = test_core_with_account().await;
    let root = db1.root().await.unwrap();
    let db2 = test_core_from(&db1).await;

    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.sync().await.unwrap();
    db2.sync().await.unwrap();

    let a = db2.create_at_path("/a/").await.unwrap();
    let b = db2.create_at_path("/a/b/").await.unwrap();
    db2.move_file(&b.id, &root.id).await.unwrap();
    db2.rename_file(&b.id, "b2").await.unwrap();
    let _c = db2.create_at_path("/c/").await.unwrap();
    db2.move_file(&b.id, &a.id).await.unwrap();

    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.test_repo_integrity(true).await.unwrap();
    assert::cores_equal(&db1, &db2).await;
}

// this case did not actually get the fuzzer stuck and was written while reproducing the issue
#[tokio::test]
async fn fuzzer_stuck_test_3() {
    let db1 = test_core_with_account().await;
    let db2 = test_core_from(&db1).await;

    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.sync().await.unwrap();
    db2.sync().await.unwrap();

    let _a = db2.create_at_path("/a/").await.unwrap();

    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.test_repo_integrity(true).await.unwrap();
    assert::cores_equal(&db1, &db2).await;

    db1.create_at_path("/a/b.md").await.unwrap();
    let c = db1.create_at_path("/a/c").await.unwrap();
    db1.rename_file(&c.id, "c2").await.unwrap();

    db1.create_at_path("/a/d").await.unwrap();

    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.test_repo_integrity(true).await.unwrap();
    assert::cores_equal(&db1, &db2).await;
}

// this case did not actually get the fuzzer stuck and was written while reproducing the issue
#[tokio::test]
async fn fuzzer_stuck_test_4() {
    let db1 = test_core_with_account().await;
    let root = db1.root().await.unwrap();
    let db2 = test_core_from(&db1).await;

    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.sync().await.unwrap();
    db2.sync().await.unwrap();

    let _a = db2.create_at_path("/a/").await.unwrap();
    let b = db2.create_at_path("/a/b/").await.unwrap();
    db2.move_file(&b.id, &root.id).await.unwrap();
    db2.rename_file(&b.id, "b2").await.unwrap();
    let c = db2.create_at_path("c.md").await.unwrap();
    db2.write_document(c.id, b"DPCN8G0CK8qXSyJhervmmEXFnkt")
        .await
        .unwrap();

    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.test_repo_integrity(true).await.unwrap();
    assert::cores_equal(&db1, &db2).await;
}

#[tokio::test]
async fn fuzzer_stuck_test_5() {
    let db1 = test_core_with_account().await;
    let root = db1.root().await.unwrap();
    let db2 = test_core_from(&db1).await;

    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.sync().await.unwrap();
    db2.sync().await.unwrap();

    let a = db1.create_at_path("/a/").await.unwrap();
    let b = db1.create_at_path("/a/b/").await.unwrap();

    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.test_repo_integrity(true).await.unwrap();
    assert::cores_equal(&db1, &db2).await;

    db1.move_file(&b.id, &root.id).await.unwrap();
    db1.move_file(&a.id, &b.id).await.unwrap();
    db1.delete(&b.id).await.unwrap();

    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.test_repo_integrity(true).await.unwrap();
    assert::cores_equal(&db1, &db2).await;
}

#[tokio::test]
async fn fuzzer_stuck_test_6() {
    let core1 = test_core_with_account().await;

    let dir1 = core1.create_at_path("quB/").await.unwrap();
    let dir2 = core1.create_at_path("OO1/").await.unwrap();
    core1.sync().await.unwrap();
    let core2 = test_core_from(&core1).await;
    core2.move_file(&dir2.id, &dir1.id).await.unwrap();
    let _doc1 = core1.create_at_path("KbW").await.unwrap();
    core1.move_file(&dir1.id, &dir2.id).await.unwrap();

    core1.sync().await.unwrap();
    core2.sync().await.unwrap();
    core1.sync().await.unwrap();
    core2.sync().await.unwrap();
    core1.test_repo_integrity(true).await.unwrap();
    assert::cores_equal(&core1, &core2).await;
}

#[tokio::test]
async fn fuzzer_get_updates_required_test() {
    let db1 = test_core_with_account().await;

    let document = db1.create_at_path("/document").await.unwrap();

    db1.sync().await.unwrap();
    let db2 = test_core_from(&db1).await;

    db1.write_document(document.id, b"document content")
        .await
        .unwrap();
    db2.write_document(document.id, b"content document")
        .await
        .unwrap();
    db2.delete(&document.id).await.unwrap();

    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
    db1.sync().await.unwrap();
    db2.sync().await.unwrap();
}

#[tokio::test]
async fn fuzzer_new_file_deleted() {
    let core = test_core_with_account().await;

    let dir1 = core.create_at_path("u88/").await.unwrap();
    core.sync().await.unwrap();
    let dir2 = core.create_at_path("mep/").await.unwrap();
    core.move_file(&dir1.id, &dir2.id).await.unwrap();
    core.delete(&dir2.id).await.unwrap();
    core.sync().await.unwrap();
}

#[tokio::test]
async fn fuzzer_create_document_in_renamed_concurrently_deleted_folder() {
    let core1 = test_core_with_account().await;
    let core2 = test_core_from(&core1).await;

    let folder = core1.create_at_path("folder/").await.unwrap();

    core1.sync().await.unwrap();
    core2.sync().await.unwrap();

    core1.delete(&folder.id).await.unwrap();
    core1.sync().await.unwrap();

    let document = core2.create_at_path("folder/document").await.unwrap();
    core2
        .write_document(document.id, b"document content")
        .await
        .unwrap();
    core2
        .rename_file(&folder.id, "folder-renamed")
        .await
        .unwrap();
    core2.sync().await.unwrap();
}

#[tokio::test]
async fn fuzzer_delete_concurrently_edited_document() {
    let core1 = test_core_with_account().await;
    let core2 = test_core_from(&core1).await;

    let document = core1.create_at_path("document.md").await.unwrap();

    core1.sync().await.unwrap();
    core2.sync().await.unwrap();

    core1.write_document(document.id, b"content").await.unwrap();
    core1.sync().await.unwrap();

    core2.write_document(document.id, b"content").await.unwrap();
    core2.delete(&document.id).await.unwrap();
    core2.sync().await.unwrap();
}

#[tokio::test]
async fn test_move_folder_with_deleted_file() {
    let mut cores = [
        vec![test_core_with_account().await],
        vec![test_core_with_account().await],
        vec![test_core_with_account().await],
    ];
    let c = another_client(&cores[1][0]).await;
    cores[1].push(c);
    let c = another_client(&cores[1][0]).await;
    cores[1].push(c);
    let c = another_client(&cores[2][0]).await;
    cores[2].push(c);

    let us6 = cores[0][0].create_at_path("US62E5M/").await.unwrap();
    let voe = cores[0][0]
        .create_at_path("US62E5M/voey6qi.md")
        .await
        .unwrap();
    cores[0][0].delete(&voe.id).await.unwrap();
    let us7 = cores[0][0].create_at_path("US7/").await.unwrap();
    cores[0][0].move_file(&us6.id, &us7.id).await.unwrap();
}

#[tokio::test]
async fn test_clean_sync_deleted_link() {
    let cores = [test_core_with_account().await, test_core_with_account().await];

    let doc = cores[0].create_at_path("welcome.md").await.unwrap();
    cores[0]
        .share_file(doc.id, &cores[1].get_account().unwrap().username, ShareMode::Write)
        .await
        .unwrap();

    cores[0].sync().await.unwrap();
    cores[1].sync().await.unwrap();

    let link_doc = cores[1]
        .create_link_at_path("welcome-path.md", doc.id)
        .await
        .unwrap();
    cores[1].sync().await.unwrap();
    cores[1].delete(&link_doc.id).await.unwrap();
    cores[1].reject_share(&doc.id).await.unwrap();
    cores[1].sync().await.unwrap();

    another_client(&cores[1]).await.sync().await.unwrap();
}

#[tokio::test]
async fn test_unmergable_conflict_progress_closure() {
    let mut cores = vec![test_core_with_account().await];
    let new_core = another_client(&cores[0]).await;
    cores.push(new_core);

    let doc = cores[0].create_at_path("test.md").await.unwrap();

    cores[0].sync().await.unwrap();
    cores[1].sync().await.unwrap();

    cores[0].write_document(doc.id, b"a").await.unwrap();
    cores[1].write_document(doc.id, b"b").await.unwrap();

    cores[0].sync().await.unwrap();
    cores[1].sync().await.unwrap();
}

#[tokio::test]
async fn concurrent_chat_appends_union_cleanly() {
    let c1 = test_core_with_account().await;
    let doc = c1.create_at_path("convo.chat").await.unwrap();

    let base = "{\"from\":\"a\",\"content\":\"hello\",\"ts\":1}\n";
    c1.write_document(doc.id, base.as_bytes()).await.unwrap();
    c1.sync().await.unwrap();

    let c2 = test_core_from(&c1).await;

    // each device appends its own turn on top of the shared base
    let turn1 = format!("{base}{{\"from\":\"a\",\"content\":\"one\",\"ts\":2}}\n");
    let turn2 = format!("{base}{{\"from\":\"b\",\"content\":\"two\",\"ts\":3}}\n");
    c1.write_document(doc.id, turn1.as_bytes()).await.unwrap();
    c2.write_document(doc.id, turn2.as_bytes()).await.unwrap();

    c1.sync().await.unwrap();
    c2.sync().await.unwrap();
    c1.sync().await.unwrap();
    c2.sync().await.unwrap();

    // no conflict copy: the single .chat is still the only document
    for c in [&c1, &c2] {
        let chats = c
            .list_metadatas()
            .await
            .unwrap()
            .into_iter()
            .filter(|f| f.name.ends_with(".chat"))
            .count();
        assert_eq!(chats, 1, "expected line-union, found a conflict copy");

        let merged = String::from_utf8(c.read_document(doc.id, false).await.unwrap()).unwrap();
        assert_eq!(merged.matches("\"content\":\"one\"").count(), 1);
        assert_eq!(merged.matches("\"content\":\"two\"").count(), 1);
        assert_eq!(merged.matches("\"content\":\"hello\"").count(), 1);
    }
    assert::cores_equal(&c1, &c2).await;
}