dsp-cli 0.1.1

AI-agent-friendly command-line interface for the DaSCH Service Platform (DSP)
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
//! Actions for `dsp auth set-token`.
//!
//! Reads a pre-issued JWT from stdin, verifies it against the server with a
//! live probe, and — only if the probe succeeds — writes it into the auth
//! cache. Subsequent commands then reuse the token until it expires.
//!
//! See plan 008 for the full validation flow and design decisions.

use std::path::Path;

use chrono::Utc;

use crate::actions::auth_state::read_auth_state;
use crate::client::DspClient;
use crate::config::auth_cache::ServerEntry;
use crate::config::{AuthCache, Config, ResolvedToken, TokenOrigin};
use crate::diagnostic::Diagnostic;
use crate::render::auth::AuthSetTokenOutcome;
use crate::render::{MetaContext, Renderer};

/// Cache a pre-issued bearer token read from stdin.
///
/// Reads a JWT from stdin, then delegates to [`run_from_line`] for the
/// trim→empty-guard→decode→probe→cache→render flow.
pub fn run(
    cfg: &Config,
    client: &dyn DspClient,
    renderer: &mut dyn Renderer,
) -> Result<(), Diagnostic> {
    let mut line = String::new();
    // `read_line` returns `Ok(0)` on EOF (not an error); the empty-guard in
    // `run_from_line` handles that case. A real I/O failure (broken pipe, etc.)
    // is a bad invocation rather than a CLI bug, so we surface it as Usage
    // rather than Internal — consistent with login.rs's stdin error handling.
    std::io::stdin()
        .read_line(&mut line)
        .map_err(|e| Diagnostic::Usage(format!("could not read token from stdin: {e}")))?;
    run_from_line(line, cfg, client, renderer, None)
}

/// Trim, guard empty, then decode→probe→cache→render.
///
/// Extracted as a plain-function seam so tests can exercise the empty-guard
/// and newline-trim path without touching stdin. Production callers use `run`;
/// tests call this directly with an injected string and a tempdir cache path.
fn run_from_line(
    mut line: String,
    cfg: &Config,
    client: &dyn DspClient,
    renderer: &mut dyn Renderer,
    cache_path: Option<&Path>,
) -> Result<(), Diagnostic> {
    // Strip a single trailing line terminator first (shared with the password
    // path; see `crate::actions::auth::trim_line_ending`).
    crate::actions::auth::trim_line_ending(&mut line);

    // Then trim all surrounding whitespace for both the empty-guard and the
    // value forwarded onward. A JWT never contains surrounding whitespace, so
    // this is safe and keeps the guard and the cached token symmetric (a pasted
    // "  <jwt>  " decodes/caches cleanly rather than failing as "not a valid
    // JWT"). Unlike `trim_line_ending`, `trim()` also eats a bare surrounding
    // `\r` — fine here, since the forwarded token must not carry whitespace.
    let token = line.trim();
    if token.is_empty() {
        return Err(Diagnostic::Usage("no token provided on stdin".to_string()));
    }

    run_impl(token, cfg, client, renderer, cache_path)
}

/// Internal entry point that accepts an explicit cache path (for tests).
///
/// Production callers use `run`; tests call this directly to inject a
/// tempdir-backed cache path and a pre-built token string.
///
/// # Security note
///
/// The `token` parameter is a bearer secret and must **never** be logged,
/// printed to stderr, or included in any `tracing::debug!` / `tracing::info!`
/// call. It is stored into `auth.toml` (which uses a manual `Debug` impl that
/// redacts the token) and forwarded to `client.verify_token` only.
fn run_impl(
    token: &str,
    cfg: &Config,
    client: &dyn DspClient,
    renderer: &mut dyn Renderer,
    cache_path: Option<&Path>,
) -> Result<(), Diagnostic> {
    // 1. Local decode: extract metadata without verifying the signature.
    //    Failure means the input is not structurally a JWT → Usage error (exit 2).
    //    We do NOT locally enforce `exp`; a locally-expired token may still pass
    //    the live probe if the server's clock differs, and the probe is the trust
    //    boundary in any case.
    let meta = crate::client::jwt::extract_meta(token)
        .ok_or_else(|| Diagnostic::Usage("input on stdin is not a valid JWT".to_string()))?;

    // 2. Live probe: verify the token is currently accepted by the server.
    //    This is the authoritative validity gate — no token is cached before this
    //    succeeds. 401/403 → AuthRequired (exit 3); other failures propagate.
    client.verify_token(&cfg.server, token)?;

    // 3. Cache (only reached when probe returned Ok).
    let entry = ServerEntry {
        token: token.to_string(),
        user: meta.sub.clone(),
        acquired_at: Some(Utc::now()),
        expires_at: meta.exp,
    };

    let mut cache = match cache_path {
        Some(p) => AuthCache::load_from(p)?,
        None => AuthCache::load()?,
    };
    cache.set_entry(&cfg.server, entry);
    match cache_path {
        Some(p) => cache.save_to(p)?,
        None => cache.save()?,
    }

    // 4. Build MetaContext reflecting the post-set-token auth state using the
    // shared ADR-0007 helper. The token was just stored in the cache as a
    // Cache-origin entry with `meta.sub` as the user. Synthesize a Cache-origin
    // ResolvedToken so `read_auth_state` picks the correct branch.
    let resolved_for_meta = ResolvedToken {
        token: token.to_string(),
        origin: TokenOrigin::Cache,
    };
    let meta_ctx = MetaContext {
        server_label: cfg.server.clone(),
        auth_state: read_auth_state(Some(&resolved_for_meta), &cache, &cfg.server),
        filter_warning: None,
    };

    // 5. Render the outcome.
    let outcome = AuthSetTokenOutcome {
        server: cfg.server.clone(),
        user: meta.sub,
        expires_at: meta.exp,
    };

    renderer.auth_set_token(&outcome, &meta_ctx)
}

#[cfg(test)]
mod tests {
    use chrono::{TimeZone, Utc};
    use jsonwebtoken::{Algorithm, EncodingKey, Header, encode};
    use tempfile::TempDir;

    use super::{run_from_line, run_impl};
    use crate::client::DspClient;
    use crate::config::auth_cache::ServerEntry;
    use crate::config::{AuthCache, Config};
    use crate::diagnostic::Diagnostic;
    use crate::model::{CreateDumpOutcome, DumpTask, LoginResponse, ProjectRef};
    use crate::render::auth::{
        AuthLoginOutcome, AuthLogoutOutcome, AuthSetTokenOutcome, AuthStatusOutcome,
    };
    use crate::render::{MetaContext, Renderer};

    // ── local mock client ─────────────────────────────────────────────────────

    struct MockDspClient {
        verify_token_result: Result<(), Diagnostic>,
    }

    impl MockDspClient {
        fn ok() -> Self {
            Self {
                verify_token_result: Ok(()),
            }
        }

        fn err(diag: Diagnostic) -> Self {
            Self {
                verify_token_result: Err(diag),
            }
        }
    }

    impl DspClient for MockDspClient {
        fn login(
            &self,
            _server: &str,
            _user: &str,
            _password: &str,
        ) -> Result<LoginResponse, Diagnostic> {
            unimplemented!("login not used by set-token tests")
        }

        fn resolve_project(&self, _server: &str, _project: &str) -> Result<ProjectRef, Diagnostic> {
            unimplemented!("resolve_project not used by set-token tests")
        }

        fn create_project_dump(
            &self,
            _server: &str,
            _project_iri: &str,
            _skip_assets: bool,
            _token: &str,
        ) -> Result<CreateDumpOutcome, Diagnostic> {
            unimplemented!("create_project_dump not used by set-token tests")
        }

        fn get_project_dump_status(
            &self,
            _server: &str,
            _project_iri: &str,
            _dump_id: &str,
            _token: &str,
        ) -> Result<DumpTask, Diagnostic> {
            unimplemented!("get_project_dump_status not used by set-token tests")
        }

        fn download_project_dump(
            &self,
            _server: &str,
            _project_iri: &str,
            _dump_id: &str,
            _token: &str,
            _dest: &mut dyn std::io::Write,
        ) -> Result<u64, Diagnostic> {
            unimplemented!("download_project_dump not used by set-token tests")
        }

        fn delete_project_dump(
            &self,
            _server: &str,
            _project_iri: &str,
            _dump_id: &str,
            _token: &str,
        ) -> Result<(), Diagnostic> {
            unimplemented!("delete_project_dump not used by set-token tests")
        }

        fn describe_project(
            &self,
            _server: &str,
            _project: &str,
            _token: Option<&str>,
        ) -> Result<crate::model::ProjectDetail, Diagnostic> {
            unimplemented!("describe_project not used by set-token tests")
        }

        fn list_projects(
            &self,
            _server: &str,
            _token: Option<&str>,
        ) -> Result<Vec<crate::model::Project>, Diagnostic> {
            Err(Diagnostic::NotImplemented(
                "list_projects not used in set_token.rs tests".into(),
            ))
        }

        fn list_data_models(
            &self,
            _server: &str,
            _project_iri: &str,
            _token: Option<&str>,
        ) -> Result<Vec<crate::model::DataModel>, Diagnostic> {
            unimplemented!("list_data_models not used by set-token tests")
        }

        fn describe_data_model(
            &self,
            _server: &str,
            _data_model_iri: &str,
            _token: Option<&str>,
        ) -> Result<crate::model::DataModelDetail, Diagnostic> {
            unimplemented!("describe_data_model not used in set-token tests")
        }

        fn describe_resource_type(
            &self,
            _server: &str,
            _data_model_iri: &str,
            _resource_type: &str,
            _token: Option<&str>,
        ) -> Result<crate::model::ResourceTypeDetail, Diagnostic> {
            unimplemented!("describe_resource_type not used in set-token tests")
        }

        fn data_model_structure(
            &self,
            _server: &str,
            _data_model_iri: &str,
            _token: Option<&str>,
        ) -> Result<crate::model::DataModelStructure, Diagnostic> {
            unimplemented!("data_model_structure not used in set-token tests")
        }

        fn list_resources(
            &self,
            _server: &str,
            _project_iri: &str,
            _resource_type_iri: &str,
            _order_by: Option<&str>,
            _page: u32,
            _token: Option<&str>,
        ) -> Result<crate::model::ResourcePage, Diagnostic> {
            unimplemented!("list_resources not used in set-token tests")
        }

        fn describe_resource(
            &self,
            _server: &str,
            _resource_iri: &str,
            _token: Option<&str>,
            _with_values: bool,
        ) -> Result<crate::model::ResourceDetail, Diagnostic> {
            unimplemented!("describe_resource not used in set-token tests")
        }

        fn verify_token(&self, _server: &str, _token: &str) -> Result<(), Diagnostic> {
            self.verify_token_result.clone()
        }
    }

    // ── recording renderer ─────────────────────────────────────────────────────

    struct RecordingRenderer {
        set_token_outcome: Option<AuthSetTokenOutcome>,
        set_token_auth_state: Option<String>,
    }

    impl RecordingRenderer {
        fn new() -> Self {
            Self {
                set_token_outcome: None,
                set_token_auth_state: None,
            }
        }
    }

    impl Renderer for RecordingRenderer {
        fn diagnostic(
            &mut self,
            _diag: &Diagnostic,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            Ok(())
        }

        fn auth_login(
            &mut self,
            _outcome: &AuthLoginOutcome,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            Ok(())
        }

        fn auth_status(
            &mut self,
            _outcome: &AuthStatusOutcome,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            Ok(())
        }

        fn auth_logout(
            &mut self,
            _outcome: &AuthLogoutOutcome,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            Ok(())
        }

        fn auth_set_token(
            &mut self,
            outcome: &AuthSetTokenOutcome,
            meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            self.set_token_outcome = Some(AuthSetTokenOutcome {
                server: outcome.server.clone(),
                user: outcome.user.clone(),
                expires_at: outcome.expires_at,
            });
            self.set_token_auth_state = Some(meta.auth_state.clone());
            Ok(())
        }

        fn project_dump(
            &mut self,
            _outcome: &crate::render::DumpOutcome,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            Ok(())
        }

        fn project_dump_deleted(
            &mut self,
            _outcome: &crate::render::DumpDeleteOutcome,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            Ok(())
        }

        fn projects(
            &mut self,
            _view: &crate::render::ProjectListView,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            Ok(())
        }

        fn project_describe(
            &mut self,
            _project: &crate::model::ProjectDetail,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            Ok(())
        }

        fn data_models(
            &mut self,
            _view: &crate::render::DataModelListView,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            Ok(())
        }

        fn data_model_describe(
            &mut self,
            _detail: &crate::model::DataModelDetail,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            Ok(())
        }

        fn resource_types(
            &mut self,
            _view: &crate::render::ResourceTypeListView,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            Ok(())
        }

        fn resource_type_describe(
            &mut self,
            _detail: &crate::model::ResourceTypeDetail,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            unimplemented!("resource_type_describe not used in set-token tests")
        }

        fn data_model_structure(
            &mut self,
            _structure: &crate::model::DataModelStructure,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            unimplemented!("data_model_structure not used in set-token tests")
        }

        fn resources(
            &mut self,
            _view: &crate::render::ResourceListView,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            Ok(())
        }

        fn resource_describe(
            &mut self,
            _detail: &crate::model::ResourceDetail,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            Ok(())
        }
    }

    // ── helpers ────────────────────────────────────────────────────────────────

    /// Produce a minimal JWT with the given JSON payload.
    /// The secret is arbitrary — `extract_meta` disables signature validation.
    fn make_jwt(payload: &serde_json::Value) -> String {
        encode(
            &Header::new(Algorithm::HS256),
            payload,
            &EncodingKey::from_secret(b"unused"),
        )
        .expect("test JWT encoding should not fail")
    }

    fn fixed_expires() -> chrono::DateTime<Utc> {
        Utc.with_ymd_and_hms(2099, 6, 25, 12, 34, 56).unwrap()
    }

    fn fixed_past_expires() -> chrono::DateTime<Utc> {
        Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap()
    }

    fn make_cfg(server: &str) -> Config {
        Config {
            server: server.to_string(),
        }
    }

    const SERVER: &str = "https://api.test.dasch.swiss";

    // ── tests ──────────────────────────────────────────────────────────────────

    #[test]
    fn happy_path_full_jwt_caches_entry_and_notifies_renderer() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let token = make_jwt(&serde_json::json!({
            "sub": "http://rdfh.ch/users/root",
            "exp": fixed_expires().timestamp(),
        }));
        let cfg = make_cfg(SERVER);
        let client = MockDspClient::ok();
        let mut renderer = RecordingRenderer::new();

        run_impl(&token, &cfg, &client, &mut renderer, Some(&cache_path)).unwrap();

        // Cache assertions.
        let loaded = AuthCache::load_from(&cache_path).unwrap();
        assert_eq!(loaded.token(SERVER), Some(token.as_str()));
        assert_eq!(
            loaded.user(SERVER),
            Some("http://rdfh.ch/users/root"),
            "user should be set to the JWT sub claim"
        );
        assert!(
            loaded.acquired_at(SERVER).is_some(),
            "acquired_at should be set after set-token"
        );
        assert_eq!(
            loaded.expires_at(SERVER),
            Some(fixed_expires()),
            "expires_at should match the JWT exp claim"
        );

        // Renderer assertions.
        let outcome = renderer.set_token_outcome.unwrap();
        assert_eq!(outcome.server, SERVER);
        assert_eq!(outcome.user.as_deref(), Some("http://rdfh.ch/users/root"));
        assert_eq!(outcome.expires_at, Some(fixed_expires()));
        assert_eq!(
            renderer.set_token_auth_state.as_deref(),
            Some("authenticated as http://rdfh.ch/users/root"),
            "auth_state should use ADR-0007 vocabulary and include the sub IRI"
        );
    }

    #[test]
    fn space_padded_jwt_is_trimmed_before_decode_and_cache() {
        // A JWT pasted with surrounding whitespace must be trimmed for both the
        // empty-guard and the value forwarded onward, so it decodes and caches
        // cleanly rather than failing as "not a valid JWT". Goes through
        // `run_from_line` (the trim seam), not `run_impl`.
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let token = make_jwt(&serde_json::json!({
            "sub": "http://rdfh.ch/users/root",
            "exp": fixed_expires().timestamp(),
        }));
        let cfg = make_cfg(SERVER);
        let client = MockDspClient::ok();
        let mut renderer = RecordingRenderer::new();

        run_from_line(
            format!("  {token}  "),
            &cfg,
            &client,
            &mut renderer,
            Some(&cache_path),
        )
        .unwrap();

        let loaded = AuthCache::load_from(&cache_path).unwrap();
        assert_eq!(
            loaded.token(SERVER),
            Some(token.as_str()),
            "the cached token must be the whitespace-trimmed JWT"
        );
    }

    #[test]
    fn happy_path_sub_absent_caches_user_none() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        // JWT with exp but no sub.
        let token = make_jwt(&serde_json::json!({
            "exp": fixed_expires().timestamp(),
        }));
        let cfg = make_cfg(SERVER);
        let client = MockDspClient::ok();
        let mut renderer = RecordingRenderer::new();

        run_impl(&token, &cfg, &client, &mut renderer, Some(&cache_path)).unwrap();

        let loaded = AuthCache::load_from(&cache_path).unwrap();
        assert_eq!(
            loaded.user(SERVER),
            None,
            "user should be None when sub absent"
        );

        let outcome = renderer.set_token_outcome.unwrap();
        assert_eq!(
            outcome.user, None,
            "outcome.user should be None when sub absent"
        );
        assert_eq!(
            renderer.set_token_auth_state.as_deref(),
            Some("authenticated"),
            "auth_state should be 'authenticated' (no sub, ADR-0007 vocabulary)"
        );
    }

    #[test]
    fn probe_200_with_locally_expired_token_still_cached() {
        // Guards the "don't pre-check exp locally" invariant from the plan:
        // if the live probe returns Ok, we cache regardless of exp value.
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let token = make_jwt(&serde_json::json!({
            "sub": "http://rdfh.ch/users/root",
            "exp": fixed_past_expires().timestamp(),
        }));
        let cfg = make_cfg(SERVER);
        let client = MockDspClient::ok(); // probe returns 200
        let mut renderer = RecordingRenderer::new();

        // Must succeed and cache the token — exp is not locally enforced.
        run_impl(&token, &cfg, &client, &mut renderer, Some(&cache_path)).unwrap();

        let loaded = AuthCache::load_from(&cache_path).unwrap();
        assert_eq!(
            loaded.token(SERVER),
            Some(token.as_str()),
            "locally-expired token should still be cached when probe returns Ok"
        );
    }

    #[test]
    fn non_jwt_stdin_returns_usage_error_nothing_cached() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let cfg = make_cfg(SERVER);
        let client = MockDspClient::ok();
        let mut renderer = RecordingRenderer::new();

        let err =
            run_impl("not-a-jwt", &cfg, &client, &mut renderer, Some(&cache_path)).unwrap_err();
        assert!(
            matches!(err, Diagnostic::Usage(_)),
            "expected Usage diagnostic for non-JWT input, got {err:?}"
        );
        // Pin the distinct decode-failure message so it can't silently collapse
        // into the empty-guard message.
        assert!(
            err.to_string().contains("not a valid JWT"),
            "decode-failure message must contain 'not a valid JWT', got: {err}"
        );

        // Nothing should have been cached.
        let loaded = AuthCache::load_from(&cache_path).unwrap();
        assert_eq!(
            loaded.token(SERVER),
            None,
            "non-JWT input must not write anything to the cache"
        );
    }

    #[test]
    fn empty_stdin_returns_usage_no_token_nothing_cached() {
        // Guards the real empty-guard path in `run_from_line` (previously untested:
        // the old test called `run_impl("")` which hit the JWT-decode branch with a
        // DIFFERENT message — "not a valid JWT" — not the empty-guard path).
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let cfg = make_cfg(SERVER);
        let client = MockDspClient::ok();
        let mut renderer = RecordingRenderer::new();

        let err = run_from_line(
            String::new(),
            &cfg,
            &client,
            &mut renderer,
            Some(&cache_path),
        )
        .unwrap_err();
        assert!(
            matches!(err, Diagnostic::Usage(_)),
            "expected Usage for empty stdin line, got {err:?}"
        );
        assert!(
            err.to_string().contains("no token provided"),
            "empty-guard message must contain 'no token provided', got: {err}"
        );

        // Nothing should have been cached.
        let loaded = AuthCache::load_from(&cache_path).unwrap();
        assert_eq!(
            loaded.token(SERVER),
            None,
            "empty stdin must not write anything to the cache"
        );
    }

    #[test]
    fn newline_only_stdin_returns_usage_no_token() {
        // A bare newline from stdin (e.g. user pressed Enter) trims to empty string
        // and must hit the empty-guard path, not the JWT-decode path.
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let cfg = make_cfg(SERVER);
        let client = MockDspClient::ok();
        let mut renderer = RecordingRenderer::new();

        let err = run_from_line(
            "\n".to_string(),
            &cfg,
            &client,
            &mut renderer,
            Some(&cache_path),
        )
        .unwrap_err();
        assert!(
            matches!(err, Diagnostic::Usage(_)),
            "expected Usage for newline-only stdin line, got {err:?}"
        );
        assert!(
            err.to_string().contains("no token provided"),
            "newline-only stdin must hit the empty-guard path; message must contain 'no token provided', got: {err}"
        );
    }

    #[test]
    fn whitespace_only_stdin_returns_usage_no_token() {
        // A spaces-only line (no newline) must hit the empty-guard path.
        // Before the fix, `line.is_empty()` let "   " fall through to JWT-decode;
        // after the fix, `line.trim().is_empty()` catches it and returns the
        // "no token provided" message.
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let cfg = make_cfg(SERVER);
        let client = MockDspClient::ok();
        let mut renderer = RecordingRenderer::new();

        let err = run_from_line(
            "   ".to_string(),
            &cfg,
            &client,
            &mut renderer,
            Some(&cache_path),
        )
        .unwrap_err();
        assert!(
            matches!(err, Diagnostic::Usage(_)),
            "expected Usage for whitespace-only stdin, got {err:?}"
        );
        assert!(
            err.to_string().contains("no token provided"),
            "whitespace-only stdin must hit the empty-guard path; message must contain 'no token provided', got: {err}"
        );

        // Nothing should have been cached.
        let loaded = AuthCache::load_from(&cache_path).unwrap();
        assert_eq!(
            loaded.token(SERVER),
            None,
            "whitespace-only stdin must not write anything to the cache"
        );
    }

    #[test]
    fn probe_401_returns_auth_required_nothing_cached() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let token = make_jwt(&serde_json::json!({
            "sub": "http://rdfh.ch/users/root",
            "exp": fixed_expires().timestamp(),
        }));
        let cfg = make_cfg(SERVER);
        let client = MockDspClient::err(Diagnostic::AuthRequired(format!(
            "token rejected by {SERVER} — it may be expired, revoked, or for a different environment"
        )));
        let mut renderer = RecordingRenderer::new();

        let err = run_impl(&token, &cfg, &client, &mut renderer, Some(&cache_path)).unwrap_err();
        assert!(
            matches!(err, Diagnostic::AuthRequired(_)),
            "expected AuthRequired for probe 401, got {err:?}"
        );

        // Nothing should have been cached.
        let loaded = AuthCache::load_from(&cache_path).unwrap();
        assert_eq!(
            loaded.token(SERVER),
            None,
            "probe 401 must not write anything to the cache"
        );
    }

    #[test]
    fn probe_403_returns_auth_required_nothing_cached() {
        // At the action layer the mock injects an identical `Diagnostic::AuthRequired`
        // as the 401 test — this only proves AuthRequired propagates and nothing is
        // cached. The real 401-vs-403 HTTP-status distinction is covered by
        // `tests/set_token_http.rs`.
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let token = make_jwt(&serde_json::json!({
            "sub": "http://rdfh.ch/users/root",
            "exp": fixed_expires().timestamp(),
        }));
        let cfg = make_cfg(SERVER);
        let client = MockDspClient::err(Diagnostic::AuthRequired(format!(
            "token rejected by {SERVER} — it may be expired, revoked, or for a different environment"
        )));
        let mut renderer = RecordingRenderer::new();

        let err = run_impl(&token, &cfg, &client, &mut renderer, Some(&cache_path)).unwrap_err();
        assert!(
            matches!(err, Diagnostic::AuthRequired(_)),
            "expected AuthRequired for probe 403, got {err:?}"
        );

        let loaded = AuthCache::load_from(&cache_path).unwrap();
        assert_eq!(
            loaded.token(SERVER),
            None,
            "probe 403 must not write anything to the cache"
        );
    }

    #[test]
    fn probe_server_error_returns_server_error_nothing_cached() {
        // Closes the action-layer ServerError gap: when verify_token returns
        // Err(ServerError), run_impl must propagate it and leave the cache clean.
        // The wiremock test already covers the HTTP-level mapping; this test covers
        // the action layer independently.
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let token = make_jwt(&serde_json::json!({
            "sub": "http://rdfh.ch/users/root",
            "exp": fixed_expires().timestamp(),
        }));
        let cfg = make_cfg(SERVER);
        let client = MockDspClient::err(Diagnostic::ServerError(
            "server returned 500 Internal Server Error".to_string(),
        ));
        let mut renderer = RecordingRenderer::new();

        let err = run_impl(&token, &cfg, &client, &mut renderer, Some(&cache_path)).unwrap_err();
        assert!(
            matches!(err, Diagnostic::ServerError(_)),
            "expected ServerError when verify_token returns ServerError, got {err:?}"
        );

        // Nothing should have been cached.
        let loaded = AuthCache::load_from(&cache_path).unwrap();
        assert_eq!(
            loaded.token(SERVER),
            None,
            "server error must not write anything to the cache"
        );
    }

    #[test]
    fn probe_network_error_propagates_nothing_cached() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let token = make_jwt(&serde_json::json!({
            "sub": "http://rdfh.ch/users/root",
            "exp": fixed_expires().timestamp(),
        }));
        let cfg = make_cfg(SERVER);
        let client = MockDspClient::err(Diagnostic::Network("connection refused".to_string()));
        let mut renderer = RecordingRenderer::new();

        let err = run_impl(&token, &cfg, &client, &mut renderer, Some(&cache_path)).unwrap_err();
        assert!(
            matches!(err, Diagnostic::Network(_)),
            "expected Network diagnostic, got {err:?}"
        );

        let loaded = AuthCache::load_from(&cache_path).unwrap();
        assert_eq!(
            loaded.token(SERVER),
            None,
            "network failure must not write anything to the cache"
        );
    }

    #[test]
    fn probe_failure_does_not_overwrite_existing_entry() {
        // Guards that a pre-existing valid token is not replaced by a failed set-token.
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");

        // Pre-populate with a good entry.
        let mut cache = AuthCache::default();
        cache.set_entry(
            SERVER,
            ServerEntry {
                token: "existing-valid-tok".to_string(),
                user: Some("existing@user.test".to_string()),
                acquired_at: None,
                expires_at: None,
            },
        );
        cache.save_to(&cache_path).unwrap();

        let token = make_jwt(&serde_json::json!({
            "sub": "http://rdfh.ch/users/new",
            "exp": fixed_expires().timestamp(),
        }));
        let cfg = make_cfg(SERVER);
        let client = MockDspClient::err(Diagnostic::AuthRequired("rejected".to_string()));
        let mut renderer = RecordingRenderer::new();

        run_impl(&token, &cfg, &client, &mut renderer, Some(&cache_path)).unwrap_err();

        let loaded = AuthCache::load_from(&cache_path).unwrap();
        assert_eq!(
            loaded.token(SERVER),
            Some("existing-valid-tok"),
            "failed set-token must not overwrite an existing valid cache entry"
        );
    }
}