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
//! Actions for `dsp auth login`.
//!
//! Reads the user's password via a `PasswordSource` indirection so tests can
//! inject a static password without touching the real TTY. See Step 5 of the
//! implementation plan.

use std::path::Path;

use chrono::Utc;

use crate::actions::auth_state::read_auth_state;
use crate::cli::LoginArgs;
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::AuthLoginOutcome;
use crate::render::{MetaContext, Renderer};

// PasswordSource is a deliberate trait-injected seam for tests; keep it.
trait PasswordSource {
    fn read(&self, prompt: &str) -> Result<String, Diagnostic>;
}

struct TtyPasswordSource;

impl PasswordSource for TtyPasswordSource {
    fn read(&self, prompt: &str) -> Result<String, Diagnostic> {
        use std::io::IsTerminal;
        if std::io::stdin().is_terminal() {
            // rpassword opens /dev/tty itself. If that fails (sandboxed/container
            // env without /dev/tty), surface a user-actionable message rather
            // than Diagnostic::Internal.
            rpassword::prompt_password(prompt).map_err(|_| {
                Diagnostic::Usage(
                    "could not open terminal for password prompt; pipe the password via stdin instead".into(),
                )
            })
        } else {
            let mut line = String::new();
            // A read failure here (closed pipe, no data on stdin) is a bad
            // invocation, not a CLI bug — surface it as Usage rather than the
            // From<io::Error> default of Internal.
            std::io::stdin().read_line(&mut line).map_err(|e| {
                Diagnostic::Usage(format!("could not read password from stdin: {e}"))
            })?;
            // Strip a single trailing line terminator via the shared helper.
            // See `crate::actions::auth::trim_line_ending` for the rationale:
            // a bare trailing `\r` is NOT stripped (it may be a legitimate
            // password char), so we must not use `trim_end_matches`.
            crate::actions::auth::trim_line_ending(&mut line);
            Ok(line)
        }
    }
}

/// Resolve the password to use for login.
///
/// A non-empty `DSP_PASSWORD` value takes precedence over the interactive
/// prompt / stdin. The env value is passed in (not read here) so this stays
/// pure and unit-testable without mutating process env.
///
/// SECURITY: `DSP_PASSWORD` is a plaintext password, typically living in a
/// `.env` file on disk. Use it only for **local / dev / test** setups —
/// **never a production password**. For non-interactive use against real
/// environments, prefer a scoped, expiring token (`DSP_TOKEN`) over a
/// durable master credential. See ADR-0007.
fn resolve_password(
    env_password: Option<String>,
    source: &dyn PasswordSource,
) -> Result<String, Diagnostic> {
    match env_password {
        Some(p) if !p.is_empty() => Ok(p),
        _ => source.read("Password: "),
    }
}

/// Log in to a DSP server.
///
/// Authenticates with the DSP-API, stores the token in the auth cache, and
/// renders the outcome. Password resolution order: `DSP_PASSWORD` env var
/// (local/dev only — see [`resolve_password`]), then the TTY prompt, then
/// stdin when stdin is not a terminal (see ADR-0007).
pub fn run(
    args: &LoginArgs,
    cfg: &Config,
    client: &dyn DspClient,
    renderer: &mut dyn Renderer,
) -> Result<(), Diagnostic> {
    let env_password = std::env::var("DSP_PASSWORD").ok();
    run_impl(
        args,
        cfg,
        client,
        renderer,
        &TtyPasswordSource,
        env_password,
        None,
    )
}

/// Internal entry point that accepts an explicit cache path (for tests) and an
/// injectable `PasswordSource`. Production callers use `run`; tests use this
/// directly to inject a tempdir-backed cache path and a static password.
fn run_impl(
    args: &LoginArgs,
    cfg: &Config,
    client: &dyn DspClient,
    renderer: &mut dyn Renderer,
    password_source: &dyn PasswordSource,
    env_password: Option<String>,
    cache_path: Option<&Path>,
) -> Result<(), Diagnostic> {
    let user = args.user.as_deref().ok_or_else(|| {
        Diagnostic::Usage("--user (email, username, or IRI) is required for login".to_string())
    })?;

    let password = resolve_password(env_password, password_source)?;

    let response = client.login(&cfg.server, user, &password)?;

    let entry = ServerEntry {
        token: response.token.clone(),
        user: Some(response.user.clone()),
        acquired_at: Some(Utc::now()),
        expires_at: response.expires_at,
    };

    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()?,
    }

    // Build the ADR-0007 auth-state via the shared helper. After a successful
    // login the token is stored in the cache as a Cache-origin token with the
    // returned user name. Synthesize a Cache-origin ResolvedToken so that
    // `read_auth_state` picks the correct branch and looks up the user from the
    // cache (which now contains `response.user`).
    let resolved_for_meta = ResolvedToken {
        token: response.token.clone(),
        origin: TokenOrigin::Cache,
    };
    let meta = MetaContext {
        server_label: cfg.server.clone(),
        auth_state: read_auth_state(Some(&resolved_for_meta), &cache, &cfg.server),
        filter_warning: None,
    };

    let outcome = AuthLoginOutcome {
        server: cfg.server.clone(),
        user: response.user,
        expires_at: response.expires_at,
    };

    renderer.auth_login(&outcome, &meta)
}

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

    use super::{PasswordSource, resolve_password, run_impl};
    use crate::cli::{FormatArgs, LoginArgs};
    use crate::client::DspClient;
    use crate::config::{AuthCache, Config};
    use crate::diagnostic::Diagnostic;
    use crate::model::LoginResponse;
    use crate::render::auth::{
        AuthLoginOutcome, AuthLogoutOutcome, AuthSetTokenOutcome, AuthStatusOutcome,
    };
    use crate::render::{Format, MetaContext, Renderer};

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

    struct MockDspClient {
        result: Result<LoginResponse, Diagnostic>,
    }

    impl MockDspClient {
        fn ok(token: &str, user: &str, expires_at: Option<chrono::DateTime<Utc>>) -> Self {
            Self {
                result: Ok(LoginResponse {
                    token: token.to_string(),
                    user: user.to_string(),
                    expires_at,
                }),
            }
        }

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

    impl DspClient for MockDspClient {
        fn login(
            &self,
            _server: &str,
            _user: &str,
            _password: &str,
        ) -> Result<LoginResponse, Diagnostic> {
            self.result.clone()
        }

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

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

        fn get_project_dump_status(
            &self,
            _server: &str,
            _project_iri: &str,
            _dump_id: &str,
            _token: &str,
        ) -> Result<crate::model::DumpTask, Diagnostic> {
            unimplemented!("get_project_dump_status not used by login 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 login 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 login tests")
        }

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

        fn describe_project(
            &self,
            _server: &str,
            _project: &str,
            _token: Option<&str>,
        ) -> Result<crate::model::ProjectDetail, Diagnostic> {
            Err(Diagnostic::NotImplemented(
                "describe_project not used in login.rs tests".into(),
            ))
        }

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

        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 login 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 login 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 login 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 login 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 login tests")
        }

        fn verify_token(&self, _server: &str, _token: &str) -> Result<(), Diagnostic> {
            unimplemented!("verify_token not used by login tests")
        }
    }

    // ── static password source ────────────────────────────────────────────────

    struct StaticPasswordSource(String);

    impl PasswordSource for StaticPasswordSource {
        fn read(&self, _prompt: &str) -> Result<String, Diagnostic> {
            Ok(self.0.clone())
        }
    }

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

    struct RecordingRenderer {
        login_outcome: Option<AuthLoginOutcome>,
        login_auth_state: Option<String>,
    }

    impl RecordingRenderer {
        fn new() -> Self {
            Self {
                login_outcome: None,
                login_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> {
            self.login_outcome = Some(AuthLoginOutcome {
                server: outcome.server.clone(),
                user: outcome.user.clone(),
                expires_at: outcome.expires_at,
            });
            self.login_auth_state = Some(meta.auth_state.clone());
            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> {
            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 login tests")
        }

        fn data_model_structure(
            &mut self,
            _structure: &crate::model::DataModelStructure,
            _meta: &MetaContext,
        ) -> Result<(), Diagnostic> {
            unimplemented!("data_model_structure not used in login 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 fixed_expires() -> chrono::DateTime<Utc> {
        Utc.with_ymd_and_hms(2026, 6, 25, 12, 34, 56).unwrap()
    }

    fn make_args(server: &str) -> (LoginArgs, Config) {
        let args = LoginArgs {
            server: Some(server.to_string()),
            user: Some("u@x.test".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)
    }

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

    #[test]
    fn happy_path_stores_entry_in_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 client = MockDspClient::ok("tok-abc", "u@x.test", Some(fixed_expires()));
        let mut renderer = RecordingRenderer::new();
        let pw = StaticPasswordSource("hunter2".to_string());

        run_impl(
            &args,
            &cfg,
            &client,
            &mut renderer,
            &pw,
            None,
            Some(&cache_path),
        )
        .unwrap();

        let loaded = AuthCache::load_from(&cache_path).unwrap();
        assert_eq!(
            loaded.token("https://api.test.dasch.swiss"),
            Some("tok-abc")
        );
        assert_eq!(
            loaded.user("https://api.test.dasch.swiss"),
            Some("u@x.test")
        );
        assert_eq!(
            loaded.expires_at("https://api.test.dasch.swiss"),
            Some(fixed_expires())
        );
        assert!(
            loaded.acquired_at("https://api.test.dasch.swiss").is_some(),
            "acquired_at should be set to Some(Utc::now()) after login"
        );
    }

    #[test]
    fn happy_path_renderer_receives_correct_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 client = MockDspClient::ok("tok-abc", "u@x.test", Some(fixed_expires()));
        let mut renderer = RecordingRenderer::new();
        let pw = StaticPasswordSource("hunter2".to_string());

        run_impl(
            &args,
            &cfg,
            &client,
            &mut renderer,
            &pw,
            None,
            Some(&cache_path),
        )
        .unwrap();

        let outcome = renderer.login_outcome.unwrap();
        assert_eq!(outcome.server, "https://api.test.dasch.swiss");
        assert_eq!(outcome.user, "u@x.test");
        assert_eq!(outcome.expires_at, Some(fixed_expires()));
        // _meta.auth must reflect the post-login state using ADR-0007 vocabulary.
        assert_eq!(
            renderer.login_auth_state.as_deref(),
            Some("authenticated as u@x.test")
        );
    }

    #[test]
    fn error_auth_required_propagates_unchanged() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");
        let client = MockDspClient::err(Diagnostic::AuthRequired(
            "Authentication failed on https://api.test.dasch.swiss".into(),
        ));
        let mut renderer = RecordingRenderer::new();
        let pw = StaticPasswordSource("bad-pw".to_string());

        let err = run_impl(
            &args,
            &cfg,
            &client,
            &mut renderer,
            &pw,
            None,
            Some(&cache_path),
        )
        .unwrap_err();
        assert!(
            matches!(err, Diagnostic::AuthRequired(_)),
            "expected AuthRequired, got {err:?}"
        );
        // Must not include the username (ADR-0007 / PRD acceptance criterion 7).
        assert!(
            !err.to_string().contains("u@x.test"),
            "error message must not contain the username; got: {err}"
        );
    }

    #[test]
    fn error_network_propagates_unchanged() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");
        let client = MockDspClient::err(Diagnostic::Network("connection refused".into()));
        let mut renderer = RecordingRenderer::new();
        let pw = StaticPasswordSource("pw".to_string());

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

    #[test]
    fn error_server_error_propagates_unchanged() {
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");
        let client = MockDspClient::err(Diagnostic::ServerError("server returned 500".into()));
        let mut renderer = RecordingRenderer::new();
        let pw = StaticPasswordSource("pw".to_string());

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

    #[test]
    fn static_password_source_reaches_client() {
        // Verifies that the PasswordSource indirection works end-to-end:
        // a mock client that always succeeds combined with a static password
        // source must complete without error and store the expected entry.
        let dir = TempDir::new().unwrap();
        let cache_path = dir.path().join("auth.toml");
        let (args, cfg) = make_args("https://api.test.dasch.swiss");
        let client = MockDspClient::ok("tok-xyz", "u@x.test", None);
        let mut renderer = RecordingRenderer::new();
        let pw = StaticPasswordSource("hunter2".to_string());

        // Should complete without touching the real TTY.
        run_impl(
            &args,
            &cfg,
            &client,
            &mut renderer,
            &pw,
            None,
            Some(&cache_path),
        )
        .unwrap();

        let loaded = AuthCache::load_from(&cache_path).unwrap();
        assert_eq!(
            loaded.token("https://api.test.dasch.swiss"),
            Some("tok-xyz")
        );
    }

    #[test]
    fn resolve_password_prefers_nonempty_env_value() {
        let src = StaticPasswordSource("from-prompt".to_string());
        let pw = resolve_password(Some("from-env".to_string()), &src).unwrap();
        assert_eq!(
            pw, "from-env",
            "non-empty DSP_PASSWORD must win over the prompt"
        );
    }

    #[test]
    fn resolve_password_ignores_empty_env_value() {
        let src = StaticPasswordSource("from-prompt".to_string());
        let pw = resolve_password(Some(String::new()), &src).unwrap();
        assert_eq!(
            pw, "from-prompt",
            "an empty DSP_PASSWORD must fall through to the prompt"
        );
    }

    #[test]
    fn resolve_password_falls_through_when_env_absent() {
        let src = StaticPasswordSource("from-prompt".to_string());
        let pw = resolve_password(None, &src).unwrap();
        assert_eq!(pw, "from-prompt");
    }
}