cosmic-pim-sync 3.3.0

Ties accounts, credentials, CalDAV, and the vdir into one sync cycle
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// Copyright 2026 Dominikos Pritis
// SPDX-License-Identifier: MPL-2.0

//! Turning a local edit into a queued push.
//!
//! The storage layer deliberately knows nothing about CalDAV: `Store::save`
//! writes an `.ics` and stops. That is the right split, but it leaves a gap —
//! something has to notice the write and queue it for the server, or the local
//! copy and the remote one diverge silently and permanently (see
//! [`cosmic_pim_caldav::push`] for why "permanently").
//!
//! This is that something. [`save_and_queue`] runs the app's save and queues
//! it as one step, under the collection's lock, which is the form to use:
//! a sync pass pulling the same resource can then neither overwrite the save
//! before it is queued nor miss it. [`queue_save`] and [`queue_delete`] queue
//! a change already made, as a second step — correct only where no pass can
//! be pulling that resource in between.
//!
//! # Local-only calendars
//!
//! A collection with no CalDAV binding is a perfectly normal thing to have —
//! the user made it themselves and no server is involved. Every function here
//! returns `Ok(false)` for that case rather than erroring, so the caller can
//! use the same code path for both and not care which kind it just wrote to.

use std::path::Path;

use cosmic_pim_caldav::{CalDavStore as _, VdirStore};

use crate::error::{Error, Result};

/// What [`save_and_queue`] did, once the write itself had succeeded.
#[derive(Debug)]
pub struct Saved<T> {
    /// What the write returned.
    pub value: T,
    /// Whether the change was queued for upload.
    ///
    /// `Ok(false)` is a collection with no server to upload to — a local
    /// calendar, one marked local-only, or one the server made read-only —
    /// or a write that changed none of its files. `Err` means the change **is on disk and is not queued** — it stays on
    /// this device until something queues it — and says why.
    pub queued: Result<bool>,
}

/// Makes a local change and queues it for upload, as one step.
///
/// `write` is the change itself — `Store::save`, a delete, a contact write —
/// and `file_names` are the files of the collection it may touch. What each
/// of them holds when `write` returns is what is queued: an upload for a file
/// that is there, a deletion on the server for one that is gone and that the
/// server knows. A file the write left as it was — the same bytes, or absent
/// before and after — is not queued, so a write that turns out to change
/// nothing uploads nothing. The bytes each held *before* `write` ran are
/// captured as the base for an automatic three-way merge, so callers no
/// longer read them themselves.
///
/// A write that learns a file's name only by writing it — a new event whose
/// name it picks, an import — goes through [`save_and_queue_creating`].
///
/// # Why one step
///
/// Saving and then calling [`queue_save`] leaves a gap between the two. A
/// sync pass pulling that resource in the gap finds a changed file and
/// nothing queued for it, takes the file for the last synced copy, and
/// writes the server's version over it; the enqueue that follows then pushes
/// the server's own bytes back. The edit is gone and nothing anywhere says
/// so. Here the write and the enqueue happen under the collection's lock,
/// and a pass decides about a resource under the same lock
/// ([`cosmic_pim_caldav::CalDavStore::exclusively`]), so a pass sees either
/// the collection before the save or the save already queued.
///
/// `write` must not call [`queue_save`], [`queue_delete`], this function, or
/// anything else that opens the same collection's sync state: the lock is
/// held around it and is not re-entrant. It should do its write and return.
///
/// # Errors
///
/// An error means nothing was written: either the collection's sync state
/// could not be opened or locked, in which case `write` was not run, or
/// `write` itself failed. A failure to *queue* after a successful write is
/// not an error here; it is [`Saved::queued`].
pub fn save_and_queue<T, E>(
    root: &Path,
    collection_id: &str,
    file_names: &[&str],
    write: impl FnOnce() -> std::result::Result<T, E>,
) -> Result<Saved<T>>
where
    Error: From<E>,
{
    save_and_queue_creating(root, collection_id, file_names, || {
        write().map(|value| (value, Vec::new()))
    })
}

/// [`save_and_queue`], for a write that also creates files whose names it
/// learns only by writing them.
///
/// `write` returns its value and the names of the files it created, and each
/// of those is queued with the rest, under the same lock: an upload if it is
/// there when `write` returns. Such a file has no bytes from before the write
/// to merge against, so it is queued with no base. A file that may already
/// exist belongs in `file_names`, where its pre-edit bytes are kept and an
/// unchanged file is skipped; a name in both lists is queued once, as a
/// `file_names` entry.
///
/// # Errors
///
/// As [`save_and_queue`].
pub fn save_and_queue_creating<T, E>(
    root: &Path,
    collection_id: &str,
    file_names: &[&str],
    write: impl FnOnce() -> std::result::Result<(T, Vec<String>), E>,
) -> Result<Saved<T>>
where
    Error: From<E>,
{
    let Some(mut store) = syncing_store(root, collection_id)? else {
        let (value, _created) = write()?;
        return Ok(Saved {
            value,
            queued: Ok(false),
        });
    };

    let dir = store.collection().path.clone();
    let mut written = None;
    let queued = store.exclusively(|store| {
        // What is on disk now, under the lock: the hrefs and etags the
        // enqueue below reads, as the last pass left them.
        store.reload()?;
        let before: Vec<std::io::Result<Vec<u8>>> = file_names
            .iter()
            .map(|name| std::fs::read(dir.join(name)))
            .collect();

        let created = match write() {
            Ok((value, created)) => {
                written = Some(Ok(value));
                created
            }
            Err(why) => {
                written = Some(Err(why));
                return Ok(false);
            }
        };

        let mut queued = false;
        for (name, before) in file_names.iter().zip(&before) {
            if unchanged(before, &dir.join(name)) {
                continue;
            }
            let base = before
                .as_ref()
                .ok()
                .and_then(|bytes| std::str::from_utf8(bytes).ok());
            queued |= queue_what_is_there(store, &dir, name, base)?;
        }
        for name in created
            .iter()
            .filter(|name| !file_names.contains(&name.as_str()))
        {
            queued |= queue_what_is_there(store, &dir, name, None)?;
        }
        Ok(queued)
    });

    match written {
        Some(Ok(value)) => Ok(Saved {
            value,
            queued: queued.map_err(Error::CalDav),
        }),
        Some(Err(why)) => Err(why.into()),
        // The step never reached the write.
        None => Err(Error::CalDav(queued.err().unwrap_or_else(|| {
            cosmic_pim_caldav::Error::internal(
                "the collection's lock was taken but the write was not run",
            )
        }))),
    }
}

/// Whether the file at `path` is as it was before a write: the same bytes, or
/// absent both times. A file that could not be read either time counts as
/// changed, so it is queued rather than skipped.
fn unchanged(before: &std::io::Result<Vec<u8>>, path: &Path) -> bool {
    use std::io::ErrorKind::NotFound;
    match (before, std::fs::read(path)) {
        (Ok(before), Ok(after)) => *before == after,
        (Err(before), Err(after)) => before.kind() == NotFound && after.kind() == NotFound,
        _ => false,
    }
}

/// The sync state of a collection that has a server to push to, or `None`
/// for one that does not: unknown, never bound, marked local-only, or
/// read-only on the server.
fn syncing_store(root: &Path, collection_id: &str) -> Result<Option<VdirStore>> {
    let Some(meta) = crate::provision::open_collection(root, collection_id) else {
        return Ok(None);
    };
    // A collection the user disconnected keeps its binding but must not
    // queue: the queue is durable, and an entry accepted now would push the
    // moment the marker came off — hours or months later, unasked.
    if cosmic_pim_caldav::is_local_only(&meta.path) {
        return Ok(None);
    }
    let store = VdirStore::open(meta)?;
    if store.href().is_none() {
        return Ok(None);
    }
    if store.is_read_only() {
        // The server will 403 this. Queueing it anyway would leave an entry
        // that can never drain and a UI that claims changes are pending
        // forever.
        tracing::warn!(
            collection_id,
            "edit saved to a collection the server made read-only; it will not be uploaded"
        );
        return Ok(None);
    }
    Ok(Some(store))
}

/// Queues what a local change left in `file_name`: an upload when the file
/// is there, a deletion when it is gone and the server has it.
fn queue_what_is_there(
    store: &mut VdirStore,
    dir: &Path,
    file_name: &str,
    base: Option<&str>,
) -> cosmic_pim_caldav::Result<bool> {
    if dir.join(file_name).exists() {
        let Some(href) = store.href_for_file(file_name) else {
            return Ok(false);
        };
        store.queue_put_with_base(&href, base)?;
        return Ok(true);
    }
    // Only a file the server knows needs a DELETE; one created and removed
    // between two syncs was never uploaded.
    let Some((href, _)) = store.entry_for_by_file(file_name) else {
        return Ok(false);
    };
    store.queue_delete(&href)?;
    Ok(true)
}

/// Queues a locally saved event for upload.
///
/// Returns whether anything was queued — `false` means the collection is not
/// CalDAV-backed.
///
/// This queues a save that has already happened. A sync pass that pulls the
/// same resource between the save and this call overwrites the save; see
/// [`save_and_queue`], which closes that gap and is what an application
/// should call.
///
/// Prefer [`queue_save_with_base`] where the caller read the file before
/// overwriting it: the pre-edit bytes are what make an automatic three-way
/// merge possible if the server turns out to have changed the same resource.
/// This form queues with no base, which merely disables that merge.
pub fn queue_save(root: &Path, collection_id: &str, file_name: &str) -> Result<bool> {
    queue_save_with_base(root, collection_id, file_name, None)
}

/// [`queue_save`], with the file's pre-edit contents.
///
/// `previous` is what the file held *before* the save this call is queueing —
/// exactly the text the server last acknowledged, when the file was in sync at
/// the moment the edit began. The queue keeps it from the first enqueue only
/// (see `VdirStore::queue_put_with_base`), so callers pass what they read and
/// never need to reason about earlier unsent edits themselves.
pub fn queue_save_with_base(
    root: &Path,
    collection_id: &str,
    file_name: &str,
    previous: Option<&str>,
) -> Result<bool> {
    let Some(meta) = crate::provision::open_collection(root, collection_id) else {
        return Ok(false);
    };

    // A collection the user disconnected keeps its binding but must not
    // queue: the queue is durable, and an entry accepted now would push the
    // moment the marker came off — hours or months later, unasked.
    if cosmic_pim_caldav::is_local_only(&meta.path) {
        return Ok(false);
    }

    let mut store = VdirStore::open(meta)?;

    if store.is_read_only() {
        // The server will 403 this. Queueing it anyway would leave an entry
        // that can never drain and a UI that claims changes are pending
        // forever.
        tracing::warn!(
            collection_id,
            "edit saved to a collection the server made read-only; it will not be uploaded"
        );
        return Ok(false);
    }

    let Some(href) = store.href_for_file(file_name) else {
        return Ok(false);
    };
    store.queue_put_with_base(&href, previous)?;
    Ok(true)
}

/// Queues a locally deleted event for removal on the server.
///
/// Safe to call either side of the local delete: the coordinates live in the
/// sidecar, which the storage layer never touches.
pub fn queue_delete(root: &Path, collection_id: &str, file_name: &str) -> Result<bool> {
    let Some(meta) = crate::provision::open_collection(root, collection_id) else {
        return Ok(false);
    };
    if cosmic_pim_caldav::is_local_only(&meta.path) {
        return Ok(false);
    }
    let mut store = VdirStore::open(meta)?;

    if store.is_read_only() {
        return Ok(false);
    }

    // Only a file the server actually knows about needs a DELETE. An event
    // created and removed locally between two syncs was never uploaded, and
    // asking the server to delete it would just 404.
    let Some(href) = store.entry_for_by_file(file_name).map(|(href, _)| href) else {
        return Ok(false);
    };

    store.queue_delete(&href)?;
    Ok(true)
}

#[cfg(test)]
mod tests {
    use super::*;
    use cosmic_pim_caldav::push::PushQueue;
    use cosmic_pim_caldav::{CalDavStore, RemoteEvent};
    use cosmic_pim_core::StoreError;
    use cosmic_pim_core::model::Rgb;
    use cosmic_pim_core::store::vdir;

    const ICS: &str = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//x//EN\r\n\
BEGIN:VEVENT\r\nUID:a@test\r\nDTSTART:20260804T090000Z\r\nSUMMARY:X\r\n\
END:VEVENT\r\nEND:VCALENDAR\r\n";

    fn collection(bind: bool) -> (tempfile::TempDir, String) {
        let dir = tempfile::tempdir().unwrap();
        let meta = vdir::create_collection(dir.path(), "Personal", Rgb(1, 2, 3)).unwrap();
        let id = meta.id.clone();
        if bind {
            let mut store = VdirStore::open(meta).unwrap();
            store.set_remote("/dav/cal/", false).unwrap();
        }
        (dir, id)
    }

    fn pending(root: &Path, id: &str) -> Vec<String> {
        let meta = crate::provision::open_collection(root, id).unwrap();
        VdirStore::open(meta)
            .unwrap()
            .pending()
            .unwrap()
            .into_iter()
            .map(|p| p.op.href().to_owned())
            .collect()
    }

    #[test]
    fn a_save_to_a_local_only_calendar_queues_nothing() {
        let (dir, id) = collection(false);
        assert!(!queue_save(dir.path(), &id, "a.ics").unwrap());
        assert_eq!(pending(dir.path(), &id), [] as [String; 0]);
    }

    #[test]
    fn a_save_to_a_synced_calendar_queues_a_put_under_the_collection_href() {
        let (dir, id) = collection(true);
        assert!(queue_save(dir.path(), &id, "a.ics").unwrap());
        assert_eq!(pending(dir.path(), &id), vec!["/dav/cal/a.ics".to_string()]);
    }

    #[test]
    fn a_save_to_a_known_file_reuses_its_existing_href() {
        let (dir, id) = collection(true);
        {
            let meta = crate::provision::open_collection(dir.path(), &id).unwrap();
            let mut store = VdirStore::open(meta).unwrap();
            // The server named it something quite unlike the file name.
            store
                .upsert(&RemoteEvent {
                    href: "/dav/cal/8f3c-aa11.ics".into(),
                    etag: "\"1\"".into(),
                    ics: ICS.into(),
                })
                .unwrap();
        }
        assert!(queue_save(dir.path(), &id, "8f3c-aa11.ics").unwrap());
        assert_eq!(
            pending(dir.path(), &id),
            vec!["/dav/cal/8f3c-aa11.ics".to_string()],
            "a synced event was queued under a freshly derived href instead of its own"
        );
    }

    #[test]
    fn a_read_only_collection_queues_nothing() {
        let dir = tempfile::tempdir().unwrap();
        let meta = vdir::create_collection(dir.path(), "Shared", Rgb(1, 2, 3)).unwrap();
        let id = meta.id.clone();
        VdirStore::open(meta)
            .unwrap()
            .set_remote("/dav/shared/", true)
            .unwrap();

        assert!(!queue_save(dir.path(), &id, "a.ics").unwrap());
        assert!(
            pending(dir.path(), &id).is_empty(),
            "queued a push that can only ever 403"
        );
    }

    #[test]
    fn deleting_an_event_the_server_never_saw_queues_nothing() {
        let (dir, id) = collection(true);
        assert!(
            !queue_delete(dir.path(), &id, "never-synced.ics").unwrap(),
            "queued a DELETE for a resource that does not exist on the server"
        );
    }

    #[test]
    fn deleting_a_synced_event_queues_a_delete() {
        let (dir, id) = collection(true);
        {
            let meta = crate::provision::open_collection(dir.path(), &id).unwrap();
            let mut store = VdirStore::open(meta).unwrap();
            store
                .upsert(&RemoteEvent {
                    href: "/dav/cal/a.ics".into(),
                    etag: "\"1\"".into(),
                    ics: ICS.into(),
                })
                .unwrap();
        }
        assert!(queue_delete(dir.path(), &id, "a.ics").unwrap());
        assert_eq!(pending(dir.path(), &id), vec!["/dav/cal/a.ics".to_string()]);
    }

    #[test]
    fn the_pre_edit_bytes_reach_the_queue_as_the_merge_base() {
        let (dir, id) = collection(true);
        {
            let meta = crate::provision::open_collection(dir.path(), &id).unwrap();
            let mut store = VdirStore::open(meta).unwrap();
            store
                .upsert(&RemoteEvent {
                    href: "/dav/cal/a.ics".into(),
                    etag: "\"1\"".into(),
                    ics: ICS.into(),
                })
                .unwrap();
        }

        assert!(queue_save_with_base(dir.path(), &id, "a.ics", Some(ICS)).unwrap());

        let meta = crate::provision::open_collection(dir.path(), &id).unwrap();
        let mut store = VdirStore::open(meta).unwrap();
        assert_eq!(
            store.unpushed_base("/dav/cal/a.ics").unwrap().as_deref(),
            Some(ICS),
            "the base was dropped between the app and the queue"
        );
    }

    const SERVER_V1: &str = "BEGIN:VCALENDAR\r\nX-V:1\r\nEND:VCALENDAR\r\n";
    const LOCAL_EDIT: &str = "BEGIN:VCALENDAR\r\nX-V:1-mine\r\nEND:VCALENDAR\r\n";
    const SERVER_V2: &str = "BEGIN:VCALENDAR\r\nX-V:2-theirs\r\nEND:VCALENDAR\r\n";

    /// A bound collection holding one synced resource, `a.ics`.
    fn synced() -> (tempfile::TempDir, String, std::path::PathBuf) {
        let (dir, id) = collection(true);
        let meta = crate::provision::open_collection(dir.path(), &id).unwrap();
        let file = meta.path.join("a.ics");
        VdirStore::open(meta)
            .unwrap()
            .upsert(&RemoteEvent {
                href: "/dav/cal/a.ics".into(),
                etag: "\"1\"".into(),
                ics: SERVER_V1.into(),
            })
            .unwrap();
        (dir, id, file)
    }

    /// What a sync pass does with one resource the server changed: ask
    /// whether an edit is waiting, and take the server's copy if not.
    fn pull(root: &Path, id: &str) {
        let meta = crate::provision::open_collection(root, id).unwrap();
        let mut store = VdirStore::open(meta).unwrap();
        store
            .exclusively(|store| {
                if store.unpushed_local("/dav/cal/a.ics")?.is_none() {
                    store.upsert(&RemoteEvent {
                        href: "/dav/cal/a.ics".into(),
                        etag: "\"2\"".into(),
                        ics: SERVER_V2.into(),
                    })?;
                }
                Ok(())
            })
            .unwrap();
    }

    #[test]
    fn a_pull_cannot_land_between_a_save_and_its_enqueue() {
        // The save writes the file; the pull arrives before the enqueue.
        // With the two as separate steps the pull found a changed file and
        // nothing queued, and wrote the server's copy over the edit.
        let (dir, id, file) = synced();
        let (written, pull_may_run) = std::sync::mpsc::channel();

        std::thread::scope(|scope| {
            scope.spawn(|| {
                let saved = save_and_queue(dir.path(), &id, &["a.ics"], || {
                    std::fs::write(&file, LOCAL_EDIT)?;
                    written.send(()).unwrap();
                    // The gap a pull used to fit into.
                    std::thread::sleep(std::time::Duration::from_millis(150));
                    Ok::<_, StoreError>(())
                })
                .unwrap();
                assert!(saved.queued.unwrap());
            });
            pull_may_run.recv().unwrap();
            pull(dir.path(), &id);
        });

        assert_eq!(
            std::fs::read_to_string(&file).unwrap(),
            LOCAL_EDIT,
            "a pull overwrote a save that had not been queued yet"
        );
        assert_eq!(pending(dir.path(), &id), ["/dav/cal/a.ics"]);
    }

    #[test]
    fn the_bytes_before_the_write_become_the_merge_base() {
        let (dir, id, file) = synced();
        save_and_queue(dir.path(), &id, &["a.ics"], || {
            std::fs::write(&file, LOCAL_EDIT).map_err(StoreError::from)
        })
        .unwrap();

        let meta = crate::provision::open_collection(dir.path(), &id).unwrap();
        assert_eq!(
            VdirStore::open(meta)
                .unwrap()
                .unpushed_base("/dav/cal/a.ics")
                .unwrap()
                .as_deref(),
            Some(SERVER_V1)
        );
    }

    #[test]
    fn a_write_that_fails_queues_nothing_and_is_the_error() {
        let (dir, id, _file) = synced();
        let result = save_and_queue(dir.path(), &id, &["a.ics"], || {
            Err::<(), _>(StoreError::from(std::io::Error::other("disk full")))
        });
        assert!(matches!(result, Err(Error::Store(_))), "{result:?}");
        assert_eq!(pending(dir.path(), &id), [] as [String; 0]);
    }

    #[test]
    fn a_write_that_removes_the_file_queues_its_deletion() {
        let (dir, id, file) = synced();
        let saved = save_and_queue(dir.path(), &id, &["a.ics", "never-synced.ics"], || {
            std::fs::remove_file(&file).map_err(StoreError::from)
        })
        .unwrap();
        assert!(saved.queued.unwrap());

        let meta = crate::provision::open_collection(dir.path(), &id).unwrap();
        let queue = VdirStore::open(meta).unwrap().pending().unwrap();
        assert_eq!(queue.len(), 1, "{queue:?}");
        assert!(
            matches!(&queue[0].op, cosmic_pim_caldav::push::PushOp::Delete { href, .. } if href == "/dav/cal/a.ics")
        );
    }

    #[test]
    fn a_write_that_changes_nothing_queues_nothing() {
        // A stale invitation, a save with nothing to save: every file is as
        // it was, and queueing them uploaded a copy the server already has.
        let (dir, id, file) = synced();
        let untouched = save_and_queue(dir.path(), &id, &["a.ics", "never-synced.ics"], || {
            Ok::<_, StoreError>(())
        })
        .unwrap();
        assert!(!untouched.queued.unwrap(), "an untouched file was queued");

        let same = save_and_queue(dir.path(), &id, &["a.ics"], || {
            std::fs::write(&file, SERVER_V1).map_err(StoreError::from)
        })
        .unwrap();
        assert!(!same.queued.unwrap(), "rewriting the same bytes was queued");
        assert_eq!(pending(dir.path(), &id), [] as [String; 0]);
    }

    #[test]
    fn a_file_the_write_creates_is_queued_with_it() {
        let (dir, id, _file) = synced();
        let path = crate::provision::open_collection(dir.path(), &id)
            .unwrap()
            .path;
        let saved = save_and_queue_creating(dir.path(), &id, &["a.ics"], || {
            // The name is the write's own choice, known only once it ran.
            std::fs::write(path.join("b-1.ics"), LOCAL_EDIT)?;
            Ok::<_, StoreError>(("made", vec!["b-1.ics".to_owned()]))
        })
        .unwrap();

        assert_eq!(saved.value, "made");
        assert!(saved.queued.unwrap());
        assert_eq!(
            pending(dir.path(), &id),
            ["/dav/cal/b-1.ics"],
            "the created file was not queued, or the unchanged one was"
        );

        // With no server, the write still runs and nothing is queued.
        let (local, local_id) = collection(false);
        let saved = save_and_queue_creating(local.path(), &local_id, &[], || {
            Ok::<_, StoreError>(((), vec!["c.ics".to_owned()]))
        })
        .unwrap();
        assert!(!saved.queued.unwrap());
    }

    #[test]
    fn a_collection_with_no_server_is_written_and_not_queued() {
        let (dir, id) = collection(false);
        let saved = save_and_queue(dir.path(), &id, &["a.ics"], || {
            Ok::<_, StoreError>("written")
        })
        .unwrap();
        assert_eq!(saved.value, "written");
        assert!(!saved.queued.unwrap());

        // Unknown and disconnected collections behave the same way.
        let saved = save_and_queue(dir.path(), "no-such-collection", &["a.ics"], || {
            Ok::<_, StoreError>(())
        })
        .unwrap();
        assert!(!saved.queued.unwrap());
    }

    #[test]
    fn an_unknown_collection_is_not_an_error() {
        let dir = tempfile::tempdir().unwrap();
        assert!(!queue_save(dir.path(), "no-such-collection", "a.ics").unwrap());
        assert!(!queue_delete(dir.path(), "no-such-collection", "a.ics").unwrap());
    }

    #[test]
    fn a_disconnected_collection_queues_nothing_despite_its_binding() {
        // The marker's whole reason: bound + synced once + marked local-only
        // must behave like a local calendar, not like a paused one whose
        // queue silently fills.
        let (dir, id) = collection(true);
        {
            let meta = crate::provision::open_collection(dir.path(), &id).unwrap();
            cosmic_pim_caldav::mark_local_only(&meta.path).unwrap();
        }

        assert!(!queue_save(dir.path(), &id, "a.ics").unwrap());
        assert!(!queue_delete(dir.path(), &id, "a.ics").unwrap());
        assert_eq!(pending(dir.path(), &id), [] as [String; 0]);

        // Unmarking reconnects, with the binding intact.
        {
            let meta = crate::provision::open_collection(dir.path(), &id).unwrap();
            cosmic_pim_caldav::unmark_local_only(&meta.path).unwrap();
        }
        assert!(queue_save(dir.path(), &id, "a.ics").unwrap());
    }
}