sapphire-framework-server 0.27.1

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
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
//! The framework-provided half of an application's CLI.

use std::fmt::Write as _;
use std::path::PathBuf;
use std::sync::Arc;

use corundum_framework_service::{Environment, ServiceCommand, SystemManager};
use sapphire_backend::protocol as proto;
use sapphire_bridge_client::BridgeClient;
use sapphire_ipc::{ClientInfo, Endpoint};

use crate::AppServer;
use crate::error::{Error, Result};
use crate::selection::SelectionFile;
use crate::settings::{AppSettings, Settings};
use sapphire_workspace::AppContext;

mod config;

pub use config::{AppConfigCommand, AppScopeFlags};

pub use sapphire_backend::protocol::{StatusReport, StatusRow};

/// The framework's commands, flattened into an application's CLI (spec decision 2).
///
/// An application composes these beside its own verbs by giving its subcommand enum a
/// `#[command(flatten)] Framework(FrameworkCommand)` variant, so every verb — the app's and
/// the framework's — sits at the same top level. The parse tests below pin that shape.
///
/// A desktop app's binary carries the same enum (#366): it is the CLI as well, so the CLI
/// version is a subset of it. Its subcommand is optional, and with none it opens the
/// window; the CLI version's is required, so a bare invocation prints the help. The verbs
/// are one definition either way:
///
/// ```rust,ignore
/// #[derive(clap::Parser)]
/// struct Desktop {
///     #[command(subcommand)]
///     command: Option<Command>, // `Command` for the CLI version
/// }
///
/// match Desktop::parse().command {
///     None => open_the_window(),
///     Some(command) => runtime.block_on(run(command)),
/// }
/// ```
#[derive(Debug, clap::Subcommand)]
pub enum FrameworkCommand {
    /// Run the server in this process until SIGTERM or SIGINT.
    Serve,
    /// Report whether a server is running, and which version.
    Status,
    /// Install, remove or report this application's operating-system service.
    #[command(subcommand)]
    Service(ServiceCommand),
    /// Replace this executable with the newest release, then do the same for
    /// `sapphire-bridge` (unless `--no-bridge`). Restart the services afterwards to run
    /// them.
    Update(AppUpdateArgs),
    /// Create, select, show and map this application's workspace. The server serves one
    /// at a time.
    #[command(subcommand)]
    Workspace(WorkspaceCommand),
    /// Add, list, retire, restore and rotate the API keys of the workspace this application
    /// serves — for clients that reach it over HTTP. Works on the workspace's files, with or
    /// without a running server; the keys sync with the workspace.
    #[command(subcommand)]
    ApiKey(corundum_framework_keys::KeyCommand),
    /// Renamed to `api-key` (#364).
    #[command(hide = true)]
    ExternalDevice(LegacyExternalDevice),
    /// List, show, set and unset this application's settings and the bridge's, by key.
    /// Without a running server, this application's own, on its files.
    #[command(subcommand)]
    Config(AppConfigCommand),
    /// Serve this application's tools to an AI agent over MCP on stdin/stdout. Talks to the
    /// running server, which it does not start; register this command with the agent.
    #[cfg(feature = "mcp")]
    Mcp,
}

impl FrameworkCommand {
    /// Carry out the command, returning the process exit code.
    ///
    /// The application matches its own variants first, then hands the framework's here,
    /// with what [`serve_restarting`](crate::serve_restarting) takes: its context, its own
    /// settings (`()` for none) and the function that builds its server from the settings.
    /// `serve` runs [`serve_restarting`](crate::serve_restarting); the commands that need an
    /// [`AppServer`] — `service`, `update` and `mcp` — read the settings and call `build`
    /// once; `config` reads and writes the settings through the running server, or the
    /// files themselves when none runs.
    ///
    /// ```rust,ignore
    /// command
    ///     .dispatch(&CTX, MyOwner, |settings: &Settings| AppServer::new(&CTX, VERSION), VERSION)
    ///     .await
    /// ```
    pub async fn dispatch<A, F>(
        self,
        ctx: &'static AppContext,
        owner: A,
        build: F,
        version: &'static str,
    ) -> Result<i32>
    where
        A: AppSettings + Clone,
        F: Fn(&Settings) -> AppServer,
    {
        let app = ctx.app_name;
        // Built only by the commands that need a server, and then once. These are the way
        // out of a bad release or a broken install, so settings that cannot be read are
        // warned about and replaced by the defaults rather than stopping them.
        let server = |owner: A| -> Result<AppServer> {
            Ok(build(&settings_or_defaults(ctx, Arc::new(owner))?))
        };
        match self {
            FrameworkCommand::Serve => {
                crate::serve_restarting(ctx, owner, build).await?;
                Ok(0)
            }
            FrameworkCommand::Status => {
                let endpoint = Endpoint::for_app(app)?;
                run_status(&endpoint, app, version).await
            }
            FrameworkCommand::Service(command) => {
                let spec = server(owner)?.service_spec();
                command
                    .run(&spec, &Environment::detect(), &SystemManager)
                    .map_err(Error::from)
            }
            FrameworkCommand::Update(args) => {
                let server = server(owner)?;
                let own = match server.release_source() {
                    Some(source) => {
                        sapphire_framework_update::run(&args.update, source, app, version)
                            .await
                            .map_err(Error::from)
                    }
                    None => {
                        println!("{app} does not say where its releases are");
                        Ok(1)
                    }
                };
                // The bridge is updated whatever became of the app: one is independent of
                // the other, and an app installed with cargo still has a released bridge.
                let bridge = if args.no_bridge {
                    0
                } else {
                    update_bridge(&args.update).await?
                };
                Ok(own?.max(bridge))
            }
            FrameworkCommand::Config(command) => config::config(ctx, owner, command, version).await,
            FrameworkCommand::Workspace(command) => command.dispatch(app, version).await,
            FrameworkCommand::ApiKey(command) => {
                api_key(ctx, &server(owner)?.profiles().list(), command)
            }
            FrameworkCommand::ExternalDevice(legacy) => {
                eprintln!("warning: `external-device` is renamed to `api-key`");
                if !legacy.app.is_empty() {
                    eprintln!(
                        "API keys are per workspace now, not per application: `--app` is not taken"
                    );
                    return Ok(2);
                }
                api_key(ctx, &server(owner)?.profiles().list(), legacy.command)
            }
            #[cfg(feature = "mcp")]
            FrameworkCommand::Mcp => {
                server(owner)?.serve_mcp_stdio(version).await?;
                Ok(0)
            }
        }
    }
}

/// `ctx`'s application's settings, or — when they cannot be read — every key at its
/// default, after one warning on stderr naming the device file and the error.
///
/// For `update`, `service` and `mcp`, which must work whatever the files hold: `update` is
/// the way out of a bad release, and `service uninstall` must always be possible.
fn settings_or_defaults(ctx: &AppContext, owner: Arc<dyn AppSettings>) -> Result<Settings> {
    match Settings::load(ctx, Arc::clone(&owner)) {
        Ok(settings) => Ok(settings),
        Err(err) => {
            let file = Settings::device_file(ctx)
                .map_or_else(|| "the settings".to_owned(), |f| f.display().to_string());
            eprintln!("warning: could not read {file} ({err}); going on with the default settings");
            Settings::defaults(ctx, owner)
        }
    }
}

/// `update`'s arguments in an application: its own, and whether the bridge follows.
#[derive(Clone, Debug, Default, clap::Args)]
pub struct AppUpdateArgs {
    /// `--check` and `--yes`, for the application and the bridge alike.
    #[command(flatten)]
    pub update: sapphire_framework_update::UpdateArgs,
    /// Update this application alone, leaving `sapphire-bridge` as it is.
    #[arg(long)]
    pub no_bridge: bool,
}

/// The bridge's executable, as it is installed beside the apps.
const BRIDGE_BINARY: &str = "sapphire-bridge";

/// Where the bridge's releases are, for a bridge too old to update itself.
const BRIDGE_RELEASES: &str = "https://github.com/sapphire-turtle/sapphire-framework/releases";

/// The first `name` (with the platform's executable suffix) in the directories of `path`.
fn find_on_path(name: &str, path: Option<&std::ffi::OsStr>) -> Option<PathBuf> {
    let file = format!("{name}{}", std::env::consts::EXE_SUFFIX);
    std::env::split_paths(path?)
        .map(|dir| dir.join(&file))
        .find(|candidate| candidate.is_file())
}

/// Run the bridge on `PATH`'s own `update` with `args`; returns its exit code. No bridge on
/// `PATH` is nothing to update. `update` does it after the application; a desktop package,
/// whose own update ends the process, does it first (#357).
pub async fn update_bridge(args: &sapphire_framework_update::UpdateArgs) -> Result<i32> {
    match find_on_path(BRIDGE_BINARY, std::env::var_os("PATH").as_deref()) {
        Some(exe) => run_bridge_update(&exe, args).await,
        None => {
            println!("{BRIDGE_BINARY} is not on PATH; not updating it");
            Ok(0)
        }
    }
}

/// Run the bridge at `exe`'s own `update` with `args`; returns its exit code.
///
/// The bridge updates itself rather than being replaced from here: it knows its own
/// version and where it was installed from, and replacing a running executable is its own
/// business on every platform.
async fn run_bridge_update(
    exe: &std::path::Path,
    args: &sapphire_framework_update::UpdateArgs,
) -> Result<i32> {
    // A bridge from before `update` rejects the subcommand.
    let knows_update = tokio::process::Command::new(exe)
        .args(["update", "--help"])
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .await?
        .success();
    if !knows_update {
        println!(
            "{} cannot update itself; install the newest from {BRIDGE_RELEASES}",
            exe.display()
        );
        return Ok(1);
    }
    let mut command = tokio::process::Command::new(exe);
    command.arg("update");
    if args.check {
        command.arg("--check");
    }
    if args.yes {
        command.arg("--yes");
    }
    // Inherited stdio: the bridge asks its own question and says what it did.
    let status = command.status().await?;
    Ok(status.code().unwrap_or(1))
}

/// The `workspace` subcommands (#215: one workspace per server).
///
/// `init`, `select`, `sync`, `move` and `remove` go to the app's server over IPC,
/// because the server owns the marker directories, the sync ids and which workspace it
/// serves; `show` asks the server for its workspace and then the bridge for the workgroup's
/// ledger. `init --remote-workspace` has the server resolve the name through the bridge.
#[derive(Debug, clap::Subcommand)]
pub enum WorkspaceCommand {
    /// Create this app's workspace home in the given directory, and switch the server to it.
    Init {
        /// Where the workspace root goes, created if missing. Defaults to the app's
        /// default workspace, `<data dir>/workspaces/default`.
        dir: Option<PathBuf>,
        /// Also start syncing it, which publishes it into the workgroup ledger.
        #[arg(long)]
        sync: bool,
        /// Sync it as this workspace of the workgroup, by name or id, instead of as a new
        /// one: its files and settings arrive from the other devices.
        #[arg(long, value_name = "NAME|ID", requires = "sync")]
        remote_workspace: Option<String>,
    },
    /// Switch the server to an existing workspace of this app.
    Select {
        /// The workspace's root. Defaults to the app's default workspace,
        /// `<data dir>/workspaces/default`.
        dir: Option<PathBuf>,
        /// Also start syncing it, which publishes it into the workgroup ledger. A
        /// workspace that has synced before turns sync back on by itself.
        #[arg(long)]
        sync: bool,
    },
    /// Turn sync on or off for the server's workspace, or show its sync state.
    #[command(subcommand)]
    Sync(WorkspaceSyncCommand),
    /// Show the server's workspace, then the workgroup's workspaces of this app.
    Show,
    /// Move the server's workspace folder to a new path on this host, keeping its sync.
    Move {
        /// The new root. It must not exist yet; its parent must.
        dir: PathBuf,
    },
    /// Stop serving the server's workspace on this host. Other devices keep their copies.
    Remove {
        /// Also delete the workspace's folder and every file in it.
        #[arg(long)]
        delete: bool,
    },
}

impl WorkspaceCommand {
    /// Carry out the command, returning the process exit code.
    ///
    /// `app` names the endpoint to open (`Endpoint::for_app`); `version` is this
    /// process's own, for the handshake.
    pub async fn dispatch(self, app: &'static str, version: &'static str) -> Result<i32> {
        match self {
            WorkspaceCommand::Init {
                dir,
                sync,
                remote_workspace,
            } => workspace_init(app, version, dir, sync, remote_workspace).await,
            WorkspaceCommand::Select { dir, sync } => {
                workspace_select(app, version, dir, sync).await
            }
            WorkspaceCommand::Sync(command) => workspace_sync(app, version, command).await,
            WorkspaceCommand::Show => workspace_show(app, version).await,
            WorkspaceCommand::Move { dir } => workspace_move(app, version, dir).await,
            WorkspaceCommand::Remove { delete } => workspace_remove(app, version, delete).await,
        }
    }
}

/// The `workspace sync` subcommands: the server's workspace's sync, switched after the fact.
#[derive(Debug, clap::Subcommand)]
pub enum WorkspaceSyncCommand {
    /// Start syncing the server's workspace, which publishes it into the workgroup ledger.
    Enable,
    /// Stop syncing the server's workspace on this host. It keeps its sync id, so enabling
    /// it again picks up where it left off; other devices keep their copies.
    Disable,
    /// Show the server's workspace's sync state.
    Status,
}

/// `external-device …`, kept one release as a hidden alias of `api-key`.
#[derive(Debug, clap::Args)]
pub struct LegacyExternalDevice {
    /// What `external-device add --app` took; refused, as keys are per workspace now.
    #[arg(long = "app", global = true, hide = true)]
    app: Vec<String>,
    #[command(subcommand)]
    command: corundum_framework_keys::KeyCommand,
}

/// `api-key …`: on the files of the workspace this application serves, as its selection
/// file records it. No workspace selected → a line naming `workspace select`, exit 1.
/// `add --profile` takes one of `profiles`.
fn api_key(
    ctx: &AppContext,
    profiles: &[sapphire_framework_keys::Profile],
    command: corundum_framework_keys::KeyCommand,
) -> Result<i32> {
    let Some(selection) = SelectionFile::for_app(ctx).load(ctx.app_name)? else {
        eprintln!(
            "{} serves no workspace yet: choose one with `{} workspace select <dir>`",
            ctx.app_name, ctx.app_name
        );
        return Ok(1);
    };
    // Opening the store creates its directory: never recreate a workspace that moved away.
    if !selection.root.join(format!(".{}", ctx.app_name)).is_dir() {
        eprintln!(
            "the workspace at {} is not reachable: bring it back, or choose another with `{} workspace select <dir>`",
            selection.root.display(),
            ctx.app_name
        );
        return Ok(1);
    }
    let run = sapphire_framework_keys::open_store(ctx.app_name, &selection.root)
        .and_then(|mut store| command.run(&mut store, profiles));
    match run {
        Ok(code) => Ok(code),
        Err(err) => {
            eprintln!("{err}");
            Ok(1)
        }
    }
}

/// `workspace init`: the server does the creating, over IPC.
///
/// The server owns the marker, the registry and the sync ids, so a workspace created
/// here is one the server already knows and a GUI sharing the registry sees. Nothing is
/// listening → the one line and exit 1; nothing is ever started (Global Constraints).
/// `--sync` rides the server's existing `sync.enable` path on the same connection, and
/// its state is printed after, so an unjoined host's "no workgroup knows it yet" is
/// plain.
///
/// `--remote-workspace` goes to `sync.map` instead, which makes the marker and takes the
/// workgroup's identity in one step: the workspace's `config.toml` is the one that
/// arrives, not one written here first (#253).
async fn workspace_init(
    app: &'static str,
    version: &'static str,
    dir: Option<PathBuf>,
    sync: bool,
    remote_workspace: Option<String>,
) -> Result<i32> {
    let endpoint = Endpoint::for_app(app)?;
    let client_info = ClientInfo {
        kind: "cli".to_owned(),
        version: version.to_owned(),
        api: proto::API_VERSION,
        app_api: None,
        pid: std::process::id(),
    };
    let Some((client, _)) = sapphire_ipc::connect_or_absent(&endpoint, app, client_info).await?
    else {
        println!("no {app} server is running");
        return Ok(1);
    };

    let dir = workspace_dir(app, dir)?;
    if let Some(selector) = remote_workspace {
        // `sync.map` needs the folder; `workspace.init` makes its own.
        std::fs::create_dir_all(&dir)?;
        let mapped: proto::SyncEnableResult = client
            .call(
                proto::SYNC_MAP,
                proto::SyncMapParams {
                    workspace: selector.clone(),
                    dir: dir.clone(),
                },
            )
            .await?;
        println!(
            "initialized {app} workspace in {} from {selector}",
            dir.display()
        );
        println!(
            "the {app} server now serves it, syncing as {}",
            mapped.workspace_id
        );
        return Ok(0);
    }
    let result: proto::WorkspaceInitResult = client
        .call(proto::WORKSPACE_INIT, proto::WorkspaceInitParams { dir })
        .await?;
    if result.created {
        println!("initialized {app} workspace in {}", result.root.display());
    } else {
        println!(
            "{} already exists (workspace {})",
            result.root.display(),
            result.workspace_id
        );
    }
    println!("the {app} server now serves {}", result.root.display());

    if sync {
        enable_sync(&client).await?;
    }
    Ok(0)
}

/// Turn sync on for the server's workspace over `client`, and print what it syncs as.
///
/// The one path behind `init --sync`, `select --sync` and `sync enable`: the server's
/// `sync.enable`, then its status, printed after, so an unjoined host's "no workgroup knows
/// it yet" is plain.
async fn enable_sync(client: &sapphire_ipc::Client) -> Result<()> {
    let enabled: proto::SyncEnableResult = client
        .call(proto::SYNC_ENABLE, serde_json::json!({}))
        .await?;
    // Never fails on account of the bridge: the workspace is synced either way, and the
    // status is the user's word for what the workgroup does not know yet.
    let status: proto::SyncStatusResult = client
        .call(proto::SYNC_STATUS, serde_json::json!({}))
        .await
        .unwrap_or(proto::SyncStatusResult {
            enabled: true,
            workspace_id: Some(enabled.workspace_id),
            ..proto::SyncStatusResult::not_synced()
        });
    println!(
        "syncing as {} ({} peer{})",
        enabled.workspace_id,
        status.peers,
        if status.peers == 1 { "" } else { "s" }
    );
    Ok(())
}

/// `workspace sync …`: the server's workspace's sync, switched after the fact.
async fn workspace_sync(
    app: &'static str,
    version: &'static str,
    command: WorkspaceSyncCommand,
) -> Result<i32> {
    let Some(client) = connect_app(app, version).await? else {
        return Ok(1);
    };
    match command {
        WorkspaceSyncCommand::Enable => enable_sync(&client).await?,
        WorkspaceSyncCommand::Disable => {
            let _: proto::Ack = client
                .call(proto::SYNC_DISABLE, serde_json::json!({}))
                .await?;
            println!("sync is off for the {app} workspace on this host; it keeps its sync id");
        }
        WorkspaceSyncCommand::Status => {
            let status: proto::SyncStatusResult = client
                .call(proto::SYNC_STATUS, serde_json::json!({}))
                .await?;
            for line in describe_sync(&status) {
                println!("{line}");
            }
        }
    }
    Ok(0)
}

/// `workspace sync status`, line by line.
fn describe_sync(status: &proto::SyncStatusResult) -> Vec<String> {
    let mut lines = vec![format!(
        "sync: {}",
        if status.enabled { "on" } else { "off" }
    )];
    if let Some(id) = status.workspace_id {
        lines.push(format!("workspace id: {id}"));
    }
    if status.enabled {
        lines.push(format!(
            "peers: {}{}",
            status.peers,
            if matches!(status.topology, proto::Topology::Star { .. }) {
                " (star)"
            } else {
                ""
            }
        ));
        lines.push(format!(
            "bridge: {}",
            if status.bridge_available {
                "reachable"
            } else {
                "not reachable"
            }
        ));
    }
    if let Some(paused) = &status.paused {
        lines.push(format!("paused: {paused}"));
    }
    if let Some(error) = &status.last_error {
        lines.push(format!("last error: {error}"));
    }
    lines
}

/// Render the server's workspace as the CLI prints it: `root state`.
///
/// Public so the integration tests can capture it; `workspace show` writes it to standard
/// output.
pub async fn render_workspace(client: &sapphire_ipc::Client, out: &mut String) -> Result<i32> {
    let current: proto::WorkspaceCurrentResult = client
        .call(proto::WORKSPACE_CURRENT, serde_json::json!({}))
        .await?;
    let Some(ws) = current.workspace else {
        writeln!(
            out,
            "no workspace yet; run `workspace init <dir>` or `workspace select <dir>`"
        )
        .expect("writing to a String cannot fail");
        return Ok(1);
    };
    writeln!(out, "{} {}", ws.root.display(), describe_state(&ws))
        .expect("writing to a String cannot fail");
    Ok(0)
}

/// `synced (star), embedding 3 pending` and the like.
fn describe_state(ws: &proto::CurrentWorkspace) -> String {
    let state = if !ws.reachable {
        "unreachable"
    } else if ws.sync.enabled {
        "synced"
    } else {
        "not synced"
    };
    let star = if matches!(ws.sync.topology, proto::Topology::Star { .. }) {
        " (star)"
    } else {
        ""
    };
    let pending = match &ws.sync.embedding {
        Some(e) if e.pending > 0 => format!(", embedding {} pending", e.pending),
        _ => String::new(),
    };
    format!("{state}{star}{pending}")
}

/// Where a workspace command acts: `dir` made absolute against this process's cwd — the
/// server's cwd is not the user's — or, when the user named none, the app's default
/// workspace (`<data dir>/workspaces/default`). The cwd is no default: the server is
/// resident and serves one workspace, so where the command happens to run says nothing
/// about which one is meant.
fn workspace_dir(app: &str, dir: Option<PathBuf>) -> std::io::Result<PathBuf> {
    Ok(match dir {
        Some(dir) => std::env::current_dir()?.join(dir),
        None => sapphire_workspace::default_workspace_dir(app),
    })
}

/// Connect to `app`'s server, or say none is running.
async fn connect_app(app: &str, version: &str) -> Result<Option<sapphire_ipc::Client>> {
    let client = try_connect_app(app, version).await?;
    if client.is_none() {
        println!("no {app} server is running");
    }
    Ok(client)
}

/// Connect to `app`'s server; `None`, silently, when none is running.
async fn try_connect_app(app: &str, version: &str) -> Result<Option<sapphire_ipc::Client>> {
    let endpoint = Endpoint::for_app(app)?;
    let client_info = ClientInfo {
        kind: "cli".to_owned(),
        version: version.to_owned(),
        api: proto::API_VERSION,
        app_api: None,
        pid: std::process::id(),
    };
    let client = sapphire_ipc::connect_or_absent(&endpoint, app, client_info).await?;
    Ok(client.map(|(c, _)| c))
}

/// `workspace select`: switch the server to an existing workspace, syncing it with `--sync`.
async fn workspace_select(
    app: &'static str,
    version: &'static str,
    dir: Option<PathBuf>,
    sync: bool,
) -> Result<i32> {
    let Some(client) = connect_app(app, version).await? else {
        return Ok(1);
    };
    let dir = workspace_dir(app, dir)?;
    let ws: proto::CurrentWorkspace = client
        .call(
            proto::WORKSPACE_SELECT,
            proto::WorkspaceSelectParams { dir },
        )
        .await?;
    println!("{} {}", ws.root.display(), describe_state(&ws));
    // A workspace that has synced before is synced again by the select itself.
    if sync && !ws.sync.enabled {
        enable_sync(&client).await?;
    }
    Ok(0)
}

/// `workspace move`: move the server's workspace folder.
async fn workspace_move(app: &'static str, version: &'static str, dir: PathBuf) -> Result<i32> {
    let Some(client) = connect_app(app, version).await? else {
        return Ok(1);
    };
    // Resolved here: the server's cwd is not the user's.
    let dir = std::env::current_dir()?.join(dir);
    let ws: proto::CurrentWorkspace = client
        .call(proto::WORKSPACE_MOVE, proto::WorkspaceMoveParams { dir })
        .await?;
    println!("{} {}", ws.root.display(), describe_state(&ws));
    Ok(0)
}

/// `workspace remove`: let go of the server's workspace, deleting it with `--delete`.
async fn workspace_remove(app: &'static str, version: &'static str, delete: bool) -> Result<i32> {
    let Some(client) = connect_app(app, version).await? else {
        return Ok(1);
    };
    let _: proto::Ack = client
        .call(
            proto::WORKSPACE_REMOVE,
            proto::WorkspaceRemoveParams { delete },
        )
        .await?;
    println!(
        "{}",
        if delete {
            "removed and deleted"
        } else {
            "removed; the files stay"
        }
    );
    Ok(0)
}

/// `workspace show`: the server's workspace, then the workgroup's workspaces of this app.
///
/// The workgroup's ledger is the bridge's to answer, and the bridge being down is not the
/// server half's failure: the ledger's absence is one line, and the exit is the server
/// half's.
async fn workspace_show(app: &'static str, version: &'static str) -> Result<i32> {
    let Some(client) = connect_app(app, version).await? else {
        return Ok(1);
    };
    let mut out = String::new();
    let code = render_workspace_show(Arc::new(client), app, &mut out).await?;
    for line in out.lines() {
        println!("{line}");
    }
    Ok(code)
}

/// [`workspace_show`]'s text, over one connection to the app's server.
///
/// The workgroup half goes through the same connection, relayed to the bridge. Public so
/// the integration tests can capture it.
#[doc(hidden)]
pub async fn render_workspace_show(
    client: Arc<sapphire_ipc::Client>,
    app: &str,
    out: &mut String,
) -> Result<i32> {
    let code = render_workspace(&client, out).await?;
    let bridge = BridgeClient::relayed(client, sapphire_ipc::runtime_dir()?);
    let workspaces = match bridge.workspaces().await {
        Ok(workspaces) => workspaces,
        Err(sapphire_ipc::Error::NotRunning(_)) => {
            writeln!(out, "no sapphire-bridge is running")
                .expect("writing to a String cannot fail");
            return Ok(code);
        }
        Err(error) => return Err(error.into()),
    };
    let mine: Vec<_> = workspaces
        .workspaces
        .into_iter()
        .filter(|w| w.app_name == app)
        .collect();
    if !mine.is_empty() {
        writeln!(out).expect("writing to a String cannot fail");
        writeln!(out, "in the workgroup:").expect("writing to a String cannot fail");
        for workspace in mine {
            writeln!(out, "{} {}", workspace.name, workspace.workspace_id)
                .expect("writing to a String cannot fail");
        }
    }
    Ok(code)
}

/// Probe, handshake and call `SERVER_INFO` in one go, or report absence.
///
/// Every step the CLI needs — liveness and the API gate — is inside
/// [`connect_or_absent`], so the command is that call plus one `SERVER_INFO` round trip.
/// The API gate doubles as the liveness check here: a live server of another API version
/// is `ApiVersionMismatch`, which names both API versions and advises upgrading and
/// restarting the service. The report goes through [`run_status_into`] so tests can
/// capture it.
async fn run_status(endpoint: &Endpoint, app: &str, version: &str) -> Result<i32> {
    let mut out = String::new();
    let code = run_status_into(endpoint, app, version, &mut out).await?;
    for line in out.lines() {
        println!("{line}");
    }
    Ok(code)
}

/// [`run_status`], writing the rendered lines into `out` instead of the process's
/// standard output.
async fn run_status_into(
    endpoint: &Endpoint,
    app: &str,
    version: &str,
    out: &mut String,
) -> Result<i32> {
    let client_info = ClientInfo {
        kind: "cli".to_owned(),
        version: version.to_owned(),
        api: proto::API_VERSION,
        app_api: None,
        pid: std::process::id(),
    };
    let Some((client, _)) = sapphire_ipc::connect_or_absent(endpoint, app, client_info).await?
    else {
        // Nothing is listening: the framework's half is only the one line, and the
        // application's rows are skipped (there is no server to have configured them).
        writeln!(out, "no {app} server is running").expect("writing to a String cannot fail");
        return Ok(1);
    };
    let report: StatusReport = client
        .call(
            sapphire_backend::protocol::SERVER_INFO,
            serde_json::json!({}),
        )
        .await?;
    writeln!(out, "running: {}", report.running).expect("writing to a String cannot fail");
    if let Some(version) = &report.version {
        writeln!(out, "version: {version}").expect("writing to a String cannot fail");
    }
    if let Some(pid) = report.pid {
        writeln!(out, "pid: {pid}").expect("writing to a String cannot fail");
    }
    if let Some(managed_by) = &report.managed_by {
        writeln!(out, "managed_by: {managed_by:?}").expect("writing to a String cannot fail");
    }
    for row in &report.app {
        writeln!(out, "{}: {}", row.name, row.value).expect("writing to a String cannot fail");
    }
    // A server older than `server.update` answers method-not-found: there is nothing to
    // say about releases then, and the restart line still stands.
    let update: Option<proto::UpdateStatus> = client
        .call(proto::SERVER_UPDATE, serde_json::json!({}))
        .await
        .ok();
    for line in describe_update(app, version, report.version.as_deref(), update.as_ref()) {
        writeln!(out, "{line}").expect("writing to a String cannot fail");
    }
    Ok(0)
}

/// The `update` and `restart` lines of `status`.
///
/// `installed` is this CLI's version — the executable on disk — and `running` the
/// server's. A server older than what is installed was upgraded under it and needs a
/// restart; a release newer than what is installed is an update to install. A release
/// that is newer than the server but already installed is only the restart.
fn describe_update(
    app: &str,
    installed: &str,
    running: Option<&str>,
    update: Option<&proto::UpdateStatus>,
) -> Vec<String> {
    let parse = |v: &str| semver::Version::parse(v).ok();
    let installed_v = parse(installed);
    let mut lines = Vec::new();
    if let Some(update) = update.filter(|u| u.enabled) {
        match (&update.latest, &update.last_error) {
            (Some(latest), _) => {
                let newer = match (parse(&latest.version), &installed_v) {
                    (Some(latest), Some(installed)) => latest > *installed,
                    _ => update.update_available,
                };
                if newer {
                    lines.push(format!(
                        "update: {} is available ({})",
                        latest.version, latest.url
                    ));
                } else {
                    lines.push(format!(
                        "update: up to date (latest release {})",
                        latest.version
                    ));
                }
            }
            (None, Some(error)) => lines.push(format!("update: could not check: {error}")),
            (None, None) => lines.push("update: not checked yet".to_owned()),
        }
    }
    if let (Some(running), Some(installed)) = (running.and_then(parse), installed_v)
        && installed > running
    {
        lines.push(format!(
            "restart: needed: {installed} is installed but {running} is running; restart the {app} service"
        ));
    }
    lines
}
#[cfg(test)]
mod tests {
    use super::*;
    use clap::Parser;

    /// The composition every app binary uses: an app-owned subcommand enum whose framework
    /// verbs ride in on a flattened variant (spec decision 2).
    ///
    /// clap 4 rejects `#[command(flatten)]` on a struct field beside a
    /// `#[command(subcommand)]` field — two subcommand enums cannot share one level, and a
    /// flattened field must implement `clap::Args`, which the `Subcommand` derive does not
    /// provide. The plan's Risks section names this probe as the decision, and the flat
    /// fallback is what it decided: one subcommand enum, every verb at the top level.
    #[derive(Parser)]
    struct Probe {
        #[command(subcommand)]
        command: Command,
    }

    #[derive(Debug, clap::Subcommand)]
    enum Command {
        /// An app-specific verb that must parse beside the framework's.
        Greet,
        #[command(flatten)]
        Framework(FrameworkCommand),
    }

    /// A desktop binary: the same verbs, and none at all to open the window.
    #[derive(Parser)]
    struct DesktopProbe {
        #[command(subcommand)]
        command: Option<Command>,
    }

    #[test]
    fn a_desktop_binary_takes_the_same_verbs_or_none() {
        let probe = DesktopProbe::try_parse_from(["app"]).unwrap();
        assert!(probe.command.is_none());
        let probe = DesktopProbe::try_parse_from(["app", "serve"]).unwrap();
        assert!(matches!(
            probe.command,
            Some(Command::Framework(FrameworkCommand::Serve))
        ));
        let probe = DesktopProbe::try_parse_from(["app", "greet"]).unwrap();
        assert!(matches!(probe.command, Some(Command::Greet)));
        // The CLI version's bare invocation is an error, which clap answers with the help.
        assert!(Probe::try_parse_from(["app"]).is_err());
    }

    #[test]
    fn app_and_framework_commands_parse_side_by_side() {
        let probe = Probe::try_parse_from(["app", "greet"]).unwrap();
        assert!(matches!(probe.command, Command::Greet));
        let probe = Probe::try_parse_from(["app", "serve"]).unwrap();
        assert!(matches!(
            probe.command,
            Command::Framework(FrameworkCommand::Serve)
        ));
        let probe = Probe::try_parse_from(["app", "status"]).unwrap();
        assert!(matches!(
            probe.command,
            Command::Framework(FrameworkCommand::Status)
        ));
    }

    #[cfg(feature = "mcp")]
    #[test]
    fn the_mcp_command_parses_beside_the_apps() {
        let probe = Probe::try_parse_from(["app", "mcp"]).unwrap();
        assert!(matches!(
            probe.command,
            Command::Framework(FrameworkCommand::Mcp)
        ));
    }

    #[test]
    fn the_update_command_parses_with_and_without_the_bridge() {
        let probe = Probe::try_parse_from(["app", "update", "--check", "-y"]).unwrap();
        let Command::Framework(FrameworkCommand::Update(args)) = probe.command else {
            panic!("not update");
        };
        assert!(args.update.check && args.update.yes && !args.no_bridge);
        let probe = Probe::try_parse_from(["app", "update", "--no-bridge"]).unwrap();
        let Command::Framework(FrameworkCommand::Update(args)) = probe.command else {
            panic!("not update");
        };
        assert!(args.no_bridge);
    }

    #[test]
    fn the_bridge_is_found_in_the_first_directory_of_path_that_has_it() {
        let tmp = tempfile::tempdir().unwrap();
        let (empty, first, second) = (
            tmp.path().join("empty"),
            tmp.path().join("first"),
            tmp.path().join("second"),
        );
        let file = format!("sapphire-bridge{}", std::env::consts::EXE_SUFFIX);
        for dir in [&empty, &first, &second] {
            std::fs::create_dir(dir).unwrap();
        }
        std::fs::write(first.join(&file), b"").unwrap();
        std::fs::write(second.join(&file), b"").unwrap();
        let path = std::env::join_paths([&empty, &first, &second]).unwrap();
        assert_eq!(
            find_on_path("sapphire-bridge", Some(&path)),
            Some(first.join(&file))
        );
        let path = std::env::join_paths([&empty]).unwrap();
        assert_eq!(find_on_path("sapphire-bridge", Some(&path)), None);
        assert_eq!(find_on_path("sapphire-bridge", None), None);
    }

    /// An executable shell script at `dir/name` that runs `body`.
    #[cfg(unix)]
    fn script(dir: &std::path::Path, name: &str, body: &str) -> PathBuf {
        use std::os::unix::fs::PermissionsExt;
        let path = dir.join(name);
        std::fs::write(&path, format!("#!/bin/sh\n{body}\n")).unwrap();
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
        path
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn the_bridge_runs_its_own_update_with_the_apps_flags() {
        let tmp = tempfile::tempdir().unwrap();
        let log = tmp.path().join("args");
        // Records the real run (not the `--help` probe) and exits as an update that
        // declined would.
        let exe = script(
            tmp.path(),
            "bridge",
            &format!(
                r#"case "$*" in *--help*) exit 0;; esac; echo "$@" > {}; exit 1"#,
                log.display()
            ),
        );
        let args = sapphire_framework_update::UpdateArgs {
            check: true,
            yes: true,
        };
        assert_eq!(run_bridge_update(&exe, &args).await.unwrap(), 1);
        assert_eq!(
            std::fs::read_to_string(&log).unwrap().trim(),
            "update --check --yes"
        );
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn a_bridge_without_update_is_not_run() {
        let tmp = tempfile::tempdir().unwrap();
        let runs = tmp.path().join("runs");
        // What clap does with an unknown subcommand: exit 2, whatever the arguments.
        let exe = script(
            tmp.path(),
            "bridge",
            &format!(r#"echo "$*" >> {}; exit 2"#, runs.display()),
        );
        let code = run_bridge_update(&exe, &Default::default()).await.unwrap();
        assert_eq!(code, 1);
        assert_eq!(
            std::fs::read_to_string(&runs).unwrap(),
            "update --help\n",
            "only the probe ran"
        );
    }

    #[test]
    fn the_service_subcommands_parse() {
        for args in [
            vec!["app", "service", "install"],
            vec!["app", "service", "uninstall"],
            vec!["app", "service", "status"],
        ] {
            assert!(Probe::try_parse_from(&args).is_ok(), "{args:?}");
        }
    }

    #[test]
    fn the_workspace_commands_parse() {
        for args in [
            vec!["app", "workspace", "init"],
            vec!["app", "workspace", "init", "papers"],
            vec!["app", "workspace", "init", "--sync"],
            vec!["app", "workspace", "show"],
            vec!["app", "workspace", "select", "papers"],
            vec!["app", "workspace", "select", "--sync"],
            vec!["app", "workspace", "select", "papers", "--sync"],
            vec!["app", "workspace", "sync", "enable"],
            vec!["app", "workspace", "sync", "disable"],
            vec!["app", "workspace", "sync", "status"],
            vec![
                "app",
                "workspace",
                "init",
                "--sync",
                "--remote-workspace=Ledger",
            ],
            vec![
                "app",
                "workspace",
                "init",
                "papers",
                "--sync",
                "--remote-workspace",
                "Ledger",
            ],
            vec!["app", "workspace", "move", "../papers"],
            vec!["app", "workspace", "remove"],
            vec!["app", "workspace", "remove", "--delete"],
        ] {
            assert!(Probe::try_parse_from(&args).is_ok(), "{args:?}");
        }
    }

    #[test]
    fn a_remote_workspace_needs_sync() {
        let args = ["app", "workspace", "init", "--remote-workspace=Ledger"];
        assert!(Probe::try_parse_from(args).is_err());
    }

    #[test]
    fn bare_invocation_prints_the_help() {
        // <app> with no subcommand prints the help; the server starts only on an explicit
        // `serve`. An app CLI gets that by a required subcommand and
        // `arg_required_else_help`, as this probe does.
        #[derive(Parser)]
        #[command(arg_required_else_help = true)]
        struct Bare {
            #[command(subcommand)]
            command: Command,
        }
        let err = Bare::try_parse_from(["app"]).err().unwrap();
        assert_eq!(
            err.kind(),
            clap::error::ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
        );
    }
}

#[cfg(test)]
mod status_tests {
    use super::*;
    use sapphire_workspace::AppContext;

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

    /// Wait until something is listening on `endpoint`.
    ///
    /// `tokio::spawn(server.run())` only schedules the server; without this wait a fast
    /// `run_status` probe can run before `run` has bound the socket, and with nothing
    /// listening that single unlucky probe is the whole test failing.
    async fn wait_until_listening(endpoint: &Endpoint) {
        let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(5);
        while !sapphire_ipc::probe(endpoint).await.unwrap() {
            assert!(
                tokio::time::Instant::now() < deadline,
                "the server never started listening"
            );
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        }
    }

    #[tokio::test]
    async fn status_reports_no_server_when_none_is_running() {
        let tmp = tempfile::tempdir().unwrap();
        let endpoint = sapphire_ipc::Endpoint::in_dir("status-test", tmp.path().to_path_buf());
        let code = run_status(&endpoint, "status-test", "0.0.0").await.unwrap();
        assert_eq!(code, 1, "no server is a non-zero exit");
    }

    fn update(latest: Option<&str>, error: Option<&str>) -> proto::UpdateStatus {
        proto::UpdateStatus {
            enabled: true,
            latest: latest.map(|version| proto::ReleaseInfo {
                version: version.to_owned(),
                url: format!("https://example.invalid/v{version}"),
            }),
            update_available: false,
            last_error: error.map(str::to_owned),
        }
    }

    #[test]
    fn status_names_a_newer_release_and_nothing_when_up_to_date() {
        let lines = describe_update(
            "app",
            "0.19.0",
            Some("0.19.0"),
            Some(&update(Some("0.20.0"), None)),
        );
        assert_eq!(
            lines,
            vec!["update: 0.20.0 is available (https://example.invalid/v0.20.0)"]
        );
        let lines = describe_update(
            "app",
            "0.19.0",
            Some("0.19.0"),
            Some(&update(Some("0.19.0"), None)),
        );
        assert_eq!(lines, vec!["update: up to date (latest release 0.19.0)"]);
    }

    #[test]
    fn status_asks_for_a_restart_when_the_installed_server_is_newer_than_the_running_one() {
        // Installed 0.20.0 under a running 0.19.0: the release is no longer news, the
        // restart is.
        let lines = describe_update(
            "app",
            "0.20.0",
            Some("0.19.0"),
            Some(&update(Some("0.20.0"), None)),
        );
        assert_eq!(
            lines,
            vec![
                "update: up to date (latest release 0.20.0)",
                "restart: needed: 0.20.0 is installed but 0.19.0 is running; restart the app service",
            ]
        );
        // An older server without `server.update` still gets the restart line.
        let lines = describe_update("app", "0.20.0", Some("0.19.0"), None);
        assert_eq!(lines.len(), 1);
        assert!(lines[0].starts_with("restart: needed"));
    }

    #[test]
    fn status_says_why_a_check_failed_and_nothing_when_checks_are_off() {
        let lines = describe_update(
            "app",
            "0.19.0",
            Some("0.19.0"),
            Some(&update(None, Some("offline"))),
        );
        assert_eq!(lines, vec!["update: could not check: offline"]);
        let off = proto::UpdateStatus::default();
        assert!(describe_update("app", "0.19.0", Some("0.19.0"), Some(&off)).is_empty());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn status_shows_a_newer_release_the_server_found() {
        let tmp = tempfile::tempdir().unwrap();
        let endpoint = sapphire_ipc::Endpoint::in_dir("status-test", tmp.path().to_path_buf());
        let api = crate::update::stub::one_response(
            "200 OK",
            r#"[{"tag_name":"v0.20.0","html_url":"https://example.invalid/v0.20.0"}]"#,
        )
        .await;
        let server = AppServer::new(&CTX, "0.19.0")
            .endpoint(endpoint.clone())
            .selection_file(tmp.path().join("workspace.toml"))
            .releases(crate::ReleaseSource::github("o", "r").api_base(api));
        let handle = tokio::spawn(async move { server.run().await });
        wait_until_listening(&endpoint).await;

        // The first look runs as the server starts; wait for its answer to land.
        let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(10);
        let out = loop {
            let mut out = String::new();
            run_status_into(&endpoint, CTX.app_name, "0.19.0", &mut out)
                .await
                .unwrap();
            if !out.contains("not checked yet") {
                break out;
            }
            assert!(
                tokio::time::Instant::now() < deadline,
                "never checked: {out}"
            );
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        };
        assert!(
            out.contains("update: 0.20.0 is available (https://example.invalid/v0.20.0)"),
            "output was: {out}"
        );
        assert!(!out.contains("restart:"), "output was: {out}");

        let (client, _) = sapphire_ipc::connect_or_absent(
            &endpoint,
            CTX.app_name,
            sapphire_ipc::ClientInfo {
                kind: "test".into(),
                version: "0.19.0".into(),
                api: sapphire_backend::protocol::API_VERSION,
                app_api: None,
                pid: std::process::id(),
            },
        )
        .await
        .unwrap()
        .expect("the server is listening");
        let _: serde_json::Value = client
            .call(sapphire_ipc::SHUTDOWN_METHOD, serde_json::json!({}))
            .await
            .unwrap();
        handle.await.unwrap().unwrap();
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn status_shows_the_extension_rows_of_a_running_server() {
        let tmp = tempfile::tempdir().unwrap();
        let endpoint = sapphire_ipc::Endpoint::in_dir("status-test", tmp.path().to_path_buf());
        let server = AppServer::new(&CTX, "0.0.0")
            .endpoint(endpoint.clone())
            // This context's directories are never initialised here.
            .selection_file(tmp.path().join("workspace.toml"))
            .status_rows(std::sync::Arc::new(|| {
                vec![StatusRow {
                    name: "sync".into(),
                    value: "on".into(),
                }]
            }));
        let handle = tokio::spawn(async move { server.run().await });
        wait_until_listening(&endpoint).await;

        // The report goes through the sink face so the rendering is asserted, not just
        // the exit code.
        let mut out = String::new();
        let code = run_status_into(&endpoint, CTX.app_name, "0.0.0", &mut out)
            .await
            .unwrap();
        assert_eq!(code, 0);
        assert!(out.contains("running: true"), "output was: {out}");
        assert!(out.contains("sync: on"), "output was: {out}");

        let (client, _) = sapphire_ipc::connect_or_absent(
            &endpoint,
            CTX.app_name,
            sapphire_ipc::ClientInfo {
                kind: "test".into(),
                version: "0.0.0".into(),
                api: sapphire_backend::protocol::API_VERSION,
                app_api: None,
                pid: std::process::id(),
            },
        )
        .await
        .unwrap()
        .expect("the server is listening");
        let _: serde_json::Value = client
            .call(sapphire_ipc::SHUTDOWN_METHOD, serde_json::json!({}))
            .await
            .unwrap();
        handle.await.unwrap().unwrap();
    }
}

#[cfg(test)]
mod service_spec_tests {
    use super::*;
    use sapphire_workspace::AppContext;

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

    #[test]
    fn the_generated_spec_runs_the_server_not_the_cli() {
        // A service manager starts the executable directly, and the executable's bare
        // invocation is `serve`: the unit's argv must be exactly that, so the server that
        // starts has no CLI verbs on its command line to misparse.
        let spec = AppServer::new(&CTX, "0.0.0").service_spec();
        assert_eq!(spec.args, vec!["serve".to_owned()]);
    }

    #[test]
    fn the_generated_spec_retires_the_first_label() {
        // The first label sapphire registered on macOS; corundum retires its own earlier
        // one (net.sapphire-turtle.<app>) by itself.
        let spec = AppServer::new(&CTX, "0.0.0").service_spec();
        assert_eq!(
            spec.legacy_labels,
            [format!("net.fireturtle.sapphire.{}", CTX.app_name)]
        );
    }

    #[test]
    fn the_generated_spec_names_this_application() {
        let spec = AppServer::new(&CTX, "0.0.0").service_spec();
        assert_eq!(spec.app_name, CTX.app_name);
    }

    #[test]
    fn the_generated_spec_describes_the_server() {
        let spec = AppServer::new(&CTX, "0.0.0").service_spec();
        assert!(
            spec.description.contains(CTX.app_name),
            "the unit's description should name the application: {:?}",
            spec.description
        );
        assert!(spec.description.contains("0.0.0"));
    }
}