sapphire-framework-server 0.15.0

App server skeleton for sapphire-framework: owns a workspace's cache and serves it over IPC
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
//! The `sync.*` methods. Each acts on the server's current workspace (#215).

use std::sync::Arc;

use sapphire_backend::protocol as proto;
use sapphire_backend::protocol::{SYNC_MAP, SyncMapParams};
use sapphire_ipc::{Router, RpcError};

use crate::current::Current;
use crate::handlers::rpc_error;

/// Add `sync.enable`, `sync.disable`, `sync.map` and `sync.status` to `router`.
///
/// `current` must have a sync runtime: a server without one does not serve `sync.*`.
pub fn sync_router(current: Arc<Current>, router: Router) -> Router {
    let enable = Arc::clone(&current);
    let map = Arc::clone(&current);
    let disable = Arc::clone(&current);
    let status = current;

    router
        .method(proto::SYNC_ENABLE, move |_| {
            let current = Arc::clone(&enable);
            async move {
                let workspace_id = current.enable_sync().await.map_err(|e| rpc_error(&e))?;
                serde_json::to_value(proto::SyncEnableResult { workspace_id })
                    .map_err(|e| RpcError::internal(e.to_string()))
            }
        })
        .method(SYNC_MAP, move |ctx| {
            let current = Arc::clone(&map);
            async move {
                let p: SyncMapParams = serde_json::from_value(ctx.params)
                    .map_err(|e| RpcError::invalid_params(format!("bad parameters: {e}")))?;
                let workspace_id = current
                    .map(&p.workspace, &p.dir)
                    .await
                    .map_err(|e| rpc_error(&e))?;
                serde_json::to_value(proto::SyncEnableResult { workspace_id })
                    .map_err(|e| RpcError::internal(e.to_string()))
            }
        })
        .method(proto::SYNC_DISABLE, move |_| {
            let current = Arc::clone(&disable);
            async move {
                current.disable_sync().await.map_err(|e| rpc_error(&e))?;
                serde_json::to_value(proto::Ack {}).map_err(|e| RpcError::internal(e.to_string()))
            }
        })
        .method(proto::SYNC_STATUS, move |_| {
            let current = Arc::clone(&status);
            async move {
                // Never fails on account of the bridge: a status call that errored when the
                // bridge was down would read as an outage of the app server itself.
                serde_json::to_value(current.status().await)
                    .map_err(|e| RpcError::internal(e.to_string()))
            }
        })
}

impl From<crate::sync::SyncStatus> for proto::SyncStatusResult {
    fn from(status: crate::sync::SyncStatus) -> Self {
        proto::SyncStatusResult {
            enabled: status.enabled,
            workspace_id: status.workspace_id,
            peers: status.peers,
            paused: status.paused,
            last_error: status.last_error,
            bridge_available: status.bridge_available,
            topology: status.topology,
            embedding: status.embedding,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sync::SyncRuntime;
    use crate::sync::testing::StubBridge;
    use crate::test_support;
    use sapphire_ipc::{Client, ClientInfo, Connection, ManagedBy, ServerInfo, serve};
    use sapphire_workspace::{AppContext, AppKind};
    use std::ffi::OsString;
    use std::path::PathBuf;

    static CTX: AppContext = AppContext::new("sapphire-syncmethods");

    /// The env vars `CTX.init` reads.
    const DIR_VARS: [&str; 3] = [
        "SAPPHIRE_SYNCMETHODS_CACHE_DIR",
        "SAPPHIRE_SYNCMETHODS_DATA_DIR",
        "SAPPHIRE_SYNCMETHODS_CONFIG_DIR",
    ];

    /// Points the context's directories at the test's scratch tree, and restores the
    /// previous values when dropped — including while unwinding from a panic.
    struct EnvGuard {
        previous: [Option<OsString>; 3],
        _lock: std::sync::MutexGuard<'static, ()>,
    }

    impl Drop for EnvGuard {
        fn drop(&mut self) {
            // SAFETY: `self._lock` still serialises the environment; it is dropped only
            // after this method returns.
            for (name, previous) in DIR_VARS.iter().zip(self.previous.iter_mut()) {
                match previous.take() {
                    Some(value) => unsafe { std::env::set_var(name, value) },
                    None => test_support::remove(name),
                }
            }
        }
    }

    /// A server exposing only the sync namespace, plus a workspace root.
    ///
    /// The environment lock is held for the whole test, as in the crate's other test
    /// modules: the context is a `static` shared by this binary, and every env mutation in
    /// these tests goes through `test_support` for that reason.
    struct Fixture {
        _tmp: tempfile::TempDir,
        _env: EnvGuard,
        root: PathBuf,
        client: Client,
        _stub: StubBridge,
    }

    /// [`bare`], with its workspace selected.
    async fn fixture() -> Fixture {
        let f = bare().await;
        let _: proto::CurrentWorkspace = f
            .client
            .call(
                proto::WORKSPACE_SELECT,
                proto::WorkspaceSelectParams {
                    dir: f.root.clone(),
                },
            )
            .await
            .unwrap();
        f
    }

    async fn bare() -> Fixture {
        let lock = test_support::lock();
        let tmp = tempfile::tempdir().unwrap();
        let previous = DIR_VARS.map(std::env::var_os);
        // SAFETY (via `test_support::set`): `lock` serialises every read and write of the
        // process environment in this test binary, and it is held until `drop` has restored
        // the old values.
        for (name, dir) in DIR_VARS
            .iter()
            .zip(["cache", "data", "config"].map(|cat| tmp.path().join(cat)))
        {
            test_support::set(name, &dir);
        }
        let env = EnvGuard {
            previous,
            _lock: lock,
        };
        CTX.init(AppKind::Server);

        let root = tmp.path().join("ws");
        std::fs::create_dir_all(root.join(".sapphire-syncmethods")).unwrap();
        let root = root.canonicalize().unwrap();

        let (stub, bridge) = StubBridge::start().await;
        let runtime = Arc::new(SyncRuntime::new(
            &CTX,
            bridge,
            "/bin/true".into(),
            ManagedBy::Service,
        ));
        let current = Arc::new(crate::current::Current::new(
            &CTX,
            Arc::new(crate::host::WorkspaceHost::new(&CTX)),
            Some(runtime),
            crate::selection::SelectionFile::at(tmp.path().join("workspace.toml")),
        ));
        let router = Arc::new(sync_router(
            Arc::clone(&current),
            crate::current::current_methods(current, sapphire_ipc::Router::new()),
        ));

        let (client_conn, server_conn) = Connection::pair();
        tokio::spawn(async move {
            let info = ServerInfo {
                version: "0.0.0".into(),
                api: sapphire_backend::protocol::API_VERSION,
                app_api: None,
                pid: std::process::id(),
                managed_by: ManagedBy::Service,
            };
            let _ = serve(server_conn, router, "sapphire-syncmethods", info).await;
        });
        let info = ClientInfo {
            kind: "cli".into(),
            version: "0.0.0".into(),
            api: sapphire_backend::protocol::API_VERSION,
            app_api: None,
            pid: std::process::id(),
        };
        let (client, _) = Client::handshake(client_conn, "sapphire-syncmethods", info)
            .await
            .unwrap();
        Fixture {
            _tmp: tmp,
            _env: env,
            root,
            client,
            _stub: stub,
        }
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn enabling_returns_the_workspace_id() {
        let f = fixture().await;
        let result: proto::SyncEnableResult = f
            .client
            .call(proto::SYNC_ENABLE, serde_json::json!({}))
            .await
            .unwrap();
        assert!(!result.workspace_id.to_string().is_empty());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn status_before_enabling_reports_disabled() {
        let f = fixture().await;
        let status: proto::SyncStatusResult = f
            .client
            .call(proto::SYNC_STATUS, serde_json::json!({}))
            .await
            .unwrap();
        assert!(!status.enabled);
        assert!(status.workspace_id.is_none());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn status_after_enabling_reports_the_same_id() {
        let f = fixture().await;
        let enabled: proto::SyncEnableResult = f
            .client
            .call(proto::SYNC_ENABLE, serde_json::json!({}))
            .await
            .unwrap();
        let status: proto::SyncStatusResult = f
            .client
            .call(proto::SYNC_STATUS, serde_json::json!({}))
            .await
            .unwrap();

        assert!(status.enabled);
        assert_eq!(status.workspace_id, Some(enabled.workspace_id));
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn disabling_an_unsynced_workspace_is_not_an_error() {
        let f = fixture().await;
        let _: proto::Ack = f
            .client
            .call(proto::SYNC_DISABLE, serde_json::json!({}))
            .await
            .expect("disabling what was never enabled is fine");
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn without_a_workspace_sync_is_an_invalid_parameter() {
        let f = bare().await;
        let err = f
            .client
            .call::<_, proto::SyncEnableResult>(proto::SYNC_ENABLE, serde_json::json!({}))
            .await
            .unwrap_err();
        match err {
            sapphire_ipc::Error::Rpc(e) => {
                assert_eq!(e.code, sapphire_ipc::codes::INVALID_PARAMS, "{}", e.message);
                assert!(e.message.contains("workspace select"), "{}", e.message);
            }
            other => panic!("got {other:?}"),
        }
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn enabling_is_remembered_and_selecting_again_resumes_it() {
        let f = fixture().await;
        let _: proto::SyncEnableResult = f
            .client
            .call(proto::SYNC_ENABLE, serde_json::json!({}))
            .await
            .unwrap();
        let saved = std::fs::read_to_string(f._tmp.path().join("workspace.toml")).unwrap();
        assert!(saved.contains("sync = true"), "{saved}");
        // The workspace now has a sync id, so selecting it turns sync on by itself.
        let _: proto::Ack = f
            .client
            .call(proto::SYNC_DISABLE, serde_json::json!({}))
            .await
            .unwrap();
        let again: proto::CurrentWorkspace = f
            .client
            .call(
                proto::WORKSPACE_SELECT,
                proto::WorkspaceSelectParams {
                    dir: f.root.clone(),
                },
            )
            .await
            .unwrap();
        assert!(again.sync.enabled);
    }
}

#[cfg(test)]
mod map_tests {
    use super::*;
    use crate::sync::SyncRuntime;
    use crate::sync::testing::StubBridge;
    use crate::test_support;
    use sapphire_bridge_api::WorkgroupWorkspaceInfo;
    use sapphire_ipc::{Client, ClientInfo, Connection, ManagedBy, ServerInfo, serve};
    use sapphire_workspace::{AppContext, AppKind};
    use std::ffi::OsString;
    use std::path::PathBuf;

    static CTX: AppContext = AppContext::new("sapphire-syncmap");

    /// The env vars `CTX.init` reads.
    const DIR_VARS: [&str; 3] = [
        "SAPPHIRE_SYNCMAP_CACHE_DIR",
        "SAPPHIRE_SYNCMAP_DATA_DIR",
        "SAPPHIRE_SYNCMAP_CONFIG_DIR",
    ];

    /// Points the context's directories at the test's scratch tree, and restores the
    /// previous values when dropped — including while unwinding from a panic.
    struct EnvGuard {
        previous: [Option<OsString>; 3],
        _lock: std::sync::MutexGuard<'static, ()>,
    }

    impl Drop for EnvGuard {
        fn drop(&mut self) {
            // SAFETY: `self._lock` still serialises the environment; it is dropped only
            // after this method returns.
            for (name, previous) in DIR_VARS.iter().zip(self.previous.iter_mut()) {
                match previous.take() {
                    Some(value) => unsafe { std::env::set_var(name, value) },
                    None => test_support::remove(name),
                }
            }
        }
    }

    /// A server exposing only the sync namespace, plus a workspace root.
    struct Fixture {
        _tmp: tempfile::TempDir,
        _env: EnvGuard,
        root: PathBuf,
        client: Client,
        stub: StubBridge,
    }

    async fn fixture() -> Fixture {
        let lock = test_support::lock();
        let tmp = tempfile::tempdir().unwrap();
        let previous = DIR_VARS.map(std::env::var_os);
        // SAFETY (via `test_support::set`): `lock` serialises every read and write of the
        // process environment in this test binary, and it is held until `drop` has restored
        // the old values.
        for (name, dir) in DIR_VARS
            .iter()
            .zip(["cache", "data", "config"].map(|cat| tmp.path().join(cat)))
        {
            test_support::set(name, &dir);
        }
        let env = EnvGuard {
            previous,
            _lock: lock,
        };
        CTX.init(AppKind::Server);

        let root = tmp.path().join("ws");
        std::fs::create_dir_all(root.join(".sapphire-syncmap")).unwrap();
        let root = root.canonicalize().unwrap();

        let (stub, bridge) = StubBridge::start().await;
        let runtime = Arc::new(SyncRuntime::new(
            &CTX,
            bridge,
            "/bin/true".into(),
            ManagedBy::Service,
        ));
        let current = Arc::new(crate::current::Current::new(
            &CTX,
            Arc::new(crate::host::WorkspaceHost::new(&CTX)),
            Some(runtime),
            crate::selection::SelectionFile::at(tmp.path().join("workspace.toml")),
        ));
        let router = Arc::new(sync_router(
            Arc::clone(&current),
            crate::current::current_methods(current, sapphire_ipc::Router::new()),
        ));

        let (client_conn, server_conn) = Connection::pair();
        tokio::spawn(async move {
            let info = ServerInfo {
                version: "0.0.0".into(),
                api: sapphire_backend::protocol::API_VERSION,
                app_api: None,
                pid: std::process::id(),
                managed_by: ManagedBy::Service,
            };
            let _ = serve(server_conn, router, "sapphire-syncmap", info).await;
        });
        let info = ClientInfo {
            kind: "cli".into(),
            version: "0.0.0".into(),
            api: sapphire_backend::protocol::API_VERSION,
            app_api: None,
            pid: std::process::id(),
        };
        let (client, _) = Client::handshake(client_conn, "sapphire-syncmap", info)
            .await
            .unwrap();
        Fixture {
            _tmp: tmp,
            _env: env,
            root,
            client,
            stub,
        }
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn mapping_by_name_writes_the_map_and_enables_sync() {
        let f = fixture().await;
        let id = grain_id::GrainId::random();
        f.stub.list(WorkgroupWorkspaceInfo {
            workspace_id: id,
            app_name: "sapphire-syncmap".into(),
            name: "notes".into(),
        });

        let result: proto::SyncEnableResult = f
            .client
            .call(
                proto::SYNC_MAP,
                proto::SyncMapParams {
                    workspace: "notes".into(),
                    dir: f.root.clone(),
                },
            )
            .await
            .unwrap();

        assert_eq!(result.workspace_id, id, "the mapped workspace's id is used");
        let map = f
            .root
            .join(".sapphire-syncmap")
            .join(crate::sync::WORKSPACE_MAP_FILE);
        assert_eq!(
            std::fs::read_to_string(&map).unwrap().trim(),
            id.to_string(),
            "the map names the workspace"
        );

        let status: proto::SyncStatusResult = f
            .client
            .call(proto::SYNC_STATUS, serde_json::json!({}))
            .await
            .unwrap();
        assert!(status.enabled, "mapping enabled sync");
        assert_eq!(status.workspace_id, Some(id));
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn mapping_by_id_works_too() {
        let f = fixture().await;
        let id = grain_id::GrainId::random();
        f.stub.list(WorkgroupWorkspaceInfo {
            workspace_id: id,
            app_name: "sapphire-syncmap".into(),
            name: "notes".into(),
        });

        let result: proto::SyncEnableResult = f
            .client
            .call(
                proto::SYNC_MAP,
                proto::SyncMapParams {
                    workspace: id.to_string(),
                    dir: f.root.clone(),
                },
            )
            .await
            .unwrap();
        assert_eq!(result.workspace_id, id);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn an_unknown_workspace_is_an_invalid_parameter() {
        let f = fixture().await;
        let err = f
            .client
            .call::<_, proto::SyncEnableResult>(
                proto::SYNC_MAP,
                proto::SyncMapParams {
                    workspace: "nowhere".into(),
                    dir: f.root.clone(),
                },
            )
            .await
            .unwrap_err();
        match err {
            sapphire_ipc::Error::Rpc(e) => {
                assert_eq!(e.code, sapphire_ipc::codes::INVALID_PARAMS, "{}", e.message);
            }
            other => panic!("got {other:?}"),
        }
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn another_applications_workspace_is_refused() {
        let f = fixture().await;
        let id = grain_id::GrainId::random();
        f.stub.list(WorkgroupWorkspaceInfo {
            workspace_id: id,
            app_name: "sapphire-journal".into(),
            name: "notes".into(),
        });

        let err = f
            .client
            .call::<_, proto::SyncEnableResult>(
                proto::SYNC_MAP,
                proto::SyncMapParams {
                    workspace: "notes".into(),
                    dir: f.root.clone(),
                },
            )
            .await
            .unwrap_err();
        match err {
            sapphire_ipc::Error::Rpc(e) => {
                assert_eq!(e.code, sapphire_ipc::codes::INVALID_PARAMS, "{}", e.message);
                assert!(
                    e.message.contains("sapphire-journal"),
                    "it names the owning application: {}",
                    e.message
                );
            }
            other => panic!("got {other:?}"),
        }
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn a_directory_that_is_not_a_workspace_is_refused() {
        let f = fixture().await;
        let plain = f._tmp.path().join("plain");
        std::fs::create_dir_all(&plain).unwrap();
        let id = grain_id::GrainId::random();
        f.stub.list(WorkgroupWorkspaceInfo {
            workspace_id: id,
            app_name: "sapphire-syncmap".into(),
            name: "notes".into(),
        });

        let err = f
            .client
            .call::<_, proto::SyncEnableResult>(
                proto::SYNC_MAP,
                proto::SyncMapParams {
                    workspace: "notes".into(),
                    dir: plain,
                },
            )
            .await
            .unwrap_err();
        match err {
            sapphire_ipc::Error::Rpc(e) => {
                assert_eq!(e.code, sapphire_ipc::codes::INVALID_PARAMS, "{}", e.message);
            }
            other => panic!("got {other:?}"),
        }
    }
}