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
//! Actions for `dsp auth status`.
//!
//! Reads the auth cache and reports whether a token is present and whether it
//! has expired. No network call is made — this is a pure cache inspection.

use std::path::Path;

use chrono::Utc;

use crate::actions::auth_state::read_auth_state;
use crate::cli::StatusArgs;
use crate::config::{AuthCache, Config, TokenOrigin, resolve_token};
use crate::diagnostic::Diagnostic;
use crate::render::auth::AuthStatusOutcome;
use crate::render::{MetaContext, Renderer};

/// Show authentication status for a DSP server.
///
/// Loads the auth cache, looks up the server, and renders the appropriate
/// outcome. Always returns `Ok(())` — missing/expired tokens are not errors.
pub fn run(args: &StatusArgs, cfg: &Config, renderer: &mut dyn Renderer) -> Result<(), Diagnostic> {
    let env_token = std::env::var("DSP_TOKEN").ok();
    run_impl(args, cfg, renderer, None, env_token)
}

fn run_impl(
    _args: &StatusArgs,
    cfg: &Config,
    renderer: &mut dyn Renderer,
    cache_path: Option<&Path>,
    env_token: Option<String>,
) -> Result<(), Diagnostic> {
    // ADR-0007 says a non-blank `DSP_TOKEN` wins regardless of cache state.
    // A corrupt or unreadable `auth.toml` therefore must not mask the env
    // token: treat a cache-load failure as an empty cache when the env token
    // would resolve. (Matches the trim-and-empty rule in `resolve_token`.)
    let env_token_would_win = env_token
        .as_deref()
        .map(str::trim)
        .map(|s| !s.is_empty())
        .unwrap_or(false);

    let cache_result = match cache_path {
        Some(p) => AuthCache::load_from(p),
        None => AuthCache::load(),
    };
    let cache = match cache_result {
        Ok(c) => c,
        Err(e) if env_token_would_win => {
            tracing::warn!(
                error = %e,
                "auth cache load failed; DSP_TOKEN is set, falling through to env token"
            );
            AuthCache::default()
        }
        Err(e) => return Err(e),
    };

    let (outcome, resolved_opt) = match resolve_token(env_token, &cache, &cfg.server) {
        Some(resolved) if resolved.origin == TokenOrigin::Env => {
            // do not log/format this binding — it is the raw bearer secret
            let token = resolved.token.clone();
            let now = Utc::now();
            // extract_exp is display-only (status makes no HTTP call) and never
            // gates access. It reads the JWT `exp` claim without validating the
            // signature, identical to the login path's cache-store behaviour.
            let expires_at = crate::client::jwt::extract_exp(&token);
            let expired = expires_at.map(|t| t < now).unwrap_or(false);
            (
                AuthStatusOutcome::AuthenticatedViaEnv {
                    server: cfg.server.clone(),
                    expires_at,
                    expired,
                },
                Some(resolved),
            )
        }
        Some(resolved) => {
            // Cache origin — read user/expires_at from cache directly (the
            // resolver only confirms origin; the full entry still comes from cache).
            let user = cache.user(&cfg.server).map(str::to_owned);
            let expires_at = cache.expires_at(&cfg.server);
            let expired = expires_at.map(|t| t < Utc::now()).unwrap_or(false);
            (
                AuthStatusOutcome::LoggedIn {
                    server: cfg.server.clone(),
                    user,
                    expires_at,
                    expired,
                },
                Some(resolved),
            )
        }
        None => (
            AuthStatusOutcome::NotLoggedIn {
                server: cfg.server.clone(),
            },
            None,
        ),
    };

    // _meta.auth uses presence/origin semantics (ADR-0007 uniform vocabulary),
    // independent of expiry. This is the same as the read commands: an expired
    // cached token still reports "authenticated as {user}" in _meta.auth.
    // The richer expiry/not-logged-in detail lives in the data output above.
    let auth_state = read_auth_state(resolved_opt.as_ref(), &cache, &cfg.server);

    let meta = MetaContext {
        server_label: cfg.server.clone(),
        auth_state,
        filter_warning: None,
    };

    renderer.auth_status(&outcome, &meta)?;
    Ok(())
}

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

    use super::run_impl;
    use crate::cli::{FormatArgs, StatusArgs};
    use crate::config::auth_cache::ServerEntry;
    use crate::config::{AuthCache, Config};
    use crate::diagnostic::Diagnostic;
    use crate::render::auth::{
        AuthLoginOutcome, AuthLogoutOutcome, AuthSetTokenOutcome, AuthStatusOutcome,
    };
    use crate::render::{Format, MetaContext, Renderer};

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

    struct LoggedInRecord {
        server: String,
        user: Option<String>,
        expires_at: Option<chrono::DateTime<Utc>>,
        expired: bool,
    }

    struct EnvRecord {
        server: String,
        expires_at: Option<chrono::DateTime<Utc>>,
        expired: bool,
    }

    struct RecordingRenderer {
        status_outcome: Option<LoggedInRecord>,
        env_outcome: Option<EnvRecord>,
        not_logged_in: Option<String>,
        last_auth_state: Option<String>,
    }

    impl RecordingRenderer {
        fn new() -> Self {
            Self {
                status_outcome: None,
                env_outcome: None,
                not_logged_in: None,
                last_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> {
            self.last_auth_state = Some(meta.auth_state.clone());
            match outcome {
                AuthStatusOutcome::LoggedIn {
                    server,
                    user,
                    expires_at,
                    expired,
                } => {
                    self.status_outcome = Some(LoggedInRecord {
                        server: server.clone(),
                        user: user.clone(),
                        expires_at: *expires_at,
                        expired: *expired,
                    });
                }
                AuthStatusOutcome::AuthenticatedViaEnv {
                    server,
                    expires_at,
                    expired,
                } => {
                    self.env_outcome = Some(EnvRecord {
                        server: server.clone(),
                        expires_at: *expires_at,
                        expired: *expired,
                    });
                }
                AuthStatusOutcome::NotLoggedIn { server } => {
                    self.not_logged_in = Some(server.clone());
                }
            }
            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> {
            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 status tests")
        }

        fn data_model_structure(
            &mut self,
            _structure: &crate::model::DataModelStructure,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            unimplemented!("data_model_structure not used in status 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 ───────────────────────────────────────────────────────────────

    fn make_args(server: &str) -> (StatusArgs, Config) {
        let args = StatusArgs {
            server: Some(server.to_string()),
            format: FormatArgs {
                format: Format::Prose,
                json: false,
                lines: false,
                columns: None,
                no_header: false,
                header_only: false,
            },
        };
        let cfg = Config {
            server: server.to_string(),
        };
        (args, cfg)
    }

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

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

    /// Produce a minimal JWT with the given JSON payload.
    /// The secret is arbitrary — `extract_exp` 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 make_jwt_with_exp(exp_ts: i64) -> String {
        make_jwt(&serde_json::json!({ "exp": exp_ts }))
    }

    // ── existing tests (regression guard) ────────────────────────────────────

    #[test]
    fn logged_in_entry_produces_logged_in_outcome() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");

        let mut cache = AuthCache::default();
        cache.set_entry(
            "https://api.test.dasch.swiss",
            ServerEntry {
                token: "tok-123".to_string(),
                user: Some("u@x.test".to_string()),
                acquired_at: None,
                expires_at: Some(fixed_future()),
            },
        );
        cache.save_to(&cache_path).unwrap();

        let mut renderer = RecordingRenderer::new();
        run_impl(&args, &cfg, &mut renderer, Some(&cache_path), None).unwrap();

        let rec = renderer.status_outcome.unwrap();
        assert_eq!(rec.server, "https://api.test.dasch.swiss");
        assert_eq!(rec.user.as_deref(), Some("u@x.test"));
        assert_eq!(rec.expires_at, Some(fixed_future()));
        assert!(
            !rec.expired,
            "token with future expiry should not be expired"
        );
    }

    #[test]
    fn empty_cache_produces_not_logged_in_outcome() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");

        let mut renderer = RecordingRenderer::new();
        run_impl(&args, &cfg, &mut renderer, Some(&cache_path), None).unwrap();

        assert!(
            renderer.status_outcome.is_none(),
            "expected no LoggedIn outcome"
        );
        assert_eq!(
            renderer.not_logged_in.as_deref(),
            Some("https://api.test.dasch.swiss")
        );
    }

    #[test]
    fn expired_entry_produces_logged_in_with_expired_true() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");

        let mut cache = AuthCache::default();
        cache.set_entry(
            "https://api.test.dasch.swiss",
            ServerEntry {
                token: "old-tok".to_string(),
                user: Some("u@x.test".to_string()),
                acquired_at: None,
                expires_at: Some(fixed_past()),
            },
        );
        cache.save_to(&cache_path).unwrap();

        let mut renderer = RecordingRenderer::new();
        run_impl(&args, &cfg, &mut renderer, Some(&cache_path), None).unwrap();

        let rec = renderer.status_outcome.unwrap();
        assert!(rec.expired, "token with past expiry should be expired");
    }

    #[test]
    fn run_always_returns_ok_even_for_not_logged_in() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");

        let mut renderer = RecordingRenderer::new();
        let result = run_impl(&args, &cfg, &mut renderer, Some(&cache_path), None);
        assert!(
            result.is_ok(),
            "status should always return Ok; got {result:?}"
        );
    }

    // ── env-token tests ───────────────────────────────────────────────────────

    #[test]
    fn env_jwt_future_exp_produces_authenticated_via_env_not_expired() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");

        // far-future exp: year 2099 ≈ Unix 4070908800
        let exp_ts = fixed_future().timestamp();
        let token = make_jwt_with_exp(exp_ts);

        let mut renderer = RecordingRenderer::new();
        run_impl(&args, &cfg, &mut renderer, Some(&cache_path), Some(token)).unwrap();

        let rec = renderer
            .env_outcome
            .expect("expected AuthenticatedViaEnv outcome");
        assert_eq!(rec.server, "https://api.test.dasch.swiss");
        assert!(rec.expires_at.is_some(), "expected Some(expires_at)");
        assert!(!rec.expired, "future exp should not be expired");
        assert_eq!(
            renderer.last_auth_state.as_deref(),
            Some("authenticated via DSP_TOKEN"),
            "_meta.auth must use ADR-0007 presence/origin vocabulary (not expiry-aware)"
        );
    }

    #[test]
    fn env_jwt_past_exp_produces_authenticated_via_env_expired() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");

        let exp_ts = fixed_past().timestamp();
        let token = make_jwt_with_exp(exp_ts);

        let mut renderer = RecordingRenderer::new();
        run_impl(&args, &cfg, &mut renderer, Some(&cache_path), Some(token)).unwrap();

        let rec = renderer
            .env_outcome
            .expect("expected AuthenticatedViaEnv outcome");
        assert!(rec.expired, "past exp should be expired");
        // _meta.auth uses presence/origin semantics — expired env token still
        // reports "authenticated via DSP_TOKEN", not an expiry string.
        assert_eq!(
            renderer.last_auth_state.as_deref(),
            Some("authenticated via DSP_TOKEN"),
            "_meta.auth for expired env token must be 'authenticated via DSP_TOKEN'"
        );
        // The data output (AuthenticatedViaEnv.expired) correctly reflects the expiry.
        assert!(rec.expired, "data output must still report expired==true");
    }

    #[test]
    fn env_non_jwt_produces_authenticated_via_env_expiry_unknown() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");

        let mut renderer = RecordingRenderer::new();
        run_impl(
            &args,
            &cfg,
            &mut renderer,
            Some(&cache_path),
            Some("not-a-jwt".to_string()),
        )
        .unwrap();

        let rec = renderer
            .env_outcome
            .expect("expected AuthenticatedViaEnv outcome");
        assert!(
            rec.expires_at.is_none(),
            "non-JWT env token should have expires_at == None"
        );
        assert!(!rec.expired, "non-JWT env token should not be expired");
        assert_eq!(
            renderer.last_auth_state.as_deref(),
            Some("authenticated via DSP_TOKEN"),
            "_meta.auth for non-JWT env token must be 'authenticated via DSP_TOKEN'"
        );
    }

    #[test]
    fn env_token_wins_over_valid_cache_entry() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");

        // Populate cache with a valid entry for the same server.
        let mut cache = AuthCache::default();
        cache.set_entry(
            "https://api.test.dasch.swiss",
            ServerEntry {
                token: "cache-tok".to_string(),
                user: Some("u@cache.test".to_string()),
                acquired_at: None,
                expires_at: Some(fixed_future()),
            },
        );
        cache.save_to(&cache_path).unwrap();

        let exp_ts = fixed_future().timestamp();
        let env_token = make_jwt_with_exp(exp_ts);

        let mut renderer = RecordingRenderer::new();
        run_impl(
            &args,
            &cfg,
            &mut renderer,
            Some(&cache_path),
            Some(env_token),
        )
        .unwrap();

        // Env wins: must be AuthenticatedViaEnv, not LoggedIn.
        assert!(
            renderer.env_outcome.is_some(),
            "env token should win over valid cache entry"
        );
        assert!(
            renderer.status_outcome.is_none(),
            "LoggedIn should not be produced when env token is present"
        );
        assert_eq!(
            renderer.last_auth_state.as_deref(),
            Some("authenticated via DSP_TOKEN"),
            "env win over valid cache should report 'authenticated via DSP_TOKEN'"
        );
    }

    #[test]
    fn env_token_wins_over_expired_cache_entry() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");

        // Populate cache with an expired entry.
        let mut cache = AuthCache::default();
        cache.set_entry(
            "https://api.test.dasch.swiss",
            ServerEntry {
                token: "old-cache-tok".to_string(),
                user: Some("u@cache.test".to_string()),
                acquired_at: None,
                expires_at: Some(fixed_past()),
            },
        );
        cache.save_to(&cache_path).unwrap();

        let exp_ts = fixed_future().timestamp();
        let env_token = make_jwt_with_exp(exp_ts);

        let mut renderer = RecordingRenderer::new();
        run_impl(
            &args,
            &cfg,
            &mut renderer,
            Some(&cache_path),
            Some(env_token),
        )
        .unwrap();

        assert!(
            renderer.env_outcome.is_some(),
            "env token should win over expired cache entry"
        );
        assert!(
            renderer.status_outcome.is_none(),
            "LoggedIn should not be produced when env token is present"
        );
        assert_eq!(
            renderer.last_auth_state.as_deref(),
            Some("authenticated via DSP_TOKEN"),
            "env win over expired cache should report 'authenticated via DSP_TOKEN'"
        );
    }

    #[test]
    fn whitespace_only_env_with_expired_cache_falls_through_to_cache() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");

        let mut cache = AuthCache::default();
        cache.set_entry(
            "https://api.test.dasch.swiss",
            ServerEntry {
                token: "old-tok".to_string(),
                user: Some("u@x.test".to_string()),
                acquired_at: None,
                expires_at: Some(fixed_past()),
            },
        );
        cache.save_to(&cache_path).unwrap();

        let mut renderer = RecordingRenderer::new();
        run_impl(
            &args,
            &cfg,
            &mut renderer,
            Some(&cache_path),
            Some("  ".to_string()), // whitespace-only → treated as absent
        )
        .unwrap();

        // Falls through to cache: must be LoggedIn with expired==true.
        let rec = renderer
            .status_outcome
            .expect("expected LoggedIn outcome from cache fall-through");
        assert!(
            rec.expired,
            "cache entry has past expiry; expired should be true"
        );
        assert!(
            renderer.env_outcome.is_none(),
            "whitespace env should not produce AuthenticatedViaEnv"
        );
        // _meta.auth uses presence/origin semantics — expired cached token with
        // a known user still reports "authenticated as {user}", not an expiry string.
        // The expiry detail lives in the data output (LoggedIn.expired == true).
        assert_eq!(
            renderer.last_auth_state.as_deref(),
            Some("authenticated as u@x.test"),
            "_meta.auth for expired cache token must be 'authenticated as <user>'"
        );
    }

    #[test]
    fn env_token_wins_when_cache_is_corrupt() {
        // ADR-0007: DSP_TOKEN wins regardless of cache state. A corrupt
        // auth.toml must not mask the env token in `dsp auth status`.
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        std::fs::write(&cache_path, b"not valid toml [[[").unwrap();

        let (args, cfg) = make_args("https://api.test.dasch.swiss");
        let exp_ts = fixed_future().timestamp();
        let env_token = make_jwt_with_exp(exp_ts);

        let mut renderer = RecordingRenderer::new();
        run_impl(
            &args,
            &cfg,
            &mut renderer,
            Some(&cache_path),
            Some(env_token),
        )
        .unwrap();

        assert!(
            renderer.env_outcome.is_some(),
            "env token should win over corrupt cache"
        );
        assert!(
            renderer.status_outcome.is_none(),
            "LoggedIn should not be produced when env token is present"
        );
        assert_eq!(
            renderer.last_auth_state.as_deref(),
            Some("authenticated via DSP_TOKEN"),
        );
    }

    #[test]
    fn corrupt_cache_without_env_token_still_errors() {
        // Regression guard for the other side of the env-wins fix: when no env
        // token is set, a corrupt cache must still propagate the error rather
        // than silently producing `not_logged_in`. The user needs to know the
        // cache is broken so they can fix or delete it.
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        std::fs::write(&cache_path, b"not valid toml [[[").unwrap();

        let (args, cfg) = make_args("https://api.test.dasch.swiss");
        let mut renderer = RecordingRenderer::new();
        let result = run_impl(&args, &cfg, &mut renderer, Some(&cache_path), None);
        assert!(
            result.is_err(),
            "without env token, a corrupt cache should propagate; got {result:?}"
        );
    }

    #[test]
    fn whitespace_only_env_with_corrupt_cache_still_errors() {
        // Mirrors the cache-fall-through whitespace test: a blank env token
        // must not be enough to swallow the cache-load error. The trim rule
        // here matches `resolve_token`'s blank-handling.
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        std::fs::write(&cache_path, b"not valid toml [[[").unwrap();

        let (args, cfg) = make_args("https://api.test.dasch.swiss");
        let mut renderer = RecordingRenderer::new();
        let result = run_impl(
            &args,
            &cfg,
            &mut renderer,
            Some(&cache_path),
            Some("  ".to_string()),
        );
        assert!(
            result.is_err(),
            "whitespace env token should not swallow corrupt-cache error; got {result:?}"
        );
    }

    #[test]
    fn absent_env_with_cache_entry_produces_logged_in() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");

        let mut cache = AuthCache::default();
        cache.set_entry(
            "https://api.test.dasch.swiss",
            ServerEntry {
                token: "tok-abc".to_string(),
                user: Some("u@x.test".to_string()),
                acquired_at: None,
                expires_at: Some(fixed_future()),
            },
        );
        cache.save_to(&cache_path).unwrap();

        let mut renderer = RecordingRenderer::new();
        run_impl(&args, &cfg, &mut renderer, Some(&cache_path), None).unwrap();

        assert!(
            renderer.status_outcome.is_some(),
            "absent env + cache entry should produce LoggedIn"
        );
        assert!(
            renderer.env_outcome.is_none(),
            "absent env should not produce AuthenticatedViaEnv"
        );
    }
}